blob: a3ce6f79a38f111ca25fce0a15a5081234592937 [file] [log] [blame]
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -07001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2014 Google, Inc.
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -07004 *
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
Myles Watson911d1ae2016-11-28 16:44:40 -080019#include "btcore/include/property.h"
Jack Hef2af1c42016-12-13 01:59:12 -080020#include <base/logging.h>
Etan Cohen3e59b5b2015-03-31 17:15:53 -070021#include <string.h>
Chris Manton97c54442015-01-07 13:34:18 -080022#include "btcore/include/device_class.h"
Chris Manton3252cf02015-01-08 10:47:59 -080023#include "osi/include/allocator.h"
Pavlin Radoslavovc5c668a2017-09-07 16:22:53 -070024#include "osi/include/compat.h"
Chris Manton3252cf02015-01-08 10:47:59 -080025
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070026using bluetooth::Uuid;
27
Myles Watson911d1ae2016-11-28 16:44:40 -080028static bt_property_t* property_new_(void* val, size_t len,
29 bt_property_type_t type);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070030
Myles Watson911d1ae2016-11-28 16:44:40 -080031bt_property_t* property_copy_array(const bt_property_t* properties,
32 size_t count) {
Jack Hef2af1c42016-12-13 01:59:12 -080033 CHECK(properties != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -080034 bt_property_t* clone =
35 static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t) * count));
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070036
37 memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count);
38 for (size_t i = 0; i < count; ++i) {
Chris Manton5c262242014-10-14 22:00:32 -070039 clone[i].val = osi_calloc(clone[i].len);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070040 memcpy(clone[i].val, properties[i].val, clone[i].len);
41 }
42
43 return clone;
44}
45
Myles Watson911d1ae2016-11-28 16:44:40 -080046bt_property_t* property_copy(bt_property_t* dest, const bt_property_t* src) {
Jack Hef2af1c42016-12-13 01:59:12 -080047 CHECK(dest != NULL);
48 CHECK(src != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -080049 return (bt_property_t*)memcpy(dest, src, sizeof(bt_property_t));
Chris Manton97c54442015-01-07 13:34:18 -080050}
51
Myles Watson911d1ae2016-11-28 16:44:40 -080052bool property_equals(const bt_property_t* p1, const bt_property_t* p2) {
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070053 // 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 }
Myles Watson911d1ae2016-11-28 16:44:40 -080073 return strlen((const char*)longer->val) == (size_t)shorter->len &&
74 !memcmp(longer->val, shorter->val, shorter->len);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070075 }
76
77 return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len);
78}
79
Jakub Pawlowskia484a882017-06-24 17:30:18 -070080bt_property_t* property_new_addr(const RawAddress* addr) {
Jack Hef2af1c42016-12-13 01:59:12 -080081 CHECK(addr != NULL);
Jakub Pawlowskia484a882017-06-24 17:30:18 -070082 return property_new_((void*)addr, sizeof(RawAddress), BT_PROPERTY_BDADDR);
Chris Manton3252cf02015-01-08 10:47:59 -080083}
84
Myles Watson911d1ae2016-11-28 16:44:40 -080085bt_property_t* property_new_device_class(const bt_device_class_t* dc) {
Jack Hef2af1c42016-12-13 01:59:12 -080086 CHECK(dc != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -080087 return property_new_((void*)dc, sizeof(bt_device_class_t),
88 BT_PROPERTY_CLASS_OF_DEVICE);
Chris Manton3252cf02015-01-08 10:47:59 -080089}
90
Myles Watson911d1ae2016-11-28 16:44:40 -080091bt_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);
Chris Manton3252cf02015-01-08 10:47:59 -080094}
95
Myles Watson911d1ae2016-11-28 16:44:40 -080096bt_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);
Chris Manton3252cf02015-01-08 10:47:59 -080099}
100
Myles Watson911d1ae2016-11-28 16:44:40 -0800101bt_property_t* property_new_name(const char* name) {
Jack Hef2af1c42016-12-13 01:59:12 -0800102 CHECK(name != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -0800103 return property_new_((void*)name, sizeof(bt_bdname_t), BT_PROPERTY_BDNAME);
Chris Manton3252cf02015-01-08 10:47:59 -0800104}
105
Myles Watson911d1ae2016-11-28 16:44:40 -0800106bt_property_t* property_new_rssi(int8_t rssi) {
107 return property_new_((void*)&rssi, sizeof(int8_t), BT_PROPERTY_REMOTE_RSSI);
Chris Manton3252cf02015-01-08 10:47:59 -0800108}
109
Myles Watson911d1ae2016-11-28 16:44:40 -0800110bt_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);
Chris Manton3252cf02015-01-08 10:47:59 -0800113}
114
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700115bt_property_t* property_new_uuids(const Uuid* uuid, size_t count) {
Jack Hef2af1c42016-12-13 01:59:12 -0800116 CHECK(uuid != NULL);
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700117 return property_new_((void*)uuid, sizeof(Uuid) * count, BT_PROPERTY_UUIDS);
Chris Manton3252cf02015-01-08 10:47:59 -0800118}
119
Myles Watson911d1ae2016-11-28 16:44:40 -0800120void property_free(bt_property_t* property) {
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -0700121 property_free_array(property, 1);
122}
123
Myles Watson911d1ae2016-11-28 16:44:40 -0800124void property_free_array(bt_property_t* properties, size_t count) {
125 if (properties == NULL) return;
Chris Manton97c54442015-01-07 13:34:18 -0800126
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -0700127 for (size_t i = 0; i < count; ++i) {
Chris Manton5c262242014-10-14 22:00:32 -0700128 osi_free(properties[i].val);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -0700129 }
130
Chris Manton5c262242014-10-14 22:00:32 -0700131 osi_free(properties);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -0700132}
Chris Manton3252cf02015-01-08 10:47:59 -0800133
Myles Watson911d1ae2016-11-28 16:44:40 -0800134bool property_is_addr(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800135 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800136 return property->type == BT_PROPERTY_BDADDR;
137}
138
Myles Watson911d1ae2016-11-28 16:44:40 -0800139bool property_is_device_class(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800140 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800141 return property->type == BT_PROPERTY_CLASS_OF_DEVICE;
142}
143
Myles Watson911d1ae2016-11-28 16:44:40 -0800144bool property_is_device_type(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800145 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800146 return property->type == BT_PROPERTY_TYPE_OF_DEVICE;
147}
148
Myles Watson911d1ae2016-11-28 16:44:40 -0800149bool property_is_discovery_timeout(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800150 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800151 return property->type == BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
152}
153
Myles Watson911d1ae2016-11-28 16:44:40 -0800154bool property_is_name(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800155 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800156 return property->type == BT_PROPERTY_BDNAME;
157}
158
Myles Watson911d1ae2016-11-28 16:44:40 -0800159bool property_is_rssi(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800160 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800161 return property->type == BT_PROPERTY_REMOTE_RSSI;
162}
163
Myles Watson911d1ae2016-11-28 16:44:40 -0800164bool property_is_scan_mode(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800165 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800166 return property->type == BT_PROPERTY_ADAPTER_SCAN_MODE;
167}
168
Myles Watson911d1ae2016-11-28 16:44:40 -0800169bool property_is_uuids(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800170 CHECK(property != NULL);
Chris Manton3252cf02015-01-08 10:47:59 -0800171 return property->type == BT_PROPERTY_UUIDS;
172}
173
174// Convenience conversion methods to property values
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700175const RawAddress* property_as_addr(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800176 CHECK(property_is_addr(property));
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700177 return (const RawAddress*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800178}
179
Myles Watson911d1ae2016-11-28 16:44:40 -0800180const bt_device_class_t* property_as_device_class(
181 const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800182 CHECK(property_is_device_class(property));
Myles Watson911d1ae2016-11-28 16:44:40 -0800183 return (const bt_device_class_t*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800184}
185
Myles Watson911d1ae2016-11-28 16:44:40 -0800186bt_device_type_t property_as_device_type(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800187 CHECK(property_is_device_type(property));
Myles Watson911d1ae2016-11-28 16:44:40 -0800188 return *(const bt_device_type_t*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800189}
190
Myles Watson911d1ae2016-11-28 16:44:40 -0800191uint32_t property_as_discovery_timeout(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800192 CHECK(property_is_discovery_timeout(property));
Myles Watson911d1ae2016-11-28 16:44:40 -0800193 return *(const uint32_t*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800194}
195
Myles Watson911d1ae2016-11-28 16:44:40 -0800196const bt_bdname_t* property_as_name(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800197 CHECK(property_is_name(property));
Myles Watson911d1ae2016-11-28 16:44:40 -0800198 return (const bt_bdname_t*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800199}
200
Myles Watson911d1ae2016-11-28 16:44:40 -0800201int8_t property_as_rssi(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800202 CHECK(property_is_rssi(property));
Myles Watson911d1ae2016-11-28 16:44:40 -0800203 return *(const int8_t*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800204}
205
Myles Watson911d1ae2016-11-28 16:44:40 -0800206bt_scan_mode_t property_as_scan_mode(const bt_property_t* property) {
Jack Hef2af1c42016-12-13 01:59:12 -0800207 CHECK(property_is_scan_mode(property));
Myles Watson911d1ae2016-11-28 16:44:40 -0800208 return *(const bt_scan_mode_t*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800209}
210
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700211const Uuid* property_as_uuids(const bt_property_t* property, size_t* count) {
Jack Hef2af1c42016-12-13 01:59:12 -0800212 CHECK(property_is_uuids(property));
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700213 *count = sizeof(Uuid) / property->len;
214 return (const Uuid*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800215}
216
Myles Watson911d1ae2016-11-28 16:44:40 -0800217static 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)));
Chris Manton3252cf02015-01-08 10:47:59 -0800221
Pavlin Radoslavovc5c668a2017-09-07 16:22:53 -0700222 property->val = osi_calloc(len + 1);
Pavlin Radoslavov2a874662017-03-18 19:35:06 -0700223 if (type == BT_PROPERTY_BDNAME) {
Pavlin Radoslavovc5c668a2017-09-07 16:22:53 -0700224 strlcpy((char*)property->val, (const char*)val, len);
Pavlin Radoslavov2a874662017-03-18 19:35:06 -0700225 } else {
226 memcpy(property->val, val, len);
227 }
Chris Manton3252cf02015-01-08 10:47:59 -0800228
229 property->type = type;
230 property->len = len;
231
232 return property;
233}