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