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