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

Commit fb7759ee authored by Himanshu Rawat's avatar Himanshu Rawat
Browse files

bta_hh_le minor refactor

Test: mmm packages/modules/Bluetooth
Bug: 332590397
Flag: EXEMPT minor refactor
Change-Id: I6a13632211d84753c1680815d2c5637d517cba49
parent 21e48a15
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -314,6 +314,17 @@ void bta_hh_security_cmpl(tBTA_HH_DEV_CB* p_cb, const tBTA_HH_DATA* p_buf);
void bta_hh_le_notify_enc_cmpl(tBTA_HH_DEV_CB* p_cb,
                               const tBTA_HH_DATA* p_data);

tBTA_HH_LE_RPT* bta_hh_le_find_alloc_report_entry(tBTA_HH_DEV_CB* p_cb,
                                                  uint8_t srvc_inst_id,
                                                  uint16_t rpt_uuid,
                                                  uint16_t inst_id);
void bta_hh_le_save_report_ref(tBTA_HH_DEV_CB* p_dev_cb, tBTA_HH_LE_RPT* p_rpt,
                               uint8_t rpt_type, uint8_t rpt_id);
void bta_hh_le_srvc_init(tBTA_HH_DEV_CB* p_dev_cb, uint16_t handle);
void bta_hh_le_save_report_map(tBTA_HH_DEV_CB* p_dev_cb, uint16_t len,
                               uint8_t* desc);
void bta_hh_le_service_parsed(tBTA_HH_DEV_CB* p_dev_cb, tGATT_STATUS status);

#if (BTA_HH_DEBUG == TRUE)
void bta_hh_trace_dev_db(void);
#endif
+93 −77
Original line number Diff line number Diff line
@@ -403,7 +403,7 @@ static tBTA_HH_LE_RPT* bta_hh_le_find_rpt_by_idtype(tBTA_HH_LE_RPT* p_head,
 *                  list.
 *
 ******************************************************************************/
static tBTA_HH_LE_RPT* bta_hh_le_find_alloc_report_entry(tBTA_HH_DEV_CB* p_cb,
tBTA_HH_LE_RPT* bta_hh_le_find_alloc_report_entry(tBTA_HH_DEV_CB* p_cb,
                                                  uint8_t srvc_inst_id,
                                                  uint16_t rpt_uuid,
                                                  uint16_t inst_id) {
@@ -497,29 +497,15 @@ static tBTA_HH_STATUS bta_hh_le_read_char_descriptor(tBTA_HH_DEV_CB* p_cb,
 * Parameters:
 *
 ******************************************************************************/
static void bta_hh_le_save_report_ref(tBTA_HH_DEV_CB* p_dev_cb,
                                      tBTA_HH_LE_RPT* p_rpt,
                                      tGATT_STATUS status, uint8_t* value,
                                      uint16_t len) {
  if (status == GATT_INSUF_AUTHENTICATION) {
    /* close connection right away */
    p_dev_cb->status = BTA_HH_ERR_AUTH_FAILED;
    /* close the connection and report service discovery complete with error */
    bta_hh_le_api_disc_act(p_dev_cb);
    return;
  }

  /* if the length of the descriptor value is right, parse it */
  if (status == GATT_SUCCESS && len == 2) {
    uint8_t* pp = value;

    STREAM_TO_UINT8(p_rpt->rpt_id, pp);
    STREAM_TO_UINT8(p_rpt->rpt_type, pp);
void bta_hh_le_save_report_ref(tBTA_HH_DEV_CB* p_dev_cb, tBTA_HH_LE_RPT* p_rpt,
                               uint8_t rpt_type, uint8_t rpt_id) {
  log::verbose("report ID:{}, report type: {}", rpt_id, rpt_type);
  p_rpt->rpt_id = rpt_id;
  p_rpt->rpt_type = rpt_type;

  if (p_rpt->rpt_type > BTA_HH_RPTT_FEATURE) /* invalid report type */
    p_rpt->rpt_type = BTA_HH_RPTT_RESRV;

    log::verbose("report ID:{}", p_rpt->rpt_id);
  tBTA_HH_RPT_CACHE_ENTRY rpt_entry;
  rpt_entry.rpt_id = p_rpt->rpt_id;
  rpt_entry.rpt_type = p_rpt->rpt_type;
@@ -530,12 +516,6 @@ static void bta_hh_le_save_report_ref(tBTA_HH_DEV_CB* p_dev_cb,
  bta_hh_le_co_rpt_info(p_dev_cb->link_spec, &rpt_entry, p_dev_cb->app_id);
}

  if (p_rpt->index < BTA_HH_LE_RPT_MAX - 1)
    p_rpt++;
  else
    p_rpt = NULL;
}

/*******************************************************************************
 *
 * Function         bta_hh_le_register_input_notif
@@ -707,6 +687,7 @@ static void write_rpt_ctl_cfg_cb(uint16_t conn_id, tGATT_STATUS status,
      log::error("Unknown char ID clt cfg:0x{:04x}", char_uuid);
  }
}

/*******************************************************************************
 *
 * Function         bta_hh_le_write_rpt_clt_cfg
@@ -742,11 +723,14 @@ static bool bta_hh_le_write_rpt_clt_cfg(tBTA_HH_DEV_CB* p_cb) {
  return false;
}

static void write_proto_mode_cb(uint16_t conn_id, tGATT_STATUS status,
                                uint16_t handle, uint16_t len,
                                const uint8_t* value, void* data) {
  tBTA_HH_DEV_CB* p_dev_cb = (tBTA_HH_DEV_CB*)data;

/*******************************************************************************
 *
 * Function         bta_hh_le_service_parsed
 *
 * Description      Continue after discovered services are parsed.
 *
 ******************************************************************************/
void bta_hh_le_service_parsed(tBTA_HH_DEV_CB* p_dev_cb, tGATT_STATUS status) {
  if (p_dev_cb->state == BTA_HH_CONN_ST) {
    /* Set protocol finished in CONN state*/

@@ -771,6 +755,13 @@ static void write_proto_mode_cb(uint16_t conn_id, tGATT_STATUS status,
  }
}

static void write_proto_mode_cb(uint16_t conn_id, tGATT_STATUS status,
                                uint16_t handle, uint16_t len,
                                const uint8_t* value, void* data) {
  tBTA_HH_DEV_CB* p_dev_cb = (tBTA_HH_DEV_CB*)data;
  bta_hh_le_service_parsed(p_dev_cb, status);
}

/*******************************************************************************
 *
 * Function         bta_hh_le_set_protocol_mode
@@ -1274,15 +1265,8 @@ static void read_hid_info_cb(uint16_t conn_id, tGATT_STATUS status,
  STREAM_TO_UINT8(p_dev_cb->dscp_info.flag, pp);
}

static void read_hid_report_map_cb(uint16_t conn_id, tGATT_STATUS status,
                                   uint16_t handle, uint16_t len,
                                   uint8_t* value, void* data) {
  if (status != GATT_SUCCESS) {
    log::error("error reading characteristic:{}", status);
    return;
  }

  tBTA_HH_DEV_CB* p_dev_cb = (tBTA_HH_DEV_CB*)data;
void bta_hh_le_save_report_map(tBTA_HH_DEV_CB* p_dev_cb, uint16_t len,
                               uint8_t* desc) {
  tBTA_HH_LE_HID_SRVC* p_srvc = &p_dev_cb->hid_srvc;

  osi_free_and_reset((void**)&p_srvc->rpt_map);
@@ -1290,13 +1274,25 @@ static void read_hid_report_map_cb(uint16_t conn_id, tGATT_STATUS status,
  if (len > 0) {
    p_srvc->rpt_map = (uint8_t*)osi_malloc(len);

    uint8_t* pp = value;
    uint8_t* pp = desc;
    STREAM_TO_ARRAY(p_srvc->rpt_map, pp, len);
    p_srvc->descriptor.dl_len = len;
    p_srvc->descriptor.dsc_list = p_dev_cb->hid_srvc.rpt_map;
  }
}

static void read_hid_report_map_cb(uint16_t conn_id, tGATT_STATUS status,
                                   uint16_t handle, uint16_t len,
                                   uint8_t* value, void* data) {
  if (status != GATT_SUCCESS) {
    log::error("error reading characteristic:{}", status);
    return;
  }

  tBTA_HH_DEV_CB* p_dev_cb = (tBTA_HH_DEV_CB*)data;
  bta_hh_le_save_report_map(p_dev_cb, len, value);
}

static void read_ext_rpt_ref_desc_cb(uint16_t conn_id, tGATT_STATUS status,
                                     uint16_t handle, uint16_t len,
                                     uint8_t* value, void* data) {
@@ -1329,6 +1325,11 @@ static void read_report_ref_desc_cb(uint16_t conn_id, tGATT_STATUS status,
    return;
  }

  if (value == nullptr || len != 2) {
    log::error("Invalid report reference");
    return;
  }

  tBTA_HH_DEV_CB* p_dev_cb = (tBTA_HH_DEV_CB*)data;
  const gatt::Descriptor* p_desc = BTA_GATTC_GetDescriptor(conn_id, handle);

@@ -1346,7 +1347,18 @@ static void read_report_ref_desc_cb(uint16_t conn_id, tGATT_STATUS status,
  p_rpt = bta_hh_le_find_report_entry(p_dev_cb, service->handle,
                                      GATT_UUID_HID_REPORT,
                                      characteristic->value_handle);
  if (p_rpt) bta_hh_le_save_report_ref(p_dev_cb, p_rpt, status, value, len);
  if (p_rpt == nullptr) {
    log::error("No such report");
    return;
  }

  uint8_t* pp = value;
  uint8_t rpt_id;
  uint8_t rpt_type;
  STREAM_TO_UINT8(rpt_id, pp);
  STREAM_TO_UINT8(rpt_type, pp);

  bta_hh_le_save_report_ref(p_dev_cb, p_rpt, rpt_type, rpt_id);
}

static void read_pref_conn_params_cb(uint16_t conn_id, tGATT_STATUS status,
@@ -1407,7 +1419,7 @@ static void read_pref_conn_params_cb(uint16_t conn_id, tGATT_STATUS status,

/*******************************************************************************
 *
 * Function         bta_hh_le_search_hid_chars
 * Function         bta_hh_le_parse_hogp_service
 *
 * Description      This function discover all characteristics a service and
 *                  all descriptors available.
@@ -1415,10 +1427,12 @@ static void read_pref_conn_params_cb(uint16_t conn_id, tGATT_STATUS status,
 * Parameters:
 *
 ******************************************************************************/
static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb,
static void bta_hh_le_parse_hogp_service(tBTA_HH_DEV_CB* p_dev_cb,
                                         const gatt::Service* service) {
  tBTA_HH_LE_RPT* p_rpt;

  bta_hh_le_srvc_init(p_dev_cb, service->handle);

  for (const gatt::Characteristic& charac : service->characteristics) {
    if (!charac.uuid.Is16Bit()) continue;

@@ -1486,6 +1500,13 @@ static void bta_hh_le_search_hid_chars(tBTA_HH_DEV_CB* p_dev_cb,
  }
}

void bta_hh_le_srvc_init(tBTA_HH_DEV_CB* p_dev_cb, uint16_t handle) {
  p_dev_cb->hid_srvc.state = BTA_HH_SERVICE_DISCOVERED;
  p_dev_cb->hid_srvc.srvc_inst_id = handle;
  p_dev_cb->hid_srvc.proto_mode_handle = 0;
  p_dev_cb->hid_srvc.control_point_handle = 0;
}

/*******************************************************************************
 *
 * Function         bta_hh_le_srvc_search_cmpl
@@ -1509,14 +1530,14 @@ static void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) {
  }

  const std::list<gatt::Service>* services = BTA_GATTC_GetServices(p_data->conn_id);
  const gatt::Service* hogp_service = nullptr;
  const gatt::Service* gap_service = nullptr;
  const gatt::Service* scp_service = nullptr;

  int num_hid_service = 0;
  bool have_hid = false;
  for (const gatt::Service& service : *services) {
    if (service.uuid == Uuid::From16Bit(UUID_SERVCLASS_LE_HID) &&
        service.is_primary && !have_hid) {
        service.is_primary && hogp_service == nullptr) {
      // TODO(b/286413526): The current implementation connects to the first HID
      // service, in the case of multiple HID services being present. As a
      // temporary mitigation, connect to the third HID service for some
@@ -1532,16 +1553,7 @@ static void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) {
      }

      /* found HID primamry service */
      have_hid = true;
      p_dev_cb->hid_srvc.state = BTA_HH_SERVICE_DISCOVERED;
      p_dev_cb->hid_srvc.srvc_inst_id = service.handle;
      p_dev_cb->hid_srvc.proto_mode_handle = 0;
      p_dev_cb->hid_srvc.control_point_handle = 0;

      bta_hh_le_search_hid_chars(p_dev_cb, &service);

      log::verbose("have HID service inst_id={}",
                   p_dev_cb->hid_srvc.srvc_inst_id);
      hogp_service = &service;
    } else if (service.uuid == Uuid::From16Bit(UUID_SERVCLASS_SCAN_PARAM)) {
      scp_service = &service;
    } else if (service.uuid == Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER)) {
@@ -1549,7 +1561,11 @@ static void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL* p_data) {
    }
  }

  if (!have_hid) {
  if (hogp_service != nullptr) {
    log::verbose("have HOGP service inst_id={}",
                 p_dev_cb->hid_srvc.srvc_inst_id);
    bta_hh_le_parse_hogp_service(p_dev_cb, hogp_service);
  } else {
    log::error("HID service not found");
    p_dev_cb->status = BTA_HH_ERR_SDP;
    bta_hh_le_api_disc_act(p_dev_cb);
@@ -1894,10 +1910,10 @@ static void write_report_cb(uint16_t conn_id, tGATT_STATUS status,

  if (p_char == nullptr) return;

  uint16_t uuid = p_char->uuid.As16Bit();
  if (uuid != GATT_UUID_HID_REPORT && uuid != GATT_UUID_HID_BT_KB_INPUT &&
      uuid != GATT_UUID_HID_BT_MOUSE_INPUT &&
      uuid != GATT_UUID_HID_BT_KB_OUTPUT) {
  uint16_t uuid16 = p_char->uuid.As16Bit();
  if (uuid16 != GATT_UUID_HID_REPORT && uuid16 != GATT_UUID_HID_BT_KB_INPUT &&
      uuid16 != GATT_UUID_HID_BT_MOUSE_INPUT &&
      uuid16 != GATT_UUID_HID_BT_KB_OUTPUT) {
    return;
  }