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
|
/*
* Copyright 2019 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 "hci/hci_layer.h"
#include <gtest/gtest.h>
#include <list>
#include <memory>
#include "hal/hci_hal.h"
#include "hci/hci_packets.h"
#include "module.h"
#include "os/log.h"
#include "os/thread.h"
#include "packet/bit_inserter.h"
#include "packet/raw_builder.h"
using bluetooth::os::Thread;
using bluetooth::packet::BitInserter;
using bluetooth::packet::RawBuilder;
using std::vector;
namespace {
vector<uint8_t> information_request = {
0xfe,
0x2e,
0x0a,
0x00,
0x06,
0x00,
0x01,
0x00,
0x0a,
0x02,
0x02,
0x00,
0x02,
0x00,
};
// 0x00, 0x01, 0x02, 0x03, ...
vector<uint8_t> counting_bytes;
// 0xFF, 0xFE, 0xFD, 0xFC, ...
vector<uint8_t> counting_down_bytes;
const size_t count_size = 0x8;
} // namespace
namespace bluetooth {
namespace hci {
constexpr std::chrono::milliseconds kTimeout = HciLayer::kHciTimeoutMs / 2;
constexpr std::chrono::milliseconds kAclTimeout = std::chrono::milliseconds(1000);
class TestHciHal : public hal::HciHal {
public:
TestHciHal() : hal::HciHal() {}
~TestHciHal() {
ASSERT_LOG(callbacks == nullptr, "unregisterIncomingPacketCallback() must be called");
}
void registerIncomingPacketCallback(hal::HciHalCallbacks* callback) override {
callbacks = callback;
}
void unregisterIncomingPacketCallback() override {
callbacks = nullptr;
}
void sendHciCommand(hal::HciPacket command) override {
outgoing_commands_.push_back(std::move(command));
if (sent_command_promise_ != nullptr) {
auto promise = std::move(sent_command_promise_);
sent_command_promise_.reset();
promise->set_value();
}
}
void sendAclData(hal::HciPacket data) override {
outgoing_acl_.push_back(std::move(data));
if (sent_acl_promise_ != nullptr) {
auto promise = std::move(sent_acl_promise_);
sent_acl_promise_.reset();
promise->set_value();
}
}
void sendScoData(hal::HciPacket data) override {
outgoing_sco_.push_back(std::move(data));
}
void sendIsoData(hal::HciPacket data) override {
outgoing_iso_.push_back(std::move(data));
if (sent_iso_promise_ != nullptr) {
auto promise = std::move(sent_iso_promise_);
sent_iso_promise_.reset();
promise->set_value();
}
}
hal::HciHalCallbacks* callbacks = nullptr;
PacketView<kLittleEndian> GetPacketView(hal::HciPacket data) {
auto shared = std::make_shared<std::vector<uint8_t>>(data);
return PacketView<kLittleEndian>(shared);
}
size_t GetNumSentCommands() {
return outgoing_commands_.size();
}
std::future<void> GetSentCommandFuture() {
ASSERT_LOG(sent_command_promise_ == nullptr, "Promises promises ... Only one at a time");
sent_command_promise_ = std::make_unique<std::promise<void>>();
return sent_command_promise_->get_future();
}
CommandView GetSentCommand() {
auto packetview = GetPacketView(std::move(outgoing_commands_.front()));
outgoing_commands_.pop_front();
return CommandView::Create(packetview);
}
std::future<void> GetSentAclFuture() {
ASSERT_LOG(sent_acl_promise_ == nullptr, "Promises promises ... Only one at a time");
sent_acl_promise_ = std::make_unique<std::promise<void>>();
return sent_acl_promise_->get_future();
}
PacketView<kLittleEndian> GetSentAcl() {
auto packetview = GetPacketView(std::move(outgoing_acl_.front()));
outgoing_acl_.pop_front();
return packetview;
}
std::future<void> GetSentIsoFuture() {
ASSERT_LOG(sent_iso_promise_ == nullptr, "Promises promises ... Only one at a time");
sent_iso_promise_ = std::make_unique<std::promise<void>>();
return sent_iso_promise_->get_future();
}
PacketView<kLittleEndian> GetSentIso() {
auto packetview = GetPacketView(std::move(outgoing_iso_.front()));
outgoing_iso_.pop_front();
return packetview;
}
void Start() {}
void Stop() {}
void ListDependencies(ModuleList*) const {}
std::string ToString() const override {
return std::string("TestHciHal");
}
static const ModuleFactory Factory;
private:
std::list<hal::HciPacket> outgoing_commands_;
std::list<hal::HciPacket> outgoing_acl_;
std::list<hal::HciPacket> outgoing_sco_;
std::list<hal::HciPacket> outgoing_iso_;
std::unique_ptr<std::promise<void>> sent_command_promise_;
std::unique_ptr<std::promise<void>> sent_acl_promise_;
std::unique_ptr<std::promise<void>> sent_iso_promise_;
};
const ModuleFactory TestHciHal::Factory = ModuleFactory([]() { return new TestHciHal(); });
class DependsOnHci : public Module {
public:
DependsOnHci() : Module() {}
void SendHciCommandExpectingStatus(std::unique_ptr<CommandBuilder> command) {
hci_->EnqueueCommand(
std::move(command), GetHandler()->BindOnceOn(this, &DependsOnHci::handle_event<CommandStatusView>));
}
void SendHciCommandExpectingComplete(std::unique_ptr<CommandBuilder> command) {
hci_->EnqueueCommand(
std::move(command), GetHandler()->BindOnceOn(this, &DependsOnHci::handle_event<CommandCompleteView>));
}
void SendSecurityCommandExpectingComplete(std::unique_ptr<SecurityCommandBuilder> command) {
if (security_interface_ == nullptr) {
security_interface_ =
hci_->GetSecurityInterface(GetHandler()->BindOn(this, &DependsOnHci::handle_event<EventView>));
}
hci_->EnqueueCommand(
std::move(command), GetHandler()->BindOnceOn(this, &DependsOnHci::handle_event<CommandCompleteView>));
}
void SendLeSecurityCommandExpectingComplete(std::unique_ptr<LeSecurityCommandBuilder> command) {
if (le_security_interface_ == nullptr) {
le_security_interface_ =
hci_->GetLeSecurityInterface(GetHandler()->BindOn(this, &DependsOnHci::handle_event<LeMetaEventView>));
}
hci_->EnqueueCommand(
std::move(command), GetHandler()->BindOnceOn(this, &DependsOnHci::handle_event<CommandCompleteView>));
}
void SendAclData(std::unique_ptr<AclBuilder> acl) {
outgoing_acl_.push(std::move(acl));
auto queue_end = hci_->GetAclQueueEnd();
queue_end->RegisterEnqueue(GetHandler(), common::Bind(&DependsOnHci::handle_enqueue, common::Unretained(this)));
}
void SendIsoData(std::unique_ptr<IsoBuilder> iso) {
outgoing_iso_.push(std::move(iso));
auto queue_end = hci_->GetIsoQueueEnd();
queue_end->RegisterEnqueue(GetHandler(), common::Bind(&DependsOnHci::handle_enqueue_iso, common::Unretained(this)));
}
std::future<void> GetReceivedEventFuture() {
ASSERT_LOG(event_promise_ == nullptr, "Promises promises ... Only one at a time");
event_promise_ = std::make_unique<std::promise<void>>();
return event_promise_->get_future();
}
EventView GetReceivedEvent() {
std::lock_guard<std::mutex> lock(list_protector_);
EventView packetview = incoming_events_.front();
incoming_events_.pop_front();
return packetview;
}
std::future<void> GetReceivedAclFuture() {
ASSERT_LOG(acl_promise_ == nullptr, "Promises promises ... Only one at a time");
acl_promise_ = std::make_unique<std::promise<void>>();
return acl_promise_->get_future();
}
size_t GetNumReceivedAclPackets() {
return incoming_acl_packets_.size();
}
AclView GetReceivedAcl() {
std::lock_guard<std::mutex> lock(list_protector_);
AclView packetview = incoming_acl_packets_.front();
incoming_acl_packets_.pop_front();
return packetview;
}
std::future<void> GetReceivedIsoFuture() {
ASSERT_LOG(iso_promise_ == nullptr, "Promises promises ... Only one at a time");
iso_promise_ = std::make_unique<std::promise<void>>();
return iso_promise_->get_future();
}
size_t GetNumReceivedIsoPackets() {
return incoming_iso_packets_.size();
}
IsoView GetReceivedIso() {
std::lock_guard<std::mutex> lock(list_protector_);
IsoView packetview = incoming_iso_packets_.front();
incoming_iso_packets_.pop_front();
return packetview;
}
void Start() {
hci_ = GetDependency<HciLayer>();
hci_->RegisterEventHandler(
EventCode::CONNECTION_COMPLETE, GetHandler()->BindOn(this, &DependsOnHci::handle_event<EventView>));
hci_->RegisterLeEventHandler(
SubeventCode::CONNECTION_COMPLETE, GetHandler()->BindOn(this, &DependsOnHci::handle_event<LeMetaEventView>));
hci_->GetAclQueueEnd()->RegisterDequeue(
GetHandler(), common::Bind(&DependsOnHci::handle_acl, common::Unretained(this)));
hci_->GetIsoQueueEnd()->RegisterDequeue(
GetHandler(), common::Bind(&DependsOnHci::handle_iso, common::Unretained(this)));
}
void Stop() {
hci_->GetAclQueueEnd()->UnregisterDequeue();
hci_->GetIsoQueueEnd()->UnregisterDequeue();
}
void ListDependencies(ModuleList* list) {
list->add<HciLayer>();
}
std::string ToString() const override {
return std::string("DependsOnHci");
}
static const ModuleFactory Factory;
private:
HciLayer* hci_ = nullptr;
const SecurityInterface* security_interface_;
const LeSecurityInterface* le_security_interface_;
std::list<EventView> incoming_events_;
std::list<AclView> incoming_acl_packets_;
std::list<IsoView> incoming_iso_packets_;
std::unique_ptr<std::promise<void>> event_promise_;
std::unique_ptr<std::promise<void>> acl_promise_;
std::unique_ptr<std::promise<void>> iso_promise_;
/* This mutex is protecting lists above from being pushed/popped from different threads at same time */
std::mutex list_protector_;
void handle_acl() {
std::lock_guard<std::mutex> lock(list_protector_);
auto acl_ptr = hci_->GetAclQueueEnd()->TryDequeue();
incoming_acl_packets_.push_back(*acl_ptr);
if (acl_promise_ != nullptr) {
auto promise = std::move(acl_promise_);
acl_promise_.reset();
promise->set_value();
}
}
template <typename T>
void handle_event(T event) {
std::lock_guard<std::mutex> lock(list_protector_);
incoming_events_.push_back(event);
if (event_promise_ != nullptr) {
auto promise = std::move(event_promise_);
event_promise_.reset();
promise->set_value();
}
}
void handle_iso() {
std::lock_guard<std::mutex> lock(list_protector_);
auto iso_ptr = hci_->GetIsoQueueEnd()->TryDequeue();
incoming_iso_packets_.push_back(*iso_ptr);
if (iso_promise_ != nullptr) {
auto promise = std::move(iso_promise_);
iso_promise_.reset();
promise->set_value();
}
}
std::queue<std::unique_ptr<AclBuilder>> outgoing_acl_;
std::unique_ptr<AclBuilder> handle_enqueue() {
hci_->GetAclQueueEnd()->UnregisterEnqueue();
auto acl = std::move(outgoing_acl_.front());
outgoing_acl_.pop();
return acl;
}
std::queue<std::unique_ptr<IsoBuilder>> outgoing_iso_;
std::unique_ptr<IsoBuilder> handle_enqueue_iso() {
hci_->GetIsoQueueEnd()->UnregisterEnqueue();
auto iso = std::move(outgoing_iso_.front());
outgoing_iso_.pop();
return iso;
}
};
const ModuleFactory DependsOnHci::Factory = ModuleFactory([]() { return new DependsOnHci(); });
class HciTest : public ::testing::Test {
public:
void SetUp() override {
counting_bytes.reserve(count_size);
counting_down_bytes.reserve(count_size);
for (size_t i = 0; i < count_size; i++) {
counting_bytes.push_back(i);
counting_down_bytes.push_back(~i);
}
hal = new TestHciHal();
auto command_future = hal->GetSentCommandFuture();
fake_registry_.InjectTestModule(&hal::HciHal::Factory, hal);
fake_registry_.Start<DependsOnHci>(&fake_registry_.GetTestThread());
hci = static_cast<HciLayer*>(fake_registry_.GetModuleUnderTest(&HciLayer::Factory));
upper = static_cast<DependsOnHci*>(fake_registry_.GetModuleUnderTest(&DependsOnHci::Factory));
ASSERT_TRUE(fake_registry_.IsStarted<HciLayer>());
auto reset_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(reset_sent_status, std::future_status::ready);
// Verify that reset was received
ASSERT_EQ(1, hal->GetNumSentCommands());
auto sent_command = hal->GetSentCommand();
auto reset_view = ResetView::Create(CommandView::Create(sent_command));
ASSERT_TRUE(reset_view.IsValid());
// Verify that only one was sent
ASSERT_EQ(0, hal->GetNumSentCommands());
// Send the response event
uint8_t num_packets = 1;
ErrorCode error_code = ErrorCode::SUCCESS;
hal->callbacks->hciEventReceived(GetPacketBytes(ResetCompleteBuilder::Create(num_packets, error_code)));
}
void TearDown() override {
fake_registry_.StopAll();
}
std::vector<uint8_t> GetPacketBytes(std::unique_ptr<packet::BasePacketBuilder> packet) {
std::vector<uint8_t> bytes;
BitInserter i(bytes);
bytes.reserve(packet->size());
packet->Serialize(i);
return bytes;
}
DependsOnHci* upper = nullptr;
TestHciHal* hal = nullptr;
HciLayer* hci = nullptr;
TestModuleRegistry fake_registry_;
};
TEST_F(HciTest, initAndClose) {}
TEST_F(HciTest, leMetaEvent) {
auto event_future = upper->GetReceivedEventFuture();
// Send an LE event
ErrorCode status = ErrorCode::SUCCESS;
uint16_t handle = 0x123;
Role role = Role::CENTRAL;
AddressType peer_address_type = AddressType::PUBLIC_DEVICE_ADDRESS;
Address peer_address = Address::kAny;
uint16_t conn_interval = 0x0ABC;
uint16_t conn_latency = 0x0123;
uint16_t supervision_timeout = 0x0B05;
ClockAccuracy central_clock_accuracy = ClockAccuracy::PPM_50;
hal->callbacks->hciEventReceived(GetPacketBytes(LeConnectionCompleteBuilder::Create(
status,
handle,
role,
peer_address_type,
peer_address,
conn_interval,
conn_latency,
supervision_timeout,
central_clock_accuracy)));
// Wait for the event
auto event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
auto event = upper->GetReceivedEvent();
ASSERT_TRUE(LeConnectionCompleteView::Create(LeMetaEventView::Create(EventView::Create(event))).IsValid());
}
TEST_F(HciTest, hciTimeOut) {
auto event_future = upper->GetReceivedEventFuture();
auto reset_command_future = hal->GetSentCommandFuture();
upper->SendHciCommandExpectingComplete(ResetBuilder::Create());
auto reset_command_sent_status = reset_command_future.wait_for(kTimeout);
ASSERT_EQ(reset_command_sent_status, std::future_status::ready);
auto reset = hal->GetSentCommand();
ASSERT_TRUE(reset.IsValid());
ASSERT_EQ(reset.GetOpCode(), OpCode::RESET);
auto debug_command_future = hal->GetSentCommandFuture();
auto event_status = event_future.wait_for(HciLayer::kHciTimeoutMs);
ASSERT_NE(event_status, std::future_status::ready);
auto debug_command_sent_status = debug_command_future.wait_for(kTimeout);
ASSERT_EQ(debug_command_sent_status, std::future_status::ready);
auto debug = hal->GetSentCommand();
ASSERT_TRUE(debug.IsValid());
ASSERT_EQ(debug.GetOpCode(), OpCode::CONTROLLER_DEBUG_INFO);
}
TEST_F(HciTest, noOpCredits) {
ASSERT_EQ(0, hal->GetNumSentCommands());
// Send 0 credits
uint8_t num_packets = 0;
hal->callbacks->hciEventReceived(GetPacketBytes(NoCommandCompleteBuilder::Create(num_packets)));
auto command_future = hal->GetSentCommandFuture();
upper->SendHciCommandExpectingComplete(ReadLocalVersionInformationBuilder::Create());
// Verify that nothing was sent
ASSERT_EQ(0, hal->GetNumSentCommands());
num_packets = 1;
hal->callbacks->hciEventReceived(GetPacketBytes(NoCommandCompleteBuilder::Create(num_packets)));
auto command_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(command_sent_status, std::future_status::ready);
// Verify that one was sent
ASSERT_EQ(1, hal->GetNumSentCommands());
auto event_future = upper->GetReceivedEventFuture();
// Send the response event
ErrorCode error_code = ErrorCode::SUCCESS;
LocalVersionInformation local_version_information;
local_version_information.hci_version_ = HciVersion::V_5_0;
local_version_information.hci_revision_ = 0x1234;
local_version_information.lmp_version_ = LmpVersion::V_4_2;
local_version_information.manufacturer_name_ = 0xBAD;
local_version_information.lmp_subversion_ = 0x5678;
hal->callbacks->hciEventReceived(GetPacketBytes(
ReadLocalVersionInformationCompleteBuilder::Create(num_packets, error_code, local_version_information)));
// Wait for the event
auto event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
auto event = upper->GetReceivedEvent();
ASSERT_TRUE(
ReadLocalVersionInformationCompleteView::Create(CommandCompleteView::Create(EventView::Create(event))).IsValid());
}
TEST_F(HciTest, creditsTest) {
ASSERT_EQ(0, hal->GetNumSentCommands());
auto command_future = hal->GetSentCommandFuture();
// Send all three commands
upper->SendHciCommandExpectingComplete(ReadLocalVersionInformationBuilder::Create());
upper->SendHciCommandExpectingComplete(ReadLocalSupportedCommandsBuilder::Create());
upper->SendHciCommandExpectingComplete(ReadLocalSupportedFeaturesBuilder::Create());
auto command_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(command_sent_status, std::future_status::ready);
// Verify that the first one is sent
ASSERT_EQ(1, hal->GetNumSentCommands());
auto sent_command = hal->GetSentCommand();
auto version_view = ReadLocalVersionInformationView::Create(CommandView::Create(sent_command));
ASSERT_TRUE(version_view.IsValid());
// Verify that only one was sent
ASSERT_EQ(0, hal->GetNumSentCommands());
// Get a new future
auto event_future = upper->GetReceivedEventFuture();
// Send the response event
uint8_t num_packets = 1;
ErrorCode error_code = ErrorCode::SUCCESS;
LocalVersionInformation local_version_information;
local_version_information.hci_version_ = HciVersion::V_5_0;
local_version_information.hci_revision_ = 0x1234;
local_version_information.lmp_version_ = LmpVersion::V_4_2;
local_version_information.manufacturer_name_ = 0xBAD;
local_version_information.lmp_subversion_ = 0x5678;
hal->callbacks->hciEventReceived(GetPacketBytes(
ReadLocalVersionInformationCompleteBuilder::Create(num_packets, error_code, local_version_information)));
// Wait for the event
auto event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
auto event = upper->GetReceivedEvent();
ASSERT_TRUE(
ReadLocalVersionInformationCompleteView::Create(CommandCompleteView::Create(EventView::Create(event))).IsValid());
// Verify that the second one is sent
command_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(command_sent_status, std::future_status::ready);
ASSERT_EQ(1, hal->GetNumSentCommands());
sent_command = hal->GetSentCommand();
auto supported_commands_view = ReadLocalSupportedCommandsView::Create(CommandView::Create(sent_command));
ASSERT_TRUE(supported_commands_view.IsValid());
// Verify that only one was sent
ASSERT_EQ(0, hal->GetNumSentCommands());
event_future = upper->GetReceivedEventFuture();
command_future = hal->GetSentCommandFuture();
// Send the response event
std::array<uint8_t, 64> supported_commands;
for (uint8_t i = 0; i < 64; i++) {
supported_commands[i] = i;
}
hal->callbacks->hciEventReceived(
GetPacketBytes(ReadLocalSupportedCommandsCompleteBuilder::Create(num_packets, error_code, supported_commands)));
// Wait for the event
event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
event = upper->GetReceivedEvent();
ASSERT_TRUE(
ReadLocalSupportedCommandsCompleteView::Create(CommandCompleteView::Create(EventView::Create(event))).IsValid());
// Verify that the third one is sent
command_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(command_sent_status, std::future_status::ready);
ASSERT_EQ(1, hal->GetNumSentCommands());
sent_command = hal->GetSentCommand();
auto supported_features_view = ReadLocalSupportedFeaturesView::Create(CommandView::Create(sent_command));
ASSERT_TRUE(supported_features_view.IsValid());
// Verify that only one was sent
ASSERT_EQ(0, hal->GetNumSentCommands());
event_future = upper->GetReceivedEventFuture();
// Send the response event
uint64_t lmp_features = 0x012345678abcdef;
hal->callbacks->hciEventReceived(
GetPacketBytes(ReadLocalSupportedFeaturesCompleteBuilder::Create(num_packets, error_code, lmp_features)));
// Wait for the event
event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
event = upper->GetReceivedEvent();
ASSERT_TRUE(
ReadLocalSupportedFeaturesCompleteView::Create(CommandCompleteView::Create(EventView::Create(event))).IsValid());
}
TEST_F(HciTest, leSecurityInterfaceTest) {
// Send LeRand to the controller
auto command_future = hal->GetSentCommandFuture();
upper->SendLeSecurityCommandExpectingComplete(LeRandBuilder::Create());
auto command_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(command_sent_status, std::future_status::ready);
// Check the command
auto sent_command = hal->GetSentCommand();
ASSERT_LT(0, sent_command.size());
LeRandView view = LeRandView::Create(LeSecurityCommandView::Create(CommandView::Create(sent_command)));
ASSERT_TRUE(view.IsValid());
// Send a Command Complete to the host
auto event_future = upper->GetReceivedEventFuture();
uint8_t num_packets = 1;
ErrorCode status = ErrorCode::SUCCESS;
uint64_t rand = 0x0123456789abcdef;
hal->callbacks->hciEventReceived(GetPacketBytes(LeRandCompleteBuilder::Create(num_packets, status, rand)));
// Verify the event
auto event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
auto event = upper->GetReceivedEvent();
ASSERT_TRUE(event.IsValid());
ASSERT_EQ(EventCode::COMMAND_COMPLETE, event.GetEventCode());
ASSERT_TRUE(LeRandCompleteView::Create(CommandCompleteView::Create(event)).IsValid());
}
TEST_F(HciTest, securityInterfacesTest) {
// Send WriteSimplePairingMode to the controller
auto command_future = hal->GetSentCommandFuture();
Enable enable = Enable::ENABLED;
upper->SendSecurityCommandExpectingComplete(WriteSimplePairingModeBuilder::Create(enable));
auto command_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(command_sent_status, std::future_status::ready);
// Check the command
auto sent_command = hal->GetSentCommand();
ASSERT_LT(0, sent_command.size());
auto view = WriteSimplePairingModeView::Create(SecurityCommandView::Create(CommandView::Create(sent_command)));
ASSERT_TRUE(view.IsValid());
// Send a Command Complete to the host
auto event_future = upper->GetReceivedEventFuture();
uint8_t num_packets = 1;
ErrorCode status = ErrorCode::SUCCESS;
hal->callbacks->hciEventReceived(GetPacketBytes(WriteSimplePairingModeCompleteBuilder::Create(num_packets, status)));
// Verify the event
auto event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
auto event = upper->GetReceivedEvent();
ASSERT_TRUE(event.IsValid());
ASSERT_EQ(EventCode::COMMAND_COMPLETE, event.GetEventCode());
ASSERT_TRUE(WriteSimplePairingModeCompleteView::Create(CommandCompleteView::Create(event)).IsValid());
}
TEST_F(HciTest, createConnectionTest) {
// Send CreateConnection to the controller
auto command_future = hal->GetSentCommandFuture();
Address bd_addr;
ASSERT_TRUE(Address::FromString("A1:A2:A3:A4:A5:A6", bd_addr));
uint16_t packet_type = 0x1234;
PageScanRepetitionMode page_scan_repetition_mode = PageScanRepetitionMode::R0;
uint16_t clock_offset = 0x3456;
ClockOffsetValid clock_offset_valid = ClockOffsetValid::VALID;
CreateConnectionRoleSwitch allow_role_switch = CreateConnectionRoleSwitch::ALLOW_ROLE_SWITCH;
upper->SendHciCommandExpectingStatus(CreateConnectionBuilder::Create(
bd_addr, packet_type, page_scan_repetition_mode, clock_offset, clock_offset_valid, allow_role_switch));
auto command_sent_status = command_future.wait_for(kTimeout);
ASSERT_EQ(command_sent_status, std::future_status::ready);
// Check the command
auto sent_command = hal->GetSentCommand();
ASSERT_LT(0, sent_command.size());
CreateConnectionView view = CreateConnectionView::Create(
ConnectionManagementCommandView::Create(AclCommandView::Create(CommandView::Create(sent_command))));
ASSERT_TRUE(view.IsValid());
ASSERT_EQ(bd_addr, view.GetBdAddr());
ASSERT_EQ(packet_type, view.GetPacketType());
ASSERT_EQ(page_scan_repetition_mode, view.GetPageScanRepetitionMode());
ASSERT_EQ(clock_offset, view.GetClockOffset());
ASSERT_EQ(clock_offset_valid, view.GetClockOffsetValid());
ASSERT_EQ(allow_role_switch, view.GetAllowRoleSwitch());
// Send a Command Status to the host
auto event_future = upper->GetReceivedEventFuture();
ErrorCode status = ErrorCode::SUCCESS;
uint16_t handle = 0x123;
LinkType link_type = LinkType::ACL;
Enable encryption_enabled = Enable::DISABLED;
hal->callbacks->hciEventReceived(GetPacketBytes(CreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 1)));
// Verify the event
auto event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
auto event = upper->GetReceivedEvent();
ASSERT_TRUE(event.IsValid());
ASSERT_EQ(EventCode::COMMAND_STATUS, event.GetEventCode());
// Send a ConnectionComplete to the host
event_future = upper->GetReceivedEventFuture();
hal->callbacks->hciEventReceived(
GetPacketBytes(ConnectionCompleteBuilder::Create(status, handle, bd_addr, link_type, encryption_enabled)));
// Verify the event
event_status = event_future.wait_for(kTimeout);
ASSERT_EQ(event_status, std::future_status::ready);
event = upper->GetReceivedEvent();
ASSERT_TRUE(event.IsValid());
ASSERT_EQ(EventCode::CONNECTION_COMPLETE, event.GetEventCode());
ConnectionCompleteView connection_complete_view = ConnectionCompleteView::Create(event);
ASSERT_TRUE(connection_complete_view.IsValid());
ASSERT_EQ(status, connection_complete_view.GetStatus());
ASSERT_EQ(handle, connection_complete_view.GetConnectionHandle());
ASSERT_EQ(link_type, connection_complete_view.GetLinkType());
ASSERT_EQ(encryption_enabled, connection_complete_view.GetEncryptionEnabled());
// Send an ACL packet from the remote
PacketBoundaryFlag packet_boundary_flag = PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE;
BroadcastFlag broadcast_flag = BroadcastFlag::POINT_TO_POINT;
auto acl_payload = std::make_unique<RawBuilder>();
acl_payload->AddAddress(bd_addr);
acl_payload->AddOctets2(handle);
auto incoming_acl_future = upper->GetReceivedAclFuture();
hal->callbacks->aclDataReceived(
GetPacketBytes(AclBuilder::Create(handle, packet_boundary_flag, broadcast_flag, std::move(acl_payload))));
// Verify the ACL packet
auto incoming_acl_status = incoming_acl_future.wait_for(kAclTimeout);
ASSERT_EQ(incoming_acl_status, std::future_status::ready);
auto acl_view = upper->GetReceivedAcl();
ASSERT_TRUE(acl_view.IsValid());
ASSERT_EQ(bd_addr.length() + sizeof(handle), acl_view.GetPayload().size());
auto itr = acl_view.GetPayload().begin();
ASSERT_EQ(bd_addr, itr.extract<Address>());
ASSERT_EQ(handle, itr.extract<uint16_t>());
// Send an ACL packet from DependsOnHci
PacketBoundaryFlag packet_boundary_flag2 = PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE;
BroadcastFlag broadcast_flag2 = BroadcastFlag::POINT_TO_POINT;
auto acl_payload2 = std::make_unique<RawBuilder>();
acl_payload2->AddOctets2(handle);
acl_payload2->AddAddress(bd_addr);
auto sent_acl_future = hal->GetSentAclFuture();
upper->SendAclData(AclBuilder::Create(handle, packet_boundary_flag2, broadcast_flag2, std::move(acl_payload2)));
// Verify the ACL packet
auto sent_acl_status = sent_acl_future.wait_for(kAclTimeout);
ASSERT_EQ(sent_acl_status, std::future_status::ready);
auto sent_acl = hal->GetSentAcl();
ASSERT_LT(0, sent_acl.size());
AclView sent_acl_view = AclView::Create(sent_acl);
ASSERT_TRUE(sent_acl_view.IsValid());
ASSERT_EQ(bd_addr.length() + sizeof(handle), sent_acl_view.GetPayload().size());
auto sent_itr = sent_acl_view.GetPayload().begin();
ASSERT_EQ(handle, sent_itr.extract<uint16_t>());
ASSERT_EQ(bd_addr, sent_itr.extract<Address>());
}
TEST_F(HciTest, receiveMultipleAclPackets) {
Address bd_addr;
ASSERT_TRUE(Address::FromString("A1:A2:A3:A4:A5:A6", bd_addr));
uint16_t handle = 0x0001;
const uint16_t num_packets = 100;
PacketBoundaryFlag packet_boundary_flag = PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE;
BroadcastFlag broadcast_flag = BroadcastFlag::POINT_TO_POINT;
for (uint16_t i = 0; i < num_packets; i++) {
auto acl_payload = std::make_unique<RawBuilder>();
acl_payload->AddAddress(bd_addr);
acl_payload->AddOctets2(handle);
acl_payload->AddOctets2(i);
hal->callbacks->aclDataReceived(
GetPacketBytes(AclBuilder::Create(handle, packet_boundary_flag, broadcast_flag, std::move(acl_payload))));
}
auto incoming_acl_future = upper->GetReceivedAclFuture();
uint16_t received_packets = 0;
while (received_packets < num_packets - 1) {
size_t num_rcv_packets = upper->GetNumReceivedAclPackets();
if (num_rcv_packets == 0) {
auto incoming_acl_status = incoming_acl_future.wait_for(kAclTimeout);
// Get the next future.
ASSERT_EQ(incoming_acl_status, std::future_status::ready);
incoming_acl_future = upper->GetReceivedAclFuture();
num_rcv_packets = upper->GetNumReceivedAclPackets();
}
for (size_t i = 0; i < num_rcv_packets; i++) {
auto acl_view = upper->GetReceivedAcl();
ASSERT_TRUE(acl_view.IsValid());
ASSERT_EQ(bd_addr.length() + sizeof(handle) + sizeof(received_packets), acl_view.GetPayload().size());
auto itr = acl_view.GetPayload().begin();
ASSERT_EQ(bd_addr, itr.extract<Address>());
ASSERT_EQ(handle, itr.extract<uint16_t>());
ASSERT_EQ(received_packets, itr.extract<uint16_t>());
received_packets += 1;
}
}
// Check to see if this future was already fulfilled.
auto acl_race_status = incoming_acl_future.wait_for(std::chrono::milliseconds(1));
if (acl_race_status == std::future_status::ready) {
// Get the next future.
incoming_acl_future = upper->GetReceivedAclFuture();
}
// One last packet to make sure they were all sent. Already got the future.
auto acl_payload = std::make_unique<RawBuilder>();
acl_payload->AddAddress(bd_addr);
acl_payload->AddOctets2(handle);
acl_payload->AddOctets2(num_packets);
hal->callbacks->aclDataReceived(
GetPacketBytes(AclBuilder::Create(handle, packet_boundary_flag, broadcast_flag, std::move(acl_payload))));
auto incoming_acl_status = incoming_acl_future.wait_for(kAclTimeout);
ASSERT_EQ(incoming_acl_status, std::future_status::ready);
auto acl_view = upper->GetReceivedAcl();
ASSERT_TRUE(acl_view.IsValid());
ASSERT_EQ(bd_addr.length() + sizeof(handle) + sizeof(received_packets), acl_view.GetPayload().size());
auto itr = acl_view.GetPayload().begin();
ASSERT_EQ(bd_addr, itr.extract<Address>());
ASSERT_EQ(handle, itr.extract<uint16_t>());
ASSERT_EQ(received_packets, itr.extract<uint16_t>());
}
TEST_F(HciTest, receiveMultipleIsoPackets) {
uint16_t handle = 0x0001;
const uint16_t num_packets = 100;
IsoPacketBoundaryFlag packet_boundary_flag = IsoPacketBoundaryFlag::COMPLETE_SDU;
TimeStampFlag timestamp_flag = TimeStampFlag::NOT_PRESENT;
for (uint16_t i = 0; i < num_packets; i++) {
auto iso_payload = std::make_unique<RawBuilder>();
iso_payload->AddOctets2(handle);
iso_payload->AddOctets2(i);
hal->callbacks->isoDataReceived(
GetPacketBytes(IsoBuilder::Create(handle, packet_boundary_flag, timestamp_flag, std::move(iso_payload))));
}
auto incoming_iso_future = upper->GetReceivedIsoFuture();
uint16_t received_packets = 0;
while (received_packets < num_packets - 1) {
size_t num_rcv_packets = upper->GetNumReceivedIsoPackets();
if (num_rcv_packets == 0) {
auto incoming_iso_status = incoming_iso_future.wait_for(kAclTimeout);
// Get the next future.
ASSERT_EQ(incoming_iso_status, std::future_status::ready);
incoming_iso_future = upper->GetReceivedIsoFuture();
num_rcv_packets = upper->GetNumReceivedIsoPackets();
}
for (size_t i = 0; i < num_rcv_packets; i++) {
auto iso_view = upper->GetReceivedIso();
ASSERT_TRUE(iso_view.IsValid());
ASSERT_EQ(sizeof(handle) + sizeof(received_packets), iso_view.GetPayload().size());
auto itr = iso_view.GetPayload().begin();
ASSERT_EQ(handle, itr.extract<uint16_t>());
ASSERT_EQ(received_packets, itr.extract<uint16_t>());
received_packets += 1;
}
}
// Check to see if this future was already fulfilled.
auto iso_race_status = incoming_iso_future.wait_for(std::chrono::milliseconds(1));
if (iso_race_status == std::future_status::ready) {
// Get the next future.
incoming_iso_future = upper->GetReceivedIsoFuture();
}
// One last packet to make sure they were all sent. Already got the future.
auto iso_payload = std::make_unique<RawBuilder>();
iso_payload->AddOctets2(handle);
iso_payload->AddOctets2(num_packets);
hal->callbacks->isoDataReceived(
GetPacketBytes(IsoBuilder::Create(handle, packet_boundary_flag, timestamp_flag, std::move(iso_payload))));
auto incoming_iso_status = incoming_iso_future.wait_for(kAclTimeout);
ASSERT_EQ(incoming_iso_status, std::future_status::ready);
auto iso_view = upper->GetReceivedIso();
ASSERT_TRUE(iso_view.IsValid());
ASSERT_EQ(sizeof(handle) + sizeof(received_packets), iso_view.GetPayload().size());
auto itr = iso_view.GetPayload().begin();
ASSERT_EQ(handle, itr.extract<uint16_t>());
ASSERT_EQ(received_packets, itr.extract<uint16_t>());
}
} // namespace hci
} // namespace bluetooth
|