BLE fota Lamp Demo

Dependencies:   mbed-rtos mbed mbed_fota_fan_control

Fork of mbed_fota_fan_control by KIM HyoengJun

ext_fota/BleMsgHandler.cpp

Committer:
dudnwjs
Date:
2015-06-24
Revision:
4:60be78a172c2
Parent:
3:1e70387e1337
Child:
5:e11b23f9aacc

File content as of revision 4:60be78a172c2:

#include "BleMsgHandler.h"
#include "dialog_fota_config.h"
#include "diss_task.h"
#include "app_task.h"


namespace sevencore_fota{

BleMsgHandler::BleMsgHandler(Serial *_device)
{
    print_flag = 0;
    device = _device;
    SerialM = new SerialManager(_device);
    MsgQ = new MsgQueue(512);
    memset(recv_msg,0,512);
}

BleMsgHandler::BleMsgHandler(Serial *_device, Serial *_hostpc)
{
    print_flag = 1;
    device = _device;
    hostpc = _hostpc;
    SerialM = new SerialManager(_device,_hostpc);
    MsgQ = new MsgQueue(512);
    memset(recv_msg,0,512);
}

BleMsgHandler::~BleMsgHandler(void)
{
    free(SerialM);
    free(MsgQ);
}

void BleMsgHandler::PrintTitle(void)
{
    if( print_flag == 1 )
        hostpc->printf("Sevencore Fota : BleMsg Handler Start\n");
}

void BleMsgHandler::BleMsgAlloc( unsigned short id,
                                 unsigned short dest_id,
                                 unsigned short src_id,
                                 unsigned short data_len,
                                 void *pdata,
                                 uint8_t *msg )
{
    memset(msg,0,sizeof(msg));
    msg[0] = 0x05;
    memcpy(&msg[1],&id,sizeof(unsigned short));
    memcpy(&msg[1+1*sizeof(unsigned short)],&dest_id,sizeof(unsigned short));
    memcpy(&msg[1+2*sizeof(unsigned short)],&src_id,sizeof(unsigned short));
    memcpy(&msg[1+3*sizeof(unsigned short)],&data_len,sizeof(unsigned short));
    memcpy(&msg[1+4*sizeof(unsigned short)],pdata,data_len);
}

int BleMsgHandler::BleSendMsg(uint8_t *msg, unsigned short size)
{
   return  SerialM->SendToSerial(msg,size);
}

void BleMsgHandler::BleReceiveMsg(void)
{
    int receive_size = -1;//default
    while(receive_size == -1)
    {
        receive_size = SerialM->ReceiveToSerial(recv_msg);
    }
    
    uint8_t *msg;
    msg = new uint8_t[receive_size];
    memcpy(msg,recv_msg,receive_size);
    memset(recv_msg,0,512);
    MsgQ->EnQueue(msg);
}

void BleMsgHandler::BleMsgHandle(void)
{
    uint8_t *msg;
    ble_hdr msg_hdr;
    unsigned short paramPos = 1 + sizeof(msg_hdr);
    
    if( print_flag == 1)
        hostpc->printf("Ble-Massage Handle Function!\n");
    
    msg = (uint8_t*)MsgQ->DeQueue();
    memcpy(&msg_hdr, &msg[1], sizeof(msg_hdr));
    
    if( print_flag == 1 )
        hostpc->printf(" handle msg : id(%d), dst(%d), src(%d), len(%d) !\n",
         msg_hdr.bType, msg_hdr.bDstid, msg_hdr.bSrcid, msg_hdr.bLength);
    
    if (msg_hdr.bDstid != TASK_GTL)
        return;
    
    switch( msg_hdr.bType )
    {
        case GAPM_CMP_EVT:
            HandleGapmCmpEvt(msg_hdr.bType,(struct gapm_cmp_evt *)&msg[paramPos],msg_hdr.bDstid,msg_hdr.bSrcid);
            break;
        case GAPM_DEVICE_READY_IND:
            gapm_device_ready_ind_handler(msg_hdr.bType,(void *)&msg[paramPos],msg_hdr.bDstid,msg_hdr.bSrcid,this);
            break;
        case GAPM_ADV_REPORT_IND:
            gapm_adv_report_ind_handler(msg_hdr.bType,(struct gapm_adv_report_ind *)&msg[paramPos],msg_hdr.bDstid,msg_hdr.bSrcid);
            break;
        case GAPC_CMP_EVT:
            HandleGapcCmpEvt(msg_hdr.bType,(struct gapc_cmp_evt *)&msg[paramPos],msg_hdr.bDstid,msg_hdr.bSrcid);
            break;
        case DISS_CREATE_DB_CFM:
            diss_create_db_cfm_handler(msg_hdr.bType,(struct diss_create_db_cfm *)&msg[paramPos],msg_hdr.bDstid,msg_hdr.bSrcid,this);
            break;
        case DISS_DISABLE_IND:
            break;
        case DISS_ERROR_IND:
            if ( print_flag == 1)
                hostpc->printf("Rcved DISS_ERROR_IND Msg\n");
            break;
        default:
            if( print_flag == 1)
                hostpc->printf("Massage Type Not Defined ! \n");
            break;
    }
}

void BleMsgHandler::HandleGapmCmpEvt(unsigned short msgid,
                    struct gapm_cmp_evt *param,
                    unsigned short dest_id,
                    unsigned short src_id)
{
    if (param->status == CO_ERROR_NO_ERROR)
    {
        switch(param->operation)
        {
            case GAPM_NO_OP:// No operation.
                break;
            case GAPM_RESET:// Reset BLE subsystem: LL and HL.
                gapm_reset_completion_handler (msgid, (struct gapm_cmp_evt *)param, dest_id, src_id,this);
                break;
            case GAPM_CANCEL:// Cancel currently executed operation.
                break;
            case GAPM_SET_DEV_CONFIG:// Set device configuration
                if( print_flag == 1)
                    hostpc->printf("Adverting Start...\n");
                gapm_set_dev_config_completion_handler(msgid, (struct gapm_cmp_evt *)param, dest_id, src_id,this);
                break;
            case GAPM_SET_DEV_NAME: // Set device name
                break;
            case GAPM_SET_CHANNEL_MAP:// Set device channel map
                break;
            case  GAPM_GET_DEV_NAME:// Get Local device name
                break;
            case GAPM_GET_DEV_VERSION:// Get Local device version
                break;
            case GAPM_GET_DEV_BDADDR:// Get Local device BD Address
                break;
            case GAPM_GET_WLIST_SIZE:// Get White List Size.
                break;
            case GAPM_ADD_DEV_IN_WLIST:// Add devices in white list.
                break;
            case GAPM_RMV_DEV_FRM_WLIST:// Remove devices form white list.
                break;
            case GAPM_CLEAR_WLIST:// Clear all devices from white list.
                break;
            case GAPM_ADV_NON_CONN:// Start non connectable advertising
            case GAPM_ADV_UNDIRECT:// Start undirected connectable advertising
            case GAPM_ADV_DIRECT:// Start directed connectable advertising
                    break;
            case GAPM_SCAN_ACTIVE:// Start active scan operation
            case GAPM_SCAN_PASSIVE:   // Start passive scan operation
                break;
            case GAPM_CONNECTION_DIRECT:// Direct connection operation
                break;
            case GAPM_CONNECTION_AUTO:// Automatic connection operation
                break;
            case GAPM_CONNECTION_SELECTIVE:// Selective connection operation
                break;
            case GAPM_CONNECTION_NAME_REQUEST:// Name Request operation (requires to start a direct connection)
                break;
            case GAPM_RESOLV_ADDR:// Resolve device address
                break;
            case GAPM_GEN_RAND_ADDR:// Generate a random address
                break;
            case GAPM_USE_ENC_BLOCK:// Use the controller's AES-128 block
                break;
            case GAPM_GEN_RAND_NB:// Generate a 8-byte random number
                break;
            case GAPM_DBG_GET_MEM_INFO:// Get memory usage
                break;
            case GAPM_PLF_RESET:// Perform a platform reset
                break;
            case GAPM_GET_DEV_ADV_TX_POWER:// Get device advertising power level
                break;
        }
    } 
}

void BleMsgHandler::HandleGapcCmpEvt(unsigned short msgid,
                    struct gapc_cmp_evt *param,
                    unsigned short dest_id,
                    unsigned short src_id)
{
     switch(param->operation)
    {
        case GAPC_NO_OP: // No operation
            break;
        case GAPC_DISCONNECT: // Disconnect link
            break;
        case GAPC_GET_PEER_NAME: // Retrieve name of peer device
            break; 
        case GAPC_GET_PEER_VERSION: // Retrieve peer device version info.
            break; 
        case GAPC_GET_PEER_FEATURES: // Retrieve peer device features.
            break; 
        case GAPC_GET_CON_RSSI: // Retrieve connection RSSI.
            break; 
        case GAPC_GET_PRIVACY: // Retrieve Privacy Info.
            break; 
        case GAPC_GET_RECON_ADDR: // Retrieve Reconnection Address Value.
            break; 
        case GAPC_SET_PRIVACY: // Set Privacy flag.
            break; 
        case GAPC_SET_RECON_ADDR: // Set Reconnection Address Value.
            break; 
        case GAPC_UPDATE_PARAMS: // Perform update of connection parameters.
            break; 
        case GAPC_BOND: // Start bonding procedure.
            break; 
        case GAPC_ENCRYPT: // Start encryption procedure.
            break; 
        case GAPC_SECURITY_REQ: // Start security request procedure
            break; 
        case GAPC_GET_CON_CHANNEL_MAP: // Retrieve Connection Channel MAP.
            break;
    }
}


 
}//namespace