/**************************************************************** FunctionName : GAgent_Clean_Config Description : GAgent clean the device config pgc : global staruc return : NULL Add by Frank Liu --2015-05-08 ****************************************************************/ void GAgent_Clean_Config( pgcontext pgc ) { memset( pgc->gc.old_did,0,DID_LEN); memset( pgc->gc.old_wifipasscode,0,PASSCODE_MAXLEN + 1); memcpy( pgc->gc.old_did,pgc->gc.DID,DID_LEN ); memcpy( pgc->gc.old_wifipasscode,pgc->gc.wifipasscode,PASSCODE_MAXLEN + 1 ); GAgent_Printf(GAGENT_INFO,"Reset GAgent and goto Disable Device !"); Cloud_ReqDisable( pgc ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_DISABLE_DID ); memset( pgc->gc.wifipasscode,0,PASSCODE_MAXLEN + 1); memset( pgc->gc.wifi_ssid,0,SSID_LEN_MAX + 1 ); memset( pgc->gc.wifi_key,0, WIFIKEY_LEN_MAX + 1 ); memset( pgc->gc.DID,0,DID_LEN); memset( (uint8*)&(pgc->gc.cloud3info),0,sizeof( GAgent3Cloud ) ); memset( pgc->gc.GServer_ip,0,IP_LEN_MAX + 1); memset( pgc->gc.m2m_ip,0,IP_LEN_MAX + 1); make_rand(pgc->gc.wifipasscode); pgc->gc.flag &=~XPG_CFG_FLAG_CONNECTED; GAgent_DevSaveConfigData( &(pgc->gc) ); }
uint32 Cloud_JD_Post_ReqFeed_Key( pgcontext pgc ) { int32 socket = 0; pgcontext pGlobalVar=NULL; pgconfig pConfigData=NULL; pGlobalVar=pgc; pConfigData = &(pgc->gc); pGlobalVar->rtinfo.waninfo.http_socketid = Cloud_InitSocket( pGlobalVar->rtinfo.waninfo.http_socketid,pConfigData->GServer_ip,80,0 ); socket = pGlobalVar->rtinfo.waninfo.http_socketid; if( socket<=0 ) return RET_FAILED; Http_JD_Post_Feed_Key_req( socket,pConfigData->cloud3info.jdinfo.feed_id,pConfigData->cloud3info.jdinfo.access_key, pConfigData->DID,HTTP_SERVER ); pConfigData->cloud3info.jdinfo.ischanged=0; GAgent_DevSaveConfigData( pConfigData ); return 0; }
uint32 Cloud_JD_Post_ResFeed_Key( pgcontext pgc,int32 respondCode ) { int ret=0; pgconfig pConfigData=NULL; pConfigData = &(pgc->gc); if( 200 != respondCode ) return 1; if( 1 == pConfigData->cloud3info.jdinfo.ischanged ) { GAgent_Printf(GAGENT_WARNING,"jd info is changed need to post again."); pConfigData->cloud3info.jdinfo.tobeuploaded=1; ret=1; } else { GAgent_Printf(GAGENT_DEBUG,"jd info post ok."); pConfigData->cloud3info.jdinfo.tobeuploaded=0; ret=0; } GAgent_DevSaveConfigData( pConfigData ); return ret; }
/**************************************************************** * * 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; uint8 cloudConfiRxbuf[1024]={0}; int8 *pDeviceID=NULL; fd_set readfd; int32 http_fd; pConfigData = &(pgc->gc); pGlobalVar = pgc; cloudstatus = pgc->rtinfo.waninfo.CloudStatus; GAgentStatus = pgc->rtinfo.GAgentStatus; if( (GAgentStatus&WIFI_STATION_STATUS)!= WIFI_STATION_STATUS) return 1 ; if( strlen( pgc->gc.GServer_ip)>IP_LEN || strlen( pgc->gc.GServer_ip)<7 ) { GAgent_Printf( GAGENT_WARNING,"GServer IP is NULL!!"); return 1; } if( cloudstatus== CLOUD_CONFIG_OK ) { 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=-1; } return 1; } pDeviceID = pConfigData->DID; http_fd = pGlobalVar->rtinfo.waninfo.http_socketid; readfd = pGlobalVar->rtinfo.readfd; if( CLOUD_INIT == cloudstatus ) { GAgent_Printf( GAGENT_INFO,"%s %d",__FUNCTION__,__LINE__ ); if( strlen(pDeviceID)==22 )/*had did*/ { GAgent_Printf( GAGENT_INFO,"Had did !!!!" ); ret = Cloud_ReqGetFid( pgc,OTATYPE_WIFI ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_GET_TARGET_FID); } 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_MS()- pGlobalVar->rtinfo.waninfo.send2HttpLastTime ); if( FD_ISSET( http_fd,&readfd )||( (cloudstatus<CLOUD_CONFIG_OK) && (dTime>GAGENT_HTTP_TIMEOUT)) ) { GAgent_Printf(GAGENT_DEBUG,"HTTP Data from Gserver!%d", 2); ret = Cloud_ReadGServerConfigData( pgc,pGlobalVar->rtinfo.waninfo.http_socketid,cloudConfiRxbuf,1024 ); respondCode = Http_Response_Code( cloudConfiRxbuf ); GAgent_Printf(GAGENT_INFO,"http read ret:%d cloudStatus : %d",ret,cloudstatus ); GAgent_Printf(GAGENT_INFO,"http response code : %d",respondCode ); switch( cloudstatus ) { case CLOUD_RES_GET_DID: ret = Cloud_ResRegister( cloudConfiRxbuf,ret,pDeviceID,respondCode ); if( (RET_SUCCESS != ret) )/* can't got the did */ { if(dTime>GAGENT_HTTP_TIMEOUT) { 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 { 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) ); ret = Cloud_ReqGetFid( pgc,OTATYPE_WIFI ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_GET_TARGET_FID ); } break; case CLOUD_RES_GET_TARGET_FID: { /* 获取OTA信息错误进入provision 成功则进行OTA. */ int8 download_url[256]={0}; ret = Cloud_ResGetFid( download_url ,pGlobalVar->rtinfo.waninfo.firmwarever ,cloudConfiRxbuf,respondCode ); if( RET_SUCCESS != ret ) { GAgent_Printf( GAGENT_WARNING,"GAgent get OTA info fail! go to provison! "); ret = Cloud_ReqProvision( pgc ); GAgent_SetCloudConfigStatus( pgc,CLOUD_RES_PROVISION ); break; } else { 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); /* 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 , cloudConfiRxbuf ); /* 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,cloudConfiRxbuf,respondCode); if( ret!=0 ) { if(dTime>GAGENT_HTTP_TIMEOUT) { GAgent_Printf(GAGENT_WARNING,"Provision res fail ret=%d.", ret ); GAgent_Printf(GAGENT_WARNING,"go to provision again."); ret = Cloud_ReqProvision( pgc ); } } else { 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 login M2M !"); GAgent_SetCloudConfigStatus( pgc,CLOUD_CONFIG_OK ); //login to m2m. GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_START ); 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; } } break; case CLOUD_RES_DISABLE_DID: ret = Cloud_ResDisable( respondCode ); if(ret!=0) { if(dTime>GAGENT_HTTP_TIMEOUT) { GAgent_Printf(GAGENT_WARNING,"Disable Device ID Fail."); } else { GAgent_SetCloudConfigStatus ( pgc,CLOUD_CONFIG_OK ); } } else { 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>GAGENT_HTTP_TIMEOUT ) { GAgent_Printf( GAGENT_WARNING," Post JD info again"); ret = Cloud_JD_Post_ReqFeed_Key( pgc ); } } else { GAgent_SetCloudConfigStatus( pgc,CLOUD_CONFIG_OK ); } break; } pGlobalVar->rtinfo.waninfo.send2HttpLastTime = GAgent_GetDevTime_MS(); } }
int32 handleWebConfig( pgcontext pgc,int32 fd) { int32 read_len; int8 *buf_head, *buf_body, *index_ssid, *index_pass, *p; pgconfig pConfigData=NULL; pConfigData = &(pgc->gc); if(fd <= 0) return -1; buf_head = (char *)malloc(1024); if (buf_head == NULL) { return -1; } memset(buf_head, 0, 1024); read_len = read(fd, buf_head, 1024); if(read_len <= 0) { free(buf_head); return -1; } buf_body = (char *)malloc(1024); if (buf_body == NULL) { free(buf_head); return -1; } memset(buf_body, 0, 1024); if( strstr(buf_head, "web_config.cgi") == NULL) { snprintf(buf_body, 1024, "%s", "<html><body><form action=\"web_config.cgi\" method=\"get\">" "<div align=\"center\" style=\"font-size:30px; padding-top:100px;\">" "<p>[0~9],[a~z],[A~Z],[-],[_],[.]</p>" "<p>SSID:<input type=\"text\" name=\"ssid\" style=\"font-size:30px;\"/></p>" "<p>PASS:<input type=\"text\" name=\"pass\" style=\"font-size:30px;\"/></p>" "<p><input type=\"submit\" value=\"OK\" style=\"font-size:30px;\"/></p>" "</form></body></html>"); memset(buf_head, 0, 1024); snprintf(buf_head, 1024, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html;charset=gb2312\r\n" "Content-Length: %d\r\n" "Cache-Control: no-cache\r\n" "Connection: close\r\n\r\n", (int)strlen(buf_body)); send(fd, buf_head, strlen(buf_head), 0); send(fd, buf_body, strlen(buf_body), 0); } else { uint16 TempWiFiStatus = 0; //GET /web_config.cgi?fname=chensf&lname=pinelinda HTTP/1.1 index_ssid = strstr(buf_head, "ssid="); index_pass = strstr(buf_head, "pass="******"ssid="); index_pass += strlen("pass="******"web_config SSID and pass too length !" ); } GAgent_Printf( GAGENT_CRITICAL,"web_config SSID and pass !" ); memset(pConfigData->wifi_ssid, 0, SSID_LEN_MAX ); memset(pConfigData->wifi_key, 0, WIFIKEY_LEN_MAX); memcpy(pConfigData->wifi_ssid, index_ssid, strlen(index_ssid)); memcpy(pConfigData->wifi_key, index_pass, strlen(index_pass)); pConfigData->wifi_ssid[ strlen(index_ssid) ] = '\0'; pConfigData->wifi_key[ strlen(index_pass) ] = '\0'; pConfigData->flag |= XPG_CFG_FLAG_CONNECTED; pConfigData->flag |= XPG_CFG_FLAG_CONFIG; pgc->ls.onboardingBroadCastTime = SEND_UDP_DATA_TIMES; GAgent_DevSaveConfigData( pConfigData ); snprintf(buf_body, 1024, "%s", "<html><body>" "<div align=\"center\" style=\"font-size:40px; padding-top:100px;\">" "<p>Config the wifi module success</p>" "</body></html>"); memset(buf_head, 0, 1024); snprintf(buf_head, 1024, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html;charset=gb2312\r\n" "Content-Length: %d\r\n" "Cache-Control: no-cache\r\n" "Connection: close\r\n\r\n", (int)strlen(buf_body)); send(fd, buf_head, strlen(buf_head), 0); send(fd, buf_body, strlen(buf_body), 0); msleep(500); GAgent_DRVWiFi_APModeStop( pgc ); GAgent_Printf( GAGENT_INFO,"webconfig ssid:%s key:%s",pConfigData->wifi_ssid,pConfigData->wifi_key ); GAgent_Printf( GAGENT_DEBUG,"file:%s function:%s line:%d ",__FILE__,__FUNCTION__,__LINE__ ); TempWiFiStatus = GAgent_DevCheckWifiStatus( WIFI_MODE_ONBOARDING,0 ); } } free(buf_head); free(buf_body); msleep(100); return 0; }
/**************************************************************** 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) ); } } } }
int32 handleWebConfig( pgcontext pgc,int32 fd) { int32 read_len; int8 *buf_head, *buf_body, *index_ssid, *index_pass, *p; pgconfig pConfigData=NULL; pConfigData = &(pgc->gc); if(fd <= 0) return -1; buf_head = (char *)malloc(1024); if (buf_head == NULL) { return -1; } memset(buf_head, 0, 1024); read_len = read(fd, buf_head, 1024); if(read_len <= 0) { free(buf_head); return -1; } buf_body = (char *)malloc(1024); if (buf_body == NULL) { free(buf_head); return -1; } memset(buf_body, 0, 1024); if( strstr(buf_head, "web_config.cgi") == NULL) { snprintf(buf_body, 1024, "%s", "<html><body><form action=\"web_config.cgi\" method=\"get\">" "<div align=\"center\" style=\"font-size:30px; padding-top:100px;\">" "<p>[0~9],[a~z],[A~Z],[-],[_],[.]</p>" "<p>SSID:<input type=\"text\" name=\"ssid\" style=\"font-size:30px;\"/></p>" "<p>PASS:<input type=\"text\" name=\"pass\" style=\"font-size:30px;\"/></p>" "<p><input type=\"submit\" value=\"OK\" style=\"font-size:30px;\"/></p>" "</form></body></html>"); memset(buf_head, 0, 1024); snprintf(buf_head, 1024, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html;charset=gb2312\r\n" "Content-Length: %d\r\n" "Cache-Control: no-cache\r\n" "Connection: close\r\n\r\n", strlen(buf_body)); send(fd, buf_head, strlen(buf_head), 0); send(fd, buf_body, strlen(buf_body), 0); } else { //GET /web_config.cgi?fname=chensf&lname=pinelinda HTTP/1.1 index_ssid = strstr(buf_head, "ssid="); index_pass = strstr(buf_head, "pass="******"ssid="); index_pass += strlen("pass="******"web_config SSID and pass too length !" ); } GAgent_Printf( GAGENT_CRITICAL,"web_config SSID and pass !" ); memset(pConfigData->wifi_ssid, 0, SSID_LEN_MAX ); memset(pConfigData->wifi_key, 0, WIFIKEY_LEN_MAX); memcpy(pConfigData->wifi_ssid, index_ssid, strlen(index_ssid)); memcpy(pConfigData->wifi_key, index_pass, strlen(index_pass)); pConfigData->wifi_ssid[ strlen(index_ssid) ] = '\0'; pConfigData->wifi_key[ strlen(index_pass) ] = '\0'; pConfigData->flag |= XPG_CFG_FLAG_CONNECTED; pConfigData->flag |= XPG_CFG_FLAG_CONFIG; GAgent_DevSaveConfigData( pConfigData ); snprintf(buf_body, 1024, "%s", "<html><body>" "<div align=\"center\" style=\"font-size:40px; padding-top:100px;\">" "<p>Config the wifi module success</p>" "</body></html>"); memset(buf_head, 0, 1024); snprintf(buf_head, 1024, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html;charset=gb2312\r\n" "Content-Length: %d\r\n" "Cache-Control: no-cache\r\n" "Connection: close\r\n\r\n", strlen(buf_body)); send(fd, buf_head, strlen(buf_head), 0); send(fd, buf_body, strlen(buf_body), 0); pgc->rtinfo.GAgentStatus &=~ WIFI_MODE_AP; pgc->rtinfo.GAgentStatus |= GAgent_DRVWiFi_StationCustomModeStart( pgc->gc.wifi_ssid,pgc->gc.wifi_key,pgc->rtinfo.GAgentStatus ); msleep(100); } } free(buf_head); free(buf_body); msleep(100); return 0; }
/**************************************************************** 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; }