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