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

Commit 99fa292f authored by Steve Gu's avatar Steve Gu Committed by Andre Eisenbach
Browse files

Adds tests for BLE advertising and BLE server register. Also fixes random seed.

Change-Id: Ic15360baefb133b761ff077d27946c0ea7fa3edb
parent d3132bc4
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ TEST_CASE_DECL(pan_quick_reconnect);

TEST_CASE_DECL(gatt_client_register);
TEST_CASE_DECL(gatt_client_scan);
TEST_CASE_DECL(gatt_client_advertise);
TEST_CASE_DECL(gatt_server_register);

// These are run with the Bluetooth adapter disabled.
const test_case_t sanity_suite[] = {
@@ -58,7 +60,9 @@ const test_case_t test_suite[] = {
  TEST_CASE(pan_disconnect),

  TEST_CASE(gatt_client_register),
  TEST_CASE(gatt_client_scan)
  TEST_CASE(gatt_client_scan),
  TEST_CASE(gatt_client_advertise),
  TEST_CASE(gatt_server_register)
};

const size_t sanity_suite_size = ARRAY_SIZE(sanity_suite);
+38 −3
Original line number Diff line number Diff line
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "base.h"
#include "support/gatt.h"
@@ -8,7 +9,7 @@
bt_uuid_t app_uuid;

static void assign_random_app_uuid(bt_uuid_t *uuid) {
  srand(time(NULL));
  srand(42);
  for (int i = 0; i < 16; ++i) {
    uuid->uu[i] = (uint8_t) (rand() % 256);
  }
@@ -19,9 +20,8 @@ bool gatt_client_register() {

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

  // Unregisters gatt client. No callback is expected.
  gatt_interface->client->unregister_client(gatt_get_client_interface());
@@ -40,3 +40,38 @@ bool gatt_client_scan() {

  return true;
}

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);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error registering GATT client app callback.");

  // Starts advertising.
  CALL_AND_WAIT(gatt_interface->client->listen(gatt_get_client_interface(), true), btgattc_advertise_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error starting BLE advertisement.");

  // Stops advertising.
  CALL_AND_WAIT(gatt_interface->client->listen(gatt_get_client_interface(), false), btgattc_advertise_cb);
  TASSERT(gatt_get_status() == BT_STATUS_SUCCESS, "Error stopping BLE advertisement.");

  // Unregisters gatt server. No callback is expected.
  gatt_interface->client->unregister_client(gatt_get_client_interface());

  return true;
}

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);
  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;
}
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@

const btgatt_interface_t *gatt_interface;
static int gatt_client_interface;
static int gatt_server_interface;
static int gatt_status;

bool gatt_init() {
@@ -34,6 +35,10 @@ int gatt_get_client_interface() {
  return gatt_client_interface;
}

int gatt_get_server_interface() {
  return gatt_server_interface;
}

int gatt_get_status() {
  return gatt_status;
}
@@ -110,11 +115,15 @@ void btgattc_remote_rssi_cb(int client_if,bt_bdaddr_t* bda, int rssi, int status
}

void btgattc_advertise_cb(int status, int client_if) {
  gatt_status = status;
  gatt_client_interface = client_if;
  CALLBACK_RET();
}

// GATT server callbacks
void btgatts_register_app_cb(int status, int server_if, bt_uuid_t *uuid) {
  gatt_status = status;
  gatt_server_interface = server_if;
  CALLBACK_RET();
}

+2 −1
Original line number Diff line number Diff line
@@ -24,4 +24,5 @@ extern const btgatt_interface_t *gatt_interface;

bool gatt_init();
int gatt_get_client_interface();
int gatt_get_server_interface();
int gatt_get_status();