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

Commit 1b4c9972 authored by Chris Manton's avatar Chris Manton
Browse files

stack::sdp: Define static functions before use

Bug: 339523931
Test: m .
Flag: EXEMPT, Whitespace Change

Change-Id: I2e14b3e60bd092b86da7a5cc3292f3753c7639e0
parent 5f5618c1
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -45,10 +45,6 @@
using bluetooth::Uuid;
using namespace bluetooth;

/**********************************************************************
 *   C L I E N T    F U N C T I O N    P R O T O T Y P E S            *
 **********************************************************************/

/*******************************************************************************
 *
 * Function         SDP_InitDiscoveryDb
+128 −140
Original line number Diff line number Diff line
@@ -41,15 +41,45 @@

using namespace bluetooth;

/******************************************************************************/
/*            L O C A L    F U N C T I O N     P R O T O T Y P E S            */
/******************************************************************************/
/*******************************************************************************
 *
 * Function         find_uuid_in_seq
 *
 * Description      This function searches a data element sequenct for a UUID.
 *
 * Returns          true if found, else false
 *
 ******************************************************************************/
static bool find_uuid_in_seq(uint8_t* p, uint32_t seq_len,
                             const uint8_t* p_his_uuid, uint16_t his_len,
                             int nest_level);
                             const uint8_t* p_uuid, uint16_t uuid_len,
                             int nest_level) {
  uint8_t* p_end = p + seq_len;
  uint8_t type;
  uint32_t len;

bool SDP_AddAttribute(uint32_t handle, uint16_t attr_id, uint8_t attr_type,
                      uint32_t attr_len, uint8_t* p_val);
  /* A little safety check to avoid excessive recursion */
  if (nest_level > 3) return (false);

  while (p < p_end) {
    type = *p++;
    p = sdpu_get_len_from_type(p, p_end, type, &len);
    if (p == NULL || (p + len) > p_end) {
      log::warn("bad length");
      break;
    }
    type = type >> 3;
    if (type == UUID_DESC_TYPE) {
      if (sdpu_compare_uuid_arrays(p, len, p_uuid, uuid_len)) return (true);
    } else if (type == DATA_ELE_SEQ_DESC_TYPE) {
      if (find_uuid_in_seq(p, len, p_uuid, uuid_len, nest_level + 1))
        return (true);
    }
    p = p + len;
  }

  /* If here, failed to match */
  return (false);
}

/*******************************************************************************
 *
@@ -105,46 +135,6 @@ const tSDP_RECORD* sdp_db_service_search(const tSDP_RECORD* p_rec,
  return (NULL);
}

/*******************************************************************************
 *
 * Function         find_uuid_in_seq
 *
 * Description      This function searches a data element sequenct for a UUID.
 *
 * Returns          true if found, else false
 *
 ******************************************************************************/
static bool find_uuid_in_seq(uint8_t* p, uint32_t seq_len,
                             const uint8_t* p_uuid, uint16_t uuid_len,
                             int nest_level) {
  uint8_t* p_end = p + seq_len;
  uint8_t type;
  uint32_t len;

  /* A little safety check to avoid excessive recursion */
  if (nest_level > 3) return (false);

  while (p < p_end) {
    type = *p++;
    p = sdpu_get_len_from_type(p, p_end, type, &len);
    if (p == NULL || (p + len) > p_end) {
      log::warn("bad length");
      break;
    }
    type = type >> 3;
    if (type == UUID_DESC_TYPE) {
      if (sdpu_compare_uuid_arrays(p, len, p_uuid, uuid_len)) return (true);
    } else if (type == DATA_ELE_SEQ_DESC_TYPE) {
      if (find_uuid_in_seq(p, len, p_uuid, uuid_len, nest_level + 1))
        return (true);
    }
    p = p + len;
  }

  /* If here, failed to match */
  return (false);
}

/*******************************************************************************
 *
 * Function         sdp_db_find_record
@@ -247,6 +237,97 @@ static int sdp_compose_proto_list(uint8_t* p, uint16_t num_elem,
  return (p - p_head);
}

/*******************************************************************************
 *
 * Function         SDP_AddAttribute
 *
 * Description      This function is called to add an attribute to a record.
 *                  This would be through the SDP database maintenance API.
 *                  If the attribute already exists in the record, it is
 *                  replaced with the new value.
 *
 * NOTE             Attribute values must be passed as a Big Endian stream.
 *
 * Returns          true if added OK, else false
 *
 ******************************************************************************/
bool SDP_AddAttribute(uint32_t handle, uint16_t attr_id, uint8_t attr_type,
                      uint32_t attr_len, uint8_t* p_val) {
  uint16_t zz;
  tSDP_RECORD* p_rec = &sdp_cb.server_db.record[0];

  if (p_val == nullptr) {
    log::warn("Trying to add attribute with p_val == nullptr, skipped");
    return (false);
  }

  // TODO(305066880): invoke would_log when implemented to check
  // if LOG_VERBOSE is displayed.
  if (true) {
    if ((attr_type == UINT_DESC_TYPE) ||
        (attr_type == TWO_COMP_INT_DESC_TYPE) ||
        (attr_type == UUID_DESC_TYPE) ||
        (attr_type == DATA_ELE_SEQ_DESC_TYPE) ||
        (attr_type == DATA_ELE_ALT_DESC_TYPE)) {
#define MAX_ARR_LEN 200
      // one extra byte for storing terminating zero byte
      char num_array[2 * MAX_ARR_LEN + 1] = {0};
      uint32_t len = (attr_len > MAX_ARR_LEN) ? MAX_ARR_LEN : attr_len;
#undef MAX_ARR_LEN

      for (uint32_t i = 0; i < len; i++) {
        snprintf(&num_array[i * 2], sizeof(num_array) - i * 2, "%02X",
                 (uint8_t)(p_val[i]));
      }
      log::verbose(
          "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
          "p_val:{}, *p_val:{}",
          handle, attr_id, attr_type, attr_len, fmt::ptr(p_val), num_array);
    } else if (attr_type == BOOLEAN_DESC_TYPE) {
      log::verbose(
          "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
          "p_val:{}, *p_val:{}",
          handle, attr_id, attr_type, attr_len, fmt::ptr(p_val), *p_val);
    } else if ((attr_type == TEXT_STR_DESC_TYPE) ||
               (attr_type == URL_DESC_TYPE)) {
      if (p_val[attr_len - 1] == '\0') {
        log::verbose(
            "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
            "p_val:{}, *p_val:{}",
            handle, attr_id, attr_type, attr_len, fmt::ptr(p_val),
            (char*)p_val);
      } else {
        log::verbose(
            "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
            "p_val:{}",
            handle, attr_id, attr_type, attr_len, fmt::ptr(p_val));
      }
    } else {
      log::verbose(
          "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, p_val:{}",
          handle, attr_id, attr_type, attr_len, fmt::ptr(p_val));
    }
  }

  /* Find the record in the database */
  for (zz = 0; zz < sdp_cb.server_db.num_records; zz++, p_rec++) {
    if (p_rec->record_handle == handle) {
      // error out early, no need to look up
      if (p_rec->free_pad_ptr >= SDP_MAX_PAD_LEN) {
        log::error(
            "the free pad for SDP record with handle {} is full, skip adding "
            "the attribute",
            handle);
        return (false);
      }

      return SDP_AddAttributeToRecord(p_rec, attr_id, attr_type, attr_len,
                                      p_val);
    }
  }
  return (false);
}

/*******************************************************************************
 *
 * Function         SDP_CreateRecord
@@ -346,99 +427,6 @@ bool SDP_DeleteRecord(uint32_t handle) {
  return (false);
}

/*******************************************************************************
 *
 * Function         SDP_AddAttribute
 *
 * Description      This function is called to add an attribute to a record.
 *                  This would be through the SDP database maintenance API.
 *                  If the attribute already exists in the record, it is
 *                  replaced with the new value.
 *
 * NOTE             Attribute values must be passed as a Big Endian stream.
 *
 * Returns          true if added OK, else false
 *
 ******************************************************************************/
bool SDP_AddAttribute(uint32_t handle, uint16_t attr_id, uint8_t attr_type,
                      uint32_t attr_len, uint8_t* p_val) {
  uint16_t zz;
  tSDP_RECORD* p_rec = &sdp_cb.server_db.record[0];

  if (p_val == nullptr) {
    log::warn("Trying to add attribute with p_val == nullptr, skipped");
    return (false);
  }

  // TODO(305066880): invoke would_log when implemented to check
  // if LOG_VERBOSE is displayed.
  if (true) {
    if ((attr_type == UINT_DESC_TYPE) ||
        (attr_type == TWO_COMP_INT_DESC_TYPE) ||
        (attr_type == UUID_DESC_TYPE) ||
        (attr_type == DATA_ELE_SEQ_DESC_TYPE) ||
        (attr_type == DATA_ELE_ALT_DESC_TYPE)) {

      #define MAX_ARR_LEN 200
      // one extra byte for storing terminating zero byte
      char num_array[2 * MAX_ARR_LEN + 1] = {0};
      uint32_t len = (attr_len > MAX_ARR_LEN) ? MAX_ARR_LEN : attr_len;
      #undef MAX_ARR_LEN

      for (uint32_t i = 0; i < len; i++) {
        snprintf(&num_array[i * 2], sizeof(num_array) - i * 2, "%02X",
                 (uint8_t)(p_val[i]));
      }
      log::verbose(
          "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
          "p_val:{}, *p_val:{}",
          handle, attr_id, attr_type, attr_len, fmt::ptr(p_val), num_array);
    } else if (attr_type == BOOLEAN_DESC_TYPE) {
      log::verbose(
          "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
          "p_val:{}, *p_val:{}",
          handle, attr_id, attr_type, attr_len, fmt::ptr(p_val), *p_val);
    } else if ((attr_type == TEXT_STR_DESC_TYPE) ||
               (attr_type == URL_DESC_TYPE)) {
      if (p_val[attr_len - 1] == '\0') {
        log::verbose(
            "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
            "p_val:{}, *p_val:{}",
            handle, attr_id, attr_type, attr_len, fmt::ptr(p_val),
            (char*)p_val);
      } else {
        log::verbose(
            "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, "
            "p_val:{}",
            handle, attr_id, attr_type, attr_len, fmt::ptr(p_val));
      }
    } else {
      log::verbose(
          "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, p_val:{}",
          handle, attr_id, attr_type, attr_len, fmt::ptr(p_val));
    }
  }

  /* Find the record in the database */
  for (zz = 0; zz < sdp_cb.server_db.num_records; zz++, p_rec++) {
    if (p_rec->record_handle == handle) {

      // error out early, no need to look up
      if (p_rec->free_pad_ptr >= SDP_MAX_PAD_LEN) {
        log::error(
            "the free pad for SDP record with handle {} is full, skip adding "
            "the attribute",
            handle);
        return (false);
      }

      return SDP_AddAttributeToRecord(p_rec, attr_id, attr_type, attr_len,
                                      p_val);
    }
  }
  return (false);
}

/*******************************************************************************
 *
 * Function         SDP_AddAttributeToRecord
+516 −533

File changed.

Preview size limit exceeded, changes collapsed.

+49 −64
Original line number Diff line number Diff line
@@ -45,70 +45,6 @@ using namespace bluetooth;
/******************************************************************************/
tSDP_CB sdp_cb;

/******************************************************************************/
/*            L O C A L    F U N C T I O N     P R O T O T Y P E S            */
/******************************************************************************/
static void sdp_connect_ind(const RawAddress& bd_addr, uint16_t l2cap_cid,
                            uint16_t psm, uint8_t l2cap_id);
static void sdp_config_ind(uint16_t l2cap_cid, tL2CAP_CFG_INFO* p_cfg);
static void sdp_config_cfm(uint16_t l2cap_cid, uint16_t result,
                           tL2CAP_CFG_INFO* p_cfg);
static void sdp_disconnect_ind(uint16_t l2cap_cid, bool ack_needed);
static void sdp_data_ind(uint16_t l2cap_cid, BT_HDR* p_msg);

static void sdp_connect_cfm(uint16_t l2cap_cid, uint16_t result);
static void sdp_disconnect_cfm(uint16_t l2cap_cid, uint16_t result);
static void sdp_on_l2cap_error(uint16_t l2cap_cid, uint16_t result);

/*******************************************************************************
 *
 * Function         sdp_init
 *
 * Description      This function initializes the SDP unit.
 *
 * Returns          void
 *
 ******************************************************************************/
void sdp_init(void) {
  /* Clears all structures and local SDP database (if Server is enabled) */
  sdp_cb = {};

  for (int i = 0; i < SDP_MAX_CONNECTIONS; i++) {
    sdp_cb.ccb[i].sdp_conn_timer = alarm_new("sdp.sdp_conn_timer");
  }

  /* Initialize the L2CAP configuration. We only care about MTU */
  sdp_cb.l2cap_my_cfg.mtu_present = true;
  sdp_cb.l2cap_my_cfg.mtu = SDP_MTU_SIZE;

  sdp_cb.max_attr_list_size = SDP_MTU_SIZE - 16;
  sdp_cb.max_recs_per_search = SDP_MAX_DISC_SERVER_RECS;

  sdp_cb.reg_info.pL2CA_ConnectInd_Cb = sdp_connect_ind;
  sdp_cb.reg_info.pL2CA_ConnectCfm_Cb = sdp_connect_cfm;
  sdp_cb.reg_info.pL2CA_ConfigInd_Cb = sdp_config_ind;
  sdp_cb.reg_info.pL2CA_ConfigCfm_Cb = sdp_config_cfm;
  sdp_cb.reg_info.pL2CA_DisconnectInd_Cb = sdp_disconnect_ind;
  sdp_cb.reg_info.pL2CA_DisconnectCfm_Cb = sdp_disconnect_cfm;
  sdp_cb.reg_info.pL2CA_DataInd_Cb = sdp_data_ind;
  sdp_cb.reg_info.pL2CA_Error_Cb = sdp_on_l2cap_error;

  /* Now, register with L2CAP */
  if (!L2CA_Register2(BT_PSM_SDP, sdp_cb.reg_info, true /* enable_snoop */,
                      nullptr, SDP_MTU_SIZE, 0, BTM_SEC_NONE)) {
    log::error("SDP Registration failed");
  }
}

void sdp_free(void) {
  L2CA_Deregister(BT_PSM_SDP);
  for (int i = 0; i < SDP_MAX_CONNECTIONS; i++) {
    alarm_free(sdp_cb.ccb[i].sdp_conn_timer);
    sdp_cb.ccb[i].sdp_conn_timer = NULL;
  }
  sdp_cb = {};
}

/*******************************************************************************
 *
 * Function         sdp_connect_ind
@@ -449,3 +385,52 @@ void sdp_conn_timer_timeout(void* data) {
  sdpu_clear_pend_ccb(ccb);
  sdpu_release_ccb(ccb);
}

/*******************************************************************************
 *
 * Function         sdp_init
 *
 * Description      This function initializes the SDP unit.
 *
 * Returns          void
 *
 ******************************************************************************/
void sdp_init(void) {
  /* Clears all structures and local SDP database (if Server is enabled) */
  sdp_cb = {};

  for (int i = 0; i < SDP_MAX_CONNECTIONS; i++) {
    sdp_cb.ccb[i].sdp_conn_timer = alarm_new("sdp.sdp_conn_timer");
  }

  /* Initialize the L2CAP configuration. We only care about MTU */
  sdp_cb.l2cap_my_cfg.mtu_present = true;
  sdp_cb.l2cap_my_cfg.mtu = SDP_MTU_SIZE;

  sdp_cb.max_attr_list_size = SDP_MTU_SIZE - 16;
  sdp_cb.max_recs_per_search = SDP_MAX_DISC_SERVER_RECS;

  sdp_cb.reg_info.pL2CA_ConnectInd_Cb = sdp_connect_ind;
  sdp_cb.reg_info.pL2CA_ConnectCfm_Cb = sdp_connect_cfm;
  sdp_cb.reg_info.pL2CA_ConfigInd_Cb = sdp_config_ind;
  sdp_cb.reg_info.pL2CA_ConfigCfm_Cb = sdp_config_cfm;
  sdp_cb.reg_info.pL2CA_DisconnectInd_Cb = sdp_disconnect_ind;
  sdp_cb.reg_info.pL2CA_DisconnectCfm_Cb = sdp_disconnect_cfm;
  sdp_cb.reg_info.pL2CA_DataInd_Cb = sdp_data_ind;
  sdp_cb.reg_info.pL2CA_Error_Cb = sdp_on_l2cap_error;

  /* Now, register with L2CAP */
  if (!L2CA_Register2(BT_PSM_SDP, sdp_cb.reg_info, true /* enable_snoop */,
                      nullptr, SDP_MTU_SIZE, 0, BTM_SEC_NONE)) {
    log::error("SDP Registration failed");
  }
}

void sdp_free(void) {
  L2CA_Deregister(BT_PSM_SDP);
  for (int i = 0; i < SDP_MAX_CONNECTIONS; i++) {
    alarm_free(sdp_cb.ccb[i].sdp_conn_timer);
    sdp_cb.ccb[i].sdp_conn_timer = NULL;
  }
  sdp_cb = {};
}
+300 −328

File changed.

Preview size limit exceeded, changes collapsed.

Loading