proBuBiao.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #include "includes.h"
  2. unsigned char buBiaoSosSendNow=0;
  3. unsigned char g_ucModemSentTcpCt=0;
  4. FEEDBACK_DEF sutFeed;
  5. static void buBiaoReg(unsigned char socketID);
  6. static void buBiaoAuth(unsigned char socketID);
  7. static void buBiaoLocation(unsigned char socketID);
  8. static void buBiaoTime(unsigned char socketID);
  9. static void buBiaoHeart(unsigned char socketID);
  10. static void buBiaoAck(unsigned char socketID);
  11. static void buBiaoSoS(unsigned char socketID);
  12. void buBiaoLogic(unsigned char socketID){
  13. static unsigned short tick=0;
  14. static unsigned char retry=0;
  15. static unsigned char autTick=0;
  16. static unsigned short timeReqCnt=0;
  17. static unsigned short heartTick=0;
  18. static unsigned short locationTick=0;
  19. static unsigned char locationHold=0;
  20. unsigned char tcpSendAlready=0;
  21. unsigned short temp;
  22. char info[30];
  23. if(buBiaoAddrChange){
  24. buBiaoAddrChange=0;
  25. SocketParaUpdate(socketID,showGIP,1395);
  26. }
  27. if(tcpSocketGetStatus(socketID)) return;
  28. if(sutGpsInfo.isGpsAuthOk==0){//未鉴权
  29. timeReqCnt=500;
  30. if(++autTick>=10*3){ //15S
  31. autTick=0;
  32. if(++retry>=3){//鉴权3次都没成功,注册
  33. retry=0;
  34. buBiaoReg(socketID);
  35. }else buBiaoAuth(socketID);//鉴权
  36. }
  37. return;
  38. }
  39. //已鉴权了,定时发心跳,获取时间,发送定位
  40. if(g_ucModemSentTcpCt>6){
  41. //如果发了3秒数据包都没响应,则主动关闭连接
  42. g_ucModemSentTcpCt=0;
  43. snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1);
  44. ModemSendAt(info);
  45. sutGpsInfo.isGpsAuthOk=0;
  46. return;
  47. }
  48. if(sutGpsInfo.isGpsValid && g_uiGpsStat){
  49. temp=GPS_TimeTable[newPara.gpsTimeIndex]*2;
  50. if(++locationTick>=temp){
  51. //定位包时间到了
  52. locationTick=0;
  53. locationHold=1;
  54. }
  55. if(locationHold && isSendDataEnable()){
  56. locationHold=0;
  57. tcpSendAlready=1;
  58. // snprintf(info, sizeof(info), "AT+GPS=\"OFF\"\r\n");
  59. // ModemSendAt(info);
  60. // SlwTrace(INF,info,0);
  61. buBiaoLocation(socketID);
  62. }
  63. }
  64. if(++timeReqCnt>=360 && !tcpSendAlready){
  65. //获取时间到了
  66. if(isSendDataEnable()){
  67. timeReqCnt=0;
  68. tcpSendAlready=1;
  69. buBiaoTime(socketID);
  70. }
  71. }
  72. if(sutFeed.feedBackStart!=0 && !tcpSendAlready){
  73. //需要主动应用服务器
  74. sutFeed.feedBackStart=0;
  75. tcpSendAlready=1;
  76. buBiaoAck(socketID);
  77. }
  78. if(buBiaoSosSendNow && !tcpSendAlready){
  79. buBiaoSosSendNow=0;
  80. tcpSendAlready=1;
  81. buBiaoSoS(socketID);
  82. }
  83. if(++heartTick>=120 && !tcpSendAlready){
  84. //发送心跳时间到了
  85. if(isSendDataEnable()){
  86. heartTick=0;
  87. buBiaoHeart(socketID);
  88. }
  89. }
  90. if(tcpSendAlready){
  91. g_ucModemSentTcpCt++;
  92. heartTick=0;//已发了其它包,心跳包重置计数
  93. }
  94. }
  95. void buBiaoDataProcess(unsigned char *pMsg, unsigned short tempLen){
  96. char buf[20];
  97. char buf2[50];
  98. char result[10];
  99. unsigned short cmd,ackCmd,smsLen;
  100. unsigned char Result,i,buBiaoResponsed;
  101. short targetIndex;
  102. SUTDS ds;
  103. USER_RTC_DEF tmpTime;
  104. cmd = (pMsg[1]<<8) | (pMsg[2]&0xff);
  105. buBiaoResponsed=1;
  106. snprintf(buf2, sizeof(buf2), "\r\ncmd=%04x", cmd);
  107. SlwTrace(INF, buf2,1);
  108. switch(cmd)
  109. {//布标命令返回
  110. case TS_TERMINAL_REGISTRATION_REPLY://终端注册应答
  111. //7E 8100 000A 000166000016 0250 0100 00 20161118165030 E07E 成功
  112. //7E 8100 0003 000166000016 0251 0100 01 A07E 已注册
  113. Result = pMsg[15];
  114. snprintf(buf2, sizeof(buf2), "\r\nresult=%02x", Result);
  115. SlwTrace(INF, buf2,1);
  116. switch(Result)
  117. {
  118. case 0://00注册成功
  119. if(0 != memcmp(newPara.authPara.authCode, pMsg+16, 7)){
  120. memcpy(newPara.authPara.authCode, pMsg+16,7);
  121. //save sys
  122. ModifyConfigureFile(0);
  123. }
  124. snprintf(result, sizeof(result), "Ok");
  125. break;
  126. case 1://01 车辆已注册
  127. snprintf(result, sizeof(result), "Car Exist");break;
  128. case 2://02 无此车辆
  129. snprintf(result, sizeof(Result), "No car");break;
  130. case 3://03 终端已经被注册
  131. snprintf(result, sizeof(result), "Device Exist");break;
  132. case 4://04 数据库无此终端
  133. snprintf(result, sizeof(result), "No Record");break;
  134. default://无效值
  135. //统称失败
  136. snprintf(result, sizeof(result), "Faile");
  137. break;
  138. }
  139. snprintf(buf2, sizeof(buf2), "\r\n[Register]%d %s", Result,result);
  140. SlwTrace(INF, buf2,1);
  141. break;
  142. case TS_TEXT_INFORMATION_ISSUED://文本下发
  143. //7E83000005000166000009000001 31323334ED7E
  144. smsLen = pMsg[3];
  145. smsLen <<= 8;
  146. smsLen &= 0xff00;
  147. smsLen |= pMsg[4];
  148. smsLen -= 1;//去掉后面较验码
  149. if(SMS_SIGNAL_LEN <= smsLen)
  150. smsLen = SMS_SIGNAL_LEN;
  151. memset(sutMess.GBKMess1, 0, sizeof(sutMess.GBKMess1));
  152. memcpy(sutMess.GBKMess1, pMsg+14, smsLen);
  153. sutMess.len = smsLen;
  154. //////////////////////////
  155. sutFeed.feedID=TS_SETTING_TERMINAL_PARAMETERS;
  156. ds.Data.ucData.b1=pMsg[11];
  157. ds.Data.ucData.b2=pMsg[12];
  158. sutFeed.feedliushuiID=ds.Data.usData;
  159. sutFeed.result=2;
  160. //////////////////////////
  161. if(sutMess.len > 0)
  162. SetGotNewMessage();
  163. break;
  164. case TS_PLATFORM_UNIVERSAL_ANSWER://通用平台应答
  165. //7E8001000500016600001602400000010201B57E
  166. //buf[0]=0x7e;
  167. //if(-1 != (targetIndex=FindTargetIndex((char *)pMsg, '\r', buf, 1,2)))
  168. targetIndex = tempLen;//这个长度是去掉了两个7E的长度
  169. {
  170. ackCmd=(pMsg[targetIndex-3]<<8) | (pMsg[targetIndex-2]&0xff);
  171. Result = pMsg[targetIndex-1];
  172. snprintf(buf2, sizeof(buf2), "\r\nLen=%d,ackCmd=%04x,result=%02x", tempLen,ackCmd,Result);
  173. SlwTrace(INF, buf2,1);
  174. switch(ackCmd)
  175. {
  176. case TS_TERMINAL_AUTHENTICATION://终端鉴权应答
  177. if(Result == 0)
  178. {//鉴权成功
  179. sutGpsInfo.isGpsAuthOk=1;
  180. snprintf(buf2, sizeof(buf2), "\r\n[ATH]Ok");
  181. enableDataSend();
  182. //TSGpsServerConect();//鉴权成功后发一包终端应答包即可以支持文本接收了
  183. }else
  184. {
  185. sutGpsInfo.isGpsAuthOk=0;
  186. snprintf(buf2, sizeof(buf2), "\r\n[ATH]Faile");
  187. }
  188. SlwTrace(INF, buf2,1);
  189. break;
  190. case TS_LOCATION_INFORMATION_REPORTING://位置包应答
  191. SlwTrace(INF, "\r\nLocation ACK",1);
  192. break;
  193. case TS_TERMINAL_HEARTBEAT://心跳应答
  194. SlwTrace(INF, "\r\nHearTick ACK",1);
  195. break;
  196. case TS_SEND_SOS_SMS://SOS应答
  197. SlwTrace(INF, "\r\nSOS ACK",1);
  198. break;
  199. }
  200. }
  201. break;
  202. case TS_TERMINAL_REQTIME://时间ACK
  203. //7e0f020007001899000022003a e20704130e2f02 427e
  204. SlwTrace(INF, "\r\nTimeAck",1);
  205. ds.Data.ucData.b1=pMsg[13];
  206. ds.Data.ucData.b2=pMsg[14];
  207. tmpTime.year=ds.Data.usData;
  208. tmpTime.month=pMsg[15];
  209. tmpTime.day=pMsg[16];
  210. tmpTime.hour=pMsg[17];
  211. tmpTime.min=pMsg[18];
  212. tmpTime.second=pMsg[19];
  213. RTC_UserSetTime(tmpTime);
  214. printf("\r\nNewTime:%02d-%02d-%02d %02d:%02d:%02d\r\n",
  215. tmpTime.year,tmpTime.month,tmpTime.day,
  216. tmpTime.hour,tmpTime.min,tmpTime.second);
  217. break;
  218. default:
  219. buBiaoResponsed=0;
  220. break;
  221. }
  222. g_ucModemSentTcpCt=0;
  223. /////////////处理其它类型指令
  224. }
  225. static int TSGpsPacket(unsigned char *Buffer,unsigned long PSN,unsigned short ID,unsigned char *pData,int DataLen);
  226. static void buBiaoReg(unsigned char socketID){
  227. unsigned char info[100];
  228. unsigned short PacketLen;
  229. REGISTRATION Regist;
  230. SlwTrace(INF, "buBiaoReg",1);
  231. memset((unsigned char *)&Regist, 0, sizeof(REGISTRATION));
  232. Regist.TerID[0]=(unsigned char)((sutProductPara.PSN>>24)&0xff);
  233. Regist.TerID[1]=(unsigned char)((sutProductPara.PSN>>16)&0xff);
  234. Regist.TerID[2]=(unsigned char)((sutProductPara.PSN>>8)&0xff);
  235. Regist.TerID[3]=(unsigned char)(sutProductPara.PSN&0xff);
  236. PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_REGISTRATION,(unsigned char *)&Regist,sizeof(REGISTRATION));
  237. tcpSocketSendData(socketID, info, PacketLen);
  238. }
  239. static void buBiaoAuth(unsigned char socketID){
  240. unsigned char info[80];
  241. unsigned short PacketLen;
  242. SlwTrace(INF, "buBiaoAuth",1);
  243. PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_AUTHENTICATION,newPara.authPara.authCode,sizeof(newPara.authPara.authCode));//实际是返回的鉴权信息
  244. tcpSocketSendData(socketID, info, PacketLen);
  245. PrintBufferData(info,PacketLen);
  246. }
  247. static void buBiaoLocation(unsigned char socketID){
  248. unsigned char info[80];
  249. unsigned short PacketLen;
  250. SUT_GPS_SEND_DATA sutGpsSendData;
  251. SlwTrace(INF, "buBiaoLocation",1);
  252. TSMakeGpsSendData(&sutGpsSendData);
  253. PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_LOCATION_INFORMATION_REPORTING,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  254. tcpSocketSendData(socketID, info, PacketLen);
  255. }
  256. static void buBiaoTime(unsigned char socketID){
  257. unsigned char info[50];
  258. unsigned short PacketLen;
  259. SlwTrace(INF, "buBiaoTime",1);
  260. PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_REQTIME,0,0);
  261. tcpSocketSendData(socketID, info, PacketLen);
  262. }
  263. static void buBiaoHeart(unsigned char socketID){
  264. unsigned char info[100];
  265. unsigned short PacketLen;
  266. SlwTrace(INF, "buBiaoHeart",1);
  267. PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_HEARTBEAT,0,0);
  268. tcpSocketSendData(socketID, info, PacketLen);
  269. }
  270. static void buBiaoAck(unsigned char socketID){
  271. int len;
  272. unsigned char info[100];
  273. unsigned char temp[5];
  274. SUTDS ds;
  275. SlwTrace(INF, "buBiaoAck",1);
  276. memcpy(temp, (unsigned char*)&sutFeed.feedliushuiID,2);
  277. ds.Data.usData=sutFeed.feedID;
  278. temp[2] = ds.Data.ucData.b2;
  279. temp[3] = ds.Data.ucData.b1;
  280. temp[4] = sutFeed.result;
  281. len=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_UNIVERSAL_ANSWER,temp,sizeof(temp));
  282. tcpSocketSendData(socketID, info, len);
  283. }
  284. static void buBiaoSoS(unsigned char socketID){
  285. unsigned char info[50];
  286. unsigned short PacketLen;
  287. SlwTrace(INF, "buBiaoSoS",1);
  288. PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_SEND_SOS_SMS,0,0);
  289. tcpSocketSendData(socketID, info, PacketLen);
  290. }
  291. static unsigned char TSGetCheckSum(unsigned char *pData,int len){
  292. int i;
  293. unsigned char sum= pData[1]; //sum=0;
  294. for(i=1;i<len-1;i++){
  295. sum=sum^pData[i+1];
  296. }
  297. return sum;
  298. }
  299. static int escapse(unsigned char *pData,int DataLen);
  300. /**********************
  301. **消息头12字节,消息体属性2个字节,这两个字节的16位。低10位是消息体长度
  302. 因此消息消息最长小于1024字节,Bit13是分包标志,看终端发到平台一个消息,可能很长,一个消息体发不完,分包发出去那么
  303. Bit12置为1,消息头长度变成16字节,消息头的后4个字节为消息包分装项内容,总共
  304. 有多少个包,这次发送的消息包是分包中的第几个消息包
  305. sutProductPara.APN
  306. **************************/
  307. static int TSGpsPacket(unsigned char *Buffer,unsigned long PSN,unsigned short ID,unsigned char *pData,int DataLen){
  308. AVL_MSG_BODY_ATTRIB msgBodyAttrib;
  309. static unsigned short liushiID=0;
  310. unsigned char *pBuf=Buffer,sum;
  311. int j,flag = 0,i=0;
  312. int talpage=1,MSnum = 0;
  313. char Psnsv[12];
  314. static unsigned short snum = 1;
  315. unsigned short Len=DataLen+6;
  316. unsigned short pageItem=DataLen+12;
  317. snprintf(Psnsv, sizeof(Psnsv),"%010u",(unsigned int)PSN);
  318. pBuf[i++]=0x7e;
  319. pBuf[i++]=(ID>>8)&0xff;
  320. pBuf[i++]=ID&0xff;
  321. //消息体属性
  322. msgBodyAttrib.EncrypType=NO_ENCRYP;//NO_ENCRYP;
  323. msgBodyAttrib.msgBodyLength=(DataLen);
  324. pBuf[i++]=msgBodyAttrib.EncrypType;
  325. pBuf[i++]=msgBodyAttrib.msgBodyLength;
  326. //SIM卡号
  327. pBuf[i++]=0; //
  328. for (j=0;j<10;j+=2) {
  329. pBuf[i++]=(Psnsv[j]<<4)|(Psnsv[j+1]&0x0F);
  330. }
  331. //消息流水号
  332. pBuf[i++] = (liushiID>>8)&0xff;
  333. pBuf[i++] = liushiID&0xff;
  334. liushiID++;
  335. if(pData!=NULL){
  336. for(j=0;j<DataLen;j++){//0
  337. pBuf[i++]= pData[j];
  338. }
  339. }
  340. sum = TSGetCheckSum(pBuf,i);
  341. pBuf[i++] = sum;
  342. //对整体数据,除sum进行转义
  343. i=escapse(&pBuf[1],i-1);//头的7E不转义
  344. i++;
  345. pBuf[i++] = 0x7e;
  346. return i;
  347. }
  348. static int escapse(unsigned char *pData,int DataLen) {
  349. unsigned char *p= (unsigned char *)malloc(DataLen * 2);
  350. unsigned char * q = p;
  351. int data_len = 0;
  352. int i;
  353. if (DataLen == 0) {
  354. return 0;
  355. }
  356. memset(p, 0, DataLen * 2);
  357. for (i = 0; i < DataLen; i ++) {
  358. if (pData[i] == 0x7d) {
  359. *q ++ = 0x7d;
  360. *q ++ = 0x01;
  361. data_len += 2;
  362. } else if (pData[i] == 0x7e) {
  363. *q ++ = 0x7d;
  364. *q ++ = 0x02;
  365. data_len += 2;
  366. } else {
  367. *q ++ = pData[i];
  368. data_len += 1;
  369. }
  370. }
  371. memcpy(pData, p, data_len);
  372. free(p);
  373. DataLen = data_len;
  374. //free(p);
  375. return DataLen;
  376. }
  377. // replace 0x7d 0x01 with 0x7d and 0x7d 0x02 with 0x7e 接收
  378. //长度不会变大,只会变小或不变
  379. int reduce(unsigned char *pData,int DataLen)
  380. {
  381. unsigned char *p = pData;
  382. int data_len=0;
  383. int i;
  384. if(DataLen==0) return 0;
  385. for(i=0;i<DataLen-1;i++)
  386. {
  387. if(pData[i] == 0x7d && pData[i+1] == 0x01)
  388. {
  389. *p++ = 0x7d;i++;
  390. }else if(pData[i] == 0x7d && pData[i+1] == 0x02)
  391. {
  392. *p++ = 0x7e;i++;
  393. }else *p++=pData[i];
  394. data_len++;
  395. }
  396. return data_len;
  397. }
  398. void TSMakeGpsSendData(SUT_GPS_SEND_DATA *sendGpsData)
  399. {
  400. char sv[17];
  401. char data[3];
  402. char buf[10];
  403. int buf1[3];
  404. int j,temp,num;
  405. char *pTemp;
  406. char WWWW[5];
  407. static int Table[4]={0};
  408. static int Table1[4]={0};
  409. sendGpsData->Time[0] = sutGpsInfo.year;
  410. sendGpsData->Time[1] = sutGpsInfo.month;
  411. sendGpsData->Time[2] = sutGpsInfo.day;
  412. sendGpsData->Time[3] = sutGpsInfo.hour;
  413. sendGpsData->Time[4] = sutGpsInfo.minu;
  414. sendGpsData->Time[5] = sutGpsInfo.sec;
  415. sendGpsData->St=0x00;
  416. sendGpsData->status=0;
  417. sendGpsData->status |=(1<<24);//b0 1 acc
  418. sendGpsData->status |=(1<<25);//b1 1 定位
  419. if(sutGpsInfo.NSFlag=='S')
  420. sendGpsData->status |=(1<<26);//b2 1 南纬
  421. if(sutGpsInfo.EWFlag=='W')
  422. sendGpsData->status |=(1<<27);//b3 1 西经
  423. sendGpsData->status |=(1<<28);//b4 1 运营
  424. sendGpsData->Mileag=((sutGpsInfo.StatisticsMile>>8)&0xFF)|(((sutGpsInfo.StatisticsMile)&0xFF)<<8);
  425. sendGpsData->SSFF=((sutGpsInfo.speed>>8)&0xFF)|(((sutGpsInfo.speed)&0xFF)<<8);
  426. sendGpsData->Acpect=sutGpsInfo.aspect;
  427. GetCoordinates(1);
  428. sendGpsData->WWWW[0] = sutGpsInfo.testWD>>24;
  429. sendGpsData->WWWW[1] = sutGpsInfo.testWD>>16;
  430. sendGpsData->WWWW[2] = sutGpsInfo.testWD>>8;
  431. sendGpsData->WWWW[3] = sutGpsInfo.testWD;
  432. sendGpsData->JJJJ[0] = sutGpsInfo.testJD>>24;
  433. sendGpsData->JJJJ[1] = sutGpsInfo.testJD>>16;
  434. sendGpsData->JJJJ[2] = sutGpsInfo.testJD>>8;
  435. sendGpsData->JJJJ[3] = sutGpsInfo.testJD;
  436. }
  437. void TSGpsDataInit(void)
  438. {
  439. memset((unsigned char *)&sutGpsInfo, 0, sizeof(SUT_GPS_INF));
  440. printf("Gpstime=%d\r\n",GPS_TimeTable[newPara.gpsTimeIndex]);
  441. }
  442. void PrintBufferData(unsigned char *data, unsigned short length)
  443. {
  444. unsigned short i;
  445. printf("Data[%d]:\r\n",length);
  446. for(i=0;i<length;i++) printf("%02x",data[i]);
  447. printf("\r\n");
  448. }