netWork.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. netWork.c
  3. 网络功能任务
  4. */
  5. #include "includes.h"
  6. #if 0
  7. /*
  8. regOffResponse
  9. 注册掉线后要处理的操作
  10. */
  11. void regOffResponse(void){
  12. if(app.netWork.reg==1){
  13. wlog_warn("reg off");
  14. }
  15. app.netWork.reg=0;
  16. app.netWork.ppp=0;
  17. app.netWork.netReayd=0;
  18. }
  19. /*
  20. regOnResponse
  21. 注册上线后要处理的操作
  22. */
  23. void regOnResponse(void){
  24. if(app.netWork.reg==0) wlog_warn("reg on");
  25. app.netWork.reg=1;
  26. }
  27. /*
  28. pppOffResponse
  29. ppp上线后要处理的操作
  30. */
  31. void pppOffResponse(void){
  32. if(app.netWork.ppp==1) wlog_warn("PPPOff");
  33. app.netWork.netReayd=0;
  34. app.netWork.ppp=0;
  35. }
  36. /*
  37. pppOnResponse
  38. ppp掉线后要处理的操作
  39. */
  40. void pppOnResponse(void){
  41. if(app.netWork.ppp==0) wlog_warn("PPPOn");
  42. app.netWork.ppp=1;
  43. }
  44. /*
  45. netifOnResponse
  46. netif上线后要处理的操作
  47. */
  48. void netifOnResponse(void){
  49. if(app.netWork.netReayd==0) wlog_warn("netIfOn");
  50. app.netWork.netReayd=1;
  51. }
  52. /*
  53. netifOffResponse
  54. netif掉线后要处理的操作
  55. */
  56. void netifOffResponse(void){
  57. if(app.netWork.netReayd==1) wlog_warn("netIfOff");
  58. app.netWork.netReayd=0;
  59. }
  60. #endif
  61. /*PDP打开与关闭*/
  62. static uint8_t tCid = 1;
  63. static uint8_t tSim = 0;
  64. unsigned pdpCtl(bool status,uint32_t *id){
  65. LSAPI_OSI_Event_t event = {0};
  66. uint8_t nState;
  67. unsigned res;
  68. if(true==status) nState=1;
  69. else nState=0;
  70. res=LSAPI_NET_GprsAct(nState, tCid, tSim, &event);
  71. *id=event.id;
  72. return res;
  73. }
  74. uint32_t ls_PdpActive(void){
  75. unsigned res = 0;
  76. uint32_t id;
  77. int act_state = 0;
  78. //char *apn = "cmnet";
  79. //res = LSAPI_NET_SetPdpcont(nCid, "CMNET", NULL, NULL);
  80. //wlog_info("LSAPI_NET_SetPdpcont nCID=%d,nRet=%d\n", nCid, res);
  81. /* 1. active PDP begin */
  82. res = pdpCtl(true,&id);
  83. wlog_info("LSAPI_NET_GprsAct event.id(%d),res(%d)", id,res);
  84. if (1 == res){
  85. LSAPI_NET_GetGprsActState(&act_state);
  86. wlog_info( "LSAPI_NET_GprsAct succ:act_state(%d),cid(%d)", act_state,tCid);
  87. return 0;
  88. }else{
  89. wlog_info("LSAPI_NET_GprsAct active fail:cid(%d)",tCid);
  90. return 1;
  91. }
  92. }
  93. void isNetWorkIdle(void){
  94. if(app.netWork.netReayd==0) ticketDeVote(TICKET_PT_NETWORK);
  95. else ticketVote(TICKET_PT_NETWORK);
  96. }
  97. static T_BOOL getIccidFlag=FALSE;
  98. /*
  99. MS5700_Networking
  100. MS5700平台网络任务入口
  101. */
  102. static void MS5700_Networking(void){
  103. T_UINT8 info[23];
  104. uint32_t nRet,id;
  105. static int nAttstate,nActstate;//需要static
  106. static int LastnAttstate=0,LastnActstate=0;
  107. T_BOOL netIf;
  108. static T_BOOL lastNetIf=FALSE;
  109. static T_UINT16 cnt=0;
  110. static T_UINT16 regCnt=0;
  111. if(FALSE==getIccidFlag){
  112. memset(info,0,sizeof(info));
  113. if(0==LSAPI_SIM_GetICCID(info)){
  114. wlog_info("ICCID:%s", info);
  115. strcpy(app.CCID, info);
  116. getIccidFlag=TRUE;
  117. }
  118. }
  119. if(FALSE==app.getImeiFlag){
  120. memset(info,0,sizeof(info));
  121. if(0==LSAPI_SIM_GetIMEI(info)){
  122. wlog_info("IMEI:%s", info);
  123. app.getImeiFlag=TRUE;
  124. strncpy(app.imei, info, sizeof(app.imei));
  125. }
  126. }
  127. if(FALSE==app.btimeFlag){
  128. memset(app.mbtime, 0, sizeof(app.mbtime));
  129. LSAPI_SYS_getModulebtime(app.mbtime);
  130. wlog_info("BTIME:%s",app.mbtime);
  131. app.btimeFlag=TRUE;
  132. }
  133. if(FALSE==getIccidFlag) return;//没卡
  134. //网络处理
  135. isNetWorkIdle();
  136. //检测是否要重置PDP
  137. if(app.netWork.resetPDP==1){
  138. app.netWork.resetPDP=0;
  139. extShutAppLinks();
  140. pdpCtl(false,&id);
  141. wlog_warn("Disable pdp!!!");
  142. }
  143. nRet = LSAPI_NET_GetGprsAttState(&nAttstate);//获取注册状态
  144. if(nRet!=0){
  145. wlog_warn("Get REG status failed");
  146. return;
  147. }
  148. if(nAttstate!=1){//注册不成功
  149. if(LastnAttstate!=nAttstate){wlog_warn("reg off");}//注册状态发生变化
  150. app.netWork.reg=0;
  151. app.netWork.pdp=0;
  152. app.netWork.netReayd=0;
  153. wlog_info("wait reg");
  154. if(regCnt==600){//注册10分钟还不成功
  155. wlog_warn("!!!CFUN=0");
  156. LSAPI_NET_CFUN(0);
  157. }else if(regCnt>=601) {
  158. regCnt=0;
  159. wlog_warn("!!!CFUN=1");
  160. LSAPI_NET_CFUN(1);
  161. }
  162. ++regCnt;
  163. }else{//注册成功
  164. if(LastnAttstate!=nAttstate){wlog_warn("reg on");}//注册状态发生变化
  165. app.netWork.reg=1;
  166. nRet = LSAPI_NET_GetGprsActState(&nActstate);//定时检测PDP状态
  167. if(nRet!=0){//检测失败
  168. wlog_warn("LSAPI_NET_GetGprsActState get failed:%d",nRet);
  169. }else{//检测成功
  170. if(nActstate!=1){//PDP断开
  171. if(LastnActstate != nActstate){wlog_warn("pdp off");}//PDP状态发生变化
  172. app.netWork.pdp=0;
  173. app.netWork.netReayd=0;
  174. //尝试激活PDP
  175. ls_PdpActive();
  176. }else{//PDP已激活
  177. if(LastnActstate != nActstate){wlog_warn("pdp on");}//PDP状态发生变化
  178. app.netWork.pdp=1;
  179. netIf=LSAPI_NET_GET_GprsNetIf();//检测netif状态
  180. if(false==netIf){//netif未创建
  181. if(lastNetIf!=netIf){wlog_warn("netif off");}//netif状态发生变化
  182. app.netWork.netReayd=0;
  183. wlog_info("netif start create");
  184. LSAPI_NET_NetIf_Create();//创建netif
  185. wlog_info("netif create done");
  186. }else{//netif已创建
  187. if(lastNetIf!=netIf){wlog_warn("netif on");}//netif状态发生变化
  188. app.netWork.netReayd=1;
  189. }
  190. lastNetIf=netIf;
  191. }
  192. LastnActstate=nActstate;
  193. }
  194. }
  195. LastnAttstate=nAttstate;
  196. }
  197. #include "lsapi_sock.h"
  198. static bool pingStatus=FALSE;
  199. void pingcallback(uint8_t flag){
  200. switch(flag){
  201. case LSAPI_SOCK_ICMP_DATA_IND:
  202. wlog_info("ping success");
  203. msgToOutter("+IPING:SUCC\r\n");
  204. break;
  205. case LSAPI_SOCK_ICMP_TIME_OUT:
  206. wlog_info("ping timeout");
  207. msgToOutter("+IPING:TIMEOUT\r\n");
  208. break;
  209. default:
  210. msgToOutter("+IPING:UNKNWON\r\n");
  211. wlog_info("ping default event:%d",flag);
  212. break;
  213. }
  214. threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT, NULL);
  215. return;
  216. }
  217. char pinghost[IP_DOMAIN_SIZE+1]={0};
  218. static void wpingTask(void *param){
  219. wlog_info("start ping:%s",pinghost);
  220. if(LSAPI_RET_OK!=LSAPI_SOCK_Ping(pinghost, 6, 255, pingcallback)){
  221. wlog_info("ping api error");
  222. msgToOutter("+IPING:ERR\r\n");
  223. pingStatus=FALSE;
  224. LSAPI_OSI_ThreadExit();
  225. return;
  226. }
  227. LSAPI_OSI_Event_t event={};
  228. for(;;){
  229. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  230. if(USER_EVENT_EXIT==event.id){
  231. pingStatus=FALSE;
  232. userExitThread("wpingTask");
  233. break;
  234. }
  235. }
  236. }
  237. void tryToPing(char *host){
  238. //host="www.baidu.com"
  239. int i,j;
  240. //return ;
  241. //计算'"'个数
  242. j=0;
  243. if(host[0] != '"') goto CMD_ERR;
  244. for(i=0;i<strlen(host);i++){
  245. if(host[i]=='"'){
  246. j++;
  247. if(j>2){
  248. goto CMD_ERR;
  249. }else if(j==2){
  250. if(host[i+1]!=0) goto CMD_ERR;
  251. host[i]=0;
  252. }
  253. }
  254. }
  255. if(TRUE==pingStatus){
  256. msgToOutter("+IPING:BUSY\r\n");
  257. wlog_warn("ping is busy");
  258. return;
  259. }
  260. //get host
  261. snprintf(pinghost, sizeof(pinghost),"%s",host+1);
  262. wlog_info("ping:%s",pinghost);
  263. if(NULL == LSAPI_OSI_ThreadCreate("wping", wpingTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, PING_THREAD_STACK, 4)){
  264. msgToOutter("+IPING:ERR\r\n");
  265. wlog_warn("ping thread failed");
  266. return;
  267. }
  268. pingStatus=TRUE;
  269. msgToOutter("OK\r\n");
  270. return;
  271. CMD_ERR:
  272. msgToOutter("+IPING:ERR\r\n");
  273. wlog_warn("ping cmd err");
  274. }
  275. /*
  276. ptNetWorkTask
  277. 网络任务入口操作
  278. */
  279. PT_THREAD (ptNetWorkTask(pt_timer_t *ptPool, struct pt *pt)){
  280. static pt_timer_t ptTimer;
  281. PT_BEGIN(pt);
  282. while(1){
  283. MS5700_Networking();
  284. PTTimerStart(ptPool, &ptTimer,100);//need set to 1 seconds
  285. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  286. }
  287. PT_END(pt);
  288. }