Bluetooth Low Energy based Firmware Over The Air with Mbed. Mbed part is a external processor of the IoT devices and communicate with a Bluetooth module. The Bluetooth module have to support BLE and implement BLE FOTA profile designed by ours. BLE FOTA profile specification is available from our GIT hub wiki(https://github.com/sevencore/BLEFOTA).
Dependencies: mbed
Fork of mbed_fota by
app_task.cpp
00001 /** 00002 * @file app_task.cpp 00003 * @brief Handling for ble events and responses. 00004 * Copyright 2015 SEVENCORE Co., Ltd. 00005 * 00006 * @author HyeongJun Kim 00007 * @version 1.0.0 00008 * @date 2015-08-20 00009 */ 00010 #include "app_task.h" 00011 #include "app.h" 00012 #include "fota_server_task.h" 00013 00014 #define BLE_FOTA_SERVICE 1 00015 /** 00016 **************************************************************************************** 00017 * @addtogroup dialog_fota module 00018 * @brief application handlers for ble events and responses. 00019 * 00020 * @{ 00021 **************************************************************************************** 00022 */ 00023 namespace sevencore_fota{ 00024 00025 /** 00026 **************************************************************************************** 00027 * @brief Handles GAPM_ADV_REPORT_IND event. 00028 * 00029 * @param[in] msgid Id of the message received. 00030 * @param[in] param Pointer to the parameters of the message. 00031 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00032 * @param[in] src_id ID of the sending task instance. 00033 * 00034 * @return If the message was consumed or not. 00035 **************************************************************************************** 00036 */ 00037 int gapm_adv_report_ind_handler(unsigned short msgid, 00038 struct gapm_adv_report_ind *param, 00039 unsigned short dest_id, 00040 unsigned short src_id) 00041 { 00042 //if (app_env.state != APP_SCAN) 00043 // return -1; 00044 00045 return 0; 00046 } 00047 /** 00048 **************************************************************************************** 00049 * @brief Handles the DISS_CREATE_DB_CFM message. 00050 * 00051 * @param[in] msgid Id of the message received. 00052 * @param[in] param Pointer to the parameters of the message. 00053 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00054 * @param[in] src_id ID of the sending task instance. 00055 * @param[in] BMH Ble Message Handler class reference. 00056 * 00057 * @return If the message was consumed or not. 00058 **************************************************************************************** 00059 */ 00060 int diss_create_db_cfm_handler(unsigned short msgid, 00061 struct diss_create_db_cfm *param, 00062 unsigned short dest_id, 00063 unsigned short src_id, 00064 BleMsgHandler* BMH) 00065 { 00066 00067 uint8_t len = strlen(APP_DIS_SW_REV); 00068 00069 if (param->status == CO_ERROR_NO_ERROR) 00070 { 00071 char str[22] = "\nDIS SET DB\n"; 00072 BMH->HostPcPrint(str); 00073 // Set Manufacturer Name value in the DB 00074 { 00075 uint8_t *msg; 00076 struct diss_set_char_val_req req_name; 00077 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MANUFACTURER_NAME_STR_LEN); 00078 00079 // Fill in the parameter structure 00080 req_name.char_code = DIS_MANUFACTURER_NAME_CHAR; 00081 req_name.val_len = APP_DIS_MANUFACTURER_NAME_STR_LEN; 00082 memcpy(&req_name.val[0], APP_DIS_MANUFACTURER_NAME_STR, APP_DIS_MANUFACTURER_NAME_STR_LEN); 00083 00084 msg = new uint8_t[msg_size]; 00085 00086 BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS,TASK_GTL, 00087 sizeof(struct diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MANUFACTURER_NAME_STR_LEN),&req_name,msg); 00088 BMH->BleSendMsg(msg,msg_size); 00089 free(msg); 00090 00091 } 00092 00093 // Set Model Number String value in the DB 00094 { 00095 uint8_t *msg; 00096 struct diss_set_char_val_req req_mod; 00097 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MODEL_NB_STR_LEN); 00098 00099 // Fill in the parameter structure 00100 req_mod.char_code = DIS_MODEL_NB_STR_CHAR; 00101 req_mod.val_len = APP_DIS_MODEL_NB_STR_LEN; 00102 memcpy(&req_mod.val[0], APP_DIS_MODEL_NB_STR, APP_DIS_MODEL_NB_STR_LEN); 00103 00104 msg = new uint8_t[msg_size]; 00105 00106 BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL, 00107 sizeof(struct diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MODEL_NB_STR_LEN),&req_mod,msg); 00108 // Send the message 00109 BMH->BleSendMsg(msg,msg_size); 00110 free(msg); 00111 } 00112 #if (BLE_FOTA_SERVICE) 00113 // Set Serial Number String value in the DB 00114 { 00115 uint8_t *msg; 00116 struct diss_set_char_val_req req_ser; 00117 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_SERIAL_NB_STR_LEN); 00118 00119 // Fill in the parameter structure 00120 req_ser.char_code = DIS_SERIAL_NB_STR_CHAR; 00121 req_ser.val_len = APP_DIS_SERIAL_NB_STR_LEN; 00122 memcpy(&req_ser.val[0], APP_DIS_SERIAL_NB_STR, APP_DIS_SERIAL_NB_STR_LEN); 00123 00124 msg = new uint8_t[msg_size]; 00125 00126 BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL, 00127 sizeof(struct diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_SERIAL_NB_STR_LEN),&req_ser,msg); 00128 // Send the message 00129 BMH->BleSendMsg(msg,msg_size); 00130 free(msg); 00131 } 00132 #endif //(BLE_FOTA_SERVICE) 00133 #if !(BLE_FOTA_SERVICE) 00134 // Set System ID value in the DB 00135 { 00136 uint8_t *msg; 00137 struct diss_set_char_val_req req_id; 00138 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req); 00139 00140 // Fill in the parameter structure 00141 req_id.char_code = DIS_SYSTEM_ID_CHAR; 00142 req_id.val_len = APP_DIS_SYSTEM_ID_LEN; 00143 memcpy(&req_id.val[0], APP_DIS_SYSTEM_ID, APP_DIS_SYSTEM_ID_LEN); 00144 00145 msg = new uint8_t[msg_size]; 00146 00147 BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_set_char_val_req),&req_id,msg); 00148 00149 // Send the message 00150 BMH->BleSendMsg(msg, msg_size); 00151 free(msg); 00152 } 00153 00154 00155 // Set the software version in the DB 00156 { 00157 uint8_t *msg; 00158 struct diss_set_char_val_req req_id; 00159 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req); 00160 00161 // Fill in the parameter structure 00162 req_id.char_code = DIS_SW_REV_STR_CHAR; 00163 req_id.val_len = len; 00164 memcpy(&req_id.val[0], APP_DIS_SW_REV, len); 00165 00166 msg = new uint8_t[msg_size]; 00167 00168 BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_set_char_val_req),&req_id,msg); 00169 // Send the message 00170 BMH->BleSendMsg(msg,msg_size); 00171 free(msg); 00172 } 00173 00174 len = strlen(APP_DIS_FIRM_REV); 00175 // Set the firmware version in the DB. This is the common code sw version 00176 { 00177 uint8_t *msg; 00178 struct diss_set_char_val_req req_id; 00179 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req); 00180 00181 // Fill in the parameter structure 00182 req_id.char_code = DIS_FIRM_REV_STR_CHAR; 00183 req_id.val_len = len; 00184 memcpy(&req_id.val[0], APP_DIS_FIRM_REV, len); 00185 00186 msg = new uint8_t[msg_size]; 00187 00188 BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_set_char_val_req),&req_id,msg); 00189 // Send the message 00190 BMH->BleSendMsg(msg, msg_size); 00191 free(msg); 00192 } 00193 #endif //!(BLE_FOTA_SERVICE) 00194 } 00195 00196 if (app_env.state == APP_IDLE) 00197 { 00198 char str[30] = "\nfota db create req!!\n"; 00199 BMH->HostPcPrint(str); 00200 //app_set_mode(BMH); 00201 app_fota_server_db_create(BMH); 00202 } 00203 00204 return 0; 00205 } 00206 /** 00207 **************************************************************************************** 00208 * @brief Handles the FOTA_SERVER_CREATE_DB_CFM message. 00209 * 00210 * @param[in] msgid Id of the message received. 00211 * @param[in] param Pointer to the parameters of the message. 00212 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00213 * @param[in] src_id ID of the sending task instance. 00214 * @param[in] BMH Ble Message Handler class reference. 00215 * 00216 * @return If the message was consumed or not. 00217 **************************************************************************************** 00218 */ 00219 int fota_server_create_db_cfm_handler(unsigned short msgid, 00220 struct fota_server_create_db_cfm *param, 00221 unsigned short dest_id, 00222 unsigned short src_id, 00223 BleMsgHandler* BMH) 00224 { 00225 uint8_t fota_normal = 0; 00226 uint8_t fota_normal_len = sizeof(fota_normal); 00227 if (param->status == CO_ERROR_NO_ERROR) 00228 { 00229 // Initialization Firmware new Version Char ("00-00-00") 00230 { 00231 uint8_t *msg; 00232 struct fota_server_set_char_val_req req_nv; 00233 unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fota_server_set_char_val_req)-(18-APP_FOTA_SERVER_FIRMWARE_NEW_VERSION_LEN); 00234 // Fill in the parameter structure 00235 req_nv.char_code = FOTA_SERVER_FIRMWARE_NEW_VERSION_CHAR; 00236 req_nv.val_len = APP_FOTA_SERVER_FIRMWARE_NEW_VERSION_LEN; 00237 memcpy(&req_nv.val[0], APP_FOTA_SERVER_FIRMWARE_NEW_VERSION, APP_FOTA_SERVER_FIRMWARE_NEW_VERSION_LEN); 00238 00239 msg = new uint8_t[msg_size]; 00240 00241 BMH->BleMsgAlloc(FOTA_SERVER_SET_CHAR_VAL_REQ,TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_set_char_val_req)-(18-APP_FOTA_SERVER_FIRMWARE_NEW_VERSION_LEN),&req_nv,msg ); 00242 // Send the message 00243 BMH->BleSendMsg(msg,msg_size); 00244 free(msg); 00245 } 00246 00247 // Initialization Sequence Number Char ( = '0' ) 00248 { 00249 uint8_t *msg; 00250 struct fota_server_set_char_val_req req_sn; 00251 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len); 00252 00253 // Fill in the parameter structure 00254 req_sn.char_code = FOTA_SERVER_SEQUENCE_NUMBER_CHAR; 00255 req_sn.val_len = fota_normal_len; 00256 memcpy(&req_sn.val[0], &fota_normal, fota_normal_len); 00257 00258 msg = new uint8_t[msg_size]; 00259 00260 BMH->BleMsgAlloc(FOTA_SERVER_SET_CHAR_VAL_REQ,TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_set_char_val_req)-(18-fota_normal_len),&req_sn,msg ); 00261 // Send the message 00262 BMH->BleSendMsg(msg,msg_size); 00263 free(msg); 00264 } 00265 00266 // Initialization Firmware Data Check Char ( = '0' ) 00267 { 00268 uint8_t *msg; 00269 struct fota_server_set_char_val_req req_fdc; 00270 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len); 00271 00272 // Fill in the parameter structure 00273 req_fdc.char_code = FOTA_SERVER_FIRMWARE_DATA_CHECK_CHAR; 00274 req_fdc.val_len = fota_normal_len; 00275 memcpy(&req_fdc.val[0], &fota_normal, fota_normal_len); 00276 00277 msg = new uint8_t[msg_size]; 00278 00279 BMH->BleMsgAlloc(FOTA_SERVER_SET_CHAR_VAL_REQ,TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_set_char_val_req)-(18-fota_normal_len),&req_fdc,msg ); 00280 // Send the message 00281 BMH->BleSendMsg(msg,msg_size); 00282 free(msg); 00283 } 00284 00285 // Initialization Firmware Status Char ( = '0' ) 00286 { 00287 uint8_t *msg; 00288 struct fota_server_set_char_val_req req_stat; 00289 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len); 00290 00291 // Fill in the parameter structure 00292 req_stat.char_code = FOTA_SERVER_FIRMWARE_STATUS_CHAR; 00293 req_stat.val_len = fota_normal_len; 00294 memcpy(&req_stat.val[0], &fota_normal, fota_normal_len); 00295 00296 msg = new uint8_t[msg_size]; 00297 00298 BMH->BleMsgAlloc(FOTA_SERVER_SET_CHAR_VAL_REQ,TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_set_char_val_req)-(18-fota_normal_len),&req_stat,msg ); 00299 // Send the message 00300 BMH->BleSendMsg(msg,msg_size); 00301 free(msg); 00302 } 00303 00304 // Initialization Reset Char ( = '0' ) 00305 { 00306 uint8_t *msg; 00307 struct fota_server_set_char_val_req req_reset; 00308 unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len); 00309 00310 // Fill in the parameter structure 00311 req_reset.char_code = FOTA_SERVER_RESET_CHAR; 00312 req_reset.val_len = fota_normal_len; 00313 memcpy(&req_reset.val[0], &fota_normal, fota_normal_len); 00314 00315 msg = new uint8_t[msg_size]; 00316 00317 BMH->BleMsgAlloc(FOTA_SERVER_SET_CHAR_VAL_REQ,TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_set_char_val_req)-(18-fota_normal_len),&req_reset,msg ); 00318 // Send the message 00319 BMH->BleSendMsg(msg,msg_size); 00320 free(msg); 00321 } 00322 00323 } 00324 00325 if (app_env.state == APP_IDLE) 00326 { 00327 app_set_mode(BMH); 00328 } 00329 00330 return 0; 00331 } 00332 /** 00333 **************************************************************************************** 00334 * @brief Handles Firmware Data Copy event. 00335 * 00336 * @param[in] msgid Id of the message received. 00337 * @param[in] param Pointer to the parameters of the message. 00338 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00339 * @param[in] src_id ID of the sending task instance. 00340 * @param[in] BMH Ble Message Handler class reference. 00341 * 00342 * @return If the message was consumed or not. 00343 **************************************************************************************** 00344 */ 00345 void fota_server_data_flash_ind_handler(unsigned short msgid, 00346 struct fota_server_data_flash_ind *param, 00347 unsigned short dest_id, 00348 unsigned short src_id, 00349 BleMsgHandler* BMH) 00350 { 00351 char version[9]; 00352 memcpy(version,param->version,8); 00353 version[8] = '\0'; 00354 BMH->FirmwareDataReceive(param->code_size,version); 00355 } 00356 /** 00357 **************************************************************************************** 00358 * @brief Handles ready indication from the GAP. 00359 * 00360 * @param[in] msgid Id of the message received. 00361 * @param[in] param Pointer to the parameters of the message. 00362 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00363 * @param[in] src_id ID of the sending task instance. 00364 * @param[in] BMH Ble Message Handler class reference. 00365 * 00366 * @return If the message was consumed or not. 00367 **************************************************************************************** 00368 */ 00369 int gapm_device_ready_ind_handler(unsigned short msgid, 00370 struct gap_ready_evt *param, 00371 unsigned short dest_id, 00372 unsigned short src_id, 00373 BleMsgHandler* BMH) 00374 { 00375 // We are now in Connectable State 00376 if (dest_id == TASK_GTL) 00377 { 00378 app_rst_gap(BMH); 00379 } 00380 00381 return 0; 00382 } 00383 /** 00384 **************************************************************************************** 00385 * @brief Handle reset GAP request completion event. 00386 * 00387 * @param[in] msgid Id of the message received. 00388 * @param[in] param Pointer to the parameters of the message. 00389 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00390 * @param[in] src_id ID of the sending task instance. 00391 * @param[in] BMH Ble Message Handler class reference. 00392 * 00393 * @return If the message was consumed or not. 00394 **************************************************************************************** 00395 */ 00396 int gapm_reset_completion_handler(unsigned short msgid, 00397 struct gapm_cmp_evt *param, 00398 unsigned short dest_id, 00399 unsigned short src_id, 00400 BleMsgHandler* BMH) 00401 { 00402 // We are now in Connectable State 00403 if (dest_id == TASK_GTL) 00404 { 00405 app_env.state = APP_IDLE; 00406 app_diss_db_create(BMH); 00407 } 00408 00409 return 0; 00410 } 00411 /** 00412 **************************************************************************************** 00413 * @brief Handles GAPM_CMP_EVT event for GAPM_SET_DEV_CONFIG_CMD. 00414 * 00415 * @param[in] msgid Id of the message received. 00416 * @param[in] param Pointer to the parameters of the message. 00417 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00418 * @param[in] src_id ID of the sending task instance. 00419 * @param[in] BMH Ble Message Handler class reference. 00420 * 00421 * @return If the message was consumed or not. 00422 **************************************************************************************** 00423 */ 00424 int gapm_set_dev_config_completion_handler(unsigned short msgid, 00425 struct gapm_cmp_evt *param, 00426 unsigned short dest_id, 00427 unsigned short src_id, 00428 BleMsgHandler* BMH) 00429 { 00430 app_env.state = APP_CONNECTABLE; 00431 wait(1); 00432 app_adv_start(BMH); // start advertising 00433 00434 return 0; 00435 } 00436 /** 00437 **************************************************************************************** 00438 * @brief Handles Connection request indication event. 00439 * 00440 * @param[in] msgid Id of the message received. 00441 * @param[in] param Pointer to the parameters of the message. 00442 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00443 * @param[in] src_id ID of the sending task instance. 00444 * @param[in] BMH Ble Message Handler class reference. 00445 * 00446 * @return If the message was consumed or not. 00447 **************************************************************************************** 00448 */ 00449 int gapc_connection_req_ind_handler(uint16_t msgid, 00450 struct gapc_connection_req_ind *param, 00451 uint16_t dest_id, 00452 uint16_t src_id, 00453 BleMsgHandler* BMH) 00454 { 00455 app_env.state = APP_CONNECTED; 00456 00457 ble_dev device; 00458 00459 // Retrieve the connection index from the GAPC task instance for the connection 00460 device.conidx = KE_IDX_GET(src_id); 00461 00462 // Retrieve the connection handle from the parameters 00463 device.conhdl = param->conhdl; 00464 00465 memcpy(device.adv_addr.addr, param->peer_addr.addr, sizeof(struct bd_addr)); 00466 00467 00468 char str[30] = "\nDIS&FOTA ENABLE and CONFIRM\n"; 00469 BMH->HostPcPrint(str); 00470 00471 app_dis_enable(&device,BMH); 00472 app_fota_server_enable(&device,BMH); 00473 app_connect_confirm(GAP_AUTH_REQ_NO_MITM_NO_BOND,&device,BMH); 00474 00475 return 0; 00476 } 00477 /** 00478 **************************************************************************************** 00479 * @brief Handles Discconnection indication event. 00480 * 00481 * @param[in] msgid Id of the message received. 00482 * @param[in] param Pointer to the parameters of the message. 00483 * @param[in] dest_id ID of the receiving task instance (TASK_GAP). 00484 * @param[in] src_id ID of the sending task instance. 00485 * @param[in] BMH Ble Message Handler class reference. 00486 * 00487 * @return If the message was consumed or not. 00488 **************************************************************************************** 00489 */ 00490 int gapc_disconnect_ind_handler(uint16_t msgid, 00491 struct gapc_disconnect_ind *param, 00492 uint16_t dest_id, 00493 uint16_t src_id, 00494 BleMsgHandler* BMH) 00495 { 00496 char str[22] = "\nDevice Disconnected\n"; 00497 BMH->HostPcPrint(str); 00498 00499 app_send_disconnect(TASK_FOTA_SERVER, param->conhdl, param->reason, BMH); 00500 app_env.state = APP_IDLE; 00501 00502 wait(1); 00503 app_set_mode(BMH); 00504 00505 return 0; 00506 } 00507 00508 00509 }//namespace 00510 00511 /// @} dialog_fota module
Generated on Tue Jul 12 2022 16:20:47 by 1.7.2