Home | History | Annotate | Download | only in gatt
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2015 Google, Inc.
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 #include <stdlib.h>
     20 #include <time.h>
     21 #include <unistd.h>
     22 
     23 #include "gatt/gatt_test.h"
     24 
     25 #define DEFAULT_RANDOM_SEED 42
     26 
     27 namespace {
     28 
     29 static void create_random_uuid(bt_uuid_t *uuid, int seed) {
     30   srand(seed < 0 ? time(NULL) : seed);
     31   for (int i = 0; i < 16; ++i) {
     32     uuid->uu[i] = (uint8_t) (rand() % 256);
     33   }
     34 }
     35 
     36 }  // namespace
     37 
     38 namespace bttest {
     39 
     40 TEST_F(GattTest, GattClientRegister) {
     41   // Registers gatt client.
     42   bt_uuid_t gatt_client_uuid;
     43   create_random_uuid(&gatt_client_uuid, DEFAULT_RANDOM_SEED);
     44   gatt_client_interface()->register_client(&gatt_client_uuid);
     45   semaphore_wait(register_client_callback_sem_);
     46   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
     47     << "Error registering GATT client app callback.";
     48 
     49   // Unregisters gatt client. No callback is expected.
     50   gatt_client_interface()->unregister_client(client_interface_id());
     51 }
     52 
     53 TEST_F(GattTest, GattClientScanRemoteDevice) {
     54   // Starts BLE scan. NB: This test assumes there is a BLE beacon advertising nearby.
     55   gatt_client_interface()->scan(true);
     56   semaphore_wait(scan_result_callback_sem_);
     57 
     58   // Ends BLE scan. No callback is expected.
     59   gatt_client_interface()->scan(false);
     60 }
     61 
     62 TEST_F(GattTest, GattClientAdvertise) {
     63   // Registers a new client app.
     64   bt_uuid_t gatt_client_uuid;
     65   create_random_uuid(&gatt_client_uuid, DEFAULT_RANDOM_SEED);
     66   gatt_client_interface()->register_client(&gatt_client_uuid);
     67   semaphore_wait(register_client_callback_sem_);
     68   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
     69     << "Error registering GATT client app callback.";
     70 
     71   // Starts advertising.
     72   gatt_client_interface()->listen(client_interface_id(), true);
     73   semaphore_wait(listen_callback_sem_);
     74   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
     75     << "Error starting BLE advertisement.";
     76 
     77   // Stops advertising.
     78   gatt_client_interface()->listen(client_interface_id(), false);
     79   semaphore_wait(listen_callback_sem_);
     80   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
     81     << "Error stopping BLE advertisement.";
     82 
     83   // Unregisters gatt server. No callback is expected.
     84   gatt_client_interface()->unregister_client(client_interface_id());
     85 }
     86 
     87 TEST_F(GattTest, GattServerRegister) {
     88   // Registers gatt server.
     89   bt_uuid_t gatt_server_uuid;
     90   create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED);
     91   gatt_server_interface()->register_server(&gatt_server_uuid);
     92   semaphore_wait(register_server_callback_sem_);
     93   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
     94     << "Error registering GATT server app callback.";
     95 
     96   // Unregisters gatt server. No callback is expected.
     97   gatt_server_interface()->unregister_server(server_interface_id());
     98 }
     99 
    100 TEST_F(GattTest, GattServerBuild) {
    101   // Registers gatt server.
    102   bt_uuid_t gatt_server_uuid;
    103   create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED);
    104   gatt_server_interface()->register_server(&gatt_server_uuid);
    105   semaphore_wait(register_server_callback_sem_);
    106   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
    107     << "Error registering GATT server app callback.";
    108 
    109   // Service UUID.
    110   btgatt_srvc_id_t srvc_id;
    111   srvc_id.id.inst_id = 0;   // there is only one instance of this service.
    112   srvc_id.is_primary = 1;   // this service is primary.
    113   create_random_uuid(&srvc_id.id.uuid, -1);
    114 
    115   // Characteristics UUID.
    116   bt_uuid_t char_uuid;
    117   create_random_uuid(&char_uuid, -1);
    118 
    119   // Descriptor UUID.
    120   bt_uuid_t desc_uuid;
    121   create_random_uuid(&desc_uuid, -1);
    122 
    123   // Adds service.
    124   int server_if = server_interface_id();
    125   gatt_server_interface()->add_service(server_if, &srvc_id, 4 /* # handles */);
    126   semaphore_wait(service_added_callback_sem_);
    127   EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error adding service.";
    128 
    129   // Adds characteristics.
    130   int srvc_handle = service_handle();
    131   gatt_server_interface()->add_characteristic(server_if, srvc_handle,
    132       &char_uuid, 0x10 /* notification */, 0x01 /* read only */);
    133   semaphore_wait(characteristic_added_callback_sem_);
    134   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
    135       << "Error adding characteristics.";
    136 
    137   // Adds descriptor.
    138   gatt_server_interface()->add_descriptor(server_if, srvc_handle,
    139                                           &desc_uuid, 0x01);
    140   semaphore_wait(descriptor_added_callback_sem_);
    141   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
    142       << "Error adding descriptor.";
    143 
    144   // Starts server.
    145   gatt_server_interface()->start_service(server_if, srvc_handle, 2 /*BREDR/LE*/);
    146   semaphore_wait(service_started_callback_sem_);
    147   EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error starting server.";
    148 
    149   // Stops server.
    150   gatt_server_interface()->stop_service(server_if, srvc_handle);
    151   semaphore_wait(service_stopped_callback_sem_);
    152   EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error stopping server.";
    153 
    154   // Deletes service.
    155   gatt_server_interface()->delete_service(server_if, srvc_handle);
    156   semaphore_wait(service_deleted_callback_sem_);
    157   EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error deleting service.";
    158 
    159   // Unregisters gatt server. No callback is expected.
    160   gatt_server_interface()->unregister_server(server_if);
    161 }
    162 
    163 }  // bttest
    164