/* netWork.c 网络功能任务 */ #include "includes.h" #if 0 /* regOffResponse 注册掉线后要处理的操作 */ void regOffResponse(void){ if(talking.netWork.reg==1){ wlog_warn("reg off"); closeTalk(); } talking.netWork.reg=0; talking.netWork.ppp=0; } /* regOnResponse 注册上线后要处理的操作 */ void regOnResponse(void){ if(talking.netWork.reg==0){ talking.netWork.reg=1; wlog_warn("reg on"); } } /* pppOffResponse ppp上线后要处理的操作 */ void pppOffResponse(void){ if(talking.netWork.ppp==1){ wlog_warn("PPPOff"); closeTalk(); tcpUdpShutDown(); } talking.netWork.ppp=0; updateLineStatus(LINES_NO_LINK, S_SET); } /* pppOnResponse ppp掉线后要处理的操作 */ void pppOnResponse(void){ updateLineStatus(LINES_NO_LINK, S_CLEAR); if(talking.netWork.ppp==0) wlog_warn("PPPOn"); talking.netWork.ppp=1; } #endif /* isNetWork4G 更新4G/非4G状态后要处理的操作 */ #define _4G_RAT_VALUE 4 void isNetWork4G(T_UINT8 rat){ static uint32_t lastRat=68; if(rat == lastRat) return; lastRat=rat; if(_4G_RAT_VALUE==rat){ wlog_info("Is 4G netWork"); talking.workIn_4G=TRUE; talking.maxValue_4G=DEFAULT_TCP_HEARTTICK - (DEFAULT_TCP_HEARTTICK % paras.udpHeartTick_4G); }else{ wlog_info("Is none-4G netWork"); talking.workIn_4G=FALSE; talking.maxValue_none_4G=DEFAULT_TCP_HEARTTICK - (DEFAULT_TCP_HEARTTICK % paras.udpHeartTick_none_4G); } talking.pocTupCnt=0; } /*PDP打开与关闭*/ static uint8_t tCid = 1; static uint8_t tSim = 0; unsigned pdpCtl(bool status,uint32_t *id){ LSAPI_OSI_Event_t event = {0}; uint8_t nState; unsigned res; if(true==status) nState=1; else nState=0; res=LSAPI_NET_GprsAct(nState, tCid, tSim, &event); *id=event.id; return res; } uint32_t ls_PdpActive(void){ unsigned res = 0; uint32_t id; int act_state = 0; //char *apn = "cmnet"; //res = LSAPI_NET_SetPdpcont(nCid, "CMNET", NULL, NULL); //wlog_info("LSAPI_NET_SetPdpcont nCID=%d,nRet=%d\n", nCid, res); /* 1. active PDP begin */ res = pdpCtl(true,&id); wlog_info("LSAPI_NET_GprsAct event.id(%d),res(%d)", id,res); if (1 == res){ LSAPI_NET_GetGprsActState(&act_state); wlog_info( "LSAPI_NET_GprsAct succ:act_state(%d),cid(%d)", act_state,tCid); return 0; }else{ wlog_info("LSAPI_NET_GprsAct active fail:cid(%d)",tCid); return 1; } } void isNetWorkIdle(void){ if(talking.netWork.netReady==0) ticketDeVote(TICKET_PT_NETWORK); else ticketVote(TICKET_PT_NETWORK); } /* MS5700_Networking MS5700平台网络任务入口 */ static void MS5700_Networking(void){ T_UINT8 info[23]; uint32_t nRet,id; static T_BOOL getIccidFlag=FALSE; static int nAttstate,nActstate;//需要static static int LastnAttstate=0,LastnActstate=0; T_BOOL netIf; static T_BOOL lastNetIf=FALSE; static T_UINT16 cnt=0; static T_UINT16 regCnt=0; if(FALSE==getIccidFlag){ memset(info,0,sizeof(info)); if(0==LSAPI_SIM_GetICCID(info)){ wlog_info("ICCID:%s", info); strcpy(talking.CCID, info); getIccidFlag=TRUE; } } if(FALSE==talking.getImeiFlag){ memset(info,0,sizeof(info)); if(0==LSAPI_SIM_GetIMEI(info)){ wlog_info("IMEI:%s", info); talking.getImeiFlag=TRUE; strncpy(talking.imei, info, sizeof(talking.imei)); } } //网络处理 isNetWorkIdle(); if(FALSE==getIccidFlag) return;//没卡 //检测是否要重置PDP if(talking.netWork.resetPDP==1){ talking.netWork.resetPDP=0; pdpCtl(false,&id); wlog_warn("Disable pdp!!!"); } nRet = LSAPI_NET_GetGprsAttState(&nAttstate);//获取注册状态 if(nRet!=0){ wlog_warn("Get REG status failed"); return; } if(nAttstate!=1){//注册不成功 if(LastnAttstate!=nAttstate){wlog_warn("reg off");}//注册状态发生变化 talking.netWork.reg=0; talking.netWork.pdp=0; talking.netWork.netReady=0; wlog_info("wait reg"); if(regCnt==600){//注册10分钟还不成功 wlog_warn("!!!CFUN=0"); LSAPI_NET_CFUN(0); }else if(regCnt>=601) { regCnt=0; wlog_warn("!!!CFUN=1"); LSAPI_NET_CFUN(1); } ++regCnt; }else{//注册成功 if(LastnAttstate!=nAttstate){wlog_warn("reg on");}//注册状态发生变化 talking.netWork.reg=1; nRet = LSAPI_NET_GetGprsActState(&nActstate);//定时检测PDP状态 if(nRet!=0){//检测失败 wlog_warn("LSAPI_NET_GetGprsActState get failed:%d",nRet); }else{//检测成功 if(nActstate!=1){//PDP断开 if(LastnActstate != nActstate){wlog_warn("pdp off");}//PDP状态发生变化 talking.netWork.pdp=0; talking.netWork.netReady=0; //尝试激活PDP ls_PdpActive(); }else{//PDP已激活 if(LastnActstate != nActstate){wlog_warn("pdp on");}//PDP状态发生变化 talking.netWork.pdp=1; netIf=LSAPI_NET_GET_GprsNetIf();//检测netif状态 if(false==netIf){//netif未创建 if(lastNetIf!=netIf){wlog_warn("netif off");}//netif状态发生变化 talking.netWork.netReady=0; wlog_info("netif start create"); LSAPI_NET_NetIf_Create();//创建netif wlog_info("netif create done"); }else{//netif已创建 if(lastNetIf!=netIf){wlog_warn("netif on");}//netif状态发生变化 talking.netWork.netReady=1; } lastNetIf=netIf; } LastnActstate=nActstate; } } LastnAttstate=nAttstate; isNetWork4G(LSAPI_NET_GetRat()); //isNetWorkIdle(); } #include "lsapi_sock.h" static bool pingStatus=FALSE; void pingcallback(uint8_t flag){ switch(flag){ case LSAPI_SOCK_ICMP_DATA_IND: wlog_info("ping success"); msgToOutter("+IPING:SUCC\r\n"); break; case LSAPI_SOCK_ICMP_TIME_OUT: wlog_info("ping timeout"); msgToOutter("+IPING:TIMEOUT\r\n"); break; default: msgToOutter("+IPING:UNKNWON\r\n"); wlog_info("ping default event:%d",flag); break; } threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT); return; } char pinghost[IP_DOMAIN_SIZE+1]={0}; static void wpingTask(void *param){ wlog_info("start ping:%s",pinghost); if(LSAPI_RET_OK!=LSAPI_SOCK_Ping(pinghost, 6, 255, pingcallback)){ wlog_info("ping api error"); msgToOutter("+IPING:ERR\r\n"); pingStatus=FALSE; LSAPI_OSI_ThreadExit(); return; } LSAPI_OSI_Event_t event={}; for(;;){ LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event); if(USER_EVENT_EXIT==event.id){ pingStatus=FALSE; userExitThread("wpingTask"); break; } } } void tryToPing(char *host){ //host="www.baidu.com" int i,j; //return ; //计算'"'个数 j=0; if(host[0] != '"') goto CMD_ERR; for(i=0;i2){ goto CMD_ERR; }else if(j==2){ if(host[i+1]!=0) goto CMD_ERR; host[i]=0; } } } if(TRUE==pingStatus){ msgToOutter("+IPING:BUSY\r\n"); wlog_warn("ping is busy"); return; } //get host snprintf(pinghost, sizeof(pinghost),"%s",host+1); wlog_info("ping:%s",pinghost); if(NULL == LSAPI_OSI_ThreadCreate("wping", wpingTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, PING_THREAD_STACK, 4)){ msgToOutter("+IPING:ERR\r\n"); wlog_warn("ping thread failed"); return; } pingStatus=TRUE; msgToOutter("OK\r\n"); return; CMD_ERR: msgToOutter("+IPING:ERR\r\n"); wlog_warn("ping cmd err"); } /* ptNetWorkTask 网络任务入口操作 */ PT_THREAD (ptNetWorkTask(pt_timer_t *ptPool, struct pt *pt)){ static pt_timer_t ptTimer; PT_BEGIN(pt); while(1){ MS5700_Networking(); PTTimerStart(ptPool, &ptTimer,100);//need set to 1 seconds PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)); } PT_END(pt); }