socket.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "includes.h"
  2. #if(SOCKET_NUM_MAX>5)
  3. #error "SOCKET_NUM_MAX 不能大于5个"
  4. #endif
  5. static void tcpSocketRun(void);
  6. SOCKET_DEF gSocket;
  7. //150ms执行一次LED任务
  8. PT_THREAD (ptSocketTask(struct pt *pt)){
  9. static timer_t ptTimer;
  10. static char firstRun=1;
  11. if(firstRun!=0){
  12. tcpSocketInit();
  13. if(tcpSocketAdd(buBiaoTickHandler, buBiaoRecvHandler, buBiaoSocketShutDown,showGIP,newPara.gpsPort)<0) SlwTrace(INF, "Create bibiao socket fail",1);//最多建议SOCKET_NUM_MAX个socket连接
  14. //if(tcpSocketAdd(FotaTickHandler, FotaIap_Handle, buBiaoSocketShutDown,FOTA_IAP_IP,FOTA_IAP_PORT)<0) SlwTrace(INF, "Create FOTA socket fail",1);
  15. firstRun=0;
  16. }
  17. PT_BEGIN(pt);
  18. while(1){
  19. if(g_ucEnterGTNow==0) tcpSocketRun();//GT模式下不操作socket
  20. PTTimerStart(&pt_timerPool, &ptTimer, 50);
  21. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  22. }
  23. PT_END(pt);
  24. }
  25. void tcpSocketInit(void){
  26. memset((unsigned char *)&gSocket,0,sizeof(SOCKET_DEF));
  27. }
  28. short tcpSocketAdd(void (*UserHandle)(unsigned char socketID),void (*RxCallBack)(unsigned char *data , unsigned int),void (*SockeShutDown)(unsigned char socketID),char *ipAddr, unsigned short port)
  29. {
  30. unsigned char i;
  31. SOCKET_INFO *socket;
  32. for(i=0;i<SOCKET_NUM_MAX;i++){
  33. socket=&gSocket.socketList[i];
  34. if(socket->status==0){
  35. strncpy(socket->Ip, ipAddr, sizeof(socket->Ip));
  36. socket->port=port;
  37. socket->UserHandle=UserHandle;
  38. socket->RxCallBack=RxCallBack;
  39. socket->SockeShutDown=SockeShutDown;
  40. socket->machine=i;
  41. socket->status=1;
  42. return i;
  43. }
  44. }
  45. return -1;
  46. }
  47. void tcpSocketDel(short socketID){
  48. unsigned char i;
  49. SOCKET_INFO *socket;
  50. char info[30];
  51. if(socketID>=SOCKET_NUM_MAX) return;
  52. socket=&gSocket.socketList[socketID];
  53. if(socket->status==0) return;
  54. if(socket->tcpStatus!=0){
  55. snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1);
  56. ModemSendAt(info);
  57. socket->tcpStatus=0;
  58. }
  59. socket->status=0;
  60. }
  61. static void tcpCloseSocket(void){
  62. unsigned char i;
  63. SOCKET_INFO *socket;
  64. char info[30];
  65. for(i=0;i<SOCKET_NUM_MAX;i++){
  66. socket=&gSocket.socketList[i];
  67. if(socket->status!=0){
  68. if(socket->tcpStatus!=0){
  69. snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",i+1);
  70. ModemSendAt(info);
  71. socket->tcpStatus=0;
  72. }
  73. }
  74. }
  75. }
  76. //网络连接检测及控制
  77. static char tcpLinkProcess(void){
  78. char ret=0;
  79. static char tick=0;
  80. static STATUE lastPPP=CLOSE;
  81. //AT+CCLK?
  82. if(hgs_poc_type==POC_OWN)
  83. {
  84. switch(QueryTick){
  85. case 0:
  86. // printf("QueryTick===%d",QueryTime);
  87. ModemSendAt("AT+POC_PPP?\r\n");
  88. ModemSendAt("AT+CSQ\r\n");
  89. if(g_uiGpsStat)ModemSendAt("AT+GPSRD=\"ALL\"\r\n");
  90. ModemSendAt("AT+CTEC?\r\n");
  91. ModemSendAt("AT+UDP?\r\n");
  92. ModemSendAt("AT+GGIP?\r\n");
  93. ModemSendAt("AT+GPIP?\r\n");
  94. ModemSendAt("AT+COPS?\r\n");
  95. break;
  96. }
  97. if(++QueryTick>=QueryTime) QueryTick=0;
  98. if(netPPP==CLOSE){
  99. if(lastPPP != netPPP) tcpCloseSocket();
  100. lastPPP=netPPP;
  101. return 1;
  102. }
  103. lastPPP=netPPP;
  104. return ret;
  105. }else if(hgs_poc_type==POC_BND) {
  106. switch(tick){
  107. case 0:break;
  108. case 2:ModemSendAt("AT+CSQ\r\n");break;//csqSendFlag=0;
  109. case 4:
  110. if(g_uiGpsStat)ModemSendAt("AT+GPSRD=\"ALL\"\r\n");
  111. break;
  112. case 6:ModemSendAt("AT+CTEC?\r\n");break;//ModemSendAt("AT^SYSINFO\r\n");break;
  113. case 8:break;
  114. case 10:break;
  115. case 12:break;
  116. case 14:ModemSendAt("AT+COPS?\r\n");
  117. break;
  118. }
  119. if(++tick>=15) tick=0;
  120. return 0;
  121. }
  122. }
  123. static char LogicHandler(unsigned char index){
  124. char ret=1;
  125. char needSendAt=1;
  126. char info[50];
  127. SOCKET_INFO *socket=&gSocket.socketList[index];
  128. if(*(socket->Ip)==0){
  129. return 0;
  130. }
  131. switch(QueryTick){
  132. case 0:snprintf(info, sizeof(info),"AT+TCPSTATUS=%d\r\n",index+1);
  133. break;
  134. case 5:
  135. if(socket->tcpStatus==0) snprintf(info, sizeof(info),"AT+TCPOPEN=%d,%s:%d\r\n", index+1,socket->Ip,socket->port);
  136. else needSendAt=0;
  137. break;
  138. default:ret=0;needSendAt=0;
  139. break;
  140. }
  141. // if(++socket->machine > QueryTime) socket->machine=0;//5
  142. if(needSendAt) ModemSendAt(info);
  143. return ret;
  144. }
  145. char tcpSocketSendData(unsigned char socketID,unsigned char *data, unsigned int length){
  146. static char runStep=0;
  147. unsigned char i,ret=0;
  148. SOCKET_INFO *socket;
  149. char buf[20];
  150. if(socketID >= SOCKET_NUM_MAX) return 1;
  151. socket=&gSocket.socketList[socketID];
  152. if(socket->status==0) return 2;
  153. if(socket->tcpStatus==0) return 3;
  154. snprintf(buf, sizeof(buf), "AT+TCPSEND=%d,%d,",socketID+1, length);
  155. ModemSendAt(buf);
  156. ModemSendData(data, length);
  157. ModemSendAt("\r\n");
  158. return 0;
  159. }
  160. static void tcpSocketRun(void){
  161. static char runStep=0;
  162. unsigned char i,j,w,ret=0;
  163. SOCKET_INFO *socket;
  164. if(tcpLinkProcess()) return;
  165. if(!g_uiGpsStat||hgs_poc_type!=POC_OWN) return; //不去增加服务器负担
  166. for(j=0;j<2;j++){
  167. if(j==0) w=0;
  168. else w=gSocket.index;
  169. for(i=w;i<SOCKET_NUM_MAX;i++){
  170. socket=&gSocket.socketList[i];
  171. if(socket->status!=0){//判断 socket是否定义
  172. if(j==0) socket->UserHandle(i);
  173. else if(LogicHandler(i))break;
  174. }
  175. }
  176. }
  177. if(i>=SOCKET_NUM_MAX) gSocket.index=0;
  178. }
  179. void tcpSocketRecv(unsigned char id, unsigned char *data, unsigned char length){
  180. unsigned char i,thisID;
  181. SOCKET_INFO *socket;
  182. thisID=id-1;
  183. if(thisID>= SOCKET_NUM_MAX) return;
  184. socket=&gSocket.socketList[thisID];
  185. if(socket->status==0) return;
  186. socket->RxCallBack(data,length);
  187. }
  188. /*
  189. 更新状态
  190. type:0 status为1表示打开
  191. type:1 status为1表示关闭
  192. */
  193. void tcpSocketStatusUpdate(char socketID, char status,char type){
  194. unsigned char i,thisID;
  195. SOCKET_INFO *socket;
  196. thisID=socketID-0x31;
  197. if(thisID>= SOCKET_NUM_MAX) return;
  198. socket=&gSocket.socketList[thisID];
  199. if(socket->status==0) return;
  200. if(type==0){
  201. if(status=='1') socket->tcpStatus=1;
  202. else if(status=='0') socket->tcpStatus=0;
  203. }else{
  204. socket->tcpStatus=0;
  205. if(NULL!=socket->SockeShutDown) socket->SockeShutDown(thisID);
  206. }
  207. }
  208. char tcpSocketGetStatus(char socketID){
  209. SOCKET_INFO *socket;
  210. if(socketID>= SOCKET_NUM_MAX) return 1;
  211. socket=&gSocket.socketList[socketID];
  212. if(socket->status==0) return 2;
  213. if(socket->tcpStatus==0) return 3;
  214. return 0;
  215. }
  216. void SocketParaUpdate(unsigned char socketID,char *ipAddr, unsigned short port){
  217. SOCKET_INFO *socket;
  218. char info[30];
  219. if(socketID>= SOCKET_NUM_MAX) return;
  220. socket=&gSocket.socketList[socketID];
  221. if(socket->status==0) return ;
  222. if(socket->tcpStatus!=0){
  223. if(strcmp(socket->Ip, ipAddr) || socket->port != port){
  224. strncpy(socket->Ip, ipAddr, sizeof(socket->Ip));
  225. socket->port=port;
  226. snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1);
  227. ModemSendAt(info);
  228. socket->tcpStatus=0;
  229. }
  230. }
  231. }