@@ -40,6 +40,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
40
40
#define BINDING_OPT_MAX_LEN 3 /* "UQ" */
41
41
#define QUEUE_OPT_MAX_LEN 2 /* "Q" */
42
42
43
+ static K_MUTEX_DEFINE (registry_lock );
43
44
/* Resources */
44
45
static sys_slist_t engine_obj_list ;
45
46
static sys_slist_t engine_obj_inst_list ;
@@ -53,6 +54,7 @@ sys_slist_t *lwm2m_engine_obj_inst_list(void) { return &engine_obj_inst_list; }
53
54
54
55
void lwm2m_register_obj (struct lwm2m_engine_obj * obj )
55
56
{
57
+ k_mutex_lock (& registry_lock , K_FOREVER );
56
58
#if defined(CONFIG_LWM2M_ACCESS_CONTROL_ENABLE )
57
59
/* If bootstrap, then bootstrap server should create the ac obj instances */
58
60
#if !IS_ENABLED (CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP )
@@ -62,15 +64,18 @@ void lwm2m_register_obj(struct lwm2m_engine_obj *obj)
62
64
#endif /* CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP */
63
65
#endif /* CONFIG_LWM2M_ACCESS_CONTROL_ENABLE */
64
66
sys_slist_append (& engine_obj_list , & obj -> node );
67
+ k_mutex_unlock (& registry_lock );
65
68
}
66
69
67
70
void lwm2m_unregister_obj (struct lwm2m_engine_obj * obj )
68
71
{
72
+ k_mutex_lock (& registry_lock , K_FOREVER );
69
73
#if defined(CONFIG_LWM2M_ACCESS_CONTROL_ENABLE )
70
74
access_control_remove_obj (obj -> obj_id );
71
75
#endif
72
76
engine_remove_observer_by_id (obj -> obj_id , -1 );
73
77
sys_slist_find_and_remove (& engine_obj_list , & obj -> node );
78
+ k_mutex_unlock (& registry_lock );
74
79
}
75
80
76
81
struct lwm2m_engine_obj * get_engine_obj (int obj_id )
@@ -163,23 +168,27 @@ struct lwm2m_engine_obj_inst *next_engine_obj_inst(int obj_id, int obj_inst_id)
163
168
int lwm2m_create_obj_inst (uint16_t obj_id , uint16_t obj_inst_id ,
164
169
struct lwm2m_engine_obj_inst * * obj_inst )
165
170
{
171
+ k_mutex_lock (& registry_lock , K_FOREVER );
166
172
struct lwm2m_engine_obj * obj ;
167
173
int ret ;
168
174
169
175
* obj_inst = NULL ;
170
176
obj = get_engine_obj (obj_id );
171
177
if (!obj ) {
172
178
LOG_ERR ("unable to find obj: %u" , obj_id );
179
+ k_mutex_unlock (& registry_lock );
173
180
return - ENOENT ;
174
181
}
175
182
176
183
if (!obj -> create_cb ) {
177
184
LOG_ERR ("obj %u has no create_cb" , obj_id );
185
+ k_mutex_unlock (& registry_lock );
178
186
return - EINVAL ;
179
187
}
180
188
181
189
if (obj -> instance_count + 1 > obj -> max_instance_count ) {
182
190
LOG_ERR ("no more instances available for obj %u" , obj_id );
191
+ k_mutex_unlock (& registry_lock );
183
192
return - ENOMEM ;
184
193
}
185
194
@@ -190,6 +199,7 @@ int lwm2m_create_obj_inst(uint16_t obj_id, uint16_t obj_inst_id,
190
199
* Already checked for instance count total.
191
200
* This can only be an error if the object instance exists.
192
201
*/
202
+ k_mutex_unlock (& registry_lock );
193
203
return - EEXIST ;
194
204
}
195
205
@@ -202,27 +212,31 @@ int lwm2m_create_obj_inst(uint16_t obj_id, uint16_t obj_inst_id,
202
212
ret = obj -> user_create_cb (obj_inst_id );
203
213
if (ret < 0 ) {
204
214
LOG_ERR ("Error in user obj create %u/%u: %d" , obj_id , obj_inst_id , ret );
215
+ k_mutex_unlock (& registry_lock );
205
216
lwm2m_delete_obj_inst (obj_id , obj_inst_id );
206
217
return ret ;
207
218
}
208
219
}
209
-
220
+ k_mutex_unlock ( & registry_lock );
210
221
return 0 ;
211
222
}
212
223
213
224
int lwm2m_delete_obj_inst (uint16_t obj_id , uint16_t obj_inst_id )
214
225
{
226
+ k_mutex_lock (& registry_lock , K_FOREVER );
215
227
int i , ret = 0 ;
216
228
struct lwm2m_engine_obj * obj ;
217
229
struct lwm2m_engine_obj_inst * obj_inst ;
218
230
219
231
obj = get_engine_obj (obj_id );
220
232
if (!obj ) {
233
+ k_mutex_unlock (& registry_lock );
221
234
return - ENOENT ;
222
235
}
223
236
224
237
obj_inst = get_engine_obj_inst (obj_id , obj_inst_id );
225
238
if (!obj_inst ) {
239
+ k_mutex_unlock (& registry_lock );
226
240
return - ENOENT ;
227
241
}
228
242
@@ -249,6 +263,7 @@ int lwm2m_delete_obj_inst(uint16_t obj_id, uint16_t obj_inst_id)
249
263
250
264
clear_attrs (obj_inst );
251
265
(void )memset (obj_inst , 0 , sizeof (struct lwm2m_engine_obj_inst ));
266
+ k_mutex_unlock (& registry_lock );
252
267
return ret ;
253
268
}
254
269
@@ -417,14 +432,17 @@ int lwm2m_engine_set_res_buf(const char *pathstr, void *buffer_ptr, uint16_t buf
417
432
return - EINVAL ;
418
433
}
419
434
435
+ k_mutex_lock (& registry_lock , K_FOREVER );
420
436
/* look up resource obj */
421
437
ret = path_to_objs (& path , NULL , NULL , NULL , & res_inst );
422
438
if (ret < 0 ) {
439
+ k_mutex_unlock (& registry_lock );
423
440
return ret ;
424
441
}
425
442
426
443
if (!res_inst ) {
427
444
LOG_ERR ("res instance %d not found" , path .res_inst_id );
445
+ k_mutex_unlock (& registry_lock );
428
446
return - ENOENT ;
429
447
}
430
448
@@ -434,6 +452,7 @@ int lwm2m_engine_set_res_buf(const char *pathstr, void *buffer_ptr, uint16_t buf
434
452
res_inst -> max_data_len = buffer_len ;
435
453
res_inst -> data_flags = data_flags ;
436
454
455
+ k_mutex_unlock (& registry_lock );
437
456
return ret ;
438
457
}
439
458
@@ -468,21 +487,25 @@ static int lwm2m_engine_set(const char *pathstr, void *value, uint16_t len)
468
487
return - EINVAL ;
469
488
}
470
489
490
+ k_mutex_lock (& registry_lock , K_FOREVER );
471
491
/* look up resource obj */
472
492
ret = path_to_objs (& path , & obj_inst , & obj_field , & res , & res_inst );
473
493
if (ret < 0 ) {
494
+ k_mutex_unlock (& registry_lock );
474
495
return ret ;
475
496
}
476
497
477
498
if (!res_inst ) {
478
499
LOG_ERR ("res instance %d not found" , path .res_inst_id );
500
+ k_mutex_unlock (& registry_lock );
479
501
return - ENOENT ;
480
502
}
481
503
482
504
if (LWM2M_HAS_RES_FLAG (res_inst , LWM2M_RES_DATA_FLAG_RO )) {
483
505
LOG_ERR ("res instance data pointer is read-only "
484
506
"[%u/%u/%u/%u:%u]" ,
485
507
path .obj_id , path .obj_inst_id , path .res_id , path .res_inst_id , path .level );
508
+ k_mutex_unlock (& registry_lock );
486
509
return - EACCES ;
487
510
}
488
511
@@ -499,12 +522,14 @@ static int lwm2m_engine_set(const char *pathstr, void *value, uint16_t len)
499
522
if (!data_ptr ) {
500
523
LOG_ERR ("res instance data pointer is NULL [%u/%u/%u/%u:%u]" , path .obj_id ,
501
524
path .obj_inst_id , path .res_id , path .res_inst_id , path .level );
525
+ k_mutex_unlock (& registry_lock );
502
526
return - EINVAL ;
503
527
}
504
528
505
529
/* check length (note: we add 1 to string length for NULL pad) */
506
530
if (len > max_data_len - (obj_field -> data_type == LWM2M_RES_TYPE_STRING ? 1 : 0 )) {
507
531
LOG_ERR ("length %u is too long for res instance %d data" , len , path .res_id );
532
+ k_mutex_unlock (& registry_lock );
508
533
return - ENOMEM ;
509
534
}
510
535
@@ -517,6 +542,7 @@ static int lwm2m_engine_set(const char *pathstr, void *value, uint16_t len)
517
542
ret = res -> validate_cb (obj_inst -> obj_inst_id , res -> res_id , res_inst -> res_inst_id ,
518
543
value , len , false, 0 );
519
544
if (ret < 0 ) {
545
+ k_mutex_unlock (& registry_lock );
520
546
return - EINVAL ;
521
547
}
522
548
}
@@ -576,6 +602,7 @@ static int lwm2m_engine_set(const char *pathstr, void *value, uint16_t len)
576
602
577
603
default :
578
604
LOG_ERR ("unknown obj data_type %d" , obj_field -> data_type );
605
+ k_mutex_unlock (& registry_lock );
579
606
return - EINVAL ;
580
607
}
581
608
@@ -589,7 +616,7 @@ static int lwm2m_engine_set(const char *pathstr, void *value, uint16_t len)
589
616
if (changed && LWM2M_HAS_PERM (obj_field , LWM2M_PERM_R )) {
590
617
lwm2m_notify_observer_path (& path );
591
618
}
592
-
619
+ k_mutex_unlock ( & registry_lock );
593
620
return ret ;
594
621
}
595
622
@@ -694,14 +721,17 @@ int lwm2m_engine_get_res_buf(const char *pathstr, void **buffer_ptr, uint16_t *b
694
721
return - EINVAL ;
695
722
}
696
723
724
+ k_mutex_lock (& registry_lock , K_FOREVER );
697
725
/* look up resource obj */
698
726
ret = path_to_objs (& path , NULL , NULL , NULL , & res_inst );
699
727
if (ret < 0 ) {
728
+ k_mutex_unlock (& registry_lock );
700
729
return ret ;
701
730
}
702
731
703
732
if (!res_inst ) {
704
733
LOG_ERR ("res instance %d not found" , path .res_inst_id );
734
+ k_mutex_unlock (& registry_lock );
705
735
return - ENOENT ;
706
736
}
707
737
@@ -718,6 +748,7 @@ int lwm2m_engine_get_res_buf(const char *pathstr, void **buffer_ptr, uint16_t *b
718
748
* data_flags = res_inst -> data_flags ;
719
749
}
720
750
751
+ k_mutex_unlock (& registry_lock );
721
752
return 0 ;
722
753
}
723
754
@@ -750,15 +781,17 @@ static int lwm2m_engine_get(const char *pathstr, void *buf, uint16_t buflen)
750
781
LOG_ERR ("path must have at least 3 parts" );
751
782
return - EINVAL ;
752
783
}
753
-
784
+ k_mutex_lock ( & registry_lock , K_FOREVER );
754
785
/* look up resource obj */
755
786
ret = path_to_objs (& path , & obj_inst , & obj_field , & res , & res_inst );
756
787
if (ret < 0 ) {
788
+ k_mutex_unlock (& registry_lock );
757
789
return ret ;
758
790
}
759
791
760
792
if (!res_inst ) {
761
793
LOG_ERR ("res instance %d not found" , path .res_inst_id );
794
+ k_mutex_unlock (& registry_lock );
762
795
return - ENOENT ;
763
796
}
764
797
@@ -779,6 +812,7 @@ static int lwm2m_engine_get(const char *pathstr, void *buf, uint16_t buflen)
779
812
780
813
case LWM2M_RES_TYPE_OPAQUE :
781
814
if (data_len > buflen ) {
815
+ k_mutex_unlock (& registry_lock );
782
816
return - ENOMEM ;
783
817
}
784
818
@@ -832,10 +866,11 @@ static int lwm2m_engine_get(const char *pathstr, void *buf, uint16_t buflen)
832
866
833
867
default :
834
868
LOG_ERR ("unknown obj data_type %d" , obj_field -> data_type );
869
+ k_mutex_unlock (& registry_lock );
835
870
return - EINVAL ;
836
871
}
837
872
}
838
-
873
+ k_mutex_unlock ( & registry_lock );
839
874
return 0 ;
840
875
}
841
876
@@ -1056,22 +1091,25 @@ int lwm2m_engine_create_res_inst(const char *pathstr)
1056
1091
LOG_ERR ("path must have 4 parts" );
1057
1092
return - EINVAL ;
1058
1093
}
1059
-
1094
+ k_mutex_lock ( & registry_lock , K_FOREVER );
1060
1095
ret = path_to_objs (& path , NULL , NULL , & res , & res_inst );
1061
1096
if (ret < 0 ) {
1097
+ k_mutex_unlock (& registry_lock );
1062
1098
return ret ;
1063
1099
}
1064
1100
1065
1101
if (!res ) {
1066
1102
LOG_ERR ("resource %u not found" , path .res_id );
1103
+ k_mutex_unlock (& registry_lock );
1067
1104
return - ENOENT ;
1068
1105
}
1069
1106
1070
1107
if (res_inst && res_inst -> res_inst_id != RES_INSTANCE_NOT_CREATED ) {
1071
1108
LOG_ERR ("res instance %u already exists" , path .res_inst_id );
1109
+ k_mutex_unlock (& registry_lock );
1072
1110
return - EINVAL ;
1073
1111
}
1074
-
1112
+ k_mutex_unlock ( & registry_lock );
1075
1113
return lwm2m_engine_allocate_resource_instance (res , & res_inst , path .res_inst_id );
1076
1114
}
1077
1115
@@ -1090,22 +1128,24 @@ int lwm2m_engine_delete_res_inst(const char *pathstr)
1090
1128
LOG_ERR ("path must have 4 parts" );
1091
1129
return - EINVAL ;
1092
1130
}
1093
-
1131
+ k_mutex_lock ( & registry_lock , K_FOREVER );
1094
1132
ret = path_to_objs (& path , NULL , NULL , NULL , & res_inst );
1095
1133
if (ret < 0 ) {
1134
+ k_mutex_unlock (& registry_lock );
1096
1135
return ret ;
1097
1136
}
1098
1137
1099
1138
if (!res_inst ) {
1100
1139
LOG_ERR ("res instance %u not found" , path .res_inst_id );
1140
+ k_mutex_unlock (& registry_lock );
1101
1141
return - ENOENT ;
1102
1142
}
1103
1143
1104
1144
res_inst -> data_ptr = NULL ;
1105
1145
res_inst -> max_data_len = 0U ;
1106
1146
res_inst -> data_len = 0U ;
1107
1147
res_inst -> res_inst_id = RES_INSTANCE_NOT_CREATED ;
1108
-
1148
+ k_mutex_unlock ( & registry_lock );
1109
1149
return 0 ;
1110
1150
}
1111
1151
/* Register callbacks */
0 commit comments