hal-utils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* SPDX-License-Identifier: Apache-2.0 */
  2. /*
  3. * Copyright (C) 2013 Intel Corporation
  4. *
  5. */
  6. #include <endian.h>
  7. #include <hardware/bluetooth.h>
  8. #define MAX_UUID_STR_LEN 37
  9. #define HAL_UUID_LEN 16
  10. #define MAX_ADDR_STR_LEN 18
  11. static const char BT_BASE_UUID[] = {
  12. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  13. 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb
  14. };
  15. const char *bt_uuid_t2str(const uint8_t *uuid, char *buf);
  16. const char *btuuid2str(const uint8_t *uuid);
  17. const char *bt_bdaddr_t2str(const bt_bdaddr_t *bd_addr, char *buf);
  18. void str2bt_bdaddr_t(const char *str, bt_bdaddr_t *bd_addr);
  19. void str2bt_uuid_t(const char *str, bt_uuid_t *uuid);
  20. const char *btproperty2str(const bt_property_t *property);
  21. const char *bdaddr2str(const bt_bdaddr_t *bd_addr);
  22. int get_config(const char *config_key, char *value, const char *fallback);
  23. /*
  24. * Begin mapping section
  25. *
  26. * There are some mappings between integer values (enums) and strings
  27. * to be presented to user. To make it easier to convert between those two
  28. * set of macros is given. It is specially useful when we want to have
  29. * strings that match constants from header files like:
  30. * BT_STATUS_SUCCESS (0) and corresponding "BT_STATUS_SUCCESS"
  31. * Example of usage:
  32. *
  33. * INTMAP(int, -1, "invalid")
  34. * DELEMENT(BT_STATUS_SUCCESS)
  35. * DELEMENT(BT_STATUS_FAIL)
  36. * MELEMENT(123, "Some strange value")
  37. * ENDMAP
  38. *
  39. * Just by doing this we have mapping table plus two functions:
  40. * int str2int(const char *str);
  41. * const char *int2str(int v);
  42. *
  43. * second argument to INTMAP specifies value to be returned from
  44. * str2int function when there is not mapping for such number
  45. * third argument specifies default value to be returned from int2str
  46. *
  47. * If same mapping is to be used in several source files put
  48. * INTMAP in c file and DECINTMAP in h file.
  49. *
  50. * For mappings that are to be used in single file only
  51. * use SINTMAP which will create the same but everything will be marked
  52. * as static.
  53. */
  54. struct int2str {
  55. int val; /* int value */
  56. const char *str; /* corresponding string */
  57. };
  58. int int2str_findint(int v, const struct int2str m[]);
  59. int int2str_findstr(const char *str, const struct int2str m[]);
  60. const char *enum_defines(void *v, int i);
  61. const char *enum_strings(void *v, int i);
  62. const char *enum_one_string(void *v, int i);
  63. #define TYPE_ENUM(type) ((void *) &__##type##2str[0])
  64. #define DECINTMAP(type) \
  65. extern struct int2str __##type##2str[]; \
  66. const char *type##2##str(type v); \
  67. type str##2##type(const char *str); \
  68. #define INTMAP(type, deft, defs) \
  69. const char *type##2##str(type v) \
  70. { \
  71. int i = int2str_findint((int) v, __##type##2str); \
  72. return (i < 0) ? defs : __##type##2str[i].str; \
  73. } \
  74. type str##2##type(const char *str) \
  75. { \
  76. int i = int2str_findstr(str, __##type##2str); \
  77. return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
  78. } \
  79. struct int2str __##type##2str[] = {
  80. #define SINTMAP(type, deft, defs) \
  81. static struct int2str __##type##2str[]; \
  82. static inline const char *type##2##str(type v) \
  83. { \
  84. int i = int2str_findint((int) v, __##type##2str); \
  85. return (i < 0) ? defs : __##type##2str[i].str; \
  86. } \
  87. static inline type str##2##type(const char *str) \
  88. { \
  89. int i = int2str_findstr(str, __##type##2str); \
  90. return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
  91. } \
  92. static struct int2str __##type##2str[] = {
  93. #define ENDMAP {0, NULL} };
  94. /* use this to generate string from header file constant */
  95. #define MELEMENT(v, s) {v, s}
  96. /* use this to have arbitrary mapping from int to string */
  97. #define DELEMENT(s) {s, #s}
  98. /* End of mapping section */
  99. DECINTMAP(bt_status_t);
  100. DECINTMAP(bt_state_t);
  101. DECINTMAP(bt_device_type_t);
  102. DECINTMAP(bt_scan_mode_t);
  103. DECINTMAP(bt_discovery_state_t);
  104. DECINTMAP(bt_acl_state_t);
  105. DECINTMAP(bt_bond_state_t);
  106. DECINTMAP(bt_ssp_variant_t);
  107. DECINTMAP(bt_property_type_t);
  108. DECINTMAP(bt_cb_thread_evt);
  109. static inline uint16_t get_le16(const void *src)
  110. {
  111. const struct __attribute__((packed)) {
  112. uint16_t le16;
  113. } *p = src;
  114. return le16toh(p->le16);
  115. }
  116. static inline void put_le16(uint16_t val, void *dst)
  117. {
  118. struct __attribute__((packed)) {
  119. uint16_t le16;
  120. } *p = dst;
  121. p->le16 = htole16(val);
  122. }