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

Commit 693a3cdc authored by Steve Gu's avatar Steve Gu Committed by Andre Eisenbach
Browse files

Builds server with service, characteristic, and descriptor UUIDs. Then starts and stops the server.

Change-Id: I22e269010c5e4d96d9562155e03903c3ea957c97
parent 99fa292f
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ TEST_CASE_DECL(gatt_client_register);
TEST_CASE_DECL(gatt_client_scan);
TEST_CASE_DECL(gatt_client_advertise);
TEST_CASE_DECL(gatt_server_register);
TEST_CASE_DECL(gatt_server_build);

// These are run with the Bluetooth adapter disabled.
const test_case_t sanity_suite[] = {
@@ -62,7 +63,8 @@ const test_case_t test_suite[] = {
  TEST_CASE(gatt_client_register),
  TEST_CASE(gatt_client_scan),
  TEST_CASE(gatt_client_advertise),
  TEST_CASE(gatt_server_register)
  TEST_CASE(gatt_server_register),
  TEST_CASE(gatt_server_build)
};

const size_t sanity_suite_size = ARRAY_SIZE(sanity_suite);
+69 −10
Original line number Diff line number Diff line
@@ -6,10 +6,10 @@
#include "support/gatt.h"
#include "support/callbacks.h"

bt_uuid_t app_uuid;
#define DEFAULT_RANDOM_SEED 42

static void assign_random_app_uuid(bt_uuid_t *uuid) {
  srand(42);
static void create_random_uuid(bt_uuid_t *uuid, int seed) {
  srand(seed < 0 ? time(NULL) : seed);
  for (int i = 0; i < 16; ++i) {
    uuid->uu[i] = (uint8_t) (rand() % 256);
  }
@@ -19,8 +19,9 @@ bool gatt_client_register() {
  TASSERT(gatt_interface != NULL, "Null GATT interface.");

  // Registers gatt client.
  assign_random_app_uuid(&app_uuid);
  CALL_AND_WAIT(gatt_interface->client->register_client(&app_uuid), btgattc_register_app_cb);
  bt_uuid_t gatt_client_uuid;
  create_random_uuid(&gatt_client_uuid, DEFAULT_RANDOM_SEED);
  CALL_AND_WAIT(gatt_interface->client->register_client(&gatt_client_uuid), btgattc_register_app_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error registering GATT client app callback.");

  // Unregisters gatt client. No callback is expected.
@@ -45,8 +46,9 @@ bool gatt_client_advertise() {
  TASSERT(gatt_interface != NULL, "Null GATT interface.");

  // Registers a new client app.
  assign_random_app_uuid(&app_uuid);
  CALL_AND_WAIT(gatt_interface->client->register_client(&app_uuid), btgattc_register_app_cb);
  bt_uuid_t gatt_client_uuid;
  create_random_uuid(&gatt_client_uuid, DEFAULT_RANDOM_SEED);
  CALL_AND_WAIT(gatt_interface->client->register_client(&gatt_client_uuid), btgattc_register_app_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error registering GATT client app callback.");

  // Starts advertising.
@@ -66,12 +68,69 @@ bool gatt_client_advertise() {
bool gatt_server_register() {
  TASSERT(gatt_interface != NULL, "Null GATT interface.");

  // Registers gatt server
  assign_random_app_uuid(&app_uuid);
  CALL_AND_WAIT(gatt_interface->server->register_server(&app_uuid), btgatts_register_app_cb);
  // Registers gatt server.
  bt_uuid_t gatt_server_uuid;
  create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED);
  CALL_AND_WAIT(gatt_interface->server->register_server(&gatt_server_uuid), btgatts_register_app_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error registering GATT server app callback.");

  // Unregisters gatt server. No callback is expected.
  gatt_interface->server->unregister_server(gatt_get_server_interface());
  return true;
}

bool gatt_server_build() {
  TASSERT(gatt_interface != NULL, "Null GATT interface.");

  // Registers gatt server.
  bt_uuid_t gatt_server_uuid;
  create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED);
  CALL_AND_WAIT(gatt_interface->server->register_server(&gatt_server_uuid), btgatts_register_app_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error registering GATT server app callback.");

  // Service UUID.
  btgatt_srvc_id_t srvc_id;
  srvc_id.id.inst_id = 0;   // there is only one instance of this service.
  srvc_id.is_primary = 1;   // this service is primary.
  create_random_uuid(&srvc_id.id.uuid, -1);

  // Characteristics UUID.
  bt_uuid_t char_uuid;
  create_random_uuid(&char_uuid, -1);

  // Descriptor UUID.
  bt_uuid_t desc_uuid;
  create_random_uuid(&desc_uuid, -1);

  // Adds service.
  int server_if = gatt_get_server_interface();
  CALL_AND_WAIT(gatt_interface->server->add_service(server_if, &srvc_id, 4 /* # handles */), btgatts_service_added_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error adding service.");

  // Adds characteristics.
  int srvc_handle = gatt_get_service_handle();
  CALL_AND_WAIT(gatt_interface->server->add_characteristic(server_if, srvc_handle, &char_uuid, 0x10 /* notification */, 0x01 /* read only */), btgatts_characteristic_added_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error adding characteristics.");

  // Adds descriptor.
  int char_handle = gatt_get_characteristic_handle();
  CALL_AND_WAIT(gatt_interface->server->add_descriptor(server_if, srvc_handle, &desc_uuid, 0x01), btgatts_descriptor_added_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error adding descriptor.");

  // Starts server.
  CALL_AND_WAIT(gatt_interface->server->start_service(server_if, srvc_handle, 2 /*BREDR/LE*/), btgatts_service_started_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error starting server.");

  // Stops server.
  CALL_AND_WAIT(gatt_interface->server->stop_service(server_if, srvc_handle), btgatts_service_stopped_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error stopping server.");

  // Deletes service.
  CALL_AND_WAIT(gatt_interface->server->delete_service(server_if, srvc_handle), btgatts_service_deleted_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error deleting service.");

  // Unregisters gatt server. No callback is expected.
  gatt_interface->server->unregister_server(server_if);

  return true;
}
 No newline at end of file
+54 −0
Original line number Diff line number Diff line
@@ -22,8 +22,14 @@
#include "support/gatt.h"

const btgatt_interface_t *gatt_interface;
static bt_bdaddr_t remote_bd_addr;
static int gatt_client_interface;
static int gatt_server_interface;
static int gatt_service_handle;
static int gatt_included_service_handle;
static int gatt_characteristic_handle;
static int gatt_descriptor_handle;
static int gatt_connection_id;
static int gatt_status;

bool gatt_init() {
@@ -31,6 +37,10 @@ bool gatt_init() {
  return gatt_interface->init(callbacks_get_gatt_struct()) == BT_STATUS_SUCCESS;
}

int gatt_get_connection_id() {
  return gatt_connection_id;
}

int gatt_get_client_interface() {
  return gatt_client_interface;
}
@@ -39,6 +49,22 @@ int gatt_get_server_interface() {
  return gatt_server_interface;
}

int gatt_get_service_handle() {
  return gatt_service_handle;
}

int gatt_get_included_service_handle() {
  return gatt_included_service_handle;
}

int gatt_get_characteristic_handle() {
  return gatt_characteristic_handle;
}

int gatt_get_descriptor_handle() {
  return gatt_descriptor_handle;
}

int gatt_get_status() {
  return gatt_status;
}
@@ -128,34 +154,62 @@ void btgatts_register_app_cb(int status, int server_if, bt_uuid_t *uuid) {
}

void btgatts_connection_cb(int conn_id, int server_if, int connected, bt_bdaddr_t *bda) {
  gatt_connection_id = conn_id;
  for (int i = 0; i < 6; ++i) {
    remote_bd_addr.address[i] = bda->address[i];
  }
  CALLBACK_RET();
}

void btgatts_service_added_cb(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle) {
  gatt_status = status;
  gatt_server_interface = server_if;
  gatt_service_handle = srvc_handle;
  CALLBACK_RET();
}

void btgatts_included_service_added_cb(int status, int server_if, int srvc_handle, int incl_srvc_handle) {
  gatt_status = status;
  gatt_server_interface = server_if;
  gatt_service_handle = srvc_handle;
  gatt_included_service_handle = incl_srvc_handle;
  CALLBACK_RET();
}

void btgatts_characteristic_added_cb(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle) {
  gatt_status = status;
  gatt_server_interface = server_if;
  gatt_service_handle = srvc_handle;
  gatt_characteristic_handle = char_handle;
  CALLBACK_RET();
}

void btgatts_descriptor_added_cb(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle) {
  gatt_status = status;
  gatt_server_interface = server_if;
  gatt_service_handle = srvc_handle;
  gatt_descriptor_handle = descr_handle;
  CALLBACK_RET();
}

void btgatts_service_started_cb(int status, int server_if, int srvc_handle) {
  gatt_status = status;
  gatt_server_interface = server_if;
  gatt_service_handle = srvc_handle;
  CALLBACK_RET();
}

void btgatts_service_stopped_cb(int status, int server_if, int srvc_handle) {
  gatt_status = status;
  gatt_server_interface = server_if;
  gatt_service_handle = srvc_handle;
  CALLBACK_RET();
}

void btgatts_service_deleted_cb(int status, int server_if, int srvc_handle) {
  gatt_status = status;
  gatt_server_interface = server_if;
  gatt_service_handle = srvc_handle;
  CALLBACK_RET();
}

+5 −0
Original line number Diff line number Diff line
@@ -23,6 +23,11 @@
extern const btgatt_interface_t *gatt_interface;

bool gatt_init();
int gatt_get_connection_id();
int gatt_get_client_interface();
int gatt_get_server_interface();
int gatt_get_service_handle();
int gatt_get_included_service_handle();
int gatt_get_characteristic_handle();
int gatt_get_descriptor_handle();
int gatt_get_status();