gprs_proc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: gprs_recv_proc.c
  4. 文件版本: 01.01
  5. 创建作者: xxxxxx
  6. 创建日期: 2014-06-10
  7. 功能说明: GPRS接收处理
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #ifdef GPRS_FUNC
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/fs.h>
  17. #include <linux/sched.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kthread.h>
  20. #include <linux/delay.h>
  21. #include <linux/wait.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/io.h>
  24. #include <asm/ioctls.h>
  25. //#include <asm/semaphore.h>
  26. #include <linux/if.h>
  27. #include <linux/tcp.h>
  28. #include <termios.h>
  29. #include <linux/syscalls.h>
  30. #include "gprs_vs_func.h"
  31. #include "gh_vs.h"
  32. #include "gprs_aux.h"
  33. #define GPRS_CHNL 6 // 最后一个串口
  34. extern struct completion *g_exit_completion_gprs_net_IEC_proc;
  35. extern struct completion *g_exit_completion_gprs_net_aux_proc;
  36. extern int main_mod_is_exit(void);
  37. //接收数据处理回调函数
  38. void (*g_IEC_Recv)(unsigned char cDat, unsigned char chnl);
  39. extern int g_vsfd;
  40. static struct completion g_exit_completion_gprs;
  41. static struct completion g_exit_completion_gprs_aux;
  42. struct gprs_info g_gprs_info; //GPRS状态信息
  43. int g_gh_vs_is_idle = 1; //虚拟串口空闲标志.1表示空闲,0表示不空闲
  44. int g_update_dz = 0; //更新定值标志.注意是内部的资源的复位,不是重新创建任务
  45. int g_update_gprs_driver = 0; //更新GPRS驱动标志.注意是内部的资源的复位,不是重新创建任务
  46. static int g_gprs_init_flag = 0;//GPRS初始化标志
  47. /******************************************************************************
  48. 函数名称: gprs_net_IEC_proc
  49. 函数版本: 01.01
  50. 创建作者: xxxxxx
  51. 创建日期: 2014-06-10
  52. 函数说明: GPRS处理任务。
  53. 参数说明: 无
  54. 返回值: 成功返回0,失败返回负数。
  55. 修改记录:
  56. */
  57. int gprs_net_IEC_proc(void *arg)
  58. {
  59. struct timeval tv;
  60. fd_set rset;
  61. int ret = 0;
  62. int i = 0;
  63. static unsigned char buf[256];
  64. g_exit_completion_gprs_net_IEC_proc = &g_exit_completion_gprs;
  65. begin:
  66. //虚拟串口空闲,任务就休眠
  67. if(g_gh_vs_is_idle)
  68. {
  69. msleep(100);
  70. if(main_mod_is_exit())
  71. {
  72. complete_and_exit(&g_exit_completion_gprs, 0);
  73. }
  74. goto begin;
  75. }
  76. //打开虚拟串口
  77. ret = gprs_vs_open();
  78. if(ret < 0) //虚拟串口被占用,需要重新尝试打开
  79. {
  80. msleep(100);
  81. if(main_mod_is_exit())
  82. {
  83. complete_and_exit(&g_exit_completion_gprs, 0);
  84. }
  85. goto begin;
  86. }
  87. while(1)
  88. {
  89. tv.tv_sec = 0;
  90. tv.tv_usec = 500000;
  91. FD_ZERO(&rset);
  92. FD_SET(g_vsfd, &rset);
  93. ret = sys_select(g_vsfd+1, &rset, NULL, NULL, &tv);
  94. //有数据可读
  95. if(ret > 0)
  96. {
  97. //读取数据
  98. ret = gprs_vs_read(buf, sizeof(buf));
  99. //处理101规约报文
  100. if(g_IEC_Recv)
  101. {
  102. for(i=0; i<ret ;i++)
  103. {
  104. //g_IEC_Recv(buf[i], GPRS_CHNL);
  105. }
  106. }
  107. #if 0
  108. //测试代码
  109. printk("\nIEC RECV:");
  110. for(i = 0; i < ret; i++)
  111. {
  112. printk("%02x ", (unsigned char)buf[i]);
  113. }
  114. printk("\n");
  115. #endif
  116. }
  117. //检查是否退出
  118. if(main_mod_is_exit())
  119. {
  120. gprs_vs_close();
  121. complete_and_exit(&g_exit_completion_gprs, 0);
  122. break;
  123. }
  124. //释放资源,关闭虚拟串口,重新打开虚拟串口
  125. if(g_update_dz)
  126. {
  127. gprs_vs_close();
  128. g_update_dz = 0;
  129. goto begin;
  130. }
  131. //升级GPRS驱动,需要关闭虚拟串口,重新打开虚拟串口
  132. if(g_update_gprs_driver)
  133. {
  134. gprs_vs_close();
  135. g_update_gprs_driver = 0;
  136. msleep(10000);//让GPRS模块有足够的时间启动
  137. goto begin;
  138. }
  139. }
  140. return 0;
  141. }
  142. /******************************************************************************
  143. 函数名称: gprs_net_get_gprs_info
  144. 函数版本: 01.01
  145. 创建作者: xxxxxx
  146. 创建日期: 2014-06-10
  147. 函数说明: 获取内部GPRS信息.
  148. 参数说明:
  149. 返回值: 成功返回信息的字节数。
  150. 修改记录:
  151. */
  152. int gprs_net_get_gprs_info(void)
  153. {
  154. int num = 0;
  155. int ret = 0;
  156. int i = 0 ;
  157. extern unsigned char strGprsInf[30][24];
  158. extern int g_aux_csq ;
  159. memset(strGprsInf, 0, sizeof(strGprsInf));
  160. ret = sys_ioctl(g_vsfd, GH_VS_GET_GPRSINFO, (uint32_t)&g_gprs_info);
  161. if(ret == 0)
  162. {
  163. if(g_gprs_info.module_state)
  164. {
  165. strcpy(strGprsInf[num++],"模块状态 :正常");
  166. }
  167. else
  168. {
  169. strcpy(strGprsInf[num++],"模块状态 :异常");
  170. }
  171. if(g_gprs_info.sim_state)
  172. {
  173. strcpy(strGprsInf[num++],"SIM卡状态:正常");
  174. }
  175. else
  176. {
  177. strcpy(strGprsInf[num++],"SIM卡状态:异常");
  178. }
  179. if(g_gprs_info.ppp_state)
  180. {
  181. strcpy(strGprsInf[num++],"PPP拨号 :正常");
  182. }
  183. else
  184. {
  185. strcpy(strGprsInf[num++],"PPP拨号 :异常");
  186. g_gprs_info.localip = 0;
  187. }
  188. sprintf(strGprsInf[num++],"%d.%d.%d.%d",(g_gprs_info.localip >> 24 & 0xFF), (g_gprs_info.localip >> 16 & 0xFF), (g_gprs_info.localip >> 8 & 0xFF), (g_gprs_info.localip >> 0 & 0xFF));
  189. for(i=0;i<4;i++)
  190. {
  191. if(g_gprs_info.target_con_state[i])
  192. {
  193. sprintf(strGprsInf[num++],"CH%d:正常 %d", i+1, g_gprs_info.port[i]);
  194. }
  195. else
  196. {
  197. sprintf(strGprsInf[num++],"CH%d:异常 %d", i+1, g_gprs_info.port[i]);
  198. }
  199. sprintf(strGprsInf[num++],"%d.%d.%d.%d", (int)(g_gprs_info.ip[i] >> 24 & 0xFF), (int)(g_gprs_info.ip[i] >> 16 & 0xFF), (int)(g_gprs_info.ip[i] >> 8 & 0xFF), (int)(g_gprs_info.ip[i] >> 0 & 0xFF));
  200. }
  201. if(g_aux_csq < 0) //第二个串口没有发送过AT+CSQ指令,用g_gprs_info.csq作为信号质量
  202. {
  203. sprintf(strGprsInf[num++],"信号强度 :%2d",g_gprs_info.csq);
  204. }
  205. else
  206. {
  207. sprintf(strGprsInf[num++],"信号强度 :%2d",g_aux_csq);
  208. }
  209. //获取IP地址
  210. //gprs_net_get_ip();
  211. strcpy(strGprsInf[num++],"频段:自适应");
  212. sprintf(strGprsInf[num++], "发送流量:(%llu B)", g_gprs_info.tx_flowcnt);
  213. sprintf(strGprsInf[num++], "接收流量:(%llu B)", g_gprs_info.rx_flowcnt);
  214. }
  215. return num;
  216. }
  217. /******************************************************************************
  218. 函数名称: gprs_net_init
  219. 函数版本: 01.01
  220. 创建作者: xxxxxx
  221. 创建日期: 2014-06-10
  222. 函数说明: GPRS初始化。
  223. 参数说明: 无
  224. 返回值: 成功返回0,失败返回负数。
  225. 修改记录:
  226. */
  227. int gprs_net_init(void)
  228. {
  229. pid_t pid=0;
  230. struct task_struct * ts;
  231. struct sched_param sp;
  232. //int ret = 0;
  233. if(g_gprs_init_flag)
  234. {
  235. return 0;
  236. }
  237. g_update_dz = 0; //清标志
  238. init_completion(&g_exit_completion_gprs);
  239. init_completion(&g_exit_completion_gprs_aux);
  240. //创建gprs_IEC_proc处理任务
  241. pid = kernel_thread(gprs_net_IEC_proc, NULL,CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  242. ts = find_task_by_vpid(pid);
  243. if(ts == NULL)
  244. {
  245. printk("%s:pid=%d,ts=%p\r\n",__func__,pid,ts);
  246. return -1;
  247. }
  248. // 设置调度策略和优先级
  249. sp.sched_priority = 12;
  250. sched_setscheduler(ts,SCHED_FIFO,&sp);
  251. // 设置进程名字
  252. set_task_comm(ts, "gprs_IEC_proc");
  253. //清零
  254. memset(&g_gprs_info, 0, sizeof(g_gprs_info));
  255. //置初始化标志
  256. g_gprs_init_flag = 1;
  257. return 0;
  258. }
  259. #endif