2016-09-26 13:37:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "bt_trace.h"
|
|
|
|
#include "bdaddr.h"
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static inline bool ets_isxdigit(char c)
|
|
|
|
{
|
|
|
|
if ((c >= '0') && (c <= '9')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((c >= 'a') && (c <= 'f')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return ((c >= 'A') && (c <= 'F'));
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bool bdaddr_is_empty(const bt_bdaddr_t *addr)
|
|
|
|
{
|
|
|
|
assert(addr != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
uint8_t zero[sizeof(bt_bdaddr_t)] = { 0 };
|
|
|
|
return memcmp(addr, &zero, sizeof(bt_bdaddr_t)) == 0;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bool bdaddr_equals(const bt_bdaddr_t *first, const bt_bdaddr_t *second)
|
|
|
|
{
|
|
|
|
assert(first != NULL);
|
|
|
|
assert(second != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return memcmp(first, second, sizeof(bt_bdaddr_t)) == 0;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bt_bdaddr_t *bdaddr_copy(bt_bdaddr_t *dest, const bt_bdaddr_t *src)
|
|
|
|
{
|
|
|
|
assert(dest != NULL);
|
|
|
|
assert(src != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return (bt_bdaddr_t *)memcpy(dest, src, sizeof(bt_bdaddr_t));
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
const char *bdaddr_to_string(const bt_bdaddr_t *addr, char *string, size_t size)
|
|
|
|
{
|
|
|
|
assert(addr != NULL);
|
|
|
|
assert(string != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (size < 18) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
const uint8_t *ptr = addr->address;
|
|
|
|
sprintf(string, "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
ptr[0], ptr[1], ptr[2],
|
|
|
|
ptr[3], ptr[4], ptr[5]);
|
|
|
|
return string;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bool string_is_bdaddr(const char *string)
|
|
|
|
{
|
|
|
|
assert(string != NULL);
|
|
|
|
|
|
|
|
size_t len = strlen(string);
|
|
|
|
if (len != 17) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
// Every 3rd char must be ':'.
|
|
|
|
if (((i + 1) % 3) == 0 && string[i] != ':') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All other chars must be a hex digit.
|
|
|
|
if (((i + 1) % 3) != 0 && !ets_isxdigit(string[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bool string_to_bdaddr(const char *string, bt_bdaddr_t *addr)
|
|
|
|
{
|
|
|
|
assert(string != NULL);
|
|
|
|
assert(addr != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bt_bdaddr_t new_addr;
|
|
|
|
uint8_t *ptr = new_addr.address;
|
|
|
|
bool ret = sscanf(string, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
|
|
|
|
&ptr[0], &ptr[1], &ptr[2], &ptr[3], &ptr[4], &ptr[5]) == 6;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (ret) {
|
|
|
|
memcpy(addr, &new_addr, sizeof(bt_bdaddr_t));
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return ret;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
hash_index_t hash_function_bdaddr(const void *key)
|
|
|
|
{
|
|
|
|
hash_index_t hash = 5381;
|
|
|
|
const char *bytes = (const char *)key;
|
|
|
|
for (size_t i = 0; i < sizeof(bt_bdaddr_t); ++i) {
|
|
|
|
hash = ((hash << 5) + hash) + bytes[i];
|
|
|
|
}
|
|
|
|
return hash;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|