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

Commit afd553da authored by Sanket Agarwal's avatar Sanket Agarwal
Browse files

Write unit test for bta/btif HF client role

Also fix a bug discovered by unit tests:
bdcmp returns 0 on success hence use negation

Bug: b/30984220
Test: Unit test

Change-Id: I37f7c71c5dd809e4df3d8c2c79906a74c2b19d34
parent a22c3670
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@ LOCAL_PATH:= $(call my-dir)

# Tests
btaTestSrc := \
  test/bta_closure_test.cc
  test/bta_closure_test.cc \
  test/bta_hf_client_test.cc

btaCommonIncludes := \
                   $(LOCAL_PATH)/../ \
@@ -122,8 +123,8 @@ LOCAL_CPP_EXTENSION := .cc

LOCAL_C_INCLUDES := $(btaCommonIncludes)
LOCAL_SRC_FILES := $(btaTestSrc)
LOCAL_SHARED_LIBRARIES := libcutils libc libchrome libhardware liblog
LOCAL_STATIC_LIBRARIES := libbtcore libbt-bta libosi
LOCAL_SHARED_LIBRARIES := libcutils libc libchrome libhardware liblog libprotobuf-cpp-lite
LOCAL_STATIC_LIBRARIES := libbtcore libbt-bta libosi libbt-protos
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := net_test_bta

+1 −1
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ void BTA_HfClientOpen(BD_ADDR bd_addr, tBTA_SEC sec_mask, uint16_t* p_handle) {
  tBTA_HF_CLIENT_API_OPEN* p_buf =
      (tBTA_HF_CLIENT_API_OPEN*)osi_malloc(sizeof(tBTA_HF_CLIENT_API_OPEN));

  if (!bta_hf_client_allocate_handle(p_handle)) {
  if (!bta_hf_client_allocate_handle(bd_addr, p_handle)) {
    APPL_TRACE_ERROR("%s: could not allocate handle", __func__);
    return;
  }
+4 −2
Original line number Diff line number Diff line
@@ -206,14 +206,15 @@ extern tBTA_HF_CLIENT_CB_ARR bta_hf_client_cb_arr;

/* main functions */
extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle);
extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(BD_ADDR bd_addr);
extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(const BD_ADDR bd_addr);
extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_rfc_handle(uint16_t handle);
extern tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle);
extern bool bta_hf_client_hdl_event(BT_HDR* p_msg);
extern void bta_hf_client_sm_execute(uint16_t event,
                                     tBTA_HF_CLIENT_DATA* p_data);
extern void bta_hf_client_slc_seq(tBTA_HF_CLIENT_CB* client_cb, bool error);
extern bool bta_hf_client_allocate_handle(uint16_t* handle);
extern bool bta_hf_client_allocate_handle(const BD_ADDR bd_addr,
                                          uint16_t* p_handle);
extern void bta_hf_client_app_callback(uint16_t event, tBTA_HF_CLIENT* data);
extern void bta_hf_client_collision_cback(tBTA_SYS_CONN_STATUS status,
                                          uint8_t id, uint8_t app_id,
@@ -226,6 +227,7 @@ extern tBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback,

extern void bta_hf_client_api_disable(void);
extern void bta_hf_client_dump_statistics(int fd);
extern void bta_hf_client_cb_arr_init(void);

/* SDP functions */
extern bool bta_hf_client_add_record(char* p_service_name, uint8_t scn,
+35 −11
Original line number Diff line number Diff line
@@ -263,6 +263,27 @@ tBTA_HF_CLIENT_CB_ARR bta_hf_client_cb_arr;
static const tBTA_SYS_REG bta_hf_client_reg = {bta_hf_client_hdl_event,
                                               BTA_HfClientDisable};

/*******************************************************************************
*
* Function         bta_hf_client_cb_arr_init
*
* Description      Initialize entire control block array set
*
*
* Returns          void
*
******************************************************************************/
void bta_hf_client_cb_arr_init() {
  memset(&bta_hf_client_cb_arr, 0, sizeof(tBTA_HF_CLIENT_CB_ARR));

  // reset the handles and make the CBs non-allocated
  for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
    // Allocate the handles in increasing order of indices
    bta_hf_client_cb_arr.cb[i].handle = BTA_HF_CLIENT_CB_FIRST_HANDLE + i;
    bta_hf_client_cb_arr.cb[i].is_allocated = false;
  }
}

/*******************************************************************************
*
* Function         bta_hf_client_cb_init
@@ -390,19 +411,13 @@ tBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback,
  /* register with BTA system manager */
  bta_sys_register(BTA_ID_HS, &bta_hf_client_reg);

  memset(&bta_hf_client_cb_arr, 0, sizeof(tBTA_HF_CLIENT_CB_ARR));
  /* reset the control blocks */
  bta_hf_client_cb_arr_init();

  bta_hf_client_cb_arr.p_cback = p_cback;
  bta_hf_client_cb_arr.serv_sec_mask = sec_mask;
  bta_hf_client_cb_arr.features = features;

  // reset the handles and make the CBs non-allocated
  for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
    // Allocate the handles in increasing order of indices
    bta_hf_client_cb_arr.cb[i].handle = BTA_HF_CLIENT_CB_FIRST_HANDLE + i;
    bta_hf_client_cb_arr.cb[i].is_allocated = false;
  }

  /* create SDP records */
  bta_hf_client_create_record(&bta_hf_client_cb_arr, p_service_name);

@@ -465,11 +480,11 @@ tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle) {
*                  none exists
*
******************************************************************************/
tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(BD_ADDR peer_addr) {
tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(const BD_ADDR peer_addr) {
  for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
    // Check if the associated index is allocated and that BD ADDR matches
    tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
    if (client_cb->is_allocated && bdcmp(peer_addr, client_cb->peer_addr)) {
    if (client_cb->is_allocated && !bdcmp(peer_addr, client_cb->peer_addr)) {
      return client_cb;
    } else {
      APPL_TRACE_WARNING("%s: bdaddr mismatch for handle %d alloc %d", __func__,
@@ -546,6 +561,8 @@ tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle) {
*                  channel for HF connection. If the channel cannot be created
*                  for a reason then false is returned
*
*                  bd_addr: Address of the device for which this block is
*                  being created. Single device can only have one block.
*                  p_handle: OUT variable to store the outcome of allocate. If
*                  allocate failed then value is not valid
*
@@ -553,7 +570,13 @@ tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle) {
* Returns          true if the creation of p_handle succeeded, false otherwise
*
******************************************************************************/
bool bta_hf_client_allocate_handle(uint16_t* p_handle) {
bool bta_hf_client_allocate_handle(const BD_ADDR bd_addr, uint16_t* p_handle) {
  tBTA_HF_CLIENT_CB* existing_cb;
  if ((existing_cb = bta_hf_client_find_cb_by_bda(bd_addr)) != NULL) {
    BTIF_TRACE_ERROR("%s: cannot allocate handle since BDADDR already exists",
                     __func__);
    return false;
  }
  /* Check that we do not have a request to for same device in the control
   * blocks */
  for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
@@ -566,6 +589,7 @@ bool bta_hf_client_allocate_handle(uint16_t* p_handle) {

    *p_handle = client_cb->handle;
    client_cb->is_allocated = true;
    bdcpy(client_cb->peer_addr, bd_addr);

    // Reset the handle for use (i.e. reset timers etc)
    bta_hf_client_cb_init(client_cb);
+9 −1
Original line number Diff line number Diff line
@@ -96,9 +96,14 @@ static void bta_hf_client_mgmt_cback(uint32_t code, uint16_t port_handle) {

      APPL_TRACE_DEBUG("%s: allocating a new CB for incoming connection",
                       __func__);
      // Find the BDADDR of the peer device
      BD_ADDR peer_addr;
      uint16_t lcid;
      PORT_CheckConnection(port_handle, peer_addr, &lcid);

      // Since we accepted a remote request we should allocate a handle first.
      uint16_t tmp_handle = -1;
      bta_hf_client_allocate_handle(&tmp_handle);
      bta_hf_client_allocate_handle(peer_addr, &tmp_handle);
      client_cb = bta_hf_client_find_cb_by_handle(tmp_handle);

      // If allocation fails then we abort.
@@ -109,6 +114,9 @@ static void bta_hf_client_mgmt_cback(uint32_t code, uint16_t port_handle) {
        // Set the connection fields for this new CB
        client_cb->conn_handle = port_handle;

        // Copy the BDADDR
        bdcpy(client_cb->peer_addr, peer_addr);

        // Since we have accepted an incoming RFCOMM connection:
        // a) Release the current server from it duties
        // b) Start a new server for more new incoming connection
Loading