1 /** 2 **************************************************************************************** 3 * 4 * @file ble.h 5 * 6 * @brief include all ble sdk header files 7 * 8 **************************************************************************************** 9 * @attention 10 #####Copyright (c) 2019 GOODIX 11 All rights reserved. 12 13 Redistribution and use in source and binary forms, with or without 14 modification, are permitted provided that the following conditions are met: 15 * Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 * Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in the 19 documentation and/or other materials provided with the distribution. 20 * Neither the name of GOODIX nor the names of its contributors may be used 21 to endorse or promote products derived from this software without 22 specific prior written permission. 23 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35 ***************************************************************************************** 36 */ 37 38 /** 39 * @addtogroup BLE 40 * @{ 41 */ 42 43 /** 44 @addtogroup BLE_COMMEN BLE Common 45 @{ 46 @brief BLE Common interface. 47 */ 48 49 #ifndef __BLE_H__ 50 #define __BLE_H__ 51 52 #include "ble_att.h" 53 #include "ble_error.h" 54 #include "ble_gapc.h" 55 #include "ble_gapm.h" 56 #include "ble_gatt.h" 57 #include "ble_gattc.h" 58 #include "ble_gatts.h" 59 #include "ble_l2cap.h" 60 #include "ble_prf.h" 61 #include "ble_sec.h" 62 63 /** @addtogroup BLE_COMMEN_ENUM Enumerations 64 * @{ 65 */ 66 /** 67 * @brief RF TX mode. 68 */ 69 typedef enum { 70 BLE_RF_TX_MODE_INVALID = 0, 71 BLE_RF_TX_MODE_LP_MODE = 1, 72 BLE_RF_TX_MODE_ULP_MODE = 2, 73 } ble_rf_tx_mode_t; 74 75 /** 76 * @brief The resistance value (ohm) of the RF match circuit. 77 */ 78 typedef enum { 79 BLE_RF_MATCH_CIRCUIT_25OHM = 25, 80 BLE_RF_MATCH_CIRCUIT_100OHM = 100, 81 } ble_rf_match_circuit_t; 82 83 /** @} */ 84 85 /** @addtogroup BLE_COMMEN_STRUCTURES Structures 86 * @{ 87 */ 88 /** @brief The app callbacks for GAP, GATT, SM and L2CAP. */ 89 typedef struct { 90 app_ble_init_cmp_cb_t 91 app_ble_init_cmp_callback; /**< Callback function for BLE initialization completed */ 92 gap_cb_fun_t *app_gap_callbacks; /**< Callback function for GAP */ 93 const gatt_common_cb_fun_t *app_gatt_common_callback; /**< Callback function for GATT common */ 94 const gattc_cb_fun_t *app_gattc_callback; /**< Callback function for GATT Client */ 95 sec_cb_fun_t *app_sec_callback; /**< Callback function for SM*/ 96 } app_callback_t; 97 98 /** @brief The table contains the pointers to four arrays which are used 99 * as heap memory by BLE stack in ROM. The size of four arrays depends on 100 * the number of connections and the number of attributes of profiles. */ 101 typedef struct { 102 uint32_t *env_ret; /**< Pointer to the array for environment heap */ 103 uint32_t *db_ret; /**< Pointer to the array for ATT DB heap */ 104 uint32_t *msg_ret; /**< Pointer to the array for message heap */ 105 uint32_t *non_ret; /**< Pointer to the array for non-retention heap */ 106 uint16_t env_ret_size; /**< The size of the array for environment heap */ 107 uint16_t db_ret_size; /**< The size of the array for ATT DB heap */ 108 uint16_t msg_ret_size; /**< The size of the array for message heap */ 109 uint16_t non_ret_size; /**< The size of the array for non-retention heap */ 110 uint8_t *prf_buf; /**< Pointer to the array for profile heap */ 111 uint32_t buf_size; /**< The size of the array for profile heap */ 112 uint8_t *bm_buf; /**< Pointer to the array for bond manager heap */ 113 uint32_t bm_size; /**< The size of the array for bond manager heap */ 114 uint8_t *conn_buf; /**< Pointer to the array for connection heap */ 115 uint32_t conn_size; /**< The size of the array for connection heap */ 116 } stack_heaps_table_t; 117 118 /** @brief The function pointers for HCI UART. */ 119 typedef struct { 120 void (*init)(void); /**< Initialize UART. */ 121 void (*flow_on)(void); /**< Flow control on. */ 122 bool (*flow_off)(void); /**< Flow control off. */ 123 void (*finish_transfers)(void); /**< Finish the current transferring. */ 124 /**< Read data. */ 125 void (*read)(uint8_t *bufptr, uint32_t size, uint8_t (*callback) (uint8_t*, uint8_t), uint8_t* dummy); 126 /**< Write data. */ 127 void (*write)(uint8_t *bufptr, uint32_t size, uint8_t (*callback) (uint8_t*, uint8_t), uint8_t* dummy); 128 } hci_uart_call_t; 129 /** @} */ 130 131 /** @addtogroup BLE_COMMEN_TYPEDEF Typedefs 132 * @{ 133 */ 134 135 /** @brief The BLE sync event callback. */ 136 typedef void (*ble_sync_evt_cb_t)(uint32_t sync_cnt, uint16_t sync_period); 137 /** @} */ 138 139 /** @addtogroup BLE_COMMEN_FUNCTIONS Functions 140 * @{ */ 141 /** 142 ***************************************************************************************** 143 * @brief Initialize BEL Stack. 144 * 145 * @param[in] p_app_callback: Pointer to the structure of app callbacks. 146 * @param[in] p_heaps_table: Pointer to the BLE stack heaps table. 147 ***************************************************************************************** 148 */ 149 void ble_stack_init(app_callback_t *p_app_callback, stack_heaps_table_t *p_heaps_table); 150 151 /** 152 ***************************************************************************************** 153 * @brief Register UART instance for HCI. 154 * 155 * @param[in] id: Instance index. 156 * @param[in] api: Pointer to the struct of function pointers for HCI UART. 157 ***************************************************************************************** 158 */ 159 void ble_hci_uart_register(uint8_t id, hci_uart_call_t *api); 160 161 /** 162 ***************************************************************************************** 163 * @brief Register BLE idle time notification callback function. 164 * 165 * @param[in] callback: function pointer of BLE idle time notification function. 166 * @note param[in] of callback: hs - the idle time of BLE in half slot (312.5μs). 167 * Callback will be called by BLE ISR to notify the rest idle time if there are some BLE activities. 168 * It should be realized as simlpe as you can. 169 * It's not suitable for ISO activities. 170 ***************************************************************************************** 171 */ 172 void ble_idle_time_notify_cb_register(void (*callback)(uint32_t hs)); 173 174 /** 175 ***************************************************************************************** 176 * @brief Register BLE activity - start notification callback function. 177 * 178 * @param[in] callback: function pointer of BLE activity - start notification. 179 * @note param[in] of callback: e_role - the role of activity, 180 * @ref gap_activity_role_t for possible roles. 181 * param[in] of callback: index - The index parameter is interpreted by role. 182 * If the role is @ref GAP_ACTIVITY_ROLE_ADV, it's the index of Advertising. 183 * If the role is @ref GAP_ACTIVITY_ROLE_CON, it's the index of Connection. 184 * For all other roles, it should be ignored. 185 * A callback will be called by BLE ISR when the BLE activity starts every time. 186 * It should be realized as simlpe as you can. 187 * Notice: You must define the start callback in the RAM space to avoid hardfault. 188 * It's not suitable for ISO activities. 189 ***************************************************************************************** 190 */ 191 void ble_activity_start_notify_cb_register(void (*callback)(gap_activity_role_t e_role, 192 uint8_t index)); 193 194 /** 195 ***************************************************************************************** 196 * @brief Register BLE activity end notification callback function. 197 * 198 * @param[in] callback: function pointer of BLE activity - end notification function. 199 * @note param[in] of callback: e_role - the role of activity, 200 * @ref gap_activity_role_t for possible roles. 201 * param[in] of callback: index - The index parameter is interpreted by role. 202 * If the role is @ref GAP_ACTIVITY_ROLE_ADV, it's the index of Advertising. 203 * If the role is @ref GAP_ACTIVITY_ROLE_CON, it's the index of Connection. 204 * For all other roles, it should be ignored. 205 * A callback will be called by BLE ISR when the BLE activity ends every time. 206 * It should be realized as simlpe as you can. You'd better to define it in the RAM space 207 * It's not suitable for ISO activities. 208 ***************************************************************************************** 209 */ 210 void ble_activity_end_notify_cb_register(void (*callback)(gap_activity_role_t e_role, 211 uint8_t index)); 212 213 /** 214 ***************************************************************************************** 215 * @brief Create sync source. 216 * 217 * @param[in] period: Period of sync source. 218 * 219 * @return SDK_ERR_DISALLOWED: Create sync source fail. 220 * SDK_SUCCESS: Create sync source successfully. 221 ***************************************************************************************** 222 */ 223 uint16_t ble_sync_source_create(uint16_t period); 224 225 /** 226 ***************************************************************************************** 227 * @brief Register sync event callback. 228 * 229 * @param[in] sync_evt_cb: Sync event callback. 230 * 231 * @return SDK_ERR_POINTER_NULL: Pointer to sync event callback is NULL. 232 * SDK_SUCCESS: Register successfully. 233 ***************************************************************************************** 234 */ 235 uint16_t ble_sync_evt_cb_register(ble_sync_evt_cb_t sync_evt_cb); 236 237 /** 238 ***************************************************************************************** 239 * @brief Distribute sync source. 240 * 241 * @param[in] conn_idx: The connection index. 242 * 243 * @return SDK_ERR_INVALID_CONN_IDX: Invalid connect index. 244 * SDK_ERR_DISALLOWED: Distribute is disallowed. 245 * SDK_SUCCESS: Distribute successfully. 246 ***************************************************************************************** 247 */ 248 uint16_t ble_sync_source_distribute(uint8_t conn_idx); 249 250 /** 251 ***************************************************************************************** 252 * @brief Destroy sync source. 253 * 254 * @return SDK_SUCCESS: Destroy successfully. 255 ***************************************************************************************** 256 */ 257 uint16_t ble_sync_source_destroy(void); 258 259 /** 260 ***************************************************************************************** 261 * @brief Change the RF TX mode of LP or ULP. 262 * 263 * @param[in] e_rf_tx_mode: Refer to @ref ble_rf_tx_mode_t. 264 * BLE_RF_TX_MODE_LP_MODE: LP mode. 265 * BLE_RF_TX_MODE_ULP_MODE: ULP mode. 266 * Others: invalid mode. 267 * 268 * @note This function should be called before BLE stack init. 269 * 270 * @return SDK_SUCCESS: Successfully set Tx mode. 271 * SDK_ERR_DISALLOWED: Failed to set Tx mode. 272 ***************************************************************************************** 273 */ 274 uint8_t ble_rf_tx_mode_set(ble_rf_tx_mode_t e_rf_tx_mode); 275 276 /** 277 ***************************************************************************************** 278 * @brief Get the RF TX mode of LP or ULP. 279 * 280 * @return BLE_RF_TX_MODE_LP_MODE: LP Mode. 281 * BLE_RF_TX_MODE_ULP_MODE: ULP Mode. 282 * Others: Fail. 283 ***************************************************************************************** 284 */ 285 ble_rf_tx_mode_t ble_rf_tx_mode_get(void); 286 287 /** 288 ***************************************************************************************** 289 * @brief Set the resistance value of the RF match circuit (unit: ohm). 290 * 291 * @note This function should be called before BLE stack init. 292 * 293 * @param[in] e_ohm: The resistance value (ohm) of the RF match circuit according to the board. 294 * BLE_RF_MATCH_CIRCUIT_25OHM: 25 ohm. 295 * BLE_RF_MATCH_CIRCUIT_100OHM: 100 ohm. 296 * Others: invalid. 297 ***************************************************************************************** 298 */ 299 void ble_rf_match_circuit_set(ble_rf_match_circuit_t e_ohm); 300 301 /** 302 ***************************************************************************************** 303 * @brief Get the resistance value of the RF match circuit (unit: ohm). 304 * 305 * @return The resistance value (ohm) of the RF match circuit according to the board (ohm). 306 * BLE_RF_MATCH_CIRCUIT_25OHM: 25 ohm. 307 * BLE_RF_MATCH_CIRCUIT_100OHM: 100 ohm. 308 * Others: invalid. 309 ***************************************************************************************** 310 */ 311 ble_rf_match_circuit_t ble_rf_match_circuit_get(void); 312 313 /** 314 ***************************************************************************************** 315 * @brief Generate a signal carrier wave. 316 * 317 * @param[in] channel: 0~39 channel, 2402~2480 Mhz. 318 * 319 ***************************************************************************************** 320 */ 321 void send_signal_carrier_wave(uint8_t channel); 322 323 /** @} */ 324 #endif 325 /** @} */ 326 /** @} */ 327