Home | History | Annotate | Download | only in src
      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 
     33   memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count);
     34   for (size_t i = 0; i < count; ++i) {
     35     clone[i].val = osi_calloc(clone[i].len);
     36     memcpy(clone[i].val, properties[i].val, clone[i].len);
     37   }
     38 
     39   return clone;
     40 }
     41 
     42 bt_property_t *property_copy(bt_property_t *dest, const bt_property_t *src) {
     43   assert(dest != NULL);
     44   assert(src != NULL);
     45   return (bt_property_t *)memcpy(dest, src, sizeof(bt_property_t));
     46 }
     47 
     48 bool property_equals(const bt_property_t *p1, const bt_property_t *p2) {
     49   // Two null properties are not the same. May need to revisit that
     50   // decision when we have a test case that exercises that condition.
     51   if (!p1 || !p2 || p1->type != p2->type) {
     52     return false;
     53   }
     54 
     55   // Although the Bluetooth name is a 249-byte array, the implementation
     56   // treats it like a variable-length array with its size specified in the
     57   // property's `len` field. We special-case the equivalence of BDNAME
     58   // types here by truncating the larger, zero-padded name to its string
     59   // length and comparing against the shorter name.
     60   //
     61   // Note: it may be the case that both strings are zero-padded but that
     62   // hasn't come up yet so this implementation doesn't handle it.
     63   if (p1->type == BT_PROPERTY_BDNAME && p1->len != p2->len) {
     64     const bt_property_t *shorter = p1, *longer = p2;
     65     if (p1->len > p2->len) {
     66       shorter = p2;
     67       longer = p1;
     68     }
     69     return strlen((const char *)longer->val) == (size_t)shorter->len && !memcmp(longer->val, shorter->val, shorter->len);
     70   }
     71 
     72   return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len);
     73 }
     74 
     75 bt_property_t *property_new_addr(const bt_bdaddr_t *addr) {
     76   assert(addr != NULL);
     77   return property_new_((void *)addr, sizeof(bt_bdaddr_t), BT_PROPERTY_BDADDR);
     78 }
     79 
     80 bt_property_t *property_new_device_class(const bt_device_class_t *dc) {
     81   assert(dc != NULL);
     82   return property_new_((void *)dc, sizeof(bt_device_class_t), BT_PROPERTY_CLASS_OF_DEVICE);
     83 }
     84 
     85 bt_property_t *property_new_device_type(bt_device_type_t type) {
     86   return property_new_((void *)&type, sizeof(bt_device_type_t), BT_PROPERTY_TYPE_OF_DEVICE);
     87 }
     88 
     89 bt_property_t *property_new_discovery_timeout(const uint32_t timeout) {
     90   return property_new_((void *)&timeout, sizeof(uint32_t), BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT);
     91 }
     92 
     93 bt_property_t *property_new_name(const char *name) {
     94   assert(name != NULL);
     95   return property_new_((void *)name, sizeof(bt_bdname_t), BT_PROPERTY_BDNAME);
     96 }
     97 
     98 bt_property_t *property_new_rssi(int8_t rssi) {
     99   return property_new_((void *)&rssi, sizeof(int8_t), BT_PROPERTY_REMOTE_RSSI);
    100 }
    101 
    102 bt_property_t *property_new_scan_mode(bt_scan_mode_t scan_mode) {
    103   return property_new_((void *)&scan_mode, sizeof(bt_scan_mode_t), BT_PROPERTY_ADAPTER_SCAN_MODE);
    104 }
    105 
    106 bt_property_t *property_new_uuids(const bt_uuid_t *uuid, size_t count) {
    107   assert(uuid != NULL);
    108   return property_new_((void *)uuid, sizeof(bt_uuid_t) * count, BT_PROPERTY_UUIDS);
    109 }
    110 
    111 void property_free(bt_property_t *property) {
    112   property_free_array(property, 1);
    113 }
    114 
    115 void property_free_array(bt_property_t *properties, size_t count) {
    116   if (properties == NULL)
    117     return;
    118 
    119   for (size_t i = 0; i < count; ++i) {
    120     osi_free(properties[i].val);
    121   }
    122 
    123   osi_free(properties);
    124 }
    125 
    126 bool property_is_addr(const bt_property_t *property) {
    127   assert(property != NULL);
    128   return property->type == BT_PROPERTY_BDADDR;
    129 }
    130 
    131 bool property_is_device_class(const bt_property_t *property) {
    132   assert(property != NULL);
    133   return property->type == BT_PROPERTY_CLASS_OF_DEVICE;
    134 }
    135 
    136 bool property_is_device_type(const bt_property_t *property) {
    137   assert(property != NULL);
    138   return property->type == BT_PROPERTY_TYPE_OF_DEVICE;
    139 }
    140 
    141 bool property_is_discovery_timeout(const bt_property_t *property) {
    142   assert(property != NULL);
    143   return property->type == BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
    144 }
    145 
    146 bool property_is_name(const bt_property_t *property) {
    147   assert(property != NULL);
    148   return property->type == BT_PROPERTY_BDNAME;
    149 }
    150 
    151 bool property_is_rssi(const bt_property_t *property) {
    152   assert(property != NULL);
    153   return property->type == BT_PROPERTY_REMOTE_RSSI;
    154 }
    155 
    156 bool property_is_scan_mode(const bt_property_t *property) {
    157   assert(property != NULL);
    158   return property->type == BT_PROPERTY_ADAPTER_SCAN_MODE;
    159 }
    160 
    161 bool property_is_uuids(const bt_property_t *property) {
    162   assert(property != NULL);
    163   return property->type == BT_PROPERTY_UUIDS;
    164 }
    165 
    166 // Convenience conversion methods to property values
    167 const bt_bdaddr_t *property_as_addr(const bt_property_t *property) {
    168   assert(property_is_addr(property));
    169   return (const bt_bdaddr_t *)property->val;
    170 }
    171 
    172 const bt_device_class_t *property_as_device_class(const bt_property_t *property) {
    173   assert(property_is_device_class(property));
    174   return (const bt_device_class_t *)property->val;
    175 }
    176 
    177 bt_device_type_t property_as_device_type(const bt_property_t *property) {
    178   assert(property_is_device_type(property));
    179   return *(const bt_device_type_t *)property->val;
    180 }
    181 
    182 uint32_t property_as_discovery_timeout(const bt_property_t *property) {
    183   assert(property_is_discovery_timeout(property));
    184   return *(const uint32_t *)property->val;
    185 }
    186 
    187 const bt_bdname_t *property_as_name(const bt_property_t *property) {
    188   assert(property_is_name(property));
    189   return (const bt_bdname_t *)property->val;
    190 }
    191 
    192 int8_t property_as_rssi(const bt_property_t *property) {
    193   assert(property_is_rssi(property));
    194   return *(const int8_t *)property->val;
    195 }
    196 
    197 bt_scan_mode_t property_as_scan_mode(const bt_property_t *property) {
    198   assert(property_is_scan_mode(property));
    199   return *(const bt_scan_mode_t *)property->val;
    200 }
    201 
    202 const bt_uuid_t *property_as_uuids(const bt_property_t *property, size_t *count) {
    203   assert(property_is_uuids(property));
    204   *count = sizeof(bt_uuid_t) / property->len;
    205   return (const bt_uuid_t *)property->val;
    206 }
    207 
    208 static bt_property_t *property_new_(void *val, size_t len, bt_property_type_t type) {
    209   bt_property_t *property = osi_calloc(sizeof(bt_property_t));
    210 
    211   property->val = osi_malloc(len);
    212   memcpy(property->val, val, len);
    213 
    214   property->type = type;
    215   property->len = len;
    216 
    217   return property;
    218 }
    219