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