/************************************************* * 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); } }
/************************************************* * 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); }
/************************************************* * 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); } }
/************************************************* * 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); } }
/************************************************* * Function: PCT_SetLicense * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_SetLicense(PTC_ProtocolCon *pstruContoller, MSG_Buffer *pstruBuffer) { s32 ret = 0; ZC_MessageHead *pstruMsg; u8 *pu8License; pstruMsg = (ZC_MessageHead*)pstruBuffer->u8MsgBuffer; pu8License = (u8 *)(pstruMsg + 1); ret = ZC_ConfigLicense(pu8License, ZC_HTONS(pstruMsg->Payloadlen)); if (ZC_RET_OK == ret) { PCT_SendAckToCloud(pstruMsg->MsgId); } else { PCT_SendErrorMsg(pstruMsg->MsgId, NULL, 0); } pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_REBOOT, PCT_TIMER_INTERVAL_REBOOT, &pstruContoller->u8RebootTimer); 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; }
/************************************************* * 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: PCT_SendMoudleTimeout * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_SendMoudleTimeout(PTC_ProtocolCon *pstruProtocolController) { ZC_Printf("send moudle timeout, MsgId = %d\n",pstruProtocolController->u8SendMoudleMsgId); PCT_SendErrorMsg(pstruProtocolController->u8SendMoudleMsgId, NULL, 0); pstruProtocolController->u8SendMoudleTimer = PCT_TIMER_INVAILD; }