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