-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
lwm2m_rw_senml_cbor.c
1126 lines (873 loc) · 25.9 KB
/
lwm2m_rw_senml_cbor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_MODULE_NAME net_lwm2m_senml_cbor
#define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <ctype.h>
#include <time.h>
#include <zephyr/sys/util.h>
#include <zephyr/kernel.h>
#include <zcbor_common.h>
#include <zcbor_decode.h>
#include <zcbor_encode.h>
#include "lwm2m_engine.h"
#include "lwm2m_object.h"
#include "lwm2m_rw_senml_cbor.h"
#include "lwm2m_senml_cbor_decode.h"
#include "lwm2m_senml_cbor_encode.h"
#include "lwm2m_senml_cbor_types.h"
#include "lwm2m_util.h"
#define SENML_MAX_NAME_SIZE sizeof("/65535/65535/")
struct cbor_out_fmt_data {
/* Data */
struct lwm2m_senml input;
/* Storage for basenames and names ~ sizeof("/65535/65535/") */
struct {
char names[CONFIG_LWM2M_RW_SENML_CBOR_RECORDS][SENML_MAX_NAME_SIZE];
size_t name_sz; /* Name buff size */
uint8_t name_cnt;
};
/* Basetime for Cached data timestamp */
time_t basetime;
/* Storage for object links */
struct {
char objlnk[CONFIG_LWM2M_RW_SENML_CBOR_RECORDS][sizeof("65535:65535")];
size_t objlnk_sz; /* Object link buff size */
uint8_t objlnk_cnt;
};
};
struct cbor_in_fmt_data {
/* Decoded data */
struct lwm2m_senml dcd; /* Decoded data */
struct record *current;
char basename[MAX_RESOURCE_LEN + 1]; /* Null terminated basename */
};
/* Statically allocated formatter data is shared between different threads */
static union cbor_io_fmt_data{
struct cbor_in_fmt_data i;
struct cbor_out_fmt_data o;
} fdio;
static int path_to_string(char *buf, size_t buf_size, const struct lwm2m_obj_path *input,
int level_max);
/*
* SEND is called from a different context than the rest of the LwM2M functionality
*/
K_MUTEX_DEFINE(fd_mtx);
#define GET_CBOR_FD_NAME(fd) ((fd)->names[(fd)->name_cnt])
/* Get the current record */
#define GET_CBOR_FD_REC(fd) \
&((fd)->input.lwm2m_senml_record_m[(fd)->input.lwm2m_senml_record_m_count])
/* Get a record */
#define GET_IN_FD_REC_I(fd, i) &((fd)->dcd.lwm2m_senml_record_m[i])
/* Consume the current record */
#define CONSUME_CBOR_FD_REC(fd) \
&((fd)->input.lwm2m_senml_record_m[(fd)->input.lwm2m_senml_record_m_count++])
/* Get CBOR output formatter data */
#define LWM2M_OFD_CBOR(octx) ((struct cbor_out_fmt_data *)engine_get_out_user_data(octx))
static void setup_out_fmt_data(struct lwm2m_message *msg)
{
k_mutex_lock(&fd_mtx, K_FOREVER);
struct cbor_out_fmt_data *fd = &fdio.o;
(void)memset(fd, 0, sizeof(*fd));
engine_set_out_user_data(&msg->out, fd);
fd->name_sz = SENML_MAX_NAME_SIZE;
fd->basetime = 0;
fd->objlnk_sz = sizeof("65535:65535");
}
static void clear_out_fmt_data(struct lwm2m_message *msg)
{
engine_clear_out_user_data(&msg->out);
k_mutex_unlock(&fd_mtx);
}
static void setup_in_fmt_data(struct lwm2m_message *msg)
{
k_mutex_lock(&fd_mtx, K_FOREVER);
struct cbor_in_fmt_data *fd = &fdio.i;
(void)memset(fd, 0, sizeof(*fd));
engine_set_in_user_data(&msg->in, fd);
}
static void clear_in_fmt_data(struct lwm2m_message *msg)
{
engine_clear_in_user_data(&msg->in);
k_mutex_unlock(&fd_mtx);
}
static int fmt_range_check(struct cbor_out_fmt_data *fd)
{
if (fd->name_cnt >= CONFIG_LWM2M_RW_SENML_CBOR_RECORDS ||
fd->objlnk_cnt >= CONFIG_LWM2M_RW_SENML_CBOR_RECORDS ||
fd->input.lwm2m_senml_record_m_count >= CONFIG_LWM2M_RW_SENML_CBOR_RECORDS) {
LOG_ERR("CONFIG_LWM2M_RW_SENML_CBOR_RECORDS too small");
return -ENOMEM;
}
return 0;
}
static int put_basename(struct lwm2m_output_context *out, struct lwm2m_obj_path *path)
{
struct cbor_out_fmt_data *fd = LWM2M_OFD_CBOR(out);
int len;
int ret;
ret = fmt_range_check(fd);
if (ret < 0) {
return ret;
}
char *basename = GET_CBOR_FD_NAME(fd);
len = path_to_string(basename, fd->name_sz, path, LWM2M_PATH_LEVEL_OBJECT_INST);
if (len < 0) {
return len;
}
/* Tell CBOR encoder where to find the name */
struct record *record = GET_CBOR_FD_REC(fd);
record->record_bn.record_bn.value = basename;
record->record_bn.record_bn.len = len;
record->record_bn_present = 1;
if ((len < sizeof("/0/0") - 1) || (len >= SENML_MAX_NAME_SIZE)) {
__ASSERT_NO_MSG(false);
return -EINVAL;
}
fd->name_cnt++;
return 0;
}
static int put_empty_array(struct lwm2m_output_context *out)
{
int len = 1;
memset(CPKT_BUF_W_PTR(out->out_cpkt), 0x80, len); /* 80 # array(0) */
out->out_cpkt->offset += len;
return len;
}
static int put_end(struct lwm2m_output_context *out, struct lwm2m_obj_path *path)
{
size_t len;
struct lwm2m_senml *input = &(LWM2M_OFD_CBOR(out)->input);
if (!input->lwm2m_senml_record_m_count) {
len = put_empty_array(out);
return len;
}
uint_fast8_t ret =
cbor_encode_lwm2m_senml(CPKT_BUF_W_REGION(out->out_cpkt), input, &len);
if (ret != ZCBOR_SUCCESS) {
LOG_ERR("unable to encode senml cbor msg");
return -E2BIG;
}
out->out_cpkt->offset += len;
return len;
}
static int put_begin_oi(struct lwm2m_output_context *out, struct lwm2m_obj_path *path)
{
int ret;
uint8_t tmp = path->level;
/* In case path level is set to 'none' or 'object' and we have only default oi */
path->level = LWM2M_PATH_LEVEL_OBJECT_INST;
ret = put_basename(out, path);
path->level = tmp;
return ret;
}
static int put_begin_r(struct lwm2m_output_context *out, struct lwm2m_obj_path *path)
{
struct cbor_out_fmt_data *fd = LWM2M_OFD_CBOR(out);
int len;
int ret;
ret = fmt_range_check(fd);
if (ret < 0) {
return ret;
}
char *name = GET_CBOR_FD_NAME(fd);
/* Write resource name */
len = snprintk(name, sizeof("65535"), "%" PRIu16 "", path->res_id);
if (len < sizeof("0") - 1) {
__ASSERT_NO_MSG(false);
return -EINVAL;
}
/* Check if we could use an already existing name
* -> latest name slot is used as a scratchpad
*/
for (int idx = 0; idx < fd->name_cnt; idx++) {
if (strncmp(name, fd->names[idx], len) == 0) {
name = fd->names[idx];
break;
}
}
/* Tell CBOR encoder where to find the name */
struct record *record = GET_CBOR_FD_REC(fd);
record->record_n.record_n.value = name;
record->record_n.record_n.len = len;
record->record_n_present = 1;
/* Makes possible to use same slot for storing r/ri name combination.
* No need to increase the name count if an existing name has been used
*/
if (path->level < LWM2M_PATH_LEVEL_RESOURCE_INST && name == GET_CBOR_FD_NAME(fd)) {
fd->name_cnt++;
}
return 0;
}
static int put_data_timestamp(struct lwm2m_output_context *out, time_t value)
{
struct record *out_record;
struct cbor_out_fmt_data *fd = LWM2M_OFD_CBOR(out);
int ret;
ret = fmt_range_check(fd);
if (ret < 0) {
return ret;
}
/* Tell CBOR encoder where to find the name */
out_record = GET_CBOR_FD_REC(fd);
if (fd->basetime) {
out_record->record_t.record_t = value - fd->basetime;
out_record->record_t_present = 1;
} else {
fd->basetime = value;
out_record->record_bt.record_bt = value;
out_record->record_bt_present = 1;
}
return 0;
}
static int put_begin_ri(struct lwm2m_output_context *out, struct lwm2m_obj_path *path)
{
struct cbor_out_fmt_data *fd = LWM2M_OFD_CBOR(out);
char *name = GET_CBOR_FD_NAME(fd);
struct record *record = GET_CBOR_FD_REC(fd);
int ret;
ret = fmt_range_check(fd);
if (ret < 0) {
return ret;
}
/* Forms name from resource id and resource instance id */
int len = snprintk(name, SENML_MAX_NAME_SIZE,
"%" PRIu16 "/%" PRIu16 "",
path->res_id, path->res_inst_id);
if (len < sizeof("0/0") - 1) {
__ASSERT_NO_MSG(false);
return -EINVAL;
}
/* Check if we could use an already existing name
* -> latest name slot is used as a scratchpad
*/
for (int idx = 0; idx < fd->name_cnt; idx++) {
if (strncmp(name, fd->names[idx], len) == 0) {
name = fd->names[idx];
break;
}
}
/* Tell CBOR encoder where to find the name */
record->record_n.record_n.value = name;
record->record_n.record_n.len = len;
record->record_n_present = 1;
/* No need to increase the name count if an existing name has been used */
if (name == GET_CBOR_FD_NAME(fd)) {
fd->name_cnt++;
}
return 0;
}
static int put_name_nth_ri(struct lwm2m_output_context *out, struct lwm2m_obj_path *path)
{
int ret = 0;
struct cbor_out_fmt_data *fd = LWM2M_OFD_CBOR(out);
struct record *record = GET_CBOR_FD_REC(fd);
/* With the first ri the resource name (and ri name) are already in place*/
if (path->res_inst_id > 0) {
ret = put_begin_ri(out, path);
} else if (record && record->record_t_present) {
/* Name need to be add for each time serialized record */
ret = put_begin_r(out, path);
}
return ret;
}
static int put_value(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, int64_t value)
{
int ret = put_name_nth_ri(out, path);
if (ret < 0) {
return ret;
}
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */
record->record_union.record_union_choice = union_vi_c;
record->record_union.union_vi = value;
record->record_union_present = 1;
return 0;
}
static int put_s8(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, int8_t value)
{
return put_value(out, path, value);
}
static int put_s16(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, int16_t value)
{
return put_value(out, path, value);
}
static int put_s32(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, int32_t value)
{
return put_value(out, path, value);
}
static int put_s64(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, int64_t value)
{
return put_value(out, path, value);
}
static int put_time(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, time_t value)
{
int ret = put_name_nth_ri(out, path);
if (ret < 0) {
return ret;
}
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */
record->record_union.record_union_choice = union_vi_c;
record->record_union.union_vi = (int64_t)value;
record->record_union_present = 1;
return 0;
}
static int put_float(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, double *value)
{
int ret = put_name_nth_ri(out, path);
if (ret < 0) {
return ret;
}
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */
record->record_union.record_union_choice = union_vf_c;
record->record_union.union_vf = *value;
record->record_union_present = 1;
return 0;
}
static int put_string(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, char *buf,
size_t buflen)
{
int ret = put_name_nth_ri(out, path);
if (ret < 0) {
return ret;
}
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */
record->record_union.record_union_choice = union_vs_c;
record->record_union.union_vs.value = buf;
record->record_union.union_vs.len = buflen;
record->record_union_present = 1;
return 0;
}
static int put_bool(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, bool value)
{
int ret = put_name_nth_ri(out, path);
if (ret < 0) {
return ret;
}
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */
record->record_union.record_union_choice = union_vb_c;
record->record_union.union_vb = value;
record->record_union_present = 1;
return 0;
}
static int put_opaque(struct lwm2m_output_context *out, struct lwm2m_obj_path *path, char *buf,
size_t buflen)
{
int ret = put_name_nth_ri(out, path);
if (ret < 0) {
return ret;
}
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */
record->record_union.record_union_choice = union_vd_c;
record->record_union.union_vd.value = buf;
record->record_union.union_vd.len = buflen;
record->record_union_present = 1;
return 0;
}
static int put_objlnk(struct lwm2m_output_context *out, struct lwm2m_obj_path *path,
struct lwm2m_objlnk *value)
{
int ret = 0;
struct cbor_out_fmt_data *fd = LWM2M_OFD_CBOR(out);
ret = fmt_range_check(fd);
if (ret < 0) {
return ret;
}
/* Format object link */
int objlnk_idx = fd->objlnk_cnt;
char *objlink_buf = fd->objlnk[objlnk_idx];
int objlnk_len =
snprintk(objlink_buf, fd->objlnk_sz, "%u:%u", value->obj_id, value->obj_inst);
if (objlnk_len < 0) {
return -EINVAL;
}
ret = put_name_nth_ri(out, path);
if (ret < 0) {
return ret;
}
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */
record->record_union.record_union_choice = union_vlo_c;
record->record_union.union_vlo.value = objlink_buf;
record->record_union.union_vlo.len = objlnk_len;
record->record_union_present = 1;
fd->objlnk_cnt++;
return 0;
}
static int get_opaque(struct lwm2m_input_context *in,
uint8_t *value, size_t buflen,
struct lwm2m_opaque_context *opaque,
bool *last_block)
{
struct cbor_in_fmt_data *fd;
uint8_t *dest = NULL;
/* Get the CBOR header only on first read. */
if (opaque->remaining == 0) {
fd = engine_get_in_user_data(in);
if (!fd || !fd->current) {
return -EINVAL;
}
opaque->len = fd->current->record_union.union_vd.len;
if (buflen < opaque->len) {
LOG_DBG("Write opaque failed, no buffer space");
return -ENOMEM;
}
dest = memcpy(value, fd->current->record_union.union_vd.value, opaque->len);
*last_block = true;
} else {
LOG_DBG("Blockwise transfer not supported with SenML CBOR");
__ASSERT_NO_MSG(false);
}
return dest ? opaque->len : -EINVAL;
}
static int get_s32(struct lwm2m_input_context *in, int32_t *value)
{
struct cbor_in_fmt_data *fd;
fd = engine_get_in_user_data(in);
if (!fd || !fd->current) {
return -EINVAL;
}
*value = fd->current->record_union.union_vi;
fd->current = NULL;
return 0;
}
static int get_s64(struct lwm2m_input_context *in, int64_t *value)
{
struct cbor_in_fmt_data *fd;
fd = engine_get_in_user_data(in);
if (!fd || !fd->current) {
return -EINVAL;
}
*value = fd->current->record_union.union_vi;
fd->current = NULL;
return 0;
}
static int get_time(struct lwm2m_input_context *in, time_t *value)
{
int64_t temp64;
int ret;
ret = get_s64(in, &temp64);
if (ret == 0) {
*value = (time_t)temp64;
}
return ret;
}
static int get_float(struct lwm2m_input_context *in, double *value)
{
struct cbor_in_fmt_data *fd;
fd = engine_get_in_user_data(in);
if (!fd || !fd->current) {
return -EINVAL;
}
switch (fd->current->record_union.record_union_choice) {
case union_vi_c:
*value = (double)fd->current->record_union.union_vi;
break;
case union_vf_c:
*value = fd->current->record_union.union_vf;
break;
default:
return -EINVAL;
}
fd->current = NULL;
return 0;
}
static int get_string(struct lwm2m_input_context *in, uint8_t *buf, size_t buflen)
{
struct cbor_in_fmt_data *fd;
int len;
fd = engine_get_in_user_data(in);
if (!fd || !fd->current) {
return -EINVAL;
}
len = MIN(buflen-1, fd->current->record_union.union_vs.len);
memcpy(buf, fd->current->record_union.union_vs.value, len);
buf[len] = '\0';
fd->current = NULL;
return 0;
}
static int get_objlnk(struct lwm2m_input_context *in,
struct lwm2m_objlnk *value)
{
char objlnk[sizeof("65535:65535")] = {0};
unsigned long id;
int ret;
ret = get_string(in, objlnk, sizeof(objlnk));
if (ret < 0) {
return ret;
}
value->obj_id = LWM2M_OBJLNK_MAX_ID;
value->obj_inst = LWM2M_OBJLNK_MAX_ID;
char *end;
char *idp = objlnk;
for (int idx = 0; idx < 2; idx++) {
errno = 0;
id = strtoul(idp, &end, 10);
idp = end + 1;
if ((id == 0 && errno == ERANGE) || id > 65535) {
LOG_WRN("decoded id %lu out of range[0..65535]", id);
return -EBADMSG;
}
switch (idx) {
case 0:
value->obj_id = id;
continue;
case 1:
value->obj_inst = id;
continue;
}
}
if (value->obj_inst != LWM2M_OBJLNK_MAX_ID && (value->obj_id == LWM2M_OBJLNK_MAX_ID)) {
LOG_WRN("decoded obj inst id without obj id");
return -EBADMSG;
}
return ret;
}
static int get_bool(struct lwm2m_input_context *in, bool *value)
{
struct cbor_in_fmt_data *fd;
fd = engine_get_in_user_data(in);
if (!fd || !fd->current) {
return -EINVAL;
}
*value = fd->current->record_union.union_vb;
fd->current = NULL;
return 0;
}
static int do_write_op_item(struct lwm2m_message *msg, struct record *rec)
{
struct lwm2m_engine_obj_inst *obj_inst = NULL;
struct lwm2m_engine_obj_field *obj_field;
struct lwm2m_engine_res *res = NULL;
struct lwm2m_engine_res_inst *res_inst = NULL;
int ret;
uint8_t created = 0U;
struct cbor_in_fmt_data *fd;
/* Composite op - name with basename */
char name[SENML_MAX_NAME_SIZE] = { 0 }; /* Null terminated name */
int len = 0;
/* Compiler requires reserving space for full length basename and name even though those two
* combined do not exceed MAX_RESOURCE_LEN
*/
char fqn[MAX_RESOURCE_LEN + SENML_MAX_NAME_SIZE + 1] = {0};
fd = engine_get_in_user_data(&msg->in);
if (!fd) {
return -EINVAL;
}
/* If there's no name then the basename forms the path */
if (rec->record_n_present) {
len = MIN(sizeof(name) - 1, rec->record_n.record_n.len);
snprintk(name, len + 1, "%s", rec->record_n.record_n.value);
}
/* Form fully qualified path name */
snprintk(fqn, sizeof(fqn), "%s%s", fd->basename, name);
/* Set path on record basis */
ret = lwm2m_string_to_path(fqn, &msg->path, '/');
if (ret < 0) {
__ASSERT_NO_MSG(false);
return ret;
}
fd->current = rec;
ret = lwm2m_get_or_create_engine_obj(msg, &obj_inst, &created);
if (ret < 0) {
return ret;
}
ret = lwm2m_engine_validate_write_access(msg, obj_inst, &obj_field);
if (ret < 0) {
return ret;
}
ret = lwm2m_engine_get_create_res_inst(&msg->path, &res, &res_inst);
if (ret < 0) {
/* if OPTIONAL and BOOTSTRAP-WRITE or CREATE use ENOTSUP */
if ((msg->ctx->bootstrap_mode ||
msg->operation == LWM2M_OP_CREATE) &&
LWM2M_HAS_PERM(obj_field, BIT(LWM2M_FLAG_OPTIONAL))) {
ret = -ENOTSUP;
return ret;
}
ret = -ENOENT;
return ret;
}
ret = lwm2m_write_handler(obj_inst, res, res_inst, obj_field, msg);
if (ret == -EACCES || ret == -ENOENT) {
/* if read-only or non-existent data buffer move on */
ret = 0;
}
return ret;
}
const struct lwm2m_writer senml_cbor_writer = {
.put_end = put_end,
.put_begin_oi = put_begin_oi,
.put_begin_r = put_begin_r,
.put_begin_ri = put_begin_ri,
.put_s8 = put_s8,
.put_s16 = put_s16,
.put_s32 = put_s32,
.put_s64 = put_s64,
.put_time = put_time,
.put_string = put_string,
.put_float = put_float,
.put_bool = put_bool,
.put_opaque = put_opaque,
.put_objlnk = put_objlnk,
.put_data_timestamp = put_data_timestamp,
};
const struct lwm2m_reader senml_cbor_reader = {
.get_s32 = get_s32,
.get_s64 = get_s64,
.get_time = get_time,
.get_string = get_string,
.get_float = get_float,
.get_bool = get_bool,
.get_opaque = get_opaque,
.get_objlnk = get_objlnk,
};
int do_read_op_senml_cbor(struct lwm2m_message *msg)
{
int ret;
setup_out_fmt_data(msg);
ret = lwm2m_perform_read_op(msg, LWM2M_FORMAT_APP_SENML_CBOR);
clear_out_fmt_data(msg);
return ret;
}
static uint8_t parse_composite_read_paths(struct lwm2m_message *msg,
sys_slist_t *lwm2m_path_list,
sys_slist_t *lwm2m_path_free_list)
{
char basename[MAX_RESOURCE_LEN + 1] = {0}; /* to include terminating null */
char name[MAX_RESOURCE_LEN + 1] = {0}; /* to include terminating null */
/* Compiler requires reserving space for full length basename and name even though those two
* combined do not exceed MAX_RESOURCE_LEN
*/
char fqn[2 * MAX_RESOURCE_LEN + 1] = {0};
struct lwm2m_obj_path path;
struct cbor_in_fmt_data *fd;
uint8_t paths = 0;
size_t isize;
uint_fast8_t dret;
int len;
int ret;
char *payload;
uint16_t in_len;
setup_in_fmt_data(msg);
fd = engine_get_in_user_data(&msg->in);
payload = (char *)coap_packet_get_payload(msg->in.in_cpkt, &in_len);
dret = cbor_decode_lwm2m_senml(payload, in_len, &fd->dcd, &isize);
if (dret != ZCBOR_SUCCESS) {
__ASSERT_NO_MSG(false);
goto out;
}
msg->in.offset += isize;
for (int idx = 0; idx < fd->dcd.lwm2m_senml_record_m_count; idx++) {
/* Where to find the basenames and names */
struct record *record = GET_IN_FD_REC_I(fd, idx);
/* Set null terminated effective basename */
if (record->record_bn_present) {
len = MIN(sizeof(basename)-1, record->record_bn.record_bn.len);
snprintk(basename, len + 1, "%s", record->record_bn.record_bn.value);
basename[len] = '\0';
}
/* Best effort with read, skip if no proper name is available */
if (!record->record_n_present) {
if (strcmp(basename, "") == 0) {
continue;
}
}
/* Set null terminated name */
if (record->record_n_present) {
len = MIN(sizeof(name)-1, record->record_n.record_n.len);
snprintk(name, len + 1, "%s", record->record_n.record_n.value);
name[len] = '\0';
}
/* Form fully qualified path name */
snprintk(fqn, sizeof(fqn), "%s%s", basename, name);
ret = lwm2m_string_to_path(fqn, &path, '/');
/* invalid path is forgiven with read */
if (ret < 0) {
continue;
}
ret = lwm2m_engine_add_path_to_list(lwm2m_path_list, lwm2m_path_free_list, &path);
if (ret < 0) {
continue;
}
paths++;
}
out:
clear_in_fmt_data(msg);
return paths;
}
int do_composite_read_op_for_parsed_path_senml_cbor(struct lwm2m_message *msg,
sys_slist_t *lwm_path_list)
{
int ret;
setup_out_fmt_data(msg);
ret = lwm2m_perform_composite_read_op(msg, LWM2M_FORMAT_APP_SENML_CBOR, lwm_path_list);
clear_out_fmt_data(msg);
return ret;
}
int do_composite_read_op_senml_cbor(struct lwm2m_message *msg)
{
struct lwm2m_obj_path_list lwm2m_path_list_buf[CONFIG_LWM2M_COMPOSITE_PATH_LIST_SIZE];
sys_slist_t lwm_path_list;
sys_slist_t lwm_path_free_list;
uint8_t len;
lwm2m_engine_path_list_init(&lwm_path_list,
&lwm_path_free_list,
lwm2m_path_list_buf,
CONFIG_LWM2M_COMPOSITE_PATH_LIST_SIZE);
/* Parse paths */
len = parse_composite_read_paths(msg, &lwm_path_list, &lwm_path_free_list);
if (len == 0) {
LOG_ERR("No Valid URL at msg");
return -ESRCH;
}
lwm2m_engine_clear_duplicate_path(&lwm_path_list, &lwm_path_free_list);
return do_composite_read_op_for_parsed_list(msg, LWM2M_FORMAT_APP_SENML_CBOR,
&lwm_path_list);
}
int do_write_op_senml_cbor(struct lwm2m_message *msg)
{
uint_fast8_t dret;
int ret = 0;
size_t decoded_sz;
struct cbor_in_fmt_data *fd;
/* With block-wise transfer consecutive blocks will not carry the content header -
* go directly to the message processing
*/
if (msg->in.block_ctx != NULL && msg->in.block_ctx->ctx.current > 0) {
msg->path.res_id = msg->in.block_ctx->path.res_id;
msg->path.level = msg->in.block_ctx->path.level;
if (msg->path.level == LWM2M_PATH_LEVEL_RESOURCE_INST) {
msg->path.res_inst_id = msg->in.block_ctx->path.res_inst_id;
}
return do_write_op_item(msg, NULL);
}
setup_in_fmt_data(msg);
fd = engine_get_in_user_data(&msg->in);
dret = cbor_decode_lwm2m_senml(ICTX_BUF_R_PTR(&msg->in), ICTX_BUF_R_LEFT_SZ(&msg->in),
&fd->dcd, &decoded_sz);
if (dret != ZCBOR_SUCCESS) {
ret = -EBADMSG;
goto error;
}
msg->in.offset += decoded_sz;
for (int idx = 0; idx < fd->dcd.lwm2m_senml_record_m_count; idx++) {
struct record *rec = &fd->dcd.lwm2m_senml_record_m[idx];
/* Basename applies for current and succeeding records */
if (rec->record_bn_present) {
int len = MIN(sizeof(fd->basename) - 1,