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

Commit 6dd8dc88 authored by Michael Sun's avatar Michael Sun
Browse files

btif: hh: enhance the dis(connect) call error returns

Perform more checks on Hid Host and the device status before handling the connect/disconnect request to provide a more meaningful return value to the caller.

BUG: 240781725
Tag: #floss
Test: gd/cert/run
Change-Id: I1008239d96a01d8eb86fe29aae1e24c71ad942df
parent 716eaa68
Loading
Loading
Loading
Loading
+49 −16
Original line number Diff line number Diff line
@@ -1320,16 +1320,35 @@ static bt_status_t init(bthh_callbacks_t* callbacks) {
 *
 ******************************************************************************/
static bt_status_t connect(RawAddress* bd_addr) {
  if (btif_hh_cb.status != BTIF_HH_DEV_CONNECTING) {
    btif_transfer_context(btif_hh_handle_evt, BTIF_HH_CONNECT_REQ_EVT,
                          (char*)bd_addr, sizeof(RawAddress), NULL);
    return BT_STATUS_SUCCESS;
  } else if ((btif_hh_cb.pending_conn_address == *bd_addr) &&
       (btif_hh_cb.status == BTIF_HH_DEV_CONNECTING)) {
    LOG(INFO) << __func__ << ": already connecting " << *bd_addr;
  btif_hh_device_t* p_dev;

  if (btif_hh_cb.status == BTIF_HH_DEV_CONNECTING) {
    BTIF_TRACE_WARNING("%s: Error, HH status = %d", __func__,
                       btif_hh_cb.status);
    return BT_STATUS_BUSY;
  } else if (btif_hh_cb.status == BTIF_HH_DISABLED ||
             btif_hh_cb.status == BTIF_HH_DISABLING) {
    BTIF_TRACE_WARNING("%s: Error, HH status = %d", __func__,
                       btif_hh_cb.status);
    return BT_STATUS_NOT_READY;
  }

  p_dev = btif_hh_find_connected_dev_by_bda(*bd_addr);
  if (p_dev) {
    if (p_dev->dev_status == BTHH_CONN_STATE_CONNECTED ||
        p_dev->dev_status == BTHH_CONN_STATE_CONNECTING) {
      BTIF_TRACE_ERROR("%s: Error, device %s already connected.", __func__,
                       bd_addr->ToString().c_str());
      return BT_STATUS_DONE;
    } else if (p_dev->dev_status == BTHH_CONN_STATE_DISCONNECTING) {
      BTIF_TRACE_ERROR("%s: Error, device %s is busy with (dis)connecting.",
                       __func__, bd_addr->ToString().c_str());
      return BT_STATUS_BUSY;
    }
  return BT_STATUS_FAIL;
  }

  return btif_transfer_context(btif_hh_handle_evt, BTIF_HH_CONNECT_REQ_EVT,
                               (char*)bd_addr, sizeof(RawAddress), NULL);
}

/*******************************************************************************
@@ -1346,19 +1365,33 @@ static bt_status_t disconnect(RawAddress* bd_addr) {
  BTIF_TRACE_EVENT("BTHH: %s", __func__);
  btif_hh_device_t* p_dev;

  if (btif_hh_cb.status == BTIF_HH_DISABLED) {
  if (btif_hh_cb.status == BTIF_HH_DISABLED ||
      btif_hh_cb.status == BTIF_HH_DISABLING) {
    BTIF_TRACE_WARNING("%s: Error, HH status = %d", __func__,
                       btif_hh_cb.status);
    return BT_STATUS_FAIL;
    return BT_STATUS_UNHANDLED;
  }

  p_dev = btif_hh_find_connected_dev_by_bda(*bd_addr);
  if (p_dev != NULL) {
  if (!p_dev) {
    BTIF_TRACE_ERROR("%s: Error, device %s not opened.", __func__,
                     bd_addr->ToString().c_str());
    return BT_STATUS_UNHANDLED;
  }

  if (p_dev->dev_status == BTHH_CONN_STATE_DISCONNECTED ||
      p_dev->dev_status == BTHH_CONN_STATE_DISCONNECTING) {
    BTIF_TRACE_ERROR("%s: Error, device %s already disconnected.", __func__,
                     bd_addr->ToString().c_str());
    return BT_STATUS_DONE;
  } else if (p_dev->dev_status == BTHH_CONN_STATE_CONNECTING) {
    BTIF_TRACE_ERROR("%s: Error, device %s is busy with (dis)connecting.",
                     __func__, bd_addr->ToString().c_str());
    return BT_STATUS_BUSY;
  }

  return btif_transfer_context(btif_hh_handle_evt, BTIF_HH_DISCONNECT_REQ_EVT,
                               (char*)bd_addr, sizeof(RawAddress), NULL);
  } else {
    BTIF_TRACE_WARNING("%s: Error, device  not opened.", __func__);
    return BT_STATUS_FAIL;
  }
}

/*******************************************************************************