bdaddr.c 3.2 KB

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