/* netWork.c 网络功能任务 */ #include "includes.h" #if 0 /* regOffResponse 注册掉线后要处理的操作 */ void regOffResponse(void){ if(app.netWork.reg==1){ wlog_warn("reg off"); } app.netWork.reg=0; app.netWork.ppp=0; app.netWork.netReayd=0; } /* regOnResponse 注册上线后要处理的操作 */ void regOnResponse(void){ if(app.netWork.reg==0) wlog_warn("reg on"); app.netWork.reg=1; } /* pppOffResponse ppp上线后要处理的操作 */ void pppOffResponse(void){ if(app.netWork.ppp==1) wlog_warn("PPPOff"); app.netWork.netReayd=0; app.netWork.ppp=0; } /* pppOnResponse ppp掉线后要处理的操作 */ void pppOnResponse(void){ if(app.netWork.ppp==0) wlog_warn("PPPOn"); app.netWork.ppp=1; } /* netifOnResponse netif上线后要处理的操作 */ void netifOnResponse(void){ if(app.netWork.netReayd==0) wlog_warn("netIfOn"); app.netWork.netReayd=1; } /* netifOffResponse netif掉线后要处理的操作 */ void netifOffResponse(void){ if(app.netWork.netReayd==1) wlog_warn("netIfOff"); app.netWork.netReayd=0; } #endif /*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(app.netWork.netReayd==0) ticketDeVote(TICKET_PT_NETWORK); else ticketVote(TICKET_PT_NETWORK); } static T_BOOL getIccidFlag=FALSE; /* MS5700_Networking MS5700平台网络任务入口 */ static void MS5700_Networking(void){ T_UINT8 info[23]; uint32_t nRet,id; 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(app.CCID, info); getIccidFlag=TRUE; } } if(FALSE==app.getImeiFlag){ memset(info,0,sizeof(info)); if(0==LSAPI_SIM_GetIMEI(info)){ wlog_info("IMEI:%s", info); app.getImeiFlag=TRUE; strncpy(app.imei, info, sizeof(app.imei)); } } if(FALSE==app.btimeFlag){ memset(app.mbtime, 0, sizeof(app.mbtime)); LSAPI_SYS_getModulebtime(app.mbtime); wlog_info("BTIME:%s",app.mbtime); app.btimeFlag=TRUE; } if(FALSE==getIccidFlag) return;//没卡 //网络处理 isNetWorkIdle(); //检测是否要重置PDP if(app.netWork.resetPDP==1){ app.netWork.resetPDP=0; extShutAppLinks(); 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");}//注册状态发生变化 app.netWork.reg=0; app.netWork.pdp=0; app.netWork.netReayd=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");}//注册状态发生变化 app.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状态发生变化 app.netWork.pdp=0; app.netWork.netReayd=0; //尝试激活PDP ls_PdpActive(); }else{//PDP已激活 if(LastnActstate != nActstate){wlog_warn("pdp on");}//PDP状态发生变化 app.netWork.pdp=1; netIf=LSAPI_NET_GET_GprsNetIf();//检测netif状态 if(false==netIf){//netif未创建 if(lastNetIf!=netIf){wlog_warn("netif off");}//netif状态发生变化 app.netWork.netReayd=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状态发生变化 app.netWork.netReayd=1; } lastNetIf=netIf; } LastnActstate=nActstate; } } LastnAttstate=nAttstate; } #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, NULL); 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); }