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