OSDN Git Service

Refactor LE dynamic psm assignment
authorHansong Zhang <hsz@google.com>
Tue, 24 Nov 2020 21:14:57 +0000 (13:14 -0800)
committerHansong Zhang <hsz@google.com>
Tue, 1 Dec 2020 20:58:05 +0000 (12:58 -0800)
Tag: #gd-refactor
Bug: 141555841
Test: cert/run --host
Test: CtsVerifier
Change-Id: If6d421a84b3978a564e5fb5da4bb97f0d440b1fe

main/shim/l2c_api.cc

index b3bc4b0..2827a90 100644 (file)
@@ -16,6 +16,9 @@
 
 #define LOG_TAG "bt_shim_l2cap"
 
+#include <unordered_map>
+#include <unordered_set>
+
 #include "main/shim/l2c_api.h"
 #include "bta/include/bta_dm_acl.h"
 #include "gd/l2cap/classic/l2cap_classic_module.h"
@@ -37,8 +40,6 @@
 using bluetooth::hci::AddressWithType;
 using namespace bluetooth::l2cap;
 
-static bluetooth::shim::legacy::L2cap shim_l2cap;
-
 // Classic Dynamic Channel Shim Helper
 
 uint16_t classic_cid_token_counter_ = 0x41;
@@ -577,18 +578,6 @@ void bluetooth::shim::L2CA_Deregister(uint16_t psm) {
   }
 }
 
-uint16_t bluetooth::shim::L2CA_AllocateLePSM(void) {
-  return shim_l2cap.GetNextDynamicLePsm();
-}
-
-void bluetooth::shim::L2CA_FreeLePSM(uint16_t psm) {
-  if (!shim_l2cap.Le().IsPsmRegistered(psm)) {
-    LOG_ERROR("%s Not previously registered le psm:%hd", __func__, psm);
-    return;
-  }
-  shim_l2cap.Le().UnregisterPsm(psm);
-}
-
 /**
  * Classic Connection Oriented Channel APIS
  */
@@ -1136,6 +1125,29 @@ std::unordered_map<uint16_t, std::unique_ptr<LeDynamicChannelHelper>>
 /**
  * Le Connection Oriented Channel APIs
  */
+
+static std::unordered_set<uint16_t> assigned_dynamic_le_psm_;
+static uint16_t next_assigned_dynamic_le_psm_ = 0x80;
+
+uint16_t bluetooth::shim::L2CA_AllocateLePSM() {
+  if (le_dynamic_channel_helper_map_.size() > 100) {
+    LOG_ERROR("Why do we need more than 100 dynamic channel PSMs?");
+    return 0;
+  }
+  while (le_dynamic_channel_helper_map_.count(next_assigned_dynamic_le_psm_)) {
+    next_assigned_dynamic_le_psm_++;
+    if (next_assigned_dynamic_le_psm_ > 0xff) {
+      next_assigned_dynamic_le_psm_ = 0x80;
+    }
+  }
+  assigned_dynamic_le_psm_.emplace(next_assigned_dynamic_le_psm_);
+  return next_assigned_dynamic_le_psm_;
+}
+
+void bluetooth::shim::L2CA_FreeLePSM(uint16_t psm) {
+  assigned_dynamic_le_psm_.erase(psm);
+}
+
 uint16_t bluetooth::shim::L2CA_RegisterLECoc(uint16_t psm,
                                              const tL2CAP_APPL_INFO& callbacks,
                                              uint16_t sec_level) {