-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
provisioner_prov.c
3541 lines (2902 loc) · 105 KB
/
provisioner_prov.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
/*
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include "crypto.h"
#include "adv.h"
#include "scan.h"
#include "mesh.h"
#include "access.h"
#include "settings.h"
#include "fast_prov.h"
#include "mesh_common.h"
#include "proxy_client.h"
#include "provisioner_prov.h"
#include "provisioner_main.h"
#if CONFIG_BLE_MESH_PROVISIONER
_Static_assert(BLE_MESH_MAX_CONN >= CONFIG_BLE_MESH_PBG_SAME_TIME,
"Too large BLE Mesh PB-GATT count");
/* 3 transmissions, 20ms interval */
#define PROV_XMIT BLE_MESH_TRANSMIT(2, 20)
#define AUTH_METHOD_NO_OOB 0x00
#define AUTH_METHOD_STATIC 0x01
#define AUTH_METHOD_OUTPUT 0x02
#define AUTH_METHOD_INPUT 0x03
#define OUTPUT_OOB_BLINK 0x00
#define OUTPUT_OOB_BEEP 0x01
#define OUTPUT_OOB_VIBRATE 0x02
#define OUTPUT_OOB_NUMBER 0x03
#define OUTPUT_OOB_STRING 0x04
#define INPUT_OOB_PUSH 0x00
#define INPUT_OOB_TWIST 0x01
#define INPUT_OOB_NUMBER 0x02
#define INPUT_OOB_STRING 0x03
#define PROV_ERR_NONE 0x00
#define PROV_ERR_NVAL_PDU 0x01
#define PROV_ERR_NVAL_FMT 0x02
#define PROV_ERR_UNEXP_PDU 0x03
#define PROV_ERR_CFM_FAILED 0x04
#define PROV_ERR_RESOURCES 0x05
#define PROV_ERR_DECRYPT 0x06
#define PROV_ERR_UNEXP_ERR 0x07
#define PROV_ERR_ADDR 0x08
#define PROV_INVITE 0x00
#define PROV_CAPABILITIES 0x01
#define PROV_START 0x02
#define PROV_PUB_KEY 0x03
#define PROV_INPUT_COMPLETE 0x04
#define PROV_CONFIRM 0x05
#define PROV_RANDOM 0x06
#define PROV_DATA 0x07
#define PROV_COMPLETE 0x08
#define PROV_FAILED 0x09
#define PROV_ALG_P256 0x00
#define GPCF(gpc) (gpc & 0x03)
#define GPC_START(last_seg) (((last_seg) << 2) | 0x00)
#define GPC_ACK 0x01
#define GPC_CONT(seg_id) (((seg_id) << 2) | 0x02)
#define GPC_CTL(op) (((op) << 2) | 0x03)
#define START_PAYLOAD_MAX 20
#define CONT_PAYLOAD_MAX 23
#define START_LAST_SEG_MAX 2
#define START_LAST_SEG(gpc) (gpc >> 2)
#define CONT_SEG_INDEX(gpc) (gpc >> 2)
#define BEARER_CTL(gpc) (gpc >> 2)
#define LINK_OPEN 0x00
#define LINK_ACK 0x01
#define LINK_CLOSE 0x02
#define CLOSE_REASON_SUCCESS 0x00
#define CLOSE_REASON_TIMEOUT 0x01
#define CLOSE_REASON_FAILED 0x02
#define PROV_AUTH_VAL_SIZE 0x10
#define PROV_CONF_SALT_SIZE 0x10
#define PROV_CONF_KEY_SIZE 0x10
#define PROV_DH_KEY_SIZE 0x20
#define PROV_CONFIRM_SIZE 0x10
#define PROV_RANDOM_SIZE 0x10
#define PROV_PROV_SALT_SIZE 0x10
#define PROV_CONF_INPUTS_SIZE 0x91
#define XACT_SEG_DATA(_idx, _seg) (&link[_idx].rx.buf->data[20 + ((_seg - 1) * 23)])
#define XACT_SEG_RECV(_idx, _seg) (link[_idx].rx.seg &= ~(1 << (_seg)))
#define XACT_NVAL 0xff
enum {
REMOTE_PUB_KEY, /* Remote key has been received */
LOCAL_PUB_KEY, /* Local public key is available */
LINK_ACTIVE, /* Link has been opened */
WAIT_GEN_DHKEY, /* Waiting for remote public key to generate DHKey */
HAVE_DHKEY, /* DHKey has been calculated */
SEND_CONFIRM, /* Waiting to send Confirm value */
WAIT_NUMBER, /* Waiting for number input from user */
WAIT_STRING, /* Waiting for string input from user */
TIMEOUT_START, /* Provision timeout timer has started */
NUM_FLAGS,
};
/** Provisioner link structure allocation
* |--------------------------------------------------------|
* | Link(PB-ADV) | Link(PB-GATT) |
* |--------------------------------------------------------|
* |<----------------------Total Link---------------------->|
*/
struct prov_link {
BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS);
uint8_t uuid[16]; /* check if device is being provisioned*/
uint16_t oob_info; /* oob info of this device */
uint8_t element_num; /* element num of device */
uint8_t ki_flags; /* Key refresh flag and iv update flag */
uint32_t iv_index; /* IV Index */
uint8_t auth_method; /* Choosen authentication method */
uint8_t auth_action; /* Choosen authentication action */
uint8_t auth_size; /* Choosen authentication size */
uint16_t assign_addr; /* Application assigned address for the device */
uint16_t unicast_addr; /* unicast address allocated for device */
bt_mesh_addr_t addr; /* Device address */
#if defined(CONFIG_BLE_MESH_PB_GATT)
bool connecting; /* start connecting with device */
struct bt_mesh_conn *conn; /* GATT connection */
#endif
uint8_t expect; /* Next expected PDU */
uint8_t *dhkey; /* Calculated DHKey */
uint8_t *auth; /* Authentication Value */
uint8_t *conf_salt; /* ConfirmationSalt */
uint8_t *conf_key; /* ConfirmationKey */
uint8_t *conf_inputs; /* ConfirmationInputs */
uint8_t *rand; /* Local Random */
uint8_t *conf; /* Remote Confirmation */
uint8_t *local_conf; /* Local Confirmation */
uint8_t *prov_salt; /* Provisioning Salt */
#if defined(CONFIG_BLE_MESH_PB_ADV)
bool linking; /* Linking is being establishing */
uint16_t send_link_close; /* Link close is being sent flag */
uint32_t link_id; /* Link ID */
uint8_t pending_ack; /* Decide which transaction id ack is pending */
uint8_t expect_ack_for; /* Transaction ACK expected for provisioning pdu */
uint8_t tx_pdu_type; /* The current transmitted Provisioning PDU type */
struct {
uint8_t trans_id; /* Transaction ID */
uint8_t prev_id; /* Previous Transaction ID */
uint8_t seg; /* Bit-field of unreceived segments */
uint8_t last_seg; /* Last segment (to check length) */
uint8_t fcs; /* Expected FCS value */
uint8_t adv_buf_id; /* index of buf allocated in adv_buf_data */
struct net_buf_simple *buf;
} rx;
struct {
/* Start timestamp of the transaction */
int64_t start;
/* Transaction id*/
uint8_t trans_id;
/* Pending outgoing buffer(s) */
struct net_buf *buf[3];
/* Retransmit timer */
struct k_delayed_work retransmit;
} tx;
#endif
/** Provision timeout timer. Spec P259 says: The provisioning protocol
* shall have a minimum timeout of 60 seconds that is reset each time
* a provisioning protocol PDU is sent or received.
*/
struct k_delayed_work timeout;
};
/* Number of devices can be provisioned at the same time equals to PB-ADV + PB-GATT */
#define BLE_MESH_PROV_SAME_TIME \
(CONFIG_BLE_MESH_PBA_SAME_TIME + CONFIG_BLE_MESH_PBG_SAME_TIME)
#define PROV_MAX_ADDR_TO_ASSIGN 0x7FFF
static struct prov_link link[BLE_MESH_PROV_SAME_TIME];
struct prov_rx {
uint32_t link_id;
uint8_t xact_id;
uint8_t gpc;
};
struct bt_mesh_prov_ctx {
/* Primary element address of Provisioner */
uint16_t primary_addr;
/* Provisioning bearers used by Provisioner */
bt_mesh_prov_bearer_t bearers;
/* Current number of PB-ADV provisioned devices simultaneously */
uint8_t pba_count;
/* Current number of PB-GATT provisioned devices simultaneously */
uint8_t pbg_count;
/* Current unicast address going to allocated */
uint16_t curr_alloc_addr;
/* Current net_idx going to be used in provisioning data */
uint16_t curr_net_idx;
/* Current flags going to be used in provisioning data */
uint8_t curr_flags;
/* Current iv_index going to be used in provisioning data */
uint16_t curr_iv_index;
/* Length of Static OOB value */
uint8_t static_oob_len;
/* Static OOB value */
uint8_t static_oob_val[16];
/* Offset of the device uuid to be matched, based on zero */
uint8_t match_offset;
/* Length of the device uuid to be matched (start from the match_offset) */
uint8_t match_length;
/* Value of the device uuid to be matched */
uint8_t match_value[16];
/* Indicate when received uuid_match adv_pkts, can provision it at once */
bool prov_after_match;
#if defined(CONFIG_BLE_MESH_PB_ADV)
/* Mutex used to protect the PB-ADV procedure */
bt_mesh_mutex_t pb_adv_lock;
/* Mutex used to protect the adv buf during PB-ADV procedure */
bt_mesh_mutex_t pb_buf_lock;
#endif
#if defined(CONFIG_BLE_MESH_PB_GATT)
/* Mutex used to protect the PB-GATT procedure */
bt_mesh_mutex_t pb_gatt_lock;
#endif
/* Fast provisioning related information */
struct {
bool enable;
uint16_t net_idx;
uint8_t flags;
uint32_t iv_index;
uint16_t unicast_addr_min;
uint16_t unicast_addr_max;
} fast_prov;
};
static struct bt_mesh_prov_ctx prov_ctx;
#define FAST_PROV_ENABLE() (prov_ctx.fast_prov.enable)
struct unprov_dev_queue {
bt_mesh_addr_t addr;
uint8_t uuid[16];
uint16_t oob_info;
uint8_t bearer;
uint8_t flags;
} __packed unprov_dev[CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM] = {
[0 ... (CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM - 1)] = {
.addr.type = 0xff,
.bearer = 0,
.flags = false,
},
};
static unprov_adv_pkt_cb_t notify_unprov_adv_pkt_cb;
#define BUF_TIMEOUT K_MSEC(400)
#if defined(CONFIG_BLE_MESH_FAST_PROV)
#define RETRANSMIT_TIMEOUT K_MSEC(360)
#define TRANSACTION_TIMEOUT K_SECONDS(3)
#define PROVISION_TIMEOUT K_SECONDS(6)
#else
#define RETRANSMIT_TIMEOUT K_MSEC(500)
#define TRANSACTION_TIMEOUT K_SECONDS(30)
#define PROVISION_TIMEOUT K_SECONDS(60)
#endif /* CONFIG_BLE_MESH_FAST_PROV */
#if defined(CONFIG_BLE_MESH_PB_GATT)
#define PROV_BUF_HEADROOM 5
#else
#define PROV_BUF_HEADROOM 0
#endif
#define PROV_BUF(name, len) \
NET_BUF_SIMPLE_DEFINE(name, PROV_BUF_HEADROOM + len)
static const struct bt_mesh_prov *prov;
#if defined(CONFIG_BLE_MESH_PB_ADV)
static void send_link_open(const uint8_t idx);
#endif
static void prov_gen_dh_key(const uint8_t idx);
static void send_pub_key(const uint8_t idx, uint8_t oob);
static void close_link(const uint8_t idx, uint8_t reason);
#if defined(CONFIG_BLE_MESH_PB_ADV)
#define ADV_BUF_SIZE 65
static struct prov_adv_buf {
struct net_buf_simple buf;
} adv_buf[CONFIG_BLE_MESH_PBA_SAME_TIME];
static uint8_t adv_buf_data[ADV_BUF_SIZE * CONFIG_BLE_MESH_PBA_SAME_TIME];
#endif /* CONFIG_BLE_MESH_PB_ADV */
#define PROV_FREE_MEM(_idx, member) \
{ \
if (link[_idx].member) { \
bt_mesh_free(link[_idx].member); \
link[_idx].member = NULL; \
} \
}
#if defined(CONFIG_BLE_MESH_PB_ADV)
static inline void bt_mesh_pb_adv_mutex_new(void)
{
if (!prov_ctx.pb_adv_lock.mutex) {
bt_mesh_mutex_create(&prov_ctx.pb_adv_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_pb_adv_mutex_free(void)
{
bt_mesh_mutex_free(&prov_ctx.pb_adv_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_pb_adv_lock(void)
{
bt_mesh_mutex_lock(&prov_ctx.pb_adv_lock);
}
static inline void bt_mesh_pb_adv_unlock(void)
{
bt_mesh_mutex_unlock(&prov_ctx.pb_adv_lock);
}
static inline void bt_mesh_pb_buf_mutex_new(void)
{
if (!prov_ctx.pb_buf_lock.mutex) {
bt_mesh_mutex_create(&prov_ctx.pb_buf_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_pb_buf_mutex_free(void)
{
bt_mesh_mutex_free(&prov_ctx.pb_buf_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_pb_buf_lock(void)
{
bt_mesh_mutex_lock(&prov_ctx.pb_buf_lock);
}
static inline void bt_mesh_pb_buf_unlock(void)
{
bt_mesh_mutex_unlock(&prov_ctx.pb_buf_lock);
}
#endif /* CONFIG_BLE_MESH_PB_ADV */
#if defined(CONFIG_BLE_MESH_PB_GATT)
static inline void bt_mesh_pb_gatt_mutex_new(void)
{
if (!prov_ctx.pb_gatt_lock.mutex) {
bt_mesh_mutex_create(&prov_ctx.pb_gatt_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_pb_gatt_mutex_free(void)
{
bt_mesh_mutex_free(&prov_ctx.pb_gatt_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
static inline void bt_mesh_pb_gatt_lock(void)
{
bt_mesh_mutex_lock(&prov_ctx.pb_gatt_lock);
}
static inline void bt_mesh_pb_gatt_unlock(void)
{
bt_mesh_mutex_unlock(&prov_ctx.pb_gatt_lock);
}
void bt_mesh_provisioner_pbg_count_dec(void)
{
if (prov_ctx.pbg_count) {
prov_ctx.pbg_count--;
}
}
static inline void provisioner_pbg_count_inc(void)
{
prov_ctx.pbg_count++;
}
void bt_mesh_provisioner_clear_link_info(const uint8_t addr[6])
{
int i;
if (!addr) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
BT_DBG("Clear device info, addr %s", bt_hex(addr, BLE_MESH_ADDR_LEN));
for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (!memcmp(link[i].addr.val, addr, BLE_MESH_ADDR_LEN)) {
link[i].connecting = false;
link[i].conn = NULL;
link[i].oob_info = 0x0;
memset(link[i].uuid, 0, 16);
memset(&link[i].addr, 0, sizeof(bt_mesh_addr_t));
bt_mesh_atomic_test_and_clear_bit(link[i].flags, LINK_ACTIVE);
if (bt_mesh_atomic_test_and_clear_bit(link[i].flags, TIMEOUT_START)) {
k_delayed_work_cancel(&link[i].timeout);
}
return;
}
}
BT_WARN("Device not found, addr %s", bt_hex(addr, BLE_MESH_ADDR_LEN));
return;
}
#endif /* CONFIG_BLE_MESH_PB_GATT */
const struct bt_mesh_prov *bt_mesh_provisioner_get_prov_info(void)
{
return prov;
}
void bt_mesh_provisioner_restore_prov_info(uint16_t primary_addr, uint16_t alloc_addr)
{
prov_ctx.primary_addr = primary_addr;
prov_ctx.curr_alloc_addr = alloc_addr;
}
static bool is_unprov_dev_being_provision(const uint8_t uuid[16])
{
int i;
#if defined(CONFIG_BLE_MESH_FAST_PROV)
/**
* During Fast Provisioning test, we found that if a device has already being
* provisioned, there is still a chance that the Provisioner can receive the
* Unprovisioned Device Beacon from the device (because the device will stop
* Unprovisioned Device Beacon when Transaction ACK for Provisioning Complete
* is received). So in Fast Provisioning the Provisioner should ignore this.
*/
if (bt_mesh_provisioner_get_node_with_uuid(uuid)) {
BT_WARN("Device has already been provisioned");
return true;
}
#endif
for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) {
#if defined(CONFIG_BLE_MESH_PB_ADV) && defined(CONFIG_BLE_MESH_PB_GATT)
if (link[i].linking || link[i].connecting ||
bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) {
#elif defined(CONFIG_BLE_MESH_PB_ADV) && !defined(CONFIG_BLE_MESH_PB_GATT)
if (link[i].linking || bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) {
#else
if (link[i].connecting || bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) {
#endif
if (!memcmp(link[i].uuid, uuid, 16)) {
BT_DBG("Device is being provisioning");
return true;
}
}
}
return false;
}
static bool is_unprov_dev_uuid_match(const uint8_t uuid[16])
{
if (prov_ctx.match_length) {
if (memcmp(uuid + prov_ctx.match_offset,
prov_ctx.match_value, prov_ctx.match_length)) {
return false;
}
}
return true;
}
static int provisioner_check_unprov_dev_info(const uint8_t uuid[16], bt_mesh_prov_bearer_t bearer)
{
if (!uuid) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
/* Check if the device uuid matches configured value */
if (is_unprov_dev_uuid_match(uuid) == false) {
BT_DBG("Device uuid mismatch");
return -EIO;
}
/* Check if this device is currently being provisioned.
* According to Zephyr's device code, if we connect with
* one device and start to provision it, we may still can
* receive the connectable prov adv pkt from this device.
* Here we check both PB-GATT and PB-ADV link status.
*/
if (is_unprov_dev_being_provision(uuid)) {
return -EALREADY;
}
/* Check if the current PB-ADV link is full */
if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
(prov_ctx.bearers & BLE_MESH_PROV_ADV) &&
(bearer == BLE_MESH_PROV_ADV) &&
(prov_ctx.pba_count == CONFIG_BLE_MESH_PBA_SAME_TIME)) {
BT_INFO("Current PB-ADV links reach max limit");
return -ENOMEM;
}
/* Check if the current PB-GATT link is full */
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
(prov_ctx.bearers & BLE_MESH_PROV_GATT) &&
(bearer == BLE_MESH_PROV_GATT) &&
(prov_ctx.pbg_count == CONFIG_BLE_MESH_PBG_SAME_TIME)) {
BT_INFO("Current PB-GATT links reach max limit");
return -ENOMEM;
}
/* Check if the device has already been provisioned */
if (bt_mesh_provisioner_get_node_with_uuid(uuid)) {
BT_INFO("Provisioned before, start to provision again");
}
return 0;
}
#if defined(CONFIG_BLE_MESH_PB_ADV)
static int provisioner_start_prov_pb_adv(const uint8_t uuid[16], const bt_mesh_addr_t *addr,
uint16_t oob_info, uint16_t assign_addr)
{
int i;
if (uuid == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
bt_mesh_pb_adv_lock();
/* If the unicast address of the node is going to be allocated internally,
* then we need to check if there are addresses can be allocated.
*/
if (assign_addr == BLE_MESH_ADDR_UNASSIGNED &&
prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) {
BT_ERR("No available unicast address to assign");
bt_mesh_pb_adv_unlock();
return -EIO;
}
if (is_unprov_dev_being_provision(uuid)) {
bt_mesh_pb_adv_unlock();
return -EALREADY;
}
for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) {
if (!bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE) && !link[i].linking) {
memcpy(link[i].uuid, uuid, 16);
link[i].oob_info = oob_info;
if (addr) {
link[i].addr.type = addr->type;
memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN);
}
send_link_open(i);
/* If a specific unicast address is assigned for the device, then
* Provisioner will use this address in the Provisioning Data PDU.
*/
if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) {
link[i].assign_addr = assign_addr;
}
/* Increase PB-ADV link count */
prov_ctx.pba_count++;
bt_mesh_pb_adv_unlock();
return 0;
}
}
BT_ERR("No PB-ADV link available");
bt_mesh_pb_adv_unlock();
return -ENOMEM;
}
#endif /* CONFIG_BLE_MESH_PB_ADV */
#if defined(CONFIG_BLE_MESH_PB_GATT)
static int provisioner_start_prov_pb_gatt(const uint8_t uuid[16], const bt_mesh_addr_t *addr,
uint16_t oob_info, uint16_t assign_addr)
{
int i;
if (uuid == NULL || addr == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
bt_mesh_pb_gatt_lock();
/* If the unicast address of the node is going to be allocated internally,
* then we need to check if there are addresses can be allocated.
*/
if (assign_addr == BLE_MESH_ADDR_UNASSIGNED &&
prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) {
BT_ERR("No available unicast address to assign");
bt_mesh_pb_gatt_unlock();
return -EIO;
}
if (is_unprov_dev_being_provision(uuid)) {
bt_mesh_pb_gatt_unlock();
return -EALREADY;
}
for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (!link[i].connecting && !bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) {
if (bt_mesh_gattc_conn_create(addr, BLE_MESH_UUID_MESH_PROV_VAL)) {
bt_mesh_pb_gatt_unlock();
return -EIO;
}
memcpy(link[i].uuid, uuid, 16);
link[i].oob_info = oob_info;
link[i].addr.type = addr->type;
memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN);
/* If the application layer assigned a specific unicast address for the device,
* then Provisioner will use this address in the Provisioning Data PDU.
*/
if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) {
link[i].assign_addr = assign_addr;
}
/* If creating connection successfully, set connecting flag to 1 */
link[i].connecting = true;
/* Increase PB-GATT link count */
provisioner_pbg_count_inc();
bt_mesh_pb_gatt_unlock();
return 0;
}
}
BT_ERR("No PB-GATT link available");
bt_mesh_pb_gatt_unlock();
return -ENOMEM;
}
#endif /* CONFIG_BLE_MESH_PB_GATT */
int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, uint8_t flags)
{
bt_mesh_addr_t add_addr = {0};
bool addr_valid = false;
uint8_t zero[16] = {0};
int err = 0;
int i;
if (add_dev == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
if (!memcmp(add_dev->uuid, zero, 16)) {
BT_ERR("Invalid device uuid to add");
return -EINVAL;
}
if (!(add_dev->bearer & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) {
BT_ERR("Invalid bearer 0x%02x", add_dev->bearer);
return -EINVAL;
}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) ||
!(prov_ctx.bearers & BLE_MESH_PROV_GATT))
&& (add_dev->bearer & BLE_MESH_PROV_GATT)) {
BT_ERR("Not support PB-GATT");
return -EINVAL;
}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) ||
!(prov_ctx.bearers & BLE_MESH_PROV_ADV))
&& (add_dev->bearer & BLE_MESH_PROV_ADV)) {
BT_ERR("Not support PB-ADV");
return -EINVAL;
}
if (memcmp(add_dev->addr, zero, BLE_MESH_ADDR_LEN)) {
addr_valid = true;
add_addr.type = add_dev->addr_type;
memcpy(add_addr.val, add_dev->addr, BLE_MESH_ADDR_LEN);
}
/* Pb-GATT needs device address to create connection */
if ((add_dev->bearer & BLE_MESH_PROV_GATT) && (addr_valid == false)) {
BT_ERR("Invalid device address for PB-GATT");
return -EINVAL;
}
/* If start provisioning immediately, only one bearer can be used */
if ((flags & START_PROV_NOW) &&
(add_dev->bearer != BLE_MESH_PROV_ADV) &&
(add_dev->bearer != BLE_MESH_PROV_GATT)) {
BT_ERR("Can not start PB-ADV & PB-GATT simultaneously");
return -EINVAL;
}
/* Check if the provisioned nodes array is full */
if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL) {
if (bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Current provisioned devices reach max limit");
return -ENOMEM;
}
}
/* Check if the device already exists in queue */
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (!memcmp(unprov_dev[i].uuid, add_dev->uuid, 16)) {
if (!(add_dev->bearer & unprov_dev[i].bearer)) {
BT_WARN("Add device with only bearer updated");
unprov_dev[i].bearer |= add_dev->bearer;
} else {
BT_WARN("Device already exists in queue");
}
goto start;
}
}
/* If not exists, try to add the device into queue */
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (unprov_dev[i].bearer) {
continue;
}
if (addr_valid) {
unprov_dev[i].addr.type = add_dev->addr_type;
memcpy(unprov_dev[i].addr.val, add_dev->addr, BLE_MESH_ADDR_LEN);
}
memcpy(unprov_dev[i].uuid, add_dev->uuid, 16);
unprov_dev[i].bearer = add_dev->bearer & BIT_MASK(2);
unprov_dev[i].flags = flags & BIT_MASK(3);
goto start;
}
/* If queue is full, find flushable device and replace it */
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (unprov_dev[i].flags & FLUSHABLE_DEV) {
memset(&unprov_dev[i], 0, sizeof(struct unprov_dev_queue));
if (addr_valid) {
unprov_dev[i].addr.type = add_dev->addr_type;
memcpy(unprov_dev[i].addr.val, add_dev->addr, BLE_MESH_ADDR_LEN);
}
memcpy(unprov_dev[i].uuid, add_dev->uuid, 16);
unprov_dev[i].bearer = add_dev->bearer & BIT_MASK(2);
unprov_dev[i].flags = flags & BIT_MASK(3);
goto start;
}
}
BT_ERR("Unprovisioned device queue is full");
return -ENOMEM;
start:
/* If not provisioning immediately, directly return here */
if (!(flags & START_PROV_NOW)) {
return 0;
}
/* Check if current provisioned node count + active link reach max limit */
if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL) {
if (bt_mesh_provisioner_get_node_count() + prov_ctx.pba_count + \
prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Node count + active link count reach max limit");
return -EIO;
}
}
if ((err = provisioner_check_unprov_dev_info(add_dev->uuid, add_dev->bearer))) {
return err;
}
if (add_dev->bearer == BLE_MESH_PROV_ADV) {
#if defined(CONFIG_BLE_MESH_PB_ADV)
if ((err = provisioner_start_prov_pb_adv(add_dev->uuid, addr_valid ? &add_addr : NULL,
add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED))) {
return err;
}
#endif
} else if (add_dev->bearer == BLE_MESH_PROV_GATT) {
#if defined(CONFIG_BLE_MESH_PB_GATT)
if ((err = provisioner_start_prov_pb_gatt(add_dev->uuid, &add_addr, add_dev->oob_info,
BLE_MESH_ADDR_UNASSIGNED))) {
return err;
}
#endif
}
return 0;
}
int bt_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], const uint8_t addr[6],
uint8_t addr_type, bt_mesh_prov_bearer_t bearer,
uint16_t oob_info, uint16_t unicast_addr)
{
bt_mesh_addr_t dev_addr = {0};
int err = 0;
if (uuid == NULL) {
BT_ERR("Invalid device uuid");
return -EINVAL;
}
if (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT) {
BT_ERR("Invalid provisioning bearer 0x%02x", bearer);
return -EINVAL;
}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) ||
!(prov_ctx.bearers & BLE_MESH_PROV_ADV)) &&
(bearer == BLE_MESH_PROV_ADV)) {
BT_ERR("Not support PB-ADV");
return -ENOTSUP;
}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) ||
!(prov_ctx.bearers & BLE_MESH_PROV_GATT)) &&
(bearer == BLE_MESH_PROV_GATT)) {
BT_ERR("Not support PB-GATT");
return -ENOTSUP;
}
if (bearer == BLE_MESH_PROV_GATT && addr == NULL) {
BT_ERR("Invalid device address for PB-GATT");
return -EINVAL;
}
if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) {
BT_ERR("Invalid unicast address 0x%04x", unicast_addr);
return -EINVAL;
}
/* Here we will not check if the assigned unicast address is overlapped
* with the unicast addresses of other nodes or Provisioner, because:
* 1. At this moment, the element number of the device is unknown
* 2. If the node is a reprovisioned device, then the original allocated
* unicast address will be used.
* 3. Some other devices may be just being provisioning, and currently we
* can not know the exactly allocated addresses of them.
*/
if (bt_mesh_provisioner_get_node_with_uuid(uuid) == NULL) {
/* Check if the provisioned nodes array is full */
if (bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Current provisioned devices reach max limit");
return -ENOMEM;
}
/* Check if current provisioned node count + active link reach max limit */
if (bt_mesh_provisioner_get_node_count() + prov_ctx.pba_count + \
prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Node count + active link count reach max limit");
return -EIO;
}
}
if ((err = provisioner_check_unprov_dev_info(uuid, bearer))) {
return err;
}
if (addr) {
dev_addr.type = addr_type;
memcpy(dev_addr.val, addr, BLE_MESH_ADDR_LEN);
}
if (bearer == BLE_MESH_PROV_ADV) {
#if defined(CONFIG_BLE_MESH_PB_ADV)
if ((err = provisioner_start_prov_pb_adv(uuid, addr ? &dev_addr : NULL, oob_info, unicast_addr))) {
return err;
}
#endif
} else if (bearer == BLE_MESH_PROV_GATT) {
#if defined(CONFIG_BLE_MESH_PB_GATT)
if ((err = provisioner_start_prov_pb_gatt(uuid, &dev_addr, oob_info, unicast_addr))) {
return err;
}
#endif
}
return 0;
}
int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev)
{
uint8_t zero[16] = {0};
int i;
if (del_dev == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
if (!memcmp(del_dev->uuid, zero, 16)) {
BT_ERR("Invalid device uuid to delete");
return -EINVAL;
}
/* Find if the device is in the device queue */
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (!memcmp(unprov_dev[i].uuid, del_dev->uuid, 16)) {
memset(&unprov_dev[i], 0, sizeof(struct unprov_dev_queue));
break;
}
}
/* Find if the device is being provisioned */
for (i = 0; i < ARRAY_SIZE(link); i++) {
if (!memcmp(link[i].uuid, del_dev->uuid, 16)) {
close_link(i, CLOSE_REASON_FAILED);
break;
}
}
return 0;
}
int bt_mesh_provisioner_set_dev_uuid_match(uint8_t offset, uint8_t length,
const uint8_t *match, bool prov_flag)
{
if (length && (!match || (offset + length > 16))) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
(void)memset(prov_ctx.match_value, 0, 16);
prov_ctx.match_offset = offset;
prov_ctx.match_length = length;
if (length) {
memcpy(prov_ctx.match_value, match, length);
}
prov_ctx.prov_after_match = prov_flag;
return 0;
}
int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb)
{
if (!cb) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;