BLE FOTA / Mbed 2 deprecated mbed_fota Featured

Dependencies:   mbed

Fork of mbed_fota by KIM HyoengJun

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers app.cpp Source File

app.cpp

Go to the documentation of this file.
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 
00016 namespace sevencore_fota{
00017 /**
00018  ****************************************************************************************
00019  * @addtogroup dialog_fota module
00020  * @brief Application entry point function definition.
00021  *
00022  * @{
00023  ****************************************************************************************
00024  */ 
00025 struct app_env_tag app_env;
00026 /**
00027  ****************************************************************************************
00028  * @brief Send Reset request to GAPM task.
00029  * @param[in] BMH     Ble Message Handler class reference
00030  * @return void.
00031  ****************************************************************************************
00032  */
00033 void app_rst_gap(BleMsgHandler *BMH)
00034 {
00035     struct gapm_reset_cmd gm_cmd;
00036     gm_cmd.operation = GAPM_RESET;
00037     
00038     app_env.state = APP_IDLE;
00039     app_env.num_of_devices = 0;
00040     for (int i=0; i < MAX_SCAN_DEVICES; i++)
00041     {
00042         app_env.devices[i].free = true;
00043         app_env.devices[i].adv_addr.addr[0] = '\0';
00044         app_env.devices[i].data[0] = '\0';
00045         app_env.devices[i].data_len = 0;
00046         app_env.devices[i].rssi = 0;
00047     }
00048     
00049     uint8_t *msg;
00050     unsigned short msg_size = 1+sizeof(ble_hdr) + sizeof(gapm_reset_cmd);
00051     msg = new uint8_t[msg_size];
00052     BMH->BleMsgAlloc(GAPM_RESET_CMD, TASK_GAPM, TASK_GTL,
00053                         sizeof(struct gapm_reset_cmd),&gm_cmd,msg);    
00054     
00055     BMH->BleSendMsg(msg,msg_size);
00056 
00057     return;
00058 }
00059 /**
00060  ****************************************************************************************
00061  * @brief Send enable request to DISS profile task.
00062  * @param[in] BMH     Ble Message Handler class reference
00063  * @return void.
00064  ****************************************************************************************
00065  */
00066 void app_diss_db_create(BleMsgHandler* BMH)
00067 {
00068     uint8_t *msg;
00069     // Add DIS in the database
00070     struct diss_create_db_req req;
00071     unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct diss_create_db_req);
00072     req.features = (DIS_MANUFACTURER_NAME_CHAR_SUP|DIS_MODEL_NB_STR_CHAR_SUP|DIS_SERIAL_NB_STR_CHAR_SUP);
00073     
00074     msg = new uint8_t[msg_size];
00075     
00076     BMH->BleMsgAlloc(DISS_CREATE_DB_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_create_db_req),&req,msg);
00077     BMH->BleSendMsg(msg,msg_size);
00078 }
00079 /**
00080  ****************************************************************************************
00081  * @brief Send enable request to FOTA profile task.
00082  * @param[in] BMH     Ble Message Handler class reference
00083  * @return void.
00084  ****************************************************************************************
00085  */
00086 void app_fota_server_db_create(BleMsgHandler* BMH)
00087 {
00088     uint8_t *msg;
00089     // Add DIS in the database
00090     struct fota_server_create_db_req req;
00091     unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct fota_server_create_db_req);
00092     req.features = 0x01FF;
00093     
00094     msg = new uint8_t[msg_size];
00095     
00096     BMH->BleMsgAlloc(FOTA_SERVER_CREATE_DB_REQ,TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_create_db_req),&req,msg);
00097     BMH->BleSendMsg(msg,msg_size);
00098 }
00099 /**
00100  ****************************************************************************************
00101  * @brief Send Start Advertising command to GAPM task.
00102  * @param[in] BMH     Ble Message Handler class reference
00103  * @return void.
00104  ****************************************************************************************
00105  */
00106 void app_adv_start(BleMsgHandler* BMH)
00107 {
00108     uint8_t device_name_length;
00109     uint8_t device_name_avail_space;
00110     uint8_t device_name_temp_buf[64];
00111     uint8_t *msg;
00112     unsigned short msg_size;
00113     msg_size = 1 + sizeof(ble_hdr) + sizeof(struct gapm_start_advertise_cmd);
00114     msg = new uint8_t[msg_size];
00115     
00116     // Allocate a message for GAP
00117     struct gapm_start_advertise_cmd cmd;
00118     cmd.op.code     = GAPM_ADV_UNDIRECT;
00119     cmd.op.addr_src = GAPM_PUBLIC_ADDR;
00120     cmd.intv_min    = APP_ADV_INT_MIN;
00121     cmd.intv_max    = APP_ADV_INT_MAX;
00122     cmd.channel_map = APP_ADV_CHMAP;
00123     cmd.info.host.mode = GAP_GEN_DISCOVERABLE;
00124     cmd.info.host.adv_data_len       = APP_ADV_DATA_MAX_SIZE;
00125     cmd.info.host.scan_rsp_data_len  = APP_SCAN_RESP_DATA_MAX_SIZE;
00126 
00127     /*-----------------------------------------------------------------------------------
00128      * Set the Advertising Data and the Scan Response Data
00129      *---------------------------------------------------------------------------------*/
00130     
00131 
00132     // Advertising Data
00133     #if (NVDS_SUPPORT)
00134     if(nvds_get(NVDS_TAG_APP_BLE_ADV_DATA, &cmd.info.host.adv_data_len,
00135                 &cmd.info.host.adv_data[0]) != NVDS_OK)
00136     #endif //(NVDS_SUPPORT)
00137     {
00138         cmd.info.host.adv_data_len = APP_DFLT_ADV_DATA_LEN;
00139         memcpy(&cmd.info.host.adv_data[0], APP_DFLT_ADV_DATA, cmd.info.host.adv_data_len);
00140 
00141         //Add list of UUID
00142         #if (BLE_APP_HT)
00143         cmd.info.host.adv_data_len += APP_HT_ADV_DATA_UUID_LEN;
00144         memcpy(&cmd.info.host.adv_data[APP_DFLT_ADV_DATA_LEN], APP_HT_ADV_DATA_UUID, APP_HT_ADV_DATA_UUID_LEN);
00145         #else
00146             #if (BLE_APP_NEB)
00147             cmd.info.host.adv_data_len += APP_NEB_ADV_DATA_UUID_LEN;
00148             memcpy(&cmd.info.host.adv_data[APP_DFLT_ADV_DATA_LEN], APP_NEB_ADV_DATA_UUID, APP_NEB_ADV_DATA_UUID_LEN);
00149             #endif //(BLE_APP_NEB)
00150         #endif //(BLE_APP_HT)
00151     }
00152 
00153     // Scan Response Data
00154     #if (NVDS_SUPPORT)
00155     if(nvds_get(NVDS_TAG_APP_BLE_SCAN_RESP_DATA, &cmd.info.host.scan_rsp_data_len,
00156                 &cmd.info.host.scan_rsp_data[0]) != NVDS_OK)
00157     #endif //(NVDS_SUPPORT)
00158     {
00159         cmd.info.host.scan_rsp_data_len = APP_SCNRSP_DATA_LENGTH;
00160         memcpy(&cmd.info.host.scan_rsp_data[0], APP_SCNRSP_DATA, cmd.info.host.scan_rsp_data_len);
00161     }
00162 
00163     // Get remaining space in the Advertising Data - 2 bytes are used for name length/flag
00164     device_name_avail_space = APP_ADV_DATA_MAX_SIZE - cmd.info.host.adv_data_len - 2;
00165 
00166     // Check if data can be added to the Advertising data
00167     if (device_name_avail_space > 0)
00168     {
00169         // Get the Device Name to add in the Advertising Data (Default one or NVDS one)
00170         #if (NVDS_SUPPORT)
00171         device_name_length = NVDS_LEN_DEVICE_NAME;
00172         if (nvds_get(NVDS_TAG_DEVICE_NAME, &device_name_length, &device_name_temp_buf[0]) != NVDS_OK)
00173         #endif //(NVDS_SUPPORT)
00174         {
00175             // Get default Device Name (No name if not enough space)
00176             device_name_length = strlen(APP_DFLT_DEVICE_NAME);
00177             memcpy(&device_name_temp_buf[0], APP_DFLT_DEVICE_NAME, device_name_length);
00178         }
00179 
00180         if(device_name_length > 0)
00181         {
00182             // Check available space
00183             if( device_name_length > device_name_avail_space)
00184                 device_name_length = device_name_avail_space;
00185 
00186             // Fill Length
00187             cmd.info.host.adv_data[cmd.info.host.adv_data_len]     = device_name_length + 1;
00188             // Fill Device Name Flag
00189             cmd.info.host.adv_data[cmd.info.host.adv_data_len + 1] = '\x09';
00190             // Copy device name
00191             memcpy(&cmd.info.host.adv_data[cmd.info.host.adv_data_len + 2], device_name_temp_buf, device_name_length);
00192 
00193             // Update Advertising Data Length
00194             cmd.info.host.adv_data_len += (device_name_length + 2);
00195         }
00196     }
00197     
00198     // Send the message
00199     BMH->BleMsgAlloc(GAPM_START_ADVERTISE_CMD,TASK_GAPM, TASK_GTL,sizeof (struct gapm_start_advertise_cmd),&cmd,msg);
00200     BMH->BleSendMsg(msg, msg_size);
00201 
00202     return;
00203 }
00204 /**
00205  ****************************************************************************************
00206  * @brief Set Bondabe mode.
00207  * @param[in] BMH     Ble Message Handler class reference
00208  * @return void.
00209  ****************************************************************************************
00210  */
00211 void app_set_mode(BleMsgHandler* BMH)
00212 {
00213     uint8_t *msg;
00214     struct gapm_set_dev_config_cmd cmd;
00215     unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct gapm_set_dev_config_cmd);
00216     msg = new uint8_t[msg_size];
00217     
00218     cmd.operation = GAPM_SET_DEV_CONFIG;
00219     // Device Role
00220     cmd.role = GAP_PERIPHERAL_SLV;
00221     // Device Appearance
00222     cmd.appearance = 0x0000;
00223     // Device Appearance write permission requirements for peer device
00224     cmd.appearance_write_perm = GAPM_WRITE_DISABLE;
00225     // Device Name write permission requirements for peer device
00226     cmd.name_write_perm = GAPM_WRITE_DISABLE;
00227     // Peripheral only: *****************************************************************
00228     // Maximum trasnimt unit size
00229     //cmd.max_mtu = 16;
00230     // Slave preferred Minimum of connection interval
00231     cmd.con_intv_min = 8;         // 10ms (8*1.25ms)
00232     // Slave preferred Maximum of connection interval
00233     cmd.con_intv_max = 16;        // 20ms (16*1.25ms)
00234     // Slave preferred Connection latency
00235     cmd.con_latency  = 0;
00236     // Slave preferred Link supervision timeout
00237     cmd.superv_to    = 100;
00238     // Privacy settings bit field
00239     cmd.flags = 0;
00240     
00241     BMH->BleMsgAlloc(GAPM_SET_DEV_CONFIG_CMD, TASK_GAPM, TASK_GTL,sizeof(struct gapm_set_dev_config_cmd ),&cmd, msg);
00242     BMH->BleSendMsg(msg,msg_size);
00243 
00244     return;
00245 }
00246 /**
00247  ****************************************************************************************
00248  * @brief Send enable request to DISS profile task.
00249  * @param[in] device  Ble device info
00250  * @param[in] BMH     Ble Message Handler class reference
00251  * @return void.
00252  ****************************************************************************************
00253  */
00254  void app_dis_enable(ble_dev *device,BleMsgHandler* BMH)
00255 {
00256     uint8_t *msg;
00257     // Allocate the message
00258     struct diss_enable_req req;
00259     unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(diss_enable_req);
00260     // Fill in the parameter structure
00261     req.conhdl             = device->conhdl;
00262     req.sec_lvl            = 1;
00263     req.con_type           = PRF_CON_DISCOVERY;
00264 
00265     msg = new uint8_t[msg_size];
00266     
00267     // Send the message
00268     BMH->BleMsgAlloc(DISS_ENABLE_REQ,TASK_DISS, TASK_GTL, sizeof(struct diss_enable_req),&req,msg);
00269     BMH->BleSendMsg(msg,msg_size);
00270 }
00271 /**
00272  ****************************************************************************************
00273  * @brief Send enable request to FOTA profile task.
00274  * @param[in] device  Ble device info
00275  * @param[in] BMH     Ble Message Handler class reference
00276  * @return void.
00277  ****************************************************************************************
00278  */
00279 void app_fota_server_enable(ble_dev *device,BleMsgHandler* BMH)
00280 {
00281     uint8_t *msg;
00282     struct fota_server_enable_req req;
00283     unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(struct fota_server_enable_req);
00284     req.conhdl = device->conhdl;
00285     req.sec_lvl = 1;
00286     
00287     msg = new uint8_t[msg_size];
00288 
00289     // Send the message
00290     BMH->BleMsgAlloc(FOTA_SERVER_ENABLE_REQ, TASK_FOTA_SERVER, TASK_GTL,sizeof(struct fota_server_enable_req),&req,msg);
00291     BMH->BleSendMsg(msg,msg_size);
00292 }
00293 /**
00294  ****************************************************************************************
00295  * @brief Send connection confirmation.
00296  * param[in] auth  Authentication requirements.
00297  * @param[in] device  Ble device info
00298  * @param[in] BMH     Ble Message Handler class reference
00299  * @return void.
00300  ****************************************************************************************
00301  */
00302 void app_connect_confirm(uint8_t auth,ble_dev *device,BleMsgHandler* BMH)
00303 {
00304     uint8_t *msg;
00305     // confirm connection
00306     struct gapc_connection_cfm cfm;
00307     unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(gapc_connection_cfm);
00308     cfm.auth = auth;
00309     cfm.authorize = GAP_AUTHZ_NOT_SET;
00310     
00311     msg = new uint8_t[msg_size];
00312 
00313     // Send the message
00314     BMH->BleMsgAlloc(GAPC_CONNECTION_CFM, KE_BUILD_ID(TASK_GAPC,device->conidx), TASK_GTL,sizeof (struct gapc_connection_cfm),&cfm,msg);
00315     BMH->BleSendMsg(msg,msg_size);
00316 }
00317 /**
00318  ****************************************************************************************
00319  * @brief Send the GAPC_DISCONNECT_IND message to a task.
00320  * @param[in] dst     Task id of the destination task.
00321  * @param[in] conhdl  The conhdl parameter of the GAPC_DISCONNECT_IND message.
00322  * @param[in] reason  The reason parameter of the GAPC_DISCONNECT_IND message.
00323  * @param[in] BMH     Ble Message Handler class reference
00324  * @return void.
00325  ****************************************************************************************
00326  */
00327 void app_send_disconnect(uint16_t dst, uint16_t conhdl, uint8_t reason,BleMsgHandler* BMH)
00328 {
00329     uint8_t *msg;
00330     struct gapc_disconnect_ind disconnect_ind; 
00331     unsigned short msg_size = 1 + sizeof(ble_hdr) + sizeof(gapc_disconnect_ind);
00332     // fill parameters
00333     disconnect_ind.conhdl   = conhdl;
00334     disconnect_ind.reason   = reason;
00335     
00336     msg = new uint8_t[msg_size];
00337 
00338     // send indication
00339     BMH->BleMsgAlloc(GAPC_DISCONNECT_IND,dst, TASK_GTL,sizeof(struct gapc_disconnect_ind),&disconnect_ind,msg);
00340     BMH->BleSendMsg(msg,msg_size);
00341 }
00342 
00343 }//namespace
00344 
00345 /// @} dialog_fota module