123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #include "includes.h"
- #define IP_SOS_HEART_UP 49
- #define IP_SOS_HEART_DOWN 54
- #define IP_SOS_REPORT_UP 40
- #define IP_SOS_REPORT_DOWN 41
- #define IP_SOS_MODIFY_HEART_UP 96
- #define IP_SOS_MODIFY_HEART_DOWN 95
- #define IP_SOS_MODIFY_ADDR_UP 108
- #define IP_SOS_MODIFY_ADDR_DOWN 109
- unsigned char g_ucIpSendTcpCt=0;
- static unsigned char ModifyAck;//终端修改操作响应0 不操作 1 心跳修改响应 2 地址修改响应
- static unsigned char ModifyResult;//修改结果 成功 '1' 失败 '0'
- static void ipSosReport(unsigned char socketID);
- static void ipSosHeart(unsigned char socketID);
- static void ipSosChangeParaAck(unsigned char socketID);
- static unsigned char ipSosFlag=0;
- void ipSosLogic(unsigned char socketID){
- char info[30];
- static unsigned short hearTick=0;
- unsigned char tcpSendAlready=0;
- if(tcpSocketGetStatus(socketID)) return;
-
- if(g_ucIpSendTcpCt>6){
- //如果发了3秒数据包都没响应,则主动关闭连接
- g_ucIpSendTcpCt=0;
- snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1);
- ModemSendAt(info);
- return;
- }
-
- if(ipSosFlag){
- tcpSendAlready=1;
- ipSosFlag=0;
- ipSosReport(socketID);
- }
- if(ModifyAck==1 || ModifyAck==2){
- tcpSendAlready=1;
- ModifyAck=0;
- ipSosChangeParaAck(socketID);
- }
- if(++hearTick>=newPara.ipSosHeartTick && !tcpSendAlready){
- //发送心跳时间到了
- if(isSendDataEnable()){
- hearTick=0;
- ipSosHeart(socketID);
- }
- }
- if(tcpSendAlready){
- g_ucIpSendTcpCt++;
- hearTick=0;//已发了其它包,心跳包重置计数
- }
- }
- void ipSosDataProcess(unsigned char *data, unsigned short len){
- unsigned int ackTmp;
- short ret;
- char *msg=(char *)data;
-
- //获取报文类型
- ret=getSegMent(msg, 1);
- if(ret<0) return;
- ackTmp=atoi(&msg[ret]);
- printf("ipAckType:%d\r\n",ackTmp);
- switch(ackTmp){
- case IP_SOS_HEART_DOWN:
- SlwTrace(INF, "IPSOS_HEART_ACK",1);
- break;
- case IP_SOS_REPORT_DOWN:
- ret=getSegMent(msg, 2);//获取上报结果
- printf("IPSOS_REPORT_ACK:");
- if(ret<0) printf("err");
- else printf("%d", atoi(&msg[ret]));
- printf("\r\n");
- break;
- case IP_SOS_MODIFY_HEART_DOWN:
- ret=getSegMent(msg, 2);//获取心跳值
- if(ret<0) ModifyResult='0';//修改失败
- else{
- //atoi(&msg[ret])
- ModifyResult='1';//修改成功
- }
- ModifyAck=1;//触发响应服务器修改结果
- break;
- case IP_SOS_MODIFY_ADDR_DOWN:
- ret=getSegMent(msg, 3);//获取IP地址
- if(ret<0){
- ModifyResult='0';//修改失败
- goto PRO_END;
- }
- ret=getSegMent(msg, 4);//获取端口
- if(ret<0){
- ModifyResult='0';//修改失败
- goto PRO_END;
- break;
- }
- //做保存参数及更新socket连接值
- ModifyResult='1';//修改成功
- PRO_END:
- ModifyAck=2;//触发响应服务器修改结果
- break;
- }
- g_ucIpSendTcpCt=0;
- }
- static unsigned short liushui=0;
- static USER_RTC_DEF reportTime;
- static void liuShuiUpdate(void){
- if(++liushui>9999) liushui=0;
- }
- static void upDateReportTime(void){
- RTC_UserGetTime(&reportTime);
- }
- void ipSosStartNow(void){
- ipSosFlag=1;
- upDateReportTime();
- }
- static void ipSosPackData(char *data, char reportType,char repeatNum){
- USER_RTC_DEF time;
-
- unsigned short i=0;
- char *ptr=data;
- char buf[20];
-
- RTC_UserGetTime(&time);
- //开始符号
- *ptr++='[';
- //0 唯一标识
- strcpy(ptr, getIMEI());ptr += strlen(getIMEI());
- *ptr++=',';
- //1 交易流水号
- snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",time.year, time.month,time.day,time.hour,time.min,time.second);
- strcpy(ptr, buf);ptr += strlen(buf);
- snprintf(buf, sizeof(buf), "%d",liushui);
- strcpy(ptr, buf);ptr += strlen(buf);
- *ptr++=',';
- //2 报文类型
- snprintf(buf, sizeof(buf), "%d",reportType);
- strcpy(ptr, buf);ptr += strlen(buf);
- *ptr++=',';
- //3 设备ICCID
- strcpy(ptr, getICCID());ptr += strlen(getICCID());
- *ptr++=',';
- switch(reportType){
- case IP_SOS_HEART_UP:
- //4 市电
- *ptr++='1';
- *ptr++=',';
- //5 电量百分比
- snprintf(buf, sizeof(buf), "%d%%",getBat());
- strcpy(ptr, buf);ptr += strlen(buf);
- *ptr++=',';
- //6 基站信息
- *ptr++=',';
- //7 GPS状态
- if(sutGpsInfo.isGpsWork) *ptr++='1';
- else *ptr++='0';
- *ptr++=',';
- //8 终端类型
- *ptr++=',';
- break;
- case IP_SOS_REPORT_UP:
- //4 重复发送序列
- *ptr++=repeatNum;
- *ptr++=',';
- //5 报警时间
- snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",reportTime.year, reportTime.month,reportTime.day,reportTime.hour,reportTime.min,reportTime.second);
- strcpy(ptr, buf);ptr += strlen(buf);
- *ptr++=',';
- //6 GPS信息
- *ptr++=',';
- //7 备用字段1
- *ptr++=',';
- //8 备用字段2
- *ptr++=',';
- //9 备用字段3
- *ptr++=',';
- //10 备用字段4
- *ptr++=',';
- //11 备用字段5
- *ptr++=',';
- break;
- case IP_SOS_MODIFY_HEART_UP:
- case IP_SOS_MODIFY_ADDR_UP:
- //4 修改结果
- *ptr++=ModifyResult;
- //5 备用字段1
- *ptr++=',';
- //6 备用字段2
- *ptr++=',';
- break;
- }
- //结束
- *ptr++=']';
- *ptr=0;
- }
- static void ipSosHeart(unsigned char socketID){
- char info[80];
- unsigned char len;
-
- SlwTrace(INF, "ipSosHeart",1);
- ipSosPackData(info,IP_SOS_HEART_UP,NULL);liuShuiUpdate();
- printf("***send[%d]%s\r\n", strlen(info),info);
- //tcpSocketSendData(socketID, (unsigned char *)info, strlen(info));
- }
- static void ipSosReport(unsigned char socketID){
- char i;
- char info[100];
- unsigned char len;
-
- for(i=0;i<3;i++){
- printf("ipSosReport:%d\r\n",i);
- ipSosPackData(info,IP_SOS_REPORT_UP,i+0x31);
- printf("***send[%d]%s\r\n", strlen(info),info);
- //tcpSocketSendData(socketID, (unsigned char *)info, strlen(info));
- __nop();
- }
- liuShuiUpdate();
- }
- static void ipSosChangeParaAck(unsigned char socketID){
- char info[80];
- unsigned char len;
- unsigned char cmd;
-
- printf("ipSosChangeParaAck:%d,%c",ModifyAck,ModifyResult);
- if(ModifyAck==1) cmd=IP_SOS_MODIFY_HEART_UP;
- else if(ModifyAck==2) cmd=IP_SOS_MODIFY_ADDR_UP;
- else return;
- ipSosPackData(info,cmd,NULL);liuShuiUpdate();
- printf("***send[%d]%s\r\n", strlen(info),info);
- //tcpSocketSendData(socketID, (unsigned char *)info, strlen(info));
- }
|