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