TSGpsProcess.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. //SIM卡号
  147. for(j=0;j<6;j++){
  148. pBuf[i++]=SimData.SIM[j];
  149. }
  150. //消息流水号
  151. pBuf[i++] = (liushiID>>8)&0xff;
  152. pBuf[i++] = liushiID&0xff;
  153. liushiID++;
  154. if(pData!=NULL)
  155. {
  156. //snum++;
  157. //消息包封装项
  158. //msgPacket.msgPacketID=0x30;
  159. //msgPacket.msgTotalCount=0x30;
  160. //pBuf[i++] = msgPacket.msgPacketID;
  161. //pBuf[i++] = msgPacket.msgTotalCount;
  162. //MSnum++;
  163. //包封装项 4
  164. for(j=0;j<DataLen;j++)
  165. {//0
  166. pBuf[i++]= pData[j];
  167. }
  168. }
  169. sum = TSGetCheckSum(pBuf,i);
  170. pBuf[i++] = sum;
  171. //对整体数据,除sum进行转义
  172. i=escapse(&pBuf[1],i-1);//头的7E不转义
  173. i++;
  174. pBuf[i++] = 0x7e;
  175. return i;
  176. }
  177. /************************************************************
  178. 位置信息汇报
  179. *************************************************************/
  180. /**********************************************************************************
  181. GpsSendPosition
  182. 发送位置数据
  183. Head(2) Cmd(2) Len(2) PSN(4) GpsData(34) Check(1) End(1)
  184. Cmd=0x0200
  185. 总长度=2+2+2+40=46
  186. ***********************************************************************************/
  187. void TSGpsSendPosition(void)
  188. {
  189. M_RESULT r;
  190. int i;
  191. int len;
  192. char tmp[5];
  193. char buf[150];
  194. unsigned char GpsBuf[50];
  195. sutGpsSendData.Acpect=0x12;
  196. //114.03524
  197. sutGpsSendData.JJJJ[0]=0x11;
  198. sutGpsSendData.JJJJ[1]=0x40;
  199. sutGpsSendData.JJJJ[2]=0x35;
  200. sutGpsSendData.JJJJ[3]=0x24;
  201. //22.62082
  202. sutGpsSendData.WWWW[0]=0x02;
  203. sutGpsSendData.WWWW[1]=0x26;
  204. sutGpsSendData.WWWW[2]=0x20;
  205. sutGpsSendData.WWWW[3]=0x82;
  206. sutGpsSendData.Mileag=0x34;
  207. sutGpsSendData.SSFF=0x56;
  208. sutGpsSendData.status=0x01;
  209. len=TSGpsPacket(GpsBuf,0x123456,0x0200,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  210. SlwTrace(DEBUG,"SendGpsD>>");
  211. for(i=0;i<len;i++){
  212. snprintf(tmp, sizeof(tmp),"%02x ",GpsBuf[i]);
  213. SlwTrace(DEBUG,tmp);
  214. }
  215. SlwTrace(DEBUG,"\r\n");
  216. r=ModemSendToSocket(0,GpsBuf,len);
  217. if(r==SUCCEED){
  218. SlwTrace(DEBUG,"Send OK!\r\n");
  219. }else if(r==FAIL){
  220. SlwTrace(DEBUG,"Send Fail!\r\n");
  221. }else{
  222. SlwTrace(DEBUG,"Send Timeout!\r\n");
  223. }
  224. }
  225. /*******************************************************
  226. GPRMC信息,与,之间的子信息处理,存储到相应的结构体中元素中
  227. *******************************************************/
  228. void TSGPS_GPRMC_Handle(unsigned char *ch,unsigned char ch1) //1处
  229. {
  230. // unsigned char x,y;
  231. //// send_byte('d');
  232. // switch(ch1)
  233. // {
  234. // case 0: //时间
  235. // for(x=0,y=0;x<8;x++)
  236. // {
  237. // if(x==2||x==5){ sutGpsInfo.Time[x]=0x3a;}
  238. // else sutGpsInfo.Time[x]=ch[y++];
  239. // }
  240. // // send_byte(time[7]);
  241. // break;
  242. // case 1: //状态
  243. // sutGpsInfo.Status=ch[0];
  244. // break;
  245. // case 2: //纬度
  246. // for(x=10,y=0;x<17;x++)
  247. // {
  248. // if(x==12||x==15) {}
  249. // else sutGpsInfo.Location[x]=ch[y++];
  250. // }
  251. // break;
  252. // case 3: //南北纬
  253. // sutGpsInfo.Location[9]=ch[0];
  254. // break;
  255. // case 4: //经度
  256. // for(x=1,y=0;x<8;x++){
  257. // if(x==4||x==7){}
  258. // else sutGpsInfo.Location[x]=ch[y++];
  259. // }
  260. // break;
  261. // case 5: //东西经
  262. // sutGpsInfo.Location[0]=ch[0];
  263. // break;
  264. // case 6: //航速
  265. // for(x=0;x<6;x++){
  266. // sutGpsInfo.Speed[x+1]=ch[x];
  267. // }
  268. // sutGpsInfo.Speed[6]=0x20;
  269. // sutGpsInfo.Speed[7]=0x20;
  270. // break;
  271. // case 7: //信息自己定义
  272. // break;
  273. // case 8: //信息自己定义
  274. // break;
  275. // case 9: //信息自己定义
  276. // break;
  277. // case 10: //信息自己定义
  278. // break;
  279. // case 11: //信息自己定义
  280. // break;
  281. // default:break;
  282. // }
  283. }
  284. /*******************************************************
  285. 处理GPRMC信息
  286. *******************************************************/
  287. void TSGPS_GPRMC_Alysis(char *RMC_Data) //2处
  288. {
  289. unsigned char i,j,k;
  290. unsigned char count;
  291. unsigned char temp[10];
  292. //send_byte('a');
  293. for(i=0;i<strlen(RMC_Data);)
  294. {
  295. switch(RMC_Data[i])
  296. {
  297. case '$':
  298. j=i++; //标记$的起始位置
  299. while(RMC_Data[i]!=',') //RMC_Data[i]==','时结束循环。
  300. {i++;}
  301. // uart1_sendbytes(GPRMC[RMC_Count].RMC_ID);
  302. break;
  303. case ',':
  304. j=i++; //标记,的起始位置
  305. while((RMC_Data[i]!=',')&&(RMC_Data[i]!='*')) //RMC_Data[i]==','或者=='*'时结束循环。
  306. {i++;}
  307. if(j+1!=i)
  308. {
  309. for(j=j+1,k=0;j<i;j++)
  310. {
  311. temp[k++]=RMC_Data[j];
  312. }
  313. temp[k]='\0';
  314. // send_byte(temp[0]);
  315. }
  316. else temp[0]='\0';
  317. // send_byte('c');
  318. TSGPS_GPRMC_Handle(temp,count++);
  319. break;
  320. case '*':
  321. i=strlen(RMC_Data); //处理完成,软件结束。
  322. break;
  323. default:break;
  324. }
  325. }
  326. // send_byte('e');
  327. }
  328. void TSGpsDataInit(void)
  329. {
  330. // unsigned char *p;
  331. // char buf2[20];
  332. // p=(unsigned char *)&sutGpsSendData;
  333. // memset(p, 0, sizeof(SUT_GPS_SEND_DATA));
  334. // p=(unsigned char *)&sutGpsInfo;
  335. // memset(p, 0, sizeof(SUT_GPS_INF));
  336. //
  337. // #if 1
  338. // //sutGpsInfo.SendTime = sutProductPara.m_GPS_SendTime;
  339. // sutGpsInfo.GPS_UploadTime = GPS_TimeTable[sutProductPara.m_GPS_TimeIndex];
  340. // snprintf(buf2, sizeof(buf2), "Gpstime=%d",sutGpsInfo.GPS_UploadTime);
  341. // SlwTrace(INF, buf2,1);
  342. // #else
  343. // sutGpsInfo.SendTime=10;//默认主动上报时间间隔 单位为15秒
  344. // sutGpsInfo.GPSSendTime=10;
  345. // #endif
  346. //
  347. // sutGpsInfo.isGpsAuthOk = 0;//未鉴权
  348. }
  349. /*************************************************************************
  350. *检查GPS 服务器发过来的数据包是否正确
  351. 非法 返回0
  352. 合法 返回命令码
  353. **************************************************************************/
  354. //"7e8300 000c 000009771810000001 4130313233343536373839 b87e"
  355. unsigned char TSGpsServerCheckPack(unsigned char *data,unsigned short DataLen)//3处
  356. {
  357. unsigned char cmd;
  358. unsigned short len;
  359. unsigned long PSN;
  360. //判断数据包的包头是否正确
  361. // escapse(data,DataLen);
  362. if(data[0]!=TSGPS_PACK_HEAD)return 0;
  363. //判断数据包的校验是否正确
  364. if(data[DataLen-2] != TSGetCheckSum(data,DataLen-2))
  365. return 255;
  366. //判断数据包的结尾是否正确
  367. if(data[DataLen-1] != TSGPS_PACK_END)
  368. return 255;
  369. //获取数据包的长度 并判断长度是否正确
  370. len=(unsigned short)data[3]*256+data[4];
  371. if(DataLen!=(len+5))return 255;
  372. // //判断终端编号是否正确
  373. // PSN=(unsigned long)data[5]<<24;
  374. // PSN+=(unsigned long)data[6]<<16;
  375. // PSN+=(unsigned long)data[7]<<8;
  376. // PSN+=(unsigned long)data[8];
  377. // if(PSN!=sutNetPara.PSN) return 0;
  378. //解析当前命令
  379. cmd = data[2];
  380. //sutGpsInfo.HostCmd = cmd;
  381. return cmd;
  382. }
  383. /**************************************************************************************************
  384. * // TS_PROID 440000
  385. // TS_CITYID 440300
  386. 车牌D4C1423738583134
  387. ***************************************************************************************************/
  388. void TSMakeSendData(void)
  389. {
  390. int i,len;
  391. // static int Table[20]={0};
  392. static int Table1[20]={0};
  393. static int Temp[8];
  394. static int Temp1[4];
  395. const unsigned char acTemp[]={
  396. 0x34,0x31,0x30,0x30,0x30,//制造商ID Regist.Manufacturers
  397. //32 30 31 34 30 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  398. 0x32,0x30,0x31,0x34,0x30,0x34,0x00,0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //终端型号Regist.TerMod
  399. 0x65,0x30,0x30,0x72, //终端IDRegist.TerID
  400. };
  401. const unsigned char TSacTemp[]={
  402. 0xD4,0xC1,0x42,0x37,0x38,0x58,0x31,0x34,
  403. };
  404. // len = strlen(sutProductPara.REG);
  405. // if(!len){
  406. // Regist.CityID = ((TS_CITYID>>8)&0xffff);
  407. // Regist.CityID = (TS_CITYID&0xffff);
  408. // Regist.CityID = ((TS_CITYID>>24)&0xffff);
  409. // Regist.CityID = ((TS_CITYID>>16)&0xffff);
  410. //
  411. // Regist.proID =((TS_PROID>>8)&0xff);
  412. // Regist.proID =(TS_PROID&0xff);
  413. //
  414. // for(i = 0; i < 5; i++){
  415. // Regist.Manufacturers[i] = acTemp[i];
  416. // }
  417. // for(i = 0; i < 20; i++){
  418. // Regist.TerMod[i]= acTemp[i+5];
  419. // }
  420. // for(i = 0; i < 4; i++){
  421. // Regist.TerID[i]= acTemp[i+25];
  422. // }
  423. //
  424. // Regist.PlateColor=0x01;//未上牌取值为0
  425. // // 车牌颜色为0 表示车辆VIN
  426. // for(i = 0; i < 8; i++){//7会返回
  427. // Regist.Plate[i] = TSacTemp[i];
  428. // }
  429. // }else{
  430. // Regist.proID =((sutProductPara.proID>>8)&0xff);
  431. // Regist.proID =(sutProductPara.proID&0xff);
  432. //
  433. // Regist.CityID = ((sutProductPara.CityID>>24)&0xffff);
  434. // Regist.CityID = ((sutProductPara.CityID>>16)&0xffff);
  435. // Regist.CityID = ((sutProductPara.CityID>>8)&0xffff);
  436. // Regist.CityID = (sutProductPara.CityID&0xffff);
  437. //
  438. // for(i = 0; i < 5; i++){
  439. // Regist.Manufacturers[i] = sutProductPara.Manufacturers[i];
  440. // }
  441. //
  442. // for(i = 0; i < 20; i++){
  443. // //sscanf(sutProductPara.TerMod+2*i,"%2x",&Table1[i]);
  444. // if((i<20)&&(sutProductPara.TerMod[i]!='0')){
  445. // //sscanf(sutProductPara.TerMod+2*i,"%2x",&Table1[i]);
  446. // Regist.TerMod[i]= sutProductPara.TerMod[i];
  447. // }else{
  448. // Regist.TerMod[i]=0x00;
  449. // }
  450. //
  451. // }
  452. // for(i = 0; i < 4; i++){
  453. // sscanf(sutProductPara.TerID+2*i,"%2x",&Temp1[i]);
  454. // Regist.TerID[i]= Temp1[i];
  455. // }
  456. // Regist.PlateColor=sutProductPara.PlateColor;//
  457. // for(i = 0; i < 8; i++){
  458. // sscanf(sutProductPara.Plate+2*i,"%2x",&Temp[i]);
  459. // Regist.Plate[i] = Temp[i];
  460. //
  461. // }
  462. // }
  463. }
  464. /************************************************************
  465. 终端注册、注销
  466. *3431303030制作商ID
  467. 3230313430340000000000000000000000000000终端型号
  468. 30303030313131 终端ID
  469. *************************************************************/
  470. uint32_t Process_TSRegistration(char* buf, uint32_t len)
  471. {
  472. // unsigned char SendBuf[100];
  473. // unsigned short PacketLen;
  474. //
  475. // if(g_ucSelect==1){
  476. // TSMakeSendData();
  477. // }else{
  478. // TSRegData();
  479. // }
  480. // //REGISTRATION Regist;
  481. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_REGISTRATION,(unsigned char *)&Regist,sizeof(REGISTRATION));
  482. // MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  483. // return len;
  484. }
  485. void Registration(void)
  486. {
  487. // SlwTrace(INF,"Try-Reg!",1);
  488. // if(sutProductPara.SIMFlag){SIMReg();}
  489. //#if 0
  490. // if(sutProductPara.Select==1) TSMakeSendStrData(); // 串口注册
  491. // else TSRegData(sutProductPara.PSN);
  492. //#else
  493. // TSRegData();
  494. //#endif
  495. }
  496. void RegistrationLogout(void)
  497. {
  498. unsigned char SendBuf[100];
  499. unsigned short PacketLen;
  500. unsigned char *data=NULL;
  501. PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_CANCELLATION,data,0);
  502. //MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  503. }
  504. /************************************************************
  505. 终端鉴权
  506. *************************************************************/
  507. void Authentication(void)
  508. {
  509. // unsigned char SendBuf[100];
  510. // unsigned short PacketLen;
  511. // SlwTrace(INF,"Re-Authentication!",1);
  512. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_AUTHENTICATION,(unsigned char *)&sutProductPara.AutH,sizeof(AUTH));//实际是返回的鉴权信息
  513. // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
  514. //}
  515. ////以下
  516. //uint32_t Process_TSAuthentication(char* buf, uint32_t len)
  517. //{
  518. // unsigned char SendBuf[100];
  519. // unsigned short PacketLen;
  520. //
  521. // TSMakeGpsSendData();
  522. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_AUTHENTICATION,(unsigned char *)&auth,sizeof(AUTH));
  523. // MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  524. // return len;
  525. }
  526. /************************************************************
  527. 位置信息查询
  528. *************************************************************/
  529. uint32_t Process_TSCenterGetPos_rsp(unsigned char* buf, unsigned short len)
  530. {
  531. unsigned char SendBuf[100];
  532. unsigned short PacketLen;
  533. TSMakeGpsSendData();
  534. PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_LOCATION_INFORMATION_INQUIRY,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  535. //MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  536. return 0;
  537. }
  538. uint32_t Process_TSClient_rsp(char* buf, unsigned short len)
  539. {
  540. // if(!sutGpsInfo.isServerLogin){
  541. // sutGpsInfo.isServerLogin=1;
  542. // SlwTrace(INF,"[Gps Logined]",1);
  543. // }
  544. // return 0;
  545. }
  546. /**************************************************************************************************
  547. *看下 如果一直发注册信息会如何
  548. ***************************************************************************************************/
  549. void TSGpsTimingSendPos(void)
  550. {
  551. // unsigned char SendBuf[100];
  552. // unsigned short PacketLen;
  553. // SlwTrace(INF,"TSGpsTimingSendPos",1);
  554. // TSMakeGpsSendData();
  555. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_LOCATION_INFORMATION_REPORTING,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  556. // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
  557. // return ;
  558. }
  559. /***********************************************************************************
  560. *服务器设置主动上报时间
  561. *数据格式
  562. HEAD1 HEAD2 CMD LEN1 LEN2 PSN1 PSN2 PSN3 PSN4 X1 X2 CHECK END
  563. 29 29 34 00 08 PSN[4] X1 X2 CHECK 0D
  564. time=X1*256+X2
  565. ***********************************************************************************/
  566. uint32_t Process_TSCenterGetPosTimerout(char* buf, uint32_t len)
  567. {
  568. // char SendBuf[100];
  569. // unsigned short t;
  570. // unsigned short PacketLen;
  571. // //跳过终端序列号4 bytes
  572. //
  573. // //获取时间
  574. // sutGpsInfo.GPS_UploadTime =((unsigned short)buf[9]&0xff<<8) + (unsigned short)buf[10]&0xff ;
  575. // snprintf(SendBuf, sizeof(SendBuf),"[GpsTime=%d]",sutGpsInfo.GPS_UploadTime);
  576. // SlwTrace(INF,SendBuf,1);
  577. // TSMakeGpsSendData();
  578. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_EVENT_REPORT,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  579. // MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  580. // return len;
  581. }
  582. /************************************************************
  583. 建立好连接后,向平台发送终端鉴权消息进行鉴权、建立链接
  584. *************************************************************/
  585. /************************************************************
  586. 连接建立和终端鉴权成功后,终端周期性向平台发送终端心跳信息,拼图收到后
  587. 向终端发送平台通用应答消息,发送周期由终端参数指定、链接维持
  588. *************************************************************/
  589. uint32_t Process_TSPlatformAnswer(char* buf, uint32_t len)
  590. {
  591. unsigned char SendBuf[100];
  592. unsigned short PacketLen;
  593. TSMakeGpsSendData();
  594. PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_PLATFORM_UNIVERSAL_ANSWER,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  595. //MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  596. return 0;
  597. }
  598. uint32_t Process_TSTerminalHeartbeat(char* buf, uint32_t len)
  599. {
  600. unsigned char SendBuf[100];
  601. unsigned short PacketLen;
  602. TSMakeGpsSendData();
  603. PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_HEARTBEAT,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
  604. //MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  605. return 0;
  606. }
  607. void TS_SendHeartTick(void)
  608. {
  609. // unsigned char SendBuf[100];
  610. // unsigned short PacketLen;
  611. // SlwTrace(INF, "HeartTick",1);
  612. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_HEARTBEAT,0,0);
  613. // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
  614. }
  615. void TS_SendSOSMessage(void)
  616. {
  617. // unsigned char SendBuf[100];
  618. // unsigned short PacketLen;
  619. // SlwTrace(INF, "SOS Send",1);
  620. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_SEND_SOS_SMS,0,0);
  621. // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
  622. }
  623. void TS_ReqRealTime(void)
  624. {
  625. // unsigned char SendBuf[100];
  626. // unsigned short PacketLen;
  627. // SlwTrace(INF, "ReqRealTime",1);
  628. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_REQTIME,0,0);
  629. // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
  630. }
  631. /************************************************************
  632. 平台主发消息,终端主发消息
  633. *************************************************************/
  634. /******************************************************************************
  635. *目前服务器只做了:注册,取消注册,位置信息,鉴权消息
  636. *******************************************************************************/
  637. TSCmd_struct TSCmd_info[] =
  638. {
  639. // {TS_TERMINAL_REGISTRATION, Process_TSRegistration},//终端注册
  640. // {TS_TERMINAL_REGISTRATION_REPLY, Process_TSRegistrationReply},//终端注册应答
  641. // {TS_TERMINAL_AUTHENTICATION, Process_TSAuthentication},//鉴权
  642. // {TS_SETTING_TERMINAL_PARAMETERS, Process_TSParametersSet},
  643. {TS_LOCATION_INFORMATION_INQUIRY, Process_TSCenterGetPos_rsp},//位置查询
  644. // {XINGAN_PACKET_CENTER_CMD_GET_STAT, Process_XinganCenterGetStat_rsp},
  645. // {XINGAN_PACKET_CENTER_CMD_RESET, Process_XinganCenterReset_rsp},
  646. // {XINGAN_PACKET_CENTER_CMD_DEFAULT_SET, Process_XinganCenterDefaultSet_rsp},
  647. // {TS_TERMINAL_HEARTBEAT, Process_TSCenterGetPosTimerout},
  648. // {XINGAN_PACKET_CENTER_CMD_GET_POS_LENOUT, Process_XinganCenterGetPosLenout},
  649. // {XINGAN_PACKET_CENTER_CMD_CLOSE_WARNING, Process_XinganCenterCloseWarning_rsp},
  650. // {XINGAN_PACKET_CENTER_CMD_OPEN_OIL_WAY, Process_XinganCenterOpenOilWay_rsp},
  651. // {XINGAN_PACKET_CENTER_CMD_CLOSE_OIL_WAY, Process_XinganCenterCloseOilWay_rsp},
  652. // {XINGAN_PACKET_CENTER_CMD_GET_VER, Process_XinganCenterGetVer_rsp},
  653. // {XINGAN_PACKET_CENTER_CMD_DIAL_CALL, Process_XinganCenterDialCall_rsp} ,
  654. // {XINGAN_PACKET_CENTER_CMD_HIGH_SPEED_WARNING, Process_XinganCenterHighSpeedWarning_rsp},
  655. // {XINGAN_PACKET_CENTER_CMD_STATISTICS_MILE, Process_XinganCenterStatisticsMile_rsp},
  656. // {XINGAN_PACKET_CENTER_CMD_UDP_IPPORT, Process_XinganCenterIPport_rsp},
  657. // {XINGAN_PACKET_CENTER_CMD_APN, Process_XinganCenterApn_rsp},
  658. //
  659. // {TS_EVENT_REPORT, Process_TSClient_rsp},
  660. //
  661. // {XINGAN_PACKET_CLIENT_CMD_CONNECT, Process_XinganClientConnectReq},
  662. // {XINGAN_PACKET_CLIENT_SEND_POS_DATA, Process_XinganClientSendPosData},
  663. // {XINGAN_PACKET_CLIENT_CMD_SEND_WARNING_DATA, Process_XinganClientSendWarnData},
  664. //{TS_QUERY_TERMINAL_PARAMETERS, Process_TSParametersQuery},
  665. //{TS_TERMINAL_CONTROL, Process_TSTerminalContrl},
  666. // {TS_PLATFORM_UNIVERSAL_ANSWER, Process_TSPlatformAnswer},
  667. // {TS_TERMINAL_HEARTBEAT, Process_TSTerminalHeartbeat},
  668. };
  669. TSGPS_process_packet GetTSProcessFun(uint32_t Cmd)
  670. {
  671. // uint32_t j;
  672. // for(j=0;j<sizeof(TSCmd_info)/sizeof(TSCmd_struct);j++)
  673. // {
  674. // if(Cmd == TSCmd_info[j].cmd)
  675. // {
  676. // return TSCmd_info[j].process_packet;
  677. // }
  678. // }
  679. // return NULL;
  680. }
  681. /***************************************************************
  682. 处理GPS服务器发过来的信息
  683. 返回:
  684. 无效消息 不处理 返回0
  685. 否则返回命令码
  686. ****************************************************************/
  687. unsigned char TSGpsServerProcess(unsigned char *data, unsigned short DataLen)//4处
  688. {
  689. unsigned char cmd;
  690. TSGPS_process_packet ProcessFun;
  691. cmd=TSGpsServerCheckPack(data,DataLen);
  692. if(255 == cmd) return 0;
  693. ProcessFun = GetTSProcessFun(cmd);
  694. if(ProcessFun !=NULL)
  695. {
  696. ProcessFun(data, DataLen);
  697. }
  698. return cmd;
  699. }
  700. /*********************************************************
  701. *************************************************************/
  702. unsigned long power(int base, int times)
  703. {
  704. int i;
  705. unsigned long rslt = 1;
  706. for(i=0; i<times; i++){
  707. rslt *= base;
  708. }
  709. return rslt;
  710. }
  711. /********************************************************
  712. BCD转10进制
  713. ********************************************************/
  714. unsigned long BCDtoDec(const unsigned char *bcd, int length)
  715. {
  716. int i, tmp;
  717. unsigned long dec = 0;
  718. for(i=0; i<length; i++)
  719. {
  720. tmp = ((bcd[i]>>4)&0x0F)*10 + (bcd[i]&0x0F);
  721. dec += tmp * power(100, length-1-i);
  722. }
  723. return dec;
  724. }
  725. /********************************************************
  726. 10进制转BCD
  727. ********************************************************/
  728. int DectoBCD(int Dec, unsigned char *Bcd, int length)
  729. {
  730. int i;
  731. int temp;
  732. for(i=length-1; i>=0; i--)
  733. {
  734. temp = Dec%100;
  735. Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
  736. Dec /= 100;
  737. }
  738. return 0;
  739. }
  740. //#define GPS_DEBUG_GPS_SEND_DATA
  741. /***************************************************************************
  742. *MakeGpsSendData
  743. 根据sutGpsInfo生成sutGpsSendData
  744. ****************************************************************************/
  745. void TSMakeGpsSendData(void)//5处
  746. {
  747. char sv[17];
  748. char data[3];
  749. char buf[10];
  750. int buf1[3];
  751. int j,temp,num;
  752. char *pTemp;
  753. char WWWW[5];
  754. static int Table[4]={0};
  755. static int Table1[4]={0};
  756. sutGpsSendData.Time[0] = sutGpsInfo.year;
  757. sutGpsSendData.Time[1] = sutGpsInfo.month;
  758. sutGpsSendData.Time[2] = sutGpsInfo.day;
  759. sutGpsSendData.Time[3] = sutGpsInfo.hour;
  760. sutGpsSendData.Time[4] = sutGpsInfo.minu;
  761. sutGpsSendData.Time[5] = sutGpsInfo.sec;
  762. sutGpsSendData.St=0x00;
  763. #if 0
  764. if(sutGpsInfo.isGpsValid)
  765. {//wjl 1121
  766. sutGpsSendData.status |=(0x01<<24);//ACC
  767. sutGpsSendData.status |=(0x01<<25);//定位标志
  768. sutGpsSendData.status |=(0x01<<28);//运营状态
  769. }else sutGpsSendData.status=0;
  770. #else
  771. sutGpsSendData.status=0;
  772. if(sutGpsInfo.isGpsValid)
  773. {
  774. // sutGpsSendData.status |=(1<<24);//b0 1 acc
  775. // sutGpsSendData.status |=(1<<25);//b1 1 定位
  776. // if(sutGpsInfo.NSFlag=='S')
  777. // sutGpsSendData.status |=(1<<26);//b2 1 南纬
  778. // if(sutGpsInfo.EWFlag=='W')
  779. // sutGpsSendData.status |=(1<<27);//b3 1 西经
  780. // sutGpsSendData.status |=(1<<28);//b4 1 运营
  781. //sutGpsSendData.status |=(1<<29);//b5 1 经纬度已加密
  782. //sutGpsSendData.status |=(1<<30);//b6 保留
  783. //sutGpsSendData.status |=(1<<31);//b7 保留
  784. //sutGpsSendData.status |=(1<<16);//b8 载状态-1
  785. //sutGpsSendData.status |=(1<<17);//b9 载状态-0
  786. //sutGpsSendData.status |=(1<<18);//b10 车辆油状态
  787. //sutGpsSendData.status |=(1<<19);//b11 车辆电路状态
  788. //sutGpsSendData.status |=(1<<20);//b12 车锁状态
  789. //sutGpsSendData.status |=(1<<21);//b13 门1状态
  790. //sutGpsSendData.status |=(1<<22);//b14 门2状态
  791. //sutGpsSendData.status |=(1<<23);//b15 门3状态
  792. //sutGpsSendData.status |=(1<<8);//b16 门4状态
  793. //sutGpsSendData.status |=(1<<9);//b17 门5状态
  794. //sutGpsSendData.status |=(1<<10);//b18 是否使用GPS卫星定位
  795. //sutGpsSendData.status |=(1<<11);//b19 是否使用北斗卫星定位
  796. //sutGpsSendData.status |=(1<<12);//b20 是否使用GLONASS卫星定位
  797. //sutGpsSendData.status |=(1<<13);//b21 是否使用Galileo卫星定位
  798. }
  799. #endif
  800. // sutGpsSendData.Mileag=((sutGpsInfo.StatisticsMile>>8)&0xFF)|(((sutGpsInfo.StatisticsMile)&0xFF)<<8);
  801. // sutGpsSendData.SSFF=((sutGpsInfo.speed>>8)&0xFF)|(((sutGpsInfo.speed)&0xFF)<<8);
  802. // sutGpsSendData.Acpect=sutGpsInfo.aspect;
  803. //
  804. // GetCoordinates(1);
  805. // sutGpsSendData.WWWW[0] = sutGpsInfo.testWD>>24;
  806. // sutGpsSendData.WWWW[1] = sutGpsInfo.testWD>>16;
  807. // sutGpsSendData.WWWW[2] = sutGpsInfo.testWD>>8;
  808. // sutGpsSendData.WWWW[3] = sutGpsInfo.testWD;
  809. // sutGpsSendData.JJJJ[0] = sutGpsInfo.testJD>>24;
  810. // sutGpsSendData.JJJJ[1] = sutGpsInfo.testJD>>16;
  811. // sutGpsSendData.JJJJ[2] = sutGpsInfo.testJD>>8;
  812. // sutGpsSendData.JJJJ[3] = sutGpsInfo.testJD;
  813. }
  814. void AscStrTurnHexStr(char *AscStr, char *HexStr)
  815. {
  816. char *pAscStr=AscStr;
  817. int i=4;
  818. unsigned char d,h,l;
  819. while(i){
  820. d=(unsigned char)*pAscStr++;
  821. l=d&0x0f;
  822. h=d>>4;
  823. *HexStr++=AscToHex(h);
  824. *HexStr++=AscToHex(l);
  825. i--;
  826. }
  827. *HexStr=0;
  828. }
  829. void getData(void)
  830. {
  831. // char buf[10];
  832. // char buf1[8];
  833. // char buf2[4]={0};
  834. // char temp[3];
  835. // char temp1[3];
  836. // char temp2[5];
  837. // char temp3[5];
  838. // char data[6];
  839. // char data1[6];
  840. // char data2[6];
  841. // int i,num,num1,j,w,Gdata,k;
  842. // double Jnum1,Jnum2,Jnum3;//有小数位 8位有效
  843. // double Wnum1,Wnum2,Wnum3;//有小数位 8位有效
  844. // char b[]="0123456789ABCDEF";
  845. // MakeGpsSendData();
  846. //// AscStrToHexStr((char *)sutLcdGpsSendData.JJJJ,buf1);
  847. // AscStrTurnHexStr((char *)sutLcdGpsSendData.JJJJ,buf1);
  848. // memset(temp, 0, sizeof(temp));
  849. // for(i=0;i<3;i++){
  850. // temp[i]=buf1[i];
  851. // }
  852. // j=atoi(temp);
  853. // memset(temp2, 0, sizeof(temp2));
  854. // for(i=0;i<6;i++){
  855. // temp2[i]=buf1[i+3];
  856. // }
  857. // num=atoi(temp2);
  858. // Jnum1=num/1000;
  859. // Jnum1 /=60;
  860. // Jnum2 = (num%1000)*60;
  861. // Jnum2 /=3600000;
  862. // Jnum3=Jnum1+Jnum2;
  863. //
  864. // //sutLcdGpsSendData.j=j*1000000+Jnum3*1000000;
  865. // sutLcdGpsSendData.j = (j+Jnum3)*1000000;
  866. // sprintf(buf,",%d,",sutLcdGpsSendData.j);
  867. // SlwTrace(INF,buf);
  868. // i =0;
  869. // k=6;
  870. // memset(data, 0, sizeof(data));
  871. // while(sutLcdGpsSendData.j>0){
  872. // data[i]=sutLcdGpsSendData.j%16;
  873. // GPSDataBuf[k]=b[data[i]];//data1
  874. // sutLcdGpsSendData.j=sutLcdGpsSendData.j/16;
  875. // k--;
  876. // i++;
  877. // }
  878. // SlwTrace(INF,GPSDataBuf);
  879. //// AscStrToHexStr((char *)sutLcdGpsSendData.WWWW,buf2);
  880. // AscStrTurnHexStr((char *)sutLcdGpsSendData.WWWW,buf2);
  881. // for(i=0;i<3;i++){
  882. // temp1[i]=buf2[i];
  883. // }
  884. // w=atoi(temp1);
  885. // for(i=0;i<6;i++){//5
  886. // temp3[i]=buf2[i+3];
  887. // }
  888. // num1=atoi(temp3);
  889. // Wnum1=num1/1000;
  890. // Wnum1 /=60;
  891. // Wnum2 = (num1%1000)*60;
  892. // Wnum2 /=3600000;
  893. // Wnum3=Wnum1+Wnum2;
  894. //
  895. // //sutLcdGpsSendData.W=w*1000000+Wnum3*1000000;
  896. // sutLcdGpsSendData.W=(w+Wnum3)*1000000;
  897. // buf[0]=0;
  898. // sprintf(buf,"%d",sutLcdGpsSendData.W);
  899. // SlwTrace(INF,buf);
  900. // i =0;
  901. // k=6;
  902. // data1[0]=0;
  903. // while(sutLcdGpsSendData.W>0){
  904. // data1[i]=sutLcdGpsSendData.W%16;
  905. // GPSDataBuf1[k]=b[data1[i]];//data1
  906. // sutLcdGpsSendData.W=sutLcdGpsSendData.W/16;
  907. // k--;
  908. // i++;
  909. // }
  910. // SlwTrace(INF,GPSDataBuf1);
  911. // buf[0]=0;
  912. // sprintf(buf,"%d",sutLcdGpsSendData.j);
  913. // SlwTrace(INF,buf);
  914. // buf[0]=0;
  915. // sprintf(buf,"%d",sutLcdGpsSendData.W);
  916. // SlwTrace(INF,buf);
  917. }
  918. void TSRegData(void){
  919. // int i;
  920. // static int Temp1[7];
  921. // static int Table1[20]={0};
  922. // static int Temp[8];
  923. // unsigned char SendBuf[100],len;
  924. // unsigned short PacketLen,data1,data2,data3,data4;
  925. // unsigned int PSN;
  926. // PSN=sutProductPara.PSN;
  927. //
  928. // Regist.proID=0;
  929. // Regist.CityID=0;
  930. // for(i = 0; i < 5; i++) Regist.Manufacturers[i] = 0;
  931. // for(i = 0; i < 20; i++) Regist.TerMod[i]= 0x00;
  932. // Regist.TerID[0]=(unsigned char)((PSN>>24)&0xff);//没出来显示乱码
  933. // Regist.TerID[1]=(unsigned char)((PSN>>16)&0xff);
  934. // Regist.TerID[2]=(unsigned char)((PSN>>8)&0xff);
  935. // Regist.TerID[3]=(unsigned char)(PSN&0xff);
  936. // for(i = 0; i < 3; i++) Regist.TerID[i+4]= 0x00;
  937. //
  938. // Regist.PlateColor=0;
  939. // for(i = 0; i < 8; i++) Regist.Plate[i] = 0x00;
  940. // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_REGISTRATION,(unsigned char *)&Regist,sizeof(REGISTRATION));
  941. // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
  942. }
  943. void SIMReg(void)
  944. {
  945. // int i;
  946. // static int Table1[6]={0};
  947. // for(i = 0; i < 6; i++){
  948. // sscanf(sutProductPara.SIM+2*i,"%2x",&Table1[i]);
  949. // SimData.SIM[i]= Table1[i];
  950. // }
  951. }