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 {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = srvc_uuid},
73 {.type = BTGATT_DB_CHARACTERISTIC,
74 .uuid = char_uuid,
75 .properties = 0x10 /* notification */,
76 .permissions = 0x01 /* read only */},
77 {.type = BTGATT_DB_DESCRIPTOR, .uuid = desc_uuid, .permissions = 0x01}};
78
79 gatt_server_interface()->add_service(server_if, service);
80 semaphore_wait(service_added_callback_sem_);
81 EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error adding service.";
82 EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if added.";
83 int service_handle_added = service_handle();
84
85 // Stops server.
86 gatt_server_interface()->stop_service(server_if, service_handle());
87 semaphore_wait(service_stopped_callback_sem_);
88 EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error stopping server.";
89 EXPECT_TRUE(service_handle() == service_handle_added)
90 << "Wrong service handle stopped.";
91 EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if stopped.";
92
93 // Deletes service.
94 gatt_server_interface()->delete_service(server_if, service_handle());
95 semaphore_wait(service_deleted_callback_sem_);
96 EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error deleting service.";
97 EXPECT_TRUE(service_handle() == service_handle_added)
98 << "Wrong service handle deleted.";
99 EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if deleted.";
100
101 // Unregisters gatt server. No callback is expected.
102 gatt_server_interface()->unregister_server(server_if);
103 }
104
105 } // bttest
106