netWork.c 7.3 KB

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