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