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 <ctype.h>
     21 #include <stdio.h>
     22 #include <string.h>
     23 
     24 #include "btcore/include/bdaddr.h"
     25 
     26 bool bdaddr_is_empty(const bt_bdaddr_t *addr) {
     27   assert(addr != NULL);
     28 
     29   uint8_t zero[sizeof(bt_bdaddr_t)] = { 0 };
     30   return memcmp(addr, &zero, sizeof(bt_bdaddr_t)) == 0;
     31 }
     32 
     33 bool bdaddr_equals(const bt_bdaddr_t *first, const bt_bdaddr_t *second) {
     34   assert(first != NULL);
     35   assert(second != NULL);
     36 
     37   return memcmp(first, second, sizeof(bt_bdaddr_t)) == 0;
     38 }
     39 
     40 bt_bdaddr_t *bdaddr_copy(bt_bdaddr_t *dest, const bt_bdaddr_t *src) {
     41   assert(dest != NULL);
     42   assert(src != NULL);
     43 
     44   return (bt_bdaddr_t *)memcpy(dest, src, sizeof(bt_bdaddr_t));
     45 }
     46 
     47 const char *bdaddr_to_string(const bt_bdaddr_t *addr, char *string, size_t size) {
     48   assert(addr != NULL);
     49   assert(string != NULL);
     50 
     51   if (size < 18)
     52     return NULL;
     53 
     54   const uint8_t *ptr = addr->address;
     55   sprintf(string, "%02x:%02x:%02x:%02x:%02x:%02x",
     56            ptr[0], ptr[1], ptr[2],
     57            ptr[3], ptr[4], ptr[5]);
     58   return string;
     59 }
     60 
     61 bool string_is_bdaddr(const char *string) {
     62   assert(string != NULL);
     63 
     64   size_t len = strlen(string);
     65   if (len != 17)
     66     return false;
     67 
     68   for (size_t i = 0; i < len; ++i) {
     69     // Every 3rd char must be ':'.
     70     if (((i + 1) % 3) == 0 && string[i] != ':')
     71       return false;
     72 
     73     // All other chars must be a hex digit.
     74     if (((i + 1) % 3) != 0 && !isxdigit(string[i]))
     75       return false;
     76   }
     77   return true;
     78 }
     79 
     80 bool string_to_bdaddr(const char *string, bt_bdaddr_t *addr) {
     81   assert(string != NULL);
     82   assert(addr != NULL);
     83 
     84   bt_bdaddr_t new_addr;
     85   uint8_t *ptr = new_addr.address;
     86   bool ret = sscanf(string, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
     87       &ptr[0], &ptr[1], &ptr[2], &ptr[3], &ptr[4], &ptr[5]) == 6;
     88 
     89   if (ret)
     90     memcpy(addr, &new_addr, sizeof(bt_bdaddr_t));
     91 
     92   return ret;
     93 }
     94 
     95 hash_index_t hash_function_bdaddr(const void *key) {
     96   hash_index_t hash = 5381;
     97   const char *bytes = (const char *)key;
     98   for (size_t i = 0; i < sizeof(bt_bdaddr_t); ++i)
     99     hash = ((hash << 5) + hash) + bytes[i];
    100   return hash;
    101 }
    102