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

Commit e5901bf4 authored by Omair Kamil's avatar Omair Kamil Committed by Gerrit Code Review
Browse files

Merge "Limit tGATT_IF range" into main

parents 92851f46 21f04ce7
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ gatt_interface_t default_gatt_interface = {
                [](tGATT_IF client_if, const RawAddress& remote_bda, bool is_direct) {
                  gatt_history_.Push(base::StringPrintf(
                          "%-32s bd_addr:%s client_if:%hu is_direct:%c", "GATTC_CancelOpen",
                          ADDRESS_TO_LOGGABLE_CSTR(remote_bda), client_if,
                          ADDRESS_TO_LOGGABLE_CSTR(remote_bda), static_cast<uint16_t>(client_if),
                          (is_direct) ? 'T' : 'F'));
                  BTA_GATTC_CancelOpen(client_if, remote_bda, is_direct);
                },
@@ -101,8 +101,8 @@ gatt_interface_t default_gatt_interface = {
                   tBTM_BLE_CONN_TYPE connection_type, bool opportunistic, uint16_t preferred_mtu) {
                  gatt_history_.Push(base::StringPrintf(
                          "%-32s bd_addr:%s client_if:%hu type:0x%x opportunistic:%c", "GATTC_Open",
                          ADDRESS_TO_LOGGABLE_CSTR(remote_bda), client_if, connection_type,
                          (opportunistic) ? 'T' : 'F'));
                          ADDRESS_TO_LOGGABLE_CSTR(remote_bda), static_cast<uint16_t>(client_if),
                          connection_type, (opportunistic) ? 'T' : 'F'));
                  BTA_GATTC_Open(client_if, remote_bda, BLE_ADDR_PUBLIC, connection_type,
                                 BT_TRANSPORT_LE, opportunistic, LE_PHY_1M, preferred_mtu);
                },
+0 −5
Original line number Diff line number Diff line
@@ -349,11 +349,6 @@
#define GATT_MAX_APPS 32 /* note: 2 apps used internally GATT and GAP */
#endif

/* connection manager doesn't generate it's own IDs. Instead, all GATT clients
 * use their gatt_if to identify against conection manager. When stack tries to
 * create l2cap connection, it will use this fixed ID. */
#define CONN_MGR_ID_L2CAP (GATT_MAX_APPS + 10)

/* This value is used for static allocation of resources. The actual maximum at
 * runtime is controlled by a system property. */
#ifndef GATT_MAX_PHY_CHANNEL
+32 −27
Original line number Diff line number Diff line
@@ -1267,6 +1267,22 @@ tGATT_IF GATT_Register(const Uuid& app_uuid128, const std::string& name, tGATT_C
  return 0;
}

static tGATT_IF GATT_FindNextFreeClRcbId() {
  tGATT_IF gatt_if = gatt_cb.last_gatt_if;
  for (int i = 0; i < GATT_IF_MAX; i++) {
    if (++gatt_if > GATT_IF_MAX) {
      gatt_if = static_cast<tGATT_IF>(1);
    }
    if (!gatt_cb.cl_rcb_map.contains(gatt_if)) {
      gatt_cb.last_gatt_if = gatt_if;
      return gatt_if;
    }
  }
  log::error("Unable to register GATT client, MAX client reached: {}", gatt_cb.cl_rcb_map.size());

  return GATT_IF_INVALID;
}

static tGATT_IF GATT_Register_Dynamic(const Uuid& app_uuid128, const std::string& name,
                                      tGATT_CBACK* p_cb_info, bool eatt_support) {
  for (auto& [gatt_if, p_reg] : gatt_cb.cl_rcb_map) {
@@ -1281,39 +1297,28 @@ static tGATT_IF GATT_Register_Dynamic(const Uuid& app_uuid128, const std::string
    eatt_support = true;
  }

  if (gatt_cb.cl_rcb_map.size() >= GATT_CL_RCB_MAX) {
  if (gatt_cb.cl_rcb_map.size() >= GATT_IF_MAX) {
    log::error("Unable to register GATT client, MAX client reached: {}", gatt_cb.cl_rcb_map.size());
    return 0;
  }

  uint8_t i_gatt_if = gatt_cb.next_gatt_if;
  for (int i = 0; i < GATT_CL_RCB_MAX; i++) {
    if (gatt_cb.cl_rcb_map.find(static_cast<tGATT_IF>(i_gatt_if)) == gatt_cb.cl_rcb_map.end()) {
      gatt_cb.cl_rcb_map.emplace(i_gatt_if, std::make_unique<tGATT_REG>());
      tGATT_REG* p_reg = gatt_cb.cl_rcb_map[i_gatt_if].get();
  tGATT_IF gatt_if = GATT_FindNextFreeClRcbId();
  if (gatt_if == GATT_IF_INVALID) {
    return gatt_if;
  }

  auto [it, ret] = gatt_cb.cl_rcb_map.emplace(gatt_if, std::make_unique<tGATT_REG>());
  tGATT_REG* p_reg = it->second.get();
  p_reg->app_uuid128 = app_uuid128;
      p_reg->gatt_if = (tGATT_IF)i_gatt_if;
  p_reg->gatt_if = gatt_if;
  p_reg->app_cb = *p_cb_info;
  p_reg->in_use = true;
  p_reg->eatt_support = eatt_support;
  p_reg->name = name;
      log::info("Allocated name:{} uuid:{} gatt_if:{} eatt_support:{}", name,
                app_uuid128.ToString(), p_reg->gatt_if, eatt_support);
  log::info("Allocated name:{} uuid:{} gatt_if:{} eatt_support:{}", name, app_uuid128.ToString(),
            p_reg->gatt_if, eatt_support);

      gatt_cb.next_gatt_if = (tGATT_IF)(i_gatt_if + 1);
      if (gatt_cb.next_gatt_if == 0) {
        gatt_cb.next_gatt_if = 1;
      }
      return p_reg->gatt_if;
    }
    i_gatt_if++;
    if (i_gatt_if == 0) {
      i_gatt_if = 1;
    }
  }

  log::error("Unable to register GATT client, MAX client reached: {}", gatt_cb.cl_rcb_map.size());
  return 0;
  return gatt_if;
}

/*******************************************************************************
+1 −1
Original line number Diff line number Diff line
@@ -415,7 +415,7 @@ typedef struct {
  fixed_queue_t* srv_chg_clt_q; /* service change clients queue */
  tGATT_REG cl_rcb[GATT_MAX_APPS];

  tGATT_IF next_gatt_if; /* potential next gatt if, should be greater than 0 */
  tGATT_IF last_gatt_if; /* last used gatt_if, used to find the next gatt_if easily */
  std::unordered_map<tGATT_IF, std::unique_ptr<tGATT_REG>> cl_rcb_map;

  /* list of connection link control blocks.
+2 −2
Original line number Diff line number Diff line
@@ -118,8 +118,8 @@ void gatt_init(void) {
  connection_manager::reset(true);
  memset(&fixed_reg, 0, sizeof(tL2CAP_FIXED_CHNL_REG));

  // To catch a potential OOB.
  gatt_cb.next_gatt_if = 40;
  // To catch a potential OOB, 40>31 is used, any valid value (1 to GATT_IF_MAX) is okay.
  gatt_cb.last_gatt_if = static_cast<tGATT_IF>(40);

  gatt_cb.sign_op_queue = fixed_queue_new(SIZE_MAX);
  gatt_cb.srv_chg_clt_q = fixed_queue_new(SIZE_MAX);
Loading