-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
cfg_cli.c
1335 lines (1098 loc) · 43.7 KB
/
cfg_cli.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
/* Bluetooth Mesh */
/*
* Copyright (c) 2017 Intel Corporation
* Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include "btc_ble_mesh_config_model.h"
#include "mesh.h"
#include "mesh_config.h"
#include "foundation.h"
#include "mesh_common.h"
#include "cfg_cli.h"
#if CONFIG_BLE_MESH_CFG_CLI
static const bt_mesh_client_op_pair_t cfg_op_pair[] = {
{ OP_BEACON_GET, OP_BEACON_STATUS },
{ OP_BEACON_SET, OP_BEACON_STATUS },
{ OP_DEV_COMP_DATA_GET, OP_DEV_COMP_DATA_STATUS },
{ OP_DEFAULT_TTL_GET, OP_DEFAULT_TTL_STATUS },
{ OP_DEFAULT_TTL_SET, OP_DEFAULT_TTL_STATUS },
{ OP_GATT_PROXY_GET, OP_GATT_PROXY_STATUS },
{ OP_GATT_PROXY_SET, OP_GATT_PROXY_STATUS },
{ OP_RELAY_GET, OP_RELAY_STATUS },
{ OP_RELAY_SET, OP_RELAY_STATUS },
{ OP_MOD_PUB_GET, OP_MOD_PUB_STATUS },
{ OP_MOD_PUB_SET, OP_MOD_PUB_STATUS },
{ OP_MOD_PUB_VA_SET, OP_MOD_PUB_STATUS },
{ OP_MOD_SUB_ADD, OP_MOD_SUB_STATUS },
{ OP_MOD_SUB_VA_ADD, OP_MOD_SUB_STATUS },
{ OP_MOD_SUB_DEL, OP_MOD_SUB_STATUS },
{ OP_MOD_SUB_VA_DEL, OP_MOD_SUB_STATUS },
{ OP_MOD_SUB_OVERWRITE, OP_MOD_SUB_STATUS },
{ OP_MOD_SUB_VA_OVERWRITE, OP_MOD_SUB_STATUS },
{ OP_MOD_SUB_DEL_ALL, OP_MOD_SUB_STATUS },
{ OP_MOD_SUB_GET, OP_MOD_SUB_LIST },
{ OP_MOD_SUB_GET_VND, OP_MOD_SUB_LIST_VND },
{ OP_NET_KEY_ADD, OP_NET_KEY_STATUS },
{ OP_NET_KEY_UPDATE, OP_NET_KEY_STATUS },
{ OP_NET_KEY_DEL, OP_NET_KEY_STATUS },
{ OP_NET_KEY_GET, OP_NET_KEY_LIST },
{ OP_APP_KEY_ADD, OP_APP_KEY_STATUS },
{ OP_APP_KEY_UPDATE, OP_APP_KEY_STATUS },
{ OP_APP_KEY_DEL, OP_APP_KEY_STATUS },
{ OP_APP_KEY_GET, OP_APP_KEY_LIST },
{ OP_NODE_IDENTITY_GET, OP_NODE_IDENTITY_STATUS },
{ OP_NODE_IDENTITY_SET, OP_NODE_IDENTITY_STATUS },
{ OP_MOD_APP_BIND, OP_MOD_APP_STATUS },
{ OP_MOD_APP_UNBIND, OP_MOD_APP_STATUS },
{ OP_SIG_MOD_APP_GET, OP_SIG_MOD_APP_LIST },
{ OP_VND_MOD_APP_GET, OP_VND_MOD_APP_LIST },
{ OP_NODE_RESET, OP_NODE_RESET_STATUS },
{ OP_FRIEND_GET, OP_FRIEND_STATUS },
{ OP_FRIEND_SET, OP_FRIEND_STATUS },
{ OP_KRP_GET, OP_KRP_STATUS },
{ OP_KRP_SET, OP_KRP_STATUS },
{ OP_HEARTBEAT_PUB_GET, OP_HEARTBEAT_PUB_STATUS },
{ OP_HEARTBEAT_PUB_SET, OP_HEARTBEAT_PUB_STATUS },
{ OP_HEARTBEAT_SUB_GET, OP_HEARTBEAT_SUB_STATUS },
{ OP_HEARTBEAT_SUB_SET, OP_HEARTBEAT_SUB_STATUS },
{ OP_LPN_TIMEOUT_GET, OP_LPN_TIMEOUT_STATUS },
{ OP_NET_TRANSMIT_GET, OP_NET_TRANSMIT_STATUS },
{ OP_NET_TRANSMIT_SET, OP_NET_TRANSMIT_STATUS },
};
static bt_mesh_mutex_t cfg_client_lock;
static inline void bt_mesh_cfg_client_mutex_new(void)
{
if (!cfg_client_lock.mutex) {
bt_mesh_mutex_create(&cfg_client_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_cfg_client_mutex_free(void)
{
bt_mesh_mutex_free(&cfg_client_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_cfg_client_lock(void)
{
bt_mesh_mutex_lock(&cfg_client_lock);
}
static inline void bt_mesh_cfg_client_unlock(void)
{
bt_mesh_mutex_unlock(&cfg_client_lock);
}
static void timeout_handler(struct k_work *work)
{
struct k_delayed_work *timer = NULL;
bt_mesh_client_node_t *node = NULL;
struct bt_mesh_msg_ctx ctx = {0};
uint32_t opcode = 0U;
BT_WARN("Receive configuration status message timeout");
bt_mesh_cfg_client_lock();
timer = CONTAINER_OF(work, struct k_delayed_work, work);
if (timer && !k_delayed_work_free(timer)) {
node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work);
if (node) {
memcpy(&ctx, &node->ctx, sizeof(ctx));
opcode = node->opcode;
bt_mesh_client_free_node(node);
bt_mesh_config_client_cb_evt_to_btc(
opcode, BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0);
}
}
bt_mesh_cfg_client_unlock();
return;
}
static void cfg_client_recv_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
void *status, size_t len)
{
bt_mesh_client_node_t *node = NULL;
struct net_buf_simple buf = {0};
uint8_t evt_type = 0xFF;
if (!model || !ctx) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
/* If it is a publish message, sent to the user directly. */
buf.data = (uint8_t *)status;
buf.len = (uint16_t)len;
bt_mesh_cfg_client_lock();
node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true);
if (!node) {
BT_DBG("Unexpected Config Status 0x%04x", ctx->recv_op);
} else {
switch (node->opcode) {
case OP_BEACON_GET:
case OP_DEV_COMP_DATA_GET:
case OP_DEFAULT_TTL_GET:
case OP_GATT_PROXY_GET:
case OP_RELAY_GET:
case OP_MOD_PUB_GET:
case OP_MOD_SUB_GET:
case OP_MOD_SUB_GET_VND:
case OP_NET_KEY_GET:
case OP_APP_KEY_GET:
case OP_NODE_IDENTITY_GET:
case OP_SIG_MOD_APP_GET:
case OP_VND_MOD_APP_GET:
case OP_FRIEND_GET:
case OP_KRP_GET:
case OP_HEARTBEAT_PUB_GET:
case OP_HEARTBEAT_SUB_GET:
case OP_LPN_TIMEOUT_GET:
case OP_NET_TRANSMIT_GET:
evt_type = BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE;
break;
case OP_BEACON_SET:
case OP_DEFAULT_TTL_SET:
case OP_GATT_PROXY_SET:
case OP_RELAY_SET:
case OP_MOD_PUB_SET:
case OP_MOD_PUB_VA_SET:
case OP_MOD_SUB_ADD:
case OP_MOD_SUB_VA_ADD:
case OP_MOD_SUB_DEL:
case OP_MOD_SUB_VA_DEL:
case OP_MOD_SUB_OVERWRITE:
case OP_MOD_SUB_VA_OVERWRITE:
case OP_MOD_SUB_DEL_ALL:
case OP_NET_KEY_ADD:
case OP_NET_KEY_UPDATE:
case OP_NET_KEY_DEL:
case OP_APP_KEY_ADD:
case OP_APP_KEY_UPDATE:
case OP_APP_KEY_DEL:
case OP_NODE_IDENTITY_SET:
case OP_MOD_APP_BIND:
case OP_MOD_APP_UNBIND:
case OP_NODE_RESET:
case OP_FRIEND_SET:
case OP_KRP_SET:
case OP_HEARTBEAT_PUB_SET:
case OP_HEARTBEAT_SUB_SET:
case OP_NET_TRANSMIT_SET:
evt_type = BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE;
break;
default:
break;
}
if (!k_delayed_work_free(&node->timer)) {
uint32_t opcode = node->opcode;
bt_mesh_client_free_node(node);
bt_mesh_config_client_cb_evt_to_btc(
opcode, evt_type, model, ctx, (const uint8_t *)status, len);
}
}
bt_mesh_cfg_client_unlock();
switch (ctx->recv_op) {
case OP_DEV_COMP_DATA_STATUS: {
struct bt_mesh_cfg_comp_data_status *val = status;
bt_mesh_free_buf(val->comp_data);
break;
}
case OP_MOD_SUB_LIST:
case OP_MOD_SUB_LIST_VND: {
struct bt_mesh_cfg_mod_sub_list *val = status;
bt_mesh_free_buf(val->addr);
break;
}
case OP_NET_KEY_LIST: {
struct bt_mesh_cfg_net_key_list *val = status;
bt_mesh_free_buf(val->net_idx);
break;
}
case OP_APP_KEY_LIST: {
struct bt_mesh_cfg_app_key_list *val = status;
bt_mesh_free_buf(val->app_idx);
break;
}
case OP_SIG_MOD_APP_LIST:
case OP_VND_MOD_APP_LIST: {
struct bt_mesh_cfg_mod_app_list *val = status;
bt_mesh_free_buf(val->app_idx);
break;
}
default:
break;
}
}
static void comp_data_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_comp_data_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.page = net_buf_simple_pull_u8(buf);
status.comp_data = bt_mesh_alloc_buf(buf->len);
if (!status.comp_data) {
BT_ERR("%s, Out of memory", __func__);
return;
}
net_buf_simple_add_mem(status.comp_data, buf->data, buf->len);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_comp_data_status));
}
static void state_status_u8(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint8_t status = 0U;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status = net_buf_simple_pull_u8(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(uint8_t));
}
static void beacon_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
state_status_u8(model, ctx, buf);
}
static void ttl_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
state_status_u8(model, ctx, buf);
}
static void friend_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
state_status_u8(model, ctx, buf);
}
static void gatt_proxy_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
state_status_u8(model, ctx, buf);
}
static void relay_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_relay_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.relay = net_buf_simple_pull_u8(buf);
status.retransmit = net_buf_simple_pull_u8(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_relay_status));
}
static void net_key_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_netkey_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.net_idx = net_buf_simple_pull_le16(buf) & 0xfff;
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_netkey_status));
}
static void app_key_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_appkey_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
key_idx_unpack(buf, &status.net_idx, &status.app_idx);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_appkey_status));
}
static void mod_app_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_mod_app_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.elem_addr = net_buf_simple_pull_le16(buf);
status.app_idx = net_buf_simple_pull_le16(buf);
if (buf->len >= 4) {
status.cid = net_buf_simple_pull_le16(buf);
} else {
status.cid = BLE_MESH_CID_NVAL;
}
status.mod_id = net_buf_simple_pull_le16(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_mod_app_status));
}
static void mod_pub_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_mod_pub_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.elem_addr = net_buf_simple_pull_le16(buf);
status.addr = net_buf_simple_pull_le16(buf);
status.app_idx = net_buf_simple_pull_le16(buf);
status.cred_flag = (status.app_idx & BIT(12));
status.app_idx &= BIT_MASK(12);
status.ttl = net_buf_simple_pull_u8(buf);
status.period = net_buf_simple_pull_u8(buf);
status.transmit = net_buf_simple_pull_u8(buf);
if (buf->len >= 4) {
status.cid = net_buf_simple_pull_le16(buf);
} else {
status.cid = BLE_MESH_CID_NVAL;
}
status.mod_id = net_buf_simple_pull_le16(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_mod_pub_status));
}
static void mod_sub_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_mod_sub_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.elem_addr = net_buf_simple_pull_le16(buf);
status.sub_addr = net_buf_simple_pull_le16(buf);
if (buf->len >= 4) {
status.cid = net_buf_simple_pull_le16(buf);
} else {
status.cid = BLE_MESH_CID_NVAL;
}
status.mod_id = net_buf_simple_pull_le16(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_mod_sub_status));
}
static void hb_sub_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_hb_sub_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.src = net_buf_simple_pull_le16(buf);
status.dst = net_buf_simple_pull_le16(buf);
status.period = net_buf_simple_pull_u8(buf);
status.count = net_buf_simple_pull_u8(buf);
status.min = net_buf_simple_pull_u8(buf);
status.max = net_buf_simple_pull_u8(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_hb_sub_status));
}
static void hb_pub_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_hb_pub_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.dst = net_buf_simple_pull_le16(buf);
status.count = net_buf_simple_pull_u8(buf);
status.period = net_buf_simple_pull_u8(buf);
status.ttl = net_buf_simple_pull_u8(buf);
status.feat = net_buf_simple_pull_u8(buf);
status.net_idx = net_buf_simple_pull_u8(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_hb_sub_status));
}
static void node_reset_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
cfg_client_recv_status(model, ctx, NULL, 0);
}
static void mod_sub_list(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_mod_sub_list list = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
list.status = net_buf_simple_pull_u8(buf);
list.elem_addr = net_buf_simple_pull_le16(buf);
if (ctx->recv_op == OP_MOD_SUB_LIST_VND) {
list.cid = net_buf_simple_pull_le16(buf);
} else {
list.cid = BLE_MESH_CID_NVAL;
}
list.mod_id = net_buf_simple_pull_le16(buf);
list.addr = bt_mesh_alloc_buf(buf->len);
if (!list.addr) {
BT_ERR("%s, Out of memory", __func__);
return;
}
net_buf_simple_add_mem(list.addr, buf->data, buf->len);
cfg_client_recv_status(model, ctx, &list, sizeof(struct bt_mesh_cfg_mod_sub_list));
}
static void net_key_list(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_net_key_list list = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
list.net_idx = bt_mesh_alloc_buf(buf->len);
if (!list.net_idx) {
BT_ERR("%s, Out of memory", __func__);
return;
}
net_buf_simple_add_mem(list.net_idx, buf->data, buf->len);
cfg_client_recv_status(model, ctx, &list, sizeof(struct bt_mesh_cfg_net_key_list));
}
static void app_key_list(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_app_key_list list = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
list.status = net_buf_simple_pull_u8(buf);
list.net_idx = net_buf_simple_pull_le16(buf);
list.app_idx = bt_mesh_alloc_buf(buf->len);
if (!list.app_idx) {
BT_ERR("%s, Out of memory", __func__);
return;
}
net_buf_simple_add_mem(list.app_idx, buf->data, buf->len);
cfg_client_recv_status(model, ctx, &list, sizeof(struct bt_mesh_cfg_app_key_list));
}
static void node_id_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_node_id_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.net_idx = net_buf_simple_pull_le16(buf);
status.identity = net_buf_simple_pull_u8(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_node_id_status));
}
static void mod_app_list(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_mod_app_list list = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
list.status = net_buf_simple_pull_u8(buf);
list.elem_addr = net_buf_simple_pull_le16(buf);
if (ctx->recv_op == OP_VND_MOD_APP_LIST) {
list.cid = net_buf_simple_pull_le16(buf);
} else {
list.cid = BLE_MESH_CID_NVAL;
}
list.mod_id = net_buf_simple_pull_le16(buf);
list.app_idx = bt_mesh_alloc_buf(buf->len);
if (!list.app_idx) {
BT_ERR("%s, Out of memory", __func__);
return;
}
net_buf_simple_add_mem(list.app_idx, buf->data, buf->len);
cfg_client_recv_status(model, ctx, &list, sizeof(struct bt_mesh_cfg_mod_app_list));
}
static void kr_phase_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_key_refresh_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.status = net_buf_simple_pull_u8(buf);
status.net_idx = net_buf_simple_pull_le16(buf);
status.phase = net_buf_simple_pull_u8(buf);
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_key_refresh_status));
}
static void lpn_pollto_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_lpn_pollto_status status = {0};
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
status.lpn_addr = net_buf_simple_pull_le16(buf);
status.timeout = net_buf_simple_pull_u8(buf);
status.timeout |= net_buf_simple_pull_u8(buf) << 8;
status.timeout |= net_buf_simple_pull_u8(buf) << 16;
cfg_client_recv_status(model, ctx, &status, sizeof(struct bt_mesh_cfg_lpn_pollto_status));
}
static void net_trans_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
state_status_u8(model, ctx, buf);
}
const struct bt_mesh_model_op bt_mesh_cfg_cli_op[] = {
{ OP_DEV_COMP_DATA_STATUS, 15, comp_data_status },
{ OP_BEACON_STATUS, 1, beacon_status },
{ OP_DEFAULT_TTL_STATUS, 1, ttl_status },
{ OP_FRIEND_STATUS, 1, friend_status },
{ OP_GATT_PROXY_STATUS, 1, gatt_proxy_status },
{ OP_RELAY_STATUS, 2, relay_status },
{ OP_NET_KEY_STATUS, 3, net_key_status },
{ OP_APP_KEY_STATUS, 4, app_key_status },
{ OP_MOD_APP_STATUS, 7, mod_app_status },
{ OP_MOD_PUB_STATUS, 12, mod_pub_status },
{ OP_MOD_SUB_STATUS, 7, mod_sub_status },
{ OP_HEARTBEAT_SUB_STATUS, 9, hb_sub_status },
{ OP_HEARTBEAT_PUB_STATUS, 10, hb_pub_status },
{ OP_NODE_RESET_STATUS, 0, node_reset_status },
{ OP_MOD_SUB_LIST, 5, mod_sub_list },
{ OP_MOD_SUB_LIST_VND, 7, mod_sub_list },
{ OP_NET_KEY_LIST, 2, net_key_list },
{ OP_APP_KEY_LIST, 3, app_key_list },
{ OP_NODE_IDENTITY_STATUS, 4, node_id_status },
{ OP_SIG_MOD_APP_LIST, 5, mod_app_list },
{ OP_VND_MOD_APP_LIST, 7, mod_app_list },
{ OP_KRP_STATUS, 4, kr_phase_status },
{ OP_LPN_TIMEOUT_STATUS, 5, lpn_pollto_status },
{ OP_NET_TRANSMIT_STATUS, 1, net_trans_status },
BLE_MESH_MODEL_OP_END,
};
static int send_msg_with_none(bt_mesh_client_common_param_t *param, uint32_t op)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, op, 0);
bt_mesh_model_msg_init(&msg, op);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
static int send_msg_with_u8(bt_mesh_client_common_param_t *param, uint32_t op, uint8_t val)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, op, 1);
bt_mesh_model_msg_init(&msg, op);
net_buf_simple_add_u8(&msg, val);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
static int send_msg_with_le16(bt_mesh_client_common_param_t *param, uint32_t op, uint16_t val)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, op, 2);
bt_mesh_model_msg_init(&msg, op);
net_buf_simple_add_le16(&msg, val);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_comp_data_get(bt_mesh_client_common_param_t *param, uint8_t page)
{
return send_msg_with_u8(param, OP_DEV_COMP_DATA_GET, page);
}
int bt_mesh_cfg_beacon_get(bt_mesh_client_common_param_t *param)
{
return send_msg_with_none(param, OP_BEACON_GET);
}
int bt_mesh_cfg_beacon_set(bt_mesh_client_common_param_t *param, uint8_t val)
{
if (val > 0x01) {
BT_ERR("Invalid beacon state 0x%02x", val);
return -EINVAL;
}
return send_msg_with_u8(param, OP_BEACON_SET, val);
}
int bt_mesh_cfg_ttl_get(bt_mesh_client_common_param_t *param)
{
return send_msg_with_none(param, OP_DEFAULT_TTL_GET);
}
int bt_mesh_cfg_ttl_set(bt_mesh_client_common_param_t *param, uint8_t val)
{
return send_msg_with_u8(param, OP_DEFAULT_TTL_SET, val);
}
int bt_mesh_cfg_friend_get(bt_mesh_client_common_param_t *param)
{
return send_msg_with_none(param, OP_FRIEND_GET);
}
int bt_mesh_cfg_friend_set(bt_mesh_client_common_param_t *param, uint8_t val)
{
return send_msg_with_u8(param, OP_FRIEND_SET, val);
}
int bt_mesh_cfg_gatt_proxy_get(bt_mesh_client_common_param_t *param)
{
return send_msg_with_none(param, OP_GATT_PROXY_GET);
}
int bt_mesh_cfg_gatt_proxy_set(bt_mesh_client_common_param_t *param, uint8_t val)
{
return send_msg_with_u8(param, OP_GATT_PROXY_SET, val);
}
int bt_mesh_cfg_relay_get(bt_mesh_client_common_param_t *param)
{
return send_msg_with_none(param, OP_RELAY_GET);
}
int bt_mesh_cfg_relay_set(bt_mesh_client_common_param_t *param,
uint8_t relay, uint8_t retransmit)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_SET, 2);
bt_mesh_model_msg_init(&msg, OP_RELAY_SET);
net_buf_simple_add_u8(&msg, relay);
net_buf_simple_add_u8(&msg, retransmit);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_net_key_add(bt_mesh_client_common_param_t *param,
uint16_t net_idx, const uint8_t net_key[16])
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_ADD, 18);
if (!net_key) {
BT_ERR("Invalid NetKey");
return -EINVAL;
}
bt_mesh_model_msg_init(&msg, OP_NET_KEY_ADD);
net_buf_simple_add_le16(&msg, net_idx);
net_buf_simple_add_mem(&msg, net_key, 16);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_app_key_add(bt_mesh_client_common_param_t *param,
uint16_t net_idx, uint16_t app_idx,
const uint8_t app_key[16])
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_ADD, 19);
if (!app_key) {
BT_ERR("Invalid AppKey");
return -EINVAL;
}
bt_mesh_model_msg_init(&msg, OP_APP_KEY_ADD);
key_idx_pack(&msg, net_idx, app_idx);
net_buf_simple_add_mem(&msg, app_key, 16);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_mod_app_bind(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, uint16_t app_idx,
uint16_t mod_id, uint16_t cid)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_BIND, 8);
bt_mesh_model_msg_init(&msg, OP_MOD_APP_BIND);
net_buf_simple_add_le16(&msg, elem_addr);
net_buf_simple_add_le16(&msg, app_idx);
if (cid != BLE_MESH_CID_NVAL) {
net_buf_simple_add_le16(&msg, cid);
}
net_buf_simple_add_le16(&msg, mod_id);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
static int mod_sub(bt_mesh_client_common_param_t *param, uint32_t op,
uint16_t elem_addr, uint16_t sub_addr,
uint16_t mod_id, uint16_t cid)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, op, 8);
bt_mesh_model_msg_init(&msg, op);
net_buf_simple_add_le16(&msg, elem_addr);
net_buf_simple_add_le16(&msg, sub_addr);
if (cid != BLE_MESH_CID_NVAL) {
net_buf_simple_add_le16(&msg, cid);
}
net_buf_simple_add_le16(&msg, mod_id);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_mod_sub_add(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, uint16_t sub_addr,
uint16_t mod_id, uint16_t cid)
{
return mod_sub(param, OP_MOD_SUB_ADD, elem_addr, sub_addr, mod_id, cid);
}
int bt_mesh_cfg_mod_sub_del(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, uint16_t sub_addr,
uint16_t mod_id, uint16_t cid)
{
return mod_sub(param, OP_MOD_SUB_DEL, elem_addr, sub_addr, mod_id, cid);
}
int bt_mesh_cfg_mod_sub_overwrite(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, uint16_t sub_addr,
uint16_t mod_id, uint16_t cid)
{
return mod_sub(param, OP_MOD_SUB_OVERWRITE, elem_addr, sub_addr, mod_id, cid);
}
static int mod_sub_va(bt_mesh_client_common_param_t *param, uint32_t op,
uint16_t elem_addr, const uint8_t label[16],
uint16_t mod_id, uint16_t cid)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, op, 22);
if (!label) {
BT_ERR("Invalid label uuid");
return -EINVAL;
}
BT_DBG("elem_addr 0x%04x label %s", elem_addr, bt_hex(label, 16));
BT_DBG("mod_id 0x%04x cid 0x%04x", mod_id, cid);
bt_mesh_model_msg_init(&msg, op);
net_buf_simple_add_le16(&msg, elem_addr);
net_buf_simple_add_mem(&msg, label, 16);
if (cid != BLE_MESH_CID_NVAL) {
net_buf_simple_add_le16(&msg, cid);
}
net_buf_simple_add_le16(&msg, mod_id);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_mod_sub_va_add(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, const uint8_t label[16],
uint16_t mod_id, uint16_t cid)
{
return mod_sub_va(param, OP_MOD_SUB_VA_ADD, elem_addr, label, mod_id, cid);
}
int bt_mesh_cfg_mod_sub_va_del(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, const uint8_t label[16],
uint16_t mod_id, uint16_t cid)
{
return mod_sub_va(param, OP_MOD_SUB_VA_DEL, elem_addr, label, mod_id, cid);
}
int bt_mesh_cfg_mod_sub_va_overwrite(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, const uint8_t label[16],
uint16_t mod_id, uint16_t cid)
{
return mod_sub_va(param, OP_MOD_SUB_VA_OVERWRITE, elem_addr, label, mod_id, cid);
}
int bt_mesh_cfg_mod_pub_get(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, uint16_t mod_id, uint16_t cid)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_GET, 6);
bt_mesh_model_msg_init(&msg, OP_MOD_PUB_GET);
net_buf_simple_add_le16(&msg, elem_addr);
if (cid != BLE_MESH_CID_NVAL) {
net_buf_simple_add_le16(&msg, cid);
}
net_buf_simple_add_le16(&msg, mod_id);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_mod_pub_set(bt_mesh_client_common_param_t *param,
uint16_t elem_addr, uint16_t mod_id, uint16_t cid,
struct bt_mesh_cfg_mod_pub *pub)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_SET, 13);
if (!pub) {
BT_ERR("Invalid model pub set");
return -EINVAL;
}
bt_mesh_model_msg_init(&msg, OP_MOD_PUB_SET);
net_buf_simple_add_le16(&msg, elem_addr);
net_buf_simple_add_le16(&msg, pub->addr);
net_buf_simple_add_le16(&msg, (pub->app_idx | (pub->cred_flag << 12)));
net_buf_simple_add_u8(&msg, pub->ttl);
net_buf_simple_add_u8(&msg, pub->period);
net_buf_simple_add_u8(&msg, pub->transmit);
if (cid != BLE_MESH_CID_NVAL) {
net_buf_simple_add_le16(&msg, cid);
}
net_buf_simple_add_le16(&msg, mod_id);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_hb_sub_set(bt_mesh_client_common_param_t *param,
struct bt_mesh_cfg_hb_sub *sub)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_SET, 5);
if (!sub) {
BT_ERR("Invalid heartbeat sub set");
return -EINVAL;
}
bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_SUB_SET);
net_buf_simple_add_le16(&msg, sub->src);
net_buf_simple_add_le16(&msg, sub->dst);
net_buf_simple_add_u8(&msg, sub->period);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_hb_sub_get(bt_mesh_client_common_param_t *param)
{
return send_msg_with_none(param, OP_HEARTBEAT_SUB_GET);
}
int bt_mesh_cfg_hb_pub_set(bt_mesh_client_common_param_t *param,
struct bt_mesh_cfg_hb_pub *pub)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_SET, 9);
if (!pub) {
BT_ERR("Invalid heartbeat pub set");
return -EINVAL;
}
bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_PUB_SET);
net_buf_simple_add_le16(&msg, pub->dst);
net_buf_simple_add_u8(&msg, pub->count);
net_buf_simple_add_u8(&msg, pub->period);
net_buf_simple_add_u8(&msg, pub->ttl);
net_buf_simple_add_le16(&msg, pub->feat);
net_buf_simple_add_le16(&msg, pub->net_idx);
return bt_mesh_client_send_msg(param, &msg, true, timeout_handler);
}
int bt_mesh_cfg_hb_pub_get(bt_mesh_client_common_param_t *param)
{
return send_msg_with_none(param, OP_HEARTBEAT_PUB_GET);
}
int bt_mesh_cfg_node_reset(bt_mesh_client_common_param_t *param)