/*************************************************
* 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;
}
/*************************************************
* 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);
}
/*************************************************
* 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);
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
0
/*************************************************
* Function: MSG_SendDataToCloud
* Description: 
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
void ICACHE_FLASH_ATTR
MSG_SendDataToCloud(u8 *pu8Connection)
{
    PTC_Connection *pstruConnection = (PTC_Connection *)pu8Connection;
    MSG_Buffer *pstruBuf = NULL;
    ZC_SendParam struParam;

    u16 u16DataLen; 

    if (1 == g_struProtocolController.u32AckFlag)
    {
        /* 起一个定时器,防止收不到ack,无法再发送包 */
        if (PCT_TIMER_INVAILD == g_struProtocolController.u8NoackTimer)
        {
            g_struProtocolController.pstruMoudleFun->pfunSetTimer(PCT_TIMER_NOACK, 
                    PCT_TIMER_INTERVAL_NOACK, &g_struProtocolController.u8NoackTimer);
        }
        return;
    }
    else
    {
        /* 取消定时器 */
        if (PCT_TIMER_INVAILD != g_struProtocolController.u8NoackTimer)
        {
            TIMER_StopTimer(g_struProtocolController.u8NoackTimer);
            g_struProtocolController.u8NoackTimer = PCT_TIMER_INVAILD;
        }
    }
    pstruBuf = (MSG_Buffer *)MSG_PopMsg(&g_struSendQueue); 
    
    if (NULL == pstruBuf)return;

    //ZC_Printf("Pop Q!!!\n");
    u16DataLen = pstruBuf->u32Len; 
    struParam.u8NeedPoll = 0;
    g_struProtocolController.pstruMoudleFun->pfunSendTcpData(pstruConnection->u32Socket, pstruBuf->u8MsgBuffer, u16DataLen, &struParam);
 
//    ZC_Printf("send data len = %d\n",u16DataLen);

    pstruBuf->u8Status = MSG_BUFFER_IDLE;
    pstruBuf->u32Len = 0;
    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;
} 
Exemplo n.º 7
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;
}