bt_common.h 9.4 KB

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