1 /****************************************************************************** 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 19 #include <assert.h> 20 #include <string.h> 21 #include "btcore/include/bdaddr.h" 22 #include "btcore/include/device_class.h" 23 #include "btcore/include/property.h" 24 #include "btcore/include/uuid.h" 25 #include "osi/include/allocator.h" 26 27 static bt_property_t *property_new_(void *val, size_t len, bt_property_type_t type); 28 29 bt_property_t *property_copy_array(const bt_property_t *properties, size_t count) { 30 assert(properties != NULL); 31 bt_property_t *clone = osi_calloc(sizeof(bt_property_t) * count); 32 if (!clone) { 33 return NULL; 34 } 35 36 memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count); 37 for (size_t i = 0; i < count; ++i) { 38 clone[i].val = osi_calloc(clone[i].len); 39 memcpy(clone[i].val, properties[i].val, clone[i].len); 40 } 41 42 return clone; 43 } 44 45 bt_property_t *property_copy(bt_property_t *dest, const bt_property_t *src) { 46 assert(dest != NULL); 47 assert(src != NULL); 48 return (bt_property_t *)memcpy(dest, src, sizeof(bt_property_t)); 49 } 50 51 bool property_equals(const bt_property_t *p1, const bt_property_t *p2) { 52 // 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 } 72 return strlen((const char *)longer->val) == (size_t)shorter->len && !memcmp(longer->val, shorter->val, shorter->len); 73 } 74 75 return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len); 76 } 77 78 bt_property_t *property_new_addr(const bt_bdaddr_t *addr) { 79 assert(addr != NULL); 80 return property_new_((void *)addr, sizeof(bt_bdaddr_t), BT_PROPERTY_BDADDR); 81 } 82 83 bt_property_t *property_new_device_class(const bt_device_class_t *dc) { 84 assert(dc != NULL); 85 return property_new_((void *)dc, sizeof(bt_device_class_t), BT_PROPERTY_CLASS_OF_DEVICE); 86 } 87 88 bt_property_t *property_new_device_type(bt_device_type_t type) { 89 return property_new_((void *)&type, sizeof(bt_device_type_t), BT_PROPERTY_TYPE_OF_DEVICE); 90 } 91 92 bt_property_t *property_new_discovery_timeout(const uint32_t timeout) { 93 return property_new_((void *)&timeout, sizeof(uint32_t), BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT); 94 } 95 96 bt_property_t *property_new_name(const char *name) { 97 assert(name != NULL); 98 return property_new_((void *)name, sizeof(bt_bdname_t), BT_PROPERTY_BDNAME); 99 } 100 101 bt_property_t *property_new_rssi(int8_t rssi) { 102 return property_new_((void *)&rssi, sizeof(int8_t), BT_PROPERTY_REMOTE_RSSI); 103 } 104 105 bt_property_t *property_new_scan_mode(bt_scan_mode_t scan_mode) { 106 return property_new_((void *)&scan_mode, sizeof(bt_scan_mode_t), BT_PROPERTY_ADAPTER_SCAN_MODE); 107 } 108 109 bt_property_t *property_new_uuids(const bt_uuid_t *uuid, size_t count) { 110 assert(uuid != NULL); 111 return property_new_((void *)uuid, sizeof(bt_uuid_t) * count, BT_PROPERTY_UUIDS); 112 } 113 114 void property_free(bt_property_t *property) { 115 property_free_array(property, 1); 116 } 117 118 void property_free_array(bt_property_t *properties, size_t count) { 119 if (properties == NULL) 120 return; 121 122 for (size_t i = 0; i < count; ++i) { 123 osi_free(properties[i].val); 124 } 125 126 osi_free(properties); 127 } 128 129 bool property_is_addr(const bt_property_t *property) { 130 assert(property != NULL); 131 return property->type == BT_PROPERTY_BDADDR; 132 } 133 134 bool property_is_device_class(const bt_property_t *property) { 135 assert(property != NULL); 136 return property->type == BT_PROPERTY_CLASS_OF_DEVICE; 137 } 138 139 bool property_is_device_type(const bt_property_t *property) { 140 assert(property != NULL); 141 return property->type == BT_PROPERTY_TYPE_OF_DEVICE; 142 } 143 144 bool property_is_discovery_timeout(const bt_property_t *property) { 145 assert(property != NULL); 146 return property->type == BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT; 147 } 148 149 bool property_is_name(const bt_property_t *property) { 150 assert(property != NULL); 151 return property->type == BT_PROPERTY_BDNAME; 152 } 153 154 bool property_is_rssi(const bt_property_t *property) { 155 assert(property != NULL); 156 return property->type == BT_PROPERTY_REMOTE_RSSI; 157 } 158 159 bool property_is_scan_mode(const bt_property_t *property) { 160 assert(property != NULL); 161 return property->type == BT_PROPERTY_ADAPTER_SCAN_MODE; 162 } 163 164 bool property_is_uuids(const bt_property_t *property) { 165 assert(property != NULL); 166 return property->type == BT_PROPERTY_UUIDS; 167 } 168 169 // Convenience conversion methods to property values 170 const bt_bdaddr_t *property_as_addr(const bt_property_t *property) { 171 assert(property_is_addr(property)); 172 return (const bt_bdaddr_t *)property->val; 173 } 174 175 const bt_device_class_t *property_as_device_class(const bt_property_t *property) { 176 assert(property_is_device_class(property)); 177 return (const bt_device_class_t *)property->val; 178 } 179 180 bt_device_type_t property_as_device_type(const bt_property_t *property) { 181 assert(property_is_device_type(property)); 182 return *(const bt_device_type_t *)property->val; 183 } 184 185 uint32_t property_as_discovery_timeout(const bt_property_t *property) { 186 assert(property_is_discovery_timeout(property)); 187 return *(const uint32_t *)property->val; 188 } 189 190 const bt_bdname_t *property_as_name(const bt_property_t *property) { 191 assert(property_is_name(property)); 192 return (const bt_bdname_t *)property->val; 193 } 194 195 int8_t property_as_rssi(const bt_property_t *property) { 196 assert(property_is_rssi(property)); 197 return *(const int8_t *)property->val; 198 } 199 200 bt_scan_mode_t property_as_scan_mode(const bt_property_t *property) { 201 assert(property_is_scan_mode(property)); 202 return *(const bt_scan_mode_t *)property->val; 203 } 204 205 const bt_uuid_t *property_as_uuids(const bt_property_t *property, size_t *count) { 206 assert(property_is_uuids(property)); 207 *count = sizeof(bt_uuid_t) / property->len; 208 return (const bt_uuid_t *)property->val; 209 } 210 211 static bt_property_t *property_new_(void *val, size_t len, bt_property_type_t type) { 212 bt_property_t *property = osi_calloc(sizeof(bt_property_t)); 213 assert(property != NULL); 214 215 property->val = osi_malloc(len); 216 assert(property->val != NULL); 217 218 memcpy(property->val, val, len); 219 220 property->type = type; 221 property->len = len; 222 223 return property; 224 } 225