Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos mbed mbed_fota_fan_control
Fork of mbed_fota_lamp_control by
app.cpp
00001 /** 00002 * @file app.cpp 00003 * @brief Application entry point 00004 * Copyright 2015 SEVENCORE Co., Ltd. 00005 * 00006 * @author HyeongJun Kim 00007 * @version 1.0.0 00008 * @date 2015-08-17 00009 */ 00010 #include "app.h" 00011 #include "gapm_task.h" 00012 #include "dialog_fota_config.h" 00013 #include "diss_task.h" 00014 #include "fota_server_task.h" 00015 #include "fan_control_task.h" 00016 #include "lamp_control_task.h" 00017 00018 00019 namespace sevencore_fota{ 00020 /** 00021 **************************************************************************************** 00022 * @addtogroup dialog_fota module 00023 * @brief Application entry point function definition. 00024 * 00025 * @{ 00026 **************************************************************************************** 00027 */ 00028 struct app_env_tag app_env; 00029 /** 00030 **************************************************************************************** 00031 * @brief Send Reset request to GAPM task. 00032 * @param[in] BMH Ble Message Handler class reference 00033 * @return void. 00034 **************************************************************************************** 00035 */ 00036 void app_rst_gap(BleMsgHandler *BMH) 00037 { 00038 struct gapm_reset_cmd gm_cmd; 00039 gm_cmd.operation = GAPM_RESET; 00040 00041 app_env.state = APP_IDLE; 00042 app_env.num_of_devices = 0; 00043 for (int i=0; i < MAX_SCAN_DEVICES; i++) 00044 { 00045 app_env.devices[i].free = true; 00046 app_env.devices[i].adv_addr.addr[0] = '\0'; 00047 app_env.devices[i].data[0] = '\0'; 00048 app_env.devices[i].data_len = 0; 00049 app_env.devices[i].rssi = 0; 00050 } 00051 00052 uint8_t *msg; 00053 unsigned short msg_size = 1+sizeof(ble_hdr) + sizeof(gapm_reset_cmd); 00054 msg = new uint8_t[msg_size]; 00055 BMH->BleMsgAlloc(GAPM_RESET_CMD, TASK_GAPM, TASK_GTL, 00056 sizeof(struct gapm_reset_cmd),&gm_cmd,msg); 00057 00058 BMH->BleSendMsg(msg,msg_size); 00059 00060 return; 00061 } 00062 /** 00063 **************************************************************************************** 00064 * @brief Send enable request to DISS profile task. 00065 * @param[in] BMH Ble Message Handler class reference 00066 * @return void. 00067 **************************************************************************************** 00068 */ 00069 void app_diss_db_create(BleMsgHandler* BMH) 00070 { 00071 uint8_t *msg; 00072 // Add DIS in the database 00073 struct diss_create_db_req req; 00074 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct diss_create_db_req); 00075 req.features = (DIS_MANUFACTURER_NAME_CHAR_SUP|DIS_MODEL_NB_STR_CHAR_SUP|DIS_SERIAL_NB_STR_CHAR_SUP); 00076 00077 msg = new uint8_t[msg_size]; 00078 00079 BMH->BleMsgAlloc(DISS_CREATE_DB_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_create_db_req),&req,msg); 00080 BMH->BleSendMsg(msg,msg_size); 00081 } 00082 /** 00083 **************************************************************************************** 00084 * @brief Send enable request to FOTA profile task. 00085 * @param[in] BMH Ble Message Handler class reference 00086 * @return void. 00087 **************************************************************************************** 00088 */ 00089 void app_fota_server_db_create(BleMsgHandler* BMH) 00090 { 00091 uint8_t *msg; 00092 // Add DIS in the database 00093 struct fota_server_create_db_req req; 00094 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct fota_server_create_db_req); 00095 req.features = 0x01FF; 00096 00097 msg = new uint8_t[msg_size]; 00098 00099 BMH->BleMsgAlloc(FOTA_SERVER_CREATE_DB_REQ,TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_create_db_req),&req,msg); 00100 BMH->BleSendMsg(msg,msg_size); 00101 } 00102 /** 00103 **************************************************************************************** 00104 * @brief Send enable request to Fan Control profile task. 00105 * @param[in] BMH Ble Message Handler class reference 00106 * @return void. 00107 **************************************************************************************** 00108 */ 00109 void app_fan_control_db_create(BleMsgHandler* BMH) 00110 { 00111 uint8_t *msg; 00112 // Add fan_control service table in the database 00113 struct fan_control_create_db_req req; 00114 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct fan_control_create_db_req); 00115 req.features = 0x001F; 00116 00117 msg = new uint8_t[msg_size]; 00118 00119 BMH->BleMsgAlloc(FAN_CONTROL_CREATE_DB_REQ,TASK_FAN_CONTROL, TASK_GTL,sizeof(struct fan_control_create_db_req),&req,msg); 00120 BMH->BleSendMsg(msg,msg_size); 00121 } 00122 /** 00123 **************************************************************************************** 00124 * @brief Send enable request to Lamp Control profile task. 00125 * @param[in] BMH Ble Message Handler class reference 00126 * @return void. 00127 **************************************************************************************** 00128 */ 00129 void app_lamp_control_db_create(BleMsgHandler* BMH) 00130 { 00131 uint8_t *msg; 00132 // Add fan_control service table in the database 00133 struct lamp_control_create_db_req req; 00134 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct lamp_control_create_db_req); 00135 req.features = 0x000F; 00136 00137 msg = new uint8_t[msg_size]; 00138 00139 BMH->BleMsgAlloc(LAMP_CONTROL_CREATE_DB_REQ,TASK_LAMP_CONTROL, TASK_GTL,sizeof(struct lamp_control_create_db_req),&req,msg); 00140 BMH->BleSendMsg(msg,msg_size); 00141 } 00142 /** 00143 **************************************************************************************** 00144 * @brief Send Start Advertising command to GAPM task. 00145 * @param[in] BMH Ble Message Handler class reference 00146 * @return void. 00147 **************************************************************************************** 00148 */ 00149 void app_adv_start(BleMsgHandler* BMH) 00150 { 00151 uint8_t device_name_length; 00152 uint8_t device_name_avail_space; 00153 uint8_t device_name_temp_buf[64]; 00154 uint8_t *msg; 00155 unsigned short msg_size; 00156 msg_size = 1 + sizeof(ble_hdr) + sizeof(struct gapm_start_advertise_cmd); 00157 msg = new uint8_t[msg_size]; 00158 00159 // Allocate a message for GAP 00160 struct gapm_start_advertise_cmd cmd; 00161 cmd.op.code = GAPM_ADV_UNDIRECT; 00162 cmd.op.addr_src = GAPM_PUBLIC_ADDR; 00163 cmd.intv_min = APP_ADV_INT_MIN; 00164 cmd.intv_max = APP_ADV_INT_MAX; 00165 cmd.channel_map = APP_ADV_CHMAP; 00166 cmd.info.host.mode = GAP_GEN_DISCOVERABLE; 00167 cmd.info.host.adv_data_len = APP_ADV_DATA_MAX_SIZE; 00168 cmd.info.host.scan_rsp_data_len = APP_SCAN_RESP_DATA_MAX_SIZE; 00169 00170 /*----------------------------------------------------------------------------------- 00171 * Set the Advertising Data and the Scan Response Data 00172 *---------------------------------------------------------------------------------*/ 00173 00174 00175 // Advertising Data 00176 #if (NVDS_SUPPORT) 00177 if(nvds_get(NVDS_TAG_APP_BLE_ADV_DATA, &cmd.info.host.adv_data_len, 00178 &cmd.info.host.adv_data[0]) != NVDS_OK) 00179 #endif //(NVDS_SUPPORT) 00180 { 00181 cmd.info.host.adv_data_len = APP_DFLT_ADV_DATA_LEN; 00182 memcpy(&cmd.info.host.adv_data[0], APP_DFLT_ADV_DATA, cmd.info.host.adv_data_len); 00183 00184 //Add list of UUID 00185 #if (BLE_APP_HT) 00186 cmd.info.host.adv_data_len += APP_HT_ADV_DATA_UUID_LEN; 00187 memcpy(&cmd.info.host.adv_data[APP_DFLT_ADV_DATA_LEN], APP_HT_ADV_DATA_UUID, APP_HT_ADV_DATA_UUID_LEN); 00188 #else 00189 #if (BLE_APP_NEB) 00190 cmd.info.host.adv_data_len += APP_NEB_ADV_DATA_UUID_LEN; 00191 memcpy(&cmd.info.host.adv_data[APP_DFLT_ADV_DATA_LEN], APP_NEB_ADV_DATA_UUID, APP_NEB_ADV_DATA_UUID_LEN); 00192 #endif //(BLE_APP_NEB) 00193 #endif //(BLE_APP_HT) 00194 } 00195 00196 // Scan Response Data 00197 #if (NVDS_SUPPORT) 00198 if(nvds_get(NVDS_TAG_APP_BLE_SCAN_RESP_DATA, &cmd.info.host.scan_rsp_data_len, 00199 &cmd.info.host.scan_rsp_data[0]) != NVDS_OK) 00200 #endif //(NVDS_SUPPORT) 00201 { 00202 cmd.info.host.scan_rsp_data_len = APP_SCNRSP_DATA_LENGTH; 00203 memcpy(&cmd.info.host.scan_rsp_data[0], APP_SCNRSP_DATA, cmd.info.host.scan_rsp_data_len); 00204 } 00205 00206 // Get remaining space in the Advertising Data - 2 bytes are used for name length/flag 00207 device_name_avail_space = APP_ADV_DATA_MAX_SIZE - cmd.info.host.adv_data_len - 2; 00208 00209 // Check if data can be added to the Advertising data 00210 if (device_name_avail_space > 0) 00211 { 00212 // Get the Device Name to add in the Advertising Data (Default one or NVDS one) 00213 #if (NVDS_SUPPORT) 00214 device_name_length = NVDS_LEN_DEVICE_NAME; 00215 if (nvds_get(NVDS_TAG_DEVICE_NAME, &device_name_length, &device_name_temp_buf[0]) != NVDS_OK) 00216 #endif //(NVDS_SUPPORT) 00217 { 00218 // Get default Device Name (No name if not enough space) 00219 device_name_length = strlen(APP_DFLT_DEVICE_NAME); 00220 memcpy(&device_name_temp_buf[0], APP_DFLT_DEVICE_NAME, device_name_length); 00221 } 00222 00223 if(device_name_length > 0) 00224 { 00225 // Check available space 00226 if( device_name_length > device_name_avail_space) 00227 device_name_length = device_name_avail_space; 00228 00229 // Fill Length 00230 cmd.info.host.adv_data[cmd.info.host.adv_data_len] = device_name_length + 1; 00231 // Fill Device Name Flag 00232 cmd.info.host.adv_data[cmd.info.host.adv_data_len + 1] = '\x09'; 00233 // Copy device name 00234 memcpy(&cmd.info.host.adv_data[cmd.info.host.adv_data_len + 2], device_name_temp_buf, device_name_length); 00235 00236 // Update Advertising Data Length 00237 cmd.info.host.adv_data_len += (device_name_length + 2); 00238 } 00239 } 00240 00241 // Send the message 00242 BMH->BleMsgAlloc(GAPM_START_ADVERTISE_CMD,TASK_GAPM, TASK_GTL,sizeof (struct gapm_start_advertise_cmd),&cmd,msg); 00243 BMH->BleSendMsg(msg, msg_size); 00244 00245 return; 00246 } 00247 /** 00248 **************************************************************************************** 00249 * @brief Set Bondabe mode. 00250 * @param[in] BMH Ble Message Handler class reference 00251 * @return void. 00252 **************************************************************************************** 00253 */ 00254 void app_set_mode(BleMsgHandler* BMH) 00255 { 00256 uint8_t *msg; 00257 struct gapm_set_dev_config_cmd cmd; 00258 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct gapm_set_dev_config_cmd); 00259 msg = new uint8_t[msg_size]; 00260 00261 cmd.operation = GAPM_SET_DEV_CONFIG; 00262 // Device Role 00263 cmd.role = GAP_PERIPHERAL_SLV; 00264 // Device Appearance 00265 cmd.appearance = 0x0000; 00266 // Device Appearance write permission requirements for peer device 00267 cmd.appearance_write_perm = GAPM_WRITE_DISABLE; 00268 // Device Name write permission requirements for peer device 00269 cmd.name_write_perm = GAPM_WRITE_DISABLE; 00270 // Peripheral only: ***************************************************************** 00271 // Maximum trasnimt unit size 00272 //cmd.max_mtu = 16; 00273 // Slave preferred Minimum of connection interval 00274 cmd.con_intv_min = 8; // 10ms (8*1.25ms) 00275 // Slave preferred Maximum of connection interval 00276 cmd.con_intv_max = 16; // 20ms (16*1.25ms) 00277 // Slave preferred Connection latency 00278 cmd.con_latency = 0; 00279 // Slave preferred Link supervision timeout 00280 cmd.superv_to = 100; 00281 // Privacy settings bit field 00282 cmd.flags = 0; 00283 00284 BMH->BleMsgAlloc(GAPM_SET_DEV_CONFIG_CMD, TASK_GAPM, TASK_GTL,sizeof(struct gapm_set_dev_config_cmd ),&cmd, msg); 00285 BMH->BleSendMsg(msg,msg_size); 00286 00287 return; 00288 } 00289 /** 00290 **************************************************************************************** 00291 * @brief Send enable request to DISS profile task. 00292 * @param[in] device Ble device info 00293 * @param[in] BMH Ble Message Handler class reference 00294 * @return void. 00295 **************************************************************************************** 00296 */ 00297 void app_dis_enable(ble_dev *device,BleMsgHandler* BMH) 00298 { 00299 uint8_t *msg; 00300 // Allocate the message 00301 struct diss_enable_req req; 00302 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(diss_enable_req); 00303 // Fill in the parameter structure 00304 req.conhdl = device->conhdl; 00305 req.sec_lvl = 1; 00306 req.con_type = PRF_CON_DISCOVERY; 00307 00308 msg = new uint8_t[msg_size]; 00309 00310 // Send the message 00311 BMH->BleMsgAlloc(DISS_ENABLE_REQ,TASK_DISS, TASK_GTL, sizeof(struct diss_enable_req),&req,msg); 00312 BMH->BleSendMsg(msg,msg_size); 00313 } 00314 /** 00315 **************************************************************************************** 00316 * @brief Send enable request to FOTA profile task. 00317 * @param[in] device Ble device info 00318 * @param[in] BMH Ble Message Handler class reference 00319 * @return void. 00320 **************************************************************************************** 00321 */ 00322 void app_fota_server_enable(ble_dev *device,BleMsgHandler* BMH) 00323 { 00324 uint8_t *msg; 00325 struct fota_server_enable_req req; 00326 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct fota_server_enable_req); 00327 req.conhdl = device->conhdl; 00328 req.sec_lvl = 1; 00329 00330 msg = new uint8_t[msg_size]; 00331 00332 // Send the message 00333 BMH->BleMsgAlloc(FOTA_SERVER_ENABLE_REQ, TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_enable_req),&req,msg); 00334 BMH->BleSendMsg(msg,msg_size); 00335 } 00336 /** 00337 **************************************************************************************** 00338 * @brief Send enable request to fan_control profile task. 00339 * @param[in] device Ble device info 00340 * @param[in] BMH Ble Message Handler class reference 00341 * @return void. 00342 **************************************************************************************** 00343 */ 00344 void app_fan_control_enable(ble_dev *device,BleMsgHandler* BMH) 00345 { 00346 uint8_t *msg; 00347 struct fan_control_enable_req req; 00348 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct fan_control_enable_req); 00349 req.conhdl = device->conhdl; 00350 req.sec_lvl = 1; 00351 00352 msg = new uint8_t[msg_size]; 00353 00354 // Send the message 00355 BMH->BleMsgAlloc(FAN_CONTROL_ENABLE_REQ, TASK_FAN_CONTROL, TASK_GTL,sizeof(struct fan_control_enable_req),&req,msg); 00356 BMH->BleSendMsg(msg,msg_size); 00357 } 00358 /** 00359 **************************************************************************************** 00360 * @brief Send enable request to lamp_control profile task. 00361 * @param[in] device Ble device info 00362 * @param[in] BMH Ble Message Handler class reference 00363 * @return void. 00364 **************************************************************************************** 00365 */ 00366 void app_lamp_control_enable(ble_dev *device,BleMsgHandler* BMH) 00367 { 00368 uint8_t *msg; 00369 struct lamp_control_enable_req req; 00370 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct lamp_control_enable_req); 00371 req.conhdl = device->conhdl; 00372 req.sec_lvl = 1; 00373 00374 msg = new uint8_t[msg_size]; 00375 00376 // Send the message 00377 BMH->BleMsgAlloc(LAMP_CONTROL_ENABLE_REQ, TASK_LAMP_CONTROL, TASK_GTL,sizeof(struct lamp_control_enable_req),&req,msg); 00378 BMH->BleSendMsg(msg,msg_size); 00379 } 00380 /** 00381 **************************************************************************************** 00382 * @brief Send connection confirmation. 00383 * param[in] auth Authentication requirements. 00384 * @param[in] device Ble device info 00385 * @param[in] BMH Ble Message Handler class reference 00386 * @return void. 00387 **************************************************************************************** 00388 */ 00389 void app_connect_confirm(uint8_t auth,ble_dev *device,BleMsgHandler* BMH) 00390 { 00391 uint8_t *msg; 00392 // confirm connection 00393 struct gapc_connection_cfm cfm; 00394 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(gapc_connection_cfm); 00395 cfm.auth = auth; 00396 cfm.authorize = GAP_AUTHZ_NOT_SET; 00397 00398 msg = new uint8_t[msg_size]; 00399 00400 // Send the message 00401 BMH->BleMsgAlloc(GAPC_CONNECTION_CFM, KE_BUILD_ID(TASK_GAPC,device->conidx), TASK_GTL,sizeof (struct gapc_connection_cfm),&cfm,msg); 00402 BMH->BleSendMsg(msg,msg_size); 00403 } 00404 /** 00405 **************************************************************************************** 00406 * @brief Send the GAPC_DISCONNECT_IND message to a task. 00407 * @param[in] dst Task id of the destination task. 00408 * @param[in] conhdl The conhdl parameter of the GAPC_DISCONNECT_IND message. 00409 * @param[in] reason The reason parameter of the GAPC_DISCONNECT_IND message. 00410 * @param[in] BMH Ble Message Handler class reference 00411 * @return void. 00412 **************************************************************************************** 00413 */ 00414 void app_send_disconnect(uint16_t dst, uint16_t conhdl, uint8_t reason,BleMsgHandler* BMH) 00415 { 00416 uint8_t *msg; 00417 struct gapc_disconnect_ind disconnect_ind; 00418 unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(gapc_disconnect_ind); 00419 // fill parameters 00420 disconnect_ind.conhdl = conhdl; 00421 disconnect_ind.reason = reason; 00422 00423 msg = new uint8_t[msg_size]; 00424 00425 // send indication 00426 BMH->BleMsgAlloc(GAPC_DISCONNECT_IND,dst, TASK_GTL,sizeof(struct gapc_disconnect_ind),&disconnect_ind,msg); 00427 BMH->BleSendMsg(msg,msg_size); 00428 } 00429 00430 }//namespace 00431 00432 /// @} dialog_fota module
Generated on Tue Jul 12 2022 21:25:05 by
