bt_common.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "esp_log.h"
  16. #ifndef FALSE
  17. #define FALSE false
  18. #endif
  19. #ifndef TRUE
  20. #define TRUE true
  21. #endif
  22. #ifndef BT_QUEUE_CONGEST_SIZE
  23. #define BT_QUEUE_CONGEST_SIZE 40
  24. #endif
  25. #ifdef CONFIG_BTC_INITIAL_TRACE_LEVEL
  26. #define BTC_INITIAL_TRACE_LEVEL CONFIG_BTC_INITIAL_TRACE_LEVEL
  27. #else
  28. #define BTC_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
  29. #endif
  30. #ifdef CONFIG_OSI_INITIAL_TRACE_LEVEL
  31. #define OSI_INITIAL_TRACE_LEVEL CONFIG_OSI_INITIAL_TRACE_LEVEL
  32. #else
  33. #define OSI_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
  34. #endif
  35. #if CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY
  36. #define BT_BLE_DYNAMIC_ENV_MEMORY TRUE
  37. #else
  38. #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE
  39. #endif
  40. #ifdef CONFIG_BLUEDROID_PINNED_TO_CORE
  41. #define TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY)
  42. #define BTC_TASK_STACK_SIZE (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig
  43. #endif
  44. #ifdef CONFIG_NIMBLE_ENABLED
  45. #define TASK_PINNED_TO_CORE (CONFIG_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY)
  46. #define BTC_TASK_STACK_SIZE 4096
  47. #endif
  48. #define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
  49. #define BTC_TASK_PRIO (configMAX_PRIORITIES - 6)
  50. #define BTC_TASK_QUEUE_LEN 60
  51. /* Define trace levels */
  52. #define BT_TRACE_LEVEL_NONE 0 /* No trace messages to be generated */
  53. #define BT_TRACE_LEVEL_ERROR 1 /* Error condition trace messages */
  54. #define BT_TRACE_LEVEL_WARNING 2 /* Warning condition trace messages */
  55. #define BT_TRACE_LEVEL_API 3 /* API traces */
  56. #define BT_TRACE_LEVEL_EVENT 4 /* Debug messages for events */
  57. #define BT_TRACE_LEVEL_DEBUG 5 /* Full debug messages */
  58. #define BT_TRACE_LEVEL_VERBOSE 6 /* Verbose debug messages */
  59. #define MAX_TRACE_LEVEL 6
  60. #ifndef LOG_LOCAL_LEVEL
  61. #ifndef BOOTLOADER_BUILD
  62. #define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
  63. #else
  64. #define LOG_LOCAL_LEVEL CONFIG_LOG_BOOTLOADER_LEVEL
  65. #endif
  66. #endif
  67. // Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL
  68. #if (LOG_LOCAL_LEVEL >= 4)
  69. #define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1)
  70. #else
  71. #define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL
  72. #endif
  73. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  74. #define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL)
  75. #define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  76. #define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  77. #define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  78. #define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  79. #define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  80. #ifndef assert
  81. #define assert(x) do { if (!(x)) BT_PRINT_E("BT", "bt host error %s %u\n", __FILE__, __LINE__); } while (0)
  82. #endif
  83. #if !CONFIG_BT_STACK_NO_LOG
  84. /* define traces for BTC */
  85. #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);}
  86. #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);}
  87. #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);}
  88. #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);}
  89. #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);}
  90. #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);}
  91. /* define traces for OSI */
  92. #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);}
  93. #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);}
  94. #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);}
  95. #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);}
  96. #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);}
  97. #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);}
  98. #else
  99. /* define traces for BTC */
  100. #define BTC_TRACE_ERROR(fmt, args...)
  101. #define BTC_TRACE_WARNING(fmt, args...)
  102. #define BTC_TRACE_API(fmt, args...)
  103. #define BTC_TRACE_EVENT(fmt, args...)
  104. #define BTC_TRACE_DEBUG(fmt, args...)
  105. #define BTC_TRACE_VERBOSE(fmt, args...)
  106. /* define traces for OSI */
  107. #define OSI_TRACE_ERROR(fmt, args...)
  108. #define OSI_TRACE_WARNING(fmt, args...)
  109. #define OSI_TRACE_API(fmt, args...)
  110. #define OSI_TRACE_EVENT(fmt, args...)
  111. #define OSI_TRACE_DEBUG(fmt, args...)
  112. #define OSI_TRACE_VERBOSE(fmt, args...)
  113. #endif
  114. /** Bluetooth Error Status */
  115. /** We need to build on this */
  116. /* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */
  117. typedef enum {
  118. BT_STATUS_SUCCESS = 0,
  119. BT_STATUS_FAIL,
  120. BT_STATUS_NOT_READY,
  121. BT_STATUS_NOMEM,
  122. BT_STATUS_BUSY,
  123. BT_STATUS_DONE, /* request already completed */
  124. BT_STATUS_UNSUPPORTED,
  125. BT_STATUS_PARM_INVALID,
  126. BT_STATUS_UNHANDLED,
  127. BT_STATUS_AUTH_FAILURE,
  128. BT_STATUS_RMT_DEV_DOWN,
  129. BT_STATUS_AUTH_REJECTED,
  130. BT_STATUS_INVALID_STATIC_RAND_ADDR,
  131. BT_STATUS_PENDING,
  132. BT_STATUS_UNACCEPT_CONN_INTERVAL,
  133. BT_STATUS_PARAM_OUT_OF_RANGE,
  134. BT_STATUS_TIMEOUT,
  135. BT_STATUS_MEMORY_FULL,
  136. BT_STATUS_EIR_TOO_LARGE,
  137. } bt_status_t;
  138. #endif /* _BT_COMMON_H_ */