1 /* 2 * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef _BT_COMMON_H_ 8 #define _BT_COMMON_H_ 9 10 #include <assert.h> 11 #include <stdbool.h> 12 #include "bt_user_config.h" 13 #include "esp_log.h" 14 15 #ifndef FALSE 16 #define FALSE false 17 #endif 18 19 #ifndef TRUE 20 #define TRUE true 21 #endif 22 23 24 #if (UC_BT_BLUFI_ENABLE) 25 #define BLUFI_INCLUDED TRUE 26 #else 27 #define BLUFI_INCLUDED FALSE 28 #endif 29 30 #ifdef CONFIG_BT_BLUEDROID_ENABLED 31 #include "esp_bt_defs.h" 32 #include "esp_bt_main.h" 33 #include "esp_gatt_defs.h" 34 #define ESP_BLE_HOST_STATUS_ENABLED ESP_BLUEDROID_STATUS_ENABLED 35 #define ESP_BLE_HOST_STATUS_CHECK(status) ESP_BLUEDROID_STATUS_CHECK(status) 36 #else 37 #define ESP_BLE_HOST_STATUS_ENABLED 0 38 #define ESP_BLE_HOST_STATUS_CHECK(status) do {} while (0) 39 #endif 40 41 #ifndef BT_QUEUE_CONGEST_SIZE 42 #define BT_QUEUE_CONGEST_SIZE 40 43 #endif 44 45 #define BTC_INITIAL_TRACE_LEVEL UC_BT_LOG_BTC_TRACE_LEVEL 46 #define OSI_INITIAL_TRACE_LEVEL UC_BT_LOG_OSI_TRACE_LEVEL 47 #define BLUFI_INITIAL_TRACE_LEVEL UC_BT_LOG_BLUFI_TRACE_LEVEL 48 49 // MEMORY 50 #if UC_BT_BLE_DYNAMIC_ENV_MEMORY 51 #define BT_BLE_DYNAMIC_ENV_MEMORY TRUE 52 #define BTC_DYNAMIC_MEMORY TRUE 53 #else 54 #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE 55 #define BTC_DYNAMIC_MEMORY FALSE 56 #endif 57 58 #if UC_BT_BLUEDROID_MEM_DEBUG 59 #define HEAP_MEMORY_DEBUG TRUE 60 #else 61 #define HEAP_MEMORY_DEBUG FALSE 62 #endif 63 64 #ifndef BT_BLE_DYNAMIC_ENV_MEMORY 65 #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE 66 #endif 67 68 #if UC_HEAP_ALLOCATION_FROM_SPIRAM_FIRST 69 #define HEAP_ALLOCATION_FROM_SPIRAM_FIRST TRUE 70 #else 71 #define HEAP_ALLOCATION_FROM_SPIRAM_FIRST FALSE 72 #endif 73 74 #if UC_BT_ABORT_WHEN_ALLOCATION_FAILS 75 #define HEAP_ALLOCATION_FAILS_ABORT TRUE 76 #else 77 #define HEAP_ALLOCATION_FAILS_ABORT FALSE 78 #endif 79 80 // HCI LOG 81 #if UC_BT_HCI_LOG_DEBUG_EN 82 #define BT_HCI_LOG_INCLUDED UC_BT_HCI_LOG_DEBUG_EN 83 #else 84 #define BT_HCI_LOG_INCLUDED FALSE 85 #endif 86 87 #if UC_BT_HCI_LOG_DATA_BUFFER_SIZE 88 #define HCI_LOG_DATA_BUFFER_SIZE UC_BT_HCI_LOG_DATA_BUFFER_SIZE 89 #else 90 #define HCI_BUFFER_SIZE (5) 91 #endif 92 93 #if UC_BT_HCI_ADV_BUFFER_SIZE 94 #define HCI_LOG_ADV_BUFFER_SIZE UC_BT_HCI_LOG_ADV_BUFFER_SIZE 95 #else 96 #define HCI_LOG_ADV_BUFFER_SIZE (5) 97 #endif 98 99 /* OS Configuration from User config (eg: sdkconfig) */ 100 #define TASK_PINNED_TO_CORE UC_TASK_PINNED_TO_CORE 101 #define BT_TASK_MAX_PRIORITIES configMAX_PRIORITIES 102 #define BT_BTC_TASK_STACK_SIZE UC_BTC_TASK_STACK_SIZE 103 104 /* Define trace levels */ 105 #define BT_TRACE_LEVEL_NONE UC_TRACE_LEVEL_NONE /* No trace messages to be generated */ 106 #define BT_TRACE_LEVEL_ERROR UC_TRACE_LEVEL_ERROR /* Error condition trace messages */ 107 #define BT_TRACE_LEVEL_WARNING UC_TRACE_LEVEL_WARNING /* Warning condition trace messages */ 108 #define BT_TRACE_LEVEL_API UC_TRACE_LEVEL_API /* API traces */ 109 #define BT_TRACE_LEVEL_EVENT UC_TRACE_LEVEL_EVENT /* Debug messages for events */ 110 #define BT_TRACE_LEVEL_DEBUG UC_TRACE_LEVEL_DEBUG /* Full debug messages */ 111 #define BT_TRACE_LEVEL_VERBOSE UC_TRACE_LEVEL_VERBOSE /* Verbose debug messages */ 112 113 #define MAX_TRACE_LEVEL UC_TRACE_LEVEL_VERBOSE 114 115 #ifndef LOG_LOCAL_LEVEL 116 #ifndef BOOTLOADER_BUILD 117 #define LOG_LOCAL_LEVEL UC_LOG_DEFAULT_LEVEL 118 #else 119 #define LOG_LOCAL_LEVEL UC_BOOTLOADER_LOG_LEVEL 120 #endif 121 #endif 122 123 // Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL 124 #if (LOG_LOCAL_LEVEL >= 4) 125 #define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1) 126 #else 127 #define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL 128 #endif 129 130 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 131 132 #define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL) 133 134 #define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 135 #define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 136 #define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 137 #define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 138 #define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 139 140 141 #if !UC_BT_STACK_NO_LOG 142 /* define traces for BTC */ 143 #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);} 144 #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);} 145 #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);} 146 #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);} 147 #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);} 148 #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);} 149 150 /* define traces for OSI */ 151 #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);} 152 #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);} 153 #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);} 154 #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);} 155 #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);} 156 #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);} 157 158 /* define traces for BLUFI */ 159 #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);} 160 #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);} 161 #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);} 162 #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);} 163 #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);} 164 #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);} 165 166 #else 167 168 /* define traces for BTC */ 169 #define BTC_TRACE_ERROR(fmt, args...) 170 #define BTC_TRACE_WARNING(fmt, args...) 171 #define BTC_TRACE_API(fmt, args...) 172 #define BTC_TRACE_EVENT(fmt, args...) 173 #define BTC_TRACE_DEBUG(fmt, args...) 174 #define BTC_TRACE_VERBOSE(fmt, args...) 175 176 /* define traces for OSI */ 177 #define OSI_TRACE_ERROR(fmt, args...) 178 #define OSI_TRACE_WARNING(fmt, args...) 179 #define OSI_TRACE_API(fmt, args...) 180 #define OSI_TRACE_EVENT(fmt, args...) 181 #define OSI_TRACE_DEBUG(fmt, args...) 182 #define OSI_TRACE_VERBOSE(fmt, args...) 183 184 /* define traces for BLUFI */ 185 #define BLUFI_TRACE_ERROR(fmt, args...) 186 #define BLUFI_TRACE_WARNING(fmt, args...) 187 #define BLUFI_TRACE_API(fmt, args...) 188 #define BLUFI_TRACE_EVENT(fmt, args...) 189 #define BLUFI_TRACE_DEBUG(fmt, args...) 190 #define BLUFI_TRACE_VERBOSE(fmt, args...) 191 192 #endif 193 194 /** Bluetooth Error Status */ 195 /** We need to build on this */ 196 197 /* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */ 198 typedef enum { 199 BT_STATUS_SUCCESS = 0, 200 BT_STATUS_FAIL, 201 BT_STATUS_NOT_READY, 202 BT_STATUS_NOMEM, 203 BT_STATUS_BUSY, 204 BT_STATUS_DONE, /* request already completed */ 205 BT_STATUS_UNSUPPORTED, 206 BT_STATUS_PARM_INVALID, 207 BT_STATUS_UNHANDLED, 208 BT_STATUS_AUTH_FAILURE, 209 BT_STATUS_RMT_DEV_DOWN, 210 BT_STATUS_AUTH_REJECTED, 211 BT_STATUS_INVALID_STATIC_RAND_ADDR, 212 BT_STATUS_PENDING, 213 BT_STATUS_UNACCEPT_CONN_INTERVAL, 214 BT_STATUS_PARAM_OUT_OF_RANGE, 215 BT_STATUS_TIMEOUT, 216 BT_STATUS_MEMORY_FULL, 217 BT_STATUS_EIR_TOO_LARGE, 218 } bt_status_t; 219 220 typedef uint8_t UINT8; 221 typedef uint16_t UINT16; 222 typedef uint32_t UINT32; 223 typedef uint64_t UINT64; 224 typedef bool BOOLEAN; 225 /* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */ 226 #define MAX_UUID_SIZE 16 227 228 typedef struct { 229 #define LEN_UUID_16 2 230 #define LEN_UUID_32 4 231 #define LEN_UUID_128 16 232 233 UINT16 len; 234 235 union { 236 UINT16 uuid16; 237 UINT32 uuid32; 238 UINT8 uuid128[MAX_UUID_SIZE]; 239 } uu; 240 241 } tBT_UUID; 242 243 /* Common Bluetooth field definitions */ 244 #define BD_ADDR_LEN 6 /* Device address length */ 245 typedef UINT8 BD_ADDR[BD_ADDR_LEN]; /* Device address */ 246 247 #endif /* _BT_COMMON_H_ */ 248