Ejemplo n.º 1
0
/*************************************************
* 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;
}
Ejemplo n.º 2
0
/*************************************************
* Function: ZC_TraceData
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void ZC_TraceData(u8* pData, u32 Len)
{
    u32 Index;
    if (0 == g_u32TraceSwitch)
    {
        return;
    }
    if (0 == Len)
    {
        return;
    }
    
    ZC_Printf("++++++++++++++++++++++++++++++++++++++++++++++++\n");
    for (Index = 0; Index + 4 < Len; Index = Index + 4)
    {
        ZC_Printf("0x%02x, 0x%02x, 0x%02x, 0x%02x,\n",
            pData[Index],
            pData[Index + 1],
            pData[Index + 2],
            pData[Index + 3]);
    }
    
    for (; Index < Len - 1; Index++)
    {
        ZC_Printf("0x%02x, ", pData[Index]);
    }
    ZC_Printf("0x%02x", pData[Index]);

    ZC_Printf("\n++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
Ejemplo n.º 3
0
/*************************************************
* 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);
}
Ejemplo n.º 4
0
/*************************************************
* Function: MX_ConnectToCloud
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
u32 
MX_ConnectToCloud(PTC_Connection *pstruConnection)
{
    int fd; 
    int opt = 0;
    struct sockaddr_t addr;
    memset((char*)&addr,0,sizeof(addr));
    
    if (1 == g_struZcConfigDb.struSwitchInfo.u32ServerAddrConfig)
    {
        addr.s_port = g_struZcConfigDb.struSwitchInfo.u16ServerPort;
        addr.s_ip = g_struZcConfigDb.struSwitchInfo.u32ServerIp;
    }
    else
    {
        addr.s_ip = u32CloudIp;
        addr.s_port = ZC_CLOUD_PORT;
        ZC_Printf("connect redirect!\n");
    }
    
    
    if (0 == addr.s_ip)
    {
        return ZC_RET_ERROR;
    }
    
	ZC_Printf("connect ip = 0x%x!\n",addr.s_ip);
    
    fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    setsockopt(fd,0,SO_BLOCKMODE,&opt,4);
    if(fd<0)
        return ZC_RET_ERROR;
    
    if (connect(fd, &addr, sizeof(addr))< 0)
    {
        close(fd);
        if(g_struProtocolController.struCloudConnection.u32ConnectionTimes++>20)
        {
            g_struZcConfigDb.struSwitchInfo.u32ServerAddrConfig = 0;
        }
        return ZC_RET_ERROR;
    }
    
    ZC_Printf("connect ok!\n");
    
    g_struProtocolController.struCloudConnection.u32Socket = fd;
    ZC_Rand(g_struProtocolController.RandMsg);
    
    return ZC_RET_OK;
}
Ejemplo 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);

}
Ejemplo n.º 6
0
/*************************************************
* Function: MX_ListenClient
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
u32 MX_ListenClient(PTC_Connection *pstruConnection)
{
    int fd; 
    struct sockaddr_t servaddr;

    fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(fd<0)
        return ZC_RET_ERROR;

    servaddr.s_port = pstruConnection->u16Port;
    servaddr.s_ip =  INADDR_ANY;  /* Accept conenction request on all network interface */
    if(bind(fd,(struct sockaddr_t *)&servaddr,sizeof(servaddr))<0)
    {
        close(fd);
        return ZC_RET_ERROR;
    }
    
    if (listen(fd, ZC_MAX_CLIENT_NUM)< 0)
    {
        close(fd);
        return ZC_RET_ERROR;
    }

    ZC_Printf("Tcp Listen Port = %d\n", pstruConnection->u16Port);
    g_struProtocolController.struClientConnection.u32Socket = fd;

    return ZC_RET_OK;
}
Ejemplo n.º 7
0
/*************************************************
* Function: MX_SendTcpData
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void MX_SendTcpData(u32 u32Fd, u8 *pu8Data, u16 u16DataLen, ZC_SendParam *pstruParam)
{
	int rtn;
    rtn=send(u32Fd, pu8Data, u16DataLen, 0); 
	if(rtn<0)
		ZC_Printf("send err = %s\n", strerror(errno));
}
Ejemplo n.º 8
0
/*************************************************
* Function: PCT_ReconnectCloud
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_ReconnectCloud(PTC_ProtocolCon *pstruContoller, u32 u32ReConnectTimer)
{
    u32 u32Index;
    if (PCT_TIMER_INVAILD != pstruContoller->u8ReconnectTimer)
    {
        ZC_Printf("already reconnected \n");
        return;
    }
    

    MSG_Init();
    g_struProtocolController.u8keyRecv = PCT_KEY_UNRECVED;

    for (u32Index = 0; u32Index < ZC_TIMER_MAX_NUM; u32Index++)
    {
        
        if (g_struTimer[u32Index].u8Status == ZC_TIMER_STATUS_USED)
        {
            TIMER_StopTimer((u8)u32Index);
        }
    }
    
    TIMER_Init();
    g_struProtocolController.u8ReconnectTimer = PCT_TIMER_INVAILD;
    g_struProtocolController.u8SendMoudleTimer = PCT_TIMER_INVAILD;
    g_struProtocolController.u8HeartTimer = PCT_TIMER_INVAILD;
    g_struProtocolController.u8MainState = PCT_STATE_INIT;

    pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_RECONNECT, 
        u32ReConnectTimer, &pstruContoller->u8ReconnectTimer);
    pstruContoller->struCloudConnection.u32Socket = PCT_INVAILD_SOCKET;
    pstruContoller->u8keyRecv = PCT_KEY_UNRECVED; 
    pstruContoller->u8MainState = PCT_STATE_INIT;
}
Ejemplo n.º 9
0
/*************************************************
* Function: PCT_SendMoudleTimeout
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SendMoudleTimeout(PTC_ProtocolCon *pstruProtocolController)
{
    MSG_Buffer *pstruBuffer;
    ZC_MessageHead *pstruMsg;
    pstruBuffer = (MSG_Buffer *)pstruProtocolController->pu8SendMoudleBuffer;
    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;

    /*Send to Moudle*/
    pstruProtocolController->u8ReSendMoudleNum++;

    ZC_Printf("send moudle timeout, data len = %d\n",pstruBuffer->u32Len);
    
    if (pstruProtocolController->u8ReSendMoudleNum > PCT_SENDMOUDLE_NUM)
    {
        pstruBuffer = (MSG_Buffer *)pstruProtocolController->pu8SendMoudleBuffer;
        pstruBuffer->u32Len = 0;
        pstruBuffer->u8Status = MSG_BUFFER_IDLE;
        pstruProtocolController->u8SendMoudleTimer = PCT_TIMER_INVAILD;
        pstruProtocolController->u8ReSendMoudleNum = 0;
        PCT_SendEmptyMsg(pstruMsg->MsgId, ZC_SEC_ALG_AES);
        PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0);
    }
    else
    {
        pstruProtocolController->pstruMoudleFun->pfunSendToMoudle(pstruBuffer->u8MsgBuffer, 
            pstruBuffer->u32Len);

        pstruProtocolController->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDMOUDLE, 
            PCT_TIMER_INTERVAL_SENDMOUDLE, &pstruProtocolController->u8SendMoudleTimer);
    }

}
Ejemplo n.º 10
0
/*************************************************
* Function: PCT_RecvAccessMsg4
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleUnbindMsg(PTC_ProtocolCon *pstruContoller)
{
    MSG_Buffer *pstruBuffer;
    ZC_MessageHead *pstruMsg;
    pstruBuffer = (MSG_Buffer *)MSG_PopMsg(&g_struRecvQueue);
    if (NULL == pstruBuffer)
    {
        return;
    }
    
    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    
    if (ZC_CODE_UNBIND == pstruMsg->MsgCode)
    {
        g_struProtocolController.u8MainState = PCT_STATE_CONNECT_CLOUD;
        if (PCT_TIMER_INVAILD != pstruContoller->u8SendUnbindTimer)
        {
           TIMER_StopTimer(pstruContoller->u8SendUnbindTimer);           
           pstruContoller->u8SendUnbindTimer = PCT_TIMER_INVAILD;
        }        
        ZC_ConfigUnBind(0xFFFFFFFF);
        ZC_Printf("recv unbind resp ok\n");
    }
    pstruBuffer->u32Len = 0;
    pstruBuffer->u8Status = MSG_BUFFER_IDLE;

    pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDHEART, 
        PCT_TIMER_INTERVAL_HEART, &pstruContoller->u8HeartTimer);
}
Ejemplo n.º 11
0
/*************************************************
* Function: PCT_SendMsgToCloud
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SetAesKey(u8 *pu8Key)
{   
    if (NULL == pu8Key)
    {
        return;
    }
    u8 *pu8DeviceId;
    u8 u8DeviceIdLen;
    
    memset(pu8Key, '0' , ZC_HS_SESSION_KEY_LEN);

    ZC_GetStoreInfor(ZC_GET_TYPE_DEVICEID, &pu8DeviceId);

    u8DeviceIdLen = strlen((const char *)pu8DeviceId);

    if (u8DeviceIdLen < PCT_DEVICE_MIN_LEN || u8DeviceIdLen > PCT_DEVICE_MAX_LEN)
    {
        ZC_Printf("PCT_SetAesKey error: u8DeviceIdLen is %d\n", u8DeviceIdLen);
        return;
    }
    /* AES Key */
    if (u8DeviceIdLen <= ZC_HS_SESSION_KEY_LEN)
    {
        memcpy(pu8Key + ZC_HS_SESSION_KEY_LEN - u8DeviceIdLen,
                pu8DeviceId,
                u8DeviceIdLen);
    }
    else if (u8DeviceIdLen > ZC_HS_SESSION_KEY_LEN)
    {
        memcpy(pu8Key,
                pu8DeviceId,
                ZC_HS_SESSION_KEY_LEN);
    }
    return;
}
Ejemplo n.º 12
0
/*************************************************
* Function: MX_Init
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void MX_Init()
{

    g_struAdapter.pfunConnectToCloud = MX_ConnectToCloud;
    g_struAdapter.pfunUpdate = MX_FirmwareUpdate;     
    g_struAdapter.pfunUpdateFinish = MX_FirmwareUpdateFinish;
    g_struAdapter.pfunSendToMoudle = MX_SendDataToMoudle;  
    g_struAdapter.pfunSetTimer = MX_SetTimer;   
    g_struAdapter.pfunStopTimer = MX_StopTimer;
    g_struAdapter.pfunListenClient = MX_ListenClient;
    g_struAdapter.pfunSendTcpData = MX_SendTcpData;  
    g_struAdapter.pfunSendUdpData = MX_SendUdpData; 
    g_struAdapter.pfunRest = MX_Rest;
    g_struAdapter.pfunWriteFlash = MX_WriteDataToFlash;
    g_struAdapter.pfunReadFlash = MX_ReadDataFormFlash;
    g_struAdapter.pfunReboot = MX_Reboot;
    g_struAdapter.pfunGetMac = MX_GetMac;
   // g_struAdapter.pfunPrintf = MX_Printf;
    g_struAdapter.pfunPrintf = (pFunPrintf)printf;
    g_struAdapter.pfunMalloc = malloc;
    g_struAdapter.pfunFree = free;

    g_u16TcpMss = 1000;
    PCT_Init(&g_struAdapter);
	MX_TimerInit();
    ZC_Printf("MT Init\n");
}
Ejemplo n.º 13
0
/*************************************************
* Function: ZC_SendBc
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void ICACHE_FLASH_ATTR
ZC_SendBc()
{
    u16 u16Len;
    static int sleepcount = 0;
    ZC_SendParam struParam;

    if (PCT_STATE_CONNECT_CLOUD != g_struProtocolController.u8MainState)
    {
        sleepcount = 0;
        return;
    }
    sleepcount++;
    if (sleepcount > g_u32BcSleepCount)
    {
        EVENT_BuildBcMsg(g_u8MsgBuildBuffer, &u16Len);
        if (g_struProtocolController.u16SendBcNum < (PCT_SEND_BC_MAX_NUM))
        {
            ZC_Printf("Send Bc...\n");
            struParam.u8NeedPoll = 0;
            struParam.pu8AddrPara = g_pu8RemoteAddr;
            g_struProtocolController.pstruMoudleFun->pfunSendUdpData(g_Bcfd, g_u8MsgBuildBuffer, u16Len, &struParam);  
            g_struProtocolController.u16SendBcNum++;
        }
        sleepcount = 0;
    }
    
}
Ejemplo n.º 14
0
/*************************************************
* Function: PCT_RecvAccessMsg4
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_RecvAccessMsg4(PTC_ProtocolCon *pstruContoller)
{
    MSG_Buffer *pstruBuffer;
    ZC_MessageHead *pstruMsg;
    ZC_HandShakeMsg4 *pstruMsg4;
    pstruBuffer = (MSG_Buffer *)MSG_PopMsg(&g_struRecvQueue);
    if (NULL == pstruBuffer)
    {
        return;
    }
    
    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    pstruMsg4 = (ZC_HandShakeMsg4 *)(pstruMsg + 1);
    
    if (ZC_CODE_HANDSHAKE_4 == pstruMsg->MsgCode)
    {
        if (ZC_RET_ERROR == PCT_CheckCrc(pstruMsg->TotalMsgCrc, (u8*)pstruMsg4, ZC_HTONS(pstruMsg->Payloadlen)))
        {
            PCT_DisConnectCloud(pstruContoller);
        }
        else
        {   
            TIMER_StopTimer(pstruContoller->u8AccessTimer);
            if (0 == memcmp(pstruMsg4->RandMsg, pstruContoller->RandMsg, ZC_HS_MSG_LEN))
            {
                pstruContoller->u8MainState = PCT_STATE_CONNECT_CLOUD;
                
                ZC_Printf("recv msg4 ok\n");
                PCT_SendNotifyMsg(ZC_CODE_CLOUD_CONNECTED);
            }
            else
            {
                PCT_DisConnectCloud(pstruContoller);
                ZC_Printf("Recv msg4 rand error \n");            
            }
        }
    }
    pstruBuffer->u32Len = 0;
    pstruBuffer->u8Status = MSG_BUFFER_IDLE;

    if (PCT_TIMER_INVAILD != pstruContoller->u8HeartTimer)
    {
        TIMER_StopTimer(pstruContoller->u8HeartTimer);
    }
    pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDHEART, 
        PCT_TIMER_INTERVAL_HEART, &pstruContoller->u8HeartTimer);
}
Ejemplo n.º 15
0
/*************************************************
* Function: PCT_RecvAccessMsg2
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_RecvAccessMsg2(PTC_ProtocolCon *pstruContoller)
{
    MSG_Buffer *pstruBuffer;
    ZC_MessageHead *pstruMsg;
    ZC_HandShakeMsg2 *pstruMsg2;
 
    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)
    {
        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))
            {
                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
            {
                PCT_DisConnectCloud(pstruContoller);
                ZC_Printf("Recv Msg2 rand error \n");            
            }
        }
    }
    
    pstruBuffer->u32Len = 0;
    pstruBuffer->u8Status = MSG_BUFFER_IDLE;
}
Ejemplo n.º 16
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);  
}
Ejemplo n.º 17
0
/*************************************************
* Function: PCT_HandleOtaFileEndMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleOtaFileEndMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer)
{
    ZC_MessageHead *pstruMsg;
    ZC_Printf("Ota File End\n");

    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    PCT_SendAckToCloud(pstruMsg->MsgId);

}
Ejemplo n.º 18
0
/*************************************************
* 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);
}
Ejemplo n.º 19
0
/*************************************************
* Function: PCT_HandleOtaBeginMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleOtaBeginMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer)
{
    ZC_MessageHead *pstruMsg;
    ZC_Printf("Ota Begin\n");
    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    PCT_SendAckToCloud(pstruMsg->MsgId);    
    
    PCT_SendNotifyMsg(ZC_CODE_ZOTA_BEGIN);
    return;
}
Ejemplo n.º 20
0
/*************************************************
* Function: MX_GetMac
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void MX_GetMac(u8 *pu8Mac)
{
    snprintf( (char *)pu8Mac, 16, "%c%c%c%c%c%c%c%c%c%c%c%c",context->micoStatus.mac[0],  context->micoStatus.mac[1], \
	                                                   context->micoStatus.mac[3],  context->micoStatus.mac[4], \
		                                                 context->micoStatus.mac[6],  context->micoStatus.mac[7], \
	                                                   context->micoStatus.mac[9],  context->micoStatus.mac[10], \
                                                     context->micoStatus.mac[12], context->micoStatus.mac[13], \
                                                     context->micoStatus.mac[15], context->micoStatus.mac[16] );
	ZC_Printf("mac=%s\n",pu8Mac);
}
Ejemplo n.º 21
0
/*************************************************
* Function: PCT_SetLocalLevel
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_SetLocalLevel(PCT_LOCAL_LEVEL Etoken)
{   
    /* set level */
    if (PCT_LOCAL_NONE_TOKEN != Etoken
        && PCT_LOCAL_STATIC_TOKEN != Etoken
         && PCT_LOCAL_DYNAMIC_TOKEN != Etoken)
    {
        ZC_Printf("PCT_SetLocalLevel error, Etoken is %d\n", Etoken);
        return;
    }
    g_struProtocolController.u32LocalTokenFlag = Etoken;
    return;
}
Ejemplo n.º 22
0
/*************************************************
* 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;
}
Ejemplo n.º 23
0
/*************************************************
* Function: PCT_ModuleOtaFileEndMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_ModuleOtaFileEndMsg(PTC_ProtocolCon *pstruContoller, u8 *pu8Msg)
{
    u32 u32RetVal;
    ZC_Printf("Ota File End\n");
    u32RetVal = pstruContoller->pstruMoudleFun->pfunUpdateFinish(pstruContoller->struOtaInfo.u32TotalLen);
    if (ZC_RET_ERROR == u32RetVal)
    {
        PCT_SendNotifyMsg(ZC_CODE_ERR);
    }
    else
    {
        PCT_SendNotifyMsg(ZC_CODE_ACK);
    }
    

}
Ejemplo n.º 24
0
/*************************************************
* Function: PCT_CheckCrc
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
u32 PCT_CheckCrc(u8 *pu8Crc, u8 *pu8Data, u16 u16Len)
{
    u16 u16Crc;
    u16 u16RecvCrc;
    u16Crc = crc16_ccitt(pu8Data, u16Len);
    u16RecvCrc = (pu8Crc[0] << 8) + pu8Crc[1];
    if (u16Crc == u16RecvCrc)
    {
        return ZC_RET_OK;
    }
    else
    {
        ZC_Printf("Crc is error, recv = %d, calc = %d\n", u16RecvCrc, u16Crc);    
        
        return ZC_RET_ERROR;    
    }
}
Ejemplo n.º 25
0
/*************************************************
* Function: PCT_HandleOtaFileEndMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleOtaEndMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer)
{
    ZC_MessageHead *pstruMsg;
    u32 u32RetVal;
    ZC_Printf("Ota End\n");

    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    u32RetVal = pstruContoller->pstruMoudleFun->pfunUpdateFinish(pstruContoller->struOtaInfo.u32TotalLen);
    if (ZC_RET_ERROR == u32RetVal)
    {
        PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0);
    }
    else
    {
        PCT_SendAckToCloud(pstruMsg->MsgId);
        PCT_SendNotifyMsg(ZC_CODE_ZOTA_END);
    }
}
Ejemplo n.º 26
0
/*************************************************
* Function: MX_BcInit
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void MX_BcInit()
{
    int tmp=1;
    struct sockaddr_t addr;
    addr.s_port = ZC_MOUDLE_PORT;
    addr.s_ip = INADDR_ANY;
    g_Bcfd = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP); 
	ZC_Printf("g_Bcfd = %d\n", g_Bcfd);
    setsockopt(g_Bcfd, SOL_SOCKET,SO_BROADCAST,&tmp,sizeof(tmp)); 
    bind(g_Bcfd, &addr, sizeof(addr));
    g_struProtocolController.u16SendBcNum = 0;
    memset((char*)&struRemoteAddr,0,sizeof(struRemoteAddr));

    struRemoteAddr.s_port = ZC_MOUDLE_BROADCAST_PORT; 
    struRemoteAddr.s_ip = inet_addr("255.255.255.255"); 
    g_pu8RemoteAddr = (u8*)&struRemoteAddr;
    g_u32BcSleepCount = 581;
    return;
}
Ejemplo n.º 27
0
/*************************************************
* 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;
}
Ejemplo n.º 28
0
/*************************************************
* Function: PCT_HandleOtaFileEndMsg
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_HandleOtaEndMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer)
{
    ZC_MessageHead *pstruMsg;
    u32 u32RetVal;
    ZC_Printf("Ota End\n");

    pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer;
    u32RetVal = pstruContoller->pstruMoudleFun->pfunUpdateFinish(pstruContoller->struOtaInfo.u32TotalLen);
    if (ZC_RET_ERROR == u32RetVal)
    {
        PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0);
    }
    else
    {
        PCT_SendAckToCloud(pstruMsg->MsgId);
        pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_REBOOT, 
            PCT_TIMER_INTERVAL_REBOOT, &pstruContoller->u8RebootTimer);
    }
}
Ejemplo n.º 29
0
/*************************************************
* Function: PCT_ReconnectCloud
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void PCT_ReconnectCloud(PTC_ProtocolCon *pstruContoller, u32 u32ReConnectTimer)
{
    u32 u32Index;
    if (PCT_TIMER_INVAILD != pstruContoller->u8ReconnectTimer)
    {
        ZC_Printf("already reconnected \n");
        return;
    }
    

    MSG_Init();
    g_struProtocolController.u8keyRecv = PCT_KEY_UNRECVED;
    memcpy(g_struProtocolController.u8SessionKey, g_struRegisterInfo.u8PrivateKey, ZC_HS_SESSION_KEY_LEN);
    memcpy(g_struProtocolController.IvSend, g_struRegisterInfo.u8PrivateKey, ZC_HS_SESSION_KEY_LEN);
    memcpy(g_struProtocolController.IvRecv, g_struRegisterInfo.u8PrivateKey, ZC_HS_SESSION_KEY_LEN);

    for (u32Index = 0; u32Index < ZC_TIMER_MAX_NUM; u32Index++)
    {
        
        if (g_struTimer[u32Index].u8Status == ZC_TIMER_STATUS_USED)
        {
            TIMER_StopTimer((u8)u32Index);
        }
    }
    
    TIMER_Init();
    g_struProtocolController.u8ReconnectTimer = PCT_TIMER_INVAILD;
    g_struProtocolController.u8SendMoudleTimer = PCT_TIMER_INVAILD;
    g_struProtocolController.u8HeartTimer = PCT_TIMER_INVAILD;
    g_struProtocolController.u8CloudAckTimer = PCT_TIMER_INVAILD;
    g_struProtocolController.u8MainState = PCT_STATE_INIT;

    g_struProtocolController.u32CloudNotAckNum = 0;

    pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_RECONNECT, 
        u32ReConnectTimer, &pstruContoller->u8ReconnectTimer);
    pstruContoller->struCloudConnection.u32Socket = PCT_INVAILD_SOCKET;
    pstruContoller->u8keyRecv = PCT_KEY_UNRECVED; 
    pstruContoller->u8MainState = PCT_STATE_INIT;
}
Ejemplo n.º 30
0
/*************************************************
* 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);
    
}