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 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef _BT_COMMON_H_ 16 #define _BT_COMMON_H_ 17 18 #include "bt_user_config.h" 19 #include "esp_log.h" 20 21 #ifndef FALSE 22 #define 0 false 23 #endif 24 25 #ifndef TRUE 26 #define TRUE true 27 #endif 28 29 30 #if (UC_BT_BLUFI_ENABLE) 31 #define BLUFI_INCLUDED 1 32 #else 33 #define BLUFI_INCLUDED 0 34 #endif 35 36 #ifdef CONFIG_BT_BLUEDROID_ENABLED 37 #include "esp_bt_defs.h" 38 #include "esp_bt_main.h" 39 #include "esp_gatt_defs.h" 40 #define ESP_BLE_HOST_STATUS_ENABLED ESP_BLUEDROID_STATUS_ENABLED 41 #define ESP_BLE_HOST_STATUS_CHECK(status) ESP_BLUEDROID_STATUS_CHECK(status) 42 #else 43 #define ESP_BLE_HOST_STATUS_ENABLED 0 44 #define ESP_BLE_HOST_STATUS_CHECK(status) do {} while (0) 45 #endif 46 47 #ifndef BT_QUEUE_CONGEST_SIZE 48 #define BT_QUEUE_CONGEST_SIZE 40 49 #endif 50 51 #define BTC_INITIAL_TRACE_LEVEL UC_BT_LOG_BTC_TRACE_LEVEL 52 #define OSI_INITIAL_TRACE_LEVEL UC_BT_LOG_OSI_TRACE_LEVEL 53 #define BLUFI_INITIAL_TRACE_LEVEL UC_BT_LOG_BLUFI_TRACE_LEVEL 54 55 #if UC_BT_BLE_DYNAMIC_ENV_MEMORY 56 #define BT_BLE_DYNAMIC_ENV_MEMORY 1 57 #define BTC_DYNAMIC_MEMORY 1 58 #else 59 #define BT_BLE_DYNAMIC_ENV_MEMORY 0 60 #define BTC_DYNAMIC_MEMORY 0 61 #endif 62 63 #ifndef BT_BLE_DYNAMIC_ENV_MEMORY 64 #define BT_BLE_DYNAMIC_ENV_MEMORY 0 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 #ifndef assert 109 #define assert(x) do { if (!(x)) BT_PRINT_E("BT", "bt host error %s %u\n", __FILE__, __LINE__); } while (0) 110 #endif 111 112 113 #if !UC_BT_STACK_NO_LOG 114 /* define traces for BTC */ 115 #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);} 116 #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);} 117 #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);} 118 #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);} 119 #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);} 120 #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);} 121 122 /* define traces for OSI */ 123 #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);} 124 #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);} 125 #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);} 126 #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);} 127 #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);} 128 #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);} 129 130 /* define traces for BLUFI */ 131 #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);} 132 #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);} 133 #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);} 134 #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);} 135 #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);} 136 #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);} 137 138 #else 139 140 /* define traces for BTC */ 141 #define BTC_TRACE_ERROR(fmt, args...) 142 #define BTC_TRACE_WARNING(fmt, args...) 143 #define BTC_TRACE_API(fmt, args...) 144 #define BTC_TRACE_EVENT(fmt, args...) 145 #define BTC_TRACE_DEBUG(fmt, args...) 146 #define BTC_TRACE_VERBOSE(fmt, args...) 147 148 /* define traces for OSI */ 149 #define OSI_TRACE_ERROR(fmt, args...) 150 #define OSI_TRACE_WARNING(fmt, args...) 151 #define OSI_TRACE_API(fmt, args...) 152 #define OSI_TRACE_EVENT(fmt, args...) 153 #define OSI_TRACE_DEBUG(fmt, args...) 154 #define OSI_TRACE_VERBOSE(fmt, args...) 155 156 /* define traces for BLUFI */ 157 #define BLUFI_TRACE_ERROR(fmt, args...) 158 #define BLUFI_TRACE_WARNING(fmt, args...) 159 #define BLUFI_TRACE_API(fmt, args...) 160 #define BLUFI_TRACE_EVENT(fmt, args...) 161 #define BLUFI_TRACE_DEBUG(fmt, args...) 162 #define BLUFI_TRACE_VERBOSE(fmt, args...) 163 164 #endif 165 166 /** Bluetooth Error Status */ 167 /** We need to build on this */ 168 169 /* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */ 170 typedef enum { 171 BT_STATUS_SUCCESS = 0, 172 BT_STATUS_FAIL, 173 BT_STATUS_NOT_READY, 174 BT_STATUS_NOMEM, 175 BT_STATUS_BUSY, 176 BT_STATUS_DONE, /* request already completed */ 177 BT_STATUS_UNSUPPORTED, 178 BT_STATUS_PARM_INVALID, 179 BT_STATUS_UNHANDLED, 180 BT_STATUS_AUTH_FAILURE, 181 BT_STATUS_RMT_DEV_DOWN, 182 BT_STATUS_AUTH_REJECTED, 183 BT_STATUS_INVALID_STATIC_RAND_ADDR, 184 BT_STATUS_PENDING, 185 BT_STATUS_UNACCEPT_CONN_INTERVAL, 186 BT_STATUS_PARAM_OUT_OF_RANGE, 187 BT_STATUS_TIMEOUT, 188 BT_STATUS_MEMORY_FULL, 189 BT_STATUS_EIR_TOO_LARGE, 190 } bt_status_t; 191 192 typedef uint8_t UINT8; 193 typedef uint16_t UINT16; 194 typedef uint32_t UINT32; 195 typedef uint64_t UINT64; 196 typedef bool BOOLEAN; 197 /* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */ 198 #define MAX_UUID_SIZE 16 199 200 typedef struct { 201 #define LEN_UUID_16 2 202 #define LEN_UUID_32 4 203 #define LEN_UUID_128 16 204 205 UINT16 len; 206 207 union { 208 UINT16 uuid16; 209 UINT32 uuid32; 210 UINT8 uuid128[MAX_UUID_SIZE]; 211 } uu; 212 213 } tBT_UUID; 214 215 /* Common Bluetooth field definitions */ 216 #define BD_ADDR_LEN 6 /* Device address length */ 217 typedef UINT8 BD_ADDR[BD_ADDR_LEN]; /* Device address */ 218 219 #endif /* _BT_COMMON_H_ */ 220