/************************************************* * Function: PCT_HandleUnbindMsg * 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; if (PCT_TIMER_INVAILD != pstruContoller->u8HeartTimer) { TIMER_StopTimer(pstruContoller->u8HeartTimer); } pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDHEART, PCT_TIMER_INTERVAL_HEART, &pstruContoller->u8HeartTimer); }
/************************************************* * Function: PCT_Sleep * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_Sleep() { u32 u32Index; 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.u8RegisterTimer = PCT_TIMER_INVAILD; g_struProtocolController.u32CloudNotAckNum = 0; PCT_SendNotifyMsg(ZC_CODE_WIFI_DISCONNECTED); ZC_ClientSleep(); }
/************************************************* * 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; }
/************************************************* * Function: PCT_HandleMoudleMsg * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_HandleMoudleMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer) { ZC_MessageHead *pstruMsg; pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer; if((pstruMsg->MsgCode < ZC_CODE_OTA_BEGIN)||(pstruMsg->MsgCode > ZC_CODE_OTA_END)) { pstruContoller->u8SendMoudleMsgId = pstruMsg->MsgId; /*start send timer*/ pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDMOUDLE, PCT_TIMER_INTERVAL_SENDMOUDLE, &pstruContoller->u8SendMoudleTimer); } /*Send to Moudle*/ pstruContoller->pstruMoudleFun->pfunSendToMoudle((u8*)pstruMsg, pstruBuffer->u32Len); /*restart heart timer*/ if (PCT_TIMER_INVAILD != pstruContoller->u8HeartTimer) { TIMER_StopTimer(pstruContoller->u8HeartTimer); } pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDHEART, PCT_TIMER_INTERVAL_HEART, &pstruContoller->u8HeartTimer); }
/************************************************* * 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_HandleMoudleMsg * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_HandleMoudleMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer) { ZC_MessageHead *pstruMsg; pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer; /*Send to Moudle*/ pstruContoller->pstruMoudleFun->pfunSendToMoudle((u8*)pstruMsg, pstruBuffer->u32Len); /*start send timer*/ pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDMOUDLE, PCT_TIMER_INTERVAL_SENDMOUDLE, &pstruContoller->u8SendMoudleTimer); /*copy buffer, prepare for retx*/ memcpy((u8*)g_struRetxBuffer.u8MsgBuffer, pstruBuffer->u8MsgBuffer, pstruBuffer->u32Len); g_struRetxBuffer.u32Len = pstruBuffer->u32Len; g_struRetxBuffer.u8Status = MSG_BUFFER_FULL; pstruContoller->pu8SendMoudleBuffer = (u8*)&g_struRetxBuffer; pstruContoller->u8ReSendMoudleNum = 0; /*restart heart timer*/ if (PCT_TIMER_INVAILD != pstruContoller->u8HeartTimer) { TIMER_StopTimer(pstruContoller->u8HeartTimer); } 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); }
/************************************************* * Function: PCT_DelCheckCloudAckTimer * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_DelCheckCloudAckTimer(void) { if (PCT_TIMER_INVAILD != g_struProtocolController.u8CloudAckTimer) { TIMER_StopTimer(g_struProtocolController.u8CloudAckTimer); g_struProtocolController.u8CloudAckTimer = PCT_TIMER_INVAILD; g_struProtocolController.u32CloudNotAckNum = 0; } return; }
void PCT_HandleMoudleMsg(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer) { ZC_MessageHead *pstruMsg; pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer; //u8 *u8Buf; //u8 u8Buf1[7] = {0xAA,0x83,0x01,0x01,0xaf,0x01,0x55}; /* ¿ª·äÃùÆ÷ */ //u8 u8Buf2[7] = {0xAA,0x84,0x01,0x01,0x30,0x01,0x55}; /* ¿ªµÆ */ /*start send timer*/ pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDMOUDLE, PCT_TIMER_INTERVAL_SENDMOUDLE, &pstruContoller->u8SendMoudleTimer); /*copy buffer, prepare for retx*/ memcpy((u8*)g_struRetxBuffer.u8MsgBuffer, pstruBuffer->u8MsgBuffer, pstruBuffer->u32Len); g_struRetxBuffer.u32Len = pstruBuffer->u32Len; g_struRetxBuffer.u8Status = MSG_BUFFER_FULL; pstruContoller->pu8SendMoudleBuffer = (u8*)&g_struRetxBuffer; pstruContoller->u8ReSendMoudleNum = 0; /*Send to Moudle*/ pstruContoller->pstruMoudleFun->pfunSendToMoudle((u8*)pstruMsg, pstruBuffer->u32Len); /*restart heart timer*/ if (PCT_TIMER_INVAILD != pstruContoller->u8HeartTimer) { TIMER_StopTimer(pstruContoller->u8HeartTimer); } pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_SENDHEART, PCT_TIMER_INTERVAL_HEART, &pstruContoller->u8HeartTimer); #if 0 if (0x03 == pstruBuffer->u8MsgBuffer[9] && 0x01 == pstruBuffer->u8MsgBuffer[11]) { //u8Buf = u8Buf2; custom_log("open light"); UartProcess(u8Buf1, 7); } #if 0 else if (0x04 == pstruBuffer->u8MsgBuffer[9] && 0x00 == pstruBuffer->u8MsgBuffer[11]) { //u8Buf = u8Buf1; custom_log("close light"); UartProcess(u8Buf1, 7); } #endif #endif }
/************************************************* * 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_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; }
/************************************************* * Function: PCT_HandleMoudleEvent * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_HandleMoudleEvent(u8 *pu8Msg, u16 u16DataLen) { ZC_SecHead struHead; ZC_MessageHead *pstruMsg = (ZC_MessageHead *)pu8Msg; if ((pstruMsg->MsgCode < ZC_CODE_REPORT_BASE)&& (pstruMsg->MsgCode > ZC_CODE_OTA_CONFIRM)) { TIMER_StopTimer(g_struProtocolController.u8SendMoudleTimer); g_struProtocolController.u8SendMoudleTimer = PCT_TIMER_INVAILD; } struHead.u8SecType = ZC_SEC_ALG_AES; struHead.u16TotalMsg = ZC_HTONS(u16DataLen); (void)PCT_SendMsgToCloud(&struHead, pu8Msg); return; }
/************************************************* * Function: MX_timer_callback * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void MX_timer_callback(void* arg) { u8 i =0; (void )arg; for(i=0;i<ZC_TIMER_MAX_NUM;i++) { if(g_struMxTimer[i].u8ValidFlag) { if(g_struMxTimer[i].u32Interval<=g_struMxTimerCount[i]++) { g_struMxTimerCount[i]=0; TIMER_TimeoutAction(i); TIMER_StopTimer(i); } } } }
/************************************************* * 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_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; }
/************************************************* * Function: PCT_HandleMoudleEvent * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_HandleMoudleEvent(u8 *pu8Msg, u16 u16DataLen) { MSG_Buffer *pstruBuffer; ZC_SecHead struHead; if (PCT_TIMER_INVAILD != g_struProtocolController.u8SendMoudleTimer) { TIMER_StopTimer(g_struProtocolController.u8SendMoudleTimer); pstruBuffer = (MSG_Buffer *)g_struProtocolController.pu8SendMoudleBuffer; pstruBuffer->u32Len = 0; pstruBuffer->u8Status = MSG_BUFFER_IDLE; g_struProtocolController.u8SendMoudleTimer = PCT_TIMER_INVAILD; g_struProtocolController.u8ReSendMoudleNum = 0; } struHead.u8SecType = ZC_SEC_ALG_AES; struHead.u16TotalMsg = ZC_HTONS(u16DataLen); (void)PCT_SendMsgToCloud(&struHead, pu8Msg); return; }
/************************************************* * Function: PCT_Sleep * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_Sleep() { u32 u32Index; 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; g_struProtocolController.u8RegisterTimer = PCT_TIMER_INVAILD; PCT_SendNotifyMsg(ZC_CODE_WIFI_DISCONNECTED); //ZC_ClientSleep(); }
/************************************************* * Function: PCT_HandleMoudleEvent * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_HandleMoudleEvent(u8 *pu8Msg, u16 u16DataLen) { MSG_Buffer *pstruBuffer; ZC_SecHead struHead; ZC_MessageHead *pstruMsg = (ZC_MessageHead *)pu8Msg; if ((PCT_TIMER_INVAILD != g_struProtocolController.u8SendMoudleTimer) && (pstruMsg->MsgCode < 200)) { TIMER_StopTimer(g_struProtocolController.u8SendMoudleTimer); pstruBuffer = (MSG_Buffer *)g_struProtocolController.pu8SendMoudleBuffer; pstruBuffer->u32Len = 0; pstruBuffer->u8Status = MSG_BUFFER_IDLE; g_struProtocolController.u8SendMoudleTimer = PCT_TIMER_INVAILD; g_struProtocolController.u8ReSendMoudleNum = 0; PCT_SendEmptyMsg(((ZC_MessageHead *)pu8Msg)->MsgId, ZC_SEC_ALG_AES); } struHead.u8SecType = ZC_SEC_ALG_AES; struHead.u16TotalMsg = ZC_HTONS(u16DataLen); (void)PCT_SendMsgToCloud(&struHead, pu8Msg); 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; }