1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "hci/acl_manager.h" 18 19 #include <algorithm> 20 #include <chrono> 21 #include <future> 22 #include <map> 23 24 #include <gmock/gmock.h> 25 #include <gtest/gtest.h> 26 27 #include "common/bind.h" 28 #include "hci/address.h" 29 #include "hci/controller.h" 30 #include "hci/hci_layer.h" 31 #include "os/thread.h" 32 #include "packet/raw_builder.h" 33 34 namespace bluetooth { 35 namespace hci { 36 namespace { 37 38 using common::BidiQueue; 39 using common::BidiQueueEnd; 40 using packet::kLittleEndian; 41 using packet::PacketView; 42 using packet::RawBuilder; 43 44 constexpr std::chrono::seconds kTimeout = std::chrono::seconds(2); 45 46 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) { 47 auto bytes = std::make_shared<std::vector<uint8_t>>(); 48 BitInserter i(*bytes); 49 bytes->reserve(packet->size()); 50 packet->Serialize(i); 51 return packet::PacketView<packet::kLittleEndian>(bytes); 52 } 53 54 std::unique_ptr<BasePacketBuilder> NextPayload(uint16_t handle) { 55 static uint32_t packet_number = 1; 56 auto payload = std::make_unique<RawBuilder>(); 57 payload->AddOctets2(6); // L2CAP PDU size 58 payload->AddOctets2(2); // L2CAP CID 59 payload->AddOctets2(handle); 60 payload->AddOctets4(packet_number++); 61 return std::move(payload); 62 } 63 64 std::unique_ptr<AclPacketBuilder> NextAclPacket(uint16_t handle) { 65 PacketBoundaryFlag packet_boundary_flag = PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE; 66 BroadcastFlag broadcast_flag = BroadcastFlag::ACTIVE_SLAVE_BROADCAST; 67 return AclPacketBuilder::Create(handle, packet_boundary_flag, broadcast_flag, NextPayload(handle)); 68 } 69 70 class TestController : public Controller { 71 public: 72 void RegisterCompletedAclPacketsCallback(common::Callback<void(uint16_t /* handle */, uint16_t /* packets */)> cb, 73 os::Handler* handler) override { 74 acl_cb_ = cb; 75 acl_cb_handler_ = handler; 76 } 77 78 uint16_t GetControllerAclPacketLength() const override { 79 return acl_buffer_length_; 80 } 81 82 uint16_t GetControllerNumAclPacketBuffers() const override { 83 return total_acl_buffers_; 84 } 85 86 uint64_t GetControllerLeLocalSupportedFeatures() const override { 87 return le_local_supported_features_; 88 } 89 90 void CompletePackets(uint16_t handle, uint16_t packets) { 91 acl_cb_handler_->Post(common::BindOnce(acl_cb_, handle, packets)); 92 } 93 94 uint16_t acl_buffer_length_ = 1024; 95 uint16_t total_acl_buffers_ = 2; 96 uint64_t le_local_supported_features_ = 0; 97 common::Callback<void(uint16_t /* handle */, uint16_t /* packets */)> acl_cb_; 98 os::Handler* acl_cb_handler_ = nullptr; 99 100 protected: 101 void Start() override {} 102 void Stop() override {} 103 void ListDependencies(ModuleList* list) override {} 104 }; 105 106 class TestHciLayer : public HciLayer { 107 public: 108 void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command, 109 common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) override { 110 command_queue_.push(std::move(command)); 111 command_status_callbacks.push_front(std::move(on_status)); 112 if (command_promise_ != nullptr) { 113 command_promise_->set_value(); 114 command_promise_.reset(); 115 } 116 } 117 118 void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command, 119 common::OnceCallback<void(CommandCompleteView)> on_complete, os::Handler* handler) override { 120 command_queue_.push(std::move(command)); 121 command_complete_callbacks.push_front(std::move(on_complete)); 122 if (command_promise_ != nullptr) { 123 command_promise_->set_value(); 124 command_promise_.reset(); 125 } 126 } 127 128 void SetCommandFuture() { 129 ASSERT_LOG(command_promise_ == nullptr, "Promises, Promises, ... Only one at a time."); 130 command_promise_ = std::make_unique<std::promise<void>>(); 131 command_future_ = std::make_unique<std::future<void>>(command_promise_->get_future()); 132 } 133 134 std::unique_ptr<CommandPacketBuilder> GetLastCommand() { 135 if (command_queue_.size() == 0) { 136 return nullptr; 137 } 138 auto last = std::move(command_queue_.front()); 139 command_queue_.pop(); 140 return last; 141 } 142 143 ConnectionManagementCommandView GetCommandPacket(OpCode op_code) { 144 if (command_future_ != nullptr) { 145 auto result = command_future_->wait_for(std::chrono::milliseconds(1000)); 146 EXPECT_NE(std::future_status::timeout, result); 147 } 148 ASSERT(command_queue_.size() > 0); 149 auto packet_view = GetPacketView(GetLastCommand()); 150 CommandPacketView command_packet_view = CommandPacketView::Create(packet_view); 151 ConnectionManagementCommandView command = ConnectionManagementCommandView::Create(command_packet_view); 152 ASSERT(command.IsValid()); 153 EXPECT_EQ(command.GetOpCode(), op_code); 154 155 return command; 156 } 157 158 void RegisterEventHandler(EventCode event_code, common::Callback<void(EventPacketView)> event_handler, 159 os::Handler* handler) override { 160 registered_events_[event_code] = event_handler; 161 } 162 163 void UnregisterEventHandler(EventCode event_code) override { 164 registered_events_.erase(event_code); 165 } 166 167 void RegisterLeEventHandler(SubeventCode subevent_code, common::Callback<void(LeMetaEventView)> event_handler, 168 os::Handler* handler) override { 169 registered_le_events_[subevent_code] = event_handler; 170 } 171 172 void UnregisterLeEventHandler(SubeventCode subevent_code) { 173 registered_le_events_.erase(subevent_code); 174 } 175 176 void IncomingEvent(std::unique_ptr<EventPacketBuilder> event_builder) { 177 auto packet = GetPacketView(std::move(event_builder)); 178 EventPacketView event = EventPacketView::Create(packet); 179 ASSERT_TRUE(event.IsValid()); 180 EventCode event_code = event.GetEventCode(); 181 ASSERT_TRUE(registered_events_.find(event_code) != registered_events_.end()) << EventCodeText(event_code); 182 registered_events_[event_code].Run(event); 183 } 184 185 void IncomingLeMetaEvent(std::unique_ptr<LeMetaEventBuilder> event_builder) { 186 auto packet = GetPacketView(std::move(event_builder)); 187 EventPacketView event = EventPacketView::Create(packet); 188 LeMetaEventView meta_event_view = LeMetaEventView::Create(event); 189 EXPECT_TRUE(meta_event_view.IsValid()); 190 SubeventCode subevent_code = meta_event_view.GetSubeventCode(); 191 EXPECT_TRUE(registered_le_events_.find(subevent_code) != registered_le_events_.end()); 192 registered_le_events_[subevent_code].Run(meta_event_view); 193 } 194 195 void IncomingAclData(uint16_t handle) { 196 os::Handler* hci_handler = GetHandler(); 197 auto* queue_end = acl_queue_.GetDownEnd(); 198 std::promise<void> promise; 199 auto future = promise.get_future(); 200 queue_end->RegisterEnqueue(hci_handler, 201 common::Bind( 202 [](decltype(queue_end) queue_end, uint16_t handle, std::promise<void> promise) { 203 auto packet = GetPacketView(NextAclPacket(handle)); 204 AclPacketView acl2 = AclPacketView::Create(packet); 205 queue_end->UnregisterEnqueue(); 206 promise.set_value(); 207 return std::make_unique<AclPacketView>(acl2); 208 }, 209 queue_end, handle, common::Passed(std::move(promise)))); 210 auto status = future.wait_for(kTimeout); 211 ASSERT_EQ(status, std::future_status::ready); 212 } 213 214 void AssertNoOutgoingAclData() { 215 auto queue_end = acl_queue_.GetDownEnd(); 216 EXPECT_EQ(queue_end->TryDequeue(), nullptr); 217 } 218 219 void CommandCompleteCallback(EventPacketView event) { 220 CommandCompleteView complete_view = CommandCompleteView::Create(event); 221 ASSERT(complete_view.IsValid()); 222 std::move(command_complete_callbacks.front()).Run(complete_view); 223 command_complete_callbacks.pop_front(); 224 } 225 226 void CommandStatusCallback(EventPacketView event) { 227 CommandStatusView status_view = CommandStatusView::Create(event); 228 ASSERT(status_view.IsValid()); 229 std::move(command_status_callbacks.front()).Run(status_view); 230 command_status_callbacks.pop_front(); 231 } 232 233 PacketView<kLittleEndian> OutgoingAclData() { 234 auto queue_end = acl_queue_.GetDownEnd(); 235 std::unique_ptr<AclPacketBuilder> received; 236 do { 237 received = queue_end->TryDequeue(); 238 } while (received == nullptr); 239 240 return GetPacketView(std::move(received)); 241 } 242 243 BidiQueueEnd<AclPacketBuilder, AclPacketView>* GetAclQueueEnd() override { 244 return acl_queue_.GetUpEnd(); 245 } 246 247 void ListDependencies(ModuleList* list) override {} 248 void Start() override { 249 RegisterEventHandler(EventCode::COMMAND_COMPLETE, 250 base::Bind(&TestHciLayer::CommandCompleteCallback, common::Unretained(this)), nullptr); 251 RegisterEventHandler(EventCode::COMMAND_STATUS, 252 base::Bind(&TestHciLayer::CommandStatusCallback, common::Unretained(this)), nullptr); 253 } 254 void Stop() override {} 255 256 private: 257 std::map<EventCode, common::Callback<void(EventPacketView)>> registered_events_; 258 std::map<SubeventCode, common::Callback<void(LeMetaEventView)>> registered_le_events_; 259 std::list<base::OnceCallback<void(CommandCompleteView)>> command_complete_callbacks; 260 std::list<base::OnceCallback<void(CommandStatusView)>> command_status_callbacks; 261 BidiQueue<AclPacketView, AclPacketBuilder> acl_queue_{3 /* TODO: Set queue depth */}; 262 263 std::queue<std::unique_ptr<CommandPacketBuilder>> command_queue_; 264 std::unique_ptr<std::promise<void>> command_promise_; 265 std::unique_ptr<std::future<void>> command_future_; 266 }; 267 268 class AclManagerNoCallbacksTest : public ::testing::Test { 269 protected: 270 void SetUp() override { 271 test_hci_layer_ = new TestHciLayer; // Ownership is transferred to registry 272 test_hci_layer_->Start(); 273 test_controller_ = new TestController; 274 fake_registry_.InjectTestModule(&HciLayer::Factory, test_hci_layer_); 275 fake_registry_.InjectTestModule(&Controller::Factory, test_controller_); 276 client_handler_ = fake_registry_.GetTestModuleHandler(&HciLayer::Factory); 277 EXPECT_NE(client_handler_, nullptr); 278 fake_registry_.Start<AclManager>(&thread_); 279 acl_manager_ = static_cast<AclManager*>(fake_registry_.GetModuleUnderTest(&AclManager::Factory)); 280 Address::FromString("A1:A2:A3:A4:A5:A6", remote); 281 } 282 283 void TearDown() override { 284 fake_registry_.SynchronizeModuleHandler(&AclManager::Factory, std::chrono::milliseconds(20)); 285 fake_registry_.StopAll(); 286 } 287 288 TestModuleRegistry fake_registry_; 289 TestHciLayer* test_hci_layer_ = nullptr; 290 TestController* test_controller_ = nullptr; 291 os::Thread& thread_ = fake_registry_.GetTestThread(); 292 AclManager* acl_manager_ = nullptr; 293 os::Handler* client_handler_ = nullptr; 294 Address remote; 295 296 std::future<void> GetConnectionFuture() { 297 ASSERT_LOG(mock_connection_callback_.connection_promise_ == nullptr, "Promises promises ... Only one at a time"); 298 mock_connection_callback_.connection_promise_ = std::make_unique<std::promise<void>>(); 299 return mock_connection_callback_.connection_promise_->get_future(); 300 } 301 302 std::future<void> GetLeConnectionFuture() { 303 ASSERT_LOG(mock_le_connection_callbacks_.le_connection_promise_ == nullptr, 304 "Promises promises ... Only one at a time"); 305 mock_le_connection_callbacks_.le_connection_promise_ = std::make_unique<std::promise<void>>(); 306 return mock_le_connection_callbacks_.le_connection_promise_->get_future(); 307 } 308 309 std::shared_ptr<AclConnection> GetLastConnection() { 310 return mock_connection_callback_.connections_.back(); 311 } 312 313 std::shared_ptr<AclConnection> GetLastLeConnection() { 314 return mock_le_connection_callbacks_.le_connections_.back(); 315 } 316 317 void SendAclData(uint16_t handle, std::shared_ptr<AclConnection> connection) { 318 auto queue_end = connection->GetAclQueueEnd(); 319 std::promise<void> promise; 320 auto future = promise.get_future(); 321 queue_end->RegisterEnqueue(client_handler_, 322 common::Bind( 323 [](decltype(queue_end) queue_end, uint16_t handle, std::promise<void> promise) { 324 queue_end->UnregisterEnqueue(); 325 promise.set_value(); 326 return NextPayload(handle); 327 }, 328 queue_end, handle, common::Passed(std::move(promise)))); 329 auto status = future.wait_for(kTimeout); 330 ASSERT_EQ(status, std::future_status::ready); 331 } 332 333 class MockConnectionCallback : public ConnectionCallbacks { 334 public: 335 void OnConnectSuccess(std::unique_ptr<AclConnection> connection) override { 336 // Convert to std::shared_ptr during push_back() 337 connections_.push_back(std::move(connection)); 338 if (connection_promise_ != nullptr) { 339 connection_promise_->set_value(); 340 connection_promise_.reset(); 341 } 342 } 343 MOCK_METHOD(void, OnConnectFail, (Address, ErrorCode reason), (override)); 344 345 std::list<std::shared_ptr<AclConnection>> connections_; 346 std::unique_ptr<std::promise<void>> connection_promise_; 347 } mock_connection_callback_; 348 349 class MockLeConnectionCallbacks : public LeConnectionCallbacks { 350 public: 351 void OnLeConnectSuccess(AddressWithType address_with_type, std::unique_ptr<AclConnection> connection) override { 352 le_connections_.push_back(std::move(connection)); 353 if (le_connection_promise_ != nullptr) { 354 le_connection_promise_->set_value(); 355 le_connection_promise_.reset(); 356 } 357 } 358 MOCK_METHOD(void, OnLeConnectFail, (AddressWithType, ErrorCode reason), (override)); 359 360 std::list<std::shared_ptr<AclConnection>> le_connections_; 361 std::unique_ptr<std::promise<void>> le_connection_promise_; 362 } mock_le_connection_callbacks_; 363 364 class MockAclManagerCallbacks : public AclManagerCallbacks { 365 public: 366 MOCK_METHOD(void, OnMasterLinkKeyComplete, (uint16_t connection_handle, KeyFlag key_flag), (override)); 367 MOCK_METHOD(void, OnRoleChange, (Address bd_addr, Role new_role), (override)); 368 MOCK_METHOD(void, OnReadDefaultLinkPolicySettingsComplete, (uint16_t default_link_policy_settings), (override)); 369 } mock_acl_manager_callbacks_; 370 }; 371 372 class AclManagerTest : public AclManagerNoCallbacksTest { 373 protected: 374 void SetUp() override { 375 AclManagerNoCallbacksTest::SetUp(); 376 acl_manager_->RegisterCallbacks(&mock_connection_callback_, client_handler_); 377 acl_manager_->RegisterLeCallbacks(&mock_le_connection_callbacks_, client_handler_); 378 acl_manager_->RegisterAclManagerCallbacks(&mock_acl_manager_callbacks_, client_handler_); 379 } 380 }; 381 382 class AclManagerWithConnectionTest : public AclManagerTest { 383 protected: 384 void SetUp() override { 385 AclManagerTest::SetUp(); 386 387 handle_ = 0x123; 388 acl_manager_->CreateConnection(remote); 389 390 // Wait for the connection request 391 std::unique_ptr<CommandPacketBuilder> last_command; 392 do { 393 last_command = test_hci_layer_->GetLastCommand(); 394 } while (last_command == nullptr); 395 396 auto first_connection = GetConnectionFuture(); 397 test_hci_layer_->IncomingEvent( 398 ConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle_, remote, LinkType::ACL, Enable::DISABLED)); 399 400 auto first_connection_status = first_connection.wait_for(kTimeout); 401 ASSERT_EQ(first_connection_status, std::future_status::ready); 402 403 connection_ = GetLastConnection(); 404 connection_->RegisterCallbacks(&mock_connection_management_callbacks_, client_handler_); 405 } 406 407 void sync_client_handler() { 408 std::promise<void> promise; 409 auto future = promise.get_future(); 410 client_handler_->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise))); 411 auto future_status = future.wait_for(std::chrono::seconds(1)); 412 EXPECT_EQ(future_status, std::future_status::ready); 413 } 414 415 uint16_t handle_; 416 std::shared_ptr<AclConnection> connection_; 417 418 class MockConnectionManagementCallbacks : public ConnectionManagementCallbacks { 419 public: 420 MOCK_METHOD1(OnConnectionPacketTypeChanged, void(uint16_t packet_type)); 421 MOCK_METHOD0(OnAuthenticationComplete, void()); 422 MOCK_METHOD1(OnEncryptionChange, void(EncryptionEnabled enabled)); 423 MOCK_METHOD0(OnChangeConnectionLinkKeyComplete, void()); 424 MOCK_METHOD1(OnReadClockOffsetComplete, void(uint16_t clock_offse)); 425 MOCK_METHOD2(OnModeChange, void(Mode current_mode, uint16_t interval)); 426 MOCK_METHOD5(OnQosSetupComplete, void(ServiceType service_type, uint32_t token_rate, uint32_t peak_bandwidth, 427 uint32_t latency, uint32_t delay_variation)); 428 MOCK_METHOD6(OnFlowSpecificationComplete, 429 void(FlowDirection flow_direction, ServiceType service_type, uint32_t token_rate, 430 uint32_t token_bucket_size, uint32_t peak_bandwidth, uint32_t access_latency)); 431 MOCK_METHOD0(OnFlushOccurred, void()); 432 MOCK_METHOD1(OnRoleDiscoveryComplete, void(Role current_role)); 433 MOCK_METHOD1(OnReadLinkPolicySettingsComplete, void(uint16_t link_policy_settings)); 434 MOCK_METHOD1(OnReadAutomaticFlushTimeoutComplete, void(uint16_t flush_timeout)); 435 MOCK_METHOD1(OnReadTransmitPowerLevelComplete, void(uint8_t transmit_power_level)); 436 MOCK_METHOD1(OnReadLinkSupervisionTimeoutComplete, void(uint16_t link_supervision_timeout)); 437 MOCK_METHOD1(OnReadFailedContactCounterComplete, void(uint16_t failed_contact_counter)); 438 MOCK_METHOD1(OnReadLinkQualityComplete, void(uint8_t link_quality)); 439 MOCK_METHOD2(OnReadAfhChannelMapComplete, void(AfhMode afh_mode, std::array<uint8_t, 10> afh_channel_map)); 440 MOCK_METHOD1(OnReadRssiComplete, void(uint8_t rssi)); 441 MOCK_METHOD2(OnReadClockComplete, void(uint32_t clock, uint16_t accuracy)); 442 } mock_connection_management_callbacks_; 443 }; 444 445 TEST_F(AclManagerTest, startup_teardown) {} 446 447 TEST_F(AclManagerNoCallbacksTest, acl_connection_before_registered_callbacks) { 448 ClassOfDevice class_of_device; 449 450 test_hci_layer_->IncomingEvent( 451 ConnectionRequestBuilder::Create(remote, class_of_device, ConnectionRequestLinkType::ACL)); 452 fake_registry_.SynchronizeModuleHandler(&HciLayer::Factory, std::chrono::milliseconds(20)); 453 fake_registry_.SynchronizeModuleHandler(&AclManager::Factory, std::chrono::milliseconds(20)); 454 fake_registry_.SynchronizeModuleHandler(&HciLayer::Factory, std::chrono::milliseconds(20)); 455 auto last_command = test_hci_layer_->GetLastCommand(); 456 auto packet = GetPacketView(std::move(last_command)); 457 CommandPacketView command = CommandPacketView::Create(packet); 458 EXPECT_TRUE(command.IsValid()); 459 OpCode op_code = command.GetOpCode(); 460 EXPECT_EQ(op_code, OpCode::REJECT_CONNECTION_REQUEST); 461 } 462 463 TEST_F(AclManagerTest, invoke_registered_callback_connection_complete_success) { 464 uint16_t handle = 1; 465 466 test_hci_layer_->SetCommandFuture(); 467 acl_manager_->CreateConnection(remote); 468 469 // Wait for the connection request 470 std::unique_ptr<CommandPacketBuilder> last_command; 471 do { 472 last_command = test_hci_layer_->GetLastCommand(); 473 } while (last_command == nullptr); 474 475 auto first_connection = GetConnectionFuture(); 476 477 test_hci_layer_->IncomingEvent( 478 ConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle, remote, LinkType::ACL, Enable::DISABLED)); 479 480 auto first_connection_status = first_connection.wait_for(kTimeout); 481 ASSERT_EQ(first_connection_status, std::future_status::ready); 482 483 std::shared_ptr<AclConnection> connection = GetLastConnection(); 484 ASSERT_EQ(connection->GetAddress(), remote); 485 } 486 487 TEST_F(AclManagerTest, invoke_registered_callback_connection_complete_fail) { 488 uint16_t handle = 0x123; 489 490 test_hci_layer_->SetCommandFuture(); 491 acl_manager_->CreateConnection(remote); 492 493 // Wait for the connection request 494 std::unique_ptr<CommandPacketBuilder> last_command; 495 do { 496 last_command = test_hci_layer_->GetLastCommand(); 497 } while (last_command == nullptr); 498 499 EXPECT_CALL(mock_connection_callback_, OnConnectFail(remote, ErrorCode::PAGE_TIMEOUT)); 500 test_hci_layer_->IncomingEvent( 501 ConnectionCompleteBuilder::Create(ErrorCode::PAGE_TIMEOUT, handle, remote, LinkType::ACL, Enable::DISABLED)); 502 fake_registry_.SynchronizeModuleHandler(&HciLayer::Factory, std::chrono::milliseconds(20)); 503 fake_registry_.SynchronizeModuleHandler(&AclManager::Factory, std::chrono::milliseconds(20)); 504 fake_registry_.SynchronizeModuleHandler(&HciLayer::Factory, std::chrono::milliseconds(20)); 505 } 506 507 // TODO: implement version of this test where controller supports Extended Advertising Feature in 508 // GetControllerLeLocalSupportedFeatures, and LE Extended Create Connection is used 509 TEST_F(AclManagerTest, invoke_registered_callback_le_connection_complete_success) { 510 AddressWithType remote_with_type(remote, AddressType::PUBLIC_DEVICE_ADDRESS); 511 test_hci_layer_->SetCommandFuture(); 512 acl_manager_->CreateLeConnection(remote_with_type); 513 514 auto packet = test_hci_layer_->GetCommandPacket(OpCode::LE_CREATE_CONNECTION); 515 auto le_connection_management_command_view = LeConnectionManagementCommandView::Create(packet); 516 auto command_view = LeCreateConnectionView::Create(le_connection_management_command_view); 517 ASSERT(command_view.IsValid()); 518 EXPECT_EQ(command_view.GetPeerAddress(), remote); 519 EXPECT_EQ(command_view.GetPeerAddressType(), AddressType::PUBLIC_DEVICE_ADDRESS); 520 521 test_hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); 522 523 auto first_connection = GetLeConnectionFuture(); 524 525 test_hci_layer_->IncomingLeMetaEvent( 526 LeConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, 0x123, Role::SLAVE, AddressType::PUBLIC_DEVICE_ADDRESS, 527 remote, 0x0100, 0x0010, 0x0011, MasterClockAccuracy::PPM_30)); 528 529 auto first_connection_status = first_connection.wait_for(kTimeout); 530 ASSERT_EQ(first_connection_status, std::future_status::ready); 531 532 std::shared_ptr<AclConnection> connection = GetLastLeConnection(); 533 ASSERT_EQ(connection->GetAddress(), remote); 534 } 535 536 TEST_F(AclManagerTest, invoke_registered_callback_le_connection_complete_fail) { 537 AddressWithType remote_with_type(remote, AddressType::PUBLIC_DEVICE_ADDRESS); 538 test_hci_layer_->SetCommandFuture(); 539 acl_manager_->CreateLeConnection(remote_with_type); 540 541 auto packet = test_hci_layer_->GetCommandPacket(OpCode::LE_CREATE_CONNECTION); 542 auto le_connection_management_command_view = LeConnectionManagementCommandView::Create(packet); 543 auto command_view = LeCreateConnectionView::Create(le_connection_management_command_view); 544 ASSERT(command_view.IsValid()); 545 EXPECT_EQ(command_view.GetPeerAddress(), remote); 546 EXPECT_EQ(command_view.GetPeerAddressType(), AddressType::PUBLIC_DEVICE_ADDRESS); 547 548 test_hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); 549 550 EXPECT_CALL(mock_le_connection_callbacks_, 551 OnLeConnectFail(remote_with_type, ErrorCode::CONNECTION_REJECTED_LIMITED_RESOURCES)); 552 test_hci_layer_->IncomingLeMetaEvent(LeConnectionCompleteBuilder::Create( 553 ErrorCode::CONNECTION_REJECTED_LIMITED_RESOURCES, 0x123, Role::SLAVE, AddressType::PUBLIC_DEVICE_ADDRESS, remote, 554 0x0100, 0x0010, 0x0011, MasterClockAccuracy::PPM_30)); 555 } 556 557 TEST_F(AclManagerTest, invoke_registered_callback_le_connection_update_success) { 558 AddressWithType remote_with_type(remote, AddressType::PUBLIC_DEVICE_ADDRESS); 559 test_hci_layer_->SetCommandFuture(); 560 acl_manager_->CreateLeConnection(remote_with_type); 561 562 auto packet = test_hci_layer_->GetCommandPacket(OpCode::LE_CREATE_CONNECTION); 563 auto le_connection_management_command_view = LeConnectionManagementCommandView::Create(packet); 564 auto command_view = LeCreateConnectionView::Create(le_connection_management_command_view); 565 ASSERT(command_view.IsValid()); 566 EXPECT_EQ(command_view.GetPeerAddress(), remote); 567 EXPECT_EQ(command_view.GetPeerAddressType(), AddressType::PUBLIC_DEVICE_ADDRESS); 568 569 test_hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01)); 570 571 auto first_connection = GetLeConnectionFuture(); 572 573 test_hci_layer_->IncomingLeMetaEvent( 574 LeConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, 0x123, Role::SLAVE, AddressType::PUBLIC_DEVICE_ADDRESS, 575 remote, 0x0100, 0x0010, 0x0011, MasterClockAccuracy::PPM_30)); 576 577 auto first_connection_status = first_connection.wait_for(kTimeout); 578 ASSERT_EQ(first_connection_status, std::future_status::ready); 579 580 std::shared_ptr<AclConnection> connection = GetLastLeConnection(); 581 ASSERT_EQ(connection->GetAddress(), remote); 582 583 std::promise<ErrorCode> promise; 584 auto future = promise.get_future(); 585 connection->LeConnectionUpdate( 586 0x0006, 0x0C80, 0x0000, 0x000A, 587 common::BindOnce([](std::promise<ErrorCode> promise, ErrorCode code) { promise.set_value(code); }, 588 std::move(promise)), 589 client_handler_); 590 test_hci_layer_->IncomingLeMetaEvent( 591 LeConnectionUpdateCompleteBuilder::Create(ErrorCode::SUCCESS, 0x123, 0x0006, 0x0000, 0x000A)); 592 EXPECT_EQ(future.wait_for(std::chrono::milliseconds(3)), std::future_status::ready); 593 EXPECT_EQ(future.get(), ErrorCode::SUCCESS); 594 } 595 596 TEST_F(AclManagerTest, invoke_registered_callback_disconnection_complete) { 597 uint16_t handle = 0x123; 598 599 test_hci_layer_->SetCommandFuture(); 600 acl_manager_->CreateConnection(remote); 601 602 // Wait for the connection request 603 std::unique_ptr<CommandPacketBuilder> last_command; 604 do { 605 last_command = test_hci_layer_->GetLastCommand(); 606 } while (last_command == nullptr); 607 608 auto first_connection = GetConnectionFuture(); 609 610 test_hci_layer_->IncomingEvent( 611 ConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle, remote, LinkType::ACL, Enable::DISABLED)); 612 613 auto first_connection_status = first_connection.wait_for(kTimeout); 614 ASSERT_EQ(first_connection_status, std::future_status::ready); 615 616 std::shared_ptr<AclConnection> connection = GetLastConnection(); 617 618 // Register the disconnect handler 619 std::promise<ErrorCode> promise; 620 auto future = promise.get_future(); 621 connection->RegisterDisconnectCallback( 622 common::BindOnce([](std::promise<ErrorCode> promise, ErrorCode reason) { promise.set_value(reason); }, 623 std::move(promise)), 624 client_handler_); 625 626 test_hci_layer_->IncomingEvent( 627 DisconnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle, ErrorCode::REMOTE_USER_TERMINATED_CONNECTION)); 628 629 auto disconnection_status = future.wait_for(kTimeout); 630 ASSERT_EQ(disconnection_status, std::future_status::ready); 631 ASSERT_EQ(ErrorCode::REMOTE_USER_TERMINATED_CONNECTION, future.get()); 632 633 fake_registry_.SynchronizeModuleHandler(&HciLayer::Factory, std::chrono::milliseconds(20)); 634 } 635 636 TEST_F(AclManagerTest, acl_connection_finish_after_disconnected) { 637 uint16_t handle = 0x123; 638 639 test_hci_layer_->SetCommandFuture(); 640 acl_manager_->CreateConnection(remote); 641 642 // Wait for the connection request 643 std::unique_ptr<CommandPacketBuilder> last_command; 644 do { 645 last_command = test_hci_layer_->GetLastCommand(); 646 } while (last_command == nullptr); 647 648 auto first_connection = GetConnectionFuture(); 649 650 test_hci_layer_->IncomingEvent( 651 ConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle, remote, LinkType::ACL, Enable::DISABLED)); 652 653 auto first_connection_status = first_connection.wait_for(kTimeout); 654 ASSERT_EQ(first_connection_status, std::future_status::ready); 655 656 std::shared_ptr<AclConnection> connection = GetLastConnection(); 657 658 // Register the disconnect handler 659 std::promise<ErrorCode> promise; 660 auto future = promise.get_future(); 661 connection->RegisterDisconnectCallback( 662 common::BindOnce([](std::promise<ErrorCode> promise, ErrorCode reason) { promise.set_value(reason); }, 663 std::move(promise)), 664 client_handler_); 665 666 test_hci_layer_->IncomingEvent(DisconnectionCompleteBuilder::Create( 667 ErrorCode::SUCCESS, handle, ErrorCode::REMOTE_DEVICE_TERMINATED_CONNECTION_POWER_OFF)); 668 669 auto disconnection_status = future.wait_for(kTimeout); 670 ASSERT_EQ(disconnection_status, std::future_status::ready); 671 ASSERT_EQ(ErrorCode::REMOTE_DEVICE_TERMINATED_CONNECTION_POWER_OFF, future.get()); 672 673 connection->Finish(); 674 } 675 676 TEST_F(AclManagerTest, acl_send_data_one_connection) { 677 uint16_t handle = 0x123; 678 679 acl_manager_->CreateConnection(remote); 680 681 // Wait for the connection request 682 std::unique_ptr<CommandPacketBuilder> last_command; 683 do { 684 last_command = test_hci_layer_->GetLastCommand(); 685 } while (last_command == nullptr); 686 687 auto first_connection = GetConnectionFuture(); 688 689 test_hci_layer_->IncomingEvent( 690 ConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle, remote, LinkType::ACL, Enable::DISABLED)); 691 692 auto first_connection_status = first_connection.wait_for(kTimeout); 693 ASSERT_EQ(first_connection_status, std::future_status::ready); 694 695 std::shared_ptr<AclConnection> connection = GetLastConnection(); 696 697 // Register the disconnect handler 698 connection->RegisterDisconnectCallback( 699 common::Bind([](std::shared_ptr<AclConnection> conn, ErrorCode) { conn->Finish(); }, connection), 700 client_handler_); 701 702 // Send a packet from HCI 703 test_hci_layer_->IncomingAclData(handle); 704 auto queue_end = connection->GetAclQueueEnd(); 705 706 std::unique_ptr<PacketView<kLittleEndian>> received; 707 do { 708 received = queue_end->TryDequeue(); 709 } while (received == nullptr); 710 711 PacketView<kLittleEndian> received_packet = *received; 712 713 // Send a packet from the connection 714 SendAclData(handle, connection); 715 716 auto sent_packet = test_hci_layer_->OutgoingAclData(); 717 718 // Send another packet from the connection 719 SendAclData(handle, connection); 720 721 sent_packet = test_hci_layer_->OutgoingAclData(); 722 connection->Disconnect(DisconnectReason::AUTHENTICATION_FAILURE); 723 } 724 725 TEST_F(AclManagerTest, acl_send_data_credits) { 726 uint16_t handle = 0x123; 727 728 acl_manager_->CreateConnection(remote); 729 730 // Wait for the connection request 731 std::unique_ptr<CommandPacketBuilder> last_command; 732 do { 733 last_command = test_hci_layer_->GetLastCommand(); 734 } while (last_command == nullptr); 735 736 auto first_connection = GetConnectionFuture(); 737 test_hci_layer_->IncomingEvent( 738 ConnectionCompleteBuilder::Create(ErrorCode::SUCCESS, handle, remote, LinkType::ACL, Enable::DISABLED)); 739 740 auto first_connection_status = first_connection.wait_for(kTimeout); 741 ASSERT_EQ(first_connection_status, std::future_status::ready); 742 743 std::shared_ptr<AclConnection> connection = GetLastConnection(); 744 745 // Register the disconnect handler 746 connection->RegisterDisconnectCallback( 747 common::BindOnce([](std::shared_ptr<AclConnection> conn, ErrorCode) { conn->Finish(); }, connection), 748 client_handler_); 749 750 // Use all the credits 751 for (uint16_t credits = 0; credits < test_controller_->total_acl_buffers_; credits++) { 752 // Send a packet from the connection 753 SendAclData(handle, connection); 754 755 auto sent_packet = test_hci_layer_->OutgoingAclData(); 756 } 757 758 // Send another packet from the connection 759 SendAclData(handle, connection); 760 761 test_hci_layer_->AssertNoOutgoingAclData(); 762 763 test_controller_->CompletePackets(handle, 1); 764 765 auto after_credits_sent_packet = test_hci_layer_->OutgoingAclData(); 766 767 connection->Disconnect(DisconnectReason::AUTHENTICATION_FAILURE); 768 } 769 770 TEST_F(AclManagerWithConnectionTest, send_switch_role) { 771 test_hci_layer_->SetCommandFuture(); 772 acl_manager_->SwitchRole(connection_->GetAddress(), Role::SLAVE); 773 auto packet = test_hci_layer_->GetCommandPacket(OpCode::SWITCH_ROLE); 774 auto command_view = SwitchRoleView::Create(packet); 775 ASSERT(command_view.IsValid()); 776 EXPECT_EQ(command_view.GetBdAddr(), connection_->GetAddress()); 777 EXPECT_EQ(command_view.GetRole(), Role::SLAVE); 778 779 EXPECT_CALL(mock_acl_manager_callbacks_, OnRoleChange(connection_->GetAddress(), Role::SLAVE)); 780 test_hci_layer_->IncomingEvent(RoleChangeBuilder::Create(ErrorCode::SUCCESS, connection_->GetAddress(), Role::SLAVE)); 781 } 782 783 TEST_F(AclManagerWithConnectionTest, send_read_default_link_policy_settings) { 784 test_hci_layer_->SetCommandFuture(); 785 acl_manager_->ReadDefaultLinkPolicySettings(); 786 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_DEFAULT_LINK_POLICY_SETTINGS); 787 auto command_view = ReadDefaultLinkPolicySettingsView::Create(packet); 788 ASSERT(command_view.IsValid()); 789 790 test_hci_layer_->SetCommandFuture(); 791 EXPECT_CALL(mock_acl_manager_callbacks_, OnReadDefaultLinkPolicySettingsComplete(0x07)); 792 uint8_t num_packets = 1; 793 test_hci_layer_->IncomingEvent( 794 ReadDefaultLinkPolicySettingsCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, 0x07)); 795 } 796 797 TEST_F(AclManagerWithConnectionTest, send_write_default_link_policy_settings) { 798 test_hci_layer_->SetCommandFuture(); 799 acl_manager_->WriteDefaultLinkPolicySettings(0x05); 800 auto packet = test_hci_layer_->GetCommandPacket(OpCode::WRITE_DEFAULT_LINK_POLICY_SETTINGS); 801 auto command_view = WriteDefaultLinkPolicySettingsView::Create(packet); 802 ASSERT(command_view.IsValid()); 803 EXPECT_EQ(command_view.GetDefaultLinkPolicySettings(), 0x05); 804 805 uint8_t num_packets = 1; 806 test_hci_layer_->IncomingEvent( 807 WriteDefaultLinkPolicySettingsCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS)); 808 } 809 810 TEST_F(AclManagerWithConnectionTest, send_change_connection_packet_type) { 811 test_hci_layer_->SetCommandFuture(); 812 connection_->ChangeConnectionPacketType(0xEE1C); 813 auto packet = test_hci_layer_->GetCommandPacket(OpCode::CHANGE_CONNECTION_PACKET_TYPE); 814 auto command_view = ChangeConnectionPacketTypeView::Create(packet); 815 ASSERT(command_view.IsValid()); 816 EXPECT_EQ(command_view.GetPacketType(), 0xEE1C); 817 818 EXPECT_CALL(mock_connection_management_callbacks_, OnConnectionPacketTypeChanged(0xEE1C)); 819 test_hci_layer_->IncomingEvent(ConnectionPacketTypeChangedBuilder::Create(ErrorCode::SUCCESS, handle_, 0xEE1C)); 820 } 821 822 TEST_F(AclManagerWithConnectionTest, send_authentication_requested) { 823 test_hci_layer_->SetCommandFuture(); 824 connection_->AuthenticationRequested(); 825 auto packet = test_hci_layer_->GetCommandPacket(OpCode::AUTHENTICATION_REQUESTED); 826 auto command_view = AuthenticationRequestedView::Create(packet); 827 ASSERT(command_view.IsValid()); 828 829 EXPECT_CALL(mock_connection_management_callbacks_, OnAuthenticationComplete); 830 test_hci_layer_->IncomingEvent(AuthenticationCompleteBuilder::Create(ErrorCode::SUCCESS, handle_)); 831 } 832 833 TEST_F(AclManagerWithConnectionTest, send_read_clock_offset) { 834 test_hci_layer_->SetCommandFuture(); 835 connection_->ReadClockOffset(); 836 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_CLOCK_OFFSET); 837 auto command_view = ReadClockOffsetView::Create(packet); 838 ASSERT(command_view.IsValid()); 839 840 EXPECT_CALL(mock_connection_management_callbacks_, OnReadClockOffsetComplete(0x0123)); 841 test_hci_layer_->IncomingEvent(ReadClockOffsetCompleteBuilder::Create(ErrorCode::SUCCESS, handle_, 0x0123)); 842 } 843 844 TEST_F(AclManagerWithConnectionTest, send_hold_mode) { 845 test_hci_layer_->SetCommandFuture(); 846 connection_->HoldMode(0x0500, 0x0020); 847 auto packet = test_hci_layer_->GetCommandPacket(OpCode::HOLD_MODE); 848 auto command_view = HoldModeView::Create(packet); 849 ASSERT(command_view.IsValid()); 850 EXPECT_EQ(command_view.GetHoldModeMaxInterval(), 0x0500); 851 EXPECT_EQ(command_view.GetHoldModeMinInterval(), 0x0020); 852 853 EXPECT_CALL(mock_connection_management_callbacks_, OnModeChange(Mode::HOLD, 0x0020)); 854 test_hci_layer_->IncomingEvent(ModeChangeBuilder::Create(ErrorCode::SUCCESS, handle_, Mode::HOLD, 0x0020)); 855 } 856 857 TEST_F(AclManagerWithConnectionTest, send_sniff_mode) { 858 test_hci_layer_->SetCommandFuture(); 859 connection_->SniffMode(0x0500, 0x0020, 0x0040, 0x0014); 860 auto packet = test_hci_layer_->GetCommandPacket(OpCode::SNIFF_MODE); 861 auto command_view = SniffModeView::Create(packet); 862 ASSERT(command_view.IsValid()); 863 EXPECT_EQ(command_view.GetSniffMaxInterval(), 0x0500); 864 EXPECT_EQ(command_view.GetSniffMinInterval(), 0x0020); 865 EXPECT_EQ(command_view.GetSniffAttempt(), 0x0040); 866 EXPECT_EQ(command_view.GetSniffTimeout(), 0x0014); 867 868 EXPECT_CALL(mock_connection_management_callbacks_, OnModeChange(Mode::SNIFF, 0x0028)); 869 test_hci_layer_->IncomingEvent(ModeChangeBuilder::Create(ErrorCode::SUCCESS, handle_, Mode::SNIFF, 0x0028)); 870 } 871 872 TEST_F(AclManagerWithConnectionTest, send_exit_sniff_mode) { 873 test_hci_layer_->SetCommandFuture(); 874 connection_->ExitSniffMode(); 875 auto packet = test_hci_layer_->GetCommandPacket(OpCode::EXIT_SNIFF_MODE); 876 auto command_view = ExitSniffModeView::Create(packet); 877 ASSERT(command_view.IsValid()); 878 879 EXPECT_CALL(mock_connection_management_callbacks_, OnModeChange(Mode::ACTIVE, 0x00)); 880 test_hci_layer_->IncomingEvent(ModeChangeBuilder::Create(ErrorCode::SUCCESS, handle_, Mode::ACTIVE, 0x00)); 881 } 882 883 TEST_F(AclManagerWithConnectionTest, send_qos_setup) { 884 test_hci_layer_->SetCommandFuture(); 885 connection_->QosSetup(ServiceType::BEST_EFFORT, 0x1234, 0x1233, 0x1232, 0x1231); 886 auto packet = test_hci_layer_->GetCommandPacket(OpCode::QOS_SETUP); 887 auto command_view = QosSetupView::Create(packet); 888 ASSERT(command_view.IsValid()); 889 EXPECT_EQ(command_view.GetServiceType(), ServiceType::BEST_EFFORT); 890 EXPECT_EQ(command_view.GetTokenRate(), 0x1234); 891 EXPECT_EQ(command_view.GetPeakBandwidth(), 0x1233); 892 EXPECT_EQ(command_view.GetLatency(), 0x1232); 893 EXPECT_EQ(command_view.GetDelayVariation(), 0x1231); 894 895 EXPECT_CALL(mock_connection_management_callbacks_, 896 OnQosSetupComplete(ServiceType::BEST_EFFORT, 0x1234, 0x1233, 0x1232, 0x1231)); 897 test_hci_layer_->IncomingEvent(QosSetupCompleteBuilder::Create(ErrorCode::SUCCESS, handle_, ServiceType::BEST_EFFORT, 898 0x1234, 0x1233, 0x1232, 0x1231)); 899 } 900 901 TEST_F(AclManagerWithConnectionTest, send_flow_specification) { 902 test_hci_layer_->SetCommandFuture(); 903 connection_->FlowSpecification(FlowDirection::OUTGOING_FLOW, ServiceType::BEST_EFFORT, 0x1234, 0x1233, 0x1232, 904 0x1231); 905 auto packet = test_hci_layer_->GetCommandPacket(OpCode::FLOW_SPECIFICATION); 906 auto command_view = FlowSpecificationView::Create(packet); 907 ASSERT(command_view.IsValid()); 908 EXPECT_EQ(command_view.GetFlowDirection(), FlowDirection::OUTGOING_FLOW); 909 EXPECT_EQ(command_view.GetServiceType(), ServiceType::BEST_EFFORT); 910 EXPECT_EQ(command_view.GetTokenRate(), 0x1234); 911 EXPECT_EQ(command_view.GetTokenBucketSize(), 0x1233); 912 EXPECT_EQ(command_view.GetPeakBandwidth(), 0x1232); 913 EXPECT_EQ(command_view.GetAccessLatency(), 0x1231); 914 915 EXPECT_CALL(mock_connection_management_callbacks_, 916 OnFlowSpecificationComplete(FlowDirection::OUTGOING_FLOW, ServiceType::BEST_EFFORT, 0x1234, 0x1233, 917 0x1232, 0x1231)); 918 test_hci_layer_->IncomingEvent( 919 FlowSpecificationCompleteBuilder::Create(ErrorCode::SUCCESS, handle_, FlowDirection::OUTGOING_FLOW, 920 ServiceType::BEST_EFFORT, 0x1234, 0x1233, 0x1232, 0x1231)); 921 } 922 923 TEST_F(AclManagerWithConnectionTest, send_flush) { 924 test_hci_layer_->SetCommandFuture(); 925 connection_->Flush(); 926 auto packet = test_hci_layer_->GetCommandPacket(OpCode::FLUSH); 927 auto command_view = FlushView::Create(packet); 928 ASSERT(command_view.IsValid()); 929 930 EXPECT_CALL(mock_connection_management_callbacks_, OnFlushOccurred()); 931 test_hci_layer_->IncomingEvent(FlushOccurredBuilder::Create(handle_)); 932 } 933 934 TEST_F(AclManagerWithConnectionTest, send_role_discovery) { 935 test_hci_layer_->SetCommandFuture(); 936 connection_->RoleDiscovery(); 937 auto packet = test_hci_layer_->GetCommandPacket(OpCode::ROLE_DISCOVERY); 938 auto command_view = RoleDiscoveryView::Create(packet); 939 ASSERT(command_view.IsValid()); 940 941 EXPECT_CALL(mock_connection_management_callbacks_, OnRoleDiscoveryComplete(Role::MASTER)); 942 uint8_t num_packets = 1; 943 test_hci_layer_->IncomingEvent( 944 RoleDiscoveryCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, Role::MASTER)); 945 } 946 947 TEST_F(AclManagerWithConnectionTest, send_read_link_policy_settings) { 948 test_hci_layer_->SetCommandFuture(); 949 connection_->ReadLinkPolicySettings(); 950 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_LINK_POLICY_SETTINGS); 951 auto command_view = ReadLinkPolicySettingsView::Create(packet); 952 ASSERT(command_view.IsValid()); 953 954 EXPECT_CALL(mock_connection_management_callbacks_, OnReadLinkPolicySettingsComplete(0x07)); 955 uint8_t num_packets = 1; 956 test_hci_layer_->IncomingEvent( 957 ReadLinkPolicySettingsCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x07)); 958 } 959 960 TEST_F(AclManagerWithConnectionTest, send_write_link_policy_settings) { 961 test_hci_layer_->SetCommandFuture(); 962 connection_->WriteLinkPolicySettings(0x05); 963 auto packet = test_hci_layer_->GetCommandPacket(OpCode::WRITE_LINK_POLICY_SETTINGS); 964 auto command_view = WriteLinkPolicySettingsView::Create(packet); 965 ASSERT(command_view.IsValid()); 966 EXPECT_EQ(command_view.GetLinkPolicySettings(), 0x05); 967 968 uint8_t num_packets = 1; 969 test_hci_layer_->IncomingEvent( 970 WriteLinkPolicySettingsCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_)); 971 } 972 973 TEST_F(AclManagerWithConnectionTest, send_sniff_subrating) { 974 test_hci_layer_->SetCommandFuture(); 975 connection_->SniffSubrating(0x1234, 0x1235, 0x1236); 976 auto packet = test_hci_layer_->GetCommandPacket(OpCode::SNIFF_SUBRATING); 977 auto command_view = SniffSubratingView::Create(packet); 978 ASSERT(command_view.IsValid()); 979 EXPECT_EQ(command_view.GetMaximumLatency(), 0x1234); 980 EXPECT_EQ(command_view.GetMinimumRemoteTimeout(), 0x1235); 981 EXPECT_EQ(command_view.GetMinimumLocalTimeout(), 0x1236); 982 983 uint8_t num_packets = 1; 984 test_hci_layer_->IncomingEvent(SniffSubratingCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_)); 985 } 986 987 TEST_F(AclManagerWithConnectionTest, send_read_automatic_flush_timeout) { 988 test_hci_layer_->SetCommandFuture(); 989 connection_->ReadAutomaticFlushTimeout(); 990 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_AUTOMATIC_FLUSH_TIMEOUT); 991 auto command_view = ReadAutomaticFlushTimeoutView::Create(packet); 992 ASSERT(command_view.IsValid()); 993 994 EXPECT_CALL(mock_connection_management_callbacks_, OnReadAutomaticFlushTimeoutComplete(0x07ff)); 995 uint8_t num_packets = 1; 996 test_hci_layer_->IncomingEvent( 997 ReadAutomaticFlushTimeoutCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x07ff)); 998 } 999 1000 TEST_F(AclManagerWithConnectionTest, send_write_automatic_flush_timeout) { 1001 test_hci_layer_->SetCommandFuture(); 1002 connection_->WriteAutomaticFlushTimeout(0x07FF); 1003 auto packet = test_hci_layer_->GetCommandPacket(OpCode::WRITE_AUTOMATIC_FLUSH_TIMEOUT); 1004 auto command_view = WriteAutomaticFlushTimeoutView::Create(packet); 1005 ASSERT(command_view.IsValid()); 1006 EXPECT_EQ(command_view.GetFlushTimeout(), 0x07FF); 1007 1008 uint8_t num_packets = 1; 1009 test_hci_layer_->IncomingEvent( 1010 WriteAutomaticFlushTimeoutCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_)); 1011 } 1012 1013 TEST_F(AclManagerWithConnectionTest, send_read_transmit_power_level) { 1014 test_hci_layer_->SetCommandFuture(); 1015 connection_->ReadTransmitPowerLevel(TransmitPowerLevelType::CURRENT); 1016 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_TRANSMIT_POWER_LEVEL); 1017 auto command_view = ReadTransmitPowerLevelView::Create(packet); 1018 ASSERT(command_view.IsValid()); 1019 EXPECT_EQ(command_view.GetType(), TransmitPowerLevelType::CURRENT); 1020 1021 EXPECT_CALL(mock_connection_management_callbacks_, OnReadTransmitPowerLevelComplete(0x07)); 1022 uint8_t num_packets = 1; 1023 test_hci_layer_->IncomingEvent( 1024 ReadTransmitPowerLevelCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x07)); 1025 } 1026 1027 TEST_F(AclManagerWithConnectionTest, send_read_link_supervision_timeout) { 1028 test_hci_layer_->SetCommandFuture(); 1029 connection_->ReadLinkSupervisionTimeout(); 1030 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_LINK_SUPERVISION_TIMEOUT); 1031 auto command_view = ReadLinkSupervisionTimeoutView::Create(packet); 1032 ASSERT(command_view.IsValid()); 1033 1034 EXPECT_CALL(mock_connection_management_callbacks_, OnReadLinkSupervisionTimeoutComplete(0x5677)); 1035 uint8_t num_packets = 1; 1036 test_hci_layer_->IncomingEvent( 1037 ReadLinkSupervisionTimeoutCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x5677)); 1038 } 1039 1040 TEST_F(AclManagerWithConnectionTest, send_write_link_supervision_timeout) { 1041 test_hci_layer_->SetCommandFuture(); 1042 connection_->WriteLinkSupervisionTimeout(0x5678); 1043 auto packet = test_hci_layer_->GetCommandPacket(OpCode::WRITE_LINK_SUPERVISION_TIMEOUT); 1044 auto command_view = WriteLinkSupervisionTimeoutView::Create(packet); 1045 ASSERT(command_view.IsValid()); 1046 EXPECT_EQ(command_view.GetLinkSupervisionTimeout(), 0x5678); 1047 1048 uint8_t num_packets = 1; 1049 test_hci_layer_->IncomingEvent( 1050 WriteLinkSupervisionTimeoutCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_)); 1051 } 1052 1053 TEST_F(AclManagerWithConnectionTest, send_read_failed_contact_counter) { 1054 test_hci_layer_->SetCommandFuture(); 1055 connection_->ReadFailedContactCounter(); 1056 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_FAILED_CONTACT_COUNTER); 1057 auto command_view = ReadFailedContactCounterView::Create(packet); 1058 ASSERT(command_view.IsValid()); 1059 1060 EXPECT_CALL(mock_connection_management_callbacks_, OnReadFailedContactCounterComplete(0x00)); 1061 uint8_t num_packets = 1; 1062 test_hci_layer_->IncomingEvent( 1063 ReadFailedContactCounterCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x00)); 1064 } 1065 1066 TEST_F(AclManagerWithConnectionTest, send_reset_failed_contact_counter) { 1067 test_hci_layer_->SetCommandFuture(); 1068 connection_->ResetFailedContactCounter(); 1069 auto packet = test_hci_layer_->GetCommandPacket(OpCode::RESET_FAILED_CONTACT_COUNTER); 1070 auto command_view = ResetFailedContactCounterView::Create(packet); 1071 ASSERT(command_view.IsValid()); 1072 1073 uint8_t num_packets = 1; 1074 test_hci_layer_->IncomingEvent( 1075 ResetFailedContactCounterCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_)); 1076 } 1077 1078 TEST_F(AclManagerWithConnectionTest, send_read_link_quality) { 1079 test_hci_layer_->SetCommandFuture(); 1080 connection_->ReadLinkQuality(); 1081 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_LINK_QUALITY); 1082 auto command_view = ReadLinkQualityView::Create(packet); 1083 ASSERT(command_view.IsValid()); 1084 1085 EXPECT_CALL(mock_connection_management_callbacks_, OnReadLinkQualityComplete(0xa9)); 1086 uint8_t num_packets = 1; 1087 test_hci_layer_->IncomingEvent( 1088 ReadLinkQualityCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0xa9)); 1089 } 1090 1091 TEST_F(AclManagerWithConnectionTest, send_read_afh_channel_map) { 1092 test_hci_layer_->SetCommandFuture(); 1093 connection_->ReadAfhChannelMap(); 1094 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_AFH_CHANNEL_MAP); 1095 auto command_view = ReadAfhChannelMapView::Create(packet); 1096 ASSERT(command_view.IsValid()); 1097 std::array<uint8_t, 10> afh_channel_map = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; 1098 1099 EXPECT_CALL(mock_connection_management_callbacks_, 1100 OnReadAfhChannelMapComplete(AfhMode::AFH_ENABLED, afh_channel_map)); 1101 uint8_t num_packets = 1; 1102 test_hci_layer_->IncomingEvent(ReadAfhChannelMapCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 1103 AfhMode::AFH_ENABLED, afh_channel_map)); 1104 } 1105 1106 TEST_F(AclManagerWithConnectionTest, send_read_rssi) { 1107 test_hci_layer_->SetCommandFuture(); 1108 connection_->ReadRssi(); 1109 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_RSSI); 1110 auto command_view = ReadRssiView::Create(packet); 1111 ASSERT(command_view.IsValid()); 1112 sync_client_handler(); 1113 EXPECT_CALL(mock_connection_management_callbacks_, OnReadRssiComplete(0x00)); 1114 uint8_t num_packets = 1; 1115 test_hci_layer_->IncomingEvent(ReadRssiCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x00)); 1116 } 1117 1118 TEST_F(AclManagerWithConnectionTest, send_read_clock) { 1119 test_hci_layer_->SetCommandFuture(); 1120 connection_->ReadClock(WhichClock::LOCAL); 1121 auto packet = test_hci_layer_->GetCommandPacket(OpCode::READ_CLOCK); 1122 auto command_view = ReadClockView::Create(packet); 1123 ASSERT(command_view.IsValid()); 1124 EXPECT_EQ(command_view.GetWhichClock(), WhichClock::LOCAL); 1125 1126 EXPECT_CALL(mock_connection_management_callbacks_, OnReadClockComplete(0x00002e6a, 0x0000)); 1127 uint8_t num_packets = 1; 1128 test_hci_layer_->IncomingEvent( 1129 ReadClockCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, handle_, 0x00002e6a, 0x0000)); 1130 } 1131 1132 } // namespace 1133 } // namespace hci 1134 } // namespace bluetooth 1135