/************************************************* * 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_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_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_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; }
/************************************************* * 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; }
/************************************************* * 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; }