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

Commit 61c743d0 authored by Himanshu Rawat's avatar Himanshu Rawat Committed by Automerger Merge Worker
Browse files

Merge "HidHost: Rearrange bta_hh_co_open() to make more readable" into main am: d0442618

parents 35487662 d0442618
Loading
Loading
Loading
Loading
+30 −52
Original line number Diff line number Diff line
@@ -383,26 +383,7 @@ int bta_hh_co_write(int fd, uint8_t* rpt, uint16_t len) {

  return uhid_write(fd, &ev);
}
static bool hh_co_update_device(btif_hh_device_t* p_dev, uint8_t dev_handle,
                                uint8_t sub_class, tBTA_HH_ATTR_MASK attr_mask,
                                uint8_t app_id) {
  p_dev->fd = -1;
  p_dev->hh_keep_polling = 0;

  // This is a new device, open the uhid driver now.
  if (!uhid_fd_open(p_dev)) {
    return false;
  }

  p_dev->dev_handle = dev_handle;
  p_dev->attr_mask = attr_mask;
  p_dev->sub_class = sub_class;
  p_dev->app_id = app_id;
  p_dev->local_vup = false;
  btif_hh_cb.device_num++;

  return true;
}
/*******************************************************************************
 *
 * Function      bta_hh_co_open
@@ -414,53 +395,50 @@ static bool hh_co_update_device(btif_hh_device_t* p_dev, uint8_t dev_handle,
 ******************************************************************************/
bool bta_hh_co_open(uint8_t dev_handle, uint8_t sub_class,
                    tBTA_HH_ATTR_MASK attr_mask, uint8_t app_id) {
  uint32_t i;
  btif_hh_device_t* p_dev = NULL;
  bool new_device = false;

  if (dev_handle == BTA_HH_INVALID_HANDLE) {
    log::warn("dev_handle ({}) is invalid", dev_handle);
    return false;
  }

  for (i = 0; i < BTIF_HH_MAX_HID; i++) {
    p_dev = &btif_hh_cb.devices[i];
    if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN &&
        p_dev->dev_handle == dev_handle) {
      // We found a device with the same handle. Must be a device reconnected.
  // Reuse existing instance if possible
  btif_hh_device_t* p_dev = btif_hh_find_dev_by_handle(dev_handle);
  if (p_dev != nullptr) {
    log::info(
        "Found an existing device with the same handle dev_status={}, "
          "address={}, attr_mask=0x{:04x}, sub_class=0x{:02x}, app_id={}",
        "device={}, attr_mask=0x{:04x}, sub_class=0x{:02x}, app_id={}, "
        "dev_handle={}",
        p_dev->dev_status, ADDRESS_TO_LOGGABLE_CSTR(p_dev->link_spec),
          p_dev->attr_mask, p_dev->sub_class, p_dev->app_id);

      if (!uhid_fd_open(p_dev)) {
        p_dev->attr_mask, p_dev->sub_class, p_dev->app_id, dev_handle);
  } else {  // Use an empty slot
    p_dev = btif_hh_find_empty_dev();
    if (p_dev == nullptr) {
      log::error("Too many HID devices are connected");
      return false;
    }
      break;
    }
    p_dev = NULL;

    new_device = true;
    log::verbose("New HID device added for handle {}", dev_handle);

    p_dev->fd = -1;
    p_dev->hh_keep_polling = 0;
    p_dev->attr_mask = attr_mask;
    p_dev->sub_class = sub_class;
    p_dev->app_id = app_id;
    p_dev->local_vup = false;
  }

  if (p_dev == NULL) {
    // Did not find a device reconnection case. Find an empty slot now.
    for (i = 0; i < BTIF_HH_MAX_HID; i++) {
      if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_UNKNOWN) {
        p_dev = &btif_hh_cb.devices[i];
        if (!hh_co_update_device(p_dev, dev_handle, sub_class, attr_mask,
                                 app_id)) {
  if (!uhid_fd_open(p_dev)) {
    return false;
  }
        break;
      }
    }
  }

  if (p_dev == NULL) {
    log::error("Too many HID devices are connected");
    return false;
  if (new_device) {
    btif_hh_cb.device_num++;
  }

  p_dev->dev_status = BTHH_CONN_STATE_CONNECTED;
  p_dev->dev_handle = dev_handle;
  p_dev->get_rpt_id_queue = fixed_queue_new(SIZE_MAX);
  CHECK(p_dev->get_rpt_id_queue);
#if ENABLE_UHID_SET_REPORT
+2 −0
Original line number Diff line number Diff line
@@ -132,6 +132,8 @@ typedef struct {
extern btif_hh_cb_t btif_hh_cb;

btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle);
btif_hh_device_t* btif_hh_find_dev_by_handle(uint8_t handle);
btif_hh_device_t* btif_hh_find_empty_dev(void);
bt_status_t btif_hh_connect(const tAclLinkSpec& link_spec);
bt_status_t btif_hh_virtual_unplug(const tAclLinkSpec& link_spec);
void btif_hh_remove_device(const tAclLinkSpec& link_spec);
+37 −0
Original line number Diff line number Diff line
@@ -345,6 +345,43 @@ btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle) {
  return NULL;
}

/*******************************************************************************
 *
 * Function         btif_hh_find_dev_by_handle
 *
 * Description      Return the device pointer of the specified device handle
 *
 * Returns          Device entry pointer in the device table
 ******************************************************************************/
btif_hh_device_t* btif_hh_find_dev_by_handle(uint8_t handle) {
  for (int i = 0; i < BTIF_HH_MAX_HID; i++) {
    btif_hh_device_t* p_dev = &btif_hh_cb.devices[i];
    if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN &&
        p_dev->dev_handle == handle) {
      return p_dev;
    }
  }
  return nullptr;
}

/*******************************************************************************
 *
 * Function         btif_hh_find_empty_dev
 *
 * Description      Return an empty device
 *
 * Returns          Device entry pointer in the device table
 ******************************************************************************/
btif_hh_device_t* btif_hh_find_empty_dev(void) {
  for (int i = 0; i < BTIF_HH_MAX_HID; i++) {
    btif_hh_device_t* p_dev = &btif_hh_cb.devices[i];
    if (p_dev->dev_status == BTHH_CONN_STATE_UNKNOWN) {
      return p_dev;
    }
  }
  return nullptr;
}

/*******************************************************************************
 *
 * Function         btif_hh_find_dev_by_link_spec