/////////////////////////////////////////// // Set the packet playback speed void EQPacket::setPlayback(int speed) { if (m_vPacket) { m_vPacket->setPlaybackSpeed(speed); } else { m_packetCapture->setPlaybackSpeed(speed); } QString string(""); if (speed == 0) string.sprintf("Playback speed set Fast as possible"); else if (speed < 0) string.sprintf("Playback paused (']' to resume)"); else string.sprintf("Playback speed set to %d", speed); emit stsMessage(string, 5000); emit resetPacket(m_client2WorldStream->packetCount(), client2world); emit resetPacket(m_world2ClientStream->packetCount(), world2client); emit resetPacket(m_client2ZoneStream->packetCount(), client2zone); emit resetPacket(m_zone2ClientStream->packetCount(), zone2client); emit playbackSpeedChanged(speed); }
/**************************************************************** * FunctionName : send_broadCastPacket * Description : Send BroadCast data. * Add by Frank Liu --2015-04-14 ****************************************************************/ void send_broadCastPacket(pgcontext pgc,uint8* ptxBuf,uint16 cmdWord) { int32 ret = 0; int32 len = 0; struct sockaddr_t addr; memset(&addr,0,sizeof(addr)); resetPacket(pgc->rtinfo.Rxbuf); addr = pgc->ls.addr; if(NULL == ptxBuf) { return; } ret = combination_broadcast_packet(pgc,ptxBuf,cmdWord); if((RET_SUCCESS == ret) && (INVALID_SOCKET != pgc->ls.udpBroadCastServerFd)) { len = ptxBuf[4] + LAN_PROTOCOL_HEAD_LEN + 1; ret = Socket_sendto(pgc->ls.udpBroadCastServerFd,ptxBuf,len,&addr,sizeof(addr)); GAgent_Printf( GAGENT_INFO,"Send BroadCast datalen=%d cmd=%04X ret :%d",len,cmdWord,ret ); } return ; }
/**************************************************************** FunctionName : GAgent_LocalSendGAgentstatus. Description : check Gagent's status whether it is update. Add by Nik.chen --2015-04-18 ****************************************************************/ void GAgent_LocalSendGAgentstatus(pgcontext pgc,uint32 dTime_s ) { uint16 GAgentStatus = 0; uint16 LastGAgentStatus = 0; resetPacket( pgc->rtinfo.Rxbuf ); if( (pgc->rtinfo.GAgentStatus) != (pgc->rtinfo.lastGAgentStatus) ) { GAgentStatus = pgc->rtinfo.GAgentStatus&LOCAL_GAGENTSTATUS_MASK; LastGAgentStatus = pgc->rtinfo.lastGAgentStatus&LOCAL_GAGENTSTATUS_MASK; GAgent_Printf( GAGENT_INFO,"GAgentStatus change, lastGAgentStatus=0x%04x, newGAgentStatus=0x%04x", LastGAgentStatus, GAgentStatus); pgc->rtinfo.lastGAgentStatus = pgc->rtinfo.GAgentStatus&LOCAL_GAGENTSTATUS_MASK; GAgentStatus = htons(GAgentStatus); memcpy((pgc->rtinfo.Rxbuf->ppayload), (uint8 *)&GAgentStatus, 2); pgc->rtinfo.Rxbuf->pend = (pgc->rtinfo.Rxbuf->ppayload)+2; pgc->rtinfo.updatestatusinterval = 0; //GAgent_Printf(GAGENT_CRITICAL,"updateGagentstatusLast time=%d", (pgc->rtinfo.send2LocalLastTime)); GAgent_LocalDataWriteP0( pgc,pgc->rtinfo.local.uart_fd, (pgc->rtinfo.Rxbuf), WIFI_STATUS2MCU ); } pgc->rtinfo.updatestatusinterval+= dTime_s; if( (pgc->rtinfo.updatestatusinterval) > LOCAL_GAGENTSTATUS_INTERVAL) { pgc->rtinfo.updatestatusinterval = 0; GAgentStatus = pgc->rtinfo.GAgentStatus&LOCAL_GAGENTSTATUS_MASK; GAgentStatus = htons(GAgentStatus); memcpy((pgc->rtinfo.Rxbuf->ppayload), (uint8 *)&GAgentStatus, 2); pgc->rtinfo.Rxbuf->pend = (pgc->rtinfo.Rxbuf->ppayload)+2; GAgent_LocalDataWriteP0( pgc,pgc->rtinfo.local.uart_fd, (pgc->rtinfo.Rxbuf), WIFI_STATUS2MCU ); } }
/**************************************************************** FunctionName : GAgent_LocalHalInit Description : init hal buf. return : NULL Add by Alex.lin --2015-04-07 ****************************************************************/ void Local_HalInit() { int totalCap = ACKBUF_LEN+ BUF_HEADLEN; int bufCap = ACKBUF_LEN; hal_ReceiveInit( ); pLocalAckbuf = (ppacket)malloc(sizeof(packet)); pLocalAckbuf->allbuf = (uint8 *)malloc( totalCap ); while( pLocalAckbuf->allbuf==NULL ) { pLocalAckbuf->allbuf = (uint8 *)malloc( totalCap ); sleep(1); } memset( pLocalAckbuf->allbuf,0,totalCap ); pLocalAckbuf->totalcap = totalCap; pLocalAckbuf->bufcap = bufCap; resetPacket( pLocalAckbuf ); }
void GAgent_LocalTick( pgcontext pgc,uint32 dTime_s ) { pgc->rtinfo.local.oneShotTimeout+=dTime_s; if( pgc->rtinfo.local.oneShotTimeout >= MCU_HEARTBEAT ) { if( pgc->rtinfo.local.timeoutCnt> 3 ) { GAgent_Printf(GAGENT_CRITICAL,"Local heartbeat time out ..."); GAgent_DevReset(); } else { pgc->rtinfo.local.oneShotTimeout = 0; pgc->rtinfo.local.timeoutCnt++; resetPacket( pgc->rtinfo.Rxbuf ); GAgent_Printf(GAGENT_CRITICAL,"Local ping..."); GAgent_LocalDataWriteP0( pgc,pgc->rtinfo.local.uart_fd, pgc->rtinfo.Rxbuf,WIFI_PING2MCU ); } } }
/**************************************************************** FunctionName : GAgent_LocalReceData Description : receive data form local io. pgc : gagent global struct. return : one packe local data length. Add by Alex.lin --2015-04-07 ****************************************************************/ int32 GAgent_Local_GetPacket( pgcontext pgc, ppacket Rxbuf ) { int32 dataLen=0; if( pgc->rtinfo.local.uart_fd<0 ) return 0; if(FD_ISSET( pgc->rtinfo.local.uart_fd,&(pgc->rtinfo.readfd)) ) { int8 ret=0; resetPacket( Rxbuf ); dataLen = hal_ReceivepOnePack( pgc->rtinfo.local.uart_fd,Rxbuf->phead ); ret = Local_DataValidityCheck( pgc->rtinfo.local.uart_fd,Rxbuf,dataLen ); if( ret!=0 ) { dataLen=0; } pgc->rtinfo.local.timeoutCnt=0; } /* if( dataLen>0 ) GAgent_DebugPacket(Rxbuf->phead, dataLen); */ return dataLen; }
/**************************************************************** FunctionName : GAgent_LocalDataWriteP0 Description : send p0 to local io and add 0x55 after 0xff auto. cmd : MCU_CTRL_CMD or WIFI_STATUS2MCU return : 0-ok other -error Add by Alex.lin --2015-04-07 ****************************************************************/ int32 GAgent_LocalDataWriteP0( pgcontext pgc,int32 fd,ppacket pTxBuf,uint8 cmd ) { int8 ret =0; uint16 datalen = 0; uint16 flag = 0; uint16 sendLen = 0; /* head(0xffff)| len(2B) | cmd(1B) | sn(1B) | flag(2B) | payload(xB) | checksum(1B) */ pTxBuf->phead = pTxBuf->ppayload - 8; pTxBuf->phead[0] = MCU_HDR_FF; pTxBuf->phead[1] = MCU_HDR_FF; datalen = pTxBuf->pend - pTxBuf->ppayload + 5; //p0 + cmd + sn + flag + checksum *(uint16 *)(pTxBuf->phead + 2) = htons(datalen); pTxBuf->phead[4] = cmd; pTxBuf->phead[5] = GAgent_NewSN(); *(uint16 *)(pTxBuf->phead + 6) = htons(flag); *( pTxBuf->pend ) = GAgent_SetCheckSum(pTxBuf->phead, (pTxBuf->pend)-(pTxBuf->phead) ); pTxBuf->pend += 1; /* add 1 Byte of checksum */ sendLen = (pTxBuf->pend) - (pTxBuf->phead); sendLen = Local_DataAdapter( (pTxBuf->phead)+2,( (pTxBuf->pend) ) - ( (pTxBuf->phead)+2 ) ); Local_SendData( fd, pTxBuf->phead,sendLen ); if(GAgent_CheckAck( fd,pgc,pTxBuf->phead,sendLen,pLocalAckbuf,GAgent_GetDevTime_MS())==0) { GAgent_Printf( GAGENT_INFO,"%s %d GAgent_CheckAck OK",__FUNCTION__,__LINE__ ); ret =0; } else { GAgent_Printf( GAGENT_INFO,"%s %d GAgent_CheckAck Fail",__FUNCTION__,__LINE__ ); ret=1; } resetPacket( pLocalAckbuf ); return ret; }
/**************************************************************** FunctionName : Cloud_M2MDataHandle. Description : Receive cloud business data . xpg : global context. Rxbuf : global buf struct. buflen : receive max len. return : >0 have business data,and need to handle. other,no business data. Add by Alex.lin --2015-03-10 ****************************************************************/ int32 Cloud_M2MDataHandle( pgcontext pgc,ppacket pbuf /*, ppacket poutBuf*/, uint32 buflen) { uint32 dTime=0,ret=0,dataLen=0; uint32 packetLen=0; pgcontext pGlobalVar=NULL; pgconfig pConfigData=NULL; int8 *username=NULL; int8 *password=NULL; uint8* pMqttBuf=NULL; fd_set readfd; int32 mqtt_fd=0; uint16 mqttstatus=0; uint8 mqttpackType=0; pConfigData = &(pgc->gc); pGlobalVar = pgc; pMqttBuf = pbuf->phead; mqttstatus = pGlobalVar->rtinfo.waninfo.mqttstatus; mqtt_fd = pGlobalVar->rtinfo.waninfo.m2m_socketid; readfd = pGlobalVar->rtinfo.readfd; username = pConfigData->DID; password = pConfigData->wifipasscode; if( strlen(pConfigData->m2m_ip)==0 ) { //GAgent_Printf( GAGENT_INFO,"M2M IP =0 IP TIME 1 %d 2%d ",pgc->rtinfo.waninfo.RefreshIPLastTime,pgc->rtinfo.waninfo.RefreshIPTime); return 0; } if( MQTT_STATUS_START==mqttstatus ) { GAgent_Printf(GAGENT_INFO,"Req to connect m2m !"); GAgent_Printf(GAGENT_INFO,"username: %s password: %s",username,password); Cloud_ReqConnect( pgc,username,password ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGIN ); GAgent_Printf(GAGENT_INFO," MQTT_STATUS_START "); pgc->rtinfo.waninfo.send2MqttLastTime = GAgent_GetDevTime_MS(); return 0; } dTime = abs( GAgent_GetDevTime_MS()-pGlobalVar->rtinfo.waninfo.send2MqttLastTime ); if( FD_ISSET( mqtt_fd,&readfd )||( mqttstatus!=MQTT_STATUS_RUNNING && dTime>GAGENT_MQTT_TIMEOUT)) { if( FD_ISSET( mqtt_fd,&readfd ) ) { GAgent_Printf(GAGENT_DEBUG,"Data form M2M!!!"); resetPacket( pbuf ); packetLen = MQTT_readPacket(mqtt_fd,pbuf,GAGENT_BUF_LEN ); if( packetLen==-1 ) { mqtt_fd=-1; pGlobalVar->rtinfo.waninfo.m2m_socketid=-1; GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_START ); GAgent_Printf(GAGENT_DEBUG,"MQTT fd was closed!!"); GAgent_Printf(GAGENT_DEBUG,"GAgent go to MQTT_STATUS_START"); return -1; } else if(packetLen>0) { mqttpackType = MQTTParseMessageType( pMqttBuf ); GAgent_Printf( GAGENT_DEBUG,"MQTT message type %d",mqttpackType ); } else { return -1; } } /*create mqtt connect to m2m.*/ if( MQTT_STATUS_RUNNING!=mqttstatus && (MQTT_MSG_CONNACK==mqttpackType||MQTT_MSG_SUBACK==mqttpackType||dTime>GAGENT_MQTT_TIMEOUT)) { switch( mqttstatus) { case MQTT_STATUS_RES_LOGIN: ret = Cloud_ResConnect( pMqttBuf ); if( ret!=0 ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_REQ_LOGIN Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf(GAGENT_DEBUG,"MQTT req connetc m2m again!"); Cloud_ReqConnect( pgc,username,password ); } } else { GAgent_Printf(GAGENT_DEBUG,"GAgent do req connect m2m OK !"); GAgent_Printf(GAGENT_DEBUG,"Go to MQTT_STATUS_REQ_LOGINTOPIC1. "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC1 ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGINTOPIC1 ); } break; case MQTT_STATUS_RES_LOGINTOPIC1: ret = Cloud_ResSubTopic(pMqttBuf,pgc->rtinfo.waninfo.mqttMsgsubid ); if( 0!=ret ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_RES_LOGINTOPIC1 Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf( GAGENT_DEBUG,"GAgent req sub LOGINTOPIC1 again "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC1 ); } } else { GAgent_Printf(GAGENT_DEBUG,"Go to MQTT_STATUS_RES_LOGINTOPIC2. "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC2 ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGINTOPIC2 ); } break; case MQTT_STATUS_RES_LOGINTOPIC2: ret = Cloud_ResSubTopic(pMqttBuf,pgc->rtinfo.waninfo.mqttMsgsubid ); if( 0 != ret ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_RES_LOGINTOPIC2 Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf( GAGENT_INFO,"GAgent req sub LOGINTOPIC2 again."); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC2 ); } } else { GAgent_Printf(GAGENT_DEBUG," Go to MQTT_STATUS_RES_LOGINTOPIC3. "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC3 ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGINTOPIC3 ); } break; case MQTT_STATUS_RES_LOGINTOPIC3: ret = Cloud_ResSubTopic(pMqttBuf,pgc->rtinfo.waninfo.mqttMsgsubid ); if( ret != 0 ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_RES_LOGINTOPIC3 Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf(GAGENT_DEBUG,"GAgent req sub LOGINTOPIC3 again." ); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC3 ); } } else { GAgent_Printf(GAGENT_CRITICAL,"GAgent Cloud Working..."); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RUNNING ); GAgent_SetWiFiStatus( pgc,WIFI_CLOUD_STATUS,0 ); } break; default: break; } pgc->rtinfo.waninfo.send2MqttLastTime = GAgent_GetDevTime_MS(); } else if( packetLen>0 && ( mqttstatus == MQTT_STATUS_RUNNING ) ) { int varlen=0,p0datalen=0; switch( mqttpackType ) { case MQTT_MSG_PINGRESP: pgc->rtinfo.waninfo.cloudPingTime=0; GAgent_Printf(GAGENT_INFO,"GAgent MQTT Pong ... \r\n"); break; case MQTT_MSG_PUBLISH: dataLen = Mqtt_DispatchPublishPacket( pgc,pMqttBuf,packetLen ); if( dataLen>0 ) { pbuf->type = SetPacketType( pbuf->type,CLOUD_DATA_IN,1 ); ParsePacket( pbuf ); GAgent_Printf(GAGENT_INFO,"%s %d type : %04X len :%d",__FUNCTION__,__LINE__,pbuf->type,dataLen ); } break; default: GAgent_Printf(GAGENT_WARNING," data form m2m but msg type is %d",mqttpackType ); break; } } } return dataLen; }
/**************************************************************** FunctionName : GAgent_LanTick. Description : check clients whether it is timeout. Add by Will.zhou --2015-03-10 ****************************************************************/ void GAgent_LanTick( pgcontext pgc,uint32 dTime_s ) { int32 i; uint16 GAgentStatus = 0; uint32 GAgentConStatus = 0; uint8 *ptxBuf = NULL; if(pgc->mcu.passcodeTimeout > 0 && ((pgc->rtinfo.GAgentStatus & WIFI_MODE_BINDING) == WIFI_MODE_BINDING)) { pgc->mcu.passcodeTimeout--; if(pgc->mcu.passcodeTimeout==0) GAgent_SetWiFiStatus( pgc,WIFI_MODE_BINDING,0); } for(i = 0; i < LAN_TCPCLIENT_MAX; i++) { if(pgc->ls.tcpClient[i].fd > 0) { if( pgc->ls.tcpClient[i].timeout <= dTime_s ) { close(pgc->ls.tcpClient[i].fd); pgc->ls.tcpClient[i].fd = -1; if( LAN_CLIENT_LOGIN_SUCCESS == pgc->ls.tcpClient[i].isLogin) { if(pgc->ls.tcpClientNums > 0) pgc->ls.tcpClientNums--; if(0 == (pgc->ls.tcpClientNums + pgc->rtinfo.waninfo.wanclient_num)) { GAgent_SetWiFiStatus( pgc,WIFI_CLIENT_ON,0 ); } } } else { pgc->ls.tcpClient[i].timeout -= dTime_s; } } } GAgentStatus = pgc->rtinfo.GAgentStatus; GAgentConStatus = pgc->gc.flag; if((GAgentStatus&WIFI_STATION_CONNECTED) == WIFI_STATION_CONNECTED) { if(pgc->ls.onboardingBroadCastTime > 0) { if( pgc->ls.udpBroadCastServerFd<=0 ) { if( pgc->ls.udp3rdCloudFd>0 ) { close( pgc->ls.udp3rdCloudFd ); pgc->ls.udp3rdCloudFd = INVALID_SOCKET; } CreateUDPBroadCastServer( pgc ); } pgc->ls.onboardingBroadCastTime--; resetPacket(pgc->rtinfo.Rxbuf); ptxBuf = pgc->rtinfo.Rxbuf->phead; if(pgc->rtinfo.firstStartUp) { GAgent_Printf( GAGENT_INFO,"UDP BC firstStartUp..."); send_broadCastPacket(pgc,ptxBuf,GAGENT_LAN_CMD_STARTUP_BROADCAST); } if((GAgentConStatus & XPG_CFG_FLAG_CONFIG) == XPG_CFG_FLAG_CONFIG) { GAgent_Printf( GAGENT_INFO,"UDP BC Config success..."); send_broadCastPacket(pgc,ptxBuf,GAGENT_LAN_CMD_AIR_BROADCAST); } if( 0==pgc->ls.onboardingBroadCastTime ) { pgc->rtinfo.firstStartUp = 0; GAgentConStatus &= (~XPG_CFG_FLAG_CONFIG); pgc->gc.flag = GAgentConStatus; DestroyUDPBroadCastServer(pgc); if( 1==pgc->rtinfo.waninfo.Cloud3Flag ) { if( 0==strcmp(pgc->gc.cloud3info.cloud3Name,"jd" ) ) { if( pgc->ls.udp3rdCloudFd <=0 ) { GAgent_Printf( GAGENT_DEBUG,"GAgent will Open JD discover protocol."); GAgent_Printf( GAGENT_DEBUG,"3rdCloud Name:%s",pgc->gc.cloud3info.cloud3Name); GAgent_Printf( GAGENT_DEBUG,"3rdCloud UUID:%s",pgc->gc.cloud3info.jdinfo.product_uuid ); pgc->ls.udp3rdCloudFd = Socket_CreateUDPServer_JD( LAN_UDP_SERVER_PORT_JD ); } } } GAgent_DevSaveConfigData( &(pgc->gc) ); } } } }
/**************************************************************** FunctionName : GAgent_LanTick. Description : check clients whether it is timeout. Add by Will.zhou --2015-03-10 ****************************************************************/ void GAgent_LanTick( pgcontext pgc,uint32 dTime_s ) { //uint32 cTime=0,dTime=0; int32 i; //static uint32 preTime = 0; uint16 GAgentStatus = 0; uint32 GAgentConStatus = 0; uint8 *ptxBuf = NULL; if(pgc->mcu.passcodeTimeout > 0 && ((pgc->rtinfo.GAgentStatus & WIFI_MODE_BINDING) == WIFI_MODE_BINDING)) { pgc->mcu.passcodeTimeout--; if(pgc->mcu.passcodeTimeout==0) GAgent_SetWiFiStatus( pgc,WIFI_MODE_BINDING,0); } for(i = 0; i < LAN_TCPCLIENT_MAX; i++) { if(pgc->ls.tcpClient[i].fd > 0) { if( pgc->ls.tcpClient[i].timeout <= dTime_s ) { close(pgc->ls.tcpClient[i].fd); pgc->ls.tcpClient[i].fd = -1; if( LAN_CLIENT_LOGIN_SUCCESS == pgc->ls.tcpClient[i].isLogin) { if(pgc->ls.tcpClientNums > 0) pgc->ls.tcpClientNums--; if(0 == (pgc->ls.tcpClientNums + pgc->rtinfo.waninfo.wanclient_num)) { GAgent_SetWiFiStatus( pgc,WIFI_CLIENT_ON,0 ); } } } else { pgc->ls.tcpClient[i].timeout -= dTime_s; } } } GAgentStatus = pgc->rtinfo.GAgentStatus; GAgentConStatus = pgc->gc.flag; if((GAgentStatus&WIFI_STATION_CONNECTED) == WIFI_STATION_CONNECTED) { if((GAgentConStatus & XPG_CFG_FLAG_CONFIG) == XPG_CFG_FLAG_CONFIG) { if(pgc->ls.onboardingBroadCastTime > 0) { resetPacket(pgc->rtinfo.Txbuf); ptxBuf = pgc->rtinfo.Txbuf->phead; send_broadCastPacket(pgc,ptxBuf,GAGENT_LAN_CMD_AIR_BROADCAST); pgc->ls.onboardingBroadCastTime--; } else { GAgentConStatus &= (~XPG_CFG_FLAG_CONFIG); pgc->gc.flag = GAgentConStatus; DestroyUDPBroadCastServer(pgc); GAgent_DevSaveConfigData( &(pgc->gc) ); } } if(pgc->rtinfo.firstStartUp) { if(pgc->ls.startupBroadCastTime > 0) { resetPacket(pgc->rtinfo.Txbuf); ptxBuf = pgc->rtinfo.Txbuf->phead; send_broadCastPacket(pgc,ptxBuf,GAGENT_LAN_CMD_STARTUP_BROADCAST); pgc->ls.startupBroadCastTime--; } else { pgc->rtinfo.firstStartUp = 0; DestroyUDPBroadCastServer(pgc); } } } }
/**************************************************************** * * function : gagent do cloud config. * cloudstatus : gagent cloud status. * return : 0 successful other fail. * Add by Alex.lin --2015-02-28 ****************************************************************/ uint32 Cloud_ConfigDataHandle( pgcontext pgc /*int32 cloudstatus*/ ) { int32 dTime=0; int32 ret =0; int32 respondCode=0; int32 cloudstatus = 0; pgcontext pGlobalVar=NULL; pgconfig pConfigData=NULL; uint16 GAgentStatus = 0; int8 *pDeviceID=NULL; int8 timeoutflag = 0; uint8 *pCloudConfiRxbuf = NULL; resetPacket(pgc->rtinfo.Txbuf); pCloudConfiRxbuf = pgc->rtinfo.Txbuf->phead; fd_set readfd; int32 http_fd; pConfigData = &(pgc->gc); pGlobalVar = pgc; cloudstatus = pgc->rtinfo.waninfo.CloudStatus; GAgentStatus = pgc->rtinfo.GAgentStatus; if((GAgentStatus&WIFI_STATION_CONNECTED) != WIFI_STATION_CONNECTED) { return 1 ; } if(strlen(pgc->gc.GServer_ip) > IP_LEN_MAX || strlen(pgc->gc.GServer_ip) < IP_LEN_MIN) { //GAgent_Printf(GAGENT_WARNING,"GServer IP is illegal!!"); return 1; } if(CLOUD_CONFIG_OK == cloudstatus) { if(pGlobalVar->rtinfo.waninfo.http_socketid > 0) { GAgent_Printf( GAGENT_CRITICAL,"http config ok ,and close the socket."); close( pGlobalVar->rtinfo.waninfo.http_socketid ); pGlobalVar->rtinfo.waninfo.http_socketid = INVALID_SOCKET; } pgc->rtinfo.waninfo.httpCloudPingTime = 0; pgc->rtinfo.waninfo.ReConnectHttpTime = GAGENT_HTTP_TIMEOUT; return 1; } pDeviceID = pConfigData->DID; http_fd = pGlobalVar->rtinfo.waninfo.http_socketid; readfd = pGlobalVar->rtinfo.readfd; if(CLOUD_INIT == cloudstatus) { if(strlen(pDeviceID) == (DID_LEN - 2))/*had did*/ { GAgent_Printf(GAGENT_INFO,"Had did !!!!\r\n go to Provision" ); ret = Cloud_ReqProvision( pgc ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_PROVISION ); } else { GAgent_Printf( GAGENT_INFO,"Need to get did!!!" ); GAgent_SetDeviceID( pgc,NULL );/*clean did*/ ret = Cloud_ReqRegister( pgc ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_GET_DID ); } return 0; } dTime = abs(GAgent_GetDevTime_S()- pGlobalVar->rtinfo.waninfo.send2HttpLastTime); if(FD_ISSET( http_fd,&readfd ) || ((cloudstatus != CLOUD_CONFIG_OK) && (dTime > pgc->rtinfo.waninfo.ReConnectHttpTime))) { GAgent_Printf(GAGENT_DEBUG,"HTTP Data from Gserver!%d", 2); if(dTime > pgc->rtinfo.waninfo.ReConnectHttpTime) { if(pGlobalVar->rtinfo.waninfo.http_socketid > 0) { close(pGlobalVar->rtinfo.waninfo.http_socketid); pGlobalVar->rtinfo.waninfo.http_socketid = INVALID_SOCKET; } respondCode = -1; } else { ret = Cloud_ReadGServerConfigData( pgc,pGlobalVar->rtinfo.waninfo.http_socketid,pCloudConfiRxbuf,1024 ); if(ret <= 0) { if(pGlobalVar->rtinfo.waninfo.http_socketid > 0) { close(pGlobalVar->rtinfo.waninfo.http_socketid); pGlobalVar->rtinfo.waninfo.http_socketid = INVALID_SOCKET; GAgent_SetGServerSocket( pgc,pGlobalVar->rtinfo.waninfo.http_socketid ); } respondCode = -1; } else { respondCode = Http_Response_Code( pCloudConfiRxbuf ); } } GAgent_Printf(GAGENT_INFO,"http read ret:%d cloudStatus : %d Response code: %d",ret,cloudstatus,respondCode ); switch( cloudstatus ) { case CLOUD_RES_GET_DID: ret = Cloud_ResRegister( pCloudConfiRxbuf,ret,pDeviceID,respondCode ); if(RET_SUCCESS != ret)/* can't got the did */ { if(dTime > pgc->rtinfo.waninfo.ReConnectHttpTime) { timeoutflag = 1; GAgent_Printf(GAGENT_ERROR,"res register fail: %s %d",__FUNCTION__,__LINE__ ); GAgent_Printf(GAGENT_ERROR,"go to req register Device id again."); ret = Cloud_ReqRegister( pgc ); } } else { pgc->rtinfo.waninfo.ReConnectHttpTime = GAGENT_HTTP_TIMEOUT; pgc->rtinfo.waninfo.httpCloudPingTime = 0; pgc->rtinfo.waninfo.firstConnectHttpTime = GAgent_GetDevTime_S(); GAgent_SetDeviceID( pgc,pDeviceID ); GAgent_DevGetConfigData( &(pgc->gc) ); GAgent_Printf( GAGENT_DEBUG,"Register got did :%s len=%d",pgc->gc.DID,strlen(pgc->gc.DID) ); GAgent_Printf( GAGENT_DEBUG,"GAgent go to Provision!!!"); ret = Cloud_ReqProvision( pgc ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_PROVISION ); } break; case CLOUD_RES_PROVISION: pGlobalVar->rtinfo.waninfo.Cloud3Flag = Http_Get3rdCloudInfo( pConfigData->cloud3info.cloud3Name,pConfigData->cloud3info.jdinfo.product_uuid , pCloudConfiRxbuf ); /* have 3rd cloud info need save to falsh */ if( pGlobalVar->rtinfo.waninfo.Cloud3Flag == 1 ) { GAgent_Printf(GAGENT_INFO,"3rd cloud name:%s",pConfigData->cloud3info.cloud3Name ); GAgent_Printf(GAGENT_INFO,"3re cloud UUID: %s",pConfigData->cloud3info.jdinfo.product_uuid); GAgent_DevSaveConfigData( pConfigData ); } ret = Cloud_ResProvision( pGlobalVar->minfo.m2m_SERVER , &pGlobalVar->minfo.m2m_Port,pCloudConfiRxbuf,respondCode); if( ret!=0 ) { if(dTime > pgc->rtinfo.waninfo.ReConnectHttpTime) { timeoutflag = 1; GAgent_Printf(GAGENT_WARNING,"Provision res fail ret=%d.", ret ); GAgent_Printf(GAGENT_WARNING,"go to provision again."); ret = Cloud_ReqProvision( pgc ); } else { ret = Cloud_ReqGetFid( pgc,OTATYPE_WIFI ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_GET_TARGET_FID ); } } else { pgc->rtinfo.waninfo.ReConnectHttpTime = GAGENT_HTTP_TIMEOUT; pgc->rtinfo.waninfo.httpCloudPingTime = 0; pgc->rtinfo.waninfo.firstConnectHttpTime = GAgent_GetDevTime_S(); //login to m2m. GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_START ); ret = Cloud_ReqGetFid( pgc,OTATYPE_WIFI ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_GET_TARGET_FID ); GAgent_Printf(GAGENT_INFO,"Provision OK!"); GAgent_Printf(GAGENT_INFO,"M2M host:%s port:%d",pGlobalVar->minfo.m2m_SERVER,pGlobalVar->minfo.m2m_Port); GAgent_Printf(GAGENT_INFO,"GAgent go to update OTA info and login M2M !"); } break; case CLOUD_RES_GET_TARGET_FID: { /* 鑾峰彇OTA淇℃伅閿欒杩涘叆provision 鎴愬姛鍒欒繘琛孫TA. */ int8 *download_url = NULL; int8 disableDIDflag=0; download_url = (int8 *)malloc(256); if(NULL == download_url) { GAgent_Printf(GAGENT_WARNING, "ota malloc fail!go to provison"); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_PROVISION ); break; } ret = Cloud_ResGetFid( download_url ,pGlobalVar->gc.FirmwareVer ,pCloudConfiRxbuf,respondCode ); if( RET_SUCCESS != ret ) { if(dTime > pgc->rtinfo.waninfo.ReConnectHttpTime) { timeoutflag = 1; ret = Cloud_ReqGetFid( pgc,OTATYPE_WIFI ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_GET_TARGET_FID ); GAgent_Printf( GAGENT_WARNING,"GAgent get OTA info Timeout do it again! "); } else { GAgent_Printf( GAGENT_WARNING,"GAgent get OTA respondCode:%d,go to check disaable Device!",respondCode ); disableDIDflag=1; } } else { pgc->rtinfo.waninfo.ReConnectHttpTime = GAGENT_HTTP_TIMEOUT; pgc->rtinfo.waninfo.httpCloudPingTime = 0; pgc->rtinfo.waninfo.firstConnectHttpTime = GAgent_GetDevTime_S(); ret = Cloud_isNeedOTA( NULL ); if( 0==ret ) { GAgent_Cloud_OTAByUrl( http_fd,download_url ); GAgent_Printf(GAGENT_INFO," CLOUD_RES_GET_TARGET_FID OK!!"); GAgent_Printf(GAGENT_INFO,"url:%s",download_url); } else { disableDIDflag=1; } } if( 1==disableDIDflag ) { if( 1==GAgent_IsNeedDisableDID( pgc ) ) { GAgent_Printf(GAGENT_INFO,"Need to Disable Device ID!"); ret = Cloud_ReqDisable( pgc ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_DISABLE_DID ); break; } } GAgent_SetCloudConfigStatus ( pgc,CLOUD_CONFIG_OK ); free(download_url); break; } case CLOUD_RES_DISABLE_DID: ret = Cloud_ResDisable( respondCode ); if(ret!=0) { if(dTime > pgc->rtinfo.waninfo.ReConnectHttpTime) { timeoutflag = 1; GAgent_Printf(GAGENT_WARNING,"Disable Device ID Fail."); } else { GAgent_SetCloudConfigStatus ( pgc,CLOUD_CONFIG_OK ); } } else { pgc->rtinfo.waninfo.ReConnectHttpTime = GAGENT_HTTP_TIMEOUT; pgc->rtinfo.waninfo.httpCloudPingTime = 0; pgc->rtinfo.waninfo.firstConnectHttpTime = GAgent_GetDevTime_S(); GAgent_Printf(GAGENT_INFO,"Disable Device ID OK!"); GAgent_SetOldDeviceID( pgc,NULL,NULL,0 ); GAgent_SetCloudConfigStatus ( pgc,CLOUD_CONFIG_OK ); } break; case CLOUD_RES_POST_JD_INFO: ret = Cloud_JD_Post_ResFeed_Key( pgc,respondCode ); if( ret!=0 ) { GAgent_Printf( GAGENT_WARNING," Post JD info respond fail!" ); if( dTime > pgc->rtinfo.waninfo.ReConnectHttpTime ) { timeoutflag = 1; GAgent_Printf( GAGENT_WARNING," Post JD info again"); ret = Cloud_JD_Post_ReqFeed_Key( pgc ); } } else { pgc->rtinfo.waninfo.ReConnectHttpTime = GAGENT_HTTP_TIMEOUT; pgc->rtinfo.waninfo.httpCloudPingTime = 0; pgc->rtinfo.waninfo.firstConnectHttpTime = GAgent_GetDevTime_S(); GAgent_SetCloudConfigStatus( pgc,CLOUD_CONFIG_OK ); } break; default: break; } if(timeoutflag) { pgc->rtinfo.waninfo.ReConnectHttpTime += (10 * ONE_SECOND); pgc->rtinfo.waninfo.httpCloudPingTime++; if(pgc->rtinfo.waninfo.httpCloudPingTime == 10) { ret = Cloud_ReqProvision( pgc ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_PROVISION ); pgc->rtinfo.waninfo.httpCloudPingTime = 0; } if((GAgent_GetDevTime_S()-pgc->rtinfo.waninfo.firstConnectHttpTime) >= 2 * ONE_HOUR) { GAgent_DevReset(); } } pGlobalVar->rtinfo.waninfo.send2HttpLastTime = GAgent_GetDevTime_S(); } return 0; }