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

Commit efa96cfb authored by Chris Manton's avatar Chris Manton
Browse files

gatt: Properly gatt register/deregister

Bug: 187827452
Test: gd/cert/run
Tag: #refactor

Change-Id: I63eba9149c2695e42c69bfdb19fd5531305b5b8d
parent 9f62b168
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1005,7 +1005,7 @@ tGATT_IF GATT_Register(const Uuid& app_uuid128, std::string name,
  for (i_gatt_if = 0, p_reg = gatt_cb.cl_rcb; i_gatt_if < GATT_MAX_APPS;
       i_gatt_if++, p_reg++) {
    if (!p_reg->in_use) {
      memset(p_reg, 0, sizeof(tGATT_REG));
      *p_reg = {};
      i_gatt_if++; /* one based number */
      p_reg->app_uuid128 = app_uuid128;
      gatt_if = p_reg->gatt_if = (tGATT_IF)i_gatt_if;
+56 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <string>

#include "common/message_loop_thread.h"
#include "common/strings.h"
#include "stack/gatt/gatt_int.h"
#include "stack/include/gatt_api.h"

@@ -34,12 +35,47 @@ bluetooth::common::MessageLoopThread* get_main_thread() { return nullptr; }

class StackGattTest : public ::testing::Test {};

namespace {

// Actual size of structure without compiler padding
size_t actual_sizeof_tGATT_REG() {
  return sizeof(bluetooth::Uuid) + sizeof(tGATT_CBACK) + sizeof(tGATT_IF) +
         sizeof(bool) + sizeof(uint8_t) + sizeof(bool);
}

void tGATT_DISC_RES_CB(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
                       tGATT_DISC_RES* p_data) {}
void tGATT_DISC_CMPL_CB(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
                        tGATT_STATUS status) {}
void tGATT_CMPL_CBACK(uint16_t conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
                      tGATT_CL_COMPLETE* p_data) {}
void tGATT_CONN_CBACK(tGATT_IF gatt_if, const RawAddress& bda, uint16_t conn_id,
                      bool connected, tGATT_DISCONN_REASON reason,
                      tBT_TRANSPORT transport) {}
void tGATT_REQ_CBACK(uint16_t conn_id, uint32_t trans_id, tGATTS_REQ_TYPE type,
                     tGATTS_DATA* p_data) {}
void tGATT_CONGESTION_CBACK(uint16_t conn_id, bool congested) {}
void tGATT_ENC_CMPL_CB(tGATT_IF gatt_if, const RawAddress& bda) {}
void tGATT_PHY_UPDATE_CB(tGATT_IF gatt_if, uint16_t conn_id, uint8_t tx_phy,
                         uint8_t rx_phy, tGATT_STATUS status) {}
void tGATT_CONN_UPDATE_CB(tGATT_IF gatt_if, uint16_t conn_id, uint16_t interval,
                          uint16_t latency, uint16_t timeout,
                          tGATT_STATUS status) {}

tGATT_CBACK gatt_callbacks = {
    .p_conn_cb = tGATT_CONN_CBACK,
    .p_cmpl_cb = tGATT_CMPL_CBACK,
    .p_disc_res_cb = tGATT_DISC_RES_CB,
    .p_disc_cmpl_cb = tGATT_DISC_CMPL_CB,
    .p_req_cb = tGATT_REQ_CBACK,
    .p_enc_cmpl_cb = tGATT_ENC_CMPL_CB,
    .p_congestion_cb = tGATT_CONGESTION_CBACK,
    .p_phy_update_cb = tGATT_PHY_UPDATE_CB,
    .p_conn_update_cb = tGATT_CONN_UPDATE_CB,
};

}  // namespace

TEST_F(StackGattTest, lifecycle_tGATT_REG) {
  {
    std::unique_ptr<tGATT_REG> reg0 = std::make_unique<tGATT_REG>();
@@ -80,7 +116,26 @@ TEST_F(StackGattTest, lifecycle_tGATT_REG) {
  }
}

TEST_F(StackGattTest, gatt_init) {
TEST_F(StackGattTest, gatt_init_free) {
  gatt_init();
  gatt_free();
}

TEST_F(StackGattTest, GATT_Register_Deregister) {
  gatt_init();

  // Gatt db profile always takes the first slot
  tGATT_IF apps[GATT_MAX_APPS - 1];

  for (int i = 0; i < GATT_MAX_APPS - 1; i++) {
    std::string name = bluetooth::common::StringFormat("name%02d", i);
    apps[i] = GATT_Register(bluetooth::Uuid::GetRandom(), name, &gatt_callbacks,
                            false);
  }

  for (int i = 0; i < GATT_MAX_APPS - 1; i++) {
    GATT_Deregister(apps[i]);
  }

  gatt_free();
}