1 /******************************************************************************
2  *
3  *  Copyright 2018 The Android Open Source Project
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 <gtest/gtest.h>
20 
21 #include <iterator>
22 #include <utility>
23 
24 #include "gatt/database_builder.h"
25 #include "types/bluetooth/uuid.h"
26 
27 using bluetooth::Uuid;
28 
29 namespace gatt {
30 
31 namespace {
32 /* EXPECT_EQ doesn't work well with static constexpr fields, need a variable
33  * with address */
34 constexpr std::pair<uint16_t, uint16_t> EXPLORE_END = DatabaseBuilder::EXPLORE_END;
35 
36 /* make_pair doesn't work well with EXPECT_EQ, have own helper instead */
make_pair_u16(uint16_t first,uint16_t second)37 inline std::pair<uint16_t, uint16_t> make_pair_u16(uint16_t first, uint16_t second) {
38   return std::make_pair(first, second);
39 }
40 
41 // clang-format off
42 Uuid SERVICE_1_UUID = Uuid::FromString("00001800-0000-1000-8000-00805f9b34fb");
43 Uuid SERVICE_2_UUID = Uuid::FromString("00001801-0000-1000-8000-00805f9b34fb");
44 Uuid SERVICE_3_UUID = Uuid::FromString("0000180f-0000-1000-8000-00805f9b34fb");
45 Uuid SERVICE_4_UUID = Uuid::FromString("0000fef5-0000-1000-8000-00805f9b34fb");
46 Uuid SERVICE_5_UUID = Uuid::FromString("0000180a-0000-1000-8000-00805f9b34fb");
47 Uuid SERVICE_6_UUID = Uuid::FromString("0000fe55-0000-1000-8000-00805f9b34fb");
48 
49 Uuid SERVICE_1_CHAR_1_UUID = Uuid::FromString("00002a00-0000-1000-8000-00805f9b34fb");
50 Uuid SERVICE_1_CHAR_2_UUID = Uuid::FromString("00002a01-0000-1000-8000-00805f9b34fb");
51 Uuid SERVICE_1_CHAR_3_UUID = Uuid::FromString("00002a04-0000-1000-8000-00805f9b34fb");
52 
53 Uuid SERVICE_3_CHAR_1_UUID = Uuid::FromString("00002a19-0000-1000-8000-00805f9b34fb");
54 Uuid SERVICE_3_CHAR_1_DESC_1_UUID = Uuid::FromString("00002902-0000-1000-8000-00805f9b34fb");
55 
56 Uuid SERVICE_4_CHAR_1_UUID = Uuid::FromString("8082caa8-41a6-4021-91c6-56f9b954cc34");
57 Uuid SERVICE_4_CHAR_2_UUID = Uuid::FromString("724249f0-5ec3-4b5f-8804-42345af08651");
58 Uuid SERVICE_4_CHAR_3_UUID = Uuid::FromString("6c53db25-47a1-45fe-a022-7c92fb334fd4");
59 Uuid SERVICE_4_CHAR_4_UUID = Uuid::FromString("9d84b9a3-000c-49d8-9183-855b673fda31");
60 Uuid SERVICE_4_CHAR_5_UUID = Uuid::FromString("457871e8-d516-4ca1-9116-57d0b17b9cb2");
61 Uuid SERVICE_4_CHAR_6_UUID = Uuid::FromString("5f78df94-798c-46f5-990a-b3eb6a065c88");
62 Uuid SERVICE_4_CHAR_6_DESC_1_UUID = Uuid::FromString("00002902-0000-1000-8000-00805f9b34fb");
63 
64 Uuid SERVICE_5_CHAR_1_UUID = Uuid::FromString("00002a29-0000-1000-8000-00805f9b34fb");
65 Uuid SERVICE_5_CHAR_2_UUID = Uuid::FromString("00002a24-0000-1000-8000-00805f9b34fb");
66 Uuid SERVICE_5_CHAR_3_UUID = Uuid::FromString("00002a25-0000-1000-8000-00805f9b34fb");
67 Uuid SERVICE_5_CHAR_4_UUID = Uuid::FromString("00002a27-0000-1000-8000-00805f9b34fb");
68 Uuid SERVICE_5_CHAR_5_UUID = Uuid::FromString("00002a26-0000-1000-8000-00805f9b34fb");
69 Uuid SERVICE_5_CHAR_6_UUID = Uuid::FromString("00002a28-0000-1000-8000-00805f9b34fb");
70 Uuid SERVICE_5_CHAR_7_UUID = Uuid::FromString("00002a50-0000-1000-8000-00805f9b34fb");
71 
72 Uuid SERVICE_6_CHAR_1_UUID = Uuid::FromString("00000001-1000-1000-8000-00805f9b34fb");
73 Uuid SERVICE_6_CHAR_1_DESC_1_UUID = Uuid::FromString("00002902-0000-1000-8000-00805f9b34fb");
74 Uuid SERVICE_6_CHAR_2_UUID = Uuid::FromString("00000002-1000-1000-8000-00805f9b34fb");
75 Uuid SERVICE_6_CHAR_3_UUID = Uuid::FromString("00000003-1000-1000-8000-00805f9b34fb");
76 // clang-format on
77 
78 }  // namespace
79 
80 // clang-format off
81 /* Content of sample database, comes from Daydream controller:
82 Service: handle=0x0001, end_handle=0x0007, uuid=00001800-0000-1000-8000-00805f9b34fb
83 	 Characteristic: declaration_handle=0x0002, value_handle=0x0003, uuid=00002a00-0000-1000-8000-00805f9b34fb, prop=0x02
84 	 Characteristic: declaration_handle=0x0004, value_handle=0x0005, uuid=00002a01-0000-1000-8000-00805f9b34fb, prop=0x02
85 	 Characteristic: declaration_handle=0x0006, value_handle=0x0007, uuid=00002a04-0000-1000-8000-00805f9b34fb, prop=0x02
86 Service: handle=0x0008, end_handle=0x0008, uuid=00001801-0000-1000-8000-00805f9b34fb
87 Service: handle=0x0009, end_handle=0x000c, uuid=0000180f-0000-1000-8000-00805f9b34fb
88 	 Characteristic: declaration_handle=0x000a, value_handle=0x000b, uuid=00002a19-0000-1000-8000-00805f9b34fb, prop=0x12
89 		 Descriptor: handle=0x000c, uuid=00002902-0000-1000-8000-00805f9b34fb
90 Service: handle=0x000d, end_handle=0x001a, uuid=0000fef5-0000-1000-8000-00805f9b34fb
91 	 Characteristic: declaration_handle=0x000e, value_handle=0x000f, uuid=8082caa8-41a6-4021-91c6-56f9b954cc34, prop=0x0a
92 	 Characteristic: declaration_handle=0x0010, value_handle=0x0011, uuid=724249f0-5ec3-4b5f-8804-42345af08651, prop=0x0a
93 	 Characteristic: declaration_handle=0x0012, value_handle=0x0013, uuid=6c53db25-47a1-45fe-a022-7c92fb334fd4, prop=0x02
94 	 Characteristic: declaration_handle=0x0014, value_handle=0x0015, uuid=9d84b9a3-000c-49d8-9183-855b673fda31, prop=0x0a
95 	 Characteristic: declaration_handle=0x0016, value_handle=0x0017, uuid=457871e8-d516-4ca1-9116-57d0b17b9cb2, prop=0x0e
96 	 Characteristic: declaration_handle=0x0018, value_handle=0x0019, uuid=5f78df94-798c-46f5-990a-b3eb6a065c88, prop=0x12
97 		 Descriptor: handle=0x001a, uuid=00002902-0000-1000-8000-00805f9b34fb
98 Service: handle=0x001b, end_handle=0x0029, uuid=0000180a-0000-1000-8000-00805f9b34fb
99 	 Characteristic: declaration_handle=0x001c, value_handle=0x001d, uuid=00002a29-0000-1000-8000-00805f9b34fb, prop=0x02
100 	 Characteristic: declaration_handle=0x001e, value_handle=0x001f, uuid=00002a24-0000-1000-8000-00805f9b34fb, prop=0x02
101 	 Characteristic: declaration_handle=0x0020, value_handle=0x0021, uuid=00002a25-0000-1000-8000-00805f9b34fb, prop=0x02
102 	 Characteristic: declaration_handle=0x0022, value_handle=0x0023, uuid=00002a27-0000-1000-8000-00805f9b34fb, prop=0x02
103 	 Characteristic: declaration_handle=0x0024, value_handle=0x0025, uuid=00002a26-0000-1000-8000-00805f9b34fb, prop=0x02
104 	 Characteristic: declaration_handle=0x0026, value_handle=0x0027, uuid=00002a28-0000-1000-8000-00805f9b34fb, prop=0x02
105 	 Characteristic: declaration_handle=0x0028, value_handle=0x0029, uuid=00002a50-0000-1000-8000-00805f9b34fb, prop=0x02
106 Service: handle=0x002a, end_handle=0x0031, uuid=0000fe55-0000-1000-8000-00805f9b34fb
107 	 Characteristic: declaration_handle=0x002b, value_handle=0x002c, uuid=00000001-1000-1000-8000-00805f9b34fb, prop=0x10
108 		 Descriptor: handle=0x002d, uuid=00002902-0000-1000-8000-00805f9b34fb
109 	 Characteristic: declaration_handle=0x002e, value_handle=0x002f, uuid=00000002-1000-1000-8000-00805f9b34fb, prop=0x08
110 	 Characteristic: declaration_handle=0x0030, value_handle=0x0031, uuid=00000003-1000-1000-8000-00805f9b34fb, prop=0x02
111 */
112 // clang-format on
113 
114 /* This test verifies that DatabaseBuilder will properly discover database
115  * content from a remote device. It also verify that after the discovery is
116  * done, returned database is equal to the discovered one */
TEST(DatabaseBuilderSampleDeviceTest,DoDiscovery)117 TEST(DatabaseBuilderSampleDeviceTest, DoDiscovery) {
118   DatabaseBuilder builder;
119 
120   EXPECT_FALSE(builder.InProgress());
121 
122   // At start of discovery, builder will receive All services in order from
123   // lower layers.
124   builder.AddService(0x0001, 0x0007, SERVICE_1_UUID, true);
125 
126   // The moment we receive first service, we are in progress
127   // TODO: we should be able to set InProgress state once we sent GATT request,
128   // not when it's back and parsed
129   EXPECT_TRUE(builder.InProgress());
130 
131   builder.AddService(0x0008, 0x0008, SERVICE_2_UUID, true);
132   builder.AddService(0x0009, 0x000c, SERVICE_3_UUID, true);
133   builder.AddService(0x000d, 0x001a, SERVICE_4_UUID, true);
134   builder.AddService(0x001b, 0x0029, SERVICE_5_UUID, true);
135   builder.AddService(0x002a, 0x0031, SERVICE_6_UUID, true);
136 
137   // At this moment, all services are received, stack will grab them one and one
138   // to discover their content.
139   EXPECT_TRUE(builder.StartNextServiceExploration());
140 
141   // Grabbing first service, to start Included Service and Characteristic
142   // discovery
143   EXPECT_EQ(builder.CurrentlyExploredService(), make_pair_u16(0x0001, 0x0007));
144 
145   builder.AddCharacteristic(0x0002, 0x0003, SERVICE_1_CHAR_1_UUID, 0x02);
146   builder.AddCharacteristic(0x0004, 0x0005, SERVICE_1_CHAR_2_UUID, 0x02);
147   builder.AddCharacteristic(0x0006, 0x0007, SERVICE_1_CHAR_3_UUID, 0x02);
148 
149   // All characteristics were discovered, stack will try to look for
150   // descriptors. Since there is no space for descriptors, builder should return
151   // nothing more to discover
152   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), EXPLORE_END);
153 
154   // Service with handles 0x0008, 0x0008 is skipped for exploration - we know
155   // it's empty.
156   EXPECT_TRUE(builder.StartNextServiceExploration());
157   EXPECT_EQ(builder.CurrentlyExploredService(), make_pair_u16(0x0009, 0x000c));
158 
159   builder.AddCharacteristic(0x000a, 0x000b, SERVICE_3_CHAR_1_UUID, 0x12);
160 
161   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), make_pair_u16(0x000c, 0x000c));
162 
163   builder.AddDescriptor(0x000c, SERVICE_3_CHAR_1_DESC_1_UUID);
164 
165   // All descriptors were explored
166   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), EXPLORE_END);
167 
168   EXPECT_TRUE(builder.StartNextServiceExploration());
169   EXPECT_EQ(builder.CurrentlyExploredService(), make_pair_u16(0x000d, 0x001a));
170 
171   builder.AddCharacteristic(0x000e, 0x000f, SERVICE_4_CHAR_1_UUID, 0x0a);
172   builder.AddCharacteristic(0x0010, 0x0011, SERVICE_4_CHAR_2_UUID, 0x0a);
173   builder.AddCharacteristic(0x0012, 0x0013, SERVICE_4_CHAR_3_UUID, 0x02);
174   builder.AddCharacteristic(0x0014, 0x0015, SERVICE_4_CHAR_4_UUID, 0x0a);
175   builder.AddCharacteristic(0x0016, 0x0017, SERVICE_4_CHAR_5_UUID, 0x0e);
176   builder.AddCharacteristic(0x0018, 0x0019, SERVICE_4_CHAR_6_UUID, 0x12);
177 
178   // Just last Characteristic have space for descriptor
179   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), make_pair_u16(0x001a, 0x001a));
180 
181   builder.AddDescriptor(0x001a, SERVICE_4_CHAR_6_DESC_1_UUID);
182 
183   // All descriptors were explored
184   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), EXPLORE_END);
185 
186   EXPECT_TRUE(builder.StartNextServiceExploration());
187   EXPECT_EQ(builder.CurrentlyExploredService(), make_pair_u16(0x001b, 0x0029));
188 
189   builder.AddCharacteristic(0x001c, 0x001d, SERVICE_5_CHAR_1_UUID, 0x02);
190   builder.AddCharacteristic(0x001e, 0x001f, SERVICE_5_CHAR_2_UUID, 0x02);
191   builder.AddCharacteristic(0x0020, 0x0021, SERVICE_5_CHAR_3_UUID, 0x02);
192   builder.AddCharacteristic(0x0022, 0x0023, SERVICE_5_CHAR_4_UUID, 0x02);
193   builder.AddCharacteristic(0x0024, 0x0025, SERVICE_5_CHAR_5_UUID, 0x02);
194   builder.AddCharacteristic(0x0026, 0x0027, SERVICE_5_CHAR_6_UUID, 0x02);
195   builder.AddCharacteristic(0x0028, 0x0029, SERVICE_5_CHAR_7_UUID, 0x02);
196 
197   // No space for descriptors
198   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), EXPLORE_END);
199 
200   EXPECT_TRUE(builder.StartNextServiceExploration());
201   EXPECT_EQ(builder.CurrentlyExploredService(), make_pair_u16(0x002a, 0x0031));
202 
203   builder.AddCharacteristic(0x002b, 0x002c, SERVICE_6_CHAR_1_UUID, 0x10);
204   builder.AddCharacteristic(0x002e, 0x002f, SERVICE_6_CHAR_2_UUID, 0x08);
205   builder.AddCharacteristic(0x0030, 0x0031, SERVICE_6_CHAR_3_UUID, 0x02);
206 
207   // Just one Characteristic have space for descriptor
208   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), make_pair_u16(0x002d, 0x002d));
209 
210   builder.AddDescriptor(0x002d, SERVICE_6_CHAR_1_DESC_1_UUID);
211 
212   // All descriptors were explored
213   EXPECT_EQ(builder.NextDescriptorRangeToExplore(), EXPLORE_END);
214 
215   EXPECT_FALSE(builder.StartNextServiceExploration());
216 
217   EXPECT_TRUE(builder.InProgress());
218   Database result = builder.Build();
219   EXPECT_FALSE(builder.InProgress());
220 
221   // verify that the returned database matches what was discovered
222   auto service = result.Services().begin();
223   EXPECT_EQ(service->handle, 0x0001);
224   EXPECT_EQ(service->uuid, SERVICE_1_UUID);
225 
226   EXPECT_EQ(service->characteristics[0].uuid, SERVICE_1_CHAR_1_UUID);
227   EXPECT_EQ(service->characteristics[1].uuid, SERVICE_1_CHAR_2_UUID);
228   EXPECT_EQ(service->characteristics[2].uuid, SERVICE_1_CHAR_3_UUID);
229 
230   service++;
231   EXPECT_EQ(service->uuid, SERVICE_2_UUID);
232 
233   service++;
234   EXPECT_EQ(service->uuid, SERVICE_3_UUID);
235   EXPECT_EQ(service->characteristics[0].uuid, SERVICE_3_CHAR_1_UUID);
236   EXPECT_EQ(service->characteristics[0].descriptors[0].uuid, SERVICE_3_CHAR_1_DESC_1_UUID);
237 
238   service++;
239   EXPECT_EQ(service->uuid, SERVICE_4_UUID);
240   EXPECT_EQ(service->characteristics[0].uuid, SERVICE_4_CHAR_1_UUID);
241   EXPECT_EQ(service->characteristics[1].uuid, SERVICE_4_CHAR_2_UUID);
242   EXPECT_EQ(service->characteristics[2].uuid, SERVICE_4_CHAR_3_UUID);
243   EXPECT_EQ(service->characteristics[3].uuid, SERVICE_4_CHAR_4_UUID);
244   EXPECT_EQ(service->characteristics[4].uuid, SERVICE_4_CHAR_5_UUID);
245   EXPECT_EQ(service->characteristics[5].uuid, SERVICE_4_CHAR_6_UUID);
246   EXPECT_EQ(service->characteristics[5].descriptors[0].uuid, SERVICE_4_CHAR_6_DESC_1_UUID);
247 
248   service++;
249   EXPECT_EQ(service->uuid, SERVICE_5_UUID);
250   EXPECT_EQ(service->characteristics[0].uuid, SERVICE_5_CHAR_1_UUID);
251   EXPECT_EQ(service->characteristics[1].uuid, SERVICE_5_CHAR_2_UUID);
252   EXPECT_EQ(service->characteristics[2].uuid, SERVICE_5_CHAR_3_UUID);
253   EXPECT_EQ(service->characteristics[3].uuid, SERVICE_5_CHAR_4_UUID);
254   EXPECT_EQ(service->characteristics[4].uuid, SERVICE_5_CHAR_5_UUID);
255   EXPECT_EQ(service->characteristics[5].uuid, SERVICE_5_CHAR_6_UUID);
256   EXPECT_EQ(service->characteristics[6].uuid, SERVICE_5_CHAR_7_UUID);
257 
258   service++;
259   EXPECT_EQ(service->uuid, SERVICE_6_UUID);
260   EXPECT_EQ(service->characteristics[0].uuid, SERVICE_6_CHAR_1_UUID);
261   EXPECT_EQ(service->characteristics[0].descriptors[0].uuid, SERVICE_6_CHAR_1_DESC_1_UUID);
262   EXPECT_EQ(service->characteristics[1].uuid, SERVICE_6_CHAR_2_UUID);
263   EXPECT_EQ(service->characteristics[2].uuid, SERVICE_6_CHAR_3_UUID);
264 }
265 
266 }  // namespace gatt
267