1 /* 2 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved. 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 16 /** 17 **************************************************************************************** 18 * 19 * @file app.h 20 * 21 * @brief Application entry point 22 * 23 **************************************************************************************** 24 */ 25 26 #ifndef APP_H_ 27 #define APP_H_ 28 29 /** 30 **************************************************************************************** 31 * @addtogroup APP 32 * @ingroup RICOW 33 * 34 * @brief Application entry point. 35 * 36 * @{ 37 **************************************************************************************** 38 */ 39 40 /* 41 * INCLUDE FILES 42 **************************************************************************************** 43 */ 44 #include "sonata_ble_hook.h" 45 #include "msm_ble_api.h" 46 #include "sonata_gap.h" 47 #include "sonata_gap_api.h" 48 49 /* 50 * DEFINES 51 **************************************************************************************** 52 */ 53 54 /* 55 * MACROS 56 **************************************************************************************** 57 */ 58 // debug trace 59 #define APP_DBG 1 60 #if APP_DBG 61 #define APP_TRC printf 62 #else 63 #define APP_TRC(...) 64 #endif // APP_DBG 65 66 #define APP_DBG_ERROR 1 67 #if APP_DBG_ERROR 68 #define APP_TRC_ERROR printf 69 #else 70 #define APP_TRC_ERROR(...) 71 #endif // APP_DBG_HIGH 72 /* 73 * ENUMERATIONS 74 **************************************************************************************** 75 */ 76 #define MAX_BONDED_DEV_NUM (3) 77 #define MAX_BONDED_DEV_INDEX (MAX_BONDED_DEV_NUM - 1) 78 #define INVALID_BONDED_INDEX (-1) 79 #define APP_GAP_KEY_LEN (0x10) 80 #define APP_GAP_RAND_NB_LEN (0x08) 81 #define APP_BD_ADDR_LEN (6) 82 #define APP_UUID_LEN (16) 83 #define KEY_LEN 0x10 84 85 enum app_connect_state { 86 /// Connection succeeded 87 APP_STATE_CONNECTED = 0, 88 // Link is disconnected 89 APP_STATE_DISCONNECTED, 90 }; 91 92 typedef enum { 93 APP_DISCONNECTED, 94 APP_CONNECTED, 95 APP_BONDING, 96 APP_BONDED, 97 } bound_conn_state; 98 99 /* 100 * TYPE DEFINITIONS 101 **************************************************************************************** 102 */ 103 104 // Long Term Key information 105 typedef struct app_sonata_gap_ltk { 106 // Long Term Key 107 uint8_t ltk[APP_GAP_KEY_LEN]; 108 // Encryption Diversifier 109 uint16_t ediv; 110 // Random Number 111 uint8_t randnb[APP_GAP_RAND_NB_LEN]; 112 } app_sonata_gap_ltk_t; 113 114 // Short Term Key information 115 typedef struct app_sonata_gap_irk { 116 // Short Term Key 117 uint8_t irk[APP_GAP_KEY_LEN]; 118 // irk addr 119 uint8_t irk_addr[APP_BD_ADDR_LEN]; 120 } app_sonata_gap_irk_t; 121 122 typedef struct bonded_dev_info { 123 uint8_t peer_addr[APP_BD_ADDR_LEN]; 124 app_sonata_gap_ltk_t ltk; 125 uint8_t ltk_in[APP_GAP_KEY_LEN]; 126 app_sonata_gap_irk_t irk; 127 uint8_t periph_bond; 128 } bonded_dev_info_t; 129 130 typedef struct bonded_dev_info_list { 131 uint8_t total_dev; 132 uint8_t current_dev_index; 133 bonded_dev_info_t bonded_device_info[MAX_BONDED_DEV_NUM]; 134 } bonded_dev_info_list_t; 135 136 typedef struct peer_conn_param { 137 // Connection interval maximum 138 uint16_t intv_max; 139 // Latency 140 uint16_t latency; 141 // Supervision timeout 142 uint16_t time_out; 143 } peer_conn_param_t; 144 145 typedef struct connect_req_info { 146 uint8_t conidx; 147 uint8_t bd_addr[APP_BD_ADDR_LEN]; 148 } connect_req_info_t; 149 150 typedef struct adv_idx_info { 151 uint8_t local_idx; 152 uint8_t adv_id; 153 } adv_idx_info_t; 154 155 typedef struct { 156 uint8_t advdata[31]; 157 uint8_t advdataLen; 158 159 } ble_adv_data_set_t; 160 161 typedef struct { 162 uint8_t respdata[31]; 163 uint8_t respdataLen; 164 165 } ble_scan_data_set_t; 166 167 typedef struct { 168 int status; 169 int len; 170 int handler; 171 uint8_t uuid[APP_UUID_LEN]; 172 } app_reg_service_cmp_t; 173 174 typedef struct { 175 int connId; 176 uint8_t addr[APP_BD_ADDR_LEN]; 177 } app_connect_status_ind_t; 178 179 typedef struct { 180 int connId; 181 int status; 182 } app_ind_sent_ind_t; 183 184 typedef struct { 185 int connId; 186 int mtu; 187 } app_mtu_change_ind_t; 188 189 typedef struct { 190 int advId; 191 int status; 192 } app_adv_status_ind_t; 193 194 /** 195 * @brief enum core evt indicate type 196 */ 197 typedef enum { 198 BLE_SERVICE_ADD_CMP, 199 BLE_DEV_CONNECTED, 200 BLE_DEV_DISCONNECTED, 201 BLE_IND_SENT, 202 BLE_MTU_CHANGE, 203 BLE_ADV_START, 204 BLE_ADV_STOP, 205 } app_core_evt_ind_t; 206 207 /* 208 * GLOBAL VARIABLE DECLARATION 209 **************************************************************************************** 210 */ 211 extern sonata_ble_hook_t app_hook; 212 213 /// app Core Event indicate Callback 214 typedef int (*app_core_evt_ind_cb)(app_core_evt_ind_t evt, void *p_param); 215 216 typedef int (*app_sec_req_cb)(uint8_t *addr); 217 218 typedef enum { 219 USER_INVALID_MODULE_ID, 220 USER_MIDEA_MODULE_ID, 221 USER_OHOS_MODULE_ID, 222 USER_MAX_MODULE_ID 223 } ble_stack_opr_module_id_t; 224 225 #define APP_ACTIVE_MAX 5 226 227 typedef struct app_uuid_t { 228 uint16_t service; 229 uint16_t read; 230 uint16_t write; 231 uint16_t ntf; 232 } app_uuids; 233 typedef struct actives_t { 234 uint8_t assign_id ; 235 uint8_t type; // A0:Adv A1:Scan A2:Peer 236 bool runing; 237 uint8_t peer[SONATA_GAP_BD_ADDR_LEN]; 238 uint8_t name[20]; 239 } actives; 240 typedef struct app_env_t { 241 242 uint8_t gAppStatus; 243 uint16_t attrHandle; 244 uint16_t targetWriteHandle; 245 uint16_t targetReadHandle; 246 uint16_t targetNtfHandle; 247 app_uuids appUuids; 248 actives act[APP_ACTIVE_MAX]; 249 } app_env; 250 /* 251 * FUNCTION DECLARATIONS 252 **************************************************************************************** 253 */ 254 255 /** 256 **************************************************************************************** 257 * @brief Initialize the BLE demo application. 258 **************************************************************************************** 259 */ 260 void app_init(void); 261 void app_ble_config_legacy_advertising(void); 262 void app_ble_config_scanning(void); 263 void app_ble_config_initiating(void); 264 void app_ble_stop_scanning(void); 265 266 uint8_t app_get_adv_status(void); 267 uint8_t app_get_connect_status(void); 268 uint16_t app_ble_start_advertising(uint8_t adv_id); 269 bool app_is_ble_test_mode(void); 270 void app_set_ble_test_mode(bool mode); 271 uint16_t app_ble_advertising_stop(uint8_t adv_id); 272 int app_ble_advertising_start(uint8_t *adv_id, ble_adv_data_set_t *data, ble_scan_data_set_t *scan_data); 273 void app_gap_set_scan_cb(app_ble_scan_callback_t cb); 274 int app_ble_stack_stop(ble_stack_opr_module_id_t module); 275 int app_ble_stack_start(ble_stack_opr_module_id_t module); 276 void app_ble_set_device_name(uint8_t *name, uint32_t len); 277 int app_set_security_io_cap(uint8_t cap); 278 int app_ble_disconnect_by_addr(uint8_t *addr); 279 int app_ble_disable_service_by_handler(uint16_t start_hdl); 280 void app_register_core_evt_ind(app_core_evt_ind_cb cb); 281 void app_gap_notify_pair_request_rsp(uint8_t *bd_addr, uint8_t accept); 282 void app_register_sec_cb(app_sec_req_cb cb); 283 int app_set_security_auth_req(uint8_t auth_req); 284 void app_set_connect_flag(uint8_t vaule); 285 void app_gap_connect_confirm(uint8_t *addr, uint8_t auth); 286 void app_ble_gatt_data_send_notify(uint16_t local_handle, uint16_t idx, uint16_t length, uint8_t *p_value); 287 uint16_t app_ble_stop_adv_without_id(void); 288 void app_ble_set_target_address(uint8_t *target); 289 void app_ble_set_uuids(uint16_t service, uint16_t read, uint16_t write, uint16_t ntf); 290 bool app_ble_master_write_data(uint8_t conidx, uint16_t length, uint8_t *data); 291 bool app_ble_master_read_data(uint8_t conidx, uint16_t length, uint8_t *data); 292 bool app_ble_master_turn_ntf(uint8_t conidx, bool on); 293 app_uuids *app_ble_get_uuids(void); 294 void app_ble_disconnect(uint8_t conidx); 295 actives *app_get_active(void); 296 void app_ble_set_test_write_uuid(uint8_t *uuid); 297 void app_ble_set_test_read_uuid(uint8_t *uuid); 298 void app_ble_start_advertising_with_param(sonata_gap_directed_adv_create_param_t *param, ble_adv_data_set_t *data, 299 ble_scan_data_set_t *scan_data, uint8_t own_addr_type, uint16_t duration, uint8_t max_adv_evt); 300 301 // @} APP 302 303 #endif // APP_H_ 304