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

Commit 7a59018e authored by Pomai Ahlo's avatar Pomai Ahlo Committed by Automerger Merge Worker
Browse files

Merge "Refactor PORT_MAX_RFC_PORTS" into main am: ee4d56d3 am: c0c0dcda...

Merge "Refactor PORT_MAX_RFC_PORTS" into main am: ee4d56d3 am: c0c0dcda am: a5767b1a am: 75b3f87a

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2741574



Change-Id: I011b348f089338360ec63355d86fd1540c80ccd5
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 95fdbfd7 75b3f87a
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -72,8 +72,6 @@ typedef uint8_t tBTA_JV_L2CAP_REASON;
#define BTA_JV_MAX_L2C_CONN                                                    \
  GAP_MAX_CONNECTIONS /* GAP handle is used as index, hence do not change this \
                         value */
#define BTA_JV_MAX_SCN \
  PORT_MAX_RFC_PORTS /* same as BTM_MAX_SCN (in btm_int.h) */
#define BTA_JV_MAX_RFC_CONN MAX_RFC_PORTS

#ifndef BTA_JV_DEF_RFC_MTU
+2 −1
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
#include "stack/include/gap_api.h"
#include "stack/include/l2cdefs.h"
#include "stack/include/port_api.h"
#include "stack/include/rfcdefs.h"
#include "stack/include/sdp_api.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"
@@ -769,7 +770,7 @@ void bta_jv_free_scn(int32_t type /* One of BTA_JV_CONN_TYPE_ */,
                     uint16_t scn) {
  switch (type) {
    case BTA_JV_CONN_TYPE_RFCOMM: {
      if (scn > 0 && scn <= BTA_JV_MAX_SCN && bta_jv_cb.scn[scn - 1]) {
      if (scn > 0 && scn <= RFCOMM_MAX_SCN && bta_jv_cb.scn[scn - 1]) {
        /* this scn is used by JV */
        bta_jv_cb.scn[scn - 1] = false;
        BTM_FreeSCN(scn);
+1 −1
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ typedef struct {
  tBTA_JV_PCB port_cb[MAX_RFC_PORTS];          /* index of this array is
                                                  the port_handle, */
  uint8_t sec_id[BTA_JV_NUM_SERVICE_ID];       /* service ID */
  bool scn[BTA_JV_MAX_SCN];                    /* SCN allocated by java */
  bool scn[RFCOMM_MAX_SCN];                    /* SCN allocated by java */
  uint16_t free_psm_list[BTA_JV_MAX_L2C_CONN]; /* PSMs freed by java
                                                (can be reused) */
  uint8_t sdp_active;                          /* see BTA_JV_SDP_ACT_* */
+2 −3
Original line number Diff line number Diff line
@@ -35,11 +35,10 @@
#include "stack/btm/security_device_record.h"
#include "stack/include/bt_octets.h"
#include "stack/include/btm_ble_api_types.h"
#include "stack/include/rfcdefs.h"
#include "stack/include/security_client_callbacks.h"
#include "types/raw_address.h"

#define BTM_MAX_SCN_ 31  // PORT_MAX_RFC_PORTS packages/modules/Bluetooth/system/stack/include/rfcdefs.h

constexpr size_t kMaxLogSize = 255;
constexpr size_t kBtmLogHistoryBufferSize = 200;
constexpr size_t kMaxInquiryScanHistory = 10;
@@ -396,7 +395,7 @@ typedef struct tBTM_CB {
  friend uint8_t BTM_AllocateSCN(void);
  friend bool BTM_TryAllocateSCN(uint8_t scn);
  friend bool BTM_FreeSCN(uint8_t scn);
  uint8_t btm_scn[BTM_MAX_SCN_];
  uint8_t btm_scn[RFCOMM_MAX_SCN];
  uint8_t btm_available_index;

  // give access to private method for test:
+25 −23
Original line number Diff line number Diff line
@@ -34,29 +34,28 @@ extern tBTM_CB btm_cb;
uint8_t BTM_AllocateSCN(void) {
  BTM_TRACE_DEBUG("BTM_AllocateSCN");

  // stack reserves scn 1 for HFP, HSP we still do the correct way.
  // SCN can be allocated in the range of [1, PORT_MAX_RFC_PORTS). Since (x + 1)
  // is returned, we iterate to less than PORT_MAX_RFC_PORTS - 1.
  for (uint8_t x = btm_cb.btm_available_index; x < PORT_MAX_RFC_PORTS - 1;
       x++) {
    if (!btm_cb.btm_scn[x]) {
      btm_cb.btm_scn[x] = true;
      btm_cb.btm_available_index = (x + 1);
      return (x + 1);
  // SCN can be allocated in the range of [1, RFCOMM_MAX_SCN]
  // btm_scn uses indexes 0 to RFCOMM_MAX_SCN-1 to track RFC ports
  for (uint8_t i = btm_cb.btm_available_index; i < RFCOMM_MAX_SCN; ++i) {
    if (!btm_cb.btm_scn[i]) {
      btm_cb.btm_scn[i] = true;
      btm_cb.btm_available_index = (i + 1);
      return (i + 1);  // allocated scn is index + 1
    }
  }

  // In order to avoid OOB, btm_available_index must be less than
  // PORT_MAX_RFC_PORTS.
  // In order to avoid OOB, btm_available_index must be no more than
  // RFCOMM_MAX_SCN.
  btm_cb.btm_available_index =
      std::min(btm_cb.btm_available_index, (uint8_t)(PORT_MAX_RFC_PORTS - 1));
      std::min(btm_cb.btm_available_index, (uint8_t)(RFCOMM_MAX_SCN));

  // Start from index 1 because index 0 (scn 1) is reserved for HFP
  // If there's no empty SCN from _last_index to BTM_MAX_SCN.
  for (uint8_t y = 1; y < btm_cb.btm_available_index; y++) {
    if (!btm_cb.btm_scn[y]) {
      btm_cb.btm_scn[y] = true;
      btm_cb.btm_available_index = (y + 1);
      return (y + 1);
  for (uint8_t i = 1; i < btm_cb.btm_available_index; ++i) {
    if (!btm_cb.btm_scn[i]) {
      btm_cb.btm_scn[i] = true;
      btm_cb.btm_available_index = (i + 1);
      return (i + 1);  // allocated scn is index + 1
    }
  }

@@ -74,18 +73,18 @@ uint8_t BTM_AllocateSCN(void) {
 ******************************************************************************/

bool BTM_TryAllocateSCN(uint8_t scn) {
  /* Make sure we don't exceed max port range.
   * Stack reserves scn 1 for HFP, HSP we still do the correct way.
  /* Make sure we don't exceed max scn range.
   * Stack reserves scn 1 for HFP and HSP
   */
  if ((scn >= PORT_MAX_RFC_PORTS) || (scn == 1) || (scn == 0)) return false;
  if ((scn > RFCOMM_MAX_SCN) || (scn == 1) || (scn == 0)) return false;

  /* check if this port is available */
  /* check if this scn is available */
  if (!btm_cb.btm_scn[scn - 1]) {
    btm_cb.btm_scn[scn - 1] = true;
    return true;
  }

  return (false); /* Port was busy */
  return (false); /* scn was busy */
}

/*******************************************************************************
@@ -99,7 +98,10 @@ bool BTM_TryAllocateSCN(uint8_t scn) {
 ******************************************************************************/
bool BTM_FreeSCN(uint8_t scn) {
  BTM_TRACE_DEBUG("BTM_FreeSCN ");
  if (scn <= PORT_MAX_RFC_PORTS && scn > 0) {
  /* Since this isn't used by HFP, this function will only free valid SCNs
   * that aren't reserved for HFP, which is range [2, RFCOMM_MAX_SCN].
   */
  if (scn < RFCOMM_MAX_SCN && scn > 1) {
    btm_cb.btm_scn[scn - 1] = false;
    return (true);
  } else {
Loading