Fan Control Demo

Dependencies:   mbed-rtos mbed

Fork of mbed_fota by KIM HyoengJun

Revision:
12:e3d3676753cc
Parent:
11:1ed93accb3fb
--- a/dialog_fota/app_task.cpp	Mon Aug 24 03:24:19 2015 +0000
+++ b/dialog_fota/app_task.cpp	Wed Sep 02 07:44:29 2015 +0000
@@ -10,6 +10,14 @@
 #include "app_task.h"
 #include "app.h"
 #include "fota_server_task.h"
+#include "fan_control_task.h"
+#include "mbed.h"
+
+extern DigitalOut port18;//mid
+extern DigitalOut port19;//low
+extern DigitalOut port20;//high
+extern uint8_t Timer_flag;
+extern uint32_t Residual_Time;
 
 #define BLE_FOTA_SERVICE 1
 /**
@@ -22,6 +30,7 @@
  */
 namespace sevencore_fota{
  
+ 
 /**
  ****************************************************************************************
  * @brief Handles GAPM_ADV_REPORT_IND event.
@@ -324,7 +333,8 @@
     
     if (app_env.state == APP_IDLE)
     {
-        app_set_mode(BMH);
+        //app_set_mode(BMH);
+        app_fan_control_db_create(BMH);
     }
     
     return 0;
@@ -351,8 +361,198 @@
     char version[9];
     memcpy(version,param->version,8);
     version[8] = '\0';
+    port18 = 1;
     BMH->FirmwareDataReceive(param->code_size,version);
 }
+
+/**
+ ****************************************************************************************
+ * @brief Handles the FAN_CONTROL_CREATE_DB_CFM message.
+ *
+ * @param[in] msgid     Id of the message received.
+ * @param[in] param     Pointer to the parameters of the message.
+ * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
+ * @param[in] src_id    ID of the sending task instance.
+ * @param[in] BMH     Ble Message Handler class reference.
+ *
+ * @return If the message was consumed or not.
+ ****************************************************************************************
+ */
+void fan_control_create_db_cfm_handler(unsigned short msgid,
+                                  struct fan_control_create_db_cfm *param,
+                                  unsigned short dest_id,
+                                  unsigned short src_id,
+                                  BleMsgHandler* BMH)
+{
+    uint8_t fota_normal = 0;
+    uint8_t fota_normal_len = sizeof(fota_normal);
+    if (param->status == CO_ERROR_NO_ERROR)
+    {
+        // Initialization All Characteristic
+        {
+            uint8_t *msg;
+            struct fan_control_set_char_val_req power_state_val;
+            unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
+            // Fill in the parameter structure
+            power_state_val.char_code     = FAN_CONTROL_POWER_STATE_CHAR;
+            power_state_val.val_len       = FAN_CONTROL_MIN_SIZE;
+            memcpy(&power_state_val.val[0], &fota_normal, fota_normal_len);
+            
+            msg = new uint8_t[msg_size];
+            
+            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 );
+            // Send the message
+            BMH->BleSendMsg(msg,msg_size);
+            free(msg);
+        }
+        {
+            uint8_t *msg;
+            struct fan_control_set_char_val_req wind_strength_val;
+            unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
+            // Fill in the parameter structure
+            wind_strength_val.char_code     = FAN_CONTROL_WIND_STRENGTH_CHAR;
+            wind_strength_val.val_len       = FAN_CONTROL_MIN_SIZE;
+            memcpy(&wind_strength_val.val[0], &fota_normal, fota_normal_len);
+            
+            msg = new uint8_t[msg_size];
+            
+            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 );
+            // Send the message
+            BMH->BleSendMsg(msg,msg_size);
+            free(msg);
+        }
+        {
+            uint8_t *msg;
+            struct fan_control_set_char_val_req fan_spin_val;
+            unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
+            // Fill in the parameter structure
+            fan_spin_val.char_code     = FAN_CONTROL_FAN_SPIN_CHAR;
+            fan_spin_val.val_len       = FAN_CONTROL_MIN_SIZE;
+            memcpy(&fan_spin_val.val[0], &fota_normal, fota_normal_len);
+            
+            msg = new uint8_t[msg_size];
+            
+            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 );
+            // Send the message
+            BMH->BleSendMsg(msg,msg_size);
+            free(msg);
+        }
+        {
+            uint8_t *msg;
+            struct fan_control_set_char_val_req fan_timer_val;
+            unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
+            // Fill in the parameter structure
+            fan_timer_val.char_code     = FAN_CONTROL_FAN_TIMER_CHAR;
+            fan_timer_val.val_len       = FAN_CONTROL_MIN_SIZE;
+            memcpy(&fan_timer_val.val[0], &fota_normal, fota_normal_len);
+            
+            msg = new uint8_t[msg_size];
+            
+            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 );
+            // Send the message
+            BMH->BleSendMsg(msg,msg_size);
+            free(msg);
+        }
+        {
+            uint8_t *msg;
+            struct fan_control_set_char_val_req residual_time_val;
+            unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MAX_SIZE);
+            // Fill in the parameter structure
+            residual_time_val.char_code     = FAN_CONTROL_RESIDUAL_TIME_CHAR;
+            residual_time_val.val_len       = FAN_CONTROL_MAX_SIZE;
+            memcpy(&residual_time_val.val[0], &fota_normal, fota_normal_len*4);
+            
+            msg = new uint8_t[msg_size];
+            
+            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 );
+            // Send the message
+            BMH->BleSendMsg(msg,msg_size);
+            free(msg);
+        }
+    }
+    if (app_env.state == APP_IDLE)
+    {
+        app_set_mode(BMH);
+    }
+}
+
+/**
+ ****************************************************************************************
+ * @brief Handles the FAN_CONTROL_COMMAND_IND message.
+ *
+ * @param[in] msgid     Id of the message received.
+ * @param[in] param     Pointer to the parameters of the message.
+ * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
+ * @param[in] src_id    ID of the sending task instance.
+ * @param[in] BMH     Ble Message Handler class reference.
+ *
+ * @return If the message was consumed or not.
+ ****************************************************************************************
+ */
+void fan_control_command_ind_handler(unsigned short msgid,
+                                  struct fan_control_command_ind *param,
+                                  unsigned short dest_id,
+                                  unsigned short src_id,
+                                  BleMsgHandler* BMH)
+{
+    char str[30] = "\nFAN remote control START!!\n";
+    BMH->HostPcPrint(str);
+    if( param->char_code == FAN_CONTROL_POWER_STATE_CHAR){
+        if(param->command == 0){
+            Timer_flag = 0;
+            Residual_Time = 0;
+            port18 = 0;
+            port19 = 0;
+            port20 = 0;
+        }
+        if(param->command == 1){
+            if( port18 == 0 && port19 == 0 && port20 == 0 )
+                port19 = 1;
+        }
+    }else if( param->char_code == FAN_CONTROL_WIND_STRENGTH_CHAR){
+        if( param->command == 1 ){ //low
+            port18 = 0;
+            port20 = 0;
+            port19 = 1;
+        }
+        else if( param->command == 2 ){ //mid
+            port19 = 0;
+            port20 = 0;
+            port18 = 1;
+        }else if( param->command == 3 ){ //high
+            port18 = 0;
+            port19 = 0;
+            port20 = 1;
+        }
+    }else if( param->char_code == FAN_CONTROL_FAN_SPIN_CHAR){
+        
+    }else if( param->char_code == FAN_CONTROL_FAN_TIMER_CHAR){
+        Timer_flag = param->command;
+    }else if( param->char_code == FAN_CONTROL_RESIDUAL_TIME_CHAR){
+        Residual_Time = param->residual_time;
+        {
+                uint8_t fota_normal = 0;
+                uint8_t *msg;
+                struct fan_control_set_char_val_req char_set_req;
+                unsigned short msg_size = 1+sizeof(ble_hdr)+ sizeof(fan_control_set_char_val_req)-(4-FAN_CONTROL_MIN_SIZE);
+                // Fill in the parameter structure
+                char_set_req.char_code     = FAN_CONTROL_POWER_STATE_CHAR;
+                char_set_req.val_len       = FAN_CONTROL_MIN_SIZE;
+                memcpy(&char_set_req.val[0], &fota_normal, sizeof(fota_normal));
+                
+                msg = new uint8_t[msg_size];
+                
+                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)
+                    ,&char_set_req,msg );
+                // Send the message
+                BMH->BleSendMsg(msg,msg_size);                
+                free(msg);
+        }
+    }
+}
+
 /**
  ****************************************************************************************
  * @brief Handles ready indication from the GAP.
@@ -465,11 +665,12 @@
     memcpy(device.adv_addr.addr, param->peer_addr.addr, sizeof(struct bd_addr));
      
     
-    char str[30] = "\nDIS&FOTA ENABLE and CONFIRM\n";
+    char str[30] = "\nPROFILE ENABLE and CONFIRM\n";
     BMH->HostPcPrint(str);
     
     app_dis_enable(&device,BMH);
     app_fota_server_enable(&device,BMH);
+    app_fan_control_enable(&device,BMH);
     app_connect_confirm(GAP_AUTH_REQ_NO_MITM_NO_BOND,&device,BMH);
    
     return 0;