1 /* 2 * SPDX-FileCopyrightText: 2015-2021 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 #if UC_BT_BLE_DYNAMIC_ENV_MEMORY 50 #define BT_BLE_DYNAMIC_ENV_MEMORY TRUE 51 #define BTC_DYNAMIC_MEMORY TRUE 52 #else 53 #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE 54 #define BTC_DYNAMIC_MEMORY FALSE 55 #endif 56 57 #if UC_BT_BLUEDROID_MEM_DEBUG 58 #define HEAP_MEMORY_DEBUG TRUE 59 #else 60 #define HEAP_MEMORY_DEBUG FALSE 61 #endif 62 63 #ifndef BT_BLE_DYNAMIC_ENV_MEMORY 64 #define BT_BLE_DYNAMIC_ENV_MEMORY FALSE 65 #endif 66 67 /* OS Configuration from User config (eg: sdkconfig) */ 68 #define TASK_PINNED_TO_CORE UC_TASK_PINNED_TO_CORE 69 #define BT_TASK_MAX_PRIORITIES configMAX_PRIORITIES 70 #define BT_BTC_TASK_STACK_SIZE UC_BTC_TASK_STACK_SIZE 71 72 /* Define trace levels */ 73 #define BT_TRACE_LEVEL_NONE UC_TRACE_LEVEL_NONE /* No trace messages to be generated */ 74 #define BT_TRACE_LEVEL_ERROR UC_TRACE_LEVEL_ERROR /* Error condition trace messages */ 75 #define BT_TRACE_LEVEL_WARNING UC_TRACE_LEVEL_WARNING /* Warning condition trace messages */ 76 #define BT_TRACE_LEVEL_API UC_TRACE_LEVEL_API /* API traces */ 77 #define BT_TRACE_LEVEL_EVENT UC_TRACE_LEVEL_EVENT /* Debug messages for events */ 78 #define BT_TRACE_LEVEL_DEBUG UC_TRACE_LEVEL_DEBUG /* Full debug messages */ 79 #define BT_TRACE_LEVEL_VERBOSE UC_TRACE_LEVEL_VERBOSE /* Verbose debug messages */ 80 81 #define MAX_TRACE_LEVEL UC_TRACE_LEVEL_VERBOSE 82 83 #ifndef LOG_LOCAL_LEVEL 84 #ifndef BOOTLOADER_BUILD 85 #define LOG_LOCAL_LEVEL UC_LOG_DEFAULT_LEVEL 86 #else 87 #define LOG_LOCAL_LEVEL UC_BOOTLOADER_LOG_LEVEL 88 #endif 89 #endif 90 91 // Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL 92 #if (LOG_LOCAL_LEVEL >= 4) 93 #define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1) 94 #else 95 #define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL 96 #endif 97 98 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 99 100 #define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL) 101 102 #define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 103 #define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 104 #define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 105 #define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 106 #define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } 107 108 109 #if !UC_BT_STACK_NO_LOG 110 /* define traces for BTC */ 111 #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);} 112 #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);} 113 #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);} 114 #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);} 115 #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);} 116 #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);} 117 118 /* define traces for OSI */ 119 #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);} 120 #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);} 121 #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);} 122 #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);} 123 #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);} 124 #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);} 125 126 /* define traces for BLUFI */ 127 #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);} 128 #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);} 129 #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);} 130 #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);} 131 #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);} 132 #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);} 133 134 #else 135 136 /* define traces for BTC */ 137 #define BTC_TRACE_ERROR(fmt, args...) 138 #define BTC_TRACE_WARNING(fmt, args...) 139 #define BTC_TRACE_API(fmt, args...) 140 #define BTC_TRACE_EVENT(fmt, args...) 141 #define BTC_TRACE_DEBUG(fmt, args...) 142 #define BTC_TRACE_VERBOSE(fmt, args...) 143 144 /* define traces for OSI */ 145 #define OSI_TRACE_ERROR(fmt, args...) 146 #define OSI_TRACE_WARNING(fmt, args...) 147 #define OSI_TRACE_API(fmt, args...) 148 #define OSI_TRACE_EVENT(fmt, args...) 149 #define OSI_TRACE_DEBUG(fmt, args...) 150 #define OSI_TRACE_VERBOSE(fmt, args...) 151 152 /* define traces for BLUFI */ 153 #define BLUFI_TRACE_ERROR(fmt, args...) 154 #define BLUFI_TRACE_WARNING(fmt, args...) 155 #define BLUFI_TRACE_API(fmt, args...) 156 #define BLUFI_TRACE_EVENT(fmt, args...) 157 #define BLUFI_TRACE_DEBUG(fmt, args...) 158 #define BLUFI_TRACE_VERBOSE(fmt, args...) 159 160 #endif 161 162 /** Bluetooth Error Status */ 163 /** We need to build on this */ 164 165 /* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */ 166 typedef enum { 167 BT_STATUS_SUCCESS = 0, 168 BT_STATUS_FAIL, 169 BT_STATUS_NOT_READY, 170 BT_STATUS_NOMEM, 171 BT_STATUS_BUSY, 172 BT_STATUS_DONE, /* request already completed */ 173 BT_STATUS_UNSUPPORTED, 174 BT_STATUS_PARM_INVALID, 175 BT_STATUS_UNHANDLED, 176 BT_STATUS_AUTH_FAILURE, 177 BT_STATUS_RMT_DEV_DOWN, 178 BT_STATUS_AUTH_REJECTED, 179 BT_STATUS_INVALID_STATIC_RAND_ADDR, 180 BT_STATUS_PENDING, 181 BT_STATUS_UNACCEPT_CONN_INTERVAL, 182 BT_STATUS_PARAM_OUT_OF_RANGE, 183 BT_STATUS_TIMEOUT, 184 BT_STATUS_MEMORY_FULL, 185 BT_STATUS_EIR_TOO_LARGE, 186 } bt_status_t; 187 188 typedef uint8_t UINT8; 189 typedef uint16_t UINT16; 190 typedef uint32_t UINT32; 191 typedef uint64_t UINT64; 192 typedef bool BOOLEAN; 193 /* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */ 194 #define MAX_UUID_SIZE 16 195 196 typedef struct { 197 #define LEN_UUID_16 2 198 #define LEN_UUID_32 4 199 #define LEN_UUID_128 16 200 201 UINT16 len; 202 203 union { 204 UINT16 uuid16; 205 UINT32 uuid32; 206 UINT8 uuid128[MAX_UUID_SIZE]; 207 } uu; 208 209 } tBT_UUID; 210 211 /* Common Bluetooth field definitions */ 212 #define BD_ADDR_LEN 6 /* Device address length */ 213 typedef UINT8 BD_ADDR[BD_ADDR_LEN]; /* Device address */ 214 215 #endif /* _BT_COMMON_H_ */ 216