Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eff888fb authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Refactor LE dynamic psm assignment

Tag: #gd-refactor
Bug: 141555841
Test: cert/run --host
Test: CtsVerifier
Change-Id: If6d421a84b3978a564e5fb5da4bb97f0d440b1fe
parent 6a4a67bb
Loading
Loading
Loading
Loading
+26 −14
Original line number Diff line number Diff line
@@ -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) {