• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <base/logging.h>
22 #include <string.h>
23 
24 #include "btcore/include/device_class.h"
25 #include "check.h"
26 #include "osi/include/allocator.h"
27 #include "osi/include/compat.h"
28 #include "types/bluetooth/uuid.h"
29 #include "types/raw_address.h"
30 
31 using bluetooth::Uuid;
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   CHECK(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   CHECK(dest != NULL);
53   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(property_is_addr(property));
182   return (const RawAddress*)property->val;
183 }
184 
property_as_device_class(const bt_property_t * property)185 const bt_device_class_t* property_as_device_class(
186     const bt_property_t* property) {
187   CHECK(property_is_device_class(property));
188   return (const bt_device_class_t*)property->val;
189 }
190 
property_as_device_type(const bt_property_t * property)191 bt_device_type_t property_as_device_type(const bt_property_t* property) {
192   CHECK(property_is_device_type(property));
193   return *(const bt_device_type_t*)property->val;
194 }
195 
property_as_discoverable_timeout(const bt_property_t * property)196 uint32_t property_as_discoverable_timeout(const bt_property_t* property) {
197   CHECK(property_is_discoverable_timeout(property));
198   return *(const uint32_t*)property->val;
199 }
200 
property_as_name(const bt_property_t * property)201 const bt_bdname_t* property_as_name(const bt_property_t* property) {
202   CHECK(property_is_name(property));
203   return (const bt_bdname_t*)property->val;
204 }
205 
property_as_rssi(const bt_property_t * property)206 int8_t property_as_rssi(const bt_property_t* property) {
207   CHECK(property_is_rssi(property));
208   return *(const int8_t*)property->val;
209 }
210 
property_as_scan_mode(const bt_property_t * property)211 bt_scan_mode_t property_as_scan_mode(const bt_property_t* property) {
212   CHECK(property_is_scan_mode(property));
213   return *(const bt_scan_mode_t*)property->val;
214 }
215 
property_as_uuids(const bt_property_t * property,size_t * count)216 const Uuid* property_as_uuids(const bt_property_t* property, size_t* count) {
217   CHECK(property_is_uuids(property));
218   *count = sizeof(Uuid) / property->len;
219   return (const Uuid*)property->val;
220 }
221 
property_new_(void * val,size_t len,bt_property_type_t type)222 static bt_property_t* property_new_(void* val, size_t len,
223                                     bt_property_type_t type) {
224   bt_property_t* property =
225       static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t)));
226 
227   property->val = osi_calloc(len + 1);
228   if (type == BT_PROPERTY_BDNAME) {
229     strlcpy((char*)property->val, (const char*)val, len);
230   } else {
231     memcpy(property->val, val, len);
232   }
233 
234   property->type = type;
235   property->len = len;
236 
237   return property;
238 }
239