/************************************************* * 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_SendCloudAccessMsg3 * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void PCT_SendCloudAccessMsg3(PTC_ProtocolCon *pstruContoller) { u16 u16Len; ZC_HandShakeMsg3 struMsg3; ZC_SecHead struSechead; u8 *pu8Vesion; ZC_GetStoreInfor(ZC_GET_TYPE_VESION, &pu8Vesion); memcpy(struMsg3.RandMsg, pstruContoller->RandMsg, ZC_HS_MSG_LEN); memcpy(struMsg3.u8EqVersion, pu8Vesion, ZC_EQVERSION_LEN); struMsg3.u8WifiVerSion[0] = (u8)(ZC_MODULE_VERSION >> 24); struMsg3.u8WifiVerSion[1] = (u8)(ZC_MODULE_VERSION >> 16); struMsg3.u8WifiVerSion[2] = (u8)(ZC_MODULE_VERSION >> 8); struMsg3.u8WifiVerSion[3] = (u8)(ZC_MODULE_VERSION); struMsg3.u8WifiType = ZC_MODULE_TYPE; /*first set key recv flag*/ g_struProtocolController.u8keyRecv = PCT_KEY_RECVED; EVENT_BuildMsg(ZC_CODE_HANDSHAKE_3, 1, g_u8MsgBuildBuffer, &u16Len, (u8*)&struMsg3, sizeof(ZC_HandShakeMsg3)); struSechead.u8SecType = ZC_SEC_ALG_AES; struSechead.u16TotalMsg = ZC_HTONS(u16Len); (void)PCT_SendMsgToCloud(&struSechead, g_u8MsgBuildBuffer); pstruContoller->u8MainState = PCT_STATE_WAIT_MSG4; pstruContoller->pstruMoudleFun->pfunSetTimer(PCT_TIMER_REACCESS, PCT_TIMER_INTERVAL_RECONNECT*10, &pstruContoller->u8AccessTimer); return; }
/************************************************* * 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; }
/************************************************* * 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); }
/************************************************* * Function: ZC_SendJdQueryReq * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void ZC_SendJDQueryReq(u8 *pu8Msg, u32 u32RecvLen,struct sockaddr_in addr) { int len_udp_back; cmd_header* pCmd = NULL; ZC_SendParam struParam; cJSON* jDevice = ZC_JDPacketAnalyse(&pCmd, (char *)pu8Msg, u32RecvLen); if(jDevice == NULL) { ZC_Printf("PacketAnalyse error\n\r"); return; } cJSON *root = cJSON_CreateObject(); //注意用完必须释放内存 if(root == NULL) { ZC_Printf("cJSON_CreateObject failed"); cJSON_Delete(jDevice); return; } switch(pCmd->type) { case 1: { //int smtlk = 0; u8 DeviceId[ZC_HS_DEVICE_ID_LEN+1]; u8 *pu8DeviceId; #if 0 hfsys_nvm_read(0,(char *)&smtlk,4); if(!((hfsys_get_time() < 2000*60) && (smtlk == 1))) { smtlk = 0; hfsys_nvm_write(0,(char *)&smtlk,4); break; } #endif cJSON *pItem = cJSON_GetObjectItem(jDevice, "productuuid"); if( (strcmp((const char *)JD_PROUUID, (const char *)pItem->valuestring) )||(pItem->valuestring[0]==0)||(strcmp("0", (const char *)pItem->valuestring) )) { if(ZC_MAGIC_FLAG!=g_struZcConfigDb.struJdInfo.u32MagicFlag) { cJSON_AddStringToObject(root,"feedid", "0"); ZC_Printf("Configuration file not exists"); } else { cJSON_AddStringToObject(root,"feedid", (const char *)g_struZcConfigDb.struJdInfo.u8Feedid); ZC_Printf("Configuration file exists"); } ZC_GetStoreInfor(ZC_GET_TYPE_DEVICEID, &pu8DeviceId); memcpy(DeviceId, pu8DeviceId, ZC_HS_DEVICE_ID_LEN); DeviceId[ZC_HS_DEVICE_ID_LEN] = 0; cJSON_AddStringToObject(root,"mac", (const char *)DeviceId); cJSON_AddStringToObject(root,(const char *)"productuuid", (const char *)JD_PROUUID); int length = ZC_JDPacketBuild(pu8Msg, 2, 2, root); struParam.u8NeedPoll = 0; struParam.pu8AddrPara = (u8 *)&addr; g_struProtocolController.pstruMoudleFun->pfunSendUdpData(g_Jdfd, pu8Msg, length, &struParam); ZC_Printf("HeartBeat_OK\n"); } break; } case 3: { cJSON *pItem = cJSON_GetObjectItem(jDevice,"feedid"); ZC_StoreFeedInfo((u8 *)pItem->valuestring); cJSON_AddNumberToObject(root,"code",0); cJSON_AddStringToObject(root,"msg","write feed_id and accesskey successfully!"); len_udp_back = ZC_JDPacketBuild(pu8Msg, 2, 4, root); struParam.u8NeedPoll = 0; struParam.pu8AddrPara = (u8*)&addr; g_struProtocolController.pstruMoudleFun->pfunSendUdpData(g_Jdfd, pu8Msg, len_udp_back, &struParam); char* p = cJSON_PrintUnformatted(jDevice); ZC_Printf("WriteOK_%s",p); hfmem_free(p); break; } default: break; } cJSON_Delete(root); cJSON_Delete(jDevice); }
/************************************************* * 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; }