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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
|
/******************************************************************************
*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed 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.
*
******************************************************************************/
#include <array>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "device/include/controller.h"
#include "stack/btm/ble_advertiser_hci_interface.h"
#include "stack/include/ble_advertiser.h"
#include "stack_config.h"
using ::testing::Args;
using ::testing::Contains;
using ::testing::DoAll;
using ::testing::ElementsAreArray;
using ::testing::Exactly;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::SaveArg;
using ::testing::SizeIs;
using ::testing::_;
using base::Bind;
using status_cb = BleAdvertiserHciInterface::status_cb;
using parameters_cb = BleAdvertiserHciInterface::parameters_cb;
using SetEnableData = BleAdvertiserHciInterface::SetEnableData;
const int num_adv_instances = 16;
/* Below are methods that must be implemented if we don't want to compile the
* whole stack. They will be removed, or changed into mocks one by one in the
* future, as the refactoring progresses */
bool BTM_BleLocalPrivacyEnabled() { return true; }
uint16_t BTM_ReadDiscoverability(uint16_t* p_window, uint16_t* p_interval) {
return true;
}
void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address) {
}
void btm_gen_resolvable_private_addr(
base::Callback<void(const RawAddress& rpa)> cb) {
cb.Run(RawAddress::kEmpty);
}
const stack_config_t interface = {};
const stack_config_t* stack_config_get_interface(void) { return &interface; }
alarm_callback_t last_alarm_cb = nullptr;
void* last_alarm_data = nullptr;
void alarm_set_on_mloop(alarm_t* alarm, period_ms_t interval_ms,
alarm_callback_t cb, void* data) {
last_alarm_cb = cb;
last_alarm_data = data;
}
void* alarm_cancel(alarm_t* alarm) { return nullptr; }
alarm_t* alarm_new_periodic(const char* name) { return nullptr; }
alarm_t* alarm_new(const char* name) { return nullptr; }
void* alarm_free(alarm_t* alarm) { return nullptr; }
const controller_t* controller_get_interface() { return nullptr; }
uint64_t btm_get_next_private_addrress_interval_ms() { return 15 * 60 * 1000; }
namespace {
void DoNothing(uint8_t) {}
void DoNothing2(uint8_t, uint8_t) {}
void TriggerRandomAddressUpdate() {
// Call to StartAdvertisingSet set the last_alarm_cb to random address timeout
// callback. Call it now in order to trigger address update
last_alarm_cb(last_alarm_data);
}
constexpr uint8_t INTERMEDIATE =
0x00; // Intermediate fragment of fragmented data
constexpr uint8_t FIRST = 0x01; // First fragment of fragmented data
constexpr uint8_t LAST = 0x02; // Last fragment of fragmented data
constexpr uint8_t COMPLETE = 0x03; // Complete extended advertising data
class AdvertiserHciMock : public BleAdvertiserHciInterface {
public:
AdvertiserHciMock() = default;
~AdvertiserHciMock() override = default;
MOCK_METHOD1(ReadInstanceCount,
void(base::Callback<void(uint8_t /* inst_cnt*/)>));
MOCK_METHOD1(SetAdvertisingEventObserver,
void(AdvertisingEventObserver* observer));
MOCK_METHOD6(SetAdvertisingData,
void(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
MOCK_METHOD6(SetScanResponseData,
void(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
MOCK_METHOD3(SetRandomAddress, void(uint8_t, const RawAddress&, status_cb));
MOCK_METHOD3(Enable, void(uint8_t, std::vector<SetEnableData>, status_cb));
MOCK_METHOD5(SetPeriodicAdvertisingParameters,
void(uint8_t, uint16_t, uint16_t, uint16_t, status_cb));
MOCK_METHOD5(SetPeriodicAdvertisingData,
void(uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
MOCK_METHOD3(SetPeriodicAdvertisingEnable, void(uint8_t, uint8_t, status_cb));
MOCK_METHOD2(RemoveAdvertisingSet, void(uint8_t, status_cb));
MOCK_METHOD1(ClearAdvertisingSets, void(status_cb));
MOCK_METHOD9(SetParameters1,
void(uint8_t, uint16_t, uint32_t, uint32_t, uint8_t, uint8_t,
const RawAddress&, uint8_t, const RawAddress&));
MOCK_METHOD8(SetParameters2, void(uint8_t, int8_t, uint8_t, uint8_t, uint8_t,
uint8_t, uint8_t, parameters_cb));
MOCK_METHOD9(CreateBIG1,
void(uint8_t, uint8_t, uint8_t, uint32_t, uint16_t, uint16_t,
uint8_t, uint8_t, uint8_t));
MOCK_METHOD3(CreateBIG2,
void(uint8_t, uint8_t, std::vector<uint8_t>));
MOCK_METHOD2(TerminateBIG, void(uint8_t, uint8_t));
void SetParameters(uint8_t handle, uint16_t properties, uint32_t adv_int_min,
uint32_t adv_int_max, uint8_t channel_map,
uint8_t own_address_type, const RawAddress& own_address,
uint8_t peer_address_type, const RawAddress& peer_address,
uint8_t filter_policy, int8_t tx_power,
uint8_t primary_phy, uint8_t secondary_max_skip,
uint8_t secondary_phy, uint8_t advertising_sid,
uint8_t scan_request_notify_enable,
parameters_cb cmd_complete) override {
SetParameters1(handle, properties, adv_int_min, adv_int_max, channel_map,
own_address_type, own_address, peer_address_type,
peer_address);
SetParameters2(filter_policy, tx_power, primary_phy, secondary_max_skip,
secondary_phy, advertising_sid, scan_request_notify_enable,
cmd_complete);
};
void CreateBIG(uint8_t big_handle,
uint8_t adv_handle,
uint8_t num_bis,
uint32_t sdu_int, //3 octets
uint16_t max_sdu,
uint16_t max_transport_latency,
uint8_t rtn,
uint8_t phy,
uint8_t packing,
uint8_t framing,
uint8_t encryption,
std::vector<uint8_t> broadcast_code) override {
CreateBIG1(big_handle, adv_handle, num_bis, sdu_int, max_sdu,
max_transport_latency, rtn, phy, packing);
CreateBIG2(framing, encryption, broadcast_code);
};
bool QuirkAdvertiserZeroHandle() { return false; }
private:
DISALLOW_COPY_AND_ASSIGN(AdvertiserHciMock);
};
} // namespace
class BleAdvertisingManagerTest : public testing::Test {
protected:
int reg_inst_id = -1;
int reg_status = -1;
int set_params_status = -1;
int set_data_status = -1;
int enable_status = -1;
int start_advertising_status = -1;
int start_advertising_set_advertiser_id = -1;
int start_advertising_set_tx_power = -1;
int start_advertising_set_status = -1;
std::unique_ptr<AdvertiserHciMock> hci_mock;
virtual void SetUp() {
hci_mock.reset(new AdvertiserHciMock());
base::Callback<void(uint8_t)> inst_cnt_Cb;
EXPECT_CALL(*hci_mock, ReadInstanceCount(_))
.Times(Exactly(1))
.WillOnce(SaveArg<0>(&inst_cnt_Cb));
BleAdvertisingManager::Initialize(hci_mock.get());
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// we are a truly gracious fake controller, let the command succeed!
inst_cnt_Cb.Run(num_adv_instances);
}
virtual void TearDown() {
BleAdvertisingManager::CleanUp();
hci_mock.reset();
}
public:
void RegistrationCb(uint8_t inst_id, uint8_t status) {
reg_inst_id = inst_id;
reg_status = status;
}
void SetParametersCb(uint8_t status, int8_t tx_power) {
set_params_status = status;
}
void SetDataCb(uint8_t status) { set_data_status = status; }
void EnableCb(uint8_t status) { enable_status = status; }
void StartAdvertisingCb(uint8_t status) { start_advertising_status = status; }
void StartAdvertisingSetCb(uint8_t advertiser_id, int8_t tx_power,
uint8_t status) {
start_advertising_set_advertiser_id = advertiser_id;
start_advertising_set_tx_power = tx_power;
start_advertising_set_status = status;
}
void StartAdvertisingSetWithSpecificAddressType(int8_t own_address_type);
};
TEST_F(BleAdvertisingManagerTest, test_registration) {
for (int i = 0; i < num_adv_instances; i++) {
BleAdvertisingManager::Get()->RegisterAdvertiser(Bind(
&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
EXPECT_EQ(i, reg_inst_id);
}
// This call should return an error - no more advertisers left.
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(ADVERTISE_FAILED_TOO_MANY_ADVERTISERS, reg_status);
// Don't bother checking inst_id, it doesn't matter
status_cb remove_cb;
EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
.Times(1)
.WillOnce(SaveArg<1>(&remove_cb));
BleAdvertisingManager::Get()->Unregister(5);
remove_cb.Run(0);
// One advertiser was freed, so should be able to register one now
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
EXPECT_EQ(5, reg_inst_id);
}
/* This test verifies that the following flow is working correctly: register,
* set parameters, set data, enable, ... (advertise) ..., unregister*/
TEST_F(BleAdvertisingManagerTest, test_android_flow) {
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
int advertiser_id = reg_inst_id;
parameters_cb set_params_cb;
tBTM_BLE_ADV_PARAMS params;
EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
.Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
BleAdvertisingManager::Get()->SetParameters(
advertiser_id, ¶ms,
Bind(&BleAdvertisingManagerTest::SetParametersCb,
base::Unretained(this)));
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// we are a truly gracious fake controller, let the command succeed!
set_params_cb.Run(0, 0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
status_cb set_data_cb;
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, std::vector<uint8_t>(),
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
set_data_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
status_cb enable_cb;
EXPECT_CALL(*hci_mock,
Enable(0x01 /* enable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->Enable(
advertiser_id, true,
Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0, 0,
base::Callback<void(uint8_t)>());
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
enable_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, enable_status);
/* fake controller should be advertising */
EXPECT_CALL(*hci_mock,
Enable(0x00 /* disable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
status_cb remove_cb;
EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
.Times(1)
.WillOnce(SaveArg<1>(&remove_cb));
BleAdvertisingManager::Get()->Unregister(advertiser_id);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
enable_cb.Run(0);
remove_cb.Run(0);
}
/* This test verifies that when advertising data is set, tx power and flags will
* be properly filled. */
TEST_F(BleAdvertisingManagerTest, test_adv_data_filling) {
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
int advertiser_id = reg_inst_id;
parameters_cb set_params_cb;
tBTM_BLE_ADV_PARAMS params;
params.advertising_event_properties =
BleAdvertisingManager::advertising_prop_legacy_connectable;
params.tx_power = -15;
EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
.Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, params.tx_power, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
BleAdvertisingManager::Get()->SetParameters(
advertiser_id, ¶ms,
Bind(&BleAdvertisingManagerTest::SetParametersCb,
base::Unretained(this)));
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// let the set parameters command succeed!
set_params_cb.Run(0, 0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
status_cb set_data_cb;
/* verify that flags will be added, and tx power filled, if call to SetData
* contained only tx power, and the advertisement is connectable */
uint8_t expected_adv_data[] = {
0x02 /* len */, 0x01 /* flags */,
0x02 /* flags value */, 0x02 /* len */,
0x0A /* tx_power */, static_cast<uint8_t>(params.tx_power)};
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
.With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false,
std::vector<uint8_t>({0x02 /* len */, 0x0A /* tx_power */, 0x00}),
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
set_data_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
}
/* This test verifies that when advertising is non-connectable, flags will not
* be added. */
TEST_F(BleAdvertisingManagerTest, test_adv_data_not_filling) {
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
int advertiser_id = reg_inst_id;
parameters_cb set_params_cb;
tBTM_BLE_ADV_PARAMS params;
params.advertising_event_properties =
BleAdvertisingManager::advertising_prop_legacy_non_connectable;
params.tx_power = -15;
EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
.Times(1);
EXPECT_CALL(*hci_mock,
SetParameters2(_, (uint8_t)params.tx_power, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
BleAdvertisingManager::Get()->SetParameters(
advertiser_id, ¶ms,
Bind(&BleAdvertisingManagerTest::SetParametersCb,
base::Unretained(this)));
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// let the set parameters command succeed!
set_params_cb.Run(0, -15);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
status_cb set_data_cb;
/* verify that flags will not be added */
uint8_t expected_adv_data[] = {
0x02 /* len */, 0xFF /* manufacturer specific */, 0x01 /* data */};
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
.With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, std::vector<uint8_t>({0x02 /* len */, 0xFF, 0x01}),
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
set_data_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
}
TEST_F(BleAdvertisingManagerTest, test_reenabling) {
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
EXPECT_EQ(0, reg_inst_id);
uint8_t advertiser_id = reg_inst_id;
status_cb enable_cb;
EXPECT_CALL(*hci_mock,
Enable(0x01 /* enable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->Enable(advertiser_id, true, Bind(DoNothing), 0,
0, Bind(DoNothing));
enable_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
EXPECT_CALL(*hci_mock,
Enable(0x01 /* enable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(advertiser_id, 0x00,
0x01ed, 0x00);
enable_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
}
/* Make sure that instance is not reenabled if it's already disabled */
TEST_F(BleAdvertisingManagerTest, test_reenabling_disabled_instance) {
uint8_t advertiser_id = 1; // any unregistered value
EXPECT_CALL(*hci_mock, Enable(_, _, _)).Times(Exactly(0));
BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(advertiser_id, 0x00,
0x05, 0x00);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
}
/* This test verifies that the only flow that is currently used on Android, is
* working correctly in happy case scenario. */
void BleAdvertisingManagerTest::StartAdvertisingSetWithSpecificAddressType(
int8_t own_address_type) {
std::vector<uint8_t> adv_data;
std::vector<uint8_t> scan_resp;
tBTM_BLE_ADV_PARAMS params;
params.own_address_type = own_address_type;
tBLE_PERIODIC_ADV_PARAMS periodic_params;
periodic_params.enable = false;
std::vector<uint8_t> periodic_data;
parameters_cb set_params_cb;
status_cb set_address_cb;
status_cb set_data_cb;
status_cb set_scan_resp_data_cb;
status_cb enable_cb;
EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
if (own_address_type != BLE_ADDR_PUBLIC) {
EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&set_address_cb));
}
EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->StartAdvertisingSet(
Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
base::Unretained(this)),
¶ms, adv_data, scan_resp, &periodic_params, periodic_data,
0 /* duration */, 0 /* maxExtAdvEvents */, Bind(DoNothing2));
// we are a truly gracious fake controller, let the commands succeed!
int selected_tx_power = -15;
set_params_cb.Run(0, selected_tx_power);
if (own_address_type != BLE_ADDR_PUBLIC) {
set_address_cb.Run(0);
}
set_data_cb.Run(0);
set_scan_resp_data_cb.Run(0);
enable_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
int advertiser_id = start_advertising_set_advertiser_id;
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// ... advertising ...
// Disable advertiser
status_cb disable_cb;
EXPECT_CALL(*hci_mock,
Enable(0x00 /* disable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&disable_cb));
status_cb remove_cb;
EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
.Times(1)
.WillOnce(SaveArg<1>(&remove_cb));
BleAdvertisingManager::Get()->Unregister(advertiser_id);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
disable_cb.Run(0);
remove_cb.Run(0);
}
TEST_F(BleAdvertisingManagerTest,
test_start_advertising_set_default_address_type) {
StartAdvertisingSetWithSpecificAddressType(BLE_ADDR_ANONYMOUS);
}
TEST_F(BleAdvertisingManagerTest,
test_start_advertising_set_public_address_type) {
StartAdvertisingSetWithSpecificAddressType(BLE_ADDR_PUBLIC);
}
TEST_F(BleAdvertisingManagerTest,
test_start_advertising_set_random_address_type) {
StartAdvertisingSetWithSpecificAddressType(BLE_ADDR_RANDOM);
}
TEST_F(BleAdvertisingManagerTest, test_start_advertising_set_params_failed) {
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
int advertiser_id = reg_inst_id;
std::vector<uint8_t> adv_data;
std::vector<uint8_t> scan_resp;
tBTM_BLE_ADV_PARAMS params;
parameters_cb set_params_cb;
EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
.Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
.Times(Exactly(0));
BleAdvertisingManager::Get()->StartAdvertising(
advertiser_id,
Bind(&BleAdvertisingManagerTest::StartAdvertisingCb,
base::Unretained(this)),
¶ms, adv_data, scan_resp, 0, base::Callback<void(uint8_t)>());
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// set params failed
set_params_cb.Run(0x01, 0);
// Expect the whole flow to fail right away
EXPECT_EQ(BTM_BLE_MULTI_ADV_FAILURE, start_advertising_status);
}
TEST_F(BleAdvertisingManagerTest, test_data_sender) {
// prepare test input vector
const int max_data_size = 1650;
std::vector<uint8_t> data(max_data_size);
for (int i = 0; i < max_data_size; i++) data[i] = i;
BleAdvertisingManager::Get()->RegisterAdvertiser(
Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
int advertiser_id = reg_inst_id;
status_cb set_data_cb;
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock,
SetAdvertisingData(advertiser_id, INTERMEDIATE, _, 251, _, _))
.Times(5)
.WillRepeatedly(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 144, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, data,
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
for (int i = 0; i < 7; i++) {
set_data_cb.Run(0x00);
}
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Expect the whole flow to succeed
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
// ***************** Try again with different data size *********************
data.resize(503);
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock,
SetAdvertisingData(advertiser_id, INTERMEDIATE, _, 251, _, _))
.Times(1)
.WillRepeatedly(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 1, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, data,
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
for (int i = 0; i < 3; i++) {
set_data_cb.Run(0x00);
}
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Expect the whole flow to succeed
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
// ***************** Try again with different data size *********************
data.resize(502);
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 251, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, data,
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
for (int i = 0; i < 2; i++) {
set_data_cb.Run(0x00);
}
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Expect the whole flow to succeed
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
// ***************** Try again with different data size *********************
data.resize(501);
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 250, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, data,
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
for (int i = 0; i < 2; i++) {
set_data_cb.Run(0x00);
}
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Expect the whole flow to succeed
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
// ***************** Try again with different data size *********************
data.resize(251);
EXPECT_CALL(*hci_mock,
SetAdvertisingData(advertiser_id, COMPLETE, _, 251, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, data,
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
set_data_cb.Run(0x00);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Expect the whole flow to succeed
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
// ***************** Try again with different data size *********************
data.resize(120);
EXPECT_CALL(*hci_mock,
SetAdvertisingData(advertiser_id, COMPLETE, _, 120, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, data,
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
set_data_cb.Run(0x00);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Expect the whole flow to succeed
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
// ***************** Try again with different data size *********************
data.resize(0);
EXPECT_CALL(*hci_mock,
SetAdvertisingData(advertiser_id, COMPLETE, _, 0, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->SetData(
advertiser_id, false, data,
Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
set_data_cb.Run(0x00);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Expect the whole flow to succeed
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
}
/* This test makes sure that conectable advertisment with timeout will get it's
* address updated once the timeout passes and one tries to enable it again.*/
TEST_F(BleAdvertisingManagerTest,
test_connectable_address_update_during_timeout) {
std::vector<uint8_t> adv_data;
std::vector<uint8_t> scan_resp;
tBTM_BLE_ADV_PARAMS params;
params.own_address_type = BLE_ADDR_ANONYMOUS;
params.advertising_event_properties = 0x1 /* connectable */;
tBLE_PERIODIC_ADV_PARAMS periodic_params;
periodic_params.enable = false;
std::vector<uint8_t> periodic_data;
uint8_t maxExtAdvEvents = 50;
parameters_cb set_params_cb;
status_cb set_address_cb;
status_cb set_data_cb;
status_cb set_scan_resp_data_cb;
status_cb enable_cb;
EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&set_address_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
EXPECT_CALL(
*hci_mock,
Enable(
0x01 /* enable */,
AllOf(SizeIs(1),
Contains(Field(&SetEnableData::max_extended_advertising_events,
maxExtAdvEvents))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->StartAdvertisingSet(
Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
base::Unretained(this)),
¶ms, adv_data, scan_resp, &periodic_params, periodic_data,
0 /* duration */, maxExtAdvEvents, Bind(DoNothing2));
// we are a truly gracious fake controller, let the commands succeed!
int selected_tx_power = -15;
set_params_cb.Run(0, selected_tx_power);
set_address_cb.Run(0);
set_data_cb.Run(0);
set_scan_resp_data_cb.Run(0);
enable_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
int advertiser_id = start_advertising_set_advertiser_id;
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// ... advertising ...
// No HCI calls should be triggered, becuase there is a timeout on a
// connectable advertisement.
TriggerRandomAddressUpdate();
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Set terminated because we advertised maxExtAdvEvents times!
BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(
0x43 /*status */, advertiser_id, 0x00 /* conn_handle*/, maxExtAdvEvents);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Try to Enable the advertiser. It should first update it's random address.
EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&set_address_cb));
EXPECT_CALL(
*hci_mock,
Enable(
0x01 /* enable */,
AllOf(SizeIs(1),
Contains(Field(&SetEnableData::max_extended_advertising_events,
maxExtAdvEvents))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->Enable(
advertiser_id, true,
Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0,
maxExtAdvEvents, Bind(DoNothing));
set_address_cb.Run(0);
enable_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Disable advertiser
status_cb disable_cb;
EXPECT_CALL(*hci_mock,
Enable(0x00 /* disable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&disable_cb));
status_cb remove_cb;
EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
.Times(1)
.WillOnce(SaveArg<1>(&remove_cb));
BleAdvertisingManager::Get()->Unregister(advertiser_id);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
disable_cb.Run(0);
remove_cb.Run(0);
}
/* This test makes sure that periodic advertising is stopped before
* unregistering the advertiser, if it was enabled. */
TEST_F(BleAdvertisingManagerTest, test_periodic_adv_disable_on_unregister) {
std::vector<uint8_t> adv_data;
std::vector<uint8_t> scan_resp;
tBTM_BLE_ADV_PARAMS params;
params.own_address_type = BLE_ADDR_ANONYMOUS;
params.advertising_event_properties = 0x1 /* connectable */;
tBLE_PERIODIC_ADV_PARAMS periodic_params;
periodic_params.enable = true; // enable periodic advertising
std::vector<uint8_t> periodic_data;
parameters_cb set_params_cb;
status_cb set_address_cb;
status_cb set_data_cb;
status_cb set_scan_resp_data_cb;
status_cb enable_cb;
status_cb set_periodic_params_cb;
status_cb set_periodic_data_cb;
status_cb set_periodic_enable_cb;
EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&set_address_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingParameters(_, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<4>(&set_periodic_params_cb));
EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingData(_, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<4>(&set_periodic_data_cb));
EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingEnable(0x01 /* enable */, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&set_periodic_enable_cb));
EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->StartAdvertisingSet(
Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
base::Unretained(this)),
¶ms, adv_data, scan_resp, &periodic_params, periodic_data,
0 /* duration */, 0 /* maxExtAdvEvents */, Bind(DoNothing2));
// we are a truly gracious fake controller, let the commands succeed!
int selected_tx_power = -15;
set_params_cb.Run(0, selected_tx_power);
set_address_cb.Run(0);
set_data_cb.Run(0);
set_scan_resp_data_cb.Run(0);
set_periodic_params_cb.Run(0);
set_periodic_data_cb.Run(0);
set_periodic_enable_cb.Run(0);
enable_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
int advertiser_id = start_advertising_set_advertiser_id;
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// ... advertising ...
// Unregister advertiser - should disable periodic advertising
status_cb disable_cb;
EXPECT_CALL(*hci_mock,
Enable(0x00 /* disable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&disable_cb));
status_cb disable_periodic_cb;
EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingEnable(0x00 /* disable */,
advertiser_id, _))
.Times(1)
.WillOnce(SaveArg<2>(&disable_periodic_cb));
status_cb remove_cb;
EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
.Times(1)
.WillOnce(SaveArg<1>(&remove_cb));
BleAdvertisingManager::Get()->Unregister(advertiser_id);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
disable_cb.Run(0);
disable_periodic_cb.Run(0);
remove_cb.Run(0);
}
TEST_F(BleAdvertisingManagerTest, test_suspend_resume) {
for (int i = 0; i < 10; i++) {
BleAdvertisingManager::Get()->RegisterAdvertiser(Bind(
&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
EXPECT_EQ(i, reg_inst_id);
}
std::array<int, 3> enabled = {{1, 3, 9}};
for (int advertiser_id : enabled) {
status_cb enable_cb;
EXPECT_CALL(*hci_mock,
Enable(0x01 /* enable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->Enable(
advertiser_id, true,
Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0,
0, base::Callback<void(uint8_t)>());
enable_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
}
// we have some advertisers registered, three advertising.
// Call to Suspend() should disable all running advertisers
status_cb disable_cb;
EXPECT_CALL(
*hci_mock,
Enable(0x00 /* disable */,
AllOf(SizeIs(3), Contains(Field(&SetEnableData::handle, 1)),
Contains(Field(&SetEnableData::handle, 3)),
Contains(Field(&SetEnableData::handle, 9))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&disable_cb));
BleAdvertisingManager::Get()->Suspend();
disable_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// Call to Resume() should re-enable advertisers
status_cb enable_cb;
EXPECT_CALL(
*hci_mock,
Enable(0x01 /* enable */,
AllOf(SizeIs(3), Contains(Field(&SetEnableData::handle, 1)),
Contains(Field(&SetEnableData::handle, 3)),
Contains(Field(&SetEnableData::handle, 9))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->Resume();
enable_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
}
/* This test makes sure that conectable advertisment with timeout will get it's
* duration and maxExtAdvEvents updated, when it's terminated due to incoming
* connection.*/
TEST_F(BleAdvertisingManagerTest, test_duration_update_during_timeout) {
std::vector<uint8_t> adv_data;
std::vector<uint8_t> scan_resp;
tBTM_BLE_ADV_PARAMS params;
params.own_address_type = BLE_ADDR_ANONYMOUS;
params.advertising_event_properties = 0x1 /* connectable */;
params.adv_int_min = params.adv_int_max = 0xA0 /* 100ms */;
tBLE_PERIODIC_ADV_PARAMS periodic_params;
periodic_params.enable = false;
std::vector<uint8_t> periodic_data;
uint8_t maxExtAdvEvents = 50;
uint16_t duration = 500 /* 5s */;
parameters_cb set_params_cb;
status_cb set_address_cb;
status_cb set_data_cb;
status_cb set_scan_resp_data_cb;
status_cb enable_cb;
EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&set_address_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
EXPECT_CALL(
*hci_mock,
Enable(0x01 /* enable */,
AllOf(SizeIs(1),
Contains(AllOf(
Field(&SetEnableData::max_extended_advertising_events,
maxExtAdvEvents),
Field(&SetEnableData::duration, duration)))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&enable_cb));
BleAdvertisingManager::Get()->StartAdvertisingSet(
Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
base::Unretained(this)),
¶ms, adv_data, scan_resp, &periodic_params, periodic_data, duration,
maxExtAdvEvents, Bind(DoNothing2));
// we are a truly gracious fake controller, let the commands succeed!
int selected_tx_power = -15;
set_params_cb.Run(0, selected_tx_power);
set_address_cb.Run(0);
set_data_cb.Run(0);
set_scan_resp_data_cb.Run(0);
enable_cb.Run(0);
EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
int advertiser_id = start_advertising_set_advertiser_id;
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
// ... advertising ...
sleep(1);
std::vector<SetEnableData> setEnableData;
// Set terminated because we received connect request! Should trigger
// re-enabling of the set
EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
.Times(1)
.WillOnce(DoAll(SaveArg<1>(&setEnableData), SaveArg<2>(&enable_cb)));
BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(
0x00 /* Advertising successfully ended with a connection being created */,
advertiser_id, 0x01fe /* conn_handle*/, 20 /* completed ExtAdvEvents */);
enable_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
uint16_t new_duration = setEnableData[0].duration;
uint8_t new_extAdvEvents = setEnableData[0].max_extended_advertising_events;
// Sleep is not super-accurate, so assume the recomputed timeouts are around
// 4s +/- 100ms
EXPECT_NEAR((duration - new_duration), 100 /*4s */, 10);
EXPECT_NEAR((maxExtAdvEvents - new_extAdvEvents), 10, 1);
// Disable advertiser
status_cb disable_cb;
EXPECT_CALL(*hci_mock,
Enable(0x00 /* disable */,
AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
advertiser_id))),
_))
.Times(1)
.WillOnce(SaveArg<2>(&disable_cb));
status_cb remove_cb;
EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
.Times(1)
.WillOnce(SaveArg<1>(&remove_cb));
BleAdvertisingManager::Get()->Unregister(advertiser_id);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
disable_cb.Run(0);
remove_cb.Run(0);
}
/* This test verifies that stack cleanup, and shutdown happening while there is
* outstanding HCI command is not triggering the callback */
TEST_F(BleAdvertisingManagerTest, test_cleanup_during_execution) {
std::vector<uint8_t> adv_data;
std::vector<uint8_t> scan_resp;
tBTM_BLE_ADV_PARAMS params;
params.own_address_type = BLE_ADDR_ANONYMOUS;
tBLE_PERIODIC_ADV_PARAMS periodic_params;
periodic_params.enable = false;
std::vector<uint8_t> periodic_data;
parameters_cb set_params_cb;
status_cb set_address_cb;
status_cb set_data_cb;
EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<7>(&set_params_cb));
EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
.Times(1)
.WillOnce(SaveArg<2>(&set_address_cb));
EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
.Times(1)
.WillOnce(SaveArg<5>(&set_data_cb));
BleAdvertisingManager::Get()->StartAdvertisingSet(
Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
base::Unretained(this)),
¶ms, adv_data, scan_resp, &periodic_params, periodic_data,
0 /* duration */, 0 /* maxExtAdvEvents */, Bind(DoNothing2));
// we are a truly gracious fake controller, let the commands succeed!
int selected_tx_power = -15;
set_params_cb.Run(0, selected_tx_power);
set_address_cb.Run(0);
// Someone shut down the stack in the middle of flow, when the HCI Set
// Advertise Data was scheduled!
BleAdvertisingManager::Get()->CleanUp();
// The HCI call returns with status, and tries to execute the callback. This
// should just silently drop the call. If it got executed, we would get crash,
// because BleAdvertisingManager object was already deleted.
set_data_cb.Run(0);
::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
}
extern void testRecomputeTimeout1();
extern void testRecomputeTimeout2();
extern void testRecomputeTimeout3();
TEST_F(BleAdvertisingManagerTest, test_recompute_timeout) {
testRecomputeTimeout1();
testRecomputeTimeout2();
testRecomputeTimeout3();
}
|