KIM HyoengJun / Mbed 2 deprecated mbed_fota_robot_control

Dependencies:   mbed-rtos mbed mbed_fota_fan_control

Fork of mbed_fota_lamp_control by KIM HyoengJun

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers app_task.cpp Source File

app_task.cpp

Go to the documentation of this file.
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 #include "fan_control_task.h"
00014 #include "lamp_control_task.h"
00015 #include "mbed.h"
00016 
00017 extern DigitalOut port18;//mid
00018 extern DigitalOut port19;//low
00019 extern DigitalOut port20;//high
00020 
00021 extern DigitalOut port21;//low
00022 extern DigitalOut port22;//high
00023 
00024 extern uint8_t Timer_flag;
00025 extern uint32_t Residual_Time;
00026 
00027 #define BLE_FOTA_SERVICE 1
00028 /**
00029  ****************************************************************************************
00030  * @addtogroup dialog_fota module
00031  * @brief application handlers for ble events and responses.
00032  *
00033  * @{
00034  ****************************************************************************************
00035  */
00036 namespace sevencore_fota{
00037  
00038  
00039 /**
00040  ****************************************************************************************
00041  * @brief Handles GAPM_ADV_REPORT_IND event.
00042  *
00043  * @param[in] msgid     Id of the message received.
00044  * @param[in] param     Pointer to the parameters of the message.
00045  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00046  * @param[in] src_id    ID of the sending task instance.
00047  *
00048  * @return If the message was consumed or not.
00049  ****************************************************************************************
00050  */   
00051 int gapm_adv_report_ind_handler(unsigned short msgid,
00052                                 struct gapm_adv_report_ind *param,
00053                                 unsigned short dest_id,
00054                                 unsigned short src_id)
00055 {
00056     //if (app_env.state != APP_SCAN)
00057     //   return -1;
00058 
00059     return 0;
00060 }
00061 /**
00062  ****************************************************************************************
00063  * @brief Handles the DISS_CREATE_DB_CFM message.
00064  *
00065  * @param[in] msgid     Id of the message received.
00066  * @param[in] param     Pointer to the parameters of the message.
00067  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00068  * @param[in] src_id    ID of the sending task instance.
00069  * @param[in] BMH     Ble Message Handler class reference.
00070  *
00071  * @return If the message was consumed or not.
00072  ****************************************************************************************
00073  */
00074 int diss_create_db_cfm_handler(unsigned short  msgid,
00075                                struct diss_create_db_cfm *param,
00076                                unsigned short  dest_id,
00077                                unsigned short  src_id,
00078                                BleMsgHandler* BMH)
00079 {
00080     
00081     uint8_t len = strlen(APP_DIS_SW_REV);  
00082       
00083     if (param->status == CO_ERROR_NO_ERROR)
00084     {
00085         char str[22] = "\nDIS SET DB\n";
00086         BMH->HostPcPrint(str);
00087         // Set Manufacturer Name value in the DB
00088         {
00089             uint8_t *msg;
00090             struct diss_set_char_val_req req_name;
00091             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MANUFACTURER_NAME_STR_LEN);
00092 
00093             // Fill in the parameter structure
00094             req_name.char_code     = DIS_MANUFACTURER_NAME_CHAR;
00095             req_name.val_len       = APP_DIS_MANUFACTURER_NAME_STR_LEN;
00096             memcpy(&req_name.val[0], APP_DIS_MANUFACTURER_NAME_STR, APP_DIS_MANUFACTURER_NAME_STR_LEN);
00097 
00098             msg = new uint8_t[msg_size];
00099             
00100             BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS,TASK_GTL,
00101                 sizeof(struct diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MANUFACTURER_NAME_STR_LEN),&req_name,msg);
00102             BMH->BleSendMsg(msg,msg_size);
00103             free(msg);
00104             
00105         }
00106 
00107         // Set Model Number String value in the DB
00108         {
00109             uint8_t *msg;
00110             struct diss_set_char_val_req req_mod;
00111             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MODEL_NB_STR_LEN);
00112 
00113             // Fill in the parameter structure
00114             req_mod.char_code     = DIS_MODEL_NB_STR_CHAR;
00115             req_mod.val_len       = APP_DIS_MODEL_NB_STR_LEN;
00116             memcpy(&req_mod.val[0], APP_DIS_MODEL_NB_STR, APP_DIS_MODEL_NB_STR_LEN);
00117 
00118             msg = new uint8_t[msg_size];
00119             
00120             BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,
00121                     sizeof(struct diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_MODEL_NB_STR_LEN),&req_mod,msg);
00122             // Send the message
00123             BMH->BleSendMsg(msg,msg_size);
00124             free(msg);
00125         }
00126 #if (BLE_FOTA_SERVICE)
00127         // Set Serial Number String value in the DB
00128         {
00129             uint8_t *msg;
00130             struct diss_set_char_val_req req_ser;
00131             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_SERIAL_NB_STR_LEN);
00132 
00133             // Fill in the parameter structure
00134             req_ser.char_code     = DIS_SERIAL_NB_STR_CHAR;
00135             req_ser.val_len       = APP_DIS_SERIAL_NB_STR_LEN;
00136             memcpy(&req_ser.val[0], APP_DIS_SERIAL_NB_STR, APP_DIS_SERIAL_NB_STR_LEN);
00137 
00138             msg = new uint8_t[msg_size];
00139             
00140             BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,
00141                     sizeof(struct diss_set_char_val_req)-(STR_MAX_LEN-APP_DIS_SERIAL_NB_STR_LEN),&req_ser,msg);
00142             // Send the message
00143             BMH->BleSendMsg(msg,msg_size);
00144             free(msg);
00145         }
00146 #endif //(BLE_FOTA_SERVICE)
00147 #if !(BLE_FOTA_SERVICE)       
00148         // Set System ID value in the DB
00149         {
00150             uint8_t *msg;
00151             struct diss_set_char_val_req req_id;
00152             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req);
00153 
00154             // Fill in the parameter structure
00155             req_id.char_code     = DIS_SYSTEM_ID_CHAR;
00156             req_id.val_len       = APP_DIS_SYSTEM_ID_LEN;
00157             memcpy(&req_id.val[0], APP_DIS_SYSTEM_ID, APP_DIS_SYSTEM_ID_LEN);
00158             
00159             msg = new uint8_t[msg_size];
00160             
00161             BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_set_char_val_req),&req_id,msg);
00162 
00163             // Send the message
00164             BMH->BleSendMsg(msg, msg_size);
00165             free(msg);
00166         }            
00167         
00168 
00169         // Set the software version in the DB
00170         {
00171             uint8_t *msg;
00172             struct diss_set_char_val_req req_id;
00173             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req);
00174             
00175             // Fill in the parameter structure
00176             req_id.char_code     = DIS_SW_REV_STR_CHAR;
00177             req_id.val_len       = len;
00178             memcpy(&req_id.val[0], APP_DIS_SW_REV, len);
00179             
00180             msg = new uint8_t[msg_size];
00181 
00182             BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_set_char_val_req),&req_id,msg);
00183             // Send the message
00184             BMH->BleSendMsg(msg,msg_size);
00185             free(msg);
00186         }
00187 
00188         len = strlen(APP_DIS_FIRM_REV);
00189         // Set the firmware version in the DB. This is the common code sw version
00190         {
00191             uint8_t *msg;
00192             struct diss_set_char_val_req req_id;
00193             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(diss_set_char_val_req);
00194             
00195             // Fill in the parameter structure
00196             req_id.char_code     = DIS_FIRM_REV_STR_CHAR;
00197             req_id.val_len       = len;
00198             memcpy(&req_id.val[0], APP_DIS_FIRM_REV, len);
00199             
00200             msg = new uint8_t[msg_size];
00201 
00202             BMH->BleMsgAlloc(DISS_SET_CHAR_VAL_REQ,TASK_DISS, TASK_GTL,sizeof(struct diss_set_char_val_req),&req_id,msg);
00203             // Send the message
00204             BMH->BleSendMsg(msg, msg_size);
00205             free(msg);
00206         }
00207 #endif //!(BLE_FOTA_SERVICE)
00208     }
00209     
00210     if (app_env.state == APP_IDLE)
00211     {
00212         char str[30] = "\nfota db create req!!\n";
00213         BMH->HostPcPrint(str);
00214         //app_set_mode(BMH);
00215         app_fota_server_db_create(BMH);
00216     }
00217         
00218     return 0;
00219 }
00220 /**
00221  ****************************************************************************************
00222  * @brief Handles the FOTA_SERVER_CREATE_DB_CFM message.
00223  *
00224  * @param[in] msgid     Id of the message received.
00225  * @param[in] param     Pointer to the parameters of the message.
00226  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00227  * @param[in] src_id    ID of the sending task instance.
00228  * @param[in] BMH     Ble Message Handler class reference.
00229  *
00230  * @return If the message was consumed or not.
00231  ****************************************************************************************
00232  */
00233 int fota_server_create_db_cfm_handler(unsigned short  msgid,
00234                                struct fota_server_create_db_cfm *param,
00235                                unsigned short  dest_id,
00236                                unsigned short  src_id,
00237                                BleMsgHandler* BMH)
00238 { 
00239     uint8_t fota_normal = 0;
00240     uint8_t fota_normal_len = sizeof(fota_normal);
00241     if (param->status == CO_ERROR_NO_ERROR)
00242     {
00243         // Initialization Firmware new Version Char ("00-00-00")
00244         {
00245             uint8_t *msg;
00246             struct fota_server_set_char_val_req req_nv;
00247             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fota_server_set_char_val_req)-(18-APP_FOTA_SERVER_FIRMWARE_NEW_VERSION_LEN);
00248             // Fill in the parameter structure
00249             req_nv.char_code     = FOTA_SERVER_FIRMWARE_NEW_VERSION_CHAR;
00250             req_nv.val_len       = APP_FOTA_SERVER_FIRMWARE_NEW_VERSION_LEN;
00251             memcpy(&req_nv.val[0], APP_FOTA_SERVER_FIRMWARE_NEW_VERSION, APP_FOTA_SERVER_FIRMWARE_NEW_VERSION_LEN);
00252             
00253             msg = new uint8_t[msg_size];
00254             
00255             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 );
00256             // Send the message
00257             BMH->BleSendMsg(msg,msg_size);
00258             free(msg);
00259         }
00260 
00261         // Initialization Sequence Number Char ( = '0' )
00262         {
00263             uint8_t *msg;
00264             struct fota_server_set_char_val_req req_sn;
00265             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len);
00266 
00267             // Fill in the parameter structure
00268             req_sn.char_code     = FOTA_SERVER_SEQUENCE_NUMBER_CHAR;
00269             req_sn.val_len       = fota_normal_len;
00270             memcpy(&req_sn.val[0], &fota_normal, fota_normal_len);
00271 
00272             msg = new uint8_t[msg_size];
00273             
00274             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 );
00275             // Send the message
00276             BMH->BleSendMsg(msg,msg_size);
00277             free(msg);
00278         }
00279         
00280         // Initialization Firmware Data Check Char ( = '0' )
00281         {
00282             uint8_t *msg;
00283             struct fota_server_set_char_val_req req_fdc;
00284             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len);
00285 
00286             // Fill in the parameter structure
00287             req_fdc.char_code     = FOTA_SERVER_FIRMWARE_DATA_CHECK_CHAR;
00288             req_fdc.val_len       = fota_normal_len;
00289             memcpy(&req_fdc.val[0], &fota_normal, fota_normal_len);
00290 
00291             msg = new uint8_t[msg_size];
00292             
00293             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 );
00294             // Send the message
00295             BMH->BleSendMsg(msg,msg_size);
00296             free(msg);
00297         }
00298         
00299         // Initialization Firmware Status Char ( = '0' )
00300         {
00301             uint8_t *msg;
00302             struct fota_server_set_char_val_req req_stat;
00303             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len);
00304 
00305             // Fill in the parameter structure
00306             req_stat.char_code     = FOTA_SERVER_FIRMWARE_STATUS_CHAR;
00307             req_stat.val_len       = fota_normal_len;
00308             memcpy(&req_stat.val[0],  &fota_normal, fota_normal_len);
00309 
00310             msg = new uint8_t[msg_size];
00311             
00312             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 );
00313             // Send the message
00314             BMH->BleSendMsg(msg,msg_size);
00315             free(msg);
00316         }
00317         
00318         // Initialization Reset Char ( = '0' )
00319         {
00320             uint8_t *msg;
00321             struct fota_server_set_char_val_req req_reset;
00322             unsigned short msg_size = 1+sizeof(ble_hdr)+sizeof(fota_server_set_char_val_req)-(18-fota_normal_len);
00323 
00324             // Fill in the parameter structure
00325             req_reset.char_code     = FOTA_SERVER_RESET_CHAR;
00326             req_reset.val_len       = fota_normal_len;
00327             memcpy(&req_reset.val[0], &fota_normal, fota_normal_len);
00328 
00329             msg = new uint8_t[msg_size];
00330             
00331             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 );
00332             // Send the message
00333             BMH->BleSendMsg(msg,msg_size);
00334             free(msg);
00335         }
00336         
00337     }
00338     
00339     if (app_env.state == APP_IDLE)
00340     {
00341         //app_set_mode(BMH);
00342         //app_fan_control_db_create(BMH);
00343         app_lamp_control_db_create(BMH);
00344     }
00345     
00346     return 0;
00347 }
00348 /**
00349  ****************************************************************************************
00350  * @brief Handles Firmware Data Copy event.
00351  *
00352  * @param[in] msgid     Id of the message received.
00353  * @param[in] param     Pointer to the parameters of the message.
00354  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00355  * @param[in] src_id    ID of the sending task instance.
00356  * @param[in] BMH     Ble Message Handler class reference.
00357  *
00358  * @return If the message was consumed or not.
00359  ****************************************************************************************
00360  */
00361 void fota_server_data_flash_ind_handler(unsigned short msgid,
00362                                   struct fota_server_data_flash_ind *param,
00363                                   unsigned short dest_id,
00364                                   unsigned short src_id,
00365                                   BleMsgHandler* BMH)
00366 {
00367     char version[9];
00368     memcpy(version,param->version,8);
00369     version[8] = '\0';
00370     port18 = 1;
00371     BMH->FirmwareDataReceive(param->code_size,version);
00372 }
00373 
00374 /**
00375  ****************************************************************************************
00376  * @brief Handles the FAN_CONTROL_CREATE_DB_CFM message.
00377  *
00378  * @param[in] msgid     Id of the message received.
00379  * @param[in] param     Pointer to the parameters of the message.
00380  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00381  * @param[in] src_id    ID of the sending task instance.
00382  * @param[in] BMH     Ble Message Handler class reference.
00383  *
00384  * @return If the message was consumed or not.
00385  ****************************************************************************************
00386  */
00387 void fan_control_create_db_cfm_handler(unsigned short msgid,
00388                                   struct fan_control_create_db_cfm *param,
00389                                   unsigned short dest_id,
00390                                   unsigned short src_id,
00391                                   BleMsgHandler* BMH)
00392 {
00393     uint8_t fota_normal = 0;
00394     uint8_t fota_normal_len = sizeof(fota_normal);
00395     if (param->status == CO_ERROR_NO_ERROR)
00396     {
00397         // Initialization All Characteristic
00398         {
00399             uint8_t *msg;
00400             struct fan_control_set_char_val_req power_state_val;
00401             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
00402             // Fill in the parameter structure
00403             power_state_val.char_code     = FAN_CONTROL_POWER_STATE_CHAR;
00404             power_state_val.val_len       = FAN_CONTROL_MIN_SIZE;
00405             memcpy(&power_state_val.val[0], &fota_normal, fota_normal_len);
00406             
00407             msg = new uint8_t[msg_size];
00408             
00409             BMH->BleMsgAlloc(FAN_CONTROL_SET_CHAR_VAL_REQ,TASK_FAN_CONTROL, TASK_GTL,sizeof(struct fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE),&power_state_val,msg );
00410             // Send the message
00411             BMH->BleSendMsg(msg,msg_size);
00412             free(msg);
00413         }
00414         {
00415             uint8_t *msg;
00416             struct fan_control_set_char_val_req wind_strength_val;
00417             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
00418             // Fill in the parameter structure
00419             wind_strength_val.char_code     = FAN_CONTROL_WIND_STRENGTH_CHAR;
00420             wind_strength_val.val_len       = FAN_CONTROL_MIN_SIZE;
00421             memcpy(&wind_strength_val.val[0], &fota_normal, fota_normal_len);
00422             
00423             msg = new uint8_t[msg_size];
00424             
00425             BMH->BleMsgAlloc(FAN_CONTROL_SET_CHAR_VAL_REQ,TASK_FAN_CONTROL, TASK_GTL,sizeof(struct fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE),&wind_strength_val,msg );
00426             // Send the message
00427             BMH->BleSendMsg(msg,msg_size);
00428             free(msg);
00429         }
00430         {
00431             uint8_t *msg;
00432             struct fan_control_set_char_val_req fan_spin_val;
00433             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
00434             // Fill in the parameter structure
00435             fan_spin_val.char_code     = FAN_CONTROL_FAN_SPIN_CHAR;
00436             fan_spin_val.val_len       = FAN_CONTROL_MIN_SIZE;
00437             memcpy(&fan_spin_val.val[0], &fota_normal, fota_normal_len);
00438             
00439             msg = new uint8_t[msg_size];
00440             
00441             BMH->BleMsgAlloc(FAN_CONTROL_SET_CHAR_VAL_REQ,TASK_FAN_CONTROL, TASK_GTL,sizeof(struct fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE),&fan_spin_val,msg );
00442             // Send the message
00443             BMH->BleSendMsg(msg,msg_size);
00444             free(msg);
00445         }
00446         {
00447             uint8_t *msg;
00448             struct fan_control_set_char_val_req fan_timer_val;
00449             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
00450             // Fill in the parameter structure
00451             fan_timer_val.char_code     = FAN_CONTROL_FAN_TIMER_CHAR;
00452             fan_timer_val.val_len       = FAN_CONTROL_MIN_SIZE;
00453             memcpy(&fan_timer_val.val[0], &fota_normal, fota_normal_len);
00454             
00455             msg = new uint8_t[msg_size];
00456             
00457             BMH->BleMsgAlloc(FAN_CONTROL_SET_CHAR_VAL_REQ,TASK_FAN_CONTROL, TASK_GTL,sizeof(struct fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE),&fan_timer_val,msg );
00458             // Send the message
00459             BMH->BleSendMsg(msg,msg_size);
00460             free(msg);
00461         }
00462         {
00463             uint8_t *msg;
00464             struct fan_control_set_char_val_req residual_time_val;
00465             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MAX_SIZE);
00466             // Fill in the parameter structure
00467             residual_time_val.char_code     = FAN_CONTROL_RESIDUAL_TIME_CHAR;
00468             residual_time_val.val_len       = FAN_CONTROL_MAX_SIZE;
00469             memcpy(&residual_time_val.val[0], &fota_normal, fota_normal_len*4);
00470             
00471             msg = new uint8_t[msg_size];
00472             
00473             BMH->BleMsgAlloc(FAN_CONTROL_SET_CHAR_VAL_REQ,TASK_FAN_CONTROL, TASK_GTL,sizeof(struct fan_control_set_char_val_req)-(4-FAN_CONTROL_MAX_SIZE),&residual_time_val,msg );
00474             // Send the message
00475             BMH->BleSendMsg(msg,msg_size);
00476             free(msg);
00477         }
00478     }
00479     if (app_env.state == APP_IDLE)
00480     {
00481         app_set_mode(BMH);
00482     }
00483 }
00484 
00485 /**
00486  ****************************************************************************************
00487  * @brief Handles the FAN_CONTROL_COMMAND_IND message.
00488  *
00489  * @param[in] msgid     Id of the message received.
00490  * @param[in] param     Pointer to the parameters of the message.
00491  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00492  * @param[in] src_id    ID of the sending task instance.
00493  * @param[in] BMH     Ble Message Handler class reference.
00494  *
00495  * @return If the message was consumed or not.
00496  ****************************************************************************************
00497  */
00498 void fan_control_command_ind_handler(unsigned short msgid,
00499                                   struct fan_control_command_ind *param,
00500                                   unsigned short dest_id,
00501                                   unsigned short src_id,
00502                                   BleMsgHandler* BMH)
00503 {
00504     char str[30] = "\nFAN remote control START!!\n";
00505     BMH->HostPcPrint(str);
00506     if( param->char_code == FAN_CONTROL_POWER_STATE_CHAR){
00507         if(param->command == 0){
00508             Timer_flag = 0;
00509             Residual_Time = 0;
00510             port18 = 0;
00511             port19 = 0;
00512             port20 = 0;
00513         }
00514         if(param->command == 1){
00515             if( port18 == 0 && port19 == 0 && port20 == 0 )
00516                 port19 = 1;
00517         }
00518     }else if( param->char_code == FAN_CONTROL_WIND_STRENGTH_CHAR){
00519         if( param->command == 1 ){ //low
00520             port18 = 0;
00521             port20 = 0;
00522             port19 = 1;
00523         }
00524         else if( param->command == 2 ){ //mid
00525             port19 = 0;
00526             port20 = 0;
00527             port18 = 1;
00528         }else if( param->command == 3 ){ //high
00529             port18 = 0;
00530             port19 = 0;
00531             port20 = 1;
00532         }
00533     }else if( param->char_code == FAN_CONTROL_FAN_SPIN_CHAR){
00534         
00535     }else if( param->char_code == FAN_CONTROL_FAN_TIMER_CHAR){
00536         Timer_flag = param->command;
00537     }else if( param->char_code == FAN_CONTROL_RESIDUAL_TIME_CHAR){
00538         Residual_Time = param->residual_time;
00539         {
00540                 uint8_t fota_normal = 0;
00541                 uint8_t *msg;
00542                 struct fan_control_set_char_val_req char_set_req;
00543                 unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
00544                 // Fill in the parameter structure
00545                 char_set_req.char_code     = FAN_CONTROL_POWER_STATE_CHAR;
00546                 char_set_req.val_len       = FAN_CONTROL_MIN_SIZE;
00547                 memcpy(&char_set_req.val[0], &fota_normal, sizeof(fota_normal));
00548                 
00549                 msg = new uint8_t[msg_size];
00550                 
00551                 BMH->BleMsgAlloc(FAN_CONTROL_SET_CHAR_VAL_REQ
00552                     ,TASK_FAN_CONTROL, TASK_GTL
00553                     ,sizeof(struct fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE)
00554                     ,&char_set_req,msg );
00555                 // Send the message
00556                 BMH->BleSendMsg(msg,msg_size);                
00557                 free(msg);
00558         }
00559     }
00560 }
00561 /**
00562  ****************************************************************************************
00563  * @brief Handles the LAMP_CONTROL_CREATE_DB_CFM message.
00564  *
00565  * @param[in] msgid     Id of the message received.
00566  * @param[in] param     Pointer to the parameters of the message.
00567  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00568  * @param[in] src_id    ID of the sending task instance.
00569  * @param[in] BMH     Ble Message Handler class reference.
00570  *
00571  * @return If the message was consumed or not.
00572  ****************************************************************************************
00573  */
00574 void lamp_control_create_db_cfm_handler(unsigned short msgid,
00575                                   struct lamp_control_create_db_cfm *param,
00576                                   unsigned short dest_id,
00577                                   unsigned short src_id,
00578                                   BleMsgHandler* BMH)
00579 {
00580     uint8_t fota_normal = 0;
00581     uint8_t fota_normal_len = sizeof(fota_normal);
00582     if (param->status == CO_ERROR_NO_ERROR)
00583     {
00584         // Initialization All Characteristic
00585         {
00586             uint8_t *msg;
00587             struct lamp_control_set_char_val_req power_state_val;
00588             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(lamp_control_set_char_val_req)-(4-LAMP_CONTROL_MIN_SIZE);
00589             // Fill in the parameter structure
00590             power_state_val.char_code     = LAMP_CONTROL_POWER_STATE_CHAR;
00591             power_state_val.val_len       = LAMP_CONTROL_MIN_SIZE;
00592             memcpy(&power_state_val.val[0], &fota_normal, fota_normal_len);
00593             
00594             msg = new uint8_t[msg_size];
00595             
00596             BMH->BleMsgAlloc(LAMP_CONTROL_SET_CHAR_VAL_REQ,TASK_LAMP_CONTROL, TASK_GTL,sizeof(struct lamp_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE),&power_state_val,msg );
00597             // Send the message
00598             BMH->BleSendMsg(msg,msg_size);
00599             free(msg);
00600         }
00601         {
00602             uint8_t *msg;
00603             struct lamp_control_set_char_val_req light_intensity_val;
00604             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(lamp_control_set_char_val_req)-(4-LAMP_CONTROL_MIN_SIZE);
00605             // Fill in the parameter structure
00606             light_intensity_val.char_code     = LAMP_CONTROL_LIGHT_INTENSITY_CHAR;
00607             light_intensity_val.val_len       = LAMP_CONTROL_MIN_SIZE;
00608             memcpy(&light_intensity_val.val[0], &fota_normal, fota_normal_len);
00609             
00610             msg = new uint8_t[msg_size];
00611             
00612             BMH->BleMsgAlloc(LAMP_CONTROL_SET_CHAR_VAL_REQ,TASK_LAMP_CONTROL, TASK_GTL,sizeof(struct lamp_control_set_char_val_req)-(4-LAMP_CONTROL_MIN_SIZE),&light_intensity_val,msg );
00613             // Send the message
00614             BMH->BleSendMsg(msg,msg_size);
00615             free(msg);
00616         }
00617         {
00618             uint8_t *msg;
00619             struct lamp_control_set_char_val_req lamp_timer_val;
00620             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(lamp_control_set_char_val_req)-(4-LAMP_CONTROL_MIN_SIZE);
00621             // Fill in the parameter structure
00622             lamp_timer_val.char_code     = LAMP_CONTROL_LAMP_TIMER_CHAR;
00623             lamp_timer_val.val_len       = LAMP_CONTROL_MIN_SIZE;
00624             memcpy(&lamp_timer_val.val[0], &fota_normal, fota_normal_len);
00625             
00626             msg = new uint8_t[msg_size];
00627             
00628             BMH->BleMsgAlloc(LAMP_CONTROL_SET_CHAR_VAL_REQ,TASK_LAMP_CONTROL, TASK_GTL,sizeof(struct lamp_control_set_char_val_req)-(4-LAMP_CONTROL_MIN_SIZE),&lamp_timer_val,msg );
00629             // Send the message
00630             BMH->BleSendMsg(msg,msg_size);
00631             free(msg);
00632         }
00633         {
00634             uint8_t *msg;
00635             struct lamp_control_set_char_val_req residual_time_val;
00636             unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(lamp_control_set_char_val_req)-(4-LAMP_CONTROL_MAX_SIZE);
00637             // Fill in the parameter structure
00638             residual_time_val.char_code     = LAMP_CONTROL_RESIDUAL_TIME_CHAR;
00639             residual_time_val.val_len       = LAMP_CONTROL_MAX_SIZE;
00640             memcpy(&residual_time_val.val[0], &fota_normal, fota_normal_len*4);
00641             
00642             msg = new uint8_t[msg_size];
00643             
00644             BMH->BleMsgAlloc(LAMP_CONTROL_SET_CHAR_VAL_REQ,TASK_LAMP_CONTROL, TASK_GTL,sizeof(struct lamp_control_set_char_val_req)-(4-FAN_CONTROL_MAX_SIZE),&residual_time_val,msg );
00645             // Send the message
00646             BMH->BleSendMsg(msg,msg_size);
00647             free(msg);
00648         }
00649     }
00650     if (app_env.state == APP_IDLE)
00651     {
00652         app_set_mode(BMH);
00653     }
00654 }
00655 
00656 /**
00657  ****************************************************************************************
00658  * @brief Handles the LAMP_CONTROL_COMMAND_IND message.
00659  *
00660  * @param[in] msgid     Id of the message received.
00661  * @param[in] param     Pointer to the parameters of the message.
00662  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00663  * @param[in] src_id    ID of the sending task instance.
00664  * @param[in] BMH     Ble Message Handler class reference.
00665  *
00666  * @return If the message was consumed or not.
00667  ****************************************************************************************
00668  */
00669 void lamp_control_command_ind_handler(unsigned short msgid,
00670                                   struct lamp_control_command_ind *param,
00671                                   unsigned short dest_id,
00672                                   unsigned short src_id,
00673                                   BleMsgHandler* BMH)
00674 {
00675     char str[30] = "\nLAMP remote control START!!\n";
00676     BMH->HostPcPrint(str);
00677     if( param->char_code == LAMP_CONTROL_POWER_STATE_CHAR){
00678         if(param->command == 0){
00679             Timer_flag = 0;
00680             Residual_Time = 0;
00681             port21 = 0;
00682             port22 = 0;
00683         }
00684         if(param->command == 1){
00685             if( port21 == 0 && port22 == 0 )
00686                 port21 = 1;
00687         }
00688     }else if( param->char_code == LAMP_CONTROL_LIGHT_INTENSITY_CHAR){
00689         if( param->command == 1 ){ //low
00690             port21 = 1;
00691             port22 = 0;
00692         }
00693         else if( param->command == 2 ){ //mid
00694             port21 = 0;
00695             port22 = 1;
00696         } 
00697     }else if( param->char_code == LAMP_CONTROL_LAMP_TIMER_CHAR){
00698         Timer_flag = param->command;
00699     }else if( param->char_code == LAMP_CONTROL_RESIDUAL_TIME_CHAR){
00700         Residual_Time = param->residual_time;
00701         {
00702                 uint8_t fota_normal = 0;
00703                 uint8_t *msg;
00704                 struct lamp_control_set_char_val_req char_set_req;
00705                 unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(lamp_control_set_char_val_req)-(4-LAMP_CONTROL_MIN_SIZE);
00706                 // Fill in the parameter structure
00707                 char_set_req.char_code     = LAMP_CONTROL_POWER_STATE_CHAR;
00708                 char_set_req.val_len       = LAMP_CONTROL_MIN_SIZE;
00709                 memcpy(&char_set_req.val[0], &fota_normal, sizeof(fota_normal));
00710                 
00711                 msg = new uint8_t[msg_size];
00712                 
00713                 BMH->BleMsgAlloc(LAMP_CONTROL_SET_CHAR_VAL_REQ
00714                     ,TASK_LAMP_CONTROL, TASK_GTL
00715                     ,sizeof(struct fan_control_set_char_val_req)-(4-LAMP_CONTROL_MIN_SIZE)
00716                     ,&char_set_req,msg );
00717                 // Send the message
00718                 BMH->BleSendMsg(msg,msg_size);                
00719                 free(msg);
00720         }
00721     }
00722 }
00723 /**
00724  ****************************************************************************************
00725  * @brief Handles ready indication from the GAP.
00726  *
00727  * @param[in] msgid     Id of the message received.
00728  * @param[in] param     Pointer to the parameters of the message.
00729  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00730  * @param[in] src_id    ID of the sending task instance.
00731  * @param[in] BMH     Ble Message Handler class reference.
00732  *
00733  * @return If the message was consumed or not.
00734  ****************************************************************************************
00735  */
00736 int gapm_device_ready_ind_handler(unsigned short msgid,
00737                                   struct gap_ready_evt *param,
00738                                   unsigned short dest_id,
00739                                   unsigned short src_id,
00740                                   BleMsgHandler* BMH)
00741 {
00742     // We are now in Connectable State
00743     if (dest_id == TASK_GTL)
00744     {
00745         app_rst_gap(BMH);
00746     }
00747     
00748     return 0;
00749 }
00750 /**
00751  ****************************************************************************************
00752  * @brief Handle reset GAP request completion event.
00753  *
00754  * @param[in] msgid     Id of the message received.
00755  * @param[in] param     Pointer to the parameters of the message.
00756  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00757  * @param[in] src_id    ID of the sending task instance.
00758  * @param[in] BMH     Ble Message Handler class reference.
00759  *
00760  * @return If the message was consumed or not.
00761  ****************************************************************************************
00762  */ 
00763 int gapm_reset_completion_handler(unsigned short msgid,
00764                                   struct gapm_cmp_evt *param,
00765                                   unsigned short dest_id,
00766                                   unsigned short src_id,
00767                                   BleMsgHandler* BMH)
00768 {
00769     // We are now in Connectable State
00770     if (dest_id == TASK_GTL)
00771     {
00772         app_env.state = APP_IDLE;           
00773         app_diss_db_create(BMH);
00774     }
00775     
00776     return 0;
00777 }
00778 /**
00779  ****************************************************************************************
00780  * @brief Handles GAPM_CMP_EVT event for GAPM_SET_DEV_CONFIG_CMD.
00781  *
00782  * @param[in] msgid     Id of the message received.
00783  * @param[in] param     Pointer to the parameters of the message.
00784  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00785  * @param[in] src_id    ID of the sending task instance.
00786  * @param[in] BMH     Ble Message Handler class reference.
00787  *
00788  * @return If the message was consumed or not.
00789  ****************************************************************************************
00790  */
00791 int gapm_set_dev_config_completion_handler(unsigned short msgid,
00792                                            struct gapm_cmp_evt *param,
00793                                            unsigned short dest_id,
00794                                            unsigned short src_id,
00795                                            BleMsgHandler* BMH)
00796 {
00797     app_env.state = APP_CONNECTABLE;
00798     wait(1);
00799     app_adv_start(BMH); // start advertising
00800 
00801     return 0;
00802 }
00803 /**
00804  ****************************************************************************************
00805  * @brief Handles Connection request indication event.
00806  *
00807  * @param[in] msgid     Id of the message received.
00808  * @param[in] param     Pointer to the parameters of the message.
00809  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00810  * @param[in] src_id    ID of the sending task instance.
00811  * @param[in] BMH     Ble Message Handler class reference.
00812  *
00813  * @return If the message was consumed or not.
00814  ****************************************************************************************
00815  */
00816 int gapc_connection_req_ind_handler(uint16_t msgid,
00817                                     struct gapc_connection_req_ind *param,
00818                                     uint16_t dest_id,
00819                                     uint16_t src_id,
00820                                     BleMsgHandler* BMH)
00821 {
00822     app_env.state = APP_CONNECTED;
00823     
00824     ble_dev device;
00825     
00826     // Retrieve the connection index from the GAPC task instance for the connection
00827     device.conidx = KE_IDX_GET(src_id);
00828         
00829     // Retrieve the connection handle from the parameters
00830     device.conhdl = param->conhdl;
00831 
00832     memcpy(device.adv_addr.addr, param->peer_addr.addr, sizeof(struct bd_addr));
00833      
00834     
00835     char str[30] = "\nPROFILE ENABLE and CONFIRM\n";
00836     BMH->HostPcPrint(str);
00837     
00838     app_dis_enable(&device,BMH);
00839     app_fota_server_enable(&device,BMH);
00840     //app_fan_control_enable(&device,BMH);
00841     app_lamp_control_enable(&device,BMH);
00842     app_connect_confirm(GAP_AUTH_REQ_NO_MITM_NO_BOND,&device,BMH);
00843    
00844     return 0;
00845 }
00846 /**
00847  ****************************************************************************************
00848  * @brief Handles Discconnection indication event.
00849  *
00850  * @param[in] msgid     Id of the message received.
00851  * @param[in] param     Pointer to the parameters of the message.
00852  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
00853  * @param[in] src_id    ID of the sending task instance.
00854  * @param[in] BMH     Ble Message Handler class reference.
00855  *
00856  * @return If the message was consumed or not.
00857  ****************************************************************************************
00858  */
00859 int gapc_disconnect_ind_handler(uint16_t msgid,
00860                                 struct gapc_disconnect_ind *param,
00861                                 uint16_t dest_id,
00862                                 uint16_t src_id,
00863                                 BleMsgHandler* BMH)
00864 {
00865     char str[22] = "\nDevice Disconnected\n";
00866     BMH->HostPcPrint(str);
00867     
00868     app_send_disconnect(TASK_FOTA_SERVER, param->conhdl, param->reason, BMH);  
00869     app_env.state = APP_IDLE;
00870     
00871     wait(1);
00872     app_set_mode(BMH);
00873     
00874     return 0;
00875 }
00876 
00877 
00878 }//namespace
00879 
00880 /// @} dialog_fota module