Loading system/main/shim/l2c_api.cc +2 −5 Original line number Diff line number Diff line Loading @@ -205,15 +205,12 @@ bool bluetooth::shim::L2CA_GetPeerLECocConfig(uint16_t lcid, */ bool bluetooth::shim::L2CA_SetConnectionCallbacks( uint16_t cid, const tL2CAP_APPL_INFO* callbacks) { return shim_l2cap.SetCallbacks(cid, callbacks); LOG_INFO(LOG_TAG, "Unsupported API %s", __func__); return false; } uint8_t bluetooth::shim::L2CA_DataWriteEx(uint16_t cid, BT_HDR* bt_hdr, uint16_t flags) { if (shim_l2cap.IsCongested(cid)) { return L2CAP_DW_CONGESTED; } bool write_success = false; switch (flags) { case L2CAP_FLUSHABLE_CH_BASED: Loading system/main/shim/l2cap.cc +14 −11 Original line number Diff line number Diff line Loading @@ -166,6 +166,19 @@ void bluetooth::legacy::shim::L2cap::RegisterService( LOG_DEBUG(LOG_TAG, "Successfully registered service on psm:%hd", psm); } void bluetooth::legacy::shim::L2cap::UnregisterService(uint16_t psm) { if (!Classic().IsPsmRegistered(psm)) { LOG_WARN(LOG_TAG, "Service must be registered in order to unregister psm:%hd", psm); return; } LOG_DEBUG(LOG_TAG, "Unregistering service on psm:%hd", psm); // TODO(cmanton) Check for open channels before unregistering bluetooth::shim::GetL2cap()->UnregisterService(psm); Classic().UnregisterPsm(psm); Classic().DeallocatePsm(psm); } uint16_t bluetooth::legacy::shim::L2cap::CreateConnection( uint16_t psm, const RawAddress& raw_address) { LOG_DEBUG(LOG_TAG, "Requesting connection to psm:%hd address:%s", psm, Loading Loading @@ -222,12 +235,6 @@ void bluetooth::legacy::shim::L2cap::OnConnectionReady( }); } bool bluetooth::legacy::shim::L2cap::IsCongested(uint16_t cid) const { CHECK(ConnectionExists(cid)); LOG_WARN(LOG_TAG, "Ignoring checks for congestion on cid:%hd", cid); return false; } bool bluetooth::legacy::shim::L2cap::Write(uint16_t cid, BT_HDR* bt_hdr) { CHECK(ConnectionExists(cid)); CHECK(bt_hdr != nullptr); Loading Loading @@ -283,11 +290,6 @@ bool bluetooth::legacy::shim::L2cap::SetCallbacks( return true; } void bluetooth::legacy::shim::L2cap::ClearCallbacks(uint16_t cid) { CHECK(ConnectionExists(cid)); cid_to_callback_map_.erase(cid); } bool bluetooth::legacy::shim::L2cap::ConnectResponse( const RawAddress& raw_address, uint8_t signal_id, uint16_t cid, uint16_t result, uint16_t status, tL2CAP_ERTM_INFO* ertm_info) { Loading Loading @@ -340,6 +342,7 @@ bool bluetooth::legacy::shim::L2cap::ConfigResponse( bool bluetooth::legacy::shim::L2cap::DisconnectRequest(uint16_t cid) { CHECK(ConnectionExists(cid)); bluetooth::shim::GetL2cap()->CloseConnection(cid); cid_to_callback_map_.erase(cid); return true; } Loading system/main/shim/l2cap.h +2 −4 Original line number Diff line number Diff line Loading @@ -52,6 +52,7 @@ class L2cap { public: void RegisterService(uint16_t psm, const tL2CAP_APPL_INFO* callbacks, bool enable_snoop); void UnregisterService(uint16_t psm); uint16_t CreateConnection(uint16_t psm, const RawAddress& raw_address); void OnConnectionReady( uint16_t psm, uint16_t cid, Loading @@ -60,10 +61,6 @@ class L2cap { bool Write(uint16_t cid, BT_HDR* bt_hdr); bool WriteFlushable(uint16_t cid, BT_HDR* bt_hdr); bool WriteNonFlushable(uint16_t cid, BT_HDR* bt_hdr); bool IsCongested(uint16_t cid) const; bool SetCallbacks(uint16_t cid, const tL2CAP_APPL_INFO* callbacks); void ClearCallbacks(uint16_t cid); uint16_t GetNextDynamicClassicPsm(); uint16_t GetNextDynamicLePsm(); Loading Loading @@ -92,6 +89,7 @@ class L2cap { private: uint16_t GetNextVirtualPsm(uint16_t real_psm); bool SetCallbacks(uint16_t cid, const tL2CAP_APPL_INFO* callbacks); PsmData classic_; PsmData le_; Loading system/main/shim/l2cap_test.cc +108 −10 Original line number Diff line number Diff line Loading @@ -178,34 +178,132 @@ tL2CAP_APPL_INFO test_callbacks{ .pL2CA_CreditsReceived_Cb = L2caCreditsReceivedCb, }; TEST_F(L2capTest, Base) { LOG_INFO(LOG_TAG, "Got test %p", &test_callbacks); } TEST_F(L2capTest, Callbacks) { bool rc = l2cap_->SetCallbacks(kPsm, &test_callbacks); CHECK(rc == true); TEST_F(L2capTest, RegisterService) { l2cap_->RegisterService(kPsm, &test_callbacks, false); CHECK(test_stack_.test_l2cap_.registered_service_.count(kPsm) == 1); } TEST_F(L2capTest, RegisterService) { l2cap_->RegisterService(123, &test_callbacks, false); TEST_F(L2capTest, UnregisterService) { l2cap_->RegisterService(kPsm, &test_callbacks, false); CHECK(test_stack_.test_l2cap_.registered_service_.count(kPsm) == 1); l2cap_->UnregisterService(kPsm); CHECK(test_stack_.test_l2cap_.registered_service_.count(kPsm) == 0); } TEST_F(L2capTest, CreateConnection_NotRegistered) { RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(123, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid == 0); } TEST_F(L2capTest, CreateConnection_Registered) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(123, &test_callbacks, false); l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); } TEST_F(L2capTest, CreateConnection_ConnectResponse) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); CHECK(l2cap_->ConnectResponse(raw_address, 0, cid, 0, 0, nullptr)); } TEST_F(L2capTest, CreateConnection_ConfigRequest) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->ConfigRequest(cid, nullptr)); } TEST_F(L2capTest, CreateConnection_ConfigResponse) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(123, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->ConfigResponse(cid, nullptr)); } TEST_F(L2capTest, CreateConnection_DisconnectRequest) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->DisconnectRequest(cid)); } TEST_F(L2capTest, CreateConnection_DisconnectResponse) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->DisconnectResponse(cid)); } TEST_F(L2capTest, CreateConnection_WithHandshake) { Loading system/main/shim/test_stack.cc +4 −1 Original line number Diff line number Diff line Loading @@ -32,9 +32,12 @@ void TestGdShimL2cap::RegisterService( uint16_t psm, bluetooth::shim::ConnectionOpenCallback on_open, std::promise<void> completed) { completed.set_value(); registered_service_.insert(psm); } void TestGdShimL2cap::UnregisterService(uint16_t psm) {} void TestGdShimL2cap::UnregisterService(uint16_t psm) { registered_service_.erase(psm); } void TestGdShimL2cap::CreateConnection(uint16_t psm, const std::string address, std::promise<uint16_t> completed) { Loading Loading
system/main/shim/l2c_api.cc +2 −5 Original line number Diff line number Diff line Loading @@ -205,15 +205,12 @@ bool bluetooth::shim::L2CA_GetPeerLECocConfig(uint16_t lcid, */ bool bluetooth::shim::L2CA_SetConnectionCallbacks( uint16_t cid, const tL2CAP_APPL_INFO* callbacks) { return shim_l2cap.SetCallbacks(cid, callbacks); LOG_INFO(LOG_TAG, "Unsupported API %s", __func__); return false; } uint8_t bluetooth::shim::L2CA_DataWriteEx(uint16_t cid, BT_HDR* bt_hdr, uint16_t flags) { if (shim_l2cap.IsCongested(cid)) { return L2CAP_DW_CONGESTED; } bool write_success = false; switch (flags) { case L2CAP_FLUSHABLE_CH_BASED: Loading
system/main/shim/l2cap.cc +14 −11 Original line number Diff line number Diff line Loading @@ -166,6 +166,19 @@ void bluetooth::legacy::shim::L2cap::RegisterService( LOG_DEBUG(LOG_TAG, "Successfully registered service on psm:%hd", psm); } void bluetooth::legacy::shim::L2cap::UnregisterService(uint16_t psm) { if (!Classic().IsPsmRegistered(psm)) { LOG_WARN(LOG_TAG, "Service must be registered in order to unregister psm:%hd", psm); return; } LOG_DEBUG(LOG_TAG, "Unregistering service on psm:%hd", psm); // TODO(cmanton) Check for open channels before unregistering bluetooth::shim::GetL2cap()->UnregisterService(psm); Classic().UnregisterPsm(psm); Classic().DeallocatePsm(psm); } uint16_t bluetooth::legacy::shim::L2cap::CreateConnection( uint16_t psm, const RawAddress& raw_address) { LOG_DEBUG(LOG_TAG, "Requesting connection to psm:%hd address:%s", psm, Loading Loading @@ -222,12 +235,6 @@ void bluetooth::legacy::shim::L2cap::OnConnectionReady( }); } bool bluetooth::legacy::shim::L2cap::IsCongested(uint16_t cid) const { CHECK(ConnectionExists(cid)); LOG_WARN(LOG_TAG, "Ignoring checks for congestion on cid:%hd", cid); return false; } bool bluetooth::legacy::shim::L2cap::Write(uint16_t cid, BT_HDR* bt_hdr) { CHECK(ConnectionExists(cid)); CHECK(bt_hdr != nullptr); Loading Loading @@ -283,11 +290,6 @@ bool bluetooth::legacy::shim::L2cap::SetCallbacks( return true; } void bluetooth::legacy::shim::L2cap::ClearCallbacks(uint16_t cid) { CHECK(ConnectionExists(cid)); cid_to_callback_map_.erase(cid); } bool bluetooth::legacy::shim::L2cap::ConnectResponse( const RawAddress& raw_address, uint8_t signal_id, uint16_t cid, uint16_t result, uint16_t status, tL2CAP_ERTM_INFO* ertm_info) { Loading Loading @@ -340,6 +342,7 @@ bool bluetooth::legacy::shim::L2cap::ConfigResponse( bool bluetooth::legacy::shim::L2cap::DisconnectRequest(uint16_t cid) { CHECK(ConnectionExists(cid)); bluetooth::shim::GetL2cap()->CloseConnection(cid); cid_to_callback_map_.erase(cid); return true; } Loading
system/main/shim/l2cap.h +2 −4 Original line number Diff line number Diff line Loading @@ -52,6 +52,7 @@ class L2cap { public: void RegisterService(uint16_t psm, const tL2CAP_APPL_INFO* callbacks, bool enable_snoop); void UnregisterService(uint16_t psm); uint16_t CreateConnection(uint16_t psm, const RawAddress& raw_address); void OnConnectionReady( uint16_t psm, uint16_t cid, Loading @@ -60,10 +61,6 @@ class L2cap { bool Write(uint16_t cid, BT_HDR* bt_hdr); bool WriteFlushable(uint16_t cid, BT_HDR* bt_hdr); bool WriteNonFlushable(uint16_t cid, BT_HDR* bt_hdr); bool IsCongested(uint16_t cid) const; bool SetCallbacks(uint16_t cid, const tL2CAP_APPL_INFO* callbacks); void ClearCallbacks(uint16_t cid); uint16_t GetNextDynamicClassicPsm(); uint16_t GetNextDynamicLePsm(); Loading Loading @@ -92,6 +89,7 @@ class L2cap { private: uint16_t GetNextVirtualPsm(uint16_t real_psm); bool SetCallbacks(uint16_t cid, const tL2CAP_APPL_INFO* callbacks); PsmData classic_; PsmData le_; Loading
system/main/shim/l2cap_test.cc +108 −10 Original line number Diff line number Diff line Loading @@ -178,34 +178,132 @@ tL2CAP_APPL_INFO test_callbacks{ .pL2CA_CreditsReceived_Cb = L2caCreditsReceivedCb, }; TEST_F(L2capTest, Base) { LOG_INFO(LOG_TAG, "Got test %p", &test_callbacks); } TEST_F(L2capTest, Callbacks) { bool rc = l2cap_->SetCallbacks(kPsm, &test_callbacks); CHECK(rc == true); TEST_F(L2capTest, RegisterService) { l2cap_->RegisterService(kPsm, &test_callbacks, false); CHECK(test_stack_.test_l2cap_.registered_service_.count(kPsm) == 1); } TEST_F(L2capTest, RegisterService) { l2cap_->RegisterService(123, &test_callbacks, false); TEST_F(L2capTest, UnregisterService) { l2cap_->RegisterService(kPsm, &test_callbacks, false); CHECK(test_stack_.test_l2cap_.registered_service_.count(kPsm) == 1); l2cap_->UnregisterService(kPsm); CHECK(test_stack_.test_l2cap_.registered_service_.count(kPsm) == 0); } TEST_F(L2capTest, CreateConnection_NotRegistered) { RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(123, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid == 0); } TEST_F(L2capTest, CreateConnection_Registered) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(123, &test_callbacks, false); l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); } TEST_F(L2capTest, CreateConnection_ConnectResponse) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); CHECK(l2cap_->ConnectResponse(raw_address, 0, cid, 0, 0, nullptr)); } TEST_F(L2capTest, CreateConnection_ConfigRequest) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->ConfigRequest(cid, nullptr)); } TEST_F(L2capTest, CreateConnection_ConfigResponse) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(123, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->ConfigResponse(cid, nullptr)); } TEST_F(L2capTest, CreateConnection_DisconnectRequest) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->DisconnectRequest(cid)); } TEST_F(L2capTest, CreateConnection_DisconnectResponse) { test_stack_.test_l2cap_.cid_ = kCid; l2cap_->RegisterService(kPsm, &test_callbacks, false); RawAddress raw_address; std::string string_address("11:22:33:44:55:66"); RawAddress::FromString(string_address, raw_address); uint16_t cid = l2cap_->CreateConnection(kPsm, raw_address); CHECK(cid != 0); { // Simulate a successful connection response l2cap_->OnConnectionReady(kPsm, kCid, [&cid](std::function<void(uint16_t)> func) { LOG_INFO(LOG_TAG, "In closure cid:%d", cid); func(cid); }); } CHECK(l2cap_->DisconnectResponse(cid)); } TEST_F(L2capTest, CreateConnection_WithHandshake) { Loading
system/main/shim/test_stack.cc +4 −1 Original line number Diff line number Diff line Loading @@ -32,9 +32,12 @@ void TestGdShimL2cap::RegisterService( uint16_t psm, bluetooth::shim::ConnectionOpenCallback on_open, std::promise<void> completed) { completed.set_value(); registered_service_.insert(psm); } void TestGdShimL2cap::UnregisterService(uint16_t psm) {} void TestGdShimL2cap::UnregisterService(uint16_t psm) { registered_service_.erase(psm); } void TestGdShimL2cap::CreateConnection(uint16_t psm, const std::string address, std::promise<uint16_t> completed) { Loading