1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #ifndef BLE_COMMON_H 19 #define BLE_COMMON_H 20 21 #include "tl_common.h" 22 23 24 25 26 27 typedef enum { 28 BLE_SUCCESS = 0, 29 30 //// HCI Status, See the Core_v5.0(Vol 2/Part D/1.3 "list of Error Codes") for more information) 31 HCI_ERR_UNKNOWN_HCI_CMD = 0x01, 32 HCI_ERR_UNKNOWN_CONN_ID = 0x02, 33 HCI_ERR_HW_FAILURE = 0x03, 34 HCI_ERR_PAGE_TIMEOUT = 0x04, 35 HCI_ERR_AUTH_FAILURE = 0x05, 36 HCI_ERR_PIN_KEY_MISSING = 0x06, 37 HCI_ERR_MEM_CAP_EXCEEDED = 0x07, 38 HCI_ERR_CONN_TIMEOUT = 0x08, 39 HCI_ERR_CONN_LIMIT_EXCEEDED = 0x09, 40 HCI_ERR_SYNCH_CONN_LIMIT_EXCEEDED = 0x0A, 41 HCI_ERR_CONN_ALREADY_EXISTS = 0x0B, 42 HCI_ERR_CMD_DISALLOWED = 0x0C, 43 HCI_ERR_CONN_REJ_LIMITED_RESOURCES = 0x0D, 44 HCI_ERR_CONN_REJECTED_SECURITY_REASONS = 0x0E, 45 HCI_ERR_CONN_REJECTED_UNACCEPTABLE_BDADDR = 0x0F, 46 HCI_ERR_CONN_ACCEPT_TIMEOUT_EXCEEDED = 0x10, 47 HCI_ERR_UNSUPPORTED_FEATURE_PARAM_VALUE = 0x11, 48 HCI_ERR_INVALID_HCI_CMD_PARAMS = 0x12, 49 HCI_ERR_REMOTE_USER_TERM_CONN = 0x13, 50 HCI_ERR_REMOTE_DEVICE_TERM_CONN_LOW_RESOURCES = 0x14, 51 HCI_ERR_REMOTE_DEVICE_TERM_CONN_POWER_OFF = 0x15, 52 HCI_ERR_CONN_TERM_BY_LOCAL_HOST = 0x16, 53 HCI_ERR_REPEATED_ATTEMPTS = 0x17, 54 HCI_ERR_PAIRING_NOT_ALLOWED = 0x18, 55 HCI_ERR_UNKNOWN_LMP_PDU = 0x19, 56 HCI_ERR_UNSUPPORTED_REMOTE_FEATURE = 0x1A, 57 HCI_ERR_SCO_OFFSET_REJ = 0x1B, 58 HCI_ERR_SCO_INTERVAL_REJ = 0x1C, 59 HCI_ERR_SCO_AIR_MODE_REJ = 0x1D, 60 HCI_ERR_INVALID_LMP_PARAMS = 0x1E, 61 HCI_ERR_UNSPECIFIED_ERROR = 0x1F, 62 HCI_ERR_UNSUPPORTED_LMP_PARAM_VAL = 0x20, 63 HCI_ERR_ROLE_CHANGE_NOT_ALLOWED = 0x21, 64 HCI_ERR_LMP_LL_RESP_TIMEOUT = 0x22, 65 HCI_ERR_LMP_ERR_TRANSACTION_COLLISION = 0x23, 66 HCI_ERR_LMP_PDU_NOT_ALLOWED = 0x24, 67 HCI_ERR_ENCRYPT_MODE_NOT_ACCEPTABLE = 0x25, 68 HCI_ERR_LINK_KEY_CAN_NOT_BE_CHANGED = 0x26, 69 HCI_ERR_REQ_QOS_NOT_SUPPORTED = 0x27, 70 HCI_ERR_INSTANT_PASSED = 0x28, 71 HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED = 0x29, 72 HCI_ERR_DIFFERENT_TRANSACTION_COLLISION = 0x2A, 73 HCI_ERR_RESERVED1 = 0x2B, 74 HCI_ERR_QOS_UNACCEPTABLE_PARAM = 0x2C, 75 HCI_ERR_QOS_REJ = 0x2D, 76 HCI_ERR_CHAN_ASSESSMENT_NOT_SUPPORTED = 0x2E, 77 HCI_ERR_INSUFFICIENT_SECURITY = 0x2F, 78 HCI_ERR_PARAM_OUT_OF_MANDATORY_RANGE = 0x30, 79 HCI_ERR_RESERVED2 = 0x31, 80 HCI_ERR_ROLE_SWITCH_PENDING = 0x32, 81 HCI_ERR_RESERVED3 = 0x33, 82 HCI_ERR_RESERVED_SLOT_VIOLATION = 0x34, 83 HCI_ERR_ROLE_SWITCH_FAILED = 0x35, 84 HCI_ERR_EXTENDED_INQUIRY_RESP_TOO_LARGE = 0x36, 85 HCI_ERR_SIMPLE_PAIRING_NOT_SUPPORTED_BY_HOST = 0x37, 86 HCI_ERR_HOST_BUSY_PAIRING = 0x38, 87 HCI_ERR_CONN_REJ_NO_SUITABLE_CHAN_FOUND = 0x39, 88 HCI_ERR_CONTROLLER_BUSY = 0x3A, 89 HCI_ERR_UNACCEPTABLE_CONN_INTERVAL = 0x3B, 90 HCI_ERR_ADVERTISING_TIMEOUT = 0x3C, 91 HCI_ERR_CONN_TERM_MIC_FAILURE = 0x3D, 92 HCI_ERR_CONN_FAILED_TO_ESTABLISH = 0x3E, 93 HCI_ERR_MAC_CONN_FAILED = 0x3F, 94 HCI_ERR_COARSE_CLOCK_ADJUSTMENT_REJECT = 0x40, 95 HCI_ERR_TYPE0_SUBMAP_NOT_DEFINED = 0x41, 96 HCI_ERR_UNKNOWN_ADV_IDENTIFIER = 0x42, 97 HCI_ERR_LIMIT_REACHED = 0x43, 98 HCI_ERR_OP_CANCELLED_BY_HOST = 0x44, 99 HCI_ERR_PACKET_TOO_LONG = 0x45, 100 //DBG used only for CIS 101 HCI_ERR_CONN_TERM_CIS_MIC_FAILURE = 0xD3, 102 103 104 105 106 ///////////////////////// TELINK define status ///////////////////////////// 107 108 //LL status 109 LL_ERR_CONNECTION_NOT_ESTABLISH = 0x80, 110 LL_ERR_TX_FIFO_NOT_ENOUGH, 111 LL_ERR_ENCRYPTION_BUSY, 112 LL_ERR_CURRENT_STATE_NOT_SUPPORTED_THIS_CMD, 113 LL_ERR_INVALID_PARAMETER, 114 LL_ERR_UNKNOWN_OPCODE, 115 116 LL_ERR_CIS_SYNC_FAIL, 117 LL_ERR_CIS_DISCONNECT, 118 119 120 //L2CAP status 121 L2CAP_ERR_INVALID_PARAMETER = 0x90, 122 L2CAP_ERR_INVALID_HANDLE, 123 L2CAP_ERR_INSUFFICIENT_RESOURCES, 124 L2CAP_ERR_PSM_NOT_REGISTER, 125 L2CAP_ERR_CONTROL_NOT_READY, 126 L2CAP_ERR_PSM_HAVE_ESTABLISH, 127 128 //SMP status 129 SMP_ERR_INVALID_PARAMETER = 0xA0, 130 SMP_ERR_PAIRING_BUSY, 131 132 //GATT status 133 GATT_ERR_INVALID_PARAMETER = 0xB0, 134 GATT_ERR_PREVIOUS_INDICATE_DATA_HAS_NOT_CONFIRMED, 135 GATT_ERR_SERVICE_DISCOVERY_TIEMOUT, 136 GATT_ERR_NOTIFY_INDICATION_NOT_PERMITTED, 137 GATT_ERR_DATA_PENDING_DUE_TO_SERVICE_DISCOVERY_BUSY, 138 GATT_ERR_DATA_LENGTH_EXCEED_MTU_SIZE, 139 140 //GAP status 141 GAP_ERR_INVALID_PARAMETER = 0xC0, 142 //IAL 143 IAL_ERR_SDU_LEN_EXCEED_SDU_MAX, 144 IAL_ERR_LOSS_SDU_INTRVEL, 145 IAL_ERR_ISO_TX_FIFO_NOT_ENOUGH, 146 IAL_ERR_SDU_BUFF_INVALID, 147 IAL_ERR_EVENT_PASSED, 148 149 //Service status 150 SERVICE_ERR_INVALID_PARAMETER = 0xD0, 151 152 153 //Application buffer check error code 154 LL_ACL_RX_BUF_NO_INIT = 0xE0, 155 LL_ACL_RX_BUF_PARAM_INVALID, 156 LL_ACL_RX_BUF_SIZE_NOT_MEET_MAX_RX_OCT, 157 LL_ACL_TX_BUF_NO_INIT, 158 LL_ACL_TX_BUF_PARAM_INVALID, 159 LL_ACL_TX_BUF_SIZE_MUL_NUM_EXCEED_4K, 160 LL_ACL_TX_BUF_SIZE_NOT_MEET_MAX_TX_OCT, 161 162 LL_CIS_RX_BUF_NO_INIT, 163 LL_CIS_RX_BUF_PARAM_INVALID, 164 LL_CIS_TX_BUF_NO_INIT, 165 LL_CIS_TX_BUF_PARAM_INVALID, 166 LL_CIS_RX_EVT_BUF_NO_INIT, 167 LL_CIS_RX_EVT_BUF_PARAM_INVALID, 168 169 LL_BIS_TX_BUF_NO_INIT, 170 LL_BIS_TX_BUF_PARAM_INVALID, 171 LL_BIS_RX_BUF_NO_INIT, 172 LL_BIS_RX_BUF_PARAM_INVALID, 173 LL_BIS_RX_PDU_INVALID, 174 LL_BIS_RX_PDU_EMPTY, 175 LL_BIS_RX_EVT_BUF_NO_INIT, 176 LL_BIS_RX_EVT_BUF_PARAM_INVALID, 177 178 HCI_ACL_DATA_BUF_PARAM_INVALID, 179 HCI_ACL_DATA_BUF_SIZE_NOT_MEET_MAX_TX_OCT, 180 181 } ble_sts_t; 182 183 184 185 186 187 188 189 190 191 192 193 /////////////////////////////// BLE MAC ADDRESS ////////////////////////////////////////////// 194 #define BLE_ADDR_PUBLIC 0 195 #define BLE_ADDR_RANDOM 1 196 #define BLE_ADDR_INVALID 0xff 197 #define BLE_ADDR_LEN 6 198 199 200 //Definition for BLE Common Address Type 201 /* 202 * 203 * |--public ..................................................... BLE_DEVICE_ADDRESS_PUBLIC 204 * | 205 * Address Type --| |-- random static ................................. BLE_DEVICE_ADDRESS_RANDOM_STATIC 206 * | | 207 * |--random --| 208 * | |-- non_resolvable private ... BLE_DEVICE_ADDRESS_NON_RESOLVABLE_PRIVATE 209 * |-- random private --| 210 * |-- resolvable private ....... BLE_DEVICE_ADDRESS_RESOLVABLE_PRIVATE 211 * 212 */ 213 214 #define BLE_DEVICE_ADDRESS_PUBLIC 1 215 #define BLE_DEVICE_ADDRESS_RANDOM_STATIC 2 216 #define BLE_DEVICE_ADDRESS_NON_RESOLVABLE_PRIVATE 3 217 #define BLE_DEVICE_ADDRESS_RESOLVABLE_PRIVATE 4 218 219 220 221 #define IS_PUBLIC_ADDR(Type, Addr) ( (Type)==BLE_ADDR_PUBLIC) ) 222 #define IS_RANDOM_STATIC_ADDR(Type, Addr) ( (Type)==BLE_ADDR_RANDOM && (Addr[5] & 0xC0) == 0xC0 ) 223 #define IS_NON_RESOLVABLE_PRIVATE_ADDR(Type, Addr) ( (Type)==BLE_ADDR_RANDOM && (Addr[5] & 0xC0) == 0x00 ) 224 #define IS_RESOLVABLE_PRIVATE_ADDR(Type, Addr) ( (Type)==BLE_ADDR_RANDOM && (Addr[5] & 0xC0) == 0x40 ) 225 226 227 #define MAC_MATCH8(md,ms) (md[0]==ms[0] && md[1]==ms[1] && md[2]==ms[2] && md[3]==ms[3] && md[4]==ms[4] && md[5]==ms[5]) 228 #define MAC_MATCH16(md,ms) (md[0]==ms[0] && md[1]==ms[1] && md[2]==ms[2]) 229 #define MAC_MATCH32(md,ms) (md[0]==ms[0] && md[1]==ms[1]) 230 ///////////////////////////////////////////////////////////////////////////// 231 232 233 234 235 /******************************************** ATT ***************************************************************/ 236 /** 237 * @brief Definition for Attribute protocol PDUs 238 */ 239 typedef enum{ 240 ATT_OP_ERROR_RSP = 0x01, 241 ATT_OP_EXCHANGE_MTU_REQ = 0x02, 242 ATT_OP_EXCHANGE_MTU_RSP = 0x03, 243 ATT_OP_FIND_INFORMATION_REQ = 0x04, ATT_OP_FIND_INFO_REQ = 0x04, 244 ATT_OP_FIND_INFORMATION_RSP = 0x05, ATT_OP_FIND_INFO_RSP = 0x05, 245 ATT_OP_FIND_BY_TYPE_VALUE_REQ = 0x06, 246 ATT_OP_FIND_BY_TYPE_VALUE_RSP = 0x07, 247 ATT_OP_READ_BY_TYPE_REQ = 0x08, 248 ATT_OP_READ_BY_TYPE_RSP = 0x09, 249 ATT_OP_READ_REQ = 0x0A, 250 ATT_OP_READ_RSP = 0x0B, 251 ATT_OP_READ_BLOB_REQ = 0x0C, 252 ATT_OP_READ_BLOB_RSP = 0x0D, 253 ATT_OP_READ_MULTIPLE_REQ = 0x0E, ATT_OP_READ_MULTI_REQ = 0x0E, 254 ATT_OP_READ_MULTIPLE_RSP = 0x0F, 255 ATT_OP_READ_BY_GROUP_TYPE_REQ = 0x10, 256 ATT_OP_READ_BY_GROUP_TYPE_RSP = 0x11, 257 ATT_OP_WRITE_REQ = 0x12, 258 ATT_OP_WRITE_RSP = 0x13, 259 ATT_OP_PREPARE_WRITE_REQ = 0x16, 260 ATT_OP_PREPARE_WRITE_RSP = 0x17, 261 ATT_OP_EXECUTE_WRITE_REQ = 0x18, 262 ATT_OP_EXECUTE_WRITE_RSP = 0x19, 263 264 ATT_OP_HANDLE_VALUE_NTF = 0x1B, ATT_OP_HANDLE_VALUE_NOTI = 0x1B, 265 ATT_OP_HANDLE_VALUE_IND = 0x1D, 266 ATT_OP_HANDLE_VALUE_CFM = 0x1E, 267 268 ATT_OP_READ_MULTIPLE_VARIABLE_REQ = 0x20, //core_5.2 269 ATT_OP_READ_MULTIPLE_VARIABLE_RSP = 0x21, //core_5.2 270 ATT_OP_MULTIPLE_HANDLE_VALUE_NTF = 0x23, //core_5.2 271 272 ATT_OP_WRITE_CMD = 0x52, 273 ATT_OP_SIGNED_WRITE_CMD = 0xD2, 274 }att_pdu_type; 275 276 277 278 /******************************************** GAP ***************************************************************/ 279 280 // https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/ 281 // EIR Data Type, Advertising Data Type (AD Type) and OOB Data Type Definitions 282 283 typedef enum { 284 DT_FLAGS = 0x01, // Flag 285 DT_INCOMPLT_LIST_16BIT_SERVICE_UUID = 0x02, // Incomplete List of 16-bit Service Class UUIDs 286 DT_COMPLETE_LIST_16BIT_SERVICE_UUID = 0x03, // Complete List of 16-bit Service Class UUIDs 287 DT_INCOMPLT_LIST_32BIT_SERVICE_UUID = 0x04, // Incomplete List of 32-bit Service Class UUIDs 288 DT_COMPLETE_LIST_32BIT_SERVICE_UUID = 0x05, // Complete List of 32-bit Service Class UUIDs 289 DT_INCOMPLT_LIST_128BIT_SERVICE_UUID = 0x06, // Incomplete List of 128-bit Service Class UUIDs 290 DT_COMPLETE_LIST_128BIT_SERVICE_UUID = 0x07, // Complete List of 128-bit Service Class UUIDs 291 DT_SHORTENED_LOCAL_NAME = 0x08, // Shortened Local Name 292 DT_COMPLETE_LOCAL_NAME = 0x09, // Complete Local Name 293 DT_TX_POWER_LEVEL = 0x0A, // Tx Power Level 294 295 DT_CLASS_OF_DEVICE = 0x0D, // Class of Device 296 297 DT_APPEARANCE = 0x19, // Appearance 298 299 DT_CHM_UPT_IND = 0x28, // Channel Map Update Indication 300 DT_BIGINFO = 0x2C, // BIGInfo 301 DT_BROADCAST_CODE = 0x2D, // Broadcast_Code 302 DT_3D_INFORMATION_DATA = 0x3D, // 3D Information Data 303 304 DATA_TYPE_MANUFACTURER_SPECIFIC_DATA = 0xFF, // Manufacturer Specific Data 305 }data_type_t; 306 307 308 309 310 311 312 313 /** 314 * @brief HCI ACL DATA buffer length = LE_ACL_Data_Packet_Length + 4, pkt_len is integer multiple of 4, so result is 4 Byte align 315 * 4 = 2(connHandle) + 1(PBFlag) + 1(length) 316 */ 317 #define CALCULATE_HCI_ACL_DATA_FIFO_SIZE(pkt_len) ((pkt_len) + 4) 318 319 320 321 /** 322 * @brief 6 = header(2)+l2cap_len(2)+CID(2) 323 */ 324 #define CAL_MTU_BUFF_SIZE(n) (((n + 6) + 3)/4 * 4) 325 326 /** 327 * @brief 12 = type(1) + len(1) + l2cap_len(2) + cid(2) + sud_len(2) + mic(4) 328 */ 329 #define L2CAP_ALLIGN4_KFRAM_DMA_BUFF(n) (((n + 12) + 3) / 4 * 4) 330 331 // 7 = rf_packet_ll_data_t 332 #define CIS_PDU_ALLIGN4_TXBUFF(n) DATA_LENGTH_ALLIGN4((CAL_LL_ISO_TX_FIFO_SIZE(n) + DATA_LENGTH_ALLIGN4(CIS_TX_PDU_BUFFER_LENGTH -7))) 333 334 #define CIS_PDU_ALLIGN4_RXBUFF(n) DATA_LENGTH_ALLIGN4(CAL_LL_ISO_RX_FIFO_SIZE(n) ) 335 336 #define BIS_PDU_ALLIGN4_TXBUFF(n) DATA_LENGTH_ALLIGN4((CAL_LL_ISO_TX_FIFO_SIZE(n) + DATA_LENGTH_ALLIGN4(BIS_TX_PDU_BUFFER_LENGTH - 7))) 337 338 #define BIS_PDU_ALLIGN4_RXBUFF(n) DATA_LENGTH_ALLIGN4(BIS_LL_RX_PDU_FIFO_SIZE(n)) 339 340 #define IAL_SDU_ALLIGN4_BUFF(n) (((n + 16) + 3) / 4 * 4) 341 342 #define HCI_ISO_ALLIGN4_BUFF(n) (((n + 4) + 3) / 4 * 4) //DMA len 4 343 344 345 346 347 348 349 350 351 352 353 #endif 354