/*************************************************
* Function: PCT_RecvAccessMsg2
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_RecvAccessMsg2(PTC_ProtocolCon *pstruContoller)
{
    MSG_Buffer *pstruBuffer;
    ZC_MessageHead *pstruMsg;
    ZC_HandShakeMsg2 *pstruMsg2;
    ZC_AccessPoint *pstruAccessPoint;
    u32 u32Addr;
    u16 u16Port;
    pstruBuffer = (MSG_Buffer *)MSG_PopMsg(&g_struRecvQueue);
    if (NULL == pstruBuffer)
    {
        return;
    }

    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    pstruMsg2 = (ZC_HandShakeMsg2*)(pstruMsg + 1);

    if ((ZC_CODE_HANDSHAKE_2 == pstruMsg->MsgCode)||(ZC_CODE_ACCESS_POINT_RSP == pstruMsg->MsgCode))
    {
        if (ZC_RET_ERROR == PCT_CheckCrc(pstruMsg->TotalMsgCrc, (u8*)pstruMsg2, ZC_HTONS(pstruMsg->Payloadlen)))
        {
            PCT_DisConnectCloud(pstruContoller);
        }
        else
        {
            TIMER_StopTimer(pstruContoller->u8AccessTimer);
            if (0 == memcmp(pstruMsg2->RandMsg, pstruContoller->RandMsg, ZC_HS_MSG_LEN))
            {
                if (ZC_CODE_HANDSHAKE_2 == pstruMsg->MsgCode)
                {
                    memcpy(pstruContoller->u8SessionKey, pstruMsg2->SessionKey, ZC_HS_SESSION_KEY_LEN);
                    memcpy(pstruContoller->IvRecv, pstruMsg2->SessionKey, ZC_HS_SESSION_KEY_LEN);
                    memcpy(pstruContoller->IvSend, pstruMsg2->SessionKey, ZC_HS_SESSION_KEY_LEN);

                    PCT_SendCloudAccessMsg3(pstruContoller);
                
                    ZC_Printf("Recv Msg2 ok\n");
                }
                else
                {
                    pstruAccessPoint=(ZC_AccessPoint*)(pstruMsg + 1);
                    u32Addr = ZC_HTONL(pstruAccessPoint->u32ServerAddr);
                    u16Port = ZC_HTONS(pstruAccessPoint->u16ServerPort);

                    ZC_StoreAccessInfo((u8 *)&u32Addr,(u8 *)&u16Port);
                    PCT_DisConnectCloud(pstruContoller);
                    ZC_Printf("Recv Access Point ok,Cloud Addr=%x,port=%x!\n",u32Addr,u16Port);
                }
            }
            else
            {
                PCT_DisConnectCloud(pstruContoller);
                ZC_Printf("Recv Msg2 rand error \n");            
            }
        }
    }
    
    pstruBuffer->u32Len = 0;
    pstruBuffer->u8Status = MSG_BUFFER_IDLE;
}
Esempio n. 2
0
/*************************************************
* Function: ZC_ConfigPara
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void ZC_ConfigPara(u8 *pu8Data)
{
    ZC_Configuration *pstruConfig;
    pstruConfig = (ZC_Configuration*)pu8Data;

    g_struZcConfigDb.struSwitchInfo.u32SecSwitch = ZC_HTONL(pstruConfig->u32SecSwitch);
    g_struZcConfigDb.struSwitchInfo.u32TraceSwitch = ZC_HTONL(pstruConfig->u32TraceSwitch);
    g_struZcConfigDb.struSwitchInfo.u32WifiConfig = ZC_HTONL(pstruConfig->u32WifiConfig);
    g_struZcConfigDb.struSwitchInfo.u32ServerAddrConfig = ZC_HTONL(pstruConfig->u32ServerAddrConfig);

    g_struZcConfigDb.struSwitchInfo.u32ServerIp = ZC_HTONL(pstruConfig->u32IpAddr);
    g_struZcConfigDb.struSwitchInfo.u16ServerPort = ZC_HTONS(pstruConfig->u16Port);

    memcpy(g_struZcConfigDb.struSwitchInfo.u8Password, pstruConfig->u8Password, ZC_PASSWORD_MAX_LEN);
    memcpy(g_struZcConfigDb.struSwitchInfo.u8Ssid, pstruConfig->u8Ssid, ZC_SSID_MAX_LEN);
    
    memcpy(g_struZcConfigDb.struCloudInfo.u8CloudAddr, pstruConfig->u8CloudAddr, ZC_CLOUD_ADDR_MAX_LEN);
    memcpy(g_struZcConfigDb.struCloudInfo.u8CloudKey, pstruConfig->u8CloudKey, ZC_CLOUD_KEY_MAX_LEN);

    g_struProtocolController.pstruMoudleFun->pfunWriteFlash((u8*)&g_struZcConfigDb, sizeof(ZC_ConfigDB));
}
/*************************************************
* Function: PCT_ModuleOtaFileChunkMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_ModuleOtaFileChunkMsg(PTC_ProtocolCon *pstruContoller, ZC_MessageHead *pstruMsg, u8 *pu8Msg)
{
    ZC_OtaFileChunkReq *pstruOta;
    u32 u32FileLen;
    u32 u32RetVal;
    u32 u32RecvOffset;
    u16 u16CalCrc;
    u16 u16RecvCrc;

    ZC_Printf("Ota File Chunk\n");
    
    pstruOta = (ZC_OtaFileChunkReq *)(pu8Msg);
    u32FileLen = ZC_HTONS(pstruMsg->Payloadlen) - sizeof(ZC_OtaFileChunkReq);
    u32RecvOffset = ZC_HTONL(pstruOta->u32Offset);
    
    /*check para*/
    if ((u32RecvOffset != pstruContoller->struOtaInfo.u32RecvOffset)
        || ((u32RecvOffset + u32FileLen) > pstruContoller->struOtaInfo.u32TotalLen)
      || (u32FileLen > ZC_OTA_MAX_CHUNK_LEN))
    {
        ZC_Printf("recv error %d,%d,%d,%d,\n", u32RecvOffset, 
            pstruContoller->struOtaInfo.u32RecvOffset,
            pstruContoller->struOtaInfo.u32TotalLen,
            u32FileLen);
        PCT_SendNotifyMsg(ZC_CODE_ERR);
        return;
    }
    //Check CRC
    u16CalCrc = crc16_ccitt((u8*)(pstruOta), ZC_HTONS(pstruMsg->Payloadlen));
    u16RecvCrc = (pstruMsg->TotalMsgCrc[0] << 8) + pstruMsg->TotalMsgCrc[1];

    if (u16CalCrc != u16RecvCrc)
    {
        PCT_SendNotifyMsg(ZC_CODE_ERR);
        return;
    }
 
    u32RetVal = pstruContoller->pstruMoudleFun->pfunUpdate((u8*)(pstruOta + 1), u32RecvOffset, u32FileLen);
    //u32RetVal = ZC_RET_OK;
    ZC_Printf("offset = %d, len = %d\n", u32RecvOffset, u32FileLen);

    if (ZC_RET_ERROR == u32RetVal)
    {
        ZC_Printf("OTA Fail\n");
        PCT_SendNotifyMsg(ZC_CODE_ERR);
        return;
    }

    /*update file offset*/
    pstruContoller->struOtaInfo.u32RecvOffset = pstruContoller->struOtaInfo.u32RecvOffset + u32FileLen;
    PCT_SendNotifyMsg(ZC_CODE_ACK);
}
/*************************************************
* Function: PCT_ModuleOtaFileBeginMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_ModuleOtaFileBeginMsg(PTC_ProtocolCon *pstruContoller, u8 *pu8Msg)
{
    ZC_OtaFileBeginReq *pstruOta;
    ZC_Printf("Ota File Begin\n");
    
    pstruOta = (ZC_OtaFileBeginReq *)(pu8Msg);
    
    pstruContoller->struOtaInfo.u32RecvOffset = 0;
    pstruContoller->struOtaInfo.u32TotalLen = ZC_HTONL(pstruOta->u32FileTotalLen);
    pstruContoller->struOtaInfo.u8Crc[0] = pstruOta->u8TotalFileCrc[0];
    pstruContoller->struOtaInfo.u8Crc[1] = pstruOta->u8TotalFileCrc[1];    
    PCT_SendNotifyMsg(ZC_CODE_ACK);
    return;
}
/*************************************************
* Function: PCT_HandleOtaFileChunkMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleOtaFileChunkMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer)
{
    ZC_MessageHead *pstruMsg;
    ZC_OtaFileChunkReq *pstruOta;
    u32 u32FileLen;
    u32 u32RetVal;
    u32 u32RecvOffset;

    ZC_Printf("Ota File Chunk\n");

    
    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    pstruOta = (ZC_OtaFileChunkReq *)(pstruMsg + 1);
    u32FileLen = ZC_HTONS(pstruMsg->Payloadlen) - sizeof(ZC_OtaFileChunkReq);
    u32RecvOffset = ZC_HTONL(pstruOta->u32Offset);
    
    /*check para*/
    if ((u32RecvOffset != pstruContoller->struOtaInfo.u32RecvOffset)
        || ((u32RecvOffset + u32FileLen) > pstruContoller->struOtaInfo.u32TotalLen)
      || (u32FileLen > ZC_OTA_MAX_CHUNK_LEN))
    {
        ZC_Printf("recv error %d,%d,%d,%d,\n", u32RecvOffset, 
            pstruContoller->struOtaInfo.u32RecvOffset,
            pstruContoller->struOtaInfo.u32TotalLen,
            u32FileLen);
        PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0);
        return;
    }

    u32RetVal = pstruContoller->pstruMoudleFun->pfunUpdate((u8*)(pstruOta + 1), u32RecvOffset, u32FileLen);
    //u32RetVal = ZC_RET_OK;
    ZC_Printf("offset = %d, len = %d\n", u32RecvOffset, u32FileLen);

    if (ZC_RET_ERROR == u32RetVal)
    {
        ZC_Printf("OTA Fail\n");
        PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0);
        return;
    }

    /*update file offset*/
    pstruContoller->struOtaInfo.u32RecvOffset = pstruContoller->struOtaInfo.u32RecvOffset + u32FileLen;
    PCT_SendAckToCloud(pstruMsg->MsgId);
}
/*************************************************
* Function: PCT_HandleOtaFileBeginMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleOtaFileBeginMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer)
{
    ZC_MessageHead *pstruMsg;
    ZC_OtaFileBeginReq *pstruOta;
    ZC_Printf("Ota File Begin\n");
    

    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    pstruOta = (ZC_OtaFileBeginReq *)(pstruMsg + 1);
    
    pstruContoller->struOtaInfo.u32RecvOffset = 0;
    pstruContoller->struOtaInfo.u32TotalLen = ZC_HTONL(pstruOta->u32FileTotalLen);
    pstruContoller->struOtaInfo.u8Crc[0] = pstruOta->u8TotalFileCrc[0];
    pstruContoller->struOtaInfo.u8Crc[1] = pstruOta->u8TotalFileCrc[1];    
    PCT_SendAckToCloud(pstruMsg->MsgId);


    return;
}
Esempio n. 7
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;
}