bt_common.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _BT_COMMON_H_
  7. #define _BT_COMMON_H_
  8. #include <assert.h>
  9. #include <stdbool.h>
  10. #include "bt_user_config.h"
  11. #include "esp_log.h"
  12. #ifndef FALSE
  13. #define FALSE false
  14. #endif
  15. #ifndef TRUE
  16. #define TRUE true
  17. #endif
  18. #if (UC_BT_BLUFI_ENABLE)
  19. #define BLUFI_INCLUDED TRUE
  20. #else
  21. #define BLUFI_INCLUDED FALSE
  22. #endif
  23. #ifdef CONFIG_BT_BLUEDROID_ENABLED
  24. #include "esp_bt_defs.h"
  25. #include "esp_bt_main.h"
  26. #include "esp_gatt_defs.h"
  27. #define ESP_BLE_HOST_STATUS_ENABLED ESP_BLUEDROID_STATUS_ENABLED
  28. #define ESP_BLE_HOST_STATUS_CHECK(status) ESP_BLUEDROID_STATUS_CHECK(status)
  29. #else
  30. #define ESP_BLE_HOST_STATUS_ENABLED 0
  31. #define ESP_BLE_HOST_STATUS_CHECK(status) do {} while (0)
  32. #endif
  33. #ifndef BT_QUEUE_CONGEST_SIZE
  34. #define BT_QUEUE_CONGEST_SIZE 40
  35. #endif
  36. #define BTC_INITIAL_TRACE_LEVEL UC_BT_LOG_BTC_TRACE_LEVEL
  37. #define OSI_INITIAL_TRACE_LEVEL UC_BT_LOG_OSI_TRACE_LEVEL
  38. #define BLUFI_INITIAL_TRACE_LEVEL UC_BT_LOG_BLUFI_TRACE_LEVEL
  39. #if UC_BT_BLE_DYNAMIC_ENV_MEMORY
  40. #define BT_BLE_DYNAMIC_ENV_MEMORY TRUE
  41. #define BTC_DYNAMIC_MEMORY TRUE
  42. #else
  43. #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE
  44. #define BTC_DYNAMIC_MEMORY FALSE
  45. #endif
  46. #ifndef BT_BLE_DYNAMIC_ENV_MEMORY
  47. #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE
  48. #endif
  49. /* OS Configuration from User config (eg: sdkconfig) */
  50. #define TASK_PINNED_TO_CORE UC_TASK_PINNED_TO_CORE
  51. #define BT_TASK_MAX_PRIORITIES configMAX_PRIORITIES
  52. #define BT_BTC_TASK_STACK_SIZE UC_BTC_TASK_STACK_SIZE
  53. /* Define trace levels */
  54. #define BT_TRACE_LEVEL_NONE UC_TRACE_LEVEL_NONE /* No trace messages to be generated */
  55. #define BT_TRACE_LEVEL_ERROR UC_TRACE_LEVEL_ERROR /* Error condition trace messages */
  56. #define BT_TRACE_LEVEL_WARNING UC_TRACE_LEVEL_WARNING /* Warning condition trace messages */
  57. #define BT_TRACE_LEVEL_API UC_TRACE_LEVEL_API /* API traces */
  58. #define BT_TRACE_LEVEL_EVENT UC_TRACE_LEVEL_EVENT /* Debug messages for events */
  59. #define BT_TRACE_LEVEL_DEBUG UC_TRACE_LEVEL_DEBUG /* Full debug messages */
  60. #define BT_TRACE_LEVEL_VERBOSE UC_TRACE_LEVEL_VERBOSE /* Verbose debug messages */
  61. #define MAX_TRACE_LEVEL UC_TRACE_LEVEL_VERBOSE
  62. #ifndef LOG_LOCAL_LEVEL
  63. #ifndef BOOTLOADER_BUILD
  64. #define LOG_LOCAL_LEVEL UC_LOG_DEFAULT_LEVEL
  65. #else
  66. #define LOG_LOCAL_LEVEL UC_BOOTLOADER_LOG_LEVEL
  67. #endif
  68. #endif
  69. // Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL
  70. #if (LOG_LOCAL_LEVEL >= 4)
  71. #define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1)
  72. #else
  73. #define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL
  74. #endif
  75. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  76. #define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL)
  77. #define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  78. #define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  79. #define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  80. #define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  81. #define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  82. #if !UC_BT_STACK_NO_LOG
  83. /* define traces for BTC */
  84. #define BTC_TRACE_ERROR(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BTC, ERROR)) BT_PRINT_E("BT_BTC", fmt, ## args);}
  85. #define BTC_TRACE_WARNING(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BTC, WARNING)) BT_PRINT_W("BT_BTC", fmt, ## args);}
  86. #define BTC_TRACE_API(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BTC,API)) BT_PRINT_I("BT_BTC", fmt, ## args);}
  87. #define BTC_TRACE_EVENT(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BTC,EVENT)) BT_PRINT_D("BT_BTC", fmt, ## args);}
  88. #define BTC_TRACE_DEBUG(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BTC,DEBUG)) BT_PRINT_D("BT_BTC", fmt, ## args);}
  89. #define BTC_TRACE_VERBOSE(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BTC,VERBOSE)) BT_PRINT_V("BT_BTC", fmt, ## args);}
  90. /* define traces for OSI */
  91. #define OSI_TRACE_ERROR(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(OSI, ERROR)) BT_PRINT_E("BT_OSI", fmt, ## args);}
  92. #define OSI_TRACE_WARNING(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(OSI, WARNING)) BT_PRINT_W("BT_OSI", fmt, ## args);}
  93. #define OSI_TRACE_API(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(OSI,API)) BT_PRINT_I("BT_OSI", fmt, ## args);}
  94. #define OSI_TRACE_EVENT(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(OSI,EVENT)) BT_PRINT_D("BT_OSI", fmt, ## args);}
  95. #define OSI_TRACE_DEBUG(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(OSI,DEBUG)) BT_PRINT_D("BT_OSI", fmt, ## args);}
  96. #define OSI_TRACE_VERBOSE(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(OSI,VERBOSE)) BT_PRINT_V("BT_OSI", fmt, ## args);}
  97. /* define traces for BLUFI */
  98. #define BLUFI_TRACE_ERROR(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BLUFI, ERROR)) BT_PRINT_E("BT_BLUFI", fmt, ## args);}
  99. #define BLUFI_TRACE_WARNING(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BLUFI, WARNING)) BT_PRINT_W("BT_BLUFI", fmt, ## args);}
  100. #define BLUFI_TRACE_API(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BLUFI,API)) BT_PRINT_I("BT_BLUFI", fmt, ## args);}
  101. #define BLUFI_TRACE_EVENT(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BLUFI,EVENT)) BT_PRINT_D("BT_BLUFI", fmt, ## args);}
  102. #define BLUFI_TRACE_DEBUG(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BLUFI,DEBUG)) BT_PRINT_D("BT_BLUFI", fmt, ## args);}
  103. #define BLUFI_TRACE_VERBOSE(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BLUFI,VERBOSE)) BT_PRINT_V("BT_BLUFI", fmt, ## args);}
  104. #else
  105. /* define traces for BTC */
  106. #define BTC_TRACE_ERROR(fmt, args...)
  107. #define BTC_TRACE_WARNING(fmt, args...)
  108. #define BTC_TRACE_API(fmt, args...)
  109. #define BTC_TRACE_EVENT(fmt, args...)
  110. #define BTC_TRACE_DEBUG(fmt, args...)
  111. #define BTC_TRACE_VERBOSE(fmt, args...)
  112. /* define traces for OSI */
  113. #define OSI_TRACE_ERROR(fmt, args...)
  114. #define OSI_TRACE_WARNING(fmt, args...)
  115. #define OSI_TRACE_API(fmt, args...)
  116. #define OSI_TRACE_EVENT(fmt, args...)
  117. #define OSI_TRACE_DEBUG(fmt, args...)
  118. #define OSI_TRACE_VERBOSE(fmt, args...)
  119. /* define traces for BLUFI */
  120. #define BLUFI_TRACE_ERROR(fmt, args...)
  121. #define BLUFI_TRACE_WARNING(fmt, args...)
  122. #define BLUFI_TRACE_API(fmt, args...)
  123. #define BLUFI_TRACE_EVENT(fmt, args...)
  124. #define BLUFI_TRACE_DEBUG(fmt, args...)
  125. #define BLUFI_TRACE_VERBOSE(fmt, args...)
  126. #endif
  127. /** Bluetooth Error Status */
  128. /** We need to build on this */
  129. /* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */
  130. typedef enum {
  131. BT_STATUS_SUCCESS = 0,
  132. BT_STATUS_FAIL,
  133. BT_STATUS_NOT_READY,
  134. BT_STATUS_NOMEM,
  135. BT_STATUS_BUSY,
  136. BT_STATUS_DONE, /* request already completed */
  137. BT_STATUS_UNSUPPORTED,
  138. BT_STATUS_PARM_INVALID,
  139. BT_STATUS_UNHANDLED,
  140. BT_STATUS_AUTH_FAILURE,
  141. BT_STATUS_RMT_DEV_DOWN,
  142. BT_STATUS_AUTH_REJECTED,
  143. BT_STATUS_INVALID_STATIC_RAND_ADDR,
  144. BT_STATUS_PENDING,
  145. BT_STATUS_UNACCEPT_CONN_INTERVAL,
  146. BT_STATUS_PARAM_OUT_OF_RANGE,
  147. BT_STATUS_TIMEOUT,
  148. BT_STATUS_MEMORY_FULL,
  149. BT_STATUS_EIR_TOO_LARGE,
  150. } bt_status_t;
  151. typedef uint8_t UINT8;
  152. typedef uint16_t UINT16;
  153. typedef uint32_t UINT32;
  154. typedef uint64_t UINT64;
  155. typedef bool BOOLEAN;
  156. /* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */
  157. #define MAX_UUID_SIZE 16
  158. typedef struct {
  159. #define LEN_UUID_16 2
  160. #define LEN_UUID_32 4
  161. #define LEN_UUID_128 16
  162. UINT16 len;
  163. union {
  164. UINT16 uuid16;
  165. UINT32 uuid32;
  166. UINT8 uuid128[MAX_UUID_SIZE];
  167. } uu;
  168. } tBT_UUID;
  169. /* Common Bluetooth field definitions */
  170. #define BD_ADDR_LEN 6 /* Device address length */
  171. typedef UINT8 BD_ADDR[BD_ADDR_LEN]; /* Device address */
  172. #endif /* _BT_COMMON_H_ */