TSGpsProcess.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /**********************************************************************************
  2. * File Name: TSGpsProcess.c
  3. * Function Describe:device for Gps Process for TS/T808
  4. * Relate Module:
  5. * Explain: 采用TCP或UDP格式传输
  6. * Writer: wuzhao
  7. * Date: 2015.12.14
  8. *外设类型:0x01,
  9. *命令类型:0x05
  10. ***********************************************************************************/
  11. #define THIS_FILE_ID 24
  12. //-------------------------------------------------------------------------------------------
  13. #include "includes.h"
  14. SUT_GPS_SEND_DATA sutGpsSendData;
  15. //SUT_PRODUCT_PARA TSsutProductPara;
  16. REGISTRATION Regist;
  17. AVL_MSG_BODY_ATTRIB msgBodyAttrib;
  18. MSG_HEAD msghead;
  19. MSG_PACKET msgPacket;
  20. AUTH auth;
  21. SIM SimData;
  22. unsigned int a;
  23. unsigned int b;
  24. char GPSDataBuf[8];
  25. char GPSDataBuf1[8];
  26. //*p_buf -- *m_data size -- m_size
  27. /**********************************************************************************
  28. GpsSendHead
  29. ***********************************************************************************/
  30. void TSGpsServerConect(void)
  31. {
  32. int len;
  33. unsigned char buf[100];
  34. unsigned char temp[5];
  35. SUTDS ds;
  36. SlwTrace(INF, "Device ACK",1);
  37. memcpy(temp, (unsigned char*)&sutFeed.feedliushuiID,2);
  38. ds.Data.usData=sutFeed.feedID;
  39. temp[2] = ds.Data.ucData.b2;
  40. temp[3] = ds.Data.ucData.b1;
  41. temp[4] = sutFeed.result;
  42. len=TSGpsPacket(buf,sutProductPara.PSN,TS_TERMINAL_UNIVERSAL_ANSWER,temp,sizeof(temp));
  43. MC8332ModemSendTcpData(GPS_DATA_SOCKET,buf,len);
  44. }
  45. //从厂商编号到用户数据依次累加的累加和,取累加的档�8位作为校验码
  46. unsigned char TSGetCheckSum(unsigned char *pData,int len)
  47. {
  48. int i;
  49. unsigned char sum= pData[1]; //sum=0;
  50. for(i=1;i<len-1;i++){
  51. sum=sum^pData[i+1];
  52. }
  53. return sum;
  54. }
  55. /*************************************************************************************
  56. *
  57. **************************************************************************************/
  58. // replace 0x7d 0x01 with 0x7d and 0x7d 0x02 with 0x7e 接收
  59. //长度不会变大,只会变小或不变
  60. int reduce(unsigned char *pData,int DataLen)
  61. {
  62. unsigned char *p = pData;
  63. int data_len=0;
  64. int i;
  65. if(DataLen==0) return 0;
  66. for(i=0;i<DataLen-1;i++)
  67. {
  68. if(pData[i] == 0x7d && pData[i+1] == 0x01)
  69. {
  70. *p++ = 0x7d;i++;
  71. }else if(pData[i] == 0x7d && pData[i+1] == 0x02)
  72. {
  73. *p++ = 0x7e;i++;
  74. }else *p++=pData[i];
  75. data_len++;
  76. }
  77. return data_len;
  78. }
  79. // loop every character, replace 0x7d with 0x7d 0x01 and 0x7e with 0x7d 0x02 发送
  80. int escapse(unsigned char *pData,int DataLen) {
  81. unsigned char *p= (unsigned char *)malloc(DataLen * 2);
  82. unsigned char * q = p;
  83. int data_len = 0;
  84. int i;
  85. if (DataLen == 0) {
  86. return 0;
  87. }
  88. memset(p, 0, DataLen * 2);
  89. for (i = 0; i < DataLen; i ++) {
  90. if (pData[i] == 0x7d) {
  91. *q ++ = 0x7d;
  92. *q ++ = 0x01;
  93. data_len += 2;
  94. } else if (pData[i] == 0x7e) {
  95. *q ++ = 0x7d;
  96. *q ++ = 0x02;
  97. data_len += 2;
  98. } else {
  99. *q ++ = pData[i];
  100. data_len += 1;
  101. }
  102. }
  103. memcpy(pData, p, data_len);
  104. free(p);
  105. DataLen = data_len;
  106. //free(p);
  107. return DataLen;
  108. }
  109. /******************************************************/
  110. int* TenToTwo(int Datalen)
  111. {
  112. static int c[10]={0};
  113. int i=0;
  114. do {
  115. c[i++]=Datalen%2;
  116. Datalen/=2;
  117. } while(Datalen!=0);
  118. return c;
  119. }
  120. /**********************
  121. **消息头12字节,消息体属性2个字节,这两个字节的16位。低10位是消息体长度
  122. 因此消息消息最长小于1024字节,Bit13是分包标志,看终端发到平台一个消息,可能很长,一个消息体发不完,分包发出去那么
  123. Bit12置为1,消息头长度变成16字节,消息头的后4个字节为消息包分装项内容,总共
  124. 有多少个包,这次发送的消息包是分包中的第几个消息包
  125. sutProductPara.APN
  126. **************************/
  127. int TSGpsPacket(unsigned char *Buffer,unsigned long PSN,unsigned short ID,unsigned char *pData,int DataLen)
  128. {
  129. static unsigned short liushiID=0;
  130. unsigned char *pBuf=Buffer,sum;
  131. int j,flag = 0,i=0;
  132. int talpage=1,MSnum = 0;
  133. char Psnsv[12];
  134. static unsigned short snum = 1;
  135. unsigned short Len=DataLen+6;
  136. unsigned short pageItem=DataLen+12;
  137. snprintf(Psnsv, sizeof(Psnsv),"%010u",(unsigned int)PSN);
  138. pBuf[i++]=0x7e;
  139. pBuf[i++]=(ID>>8)&0xff;
  140. pBuf[i++]=ID&0xff;
  141. //消息体属性
  142. msgBodyAttrib.EncrypType=NO_ENCRYP;//NO_ENCRYP;
  143. msgBodyAttrib.msgBodyLength=(DataLen);
  144. pBuf[i++]=msgBodyAttrib.EncrypType;
  145. pBuf[i++]=msgBodyAttrib.msgBodyLength;
  146. for(j=0;j<6;j++){
  147. pBuf[i++]=SimData.SIM[j];
  148. }
  149. //消息流水号
  150. pBuf[i++] = (liushiID>>8)&0xff;
  151. pBuf[i++] = liushiID&0xff;
  152. liushiID++;
  153. if(pData!=NULL)
  154. {
  155. //snum++;
  156. //消息包封装项
  157. //msgPacket.msgPacketID=0x30;
  158. //msgPacket.msgTotalCount=0x30;
  159. //pBuf[i++] = msgPacket.msgPacketID;
  160. //pBuf[i++] = msgPacket.msgTotalCount;
  161. //MSnum++;
  162. //包封装项 4
  163. for(j=0;j<DataLen;j++)
  164. {//0
  165. pBuf[i++]= pData[j];
  166. }
  167. }
  168. sum = TSGetCheckSum(pBuf,i);
  169. pBuf[i++] = sum;
  170. //对整体数据,除sum进行转义
  171. i=escapse(&pBuf[1],i-1);//头的7E不转义
  172. i++;
  173. pBuf[i++] = 0x7e;
  174. return i;
  175. }
  176. /*******************************************************
  177. GPRMC信息,与,之间的子信息处理,存储到相应的结构体中元素中
  178. *******************************************************/
  179. void TSGPS_GPRMC_Handle(unsigned char *ch,unsigned char ch1) //1处
  180. {
  181. unsigned char x,y;
  182. // send_byte('d');
  183. switch(ch1)
  184. {
  185. case 0: //时间
  186. for(x=0,y=0;x<8;x++)
  187. {
  188. if(x==2||x==5){ sutGpsInfo.Time[x]=0x3a;}
  189. else sutGpsInfo.Time[x]=ch[y++];
  190. }
  191. // send_byte(time[7]);
  192. break;
  193. case 1: //状态
  194. sutGpsInfo.Status=ch[0];
  195. break;
  196. case 2: //纬度
  197. for(x=10,y=0;x<17;x++)
  198. {
  199. if(x==12||x==15) {}
  200. else sutGpsInfo.Location[x]=ch[y++];
  201. }
  202. break;
  203. case 3: //南北纬
  204. sutGpsInfo.Location[9]=ch[0];
  205. break;
  206. case 4: //经度
  207. for(x=1,y=0;x<8;x++){
  208. if(x==4||x==7){}
  209. else sutGpsInfo.Location[x]=ch[y++];
  210. }
  211. break;
  212. case 5: //东西经
  213. sutGpsInfo.Location[0]=ch[0];
  214. break;
  215. case 6: //航速
  216. for(x=0;x<6;x++){
  217. sutGpsInfo.Speed[x+1]=ch[x];
  218. }
  219. sutGpsInfo.Speed[6]=0x20;
  220. sutGpsInfo.Speed[7]=0x20;
  221. break;
  222. case 7: //信息自己定义
  223. break;
  224. case 8: //信息自己定义
  225. break;
  226. case 9: //信息自己定义
  227. break;
  228. case 10: //信息自己定义
  229. break;
  230. case 11: //信息自己定义
  231. break;
  232. default:break;
  233. }
  234. }
  235. /*******************************************************
  236. 处理GPRMC信息
  237. *******************************************************/
  238. void TSGPS_GPRMC_Alysis(char *RMC_Data) //2处
  239. {
  240. unsigned char i,j,k;
  241. unsigned char count;
  242. unsigned char temp[10];
  243. //send_byte('a');
  244. for(i=0;i<strlen(RMC_Data);)
  245. {
  246. switch(RMC_Data[i])
  247. {
  248. case '$':
  249. j=i++; //标记$的起始位置
  250. while(RMC_Data[i]!=',') //RMC_Data[i]==','时结束循环。
  251. {i++;}
  252. // uart1_sendbytes(GPRMC[RMC_Count].RMC_ID);
  253. break;
  254. case ',':
  255. j=i++; //标记,的起始位置
  256. while((RMC_Data[i]!=',')&&(RMC_Data[i]!='*')) //RMC_Data[i]==','或者=='*'时结束循环。
  257. {i++;}
  258. if(j+1!=i)
  259. {
  260. for(j=j+1,k=0;j<i;j++)
  261. {
  262. temp[k++]=RMC_Data[j];
  263. }
  264. temp[k]='\0';
  265. // send_byte(temp[0]);
  266. }
  267. else temp[0]='\0';
  268. // send_byte('c');
  269. TSGPS_GPRMC_Handle(temp,count++);
  270. break;
  271. case '*':
  272. i=strlen(RMC_Data); //处理完成,软件结束。
  273. break;
  274. default:break;
  275. }
  276. }
  277. // send_byte('e');
  278. }
  279. void TSGpsDataInit(void)
  280. {
  281. unsigned char *p;
  282. char buf2[20];
  283. p=(unsigned char *)&sutGpsSendData;
  284. memset(p, 0, sizeof(SUT_GPS_SEND_DATA));
  285. p=(unsigned char *)&sutGpsInfo;
  286. memset(p, 0, sizeof(SUT_GPS_INF));
  287. sutGpsInfo.isGpsAuthOk = 0;//未鉴权
  288. }
  289. /*************************************************************************
  290. *检查GPS 服务器发过来的数据包是否正确
  291. 非法 返回0
  292. 合法 返回命令码
  293. **************************************************************************/
  294. //"7e8300 000c 000009771810000001 4130313233343536373839 b87e"
  295. unsigned char TSGpsServerCheckPack(unsigned char *data,unsigned short DataLen)//3处
  296. {
  297. unsigned char cmd;
  298. unsigned short len;
  299. unsigned long PSN;
  300. //判断数据包的包头是否正确
  301. // escapse(data,DataLen);
  302. if(data[0]!=TSGPS_PACK_HEAD)return 0;
  303. //判断数据包的校验是否正确
  304. if(data[DataLen-2] != TSGetCheckSum(data,DataLen-2))
  305. return 255;
  306. //判断数据包的结尾是否正确
  307. if(data[DataLen-1] != TSGPS_PACK_END)
  308. return 255;
  309. //获取数据包的长度 并判断长度是否正确
  310. len=(unsigned short)data[3]*256+data[4];
  311. if(DataLen!=(len+5))return 255;
  312. // //判断终端编号是否正确
  313. // PSN=(unsigned long)data[5]<<24;
  314. // PSN+=(unsigned long)data[6]<<16;
  315. // PSN+=(unsigned long)data[7]<<8;
  316. // PSN+=(unsigned long)data[8];
  317. // if(PSN!=sutNetPara.PSN) return 0;
  318. //解析当前命令
  319. cmd = data[2];
  320. sutGpsInfo.HostCmd = cmd;
  321. return cmd;
  322. }
  323. int turn(int a)
  324. {
  325. a = ~a + 1;
  326. return a;
  327. }
  328. /**************************************************************************************************
  329. *看下 如果一直发注册信息会如何
  330. ***************************************************************************************************/
  331. void TSGpsTimingSendPos(void)
  332. {
  333. unsigned char SendBuf[100];
  334. // unsigned char Jbuf[10];
  335. // unsigned char Wbuf[10];
  336. signed int Jtmp;
  337. signed int Wtmp;
  338. // SlwTrace(INF,"TSGpsTimingSendPos",1);
  339. // TSMakeGpsSendData();
  340. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_LOCATION_INFORMATION_REPORTING,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  341. // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
  342. /***************M20*************///打包发送GPS数据。
  343. //AT+POC=525&时间&经度&纬度&高度&速度&方位&卫星数
  344. //time= ((sutGpsInfo.hour/16*10+sutGpsInfo.hour%16)*3600+(sutGpsInfo.minu/16*10+sutGpsInfo.minu%16)*60+(sutGpsInfo.sec/16*10+sutGpsInfo.sec%16)); //时间
  345. GetCoordinates(1);
  346. // snprintf(Jbuf,sizeof(Jbuf),"%9d",sutGpsInfo.testJD); //经度 纬度
  347. // snprintf(Wbuf,sizeof(Wbuf),"%9d",sutGpsInfo.testWD);
  348. if(sutGpsInfo.EWFlag=='W') Jtmp= turn(sutGpsInfo.testJD);
  349. else Jtmp=sutGpsInfo.testJD;
  350. if(sutGpsInfo.NSFlag=='S') Wtmp= turn(sutGpsInfo.testWD);//S
  351. else Wtmp=sutGpsInfo.testWD;
  352. snprintf(SendBuf,sizeof(SendBuf),"AT+POC=525&%9d&%d&%d&%d&%d&%d&%d\r\n",sutGpsInfo.SendTime,Jtmp,Wtmp,sutGpsInfo.hight,sutGpsInfo.AvgSpeed,sutGpsInfo.aspect,sutGpsInfo.startnum);
  353. SlwTrace(INF,"GPS start/**************************************************************/",1);
  354. ModemSendAT(SendBuf);
  355. SlwTrace(INF,SendBuf,1);
  356. SlwTrace(INF,"GPS end/***************************************************************/",1);
  357. return ;
  358. }
  359. /*********************************************************
  360. *************************************************************/
  361. unsigned long power(int base, int times)
  362. {
  363. int i;
  364. unsigned long rslt = 1;
  365. for(i=0; i<times; i++){
  366. rslt *= base;
  367. }
  368. return rslt;
  369. }
  370. /********************************************************
  371. BCD转10进制
  372. ********************************************************/
  373. unsigned long BCDtoDec(const unsigned char *bcd, int length)
  374. {
  375. int i, tmp;
  376. unsigned long dec = 0;
  377. for(i=0; i<length; i++)
  378. {
  379. tmp = ((bcd[i]>>4)&0x0F)*10 + (bcd[i]&0x0F);
  380. dec += tmp * power(100, length-1-i);
  381. }
  382. return dec;
  383. }
  384. /********************************************************
  385. 10进制转BCD
  386. ********************************************************/
  387. int DectoBCD(int Dec, unsigned char *Bcd, int length)
  388. {
  389. int i;
  390. int temp;
  391. for(i=length-1; i>=0; i--)
  392. {
  393. temp = Dec%100;
  394. Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
  395. Dec /= 100;
  396. }
  397. return 0;
  398. }
  399. //#define GPS_DEBUG_GPS_SEND_DATA
  400. /***************************************************************************
  401. *MakeGpsSendData
  402. 根据sutGpsInfo生成sutGpsSendData
  403. ****************************************************************************/
  404. void TSMakeGpsSendData(void)//5处
  405. {
  406. char sv[17];
  407. char data[3];
  408. char buf[10];
  409. int buf1[3];
  410. int j,temp,num;
  411. char *pTemp;
  412. char WWWW[5];
  413. static int Table[4]={0};
  414. static int Table1[4]={0};
  415. sutGpsSendData.Time[0] = sutGpsInfo.year;
  416. sutGpsSendData.Time[1] = sutGpsInfo.month;
  417. sutGpsSendData.Time[2] = sutGpsInfo.day;
  418. sutGpsSendData.Time[3] = sutGpsInfo.hour;
  419. sutGpsSendData.Time[4] = sutGpsInfo.minu;
  420. sutGpsSendData.Time[5] = sutGpsInfo.sec;
  421. sutGpsSendData.St=0x00;
  422. sutGpsSendData.status=0;
  423. if(sutGpsInfo.isGpsValid)
  424. {
  425. sutGpsSendData.status |=(1<<24);//b0 1 acc
  426. sutGpsSendData.status |=(1<<25);//b1 1 定位
  427. if(sutGpsInfo.NSFlag=='S')
  428. sutGpsSendData.status |=(1<<26);//b2 1 南纬
  429. if(sutGpsInfo.EWFlag=='W')
  430. sutGpsSendData.status |=(1<<27);//b3 1 西经
  431. sutGpsSendData.status |=(1<<28);//b4 1 运营
  432. }
  433. sutGpsSendData.Mileag=((sutGpsInfo.StatisticsMile>>8)&0xFF)|(((sutGpsInfo.StatisticsMile)&0xFF)<<8);
  434. sutGpsSendData.SSFF=((sutGpsInfo.speed>>8)&0xFF)|(((sutGpsInfo.speed)&0xFF)<<8);
  435. sutGpsSendData.Acpect=sutGpsInfo.aspect;
  436. GetCoordinates(1);
  437. sutGpsSendData.WWWW[0] = sutGpsInfo.testWD>>24;
  438. sutGpsSendData.WWWW[1] = sutGpsInfo.testWD>>16;
  439. sutGpsSendData.WWWW[2] = sutGpsInfo.testWD>>8;
  440. sutGpsSendData.WWWW[3] = sutGpsInfo.testWD;
  441. sutGpsSendData.JJJJ[0] = sutGpsInfo.testJD>>24;
  442. sutGpsSendData.JJJJ[1] = sutGpsInfo.testJD>>16;
  443. sutGpsSendData.JJJJ[2] = sutGpsInfo.testJD>>8;
  444. sutGpsSendData.JJJJ[3] = sutGpsInfo.testJD;
  445. }
  446. void AscStrTurnHexStr(char *AscStr, char *HexStr)
  447. {
  448. char *pAscStr=AscStr;
  449. int i=4;
  450. unsigned char d,h,l;
  451. while(i){
  452. d=(unsigned char)*pAscStr++;
  453. l=d&0x0f;
  454. h=d>>4;
  455. *HexStr++=AscToHex(h);
  456. *HexStr++=AscToHex(l);
  457. i--;
  458. }
  459. *HexStr=0;
  460. }