blob: e3161733767552e6797b24ac993b0e7eb5036117 [file] [log] [blame]
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -07001/******************************************************************************
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
Myles Watson911d1ae2016-11-28 16:44:40 -080019#include "btcore/include/property.h"
Chris Manton97c54442015-01-07 13:34:18 -080020#include <assert.h>
Etan Cohen3e59b5b2015-03-31 17:15:53 -070021#include <string.h>
Chris Manton97c54442015-01-07 13:34:18 -080022#include "btcore/include/bdaddr.h"
23#include "btcore/include/device_class.h"
Chris Manton3252cf02015-01-08 10:47:59 -080024#include "btcore/include/uuid.h"
25#include "osi/include/allocator.h"
26
Myles Watson911d1ae2016-11-28 16:44:40 -080027static bt_property_t* property_new_(void* val, size_t len,
28 bt_property_type_t type);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070029
Myles Watson911d1ae2016-11-28 16:44:40 -080030bt_property_t* property_copy_array(const bt_property_t* properties,
31 size_t count) {
Chris Manton97c54442015-01-07 13:34:18 -080032 assert(properties != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -080033 bt_property_t* clone =
34 static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t) * count));
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070035
36 memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count);
37 for (size_t i = 0; i < count; ++i) {
Chris Manton5c262242014-10-14 22:00:32 -070038 clone[i].val = osi_calloc(clone[i].len);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070039 memcpy(clone[i].val, properties[i].val, clone[i].len);
40 }
41
42 return clone;
43}
44
Myles Watson911d1ae2016-11-28 16:44:40 -080045bt_property_t* property_copy(bt_property_t* dest, const bt_property_t* src) {
Chris Manton97c54442015-01-07 13:34:18 -080046 assert(dest != NULL);
47 assert(src != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -080048 return (bt_property_t*)memcpy(dest, src, sizeof(bt_property_t));
Chris Manton97c54442015-01-07 13:34:18 -080049}
50
Myles Watson911d1ae2016-11-28 16:44:40 -080051bool property_equals(const bt_property_t* p1, const bt_property_t* p2) {
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070052 // 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 }
Myles Watson911d1ae2016-11-28 16:44:40 -080072 return strlen((const char*)longer->val) == (size_t)shorter->len &&
73 !memcmp(longer->val, shorter->val, shorter->len);
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070074 }
75
76 return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len);
77}
78
Myles Watson911d1ae2016-11-28 16:44:40 -080079bt_property_t* property_new_addr(const bt_bdaddr_t* addr) {
Chris Manton3252cf02015-01-08 10:47:59 -080080 assert(addr != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -080081 return property_new_((void*)addr, sizeof(bt_bdaddr_t), BT_PROPERTY_BDADDR);
Chris Manton3252cf02015-01-08 10:47:59 -080082}
83
Myles Watson911d1ae2016-11-28 16:44:40 -080084bt_property_t* property_new_device_class(const bt_device_class_t* dc) {
Chris Manton3252cf02015-01-08 10:47:59 -080085 assert(dc != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -080086 return property_new_((void*)dc, sizeof(bt_device_class_t),
87 BT_PROPERTY_CLASS_OF_DEVICE);
Chris Manton3252cf02015-01-08 10:47:59 -080088}
89
Myles Watson911d1ae2016-11-28 16:44:40 -080090bt_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);
Chris Manton3252cf02015-01-08 10:47:59 -080093}
94
Myles Watson911d1ae2016-11-28 16:44:40 -080095bt_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);
Chris Manton3252cf02015-01-08 10:47:59 -080098}
99
Myles Watson911d1ae2016-11-28 16:44:40 -0800100bt_property_t* property_new_name(const char* name) {
Chris Manton3252cf02015-01-08 10:47:59 -0800101 assert(name != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -0800102 return property_new_((void*)name, sizeof(bt_bdname_t), BT_PROPERTY_BDNAME);
Chris Manton3252cf02015-01-08 10:47:59 -0800103}
104
Myles Watson911d1ae2016-11-28 16:44:40 -0800105bt_property_t* property_new_rssi(int8_t rssi) {
106 return property_new_((void*)&rssi, sizeof(int8_t), BT_PROPERTY_REMOTE_RSSI);
Chris Manton3252cf02015-01-08 10:47:59 -0800107}
108
Myles Watson911d1ae2016-11-28 16:44:40 -0800109bt_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);
Chris Manton3252cf02015-01-08 10:47:59 -0800112}
113
Myles Watson911d1ae2016-11-28 16:44:40 -0800114bt_property_t* property_new_uuids(const bt_uuid_t* uuid, size_t count) {
Chris Manton3252cf02015-01-08 10:47:59 -0800115 assert(uuid != NULL);
Myles Watson911d1ae2016-11-28 16:44:40 -0800116 return property_new_((void*)uuid, sizeof(bt_uuid_t) * count,
117 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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800135 assert(property != NULL);
136 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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800140 assert(property != NULL);
141 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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800145 assert(property != NULL);
146 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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800150 assert(property != NULL);
151 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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800155 assert(property != NULL);
156 return property->type == BT_PROPERTY_BDNAME;
157}
158
Myles Watson911d1ae2016-11-28 16:44:40 -0800159bool property_is_rssi(const bt_property_t* property) {
Chris Manton3252cf02015-01-08 10:47:59 -0800160 assert(property != NULL);
161 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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800165 assert(property != NULL);
166 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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800170 assert(property != NULL);
171 return property->type == BT_PROPERTY_UUIDS;
172}
173
174// Convenience conversion methods to property values
Myles Watson911d1ae2016-11-28 16:44:40 -0800175const bt_bdaddr_t* property_as_addr(const bt_property_t* property) {
Chris Manton3252cf02015-01-08 10:47:59 -0800176 assert(property_is_addr(property));
Myles Watson911d1ae2016-11-28 16:44:40 -0800177 return (const bt_bdaddr_t*)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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800182 assert(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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800187 assert(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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800192 assert(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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800197 assert(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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800202 assert(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) {
Chris Manton3252cf02015-01-08 10:47:59 -0800207 assert(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
Myles Watson911d1ae2016-11-28 16:44:40 -0800211const bt_uuid_t* property_as_uuids(const bt_property_t* property,
212 size_t* count) {
Chris Manton3252cf02015-01-08 10:47:59 -0800213 assert(property_is_uuids(property));
214 *count = sizeof(bt_uuid_t) / property->len;
Myles Watson911d1ae2016-11-28 16:44:40 -0800215 return (const bt_uuid_t*)property->val;
Chris Manton3252cf02015-01-08 10:47:59 -0800216}
217
Myles Watson911d1ae2016-11-28 16:44:40 -0800218static 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)));
Chris Manton3252cf02015-01-08 10:47:59 -0800222
223 property->val = osi_malloc(len);
Chris Manton3252cf02015-01-08 10:47:59 -0800224 memcpy(property->val, val, len);
225
226 property->type = type;
227 property->len = len;
228
229 return property;
230}