1 /******************************************************************************
2  *
3  *  Copyright 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 #include "gd/os/rand.h"
25 #include "types/bluetooth/uuid.h"
26 
27 namespace bttest {
28 
TEST_F(GattTest,GattClientRegister)29 TEST_F(GattTest, GattClientRegister) {
30   // Registers gatt client.
31   bluetooth::Uuid gatt_client_uuid = bluetooth::Uuid::From128BitBE(
32       bluetooth::os::GenerateRandom<bluetooth::Uuid::kNumBytes128>());
33   gatt_client_interface()->register_client(gatt_client_uuid, false);
34   semaphore_wait(register_client_callback_sem_);
35   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
36       << "Error registering GATT client app callback.";
37 
38   // Unregisters gatt client. No callback is expected.
39   gatt_client_interface()->unregister_client(client_interface_id());
40 }
41 
TEST_F(GattTest,GattServerRegister)42 TEST_F(GattTest, GattServerRegister) {
43   // Registers gatt server.
44   bluetooth::Uuid gatt_server_uuid = bluetooth::Uuid::From128BitBE(
45       bluetooth::os::GenerateRandom<bluetooth::Uuid::kNumBytes128>());
46   gatt_server_interface()->register_server(gatt_server_uuid, false);
47   semaphore_wait(register_server_callback_sem_);
48   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
49       << "Error registering GATT server app callback.";
50 
51   // Unregisters gatt server. No callback is expected.
52   gatt_server_interface()->unregister_server(server_interface_id());
53 }
54 
TEST_F(GattTest,GattServerBuild)55 TEST_F(GattTest, GattServerBuild) {
56   // Registers gatt server.
57   bluetooth::Uuid gatt_server_uuid = bluetooth::Uuid::From128BitBE(
58       bluetooth::os::GenerateRandom<bluetooth::Uuid::kNumBytes128>());
59   gatt_server_interface()->register_server(gatt_server_uuid, false);
60   semaphore_wait(register_server_callback_sem_);
61   EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
62       << "Error registering GATT server app callback.";
63 
64   // Service UUID.
65   bluetooth::Uuid srvc_uuid = bluetooth::Uuid::From128BitBE(
66       bluetooth::os::GenerateRandom<bluetooth::Uuid::kNumBytes128>());
67 
68   // Characteristics UUID.
69   bluetooth::Uuid char_uuid = bluetooth::Uuid::From128BitBE(
70       bluetooth::os::GenerateRandom<bluetooth::Uuid::kNumBytes128>());
71 
72   // Descriptor UUID.
73   bluetooth::Uuid desc_uuid = bluetooth::Uuid::From128BitBE(
74       bluetooth::os::GenerateRandom<bluetooth::Uuid::kNumBytes128>());
75 
76   // Adds service.
77   int server_if = server_interface_id();
78 
79   std::vector<btgatt_db_element_t> service = {
80       {
81           .uuid = srvc_uuid,
82           .type = BTGATT_DB_PRIMARY_SERVICE,
83       },
84       {
85           .uuid = char_uuid,
86           .type = BTGATT_DB_CHARACTERISTIC,
87           .properties = 0x10,  /* notification */
88           .permissions = 0x01, /* read only */
89       },
90       {
91           .uuid = desc_uuid,
92           .type = BTGATT_DB_DESCRIPTOR,
93           .permissions = 0x01,
94       }};
95 
96   gatt_server_interface()->add_service(server_if, service.data(),
97                                        service.size());
98   semaphore_wait(service_added_callback_sem_);
99   EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error adding service.";
100   EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if added.";
101   int service_handle_added = service_handle();
102 
103   // Stops server.
104   gatt_server_interface()->stop_service(server_if, service_handle());
105   semaphore_wait(service_stopped_callback_sem_);
106   EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error stopping server.";
107   EXPECT_TRUE(service_handle() == service_handle_added)
108       << "Wrong service handle stopped.";
109   EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if stopped.";
110 
111   // Deletes service.
112   gatt_server_interface()->delete_service(server_if, service_handle());
113   semaphore_wait(service_deleted_callback_sem_);
114   EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error deleting service.";
115   EXPECT_TRUE(service_handle() == service_handle_added)
116       << "Wrong service handle deleted.";
117   EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if deleted.";
118 
119   // Unregisters gatt server. No callback is expected.
120   gatt_server_interface()->unregister_server(server_if);
121 }
122 
123 }  // bttest
124