ipSos.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "includes.h"
  2. #define IP_SOS_HEART_UP 49
  3. #define IP_SOS_HEART_DOWN 54
  4. #define IP_SOS_REPORT_UP 40
  5. #define IP_SOS_REPORT_DOWN 41
  6. #define IP_SOS_MODIFY_HEART_UP 96
  7. #define IP_SOS_MODIFY_HEART_DOWN 95
  8. #define IP_SOS_MODIFY_ADDR_UP 108
  9. #define IP_SOS_MODIFY_ADDR_DOWN 109
  10. unsigned char g_ucIpSendTcpCt=0;
  11. static unsigned char ModifyAck;//终端修改操作响应0 不操作 1 心跳修改响应 2 地址修改响应
  12. static unsigned char ModifyResult;//修改结果 成功 '1' 失败 '0'
  13. static void ipSosReport(unsigned char socketID);
  14. static void ipSosHeart(unsigned char socketID);
  15. static void ipSosChangeParaAck(unsigned char socketID);
  16. static unsigned char ipSosFlag=0;
  17. void ipSosLogic(unsigned char socketID){
  18. char info[30];
  19. static unsigned short hearTick=0;
  20. unsigned char tcpSendAlready=0;
  21. if(tcpSocketGetStatus(socketID)) return;
  22. if(g_ucIpSendTcpCt>6){
  23. //如果发了3秒数据包都没响应,则主动关闭连接
  24. g_ucIpSendTcpCt=0;
  25. snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1);
  26. ModemSendAt(info);
  27. return;
  28. }
  29. if(ipSosFlag){
  30. tcpSendAlready=1;
  31. ipSosFlag=0;
  32. ipSosReport(socketID);
  33. }
  34. if(ModifyAck==1 || ModifyAck==2){
  35. tcpSendAlready=1;
  36. ModifyAck=0;
  37. ipSosChangeParaAck(socketID);
  38. }
  39. if(++hearTick>=newPara.ipSosHeartTick && !tcpSendAlready){
  40. //发送心跳时间到了
  41. if(isSendDataEnable()){
  42. hearTick=0;
  43. ipSosHeart(socketID);
  44. }
  45. }
  46. if(tcpSendAlready){
  47. g_ucIpSendTcpCt++;
  48. hearTick=0;//已发了其它包,心跳包重置计数
  49. }
  50. }
  51. void ipSosDataProcess(unsigned char *data, unsigned short len){
  52. unsigned int ackTmp;
  53. short ret;
  54. char *msg=(char *)data;
  55. //获取报文类型
  56. ret=getSegMent(msg, 1);
  57. if(ret<0) return;
  58. ackTmp=atoi(&msg[ret]);
  59. printf("ipAckType:%d\r\n",ackTmp);
  60. switch(ackTmp){
  61. case IP_SOS_HEART_DOWN:
  62. SlwTrace(INF, "IPSOS_HEART_ACK",1);
  63. break;
  64. case IP_SOS_REPORT_DOWN:
  65. ret=getSegMent(msg, 2);//获取上报结果
  66. printf("IPSOS_REPORT_ACK:");
  67. if(ret<0) printf("err");
  68. else printf("%d", atoi(&msg[ret]));
  69. printf("\r\n");
  70. break;
  71. case IP_SOS_MODIFY_HEART_DOWN:
  72. ret=getSegMent(msg, 2);//获取心跳值
  73. if(ret<0) ModifyResult='0';//修改失败
  74. else{
  75. //atoi(&msg[ret])
  76. ModifyResult='1';//修改成功
  77. }
  78. ModifyAck=1;//触发响应服务器修改结果
  79. break;
  80. case IP_SOS_MODIFY_ADDR_DOWN:
  81. ret=getSegMent(msg, 3);//获取IP地址
  82. if(ret<0){
  83. ModifyResult='0';//修改失败
  84. goto PRO_END;
  85. }
  86. ret=getSegMent(msg, 4);//获取端口
  87. if(ret<0){
  88. ModifyResult='0';//修改失败
  89. goto PRO_END;
  90. break;
  91. }
  92. //做保存参数及更新socket连接值
  93. ModifyResult='1';//修改成功
  94. PRO_END:
  95. ModifyAck=2;//触发响应服务器修改结果
  96. break;
  97. }
  98. g_ucIpSendTcpCt=0;
  99. }
  100. static unsigned short liushui=0;
  101. static USER_RTC_DEF reportTime;
  102. static void liuShuiUpdate(void){
  103. if(++liushui>9999) liushui=0;
  104. }
  105. static void upDateReportTime(void){
  106. RTC_UserGetTime(&reportTime);
  107. }
  108. void ipSosStartNow(void){
  109. ipSosFlag=1;
  110. upDateReportTime();
  111. }
  112. static void ipSosPackData(char *data, char reportType,char repeatNum){
  113. USER_RTC_DEF time;
  114. unsigned short i=0;
  115. char *ptr=data;
  116. char buf[20];
  117. RTC_UserGetTime(&time);
  118. //开始符号
  119. *ptr++='[';
  120. //0 唯一标识
  121. strcpy(ptr, getIMEI());ptr += strlen(getIMEI());
  122. *ptr++=',';
  123. //1 交易流水号
  124. snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",time.year, time.month,time.day,time.hour,time.min,time.second);
  125. strcpy(ptr, buf);ptr += strlen(buf);
  126. snprintf(buf, sizeof(buf), "%d",liushui);
  127. strcpy(ptr, buf);ptr += strlen(buf);
  128. *ptr++=',';
  129. //2 报文类型
  130. snprintf(buf, sizeof(buf), "%d",reportType);
  131. strcpy(ptr, buf);ptr += strlen(buf);
  132. *ptr++=',';
  133. //3 设备ICCID
  134. strcpy(ptr, getICCID());ptr += strlen(getICCID());
  135. *ptr++=',';
  136. switch(reportType){
  137. case IP_SOS_HEART_UP:
  138. //4 市电
  139. *ptr++='1';
  140. *ptr++=',';
  141. //5 电量百分比
  142. snprintf(buf, sizeof(buf), "%d%%",getBat());
  143. strcpy(ptr, buf);ptr += strlen(buf);
  144. *ptr++=',';
  145. //6 基站信息
  146. *ptr++=',';
  147. //7 GPS状态
  148. if(sutGpsInfo.isGpsWork) *ptr++='1';
  149. else *ptr++='0';
  150. *ptr++=',';
  151. //8 终端类型
  152. *ptr++=',';
  153. break;
  154. case IP_SOS_REPORT_UP:
  155. //4 重复发送序列
  156. *ptr++=repeatNum;
  157. *ptr++=',';
  158. //5 报警时间
  159. snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",reportTime.year, reportTime.month,reportTime.day,reportTime.hour,reportTime.min,reportTime.second);
  160. strcpy(ptr, buf);ptr += strlen(buf);
  161. *ptr++=',';
  162. //6 GPS信息
  163. *ptr++=',';
  164. //7 备用字段1
  165. *ptr++=',';
  166. //8 备用字段2
  167. *ptr++=',';
  168. //9 备用字段3
  169. *ptr++=',';
  170. //10 备用字段4
  171. *ptr++=',';
  172. //11 备用字段5
  173. *ptr++=',';
  174. break;
  175. case IP_SOS_MODIFY_HEART_UP:
  176. case IP_SOS_MODIFY_ADDR_UP:
  177. //4 修改结果
  178. *ptr++=ModifyResult;
  179. //5 备用字段1
  180. *ptr++=',';
  181. //6 备用字段2
  182. *ptr++=',';
  183. break;
  184. }
  185. //结束
  186. *ptr++=']';
  187. *ptr=0;
  188. }
  189. static void ipSosHeart(unsigned char socketID){
  190. char info[80];
  191. unsigned char len;
  192. SlwTrace(INF, "ipSosHeart",1);
  193. ipSosPackData(info,IP_SOS_HEART_UP,NULL);liuShuiUpdate();
  194. printf("***send[%d]%s\r\n", strlen(info),info);
  195. //tcpSocketSendData(socketID, (unsigned char *)info, strlen(info));
  196. }
  197. static void ipSosReport(unsigned char socketID){
  198. char i;
  199. char info[100];
  200. unsigned char len;
  201. for(i=0;i<3;i++){
  202. printf("ipSosReport:%d\r\n",i);
  203. ipSosPackData(info,IP_SOS_REPORT_UP,i+0x31);
  204. printf("***send[%d]%s\r\n", strlen(info),info);
  205. //tcpSocketSendData(socketID, (unsigned char *)info, strlen(info));
  206. __nop();
  207. }
  208. liuShuiUpdate();
  209. }
  210. static void ipSosChangeParaAck(unsigned char socketID){
  211. char info[80];
  212. unsigned char len;
  213. unsigned char cmd;
  214. printf("ipSosChangeParaAck:%d,%c",ModifyAck,ModifyResult);
  215. if(ModifyAck==1) cmd=IP_SOS_MODIFY_HEART_UP;
  216. else if(ModifyAck==2) cmd=IP_SOS_MODIFY_ADDR_UP;
  217. else return;
  218. ipSosPackData(info,cmd,NULL);liuShuiUpdate();
  219. printf("***send[%d]%s\r\n", strlen(info),info);
  220. //tcpSocketSendData(socketID, (unsigned char *)info, strlen(info));
  221. }