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 "database.h"
20
21 #include <base/logging.h>
22
23 #include <algorithm>
24 #include <list>
25 #include <memory>
26 #include <sstream>
27
28 #include "bt_trace.h"
29 #include "stack/crypto_toolbox/crypto_toolbox.h"
30 #include "stack/include/gattdefs.h"
31 #include "types/bluetooth/uuid.h"
32
33 using bluetooth::Uuid;
34
35 namespace gatt {
36
37 namespace {
38 const Uuid PRIMARY_SERVICE = Uuid::From16Bit(GATT_UUID_PRI_SERVICE);
39 const Uuid SECONDARY_SERVICE = Uuid::From16Bit(GATT_UUID_SEC_SERVICE);
40 const Uuid INCLUDE = Uuid::From16Bit(GATT_UUID_INCLUDE_SERVICE);
41 const Uuid CHARACTERISTIC = Uuid::From16Bit(GATT_UUID_CHAR_DECLARE);
42 const Uuid CHARACTERISTIC_EXTENDED_PROPERTIES =
43 Uuid::From16Bit(GATT_UUID_CHAR_EXT_PROP);
44
HandleInRange(const Service & svc,uint16_t handle)45 bool HandleInRange(const Service& svc, uint16_t handle) {
46 return handle >= svc.handle && handle <= svc.end_handle;
47 }
48 } // namespace
49
UuidSize(const Uuid & uuid)50 static size_t UuidSize(const Uuid& uuid) {
51 size_t len = uuid.GetShortestRepresentationSize();
52 return (len == Uuid::kNumBytes32) ? Uuid::kNumBytes128 : len;
53 }
54
FindService(std::list<Service> & services,uint16_t handle)55 Service* FindService(std::list<Service>& services, uint16_t handle) {
56 for (Service& service : services) {
57 if (handle >= service.handle && handle <= service.end_handle)
58 return &service;
59 }
60
61 return nullptr;
62 }
63
ToString() const64 std::string Database::ToString() const {
65 std::stringstream tmp;
66
67 for (const Service& service : services) {
68 tmp << "Service: handle=" << loghex(service.handle)
69 << ", end_handle=" << loghex(service.end_handle)
70 << ", uuid=" << service.uuid << "\n";
71
72 for (const auto& is : service.included_services) {
73 tmp << "\t Included service: handle=" << loghex(is.handle)
74 << ", start_handle=" << loghex(is.start_handle)
75 << ", end_handle=" << loghex(is.end_handle) << ", uuid=" << is.uuid
76 << "\n";
77 }
78
79 for (const Characteristic& c : service.characteristics) {
80 tmp << "\t Characteristic: declaration_handle="
81 << loghex(c.declaration_handle)
82 << ", value_handle=" << loghex(c.value_handle) << ", uuid=" << c.uuid
83 << ", prop=" << loghex(c.properties) << "\n";
84
85 for (const Descriptor& d : c.descriptors) {
86 tmp << "\t\t Descriptor: handle=" << loghex(d.handle)
87 << ", uuid=" << d.uuid << "\n";
88 }
89 }
90 }
91 return tmp.str();
92 }
93
Serialize() const94 std::vector<StoredAttribute> Database::Serialize() const {
95 std::vector<StoredAttribute> nv_attr;
96
97 if (services.empty()) return std::vector<StoredAttribute>();
98
99 for (const Service& service : services) {
100 // TODO: add constructor to NV_ATTR, use emplace_back
101 nv_attr.push_back({service.handle,
102 service.is_primary ? PRIMARY_SERVICE : SECONDARY_SERVICE,
103 {.service = {.uuid = service.uuid,
104 .end_handle = service.end_handle}}});
105 }
106
107 for (const Service& service : services) {
108 for (const IncludedService& p_isvc : service.included_services) {
109 nv_attr.push_back({p_isvc.handle,
110 INCLUDE,
111 {.included_service = {.handle = p_isvc.start_handle,
112 .end_handle = p_isvc.end_handle,
113 .uuid = p_isvc.uuid}}});
114 }
115
116 for (const Characteristic& charac : service.characteristics) {
117 nv_attr.push_back(
118 {charac.declaration_handle,
119 CHARACTERISTIC,
120 {.characteristic = {.properties = charac.properties,
121 .value_handle = charac.value_handle,
122 .uuid = charac.uuid}}});
123
124 for (const Descriptor& desc : charac.descriptors) {
125 if (desc.uuid == CHARACTERISTIC_EXTENDED_PROPERTIES) {
126 nv_attr.push_back({desc.handle,
127 desc.uuid,
128 {.characteristic_extended_properties =
129 desc.characteristic_extended_properties}});
130 } else {
131 nv_attr.push_back({desc.handle, desc.uuid, {}});
132 }
133 }
134 }
135 }
136
137 return nv_attr;
138 }
139
Deserialize(const std::vector<StoredAttribute> & nv_attr,bool * success)140 Database Database::Deserialize(const std::vector<StoredAttribute>& nv_attr,
141 bool* success) {
142 // clear reallocating
143 Database result;
144 auto it = nv_attr.cbegin();
145
146 for (; it != nv_attr.cend(); ++it) {
147 const auto& attr = *it;
148 if (attr.type != PRIMARY_SERVICE && attr.type != SECONDARY_SERVICE) break;
149 result.services.emplace_back(Service{
150 .handle = attr.handle,
151 .uuid = attr.value.service.uuid,
152 .is_primary = (attr.type == PRIMARY_SERVICE),
153 .end_handle = attr.value.service.end_handle,
154 });
155 }
156
157 auto current_service_it = result.services.begin();
158 for (; it != nv_attr.cend(); it++) {
159 const auto& attr = *it;
160
161 // go to the service this attribute belongs to; attributes are stored in
162 // order, so iterating just forward is enough
163 while (current_service_it != result.services.end() &&
164 current_service_it->end_handle < attr.handle) {
165 current_service_it++;
166 }
167
168 if (current_service_it == result.services.end() ||
169 !HandleInRange(*current_service_it, attr.handle)) {
170 LOG(ERROR) << "Can't find service for attribute with handle: "
171 << loghex(attr.handle);
172 *success = false;
173 return result;
174 }
175
176 if (attr.type == INCLUDE) {
177 Service* included_service =
178 FindService(result.services, attr.value.included_service.handle);
179 if (!included_service) {
180 LOG(ERROR) << __func__ << ": Non-existing included service!";
181 *success = false;
182 return result;
183 }
184 current_service_it->included_services.push_back(IncludedService{
185 .handle = attr.handle,
186 .uuid = attr.value.included_service.uuid,
187 .start_handle = attr.value.included_service.handle,
188 .end_handle = attr.value.included_service.end_handle,
189 });
190 } else if (attr.type == CHARACTERISTIC) {
191 current_service_it->characteristics.emplace_back(Characteristic{
192 .declaration_handle = attr.handle,
193 .uuid = attr.value.characteristic.uuid,
194 .value_handle = attr.value.characteristic.value_handle,
195 .properties = attr.value.characteristic.properties,
196 });
197
198 } else {
199 if (attr.type == CHARACTERISTIC_EXTENDED_PROPERTIES) {
200 current_service_it->characteristics.back().descriptors.emplace_back(
201 Descriptor{.handle = attr.handle,
202 .uuid = attr.type,
203 .characteristic_extended_properties =
204 attr.value.characteristic_extended_properties});
205
206 } else {
207 current_service_it->characteristics.back().descriptors.emplace_back(
208 Descriptor{.handle = attr.handle, .uuid = attr.type});
209 }
210 }
211 }
212 *success = true;
213 return result;
214 }
215
Hash() const216 Octet16 Database::Hash() const {
217 int len = 0;
218 // Compute how much space we need to actually hold the data.
219 for (const Service& service : services) {
220 len += 4 + UuidSize(service.uuid);
221
222 for (const auto& is : service.included_services) {
223 len += 8 + UuidSize(is.uuid);
224 }
225
226 for (const Characteristic& c : service.characteristics) {
227 len += 7 + UuidSize(c.uuid);
228
229 for (const Descriptor& d : c.descriptors) {
230 if (UuidSize(d.uuid) != Uuid::kNumBytes16) {
231 continue;
232 }
233 uint16_t value = d.uuid.As16Bit();
234 if (value == GATT_UUID_CHAR_DESCRIPTION ||
235 value == GATT_UUID_CHAR_CLIENT_CONFIG ||
236 value == GATT_UUID_CHAR_SRVR_CONFIG ||
237 value == GATT_UUID_CHAR_PRESENT_FORMAT ||
238 value == GATT_UUID_CHAR_AGG_FORMAT) {
239 len += 2 + UuidSize(d.uuid);
240 } else if (value == GATT_UUID_CHAR_EXT_PROP) {
241 len += 4 + UuidSize(d.uuid);
242 }
243 }
244 }
245 }
246
247 std::vector<uint8_t> serialized(len);
248 uint8_t* p = serialized.data();
249 for (const Service& service : services) {
250 UINT16_TO_STREAM(p, service.handle);
251 if (service.is_primary) {
252 UINT16_TO_STREAM(p, GATT_UUID_PRI_SERVICE);
253 } else {
254 UINT16_TO_STREAM(p, GATT_UUID_SEC_SERVICE);
255 }
256
257 if (UuidSize(service.uuid) == Uuid::kNumBytes16) {
258 UINT16_TO_STREAM(p, service.uuid.As16Bit());
259 } else {
260 ARRAY_TO_STREAM(p, service.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
261 }
262
263 for (const auto& is : service.included_services) {
264 UINT16_TO_STREAM(p, is.handle);
265 UINT16_TO_STREAM(p, GATT_UUID_INCLUDE_SERVICE);
266 UINT16_TO_STREAM(p, is.start_handle);
267 UINT16_TO_STREAM(p, is.end_handle);
268
269 if (UuidSize(is.uuid) == Uuid::kNumBytes16) {
270 UINT16_TO_STREAM(p, is.uuid.As16Bit());
271 } else {
272 ARRAY_TO_STREAM(p, is.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
273 }
274 }
275
276 for (const Characteristic& c : service.characteristics) {
277 UINT16_TO_STREAM(p, c.declaration_handle);
278 UINT16_TO_STREAM(p, GATT_UUID_CHAR_DECLARE);
279 UINT8_TO_STREAM(p, c.properties);
280 UINT16_TO_STREAM(p, c.value_handle);
281
282 if (UuidSize(c.uuid) == Uuid::kNumBytes16) {
283 UINT16_TO_STREAM(p, c.uuid.As16Bit());
284 } else {
285 ARRAY_TO_STREAM(p, c.uuid.To128BitLE(), (int)Uuid::kNumBytes128);
286 }
287
288 for (const Descriptor& d : c.descriptors) {
289 if (UuidSize(d.uuid) != Uuid::kNumBytes16) continue;
290 uint16_t value = d.uuid.As16Bit();
291 if (value == GATT_UUID_CHAR_DESCRIPTION ||
292 value == GATT_UUID_CHAR_CLIENT_CONFIG ||
293 value == GATT_UUID_CHAR_SRVR_CONFIG ||
294 value == GATT_UUID_CHAR_PRESENT_FORMAT ||
295 value == GATT_UUID_CHAR_AGG_FORMAT) {
296 UINT16_TO_STREAM(p, d.handle);
297 UINT16_TO_STREAM(p, d.uuid.As16Bit());
298 } else if (value == GATT_UUID_CHAR_EXT_PROP) {
299 UINT16_TO_STREAM(p, d.handle);
300 UINT16_TO_STREAM(p, d.uuid.As16Bit());
301 UINT16_TO_STREAM(p, d.characteristic_extended_properties);
302 }
303 }
304 }
305 }
306
307 std::reverse(serialized.begin(), serialized.end());
308 return crypto_toolbox::aes_cmac(Octet16{0}, serialized.data(),
309 serialized.size());
310 }
311 } // namespace gatt
312