/* Use this function carefully!!!!!!!!!!!!!!!!!!!! */ void GAgent_Reset( pgcontext pgc ) { GAgent_Clean_Config(pgc); sleep(2); GAgent_DevReset(); }
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 ); } } }
uint32 GAgent_LocalDataHandle( pgcontext pgc,ppacket Rxbuf,int32 RxLen /*,ppacket Txbuf*/ ) { int8 cmd=0; uint8 sn=0,checksum=0; uint8 *localRxbuf=NULL; uint32 ret = 0; uint8 configType=0; _tm tm; if( RxLen>0 ) { localRxbuf = Rxbuf->phead; cmd = localRxbuf[4]; sn = localRxbuf[5]; checksum = GAgent_SetCheckSum( localRxbuf,RxLen-1 ); if( checksum!=localRxbuf[RxLen-1] ) { GAgent_Printf( GAGENT_ERROR,"local data cmd=%02x checksum error !",cmd ); GAgent_DebugPacket(Rxbuf->phead, RxLen); return 0; } switch( cmd ) { case MCU_REPORT: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); Rxbuf->type = SetPacketType( Rxbuf->type,LOCAL_DATA_IN,1 ); ParsePacket( Rxbuf ); ret = 1; break; case MCU_CONFIG_WIFI: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); configType = localRxbuf[8]; GAgent_Config( configType,pgc ); ret = 0; break; case MCU_RESET_WIFI: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); GAgent_Clean_Config(pgc); sleep(2); GAgent_DevReset(); ret = 0; break; case WIFI_PING2MCU_ACK: pgc->rtinfo.local.timeoutCnt=0; GAgent_Printf( GAGENT_CRITICAL,"Local pong..."); ret = 0 ; break; case MCU_CTRL_CMD_ACK: ret = 0; break; case WIFI_TEST: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); GAgent_EnterTest( pgc ); ret = 0; break; case MCU_ENABLE_BIND: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); pgc->mcu.passcodeTimeout = pgc->mcu.passcodeEnableTime; GAgent_SetWiFiStatus( pgc,WIFI_MODE_BINDING,1 ); ret = 0; break; case MCU_REQ_GSERVER_TIME: tm = GAgent_GetLocalTimeForm(pgc->rtinfo.clock); *(uint16 *)(Rxbuf->ppayload) = htons(tm.year); Rxbuf->ppayload[2] = tm.month; Rxbuf->ppayload[3] = tm.day; Rxbuf->ppayload[4] = tm.hour; Rxbuf->ppayload[5] = tm.minute; Rxbuf->ppayload[6] = tm.second; Rxbuf->pend = (Rxbuf->ppayload) + 7; Local_Ack2MCUwithP0( Rxbuf, pgc->rtinfo.local.uart_fd, sn, MCU_REQ_GSERVER_TIME_ACK ); ret = 0; break; default: ret = 0; break; } //... } return ret; }
/**************************************************************** FunctionName : GAgent_LocalGetInfo Description : get localinfo like pk. return : return Add by Alex.lin --2015-04-18 ****************************************************************/ void Local_GetInfo( pgcontext pgc ) { uint8 i=0; int32 pos=0; int8 length =0; uint16 *pTime=NULL; uint16 *pplength=NULL; uint8 get_Mcu_InfoBuf[9]= { 0xff,0xff,0x00,0x05,0x01,0x01,0x00,0x00,0x07 }; //memset(&mcuAttrData[0],0,sizeof(mcuAttrData) ); GAgent_DevLED_Green(0); get_Mcu_InfoBuf[8] = GAgent_SetCheckSum( get_Mcu_InfoBuf, 8); Local_SendData( pgc->rtinfo.local.uart_fd,get_Mcu_InfoBuf, 9 ); for( i=0;i<20;i++ ) { if(GAgent_CheckAck( pgc->rtinfo.local.uart_fd,pgc,get_Mcu_InfoBuf,9,pgc->rtinfo.Rxbuf,GAgent_GetDevTime_MS())==0) { uint8 * Rxbuf=NULL; Rxbuf = pgc->rtinfo.Rxbuf->phead; pplength = (u16*)&((pgc->rtinfo.Rxbuf->phead +2)[0]); length = ntohs(*pplength); pos+=8; memcpy( pgc->mcu.protocol_ver, Rxbuf+pos, MCU_PROTOCOLVER_LEN ); pgc->mcu.protocol_ver[MCU_PROTOCOLVER_LEN] = '\0'; pos += MCU_PROTOCOLVER_LEN; memcpy( pgc->mcu.p0_ver,Rxbuf+pos, MCU_P0VER_LEN); pgc->mcu.p0_ver[MCU_P0VER_LEN] = '\0'; pos+=MCU_P0VER_LEN; memcpy( pgc->mcu.hard_ver,Rxbuf+pos,MCU_HARDVER_LEN); pgc->mcu.hard_ver[MCU_HARDVER_LEN] = '\0'; pos+=MCU_HARDVER_LEN; memcpy( pgc->mcu.soft_ver,Rxbuf+pos,MCU_SOFTVER_LEN); pgc->mcu.soft_ver[MCU_SOFTVER_LEN] = '\0'; pos+=MCU_SOFTVER_LEN; memcpy( pgc->mcu.product_key,Rxbuf+pos,PK_LEN); pgc->mcu.product_key[PK_LEN] = '\0'; pos+=PK_LEN; pTime = (u16*)&Rxbuf[pos]; pgc->mcu.passcodeEnableTime = ntohs(*pTime); pos+=2; if( length >= (pos+MCU_MCUATTR_LEN+1 - MCU_P0_LEN - MCU_CMD_LEN) ) //pos+8+1:pos + mcu_attr(8B)+checksum(1B) { memcpy( pgc->mcu.mcu_attr,Rxbuf+pos, MCU_MCUATTR_LEN); } else { memset( pgc->mcu.mcu_attr, 0, MCU_MCUATTR_LEN); } GAgent_Printf( GAGENT_INFO,"GAgent get local info ok."); GAgent_Printf( GAGENT_INFO,"MCU Protocol Vertion:%s.",pgc->mcu.protocol_ver); GAgent_Printf( GAGENT_INFO,"MCU P0 Vertion:%s.",pgc->mcu.p0_ver); GAgent_Printf( GAGENT_INFO,"MCU Hard Vertion:%s.",pgc->mcu.hard_ver); GAgent_Printf( GAGENT_INFO,"MCU Soft Vertion:%s.",pgc->mcu.soft_ver); GAgent_Printf( GAGENT_INFO,"MCU old product_key:%s.",pgc->gc.old_productkey); GAgent_Printf( GAGENT_INFO,"MCU product_key:%s.",pgc->mcu.product_key); GAgent_Printf( GAGENT_INFO,"MCU passcodeEnableTime:%d s.\r\n",pgc->mcu.passcodeEnableTime); for( i=0;i<MCU_MCUATTR_LEN;i++ ) { GAgent_Printf( GAGENT_INFO,"MCU mcu_attr[%d]= 0x%x.",i, (uint32)pgc->mcu.mcu_attr[i]); } if( strcmp( (int8 *)pgc->mcu.product_key,pgc->gc.old_productkey )!=0 ) { GAgent_UpdateInfo( pgc,pgc->mcu.product_key ); GAgent_Printf( GAGENT_INFO,"2 MCU old product_key:%s.",pgc->gc.old_productkey); } break; } } if( 20==i ) { GAgent_Printf( GAGENT_INFO," GAgent get local info fail ... "); GAgent_Printf( GAGENT_INFO," Please check your local data,and restart GAgent again !!"); GAgent_DevReset(); } }
/**************************************************************** * * 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; }