/*************************************************
* Function: PCT_SendCloudAccessMsg3
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SendCloudAccessMsg3(PTC_ProtocolCon *pstruContoller)
{
    u16 u16Len;
    ZC_HandShakeMsg3 struMsg3;
    ZC_SecHead struSechead;
    u8 *pu8Vesion;

    ZC_GetStoreInfor(ZC_GET_TYPE_VESION, &pu8Vesion);
    
    memcpy(struMsg3.RandMsg, pstruContoller->RandMsg, ZC_HS_MSG_LEN);
    memcpy(struMsg3.u8EqVersion, pu8Vesion, ZC_EQVERSION_LEN);
    struMsg3.u8WifiVerSion[0] = (u8)(ZC_MODULE_VERSION >> 24);
    struMsg3.u8WifiVerSion[1] = (u8)(ZC_MODULE_VERSION >> 16);
    struMsg3.u8WifiVerSion[2] = (u8)(ZC_MODULE_VERSION >> 8);
    struMsg3.u8WifiVerSion[3] = (u8)(ZC_MODULE_VERSION);

    struMsg3.u8WifiType = ZC_MODULE_TYPE;
    /*first set key recv flag*/
    g_struProtocolController.u8keyRecv = PCT_KEY_RECVED;

    EVENT_BuildMsg(ZC_CODE_HANDSHAKE_3, 1, g_u8MsgBuildBuffer, &u16Len, 
        (u8*)&struMsg3, sizeof(ZC_HandShakeMsg3));

    struSechead.u8SecType = ZC_SEC_ALG_AES;
    struSechead.u16TotalMsg = ZC_HTONS(u16Len);

    (void)PCT_SendMsgToCloud(&struSechead, g_u8MsgBuildBuffer);
    
    pstruContoller->u8MainState = PCT_STATE_WAIT_MSG4;
    pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_REACCESS, 
        PCT_TIMER_INTERVAL_RECONNECT*10, &pstruContoller->u8AccessTimer);

    return;
}
Exemplo n.º 2
0
/*************************************************
* Function: ZC_SendClientQueryReq
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void ICACHE_FLASH_ATTR
ZC_SendClientQueryReq(u8 *pu8Msg, u16 u16RecvLen)
{
    ZC_MessageHead *pstruMsg;
    ZC_ClientQueryRsp struRsp;
    u16 u16Len;
    u8 *pu8DeviceId;
    u8 *pu8Domain;    
    u32 u32Index;
    ZC_ClientQueryReq *pstruQuery;
    ZC_SendParam struParam;

    if (g_struProtocolController.u8MainState < PCT_STATE_ACCESS_NET)
    {
        return;
    }
    
    if (u16RecvLen != sizeof(ZC_MessageHead) + sizeof(ZC_ClientQueryReq))
    {
        return;
    }
    
    pstruMsg = (ZC_MessageHead *)pu8Msg;
    pstruQuery = (ZC_ClientQueryReq *)(pstruMsg + 1);

    if (ZC_CODE_CLIENT_QUERY_REQ != pstruMsg->MsgCode)
    {
        return;
    }
    ZC_GetStoreInfor(ZC_GET_TYPE_DEVICEID, &pu8DeviceId);
    pu8Domain = pu8DeviceId + ZC_HS_DEVICE_ID_LEN;

    /*Only first 6 bytes is vaild*/
    for (u32Index = 0; u32Index < 6; u32Index++)
    {
        if (pstruQuery->u8Domain[u32Index] != pu8Domain[u32Index])
        {
            return;
        }
        
    }
    
    struRsp.addr[0] = g_u32GloablIp & 0xff;
    struRsp.addr[1] = (g_u32GloablIp >> 8) & 0xff;
    struRsp.addr[2] = (g_u32GloablIp >> 16) & 0xff;
    struRsp.addr[3] = (g_u32GloablIp >> 24)  & 0xff;
#if 0
    ZC_Printf("struRsp.addr %d.%d.%d.%d \r\n",
    		struRsp.addr[0], struRsp.addr[1],
			struRsp.addr[2], struRsp.addr[3]);
#endif
    memcpy(struRsp.DeviceId, pu8DeviceId, ZC_HS_DEVICE_ID_LEN);
    EVENT_BuildMsg(ZC_CODE_CLIENT_QUERY_RSP, 0, g_u8MsgBuildBuffer, &u16Len, (u8*)&struRsp, sizeof(ZC_ClientQueryRsp));
    struParam.u8NeedPoll = 0;
    struParam.pu8AddrPara = g_pu8RemoteAddr;
    g_struProtocolController.pstruMoudleFun->pfunSendUdpData(g_Bcfd, g_u8MsgBuildBuffer, u16Len, &struParam);  
}
/*************************************************
* Function: PCT_SendAckToCloud
* Description:
* Author: cxy
* Returns:
* Parameter:
* History:
*************************************************/
void PCT_SendAckToCloud(u8 u8MsgId)
{
    u16 u16Len;
    ZC_SecHead struSechead;

    EVENT_BuildMsg(ZC_CODE_ACK, u8MsgId, g_u8MsgBuildBuffer, &u16Len, NULL, 0);
    struSechead.u8SecType = ZC_SEC_ALG_AES;
    struSechead.u16TotalMsg = ZC_HTONS(u16Len);
    (void)PCT_SendMsgToCloud(&struSechead, g_u8MsgBuildBuffer);
}
/*************************************************
* Function: PCT_SendErrorMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SendErrorMsg(u8 u8MsgId, u8 *pu8Error, u16 u16ErrorLen)
{
    u16 u16Len;
    ZC_SecHead struSecHead;

    EVENT_BuildMsg(ZC_CODE_ERR, u8MsgId, g_u8MsgBuildBuffer, &u16Len, 
        pu8Error, u16ErrorLen);
    
    /*build sec head*/
    struSecHead.u8SecType = ZC_SEC_ALG_AES;
    struSecHead.u16TotalMsg = ZC_HTONS(u16Len);
    
    (void)PCT_SendMsgToCloud(&struSecHead, (u8*)g_u8MsgBuildBuffer);
}
Exemplo n.º 5
0
/*************************************************
* Function: PCT_SendCloudAccessMsg1
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SendCloudAccessMsg1(PTC_ProtocolCon *pstruContoller)
{
    u16 u16Len;
    ZC_SecHead struSechead;
    ZC_HandShakeMsg1 *pstruMsg1 = (ZC_HandShakeMsg1 *)g_u8MsgBuildBuffer;
    u8 *pu8DeviceId;
    u8 *pu8Domain;
    u32 u32RetVal;
    u8 u8DeviceIdLen;
    /*stop reconnection timer*/
    if (PCT_TIMER_INVAILD != pstruContoller->u8ReconnectTimer)
    {
        ZC_Printf("msg1 stop u8ReconnectTimer\n");
        TIMER_StopTimer(pstruContoller->u8ReconnectTimer);
        pstruContoller->u8ReconnectTimer = PCT_TIMER_INVAILD;
    }
    
    ZC_GetStoreInfor(ZC_GET_TYPE_DEVICEID, &pu8DeviceId);
    ZC_GetStoreInfor(ZC_GET_TYPE_DOMAIN, &pu8Domain);
    u8DeviceIdLen = strlen((const char *)pu8DeviceId);
    memcpy(pstruMsg1->RandMsg, pstruContoller->RandMsg, ZC_HS_MSG_LEN);
    memcpy(pstruMsg1->DeviceId, pu8DeviceId, u8DeviceIdLen);
    memcpy(pstruMsg1->u8Domain, pu8Domain, ZC_DOMAIN_LEN);

   
    EVENT_BuildMsg(ZC_CODE_HANDSHAKE_1, 1, g_u8MsgBuildBuffer, &u16Len, 
        (u8*)pstruMsg1, sizeof(ZC_HandShakeMsg1)+u8DeviceIdLen);

    struSechead.u8SecType = ZC_SEC_ALG_RSA;
    struSechead.u16TotalMsg = ZC_HTONS(u16Len);
    struSechead.u8Resver = 0x5A;
    u32RetVal = PCT_SendMsgToCloud(&struSechead, g_u8MsgBuildBuffer);
    
    if (ZC_RET_ERROR == u32RetVal)
    {
        ZC_Printf("Send Msg1 fail disconnect\n");
        PCT_DisConnectCloud(pstruContoller);
        return;
    }

    ZC_Printf("Send Msg1 \n");

    pstruContoller->u8MainState = PCT_STATE_WAIT_ACCESSRSP;

    
    pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_REACCESS, 
        PCT_TIMER_INTERVAL_RECONNECT * 120, &pstruContoller->u8AccessTimer);

}
/*************************************************
* Function: PCT_SendHeartMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SendUnbindMsg()
{
    ZC_MessageHead struUnbind;
    ZC_SecHead struSecHead;
    u16 u16Len = 0;
    u32 u32Timer = 0;
    EVENT_BuildMsg(ZC_CODE_UNBIND, 0, (u8*)&struUnbind, &u16Len, 
        NULL, 0);
    
    /*build sec head*/
    struSecHead.u8SecType = ZC_SEC_ALG_AES;
    struSecHead.u16TotalMsg = ZC_HTONS(u16Len);
    
    (void)PCT_SendMsgToCloud(&struSecHead, (u8*)&struUnbind);
     ZC_Printf("Send Unbind msg ok\n");

    u32Timer = rand();
    u32Timer = (PCT_TIMER_INTERVAL_SENDUBIND) * (u32Timer % 10 + 1);

    g_struProtocolController.pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDUBIND, 
        u32Timer, &g_struProtocolController.u8SendUnbindTimer);
    
}
/*************************************************
* Function: PCT_SendNotifyMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SendNotifyMsg(u8 u8NotifyCode)
{
    u16 u16Len;
    EVENT_BuildMsg(u8NotifyCode, 0, g_u8MsgBuildBuffer, &u16Len, NULL, 0);
    g_struProtocolController.pstruMoudleFun->pfunSendToMoudle(g_u8MsgBuildBuffer, u16Len);
}
Exemplo n.º 8
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;
}