socket.c 5.8 KB

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