netWork.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. netWork.c
  3. 网络功能任务
  4. */
  5. #include "includes.h"
  6. /*
  7. isNetWork4G
  8. 更新4G/非4G状态后要处理的操作
  9. */
  10. void isNetWork4G(void){
  11. static nwy_nw_mode_type_t last_network_mode = NWY_NW_MODE_NONE;
  12. nwy_nw_mode_type_t network_mode;
  13. if (NWY_SUCCESS == nwy_nw_get_network_mode(&network_mode)){
  14. if(network_mode!=last_network_mode){
  15. last_network_mode=network_mode;
  16. if(network_mode==NWY_NW_MODE_GSM){
  17. wlog_info("Is none-4G netWork");
  18. talking.workIn_4G=FALSE;
  19. talking.maxValue_none_4G=DEFAULT_TCP_HEARTTICK - (DEFAULT_TCP_HEARTTICK % paras.udpHeartTick_none_4G);
  20. }else if(network_mode==NWY_NW_MODE_LTE){
  21. wlog_info("Is 4G netWork");
  22. talking.workIn_4G=TRUE;
  23. talking.maxValue_4G=DEFAULT_TCP_HEARTTICK - (DEFAULT_TCP_HEARTTICK % paras.udpHeartTick_4G);
  24. }
  25. talking.pocTupCnt=0;
  26. }
  27. }
  28. }
  29. void isNetWorkIdle(void){
  30. if(talking.netWork.pdp==0) ticketDeVote(TICKET_PT_NETWORK);
  31. else ticketVote(TICKET_PT_NETWORK);
  32. }
  33. static void nwy_data_cb_fun(int hndl, nwy_data_call_state_t ind_state){
  34. static nwy_data_call_state_t lastDataCall=NWY_DATA_CALL_INVALID;
  35. wlog_info("dataCallCb hndl=%d,ind_state=%d",hndl, ind_state);
  36. if(lastDataCall!=ind_state){
  37. lastDataCall=ind_state;
  38. if(ind_state==NWY_DATA_CALL_CONNECTED){
  39. wlog_warn("pdp on");
  40. talking.netWork.pdp=1;
  41. }else{
  42. wlog_warn("pdp off");
  43. talking.netWork.pdp=0;
  44. }
  45. }
  46. }
  47. /*
  48. MS5700_Networking
  49. MS5700平台网络任务入口
  50. */
  51. int myhndl=-1;
  52. void pdpRstHandler(void){
  53. static char rstStep=0;
  54. if(talking.netWork.resetPDP!=0){
  55. talking.netWork.resetPDP=0;
  56. rstStep=1;
  57. wlog_warn("Disable pdp!!!");
  58. }
  59. switch(rstStep){
  60. case 1:
  61. wlog_warn("stop call");
  62. if(myhndl>0) nwy_data_stop_call(myhndl);
  63. rstStep=2;
  64. break;
  65. case 2:
  66. wlog_warn("release srv");
  67. if(myhndl>0) nwy_data_relealse_srv_handle(myhndl);
  68. myhndl=-1;
  69. rstStep=0;
  70. break;
  71. }
  72. }
  73. static void MS5700_Networking(void){
  74. nwy_sim_result_type info={0};
  75. nwy_nw_regs_info_type_t reg_info = {0};
  76. static nwy_nw_service_type_t lastRegStatus=NWY_NW_SERVICE_NONE;
  77. static T_BOOL getIccidFlag=FALSE;
  78. static T_UINT16 cnt=0;
  79. static T_UINT16 regCnt=0;
  80. if(FALSE==getIccidFlag){
  81. if(NWY_SMS_SUCCESS==nwy_sim_get_iccid(&info)){
  82. wlog_info("ICCID:%s", info.iccid);
  83. strcpy(talking.CCID, info.iccid);
  84. getIccidFlag=TRUE;
  85. }
  86. }
  87. if(FALSE==talking.getImeiFlag){
  88. if(NWY_SMS_SUCCESS==nwy_sim_get_imei(&info)){
  89. wlog_info("IMEI:%s", info.nImei);
  90. strcpy(talking.imei, info.nImei);
  91. talking.getImeiFlag=TRUE;
  92. }
  93. }
  94. if(FALSE==getIccidFlag) return;//没卡
  95. //网络处理
  96. isNetWorkIdle();
  97. //检测是否要重置PDP
  98. pdpRstHandler();
  99. if(NWY_RES_OK != nwy_nw_get_register_info(&reg_info)){
  100. wlog_warn("Get REG status failed");
  101. return;
  102. }
  103. if(reg_info.data_regs.regs_state == NWY_NW_SERVICE_NONE){//注册不成功
  104. if(lastRegStatus != reg_info.data_regs.regs_state) {wlog_warn("reg off");}//注册状态发生变化
  105. talking.netWork.reg=0;
  106. talking.netWork.pdp=0;
  107. wlog_info("wait reg");
  108. if(regCnt==600){//注册10分钟还不成功
  109. wlog_warn("!!!CFUN=0");
  110. // LSAPI_NET_CFUN(0);
  111. }else if(regCnt>=601) {
  112. regCnt=0;
  113. wlog_warn("!!!CFUN=1");
  114. // LSAPI_NET_CFUN(1);
  115. }
  116. ++regCnt;
  117. }else{//注册成功
  118. if(lastRegStatus != reg_info.data_regs.regs_state) {wlog_warn("reg on");}//注册状态发生变化
  119. talking.netWork.reg=1;
  120. if(myhndl<0){
  121. myhndl = nwy_data_get_srv_handle(nwy_data_cb_fun);
  122. if(myhndl>0){
  123. nwy_data_start_call_v02_t param_t={1,1,10,5000};
  124. if(nwy_data_start_call(myhndl,&param_t)==NWY_RES_OK) wlog_warn("nwy_data_start_call ok");
  125. else{
  126. wlog_warn("nwy_data_start_call failed,rst pdp");
  127. talking.netWork.resetPDP=1;
  128. }
  129. }
  130. }
  131. }
  132. lastRegStatus=reg_info.data_regs.regs_state;
  133. isNetWork4G();
  134. isNetWorkIdle();
  135. }
  136. //#include "lsapi_sock.h"
  137. //static bool pingStatus=FALSE;
  138. void pingcallback(uint8_t flag){
  139. #if 0
  140. switch(flag){
  141. case LSAPI_SOCK_ICMP_DATA_IND:
  142. wlog_info("ping success");
  143. msgToOutter("+IPING:SUCC\r\n");
  144. break;
  145. case LSAPI_SOCK_ICMP_TIME_OUT:
  146. wlog_info("ping timeout");
  147. msgToOutter("+IPING:TIMEOUT\r\n");
  148. break;
  149. default:
  150. msgToOutter("+IPING:UNKNWON\r\n");
  151. wlog_info("ping default event:%d",flag);
  152. break;
  153. }
  154. threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT);
  155. return;
  156. #endif
  157. }
  158. char pinghost[IP_DOMAIN_SIZE+1]={0};
  159. static void wpingTask(void *param){
  160. #if 0
  161. wlog_info("start ping:%s",pinghost);
  162. if(LSAPI_RET_OK!=LSAPI_SOCK_Ping(pinghost, 6, 255, pingcallback)){
  163. wlog_info("ping api error");
  164. msgToOutter("+IPING:ERR\r\n");
  165. pingStatus=FALSE;
  166. nwy_exit_thread();
  167. return;
  168. }
  169. LSAPI_OSI_Event_t event={};
  170. for(;;){
  171. nwy_wait_thead_event(LSAPI_OSI_ThreadCurrent(), &event,0);
  172. if(USER_EVENT_EXIT==event.id){
  173. pingStatus=FALSE;
  174. userExitThread("wpingTask");
  175. break;
  176. }
  177. }
  178. #endif
  179. }
  180. void tryToPing(char *host){
  181. #if 0
  182. //host="www.baidu.com"
  183. int i,j;
  184. //return ;
  185. //计算'"'个数
  186. j=0;
  187. if(host[0] != '"') goto CMD_ERR;
  188. for(i=0;i<strlen(host);i++){
  189. if(host[i]=='"'){
  190. j++;
  191. if(j>2){
  192. goto CMD_ERR;
  193. }else if(j==2){
  194. if(host[i+1]!=0) goto CMD_ERR;
  195. host[i]=0;
  196. }
  197. }
  198. }
  199. if(TRUE==pingStatus){
  200. msgToOutter("+IPING:BUSY\r\n");
  201. wlog_warn("ping is busy");
  202. return;
  203. }
  204. //get host
  205. snprintf(pinghost, sizeof(pinghost),"%s",host+1);
  206. wlog_info("ping:%s",pinghost);
  207. if(NULL == LSAPI_OSI_ThreadCreate("wping", wpingTask, NULL, NWY_OSI_PRIORITY_NORMAL, PING_THREAD_STACK, 4)){
  208. msgToOutter("+IPING:ERR\r\n");
  209. wlog_warn("ping thread failed");
  210. return;
  211. }
  212. pingStatus=TRUE;
  213. msgToOutter("OK\r\n");
  214. return;
  215. CMD_ERR:
  216. msgToOutter("+IPING:ERR\r\n");
  217. wlog_warn("ping cmd err");
  218. #endif
  219. }
  220. /*
  221. ptNetWorkTask
  222. 网络任务入口操作
  223. */
  224. PT_THREAD (ptNetWorkTask(pt_timer_t *ptPool, struct pt *pt)){
  225. static pt_timer_t ptTimer;
  226. PT_BEGIN(pt);
  227. while(1){
  228. MS5700_Networking();
  229. PTTimerStart(ptPool, &ptTimer,100);//need set to 1 seconds
  230. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  231. }
  232. PT_END(pt);
  233. }