• 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 #include <base/logging.h>
21 #include <string.h>
22 #include "btcore/include/device_class.h"
23 #include "osi/include/allocator.h"
24 #include "osi/include/compat.h"
25 
26 using bluetooth::Uuid;
27 
28 static bt_property_t* property_new_(void* val, size_t len,
29                                     bt_property_type_t type);
30 
property_copy_array(const bt_property_t * properties,size_t count)31 bt_property_t* property_copy_array(const bt_property_t* properties,
32                                    size_t count) {
33   CHECK(properties != NULL);
34   bt_property_t* clone =
35       static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t) * count));
36 
37   memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count);
38   for (size_t i = 0; i < count; ++i) {
39     clone[i].val = osi_calloc(clone[i].len);
40     memcpy(clone[i].val, properties[i].val, clone[i].len);
41   }
42 
43   return clone;
44 }
45 
property_copy(bt_property_t * dest,const bt_property_t * src)46 bt_property_t* property_copy(bt_property_t* dest, const bt_property_t* src) {
47   CHECK(dest != NULL);
48   CHECK(src != NULL);
49   return (bt_property_t*)memcpy(dest, src, sizeof(bt_property_t));
50 }
51 
property_equals(const bt_property_t * p1,const bt_property_t * p2)52 bool property_equals(const bt_property_t* p1, const bt_property_t* p2) {
53   // Two null properties are not the same. May need to revisit that
54   // decision when we have a test case that exercises that condition.
55   if (!p1 || !p2 || p1->type != p2->type) {
56     return false;
57   }
58 
59   // Although the Bluetooth name is a 249-byte array, the implementation
60   // treats it like a variable-length array with its size specified in the
61   // property's `len` field. We special-case the equivalence of BDNAME
62   // types here by truncating the larger, zero-padded name to its string
63   // length and comparing against the shorter name.
64   //
65   // Note: it may be the case that both strings are zero-padded but that
66   // hasn't come up yet so this implementation doesn't handle it.
67   if (p1->type == BT_PROPERTY_BDNAME && p1->len != p2->len) {
68     const bt_property_t *shorter = p1, *longer = p2;
69     if (p1->len > p2->len) {
70       shorter = p2;
71       longer = p1;
72     }
73     return strlen((const char*)longer->val) == (size_t)shorter->len &&
74            !memcmp(longer->val, shorter->val, shorter->len);
75   }
76 
77   return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len);
78 }
79 
property_new_addr(const RawAddress * addr)80 bt_property_t* property_new_addr(const RawAddress* addr) {
81   CHECK(addr != NULL);
82   return property_new_((void*)addr, sizeof(RawAddress), BT_PROPERTY_BDADDR);
83 }
84 
property_new_device_class(const bt_device_class_t * dc)85 bt_property_t* property_new_device_class(const bt_device_class_t* dc) {
86   CHECK(dc != NULL);
87   return property_new_((void*)dc, sizeof(bt_device_class_t),
88                        BT_PROPERTY_CLASS_OF_DEVICE);
89 }
90 
property_new_device_type(bt_device_type_t type)91 bt_property_t* property_new_device_type(bt_device_type_t type) {
92   return property_new_((void*)&type, sizeof(bt_device_type_t),
93                        BT_PROPERTY_TYPE_OF_DEVICE);
94 }
95 
property_new_discovery_timeout(const uint32_t timeout)96 bt_property_t* property_new_discovery_timeout(const uint32_t timeout) {
97   return property_new_((void*)&timeout, sizeof(uint32_t),
98                        BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT);
99 }
100 
property_new_name(const char * name)101 bt_property_t* property_new_name(const char* name) {
102   CHECK(name != NULL);
103   return property_new_((void*)name, sizeof(bt_bdname_t), BT_PROPERTY_BDNAME);
104 }
105 
property_new_rssi(int8_t rssi)106 bt_property_t* property_new_rssi(int8_t rssi) {
107   return property_new_((void*)&rssi, sizeof(int8_t), BT_PROPERTY_REMOTE_RSSI);
108 }
109 
property_new_scan_mode(bt_scan_mode_t scan_mode)110 bt_property_t* property_new_scan_mode(bt_scan_mode_t scan_mode) {
111   return property_new_((void*)&scan_mode, sizeof(bt_scan_mode_t),
112                        BT_PROPERTY_ADAPTER_SCAN_MODE);
113 }
114 
property_new_uuids(const Uuid * uuid,size_t count)115 bt_property_t* property_new_uuids(const Uuid* uuid, size_t count) {
116   CHECK(uuid != NULL);
117   return property_new_((void*)uuid, sizeof(Uuid) * count, 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 RawAddress* property_as_addr(const bt_property_t* property) {
176   CHECK(property_is_addr(property));
177   return (const RawAddress*)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 Uuid* property_as_uuids(const bt_property_t* property, size_t* count) {
212   CHECK(property_is_uuids(property));
213   *count = sizeof(Uuid) / property->len;
214   return (const Uuid*)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_calloc(len + 1);
223   if (type == BT_PROPERTY_BDNAME) {
224     strlcpy((char*)property->val, (const char*)val, len);
225   } else {
226     memcpy(property->val, val, len);
227   }
228 
229   property->type = type;
230   property->len = len;
231 
232   return property;
233 }
234