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
create_random_uuid(bt_uuid_t * uuid,int seed)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
TEST_F(GattTest,GattClientRegister)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
TEST_F(GattTest,GattServerRegister)53 TEST_F(GattTest, GattServerRegister) {
54 // Registers gatt server.
55 bt_uuid_t gatt_server_uuid;
56 create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED);
57 gatt_server_interface()->register_server(&gatt_server_uuid);
58 semaphore_wait(register_server_callback_sem_);
59 EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
60 << "Error registering GATT server app callback.";
61
62 // Unregisters gatt server. No callback is expected.
63 gatt_server_interface()->unregister_server(server_interface_id());
64 }
65
TEST_F(GattTest,GattServerBuild)66 TEST_F(GattTest, GattServerBuild) {
67 // Registers gatt server.
68 bt_uuid_t gatt_server_uuid;
69 create_random_uuid(&gatt_server_uuid, DEFAULT_RANDOM_SEED);
70 gatt_server_interface()->register_server(&gatt_server_uuid);
71 semaphore_wait(register_server_callback_sem_);
72 EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
73 << "Error registering GATT server app callback.";
74
75 // Service UUID.
76 bt_uuid_t srvc_uuid;
77 create_random_uuid(&srvc_uuid, -1);
78
79 // Characteristics UUID.
80 bt_uuid_t char_uuid;
81 create_random_uuid(&char_uuid, -1);
82
83 // Descriptor UUID.
84 bt_uuid_t desc_uuid;
85 create_random_uuid(&desc_uuid, -1);
86
87 // Adds service.
88 int server_if = server_interface_id();
89
90 std::vector<btgatt_db_element_t> service = {
91 {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = srvc_uuid},
92 {.type = BTGATT_DB_CHARACTERISTIC,
93 .uuid = char_uuid,
94 .properties = 0x10 /* notification */,
95 .permissions = 0x01 /* read only */},
96 {.type = BTGATT_DB_DESCRIPTOR, .uuid = desc_uuid, .permissions = 0x01}};
97
98 gatt_server_interface()->add_service(server_if, service);
99 semaphore_wait(service_added_callback_sem_);
100 EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error adding service.";
101
102 // Stops server.
103 gatt_server_interface()->stop_service(server_if, service_handle());
104 semaphore_wait(service_stopped_callback_sem_);
105 EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error stopping server.";
106
107 // Deletes service.
108 gatt_server_interface()->delete_service(server_if, service_handle());
109 semaphore_wait(service_deleted_callback_sem_);
110 EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error deleting service.";
111
112 // Unregisters gatt server. No callback is expected.
113 gatt_server_interface()->unregister_server(server_if);
114 }
115
116 } // bttest
117