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

Commit 9a6abb1b authored by Scott James Remnant's avatar Scott James Remnant Committed by Joshua Schwarz
Browse files

DO NOT MERGE ANYWHERE net_test_bluetooth: replace with gtest variant

Bug: 25385380
Change-Id: Ia719363823d8d538caa6b42f076d5d6d737ae036
parent 55570fcf
Loading
Loading
Loading
Loading
+0 −39
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#pragma once

#include <malloc.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include <hardware/bluetooth.h>
#include <hardware/bt_gatt.h>
#include <hardware/bt_pan.h>
#include <hardware/bt_sock.h>
#include <hardware/hardware.h>

#ifndef ARRAY_SIZE
#  define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

#define TASSERT(c, ...) if (!(c)) { fprintf(stderr, "%s:%d: ", __func__, __LINE__); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); return false; }

extern const bt_interface_t *bt_interface;
extern bt_bdaddr_t bt_remote_bdaddr;
+0 −97
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include "base.h"
#include "support/adapter.h"
#include "support/callbacks.h"
#include "btcore/include/property.h"

bool adapter_enable_disable() {
  int error;

  CALL_AND_WAIT(error = bt_interface->enable(), adapter_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error enabling Bluetooth: %d", error);
  TASSERT(adapter_get_state() == BT_STATE_ON, "Adapter did not turn on.");

  CALL_AND_WAIT(error = bt_interface->disable(), adapter_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error disabling Bluetooth: %d", error);
  TASSERT(adapter_get_state() == BT_STATE_OFF, "Adapter did not turn off.");

  return true;
}

bool adapter_repeated_enable_disable() {
  int i;
  for (i = 0; i < 10; ++i) {
    if (!adapter_enable_disable()) {
      return false;
    }
  }
  return true;
}

bool adapter_set_name() {
  int error;
  bt_property_t *name = property_new_name("set_name");

  CALL_AND_WAIT(error = bt_interface->set_adapter_property(name), adapter_properties);
  TASSERT(error == BT_STATUS_SUCCESS, "Error setting device name.");
  TASSERT(adapter_get_property_count() == 1, "Expected 1 adapter property change, found %d instead.", adapter_get_property_count());
  TASSERT(adapter_get_property(BT_PROPERTY_BDNAME), "The Bluetooth name property did not change.");
  TASSERT(property_equals(adapter_get_property(BT_PROPERTY_BDNAME), name), "Bluetooth name '%s' does not match test value", property_as_name(adapter_get_property(BT_PROPERTY_BDNAME))->name);

  property_free(name);

  return true;
}

bool adapter_get_name() {
  int error;
  bt_property_t *name = property_new_name("get_name");

  CALL_AND_WAIT(bt_interface->set_adapter_property(name), adapter_properties);
  CALL_AND_WAIT(error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME), adapter_properties);
  TASSERT(error == BT_STATUS_SUCCESS, "Error getting device name.");
  TASSERT(adapter_get_property_count() == 1, "Expected 1 adapter property change, found %d instead.", adapter_get_property_count());
  TASSERT(adapter_get_property(BT_PROPERTY_BDNAME), "The Bluetooth name property did not change.");
  TASSERT(property_equals(adapter_get_property(BT_PROPERTY_BDNAME), name), "Bluetooth name '%s' does not match test value", property_as_name(adapter_get_property(BT_PROPERTY_BDNAME))->name);

  property_free(name);
  return true;
}

bool adapter_start_discovery() {
  int error;

  CALL_AND_WAIT(error = bt_interface->start_discovery(), discovery_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error calling start_discovery: %d", error);
  TASSERT(adapter_get_discovery_state() == BT_DISCOVERY_STARTED, "Unable to start discovery.");

  return true;
}

bool adapter_cancel_discovery() {
  int error;

  CALL_AND_WAIT(bt_interface->start_discovery(), discovery_state_changed);
  CALL_AND_WAIT(error = bt_interface->cancel_discovery(), discovery_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error calling cancel_discovery: %d", error);
  TASSERT(adapter_get_discovery_state() == BT_DISCOVERY_STOPPED, "Unable to stop discovery.");

  return true;
}
+0 −71
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include "base.h"
#include "cases/cases.h"

TEST_CASE_DECL(adapter_enable_disable);
TEST_CASE_DECL(adapter_repeated_enable_disable);
TEST_CASE_DECL(adapter_set_name);
TEST_CASE_DECL(adapter_get_name);
TEST_CASE_DECL(adapter_start_discovery);
TEST_CASE_DECL(adapter_cancel_discovery);

TEST_CASE_DECL(rfcomm_connect);
TEST_CASE_DECL(rfcomm_repeated_connect);

TEST_CASE_DECL(pan_enable);
TEST_CASE_DECL(pan_connect);
TEST_CASE_DECL(pan_disconnect);
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);
TEST_CASE_DECL(gatt_server_build);

// These are run with the Bluetooth adapter disabled.
const test_case_t sanity_suite[] = {
  TEST_CASE(adapter_enable_disable),
  TEST_CASE(adapter_repeated_enable_disable)
};

// The normal test suite is run with the adapter enabled.
const test_case_t test_suite[] = {
  TEST_CASE(adapter_set_name),
  TEST_CASE(adapter_get_name),
  TEST_CASE(adapter_start_discovery),
  TEST_CASE(adapter_cancel_discovery),

  TEST_CASE(rfcomm_connect),
  TEST_CASE(rfcomm_repeated_connect),

  TEST_CASE(pan_enable),
  TEST_CASE(pan_connect),
  TEST_CASE(pan_disconnect),

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

const size_t sanity_suite_size = ARRAY_SIZE(sanity_suite);
const size_t test_suite_size = ARRAY_SIZE(test_suite);
+0 −34
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#pragma once

#include "base.h"

#define TEST_CASE_DECL(x) bool x()
#define TEST_CASE(x) { x, #x }

typedef struct {
  bool (*function)();
  const char *function_name;
} test_case_t;

extern const test_case_t test_suite[];
extern const test_case_t sanity_suite[];
extern const size_t test_suite_size;
extern const size_t sanity_suite_size;
+0 −154
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "base.h"
#include "support/gatt.h"
#include "support/callbacks.h"

#define DEFAULT_RANDOM_SEED 42

static void create_random_uuid(bt_uuid_t *uuid, int seed) {
  srand(seed < 0 ? time(NULL) : seed);
  int i;
  for (i = 0; i < 16; ++i) {
    uuid->uu[i] = (uint8_t) (rand() % 256);
  }
}

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

  // Registers gatt client.
  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.
  gatt_interface->client->unregister_client(gatt_get_client_interface());

  return true;
}

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

  // Starts BLE scan. NB: This test assumes there is a BLE beacon advertising nearby.
  CALL_AND_WAIT(gatt_interface->client->scan(true), btgattc_scan_result_cb);

  // Ends BLE scan. No callback is expected.
  gatt_interface->client->scan(false);

  return true;
}

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

  // Registers a new client app.
  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.
  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.
  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.
  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;
}
Loading