bt_common.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <assert.h>
  16. #include "bt_user_config.h"
  17. #include "esp_log.h"
  18. #ifndef FALSE
  19. #define FALSE false
  20. #endif
  21. #ifndef TRUE
  22. #define TRUE true
  23. #endif
  24. #ifndef BT_QUEUE_CONGEST_SIZE
  25. #define BT_QUEUE_CONGEST_SIZE 40
  26. #endif
  27. #define BTC_INITIAL_TRACE_LEVEL UC_BT_LOG_BTC_TRACE_LEVEL
  28. #define OSI_INITIAL_TRACE_LEVEL UC_BT_LOG_OSI_TRACE_LEVEL
  29. #if UC_BT_BLE_DYNAMIC_ENV_MEMORY
  30. #define BT_BLE_DYNAMIC_ENV_MEMORY TRUE
  31. #define BTC_DYNAMIC_MEMORY TRUE
  32. #else
  33. #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE
  34. #define BTC_DYNAMIC_MEMORY FALSE
  35. #endif
  36. #ifndef BT_BLE_DYNAMIC_ENV_MEMORY
  37. #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE
  38. #endif
  39. /* OS Configuration from User config (eg: sdkconfig) */
  40. #define TASK_PINNED_TO_CORE UC_TASK_PINNED_TO_CORE
  41. #define BT_TASK_MAX_PRIORITIES configMAX_PRIORITIES
  42. #define BT_BTC_TASK_STACK_SIZE UC_BTC_TASK_STACK_SIZE
  43. /* Define trace levels */
  44. #define BT_TRACE_LEVEL_NONE UC_TRACE_LEVEL_NONE /* No trace messages to be generated */
  45. #define BT_TRACE_LEVEL_ERROR UC_TRACE_LEVEL_ERROR /* Error condition trace messages */
  46. #define BT_TRACE_LEVEL_WARNING UC_TRACE_LEVEL_WARNING /* Warning condition trace messages */
  47. #define BT_TRACE_LEVEL_API UC_TRACE_LEVEL_API /* API traces */
  48. #define BT_TRACE_LEVEL_EVENT UC_TRACE_LEVEL_EVENT /* Debug messages for events */
  49. #define BT_TRACE_LEVEL_DEBUG UC_TRACE_LEVEL_DEBUG /* Full debug messages */
  50. #define BT_TRACE_LEVEL_VERBOSE UC_TRACE_LEVEL_VERBOSE /* Verbose debug messages */
  51. #define MAX_TRACE_LEVEL UC_TRACE_LEVEL_VERBOSE
  52. #ifndef LOG_LOCAL_LEVEL
  53. #ifndef BOOTLOADER_BUILD
  54. #define LOG_LOCAL_LEVEL UC_LOG_DEFAULT_LEVEL
  55. #else
  56. #define LOG_LOCAL_LEVEL UC_BOOTLOADER_LOG_LEVEL
  57. #endif
  58. #endif
  59. // Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL
  60. #if (LOG_LOCAL_LEVEL >= 4)
  61. #define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1)
  62. #else
  63. #define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL
  64. #endif
  65. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  66. #define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL)
  67. #define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  68. #define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  69. #define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  70. #define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  71. #define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); }
  72. #if !UC_BT_STACK_NO_LOG
  73. /* define traces for BTC */
  74. #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);}
  75. #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);}
  76. #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);}
  77. #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);}
  78. #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);}
  79. #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);}
  80. /* define traces for OSI */
  81. #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);}
  82. #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);}
  83. #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);}
  84. #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);}
  85. #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);}
  86. #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);}
  87. #else
  88. /* define traces for BTC */
  89. #define BTC_TRACE_ERROR(fmt, args...)
  90. #define BTC_TRACE_WARNING(fmt, args...)
  91. #define BTC_TRACE_API(fmt, args...)
  92. #define BTC_TRACE_EVENT(fmt, args...)
  93. #define BTC_TRACE_DEBUG(fmt, args...)
  94. #define BTC_TRACE_VERBOSE(fmt, args...)
  95. /* define traces for OSI */
  96. #define OSI_TRACE_ERROR(fmt, args...)
  97. #define OSI_TRACE_WARNING(fmt, args...)
  98. #define OSI_TRACE_API(fmt, args...)
  99. #define OSI_TRACE_EVENT(fmt, args...)
  100. #define OSI_TRACE_DEBUG(fmt, args...)
  101. #define OSI_TRACE_VERBOSE(fmt, args...)
  102. #endif
  103. /** Bluetooth Error Status */
  104. /** We need to build on this */
  105. /* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */
  106. typedef enum {
  107. BT_STATUS_SUCCESS = 0,
  108. BT_STATUS_FAIL,
  109. BT_STATUS_NOT_READY,
  110. BT_STATUS_NOMEM,
  111. BT_STATUS_BUSY,
  112. BT_STATUS_DONE, /* request already completed */
  113. BT_STATUS_UNSUPPORTED,
  114. BT_STATUS_PARM_INVALID,
  115. BT_STATUS_UNHANDLED,
  116. BT_STATUS_AUTH_FAILURE,
  117. BT_STATUS_RMT_DEV_DOWN,
  118. BT_STATUS_AUTH_REJECTED,
  119. BT_STATUS_INVALID_STATIC_RAND_ADDR,
  120. BT_STATUS_PENDING,
  121. BT_STATUS_UNACCEPT_CONN_INTERVAL,
  122. BT_STATUS_PARAM_OUT_OF_RANGE,
  123. BT_STATUS_TIMEOUT,
  124. BT_STATUS_MEMORY_FULL,
  125. BT_STATUS_EIR_TOO_LARGE,
  126. } bt_status_t;
  127. #endif /* _BT_COMMON_H_ */