-
Notifications
You must be signed in to change notification settings - Fork 488
Expand file tree
/
Copy pathbtp_gap.c
More file actions
2511 lines (2110 loc) · 71.1 KB
/
btp_gap.c
File metadata and controls
2511 lines (2110 loc) · 71.1 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* gap.c - Bluetooth GAP Tester */
/*
* Copyright (c) 2015-2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "host/ble_gap.h"
#include "host/util/util.h"
#include "console/console.h"
#include "../../../nimble/host/src/ble_hs_pvcy_priv.h"
#include "../../../nimble/host/src/ble_hs_hci_priv.h"
#include "../../../nimble/host/src/ble_sm_priv.h"
#include "btp/btp.h"
#include <errno.h>
#define CONTROLLER_INDEX 0
#define CONTROLLER_NAME "btp_tester"
#define BLE_AD_DISCOV_MASK (BLE_HS_ADV_F_DISC_LTD | BLE_HS_ADV_F_DISC_GEN)
#define ADV_BUF_LEN (sizeof(struct btp_gap_device_found_ev) + 2 * 31)
/* parameter values to reject in CPUP if all match the pattern */
#define REJECT_INTERVAL_MIN 0x0C80
#define REJECT_INTERVAL_MAX 0x0C80
#define REJECT_LATENCY 0x0000
#define REJECT_SUPERVISION_TIMEOUT 0x0C80
const uint8_t irk[16] = {
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
};
static uint8_t oob[16];
static struct ble_sm_sc_oob_data oob_data_local;
static struct ble_sm_sc_oob_data oob_data_remote;
static uint16_t current_settings;
uint8_t own_addr_type;
static ble_addr_t peer_id_addr;
static ble_addr_t peer_ota_addr;
static bool encrypted = false;
static bool use_filter_policy = false;
static struct os_callout update_params_co;
static struct btp_gap_conn_param_update_cmd update_params;
static struct os_callout connected_ev_co;
static struct btp_gap_device_connected_ev connected_ev;
#define CONNECTED_EV_DELAY_MS(itvl) 8 * BLE_HCI_CONN_ITVL * itvl / 1000
static int connection_attempts;
#if MYNEWT_VAL(BTTESTER_PRIVACY_MODE) && MYNEWT_VAL(BTTESTER_USE_NRPA)
static int64_t advertising_start;
static struct os_callout bttester_nrpa_rotate_timer;
#endif
static const struct ble_gap_conn_params dflt_conn_params = {
.scan_itvl = 0x0010,
.scan_window = 0x0010,
.itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN,
.itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX,
.latency = 0,
.supervision_timeout = 0x0100,
.min_ce_len = 0x0010,
.max_ce_len = 0x0300,
};
static int
gap_conn_find_by_addr(const ble_addr_t *dev_addr,
struct ble_gap_conn_desc *out_desc)
{
ble_addr_t addr = *dev_addr;
if (memcmp(BLE_ADDR_ANY, &peer_id_addr, 6) == 0) {
return ble_gap_conn_find_by_addr(&addr, out_desc);
}
if (BLE_ADDR_IS_RPA(&addr)) {
if (ble_addr_cmp(&peer_ota_addr, &addr) != 0) {
return -1;
}
return ble_gap_conn_find_by_addr(&addr, out_desc);
} else {
if (ble_addr_cmp(&peer_id_addr, &addr) != 0) {
return -1;
}
if (BLE_ADDR_IS_RPA(&peer_ota_addr)) {
/* Change addr type to ID addr */
addr.type |= 2;
}
return ble_gap_conn_find_by_addr(&addr, out_desc);
}
}
static int
gap_event_cb(struct ble_gap_event *event, void *arg);
static uint8_t
supported_commands(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
struct btp_gap_read_supported_commands_rp *rp = rsp;
*rsp_len = tester_supported_commands(BTP_SERVICE_ID_GAP, rp->data);
*rsp_len += sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static uint8_t
controller_index_list(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
struct btp_gap_read_controller_index_list_rp *rp = rsp;
SYS_LOG_DBG("");
rp->num = 1;
rp->index[0] = CONTROLLER_INDEX;
*rsp_len = sizeof(*rp) + 1;
return BTP_STATUS_SUCCESS;
}
#if MYNEWT_VAL(BLE_EXT_ADV)
static struct ble_gap_ext_adv_params adv_params = {
.primary_phy = BLE_HCI_LE_PHY_1M,
.secondary_phy = BLE_HCI_LE_PHY_1M,
.sid = 1,
.legacy_pdu = 1,
};
#else
static struct ble_gap_adv_params adv_params = {
.conn_mode = BLE_GAP_CONN_MODE_NON,
.disc_mode = BLE_GAP_DISC_MODE_NON,
};
#endif
static uint8_t
controller_info(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
struct btp_gap_read_controller_info_rp *rp = rsp;
uint32_t supported_settings = 0;
ble_addr_t addr;
int rc;
SYS_LOG_DBG("");
rc = ble_hs_pvcy_set_our_irk(irk);
assert(rc == 0);
/* Make sure we have proper identity address set (public preferred) */
rc = ble_hs_util_ensure_addr(1);
assert(rc == 0);
rc = ble_hs_id_copy_addr(BLE_ADDR_RANDOM, addr.val, NULL);
assert(rc == 0);
if (MYNEWT_VAL(BTTESTER_PRIVACY_MODE)) {
if (MYNEWT_VAL(BTTESTER_USE_NRPA)) {
own_addr_type = BLE_OWN_ADDR_RANDOM;
rc = ble_hs_id_gen_rnd(1, &addr);
assert(rc == 0);
rc = ble_hs_id_set_rnd(addr.val);
assert(rc == 0);
} else {
own_addr_type = BLE_OWN_ADDR_RPA_RANDOM_DEFAULT;
}
current_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY);
supported_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY);
memcpy(&rp->address, &addr, sizeof(rp->address));
} else {
rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, rp->address, NULL);
if (rc) {
own_addr_type = BLE_OWN_ADDR_RANDOM;
memcpy(rp->address, addr.val, sizeof(rp->address));
supported_settings |= BIT(BTP_GAP_SETTINGS_STATIC_ADDRESS);
current_settings |= BIT(BTP_GAP_SETTINGS_STATIC_ADDRESS);
} else {
own_addr_type = BLE_OWN_ADDR_PUBLIC;
}
}
supported_settings |= BIT(BTP_GAP_SETTINGS_POWERED);
supported_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE);
supported_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE);
supported_settings |= BIT(BTP_GAP_SETTINGS_LE);
supported_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING);
supported_settings |= BIT(BTP_GAP_SETTINGS_SC);
#if MYNEWT_VAL(BLE_EXT_ADV)
if (adv_params.connectable) {
current_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE);
} else {
current_settings &= ~BIT(BTP_GAP_SETTINGS_CONNECTABLE);
}
#else
if (adv_params.conn_mode != BLE_GAP_CONN_MODE_NON) {
current_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE);
} else {
current_settings &= ~BIT(BTP_GAP_SETTINGS_CONNECTABLE);
}
#endif
if (ble_hs_cfg.sm_bonding) {
current_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE);
}
if (ble_hs_cfg.sm_sc) {
current_settings |= BIT(BTP_GAP_SETTINGS_SC);
}
rp->supported_settings = htole32(supported_settings);
rp->current_settings = htole32(current_settings);
memcpy(rp->name, CONTROLLER_NAME, sizeof(CONTROLLER_NAME));
*rsp_len = sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static uint8_t ad_flags = BLE_HS_ADV_F_BREDR_UNSUP;
#if MYNEWT_VAL(BTTESTER_PRIVACY_MODE) && MYNEWT_VAL(BTTESTER_USE_NRPA)
static void rotate_nrpa_cb(struct os_event *ev)
{
int rc;
ble_addr_t addr;
int32_t duration_ms = BLE_HS_FOREVER;
int32_t remaining_time;
os_time_t remaining_ticks;
if (current_settings & BIT(BTP_GAP_SETTINGS_DISCOVERABLE)) {
if (ad_flags & BLE_HS_ADV_F_DISC_LTD) {
duration_ms = MYNEWT_VAL(BTTESTER_LTD_ADV_TIMEOUT);
}
}
#if MYNEWT_VAL(BLE_EXT_ADV)
ble_gap_ext_adv_stop(0);
#else
ble_gap_adv_stop();
#endif
rc = ble_hs_id_gen_rnd(1, &addr);
assert(rc == 0);
rc = ble_hs_id_set_rnd(addr.val);
assert(rc == 0);
#if MYNEWT_VAL(BLE_EXT_ADV)
ble_gap_ext_adv_start(0, duration_ms / 10, 0);
#else
ble_gap_adv_start(own_addr_type, NULL, duration_ms,
&adv_params, gap_event_cb, NULL);
#endif
remaining_time = os_get_uptime_usec() - advertising_start;
if (remaining_time > 0) {
advertising_start = os_get_uptime_usec();
os_time_ms_to_ticks(remaining_time, &remaining_ticks);
os_callout_reset(&bttester_nrpa_rotate_timer,
remaining_ticks);
}
}
#endif
static uint8_t
set_connectable(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_gap_set_connectable_cmd *cp = cmd;
struct btp_gap_set_connectable_rp *rp = rsp;
SYS_LOG_DBG("");
if (cp->connectable) {
current_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE);
#if MYNEWT_VAL(BLE_EXT_ADV)
adv_params.connectable = 1;
adv_params.scannable = 1;
#else
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
#endif
} else {
current_settings &= ~BIT(BTP_GAP_SETTINGS_CONNECTABLE);
#if MYNEWT_VAL(BLE_EXT_ADV)
adv_params.connectable = 0;
adv_params.scannable = 0;
#else
adv_params.conn_mode = BLE_GAP_CONN_MODE_NON;
#endif
}
rp->current_settings = htole32(current_settings);
*rsp_len = sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static uint8_t
set_discoverable(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_gap_set_discoverable_cmd *cp = cmd;
struct btp_gap_set_discoverable_rp *rp = rsp;
SYS_LOG_DBG("");
switch (cp->discoverable) {
case BTP_GAP_NON_DISCOVERABLE:
ad_flags &= ~(BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_DISC_LTD);
#if !MYNEWT_VAL(BLE_EXT_ADV)
adv_params.disc_mode = BLE_GAP_DISC_MODE_NON;
#endif
current_settings &= ~BIT(BTP_GAP_SETTINGS_DISCOVERABLE);
break;
case BTP_GAP_GENERAL_DISCOVERABLE:
ad_flags &= ~BLE_HS_ADV_F_DISC_LTD;
ad_flags |= BLE_HS_ADV_F_DISC_GEN;
#if !MYNEWT_VAL(BLE_EXT_ADV)
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
#endif
current_settings |= BIT(BTP_GAP_SETTINGS_DISCOVERABLE);
break;
case BTP_GAP_LIMITED_DISCOVERABLE:
ad_flags &= ~BLE_HS_ADV_F_DISC_GEN;
ad_flags |= BLE_HS_ADV_F_DISC_LTD;
#if !MYNEWT_VAL(BLE_EXT_ADV)
adv_params.disc_mode = BLE_GAP_DISC_MODE_LTD;
#endif
current_settings |= BIT(BTP_GAP_SETTINGS_DISCOVERABLE);
break;
default:
return BTP_STATUS_FAILED;
}
rp->current_settings = htole32(current_settings);
*rsp_len = sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static uint8_t
set_bondable(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_gap_set_bondable_cmd *cp = cmd;
struct btp_gap_set_bondable_rp *rp = rsp;
SYS_LOG_DBG("bondable: %d", cp->bondable);
ble_hs_cfg.sm_bonding = cp->bondable;
if (ble_hs_cfg.sm_bonding) {
current_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE);
} else {
current_settings &= ~BIT(BTP_GAP_SETTINGS_BONDABLE);
}
rp->current_settings = htole32(current_settings);
*rsp_len = sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static struct adv_data ad[10] = {
ADV_DATA(BLE_HS_ADV_TYPE_FLAGS, &ad_flags, sizeof(ad_flags)),
};
static struct adv_data sd[10];
static int
set_ad(const struct adv_data *ad_data, size_t ad_len,
uint8_t *buf, uint8_t *buf_len)
{
int i;
for (i = 0; i < ad_len; i++) {
buf[(*buf_len)++] = ad_data[i].data_len + 1;
buf[(*buf_len)++] = ad_data[i].type;
memcpy(&buf[*buf_len], ad_data[i].data,
ad_data[i].data_len);
*buf_len += ad_data[i].data_len;
}
return 0;
}
static uint8_t
start_advertising(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_gap_start_advertising_cmd *cp = cmd;
struct btp_gap_start_advertising_rp *rp = rsp;
uint8_t buf[BLE_HS_ADV_MAX_SZ];
uint8_t buf_len = 0;
uint8_t adv_len, sd_len = 0;
uint8_t addr_type;
uint32_t duration;
int err;
int i;
#if MYNEWT_VAL(BLE_EXT_ADV)
struct os_mbuf *ad_buf;
int duration_ms = 0;
#else
int32_t duration_ms = BLE_HS_FOREVER;
#endif
SYS_LOG_DBG("");
/* This command is very unfortunate since after variable data there is
* additional 5 bytes (4 bytes for duration, 1 byte for own address
* type.
*/
if ((cmd_len < sizeof(*cp)) ||
(cmd_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len +
sizeof(duration) + sizeof(own_addr_type))) {
return BTP_STATUS_FAILED;
}
/* currently ignored */
duration = get_le32(cp->adv_sr_data + cp->adv_data_len + cp->scan_rsp_len);
(void)duration;
addr_type = cp->adv_sr_data[cp->adv_data_len +
cp->scan_rsp_len +
sizeof(duration)];
for (i = 0, adv_len = 1U; i < cp->adv_data_len; adv_len++) {
if (adv_len >= ARRAY_SIZE(ad)) {
SYS_LOG_ERR("ad[] Out of memory");
return BTP_STATUS_FAILED;
}
ad[adv_len].type = cp->adv_sr_data[i++];
ad[adv_len].data_len = cp->adv_sr_data[i++];
ad[adv_len].data = &cp->adv_sr_data[i];
i += ad[adv_len].data_len;
}
for (sd_len = 0U; i < (cp->adv_data_len + cp->scan_rsp_len); sd_len++) {
if (sd_len >= ARRAY_SIZE(sd)) {
SYS_LOG_ERR("sd[] Out of memory");
return BTP_STATUS_FAILED;
}
sd[sd_len].type = cp->adv_sr_data[i++];
sd[sd_len].data_len = cp->adv_sr_data[i++];
sd[sd_len].data = &cp->adv_sr_data[i];
i += sd[sd_len].data_len;
}
err = set_ad(ad, adv_len, buf, &buf_len);
if (err) {
return BTP_STATUS_FAILED;
}
#if MYNEWT_VAL(BLE_EXT_ADV)
adv_params.own_addr_type = own_addr_type;
if (sd_len != 0 && adv_params.legacy_pdu) {
adv_params.scannable = 1;
}
if (use_filter_policy) {
adv_params.filter_policy = BLE_HCI_ADV_FILT_BOTH;
}
err = ble_gap_ext_adv_configure(0, &adv_params, NULL, gap_event_cb, NULL);
if (err) {
SYS_LOG_ERR("Failed to configure extended advertiser; rc=%d", err);
return BTP_STATUS_FAILED;
}
ad_buf = os_msys_get_pkthdr(BLE_HS_ADV_MAX_SZ, 0);
if (os_mbuf_append(ad_buf, buf, buf_len)) {
os_mbuf_free_chain(ad_buf);
return BTP_STATUS_FAILED;
}
err = ble_gap_ext_adv_set_data(0, ad_buf);
#else
err = ble_gap_adv_set_data(buf, buf_len);
#endif
if (err) {
SYS_LOG_ERR("Failed to set advertising data; rc=%d", err);
return BTP_STATUS_FAILED;
}
if (sd_len) {
buf_len = 0;
err = set_ad(sd, sd_len, buf, &buf_len);
if (err) {
SYS_LOG_ERR("Advertising failed: err %d", err);
return BTP_STATUS_FAILED;
}
#if MYNEWT_VAL(BLE_EXT_ADV)
ad_buf = os_msys_get_pkthdr(BLE_HS_ADV_MAX_SZ, 0);
if (os_mbuf_append(ad_buf, buf, buf_len)) {
os_mbuf_free_chain(ad_buf);
return BTP_STATUS_FAILED;
}
err = ble_gap_ext_adv_rsp_set_data(0, ad_buf);
#else
err = ble_gap_adv_rsp_set_data(buf, buf_len);
#endif
if (err != 0) {
SYS_LOG_ERR("Advertising failed: err %d", err);
return BTP_STATUS_FAILED;
}
}
if (current_settings & BIT(BTP_GAP_SETTINGS_DISCOVERABLE)) {
if (ad_flags & BLE_HS_ADV_F_DISC_LTD) {
duration_ms = MYNEWT_VAL(BTTESTER_LTD_ADV_TIMEOUT);
}
}
/* In NimBLE, own_addr_type is configured in `controller_info` function.
* Let's just verify restrictions for Privacy options.
*/
switch (addr_type) {
case 0x00:
break;
#if MYNEWT_VAL(BTTESTER_PRIVACY_MODE)
case 0x01:
/* RPA usage is is controlled via privacy settings */
if (!(current_settings & BIT(BTP_GAP_SETTINGS_PRIVACY))) {
return BTP_STATUS_FAILED;
}
break;
case 0x02:
/* NRPA is used only for non-connectable advertising */
if ((current_settings & BIT(BTP_GAP_SETTINGS_CONNECTABLE))) {
return BTP_STATUS_FAILED;
}
break;
#endif
default:
return BTP_STATUS_FAILED;
}
#if MYNEWT_VAL(BTTESTER_PRIVACY_MODE) && MYNEWT_VAL(BTTESTER_USE_NRPA)
if (MYNEWT_VAL(BTTESTER_NRPA_TIMEOUT) < duration_ms / 1000) {
advertising_start = os_get_uptime_usec();
os_callout_reset(&bttester_nrpa_rotate_timer,
OS_TICKS_PER_SEC * MYNEWT_VAL(BTTESTER_NRPA_TIMEOUT));
}
#endif
#if MYNEWT_VAL(BLE_EXT_ADV)
err = ble_gap_ext_adv_start(0, duration_ms / 10, 0);
#else
err = ble_gap_adv_start(own_addr_type, NULL, duration_ms,
&adv_params, gap_event_cb, NULL);
#endif
if (err) {
SYS_LOG_ERR("Advertising failed: err %d", err);
return BTP_STATUS_FAILED;
}
current_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING);
rp->current_settings = htole32(current_settings);
*rsp_len = sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static uint8_t
stop_advertising(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
struct btp_gap_stop_advertising_rp *rp = rsp;
SYS_LOG_DBG("");
#if MYNEWT_VAL(BLE_EXT_ADV)
if (ble_gap_ext_adv_stop(0) != 0) {
return BTP_STATUS_FAILED;
}
#else
if (ble_gap_adv_stop() != 0) {
return BTP_STATUS_FAILED;
}
#endif
current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING);
rp->current_settings = htole32(current_settings);
*rsp_len = sizeof(*rp);
return BTP_STATUS_SUCCESS;
}
static uint8_t
get_ad_flags(const uint8_t *data, uint8_t data_len)
{
uint8_t len, i;
/* Parse advertisement to get flags */
for (i = 0; i < data_len; i += len - 1) {
len = data[i++];
if (!len) {
break;
}
/* Check if field length is correct */
if (len > (data_len - i) || (data_len - i) < 1) {
break;
}
switch (data[i++]) {
case BLE_HS_ADV_TYPE_FLAGS:
return data[i];
default:
break;
}
}
return 0;
}
static uint8_t discovery_flags;
static struct os_mbuf *adv_buf;
static void
store_adv(const ble_addr_t *addr, int8_t rssi,
const uint8_t *data, uint8_t len)
{
struct btp_gap_device_found_ev *ev;
void *adv_data;
/* cleanup */
tester_mbuf_reset(adv_buf);
ev = os_mbuf_extend(adv_buf, sizeof(*ev));
if (!ev) {
return;
}
memcpy(&ev->address, addr, sizeof(ev->address));
ev->rssi = rssi;
ev->flags = BTP_GAP_DEVICE_FOUND_FLAG_AD | BTP_GAP_DEVICE_FOUND_FLAG_RSSI;
ev->eir_data_len = len;
adv_data = os_mbuf_extend(adv_buf, len);
if (!adv_data) {
return;
}
memcpy(adv_data, data, len);
}
static void
device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype,
const uint8_t *data, uint8_t len)
{
struct btp_gap_device_found_ev *ev;
void *adv_data;
ble_addr_t a;
/* if General/Limited Discovery - parse Advertising data to get flags */
if (!(discovery_flags & BTP_GAP_DISCOVERY_FLAG_LE_OBSERVE) &&
(evtype != BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP)) {
uint8_t flags = get_ad_flags(data, len);
/* ignore non-discoverable devices */
if (!(flags & BLE_AD_DISCOV_MASK)) {
SYS_LOG_DBG("Non discoverable, skipping");
return;
}
/* if Limited Discovery - ignore general discoverable devices */
if ((discovery_flags & BTP_GAP_DISCOVERY_FLAG_LIMITED) &&
!(flags & BLE_HS_ADV_F_DISC_LTD)) {
SYS_LOG_DBG("General discoverable, skipping");
return;
}
}
/* attach Scan Response data */
if (evtype == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
/* skip if there is no pending advertisement */
if (!adv_buf->om_len) {
SYS_LOG_INF("No pending advertisement, skipping");
return;
}
ev = (void *) adv_buf->om_data;
memcpy(&a, &ev->address, sizeof(a));
/*
* in general, the Scan Response comes right after the
* Advertisement, but if not if send stored event and ignore
* this one
*/
if (ble_addr_cmp(addr, &a)) {
SYS_LOG_INF("Address does not match, skipping");
goto done;
}
ev->eir_data_len += len;
ev->flags |= BTP_GAP_DEVICE_FOUND_FLAG_SD;
adv_data = os_mbuf_extend(adv_buf, len);
if (!adv_data) {
return;
}
memcpy(adv_data, data, len);
goto done;
}
/*
* if there is another pending advertisement, send it and store the
* current one
*/
if (adv_buf->om_len) {
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND,
adv_buf->om_data, adv_buf->om_len);
}
store_adv(addr, rssi, data, len);
/* if Active Scan and scannable event - wait for Scan Response */
if ((discovery_flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) &&
(evtype == BLE_HCI_ADV_RPT_EVTYPE_ADV_IND ||
evtype == BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND)) {
SYS_LOG_DBG("Waiting for scan response");
return;
}
done:
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND,
adv_buf->om_data, adv_buf->om_len);
}
static int
discovery_cb(struct ble_gap_event *event, void *arg)
{
if (event->type == BLE_GAP_EVENT_DISC) {
device_found(&event->disc.addr, event->disc.rssi,
event->disc.event_type, event->disc.data,
event->disc.length_data);
}
return 0;
}
static uint8_t
start_discovery(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
const struct btp_gap_start_discovery_cmd *cp = cmd;
struct ble_gap_disc_params params = {0};
SYS_LOG_DBG("");
/* only LE scan is supported */
if (cp->flags & BTP_GAP_DISCOVERY_FLAG_BREDR) {
return BTP_STATUS_FAILED;
}
params.passive = (cp->flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) == 0;
params.limited = (cp->flags & BTP_GAP_DISCOVERY_FLAG_LIMITED) > 0;
params.filter_duplicates = 1;
if (ble_gap_disc(own_addr_type, BLE_HS_FOREVER,
¶ms, discovery_cb, NULL) != 0) {
return BTP_STATUS_FAILED;
}
tester_mbuf_reset(adv_buf);
discovery_flags = cp->flags;
return BTP_STATUS_SUCCESS;
}
static uint8_t
stop_discovery(const void *cmd, uint16_t cmd_len,
void *rsp, uint16_t *rsp_len)
{
SYS_LOG_DBG("");
if (ble_gap_disc_cancel() != 0) {
return BTP_STATUS_FAILED;
}
return BTP_STATUS_SUCCESS;
}
/* Bluetooth Core Spec v5.1 | Section 10.7.1
* If a privacy-enabled Peripheral, that has a stored bond,
* receives a resolvable private address, the Host may resolve
* the resolvable private address [...]
* If the resolution is successful, the Host may accept the connection.
* If the resolution procedure fails, then the Host shall disconnect
* with the error code "Authentication failure" [...]
*/
static void
periph_privacy(struct ble_gap_conn_desc desc)
{
#if !MYNEWT_VAL(BTTESTER_PRIVACY_MODE)
return;
#endif
int count;
SYS_LOG_DBG("");
ble_store_util_count(BLE_STORE_OBJ_TYPE_PEER_SEC, &count);
if (count > 0 && BLE_ADDR_IS_RPA(&desc.peer_id_addr)) {
SYS_LOG_DBG("Authentication failure, disconnecting");
ble_gap_terminate(desc.conn_handle, BLE_ERR_AUTH_FAIL);
}
}
static void
device_connected_ev_send(struct os_event *ev)
{
struct ble_gap_conn_desc desc;
int rc;
SYS_LOG_DBG("");
rc = gap_conn_find_by_addr((ble_addr_t *) &connected_ev, &desc);
if (rc) {
tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED,
BTP_STATUS_FAILED);
return;
}
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED,
(uint8_t *) &connected_ev, sizeof(connected_ev));
periph_privacy(desc);
}
static void
le_connected(uint16_t conn_handle, int status)
{
struct ble_gap_conn_desc desc;
ble_addr_t *addr;
int rc;
SYS_LOG_DBG("");
if (status != 0) {
return;
}
rc = ble_gap_conn_find(conn_handle, &desc);
if (rc) {
return;
}
peer_id_addr = desc.peer_id_addr;
peer_ota_addr = desc.peer_ota_addr;
addr = &desc.peer_id_addr;
memcpy(&connected_ev.address, addr, sizeof(connected_ev.address));
connected_ev.conn_itvl = desc.conn_itvl;
connected_ev.conn_latency = desc.conn_latency;
connected_ev.supervision_timeout = desc.supervision_timeout;
#if MYNEWT_VAL(BTTESTER_CONN_RETRY)
os_callout_reset(&connected_ev_co,
os_time_ms_to_ticks32(
CONNECTED_EV_DELAY_MS(desc.conn_itvl)));
#else
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED,
(uint8_t *) &connected_ev,
sizeof(connected_ev));
#endif
}
static void
le_disconnected(struct ble_gap_conn_desc *conn, int reason)
{
struct btp_gap_device_disconnected_ev ev;
ble_addr_t *addr = &conn->peer_ota_addr;
SYS_LOG_DBG("");
#if MYNEWT_VAL(BTTESTER_CONN_RETRY)
int rc;
if ((reason == BLE_HS_HCI_ERR(BLE_ERR_CONN_ESTABLISHMENT)) &&
os_callout_queued(&connected_ev_co)) {
if (connection_attempts < MYNEWT_VAL(BTTESTER_CONN_RETRY)) {
os_callout_stop(&connected_ev_co);
/* try connecting again */
rc = ble_gap_connect(own_addr_type, addr, 0,
&dflt_conn_params, gap_event_cb,
NULL);
if (rc == 0) {
connection_attempts++;
return;
}
}
} else if (os_callout_queued(&connected_ev_co)) {
os_callout_stop(&connected_ev_co);
return;
}
#endif
connection_attempts = 0;
memset(&connected_ev, 0, sizeof(connected_ev));
memcpy(&ev.address, addr, sizeof(ev.address));
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_DISCONNECTED,
(uint8_t *) &ev, sizeof(ev));
}
static void
auth_passkey_oob(uint16_t conn_handle)
{
struct ble_gap_conn_desc desc;
struct ble_sm_io pk;
int rc;
SYS_LOG_DBG("");
rc = ble_gap_conn_find(conn_handle, &desc);
if (rc) {
return;
}
memcpy(pk.oob, oob, sizeof(oob));
pk.action = BLE_SM_IOACT_OOB;
rc = ble_sm_inject_io(conn_handle, &pk);
assert(rc == 0);
}
static void
auth_passkey_display(uint16_t conn_handle, unsigned int passkey)
{
struct ble_gap_conn_desc desc;
struct btp_gap_passkey_display_ev ev;
ble_addr_t *addr;
struct ble_sm_io pk;
int rc;
SYS_LOG_DBG("");
rc = ble_gap_conn_find(conn_handle, &desc);
if (rc) {
return;
}
rc = ble_hs_hci_rand(&pk.passkey, sizeof(pk.passkey));
assert(rc == 0);
/* Max value is 999999 */
pk.passkey %= 1000000;
pk.action = BLE_SM_IOACT_DISP;
rc = ble_sm_inject_io(conn_handle, &pk);
assert(rc == 0);
addr = &desc.peer_ota_addr;
memcpy(&ev.address, addr, sizeof(ev.address));
ev.passkey = htole32(pk.passkey);
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY,
(uint8_t *) &ev, sizeof(ev));