1 /******************************************************************************
2 *
3 * Copyright 2014 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 "btcore/include/property.h"
20
21 #include <bluetooth/log.h>
22 #include <string.h>
23
24 #include "btcore/include/device_class.h"
25 #include "osi/include/allocator.h"
26 #include "osi/include/compat.h"
27 #include "types/bluetooth/uuid.h"
28 #include "types/raw_address.h"
29
30 using bluetooth::Uuid;
31 using namespace bluetooth;
32
33 static bt_property_t* property_new_(void* val, size_t len,
34 bt_property_type_t type);
35
property_copy_array(const bt_property_t * properties,size_t count)36 bt_property_t* property_copy_array(const bt_property_t* properties,
37 size_t count) {
38 log::assert_that(properties != NULL, "assert failed: properties != NULL");
39 bt_property_t* clone =
40 static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t) * count));
41
42 memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count);
43 for (size_t i = 0; i < count; ++i) {
44 clone[i].val = osi_calloc(clone[i].len);
45 memcpy(clone[i].val, properties[i].val, clone[i].len);
46 }
47
48 return clone;
49 }
50
property_copy(bt_property_t * dest,const bt_property_t * src)51 bt_property_t* property_copy(bt_property_t* dest, const bt_property_t* src) {
52 log::assert_that(dest != NULL, "assert failed: dest != NULL");
53 log::assert_that(src != NULL, "assert failed: src != NULL");
54 return (bt_property_t*)memcpy(dest, src, sizeof(bt_property_t));
55 }
56
property_equals(const bt_property_t * p1,const bt_property_t * p2)57 bool property_equals(const bt_property_t* p1, const bt_property_t* p2) {
58 // Two null properties are not the same. May need to revisit that
59 // decision when we have a test case that exercises that condition.
60 if (!p1 || !p2 || p1->type != p2->type) {
61 return false;
62 }
63
64 // Although the Bluetooth name is a 249-byte array, the implementation
65 // treats it like a variable-length array with its size specified in the
66 // property's `len` field. We special-case the equivalence of BDNAME
67 // types here by truncating the larger, zero-padded name to its string
68 // length and comparing against the shorter name.
69 //
70 // Note: it may be the case that both strings are zero-padded but that
71 // hasn't come up yet so this implementation doesn't handle it.
72 if (p1->type == BT_PROPERTY_BDNAME && p1->len != p2->len) {
73 const bt_property_t *shorter = p1, *longer = p2;
74 if (p1->len > p2->len) {
75 shorter = p2;
76 longer = p1;
77 }
78 return strlen((const char*)longer->val) == (size_t)shorter->len &&
79 !memcmp(longer->val, shorter->val, shorter->len);
80 }
81
82 return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len);
83 }
84
property_new_addr(const RawAddress * addr)85 bt_property_t* property_new_addr(const RawAddress* addr) {
86 log::assert_that(addr != NULL, "assert failed: addr != NULL");
87 return property_new_((void*)addr, sizeof(RawAddress), BT_PROPERTY_BDADDR);
88 }
89
property_new_device_class(const bt_device_class_t * dc)90 bt_property_t* property_new_device_class(const bt_device_class_t* dc) {
91 log::assert_that(dc != NULL, "assert failed: dc != NULL");
92 return property_new_((void*)dc, sizeof(bt_device_class_t),
93 BT_PROPERTY_CLASS_OF_DEVICE);
94 }
95
property_new_device_type(bt_device_type_t type)96 bt_property_t* property_new_device_type(bt_device_type_t type) {
97 return property_new_((void*)&type, sizeof(bt_device_type_t),
98 BT_PROPERTY_TYPE_OF_DEVICE);
99 }
100
property_new_discoverable_timeout(const uint32_t timeout)101 bt_property_t* property_new_discoverable_timeout(const uint32_t timeout) {
102 return property_new_((void*)&timeout, sizeof(uint32_t),
103 BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT);
104 }
105
property_new_name(const char * name)106 bt_property_t* property_new_name(const char* name) {
107 log::assert_that(name != NULL, "assert failed: name != NULL");
108 return property_new_((void*)name, sizeof(bt_bdname_t), BT_PROPERTY_BDNAME);
109 }
110
property_new_rssi(int8_t rssi)111 bt_property_t* property_new_rssi(int8_t rssi) {
112 return property_new_((void*)&rssi, sizeof(int8_t), BT_PROPERTY_REMOTE_RSSI);
113 }
114
property_new_scan_mode(bt_scan_mode_t scan_mode)115 bt_property_t* property_new_scan_mode(bt_scan_mode_t scan_mode) {
116 return property_new_((void*)&scan_mode, sizeof(bt_scan_mode_t),
117 BT_PROPERTY_ADAPTER_SCAN_MODE);
118 }
119
property_new_uuids(const Uuid * uuid,size_t count)120 bt_property_t* property_new_uuids(const Uuid* uuid, size_t count) {
121 log::assert_that(uuid != NULL, "assert failed: uuid != NULL");
122 return property_new_((void*)uuid, sizeof(Uuid) * count, BT_PROPERTY_UUIDS);
123 }
124
property_free(bt_property_t * property)125 void property_free(bt_property_t* property) {
126 property_free_array(property, 1);
127 }
128
property_free_array(bt_property_t * properties,size_t count)129 void property_free_array(bt_property_t* properties, size_t count) {
130 if (properties == NULL) return;
131
132 for (size_t i = 0; i < count; ++i) {
133 osi_free(properties[i].val);
134 }
135
136 osi_free(properties);
137 }
138
property_is_addr(const bt_property_t * property)139 bool property_is_addr(const bt_property_t* property) {
140 log::assert_that(property != NULL, "assert failed: property != NULL");
141 return property->type == BT_PROPERTY_BDADDR;
142 }
143
property_is_device_class(const bt_property_t * property)144 bool property_is_device_class(const bt_property_t* property) {
145 log::assert_that(property != NULL, "assert failed: property != NULL");
146 return property->type == BT_PROPERTY_CLASS_OF_DEVICE;
147 }
148
property_is_device_type(const bt_property_t * property)149 bool property_is_device_type(const bt_property_t* property) {
150 log::assert_that(property != NULL, "assert failed: property != NULL");
151 return property->type == BT_PROPERTY_TYPE_OF_DEVICE;
152 }
153
property_is_discoverable_timeout(const bt_property_t * property)154 bool property_is_discoverable_timeout(const bt_property_t* property) {
155 log::assert_that(property != NULL, "assert failed: property != NULL");
156 return property->type == BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT;
157 }
158
property_is_name(const bt_property_t * property)159 bool property_is_name(const bt_property_t* property) {
160 log::assert_that(property != NULL, "assert failed: property != NULL");
161 return property->type == BT_PROPERTY_BDNAME;
162 }
163
property_is_rssi(const bt_property_t * property)164 bool property_is_rssi(const bt_property_t* property) {
165 log::assert_that(property != NULL, "assert failed: property != NULL");
166 return property->type == BT_PROPERTY_REMOTE_RSSI;
167 }
168
property_is_scan_mode(const bt_property_t * property)169 bool property_is_scan_mode(const bt_property_t* property) {
170 log::assert_that(property != NULL, "assert failed: property != NULL");
171 return property->type == BT_PROPERTY_ADAPTER_SCAN_MODE;
172 }
173
property_is_uuids(const bt_property_t * property)174 bool property_is_uuids(const bt_property_t* property) {
175 log::assert_that(property != NULL, "assert failed: property != NULL");
176 return property->type == BT_PROPERTY_UUIDS;
177 }
178
179 // Convenience conversion methods to property values
property_as_addr(const bt_property_t * property)180 const RawAddress* property_as_addr(const bt_property_t* property) {
181 log::assert_that(property_is_addr(property),
182 "assert failed: property_is_addr(property)");
183 return (const RawAddress*)property->val;
184 }
185
property_as_device_class(const bt_property_t * property)186 const bt_device_class_t* property_as_device_class(
187 const bt_property_t* property) {
188 log::assert_that(property_is_device_class(property),
189 "assert failed: property_is_device_class(property)");
190 return (const bt_device_class_t*)property->val;
191 }
192
property_as_device_type(const bt_property_t * property)193 bt_device_type_t property_as_device_type(const bt_property_t* property) {
194 log::assert_that(property_is_device_type(property),
195 "assert failed: property_is_device_type(property)");
196 return *(const bt_device_type_t*)property->val;
197 }
198
property_as_discoverable_timeout(const bt_property_t * property)199 uint32_t property_as_discoverable_timeout(const bt_property_t* property) {
200 log::assert_that(property_is_discoverable_timeout(property),
201 "assert failed: property_is_discoverable_timeout(property)");
202 return *(const uint32_t*)property->val;
203 }
204
property_as_name(const bt_property_t * property)205 const bt_bdname_t* property_as_name(const bt_property_t* property) {
206 log::assert_that(property_is_name(property),
207 "assert failed: property_is_name(property)");
208 return (const bt_bdname_t*)property->val;
209 }
210
property_as_rssi(const bt_property_t * property)211 int8_t property_as_rssi(const bt_property_t* property) {
212 log::assert_that(property_is_rssi(property),
213 "assert failed: property_is_rssi(property)");
214 return *(const int8_t*)property->val;
215 }
216
property_as_scan_mode(const bt_property_t * property)217 bt_scan_mode_t property_as_scan_mode(const bt_property_t* property) {
218 log::assert_that(property_is_scan_mode(property),
219 "assert failed: property_is_scan_mode(property)");
220 return *(const bt_scan_mode_t*)property->val;
221 }
222
property_as_uuids(const bt_property_t * property,size_t * count)223 const Uuid* property_as_uuids(const bt_property_t* property, size_t* count) {
224 log::assert_that(property_is_uuids(property),
225 "assert failed: property_is_uuids(property)");
226 *count = sizeof(Uuid) / property->len;
227 return (const Uuid*)property->val;
228 }
229
property_new_(void * val,size_t len,bt_property_type_t type)230 static bt_property_t* property_new_(void* val, size_t len,
231 bt_property_type_t type) {
232 bt_property_t* property =
233 static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t)));
234
235 property->val = osi_calloc(len + 1);
236 if (type == BT_PROPERTY_BDNAME) {
237 strlcpy((char*)property->val, (const char*)val, len);
238 } else {
239 memcpy(property->val, val, len);
240 }
241
242 property->type = type;
243 property->len = len;
244
245 return property;
246 }
247