Пример #1
0
/*************************************************
* Function: PCT_HandleEvent
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleEvent(PTC_ProtocolCon *pstruContoller)
{
    MSG_Buffer *pstruBuffer;
    ZC_MessageHead *pstruMsg;
    
    if (PCT_TIMER_INVAILD != pstruContoller->u8SendMoudleTimer)
    {
        return;
    }
    
    pstruBuffer = (MSG_Buffer *)MSG_PopMsg(&g_struRecvQueue);
    if (NULL == pstruBuffer)
    {
        return;
    }
    
    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    ZC_Printf("event %d recv len =%d\n", pstruMsg->MsgId, ZC_HTONS(pstruMsg->Payloadlen) + sizeof(ZC_MessageHead));
    ZC_TraceData((u8*)pstruMsg, ZC_HTONS(pstruMsg->Payloadlen) + sizeof(ZC_MessageHead));
    if (ZC_RET_ERROR == PCT_CheckCrc(pstruMsg->TotalMsgCrc, (u8*)(pstruMsg + 1), ZC_HTONS(pstruMsg->Payloadlen)))
    {
        PCT_SendEmptyMsg(pstruMsg->MsgId, ZC_SEC_ALG_AES);
        PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0);

        pstruBuffer->u32Len = 0;
        pstruBuffer->u8Status = MSG_BUFFER_IDLE;
        
        return;
    }
    
    /*when do OTA, does not send empty*/
    switch (pstruMsg->MsgCode)
    {
        case ZC_CODE_ZOTA_BEGIN:
            PCT_HandleOtaBeginMsg(pstruContoller, pstruBuffer);
            break;   
        case ZC_CODE_ZOTA_FILE_BEGIN:
            PCT_HandleOtaFileBeginMsg(pstruContoller, pstruBuffer);
            break;
        case ZC_CODE_ZOTA_FILE_CHUNK:
            PCT_HandleOtaFileChunkMsg(pstruContoller, pstruBuffer);
            break;
        case ZC_CODE_ZOTA_FILE_END:
            PCT_HandleOtaFileEndMsg(pstruContoller, pstruBuffer);
            break;  
        case ZC_CODE_ZOTA_END:
            PCT_HandleOtaEndMsg(pstruContoller, pstruBuffer);
            break; 
        case ZC_CODE_OTA_BEGIN:
        case ZC_CODE_OTA_FILE_BEGIN:     
        case ZC_CODE_OTA_FILE_CHUNK:
        case ZC_CODE_OTA_FILE_END:
        case ZC_CODE_OTA_END:
            PCT_HandleMoudleMsg(pstruContoller, pstruBuffer);
            break;
        case ZC_CODE_TOKEN_SET:
            PCT_SetTokenKey(pstruContoller, pstruBuffer);
            break;
        default:
            PCT_HandleMoudleMsg(pstruContoller, pstruBuffer);
            PCT_SendEmptyMsg(pstruMsg->MsgId, ZC_SEC_ALG_AES);
            break;                                    
    }

    pstruBuffer->u32Len = 0;
    pstruBuffer->u8Status = MSG_BUFFER_IDLE;

    
    return;
} 
/*************************************************
* Function: PCT_HandleEvent
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleEvent(PTC_ProtocolCon *pstruContoller)
{
    MSG_Buffer *pstruBuffer;
    ZC_MessageHead *pstruMsg;
    //u32 u32i;
	if (ZC_MAGIC_FLAG == g_struZcConfigDb.struDeviceInfo.u32UnBindFlag)
	{
        g_struProtocolController.u8MainState = PCT_STATE_WAIT_UNBIND;
        if (PCT_TIMER_INVAILD != pstruContoller->u8SendUnbindTimer)
        {
            TIMER_StopTimer(pstruContoller->u8SendUnbindTimer);
        }
        PCT_SendUnbindMsg();
        return;
    }
    pstruBuffer = (MSG_Buffer *)MSG_PopMsg(&g_struRecvQueue);
    if (NULL == pstruBuffer)
    {
        return;
    }
    
    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    ZC_Printf("event %d recv len =%d\n", pstruMsg->MsgId, ZC_HTONS(pstruMsg->Payloadlen) + sizeof(ZC_MessageHead));
#if 0
    for (u32i = 0; u32i < ZC_HTONS(pstruMsg->Payloadlen) + sizeof(ZC_MessageHead); u32i++)
        ZC_Printf("data[%d] = 0x%02x\n", u32i, pstruBuffer->u8MsgBuffer[u32i]);
#endif
    ZC_TraceData((u8*)pstruMsg, ZC_HTONS(pstruMsg->Payloadlen) + sizeof(ZC_MessageHead));
    if (ZC_RET_ERROR == PCT_CheckCrc(pstruMsg->TotalMsgCrc, (u8*)(pstruMsg + 1), ZC_HTONS(pstruMsg->Payloadlen)))
    {
        PCT_SendEmptyMsg(pstruMsg->MsgId, ZC_SEC_ALG_AES);
        PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0);

        pstruBuffer->u32Len = 0;
        pstruBuffer->u8Status = MSG_BUFFER_IDLE;
        
        return;
    }
				
    /*when do OTA, does not send empty*/
    switch (pstruMsg->MsgCode)
    {
        case ZC_CODE_ZOTA_BEGIN:
            PCT_HandleOtaBeginMsg(pstruContoller, pstruBuffer);
            break;   
        case ZC_CODE_ZOTA_FILE_BEGIN:
            PCT_HandleOtaFileBeginMsg(pstruContoller, pstruBuffer);
            break;
        case ZC_CODE_ZOTA_FILE_CHUNK:
            PCT_HandleOtaFileChunkMsg(pstruContoller, pstruBuffer);
            break;
        case ZC_CODE_ZOTA_FILE_END:
            PCT_HandleOtaFileEndMsg(pstruContoller, pstruBuffer);
            break;  
        case ZC_CODE_ZOTA_END:
            PCT_HandleOtaEndMsg(pstruContoller, pstruBuffer);
            break; 
#if 0
        case ZC_CODE_OTA_BEGIN:
        case ZC_CODE_OTA_FILE_BEGIN:     
        case ZC_CODE_OTA_FILE_CHUNK:
        case ZC_CODE_OTA_FILE_END:
        case ZC_CODE_OTA_END:
        case ZC_CODE_OTA_CONFIRM:
            PCT_HandleMoudleMsg(pstruContoller, pstruBuffer);
            break;
#endif
        case ZC_CODE_TOKEN_SET:
            PCT_SetTokenKey(pstruContoller, pstruBuffer);
            break;
        case ZC_CODE_RESET_NETWORK:
            PCT_ResetNetWork(pstruContoller, pstruBuffer);
            break;
        case ZC_CODE_UNBIND:
            break;
        default:
            PCT_HandleMoudleMsg(pstruContoller, pstruBuffer);

            break;                                    
    }

    pstruBuffer->u32Len = 0;
    pstruBuffer->u8Status = MSG_BUFFER_IDLE;

    
    return;
} 
Пример #3
0
/*************************************************
* Function: ZC_RecvDataFromClient
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void ZC_RecvDataFromClient(u32 ClientId, u8 *pu8Data, u32 u32DataLen)
{
    u32 u32RetVal;
    ZC_MessageHead *pstruMsg;
    ZC_MessageOptHead struOpt;
    ZC_SsessionInfo struSessionMsg;
    ZC_SecHead *pstruHead;
    u16 u16Len;
    u32 u32CiperLen;
    u8 *pu8Key;
    ZC_SendParam struParam;
    u8 u8Iv[ZC_HS_SESSION_KEY_LEN];
    u16 crc;

    /*can hanle it,get aes key*/
    
    ZC_GetStoreInfor(ZC_GET_TYPE_TOKENKEY, &pu8Key);

    u32RetVal = ZC_CheckClientIdle(ClientId);
    if (ZC_RET_ERROR == u32RetVal)
    {
        pstruHead = (ZC_SecHead*)(g_u8MsgBuildBuffer);
        
        EVENT_BuildMsg(ZC_CODE_ERR, 0, g_u8MsgBuildBuffer + sizeof(ZC_SecHead), &u16Len, 
            NULL, 0);
        memcpy(u8Iv, pu8Key, ZC_HS_SESSION_KEY_LEN);
        AES_CBC_Encrypt(g_u8MsgBuildBuffer + sizeof(ZC_SecHead), u16Len,
            pu8Key, ZC_HS_SESSION_KEY_LEN,
            u8Iv, ZC_HS_SESSION_KEY_LEN,
            g_u8MsgBuildBuffer + sizeof(ZC_SecHead), &u32CiperLen);

        pstruHead->u8SecType = ZC_SEC_ALG_AES;
        pstruHead->u16TotalMsg = ZC_HTONS((u16)u32CiperLen);

        struParam.u8NeedPoll = 0;            
        g_struProtocolController.pstruMoudleFun->pfunSendTcpData(ClientId, g_u8MsgBuildBuffer, 
            u32CiperLen + sizeof(ZC_SecHead), &struParam);
        return;            
    }
    
    /*set client busy*/
    ZC_SetClientBusy(ClientId);
    
    u32RetVal = MSG_RecvData(&g_struClientBuffer, pu8Data, u32DataLen);
    if ((MSG_BUFFER_FULL == g_struClientBuffer.u8Status)&&(ZC_RET_OK == u32RetVal))
    {  
        do
        {
            pstruHead = (ZC_SecHead *)g_struClientBuffer.u8MsgBuffer;

            if (ZC_HTONS(pstruHead->u16TotalMsg) >= MSG_BULID_BUFFER_MAXLEN)
            {
                break;
            }
         
            memcpy(u8Iv, pu8Key, ZC_HS_SESSION_KEY_LEN);
            AES_CBC_Decrypt(g_struClientBuffer.u8MsgBuffer + sizeof(ZC_SecHead), ZC_HTONS(pstruHead->u16TotalMsg), 
                pu8Key, ZC_HS_SESSION_KEY_LEN, 
                u8Iv, ZC_HS_SESSION_KEY_LEN, 
                g_u8MsgBuildBuffer, &u32CiperLen);

            pstruMsg = (ZC_MessageHead*)(g_u8MsgBuildBuffer);
            if(ZC_RET_ERROR == PCT_CheckCrc(pstruMsg->TotalMsgCrc, (u8 *)(pstruMsg + 1), ZC_HTONS(pstruMsg->Payloadlen)))
            {
                break;
            }

            pstruMsg->Payloadlen = ZC_HTONS(ZC_HTONS(pstruMsg->Payloadlen) + sizeof(ZC_MessageOptHead) + sizeof(ZC_SsessionInfo));
            if (ZC_HTONS(pstruMsg->Payloadlen) > MSG_BULID_BUFFER_MAXLEN)
            {
                break;
            }

            pstruMsg->OptNum = pstruMsg->OptNum + 1;
            struOpt.OptCode = ZC_HTONS(ZC_OPT_SSESSION);
            struOpt.OptLen = ZC_HTONS(sizeof(ZC_SsessionInfo));
            struSessionMsg.u32SsessionId = ZC_HTONL(ClientId);

            u16Len = 0;
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, pstruMsg, sizeof(ZC_MessageHead));
 
            /*insert opt*/
            u16Len += sizeof(ZC_MessageHead);
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, 
                 &struOpt, sizeof(ZC_MessageOptHead));
            u16Len += sizeof(ZC_MessageOptHead);
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, 
                 &struSessionMsg, sizeof(struSessionMsg));
 
            /*copy message*/
            u16Len += sizeof(struSessionMsg);    
            memcpy(g_struClientBuffer.u8MsgBuffer + u16Len, 
                (u8*)(pstruMsg+1), ZC_HTONS(pstruMsg->Payloadlen) - (sizeof(ZC_MessageOptHead) + sizeof(struSessionMsg)));   
 
            u16Len += ZC_HTONS(pstruMsg->Payloadlen) - (sizeof(ZC_MessageOptHead) + sizeof(struSessionMsg));     
            g_struClientBuffer.u32Len = u16Len;

            crc = crc16_ccitt(g_struClientBuffer.u8MsgBuffer+sizeof(ZC_MessageHead),u16Len - sizeof(ZC_MessageHead));
            pstruMsg =  (ZC_MessageHead*)(g_struClientBuffer.u8MsgBuffer);
            pstruMsg->TotalMsgCrc[0]=(crc&0xff00)>>8;
            pstruMsg->TotalMsgCrc[1]=(crc&0xff);

            ZC_TraceData(g_struClientBuffer.u8MsgBuffer, g_struClientBuffer.u32Len);
            
            /*send to moudle*/
            g_struProtocolController.pstruMoudleFun->pfunSendToMoudle(g_struClientBuffer.u8MsgBuffer, g_struClientBuffer.u32Len);
        }while(0);
        g_struClientBuffer.u8Status = MSG_BUFFER_IDLE;
        g_struClientBuffer.u32Len = 0;
        ZC_SetClientFree(ClientId);

    }
  
    return;
}