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

Commit 5b5d86fc authored by Chris Manton's avatar Chris Manton
Browse files

stack::rfcomm::api Extract get_port_from_handle

Bug: 343301203
Test: m .
Flag: EXEMPT, Mechanical Refactor

Change-Id: I4867df792f9c52f4b815bd654d18e9e789ee1f66
parent a5da10a2
Loading
Loading
Loading
Loading
+50 −55
Original line number Diff line number Diff line
@@ -283,6 +283,14 @@ int RFCOMM_ControlReqFromBTSOCK(uint8_t dlci, const RawAddress& bd_addr,
  return PORT_SUCCESS;
}

static tPORT* get_port_from_handle(uint16_t handle) {
  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
    return nullptr;
  }
  return &rfc_cb.port.port[handle - 1];
}

/*******************************************************************************
 *
 * Function         RFCOMM_RemoveConnection
@@ -295,12 +303,11 @@ int RFCOMM_ControlReqFromBTSOCK(uint8_t dlci, const RawAddress& bd_addr,
int RFCOMM_RemoveConnection(uint16_t handle) {
  log::verbose("RFCOMM_RemoveConnection() handle:{}", handle);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
    log::error("RFCOMM_RemoveConnection() BAD handle:{}", handle);
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    log::verbose("RFCOMM_RemoveConnection() Not opened:{}", handle);
@@ -332,12 +339,11 @@ int RFCOMM_RemoveConnection(uint16_t handle) {
 *
 ******************************************************************************/
int RFCOMM_RemoveServer(uint16_t handle) {
  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
    log::error("bad handle {}", handle);
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  /* Do not report any events to the client any more. */
  p_port->p_mgmt_callback = nullptr;
@@ -381,13 +387,12 @@ int RFCOMM_RemoveServer(uint16_t handle) {
 *
 ******************************************************************************/
int PORT_SetEventCallback(uint16_t handle, tPORT_CALLBACK* p_port_cb) {
  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    return (PORT_NOT_OPENED);
  }
@@ -409,12 +414,11 @@ int PORT_SetEventCallback(uint16_t handle, tPORT_CALLBACK* p_port_cb) {
 ******************************************************************************/

int PORT_ClearKeepHandleFlag(uint16_t handle) {
  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];
  p_port->keep_port_handle = 0;
  return (PORT_SUCCESS);
}
@@ -436,13 +440,11 @@ int PORT_SetDataCOCallback(uint16_t handle, tPORT_DATA_CO_CALLBACK* p_port_cb) {
  log::verbose("PORT_SetDataCOCallback() handle:{} cb 0x{}", handle,
               fmt::ptr(p_port_cb));

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    return (PORT_NOT_OPENED);
  }
@@ -464,14 +466,12 @@ int PORT_SetDataCOCallback(uint16_t handle, tPORT_DATA_CO_CALLBACK* p_port_cb) {
 ******************************************************************************/
int PORT_SetEventMask(uint16_t handle, uint32_t mask) {
  log::verbose("PORT_SetEventMask() handle:{} mask:0x{:x}", handle, mask);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    return (PORT_NOT_OPENED);
  }
@@ -495,11 +495,11 @@ int PORT_SetEventMask(uint16_t handle, uint32_t mask) {
 ******************************************************************************/
int PORT_CheckConnection(uint16_t handle, RawAddress* bd_addr,
                         uint16_t* p_lcid) {
  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];
  log::verbose(
      "handle={}, in_use={}, port_state={}, p_mcb={}, peer_ready={}, "
      "rfc_state={}",
@@ -590,14 +590,12 @@ int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) {
  uint8_t baud_rate;

  log::verbose("PORT_SetState() handle:{}", handle);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    return (PORT_NOT_OPENED);
  }
@@ -634,13 +632,12 @@ int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) {
int PORT_GetState(uint16_t handle, tPORT_STATE* p_settings) {
  log::verbose("PORT_GetState() handle:{}", handle);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    return (PORT_NOT_OPENED);
  }
@@ -673,13 +670,12 @@ int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable) {

  log::verbose("PORT_FlowControl() handle:{} enable: {}", handle, enable);

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    return (PORT_NOT_OPENED);
  }
@@ -743,13 +739,12 @@ int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
  /* Initialize this in case of an error */
  *p_len = 0;

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }

  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    return (PORT_NOT_OPENED);
  }
@@ -902,11 +897,11 @@ int PORT_WriteDataCO(uint16_t handle, int* p_len) {
  log::verbose("PORT_WriteDataCO() handle:{}", handle);
  *p_len = 0;

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    log::warn("PORT_WriteDataByFd() no port state:{}", p_port->state);
@@ -1059,11 +1054,11 @@ int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,

  *p_len = 0;

  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];

  if (!p_port->in_use || (p_port->state == PORT_CONNECTION_STATE_CLOSED)) {
    log::warn("PORT_WriteData() no port state:{}", p_port->state);
@@ -1194,11 +1189,11 @@ const char* PORT_GetResultString(const uint8_t result_code) {
 *
 ******************************************************************************/
int PORT_GetSecurityMask(uint16_t handle, uint16_t* sec_mask) {
  /* Check if handle is valid to avoid crashing */
  if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
  tPORT* p_port = get_port_from_handle(handle);
  if (p_port == nullptr) {
    log::error("Unable to get RFCOMM port control block bad handle:{}", handle);
    return (PORT_BAD_HANDLE);
  }
  tPORT* p_port = &rfc_cb.port.port[handle - 1];
  *sec_mask = p_port->sec_mask;
  return (PORT_SUCCESS);
}