tt808.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. #include "tt808.h"
  2. #include "usart.h"
  3. #include "key.h"
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "rtc.h"
  9. #include "database.h"
  10. //*整型
  11. int rlt;
  12. uint16_t uploadtime=0; //上传时间
  13. uint16_t hccd = 0;//ascii个数(*2)
  14. uint8_t header[12]; //处理头部
  15. uint8_t index_2c = 0;
  16. uint8_t MIPurc[100]; //接收平台数据
  17. uint8_t MIPurc1[100]; //接收平台数据
  18. uint8_t MIPurcXXTCSJ[100];//平台下行透传数据
  19. uint8_t speedBegin=0;
  20. uint8_t speedEnd=0;
  21. uint8_t speed[10]; //处理头部
  22. uint8_t DTUurc[100]; //接收平台数据
  23. uint8_t DTUurc1[15]; //接收平台数据
  24. int pushpullcount = 0;
  25. double dSpeed=0;
  26. double dlatitude=0;
  27. double dlongitude=0;
  28. char cSpeed[10];
  29. char clatitude[15];
  30. char clongitude[15];
  31. char csatellitesNum[5];
  32. char MGNSSLOC[100]; //接收定位数据
  33. char CSQ[12]; //接收定位数据
  34. char MGNSSLOC_time[6]; //接收定位数据
  35. uint32_t Stamp =0;
  36. uint8_t GNSSdata[40]={0};
  37. //*结构体
  38. TT808 tt808;
  39. extern TipsFlag tipsflag;
  40. extern Menu_table menu;
  41. extern bool refresh;
  42. extern LockInfo lockinfo;
  43. //*布尔
  44. bool authFlag = false;
  45. bool GnssFlag = false;
  46. bool Offline = false;
  47. bool Retrans = false;
  48. bool Online = false;
  49. bool reIME = false;
  50. bool reAuth = false;//回复权限
  51. /**
  52. 时间 纬度(北纬) 经度(东经) 水平精度因子 海拔高度 定位类型(1无,2 2d,3 3d) 运动角度 水平运动速度(KM/h) 水平运动速度(Knots) 日月年 卫星数量 差分定位标识(1单点,2差分)
  53. eg.+MGNSSLOC: 015032.301,3014.8605N,12001.9250E,1.8, 17.6, 3, 0.00, 3.6, 1.9, 080424, 12, 1
  54. */
  55. void reconn(void)
  56. {
  57. char TCPconnvalue[120];
  58. TcpConn(TCPconnvalue, 1, JLPTIP, JLPTPORT,0); //cid=1 tcpConnmode=0 普通模式
  59. if(sendCmd_4G(TCPconnvalue, "+MIPOPEN: 1,0", 1, 1)) //TCP连接 0普通模式 2缓存模式 1透传模式
  60. {
  61. printf("重连成功\r\n");
  62. }
  63. }
  64. uint8_t Messagetemp_length=0;
  65. uint8_t MessageHandleData[100]; //放入封装后数据
  66. uint8_t tcppacket[100]; //消息数据
  67. void MessageHandle(uint16_t MessageID)
  68. {
  69. Flash_ReadBytes((uint16_t*)&(tt808.LSH),tt808serialNum,1);//读取loc头部是否发生写入
  70. if(tt808.LSH==0xFFFF)
  71. {
  72. tt808.LSH=0;//初始未写入
  73. }
  74. uint16_t MessageAttributeData = Messagetemp_length;
  75. MessageAttributeData |= ( Messagetemp_length | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
  76. header[0] = ((MessageID>>8)&0xff);
  77. header[1] = (MessageID&0xff); //消息ID
  78. header[2] = ((MessageAttributeData>>8)&0xff);
  79. header[3] = (MessageAttributeData&0xff); //消息属性
  80. //*test phoneNumxy
  81. //*work phoneNum
  82. if(phonenull)
  83. {
  84. header[4] = 0x00;
  85. header[5] = 0x00;
  86. header[6] = 0x16;
  87. header[7] = 0x00;
  88. header[8] = 0x00;
  89. header[9] = 0x08; //“手机号”
  90. phonenum[0]=0x0000;
  91. phonenum[1]=0x1600;
  92. phonenum[2]=0x0008;
  93. }
  94. else
  95. {
  96. header[4] = (phonenum[0]>>8)&0xff;
  97. header[5] = (phonenum[0])&0xff;
  98. header[6] = (phonenum[1]>>8)&0xff;
  99. header[7] = (phonenum[1])&0xff;
  100. header[8] = (phonenum[2]>>8)&0xff;
  101. header[9] = (phonenum[2])&0xff; //“手机号”
  102. }
  103. header[10] = ((tt808.LSH>>8)&0xff);
  104. header[11] = (tt808.LSH&0xff);
  105. int index = 0;
  106. tcppacket[index++] = TT808FLAG; // Set the headflag 0x7E
  107. for (int i = 0; i < 12; i++) {
  108. tcppacket[index++] = header[i];
  109. }
  110. // Copy message body to packet
  111. for (size_t i = 0; i < Messagetemp_length; i++) {
  112. tcppacket[index++] = MessageHandleData[i];
  113. }
  114. tcppacket[index++] = xorBytes(&tcppacket[1],12+Messagetemp_length);; // Set the checksum
  115. tcppacket[index++] = TT808FLAG; // Set the flag at the end
  116. // for (int i = 0; i < index; i++) {
  117. // printf("%02X ", tcppacket[i]); // 以十六进制格式打印每个元素
  118. // }
  119. printf("\r\n");
  120. //#if open4G ==1
  121. char TCPvalue[120];
  122. char *ptr = TCPvalue;
  123. ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",index);
  124. for (int i = 0; i < index; i++) {
  125. ptr += sprintf(ptr, "%02X", tcppacket[i]);
  126. }
  127. sprintf(ptr, "\"\r\n");//TCPvalue
  128. if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送
  129. {
  130. printf("#等待回应\r\n");
  131. }
  132. //#endif
  133. tt808.LSH+=1;//需做写入处理
  134. Flash_WriteBytes((uint16_t*)&(tt808.LSH),tt808serialNum,1);
  135. }
  136. uint16_t swapBytes16(uint16_t value) {
  137. return (value >> 8) | (value << 8);
  138. }
  139. uint32_t swapBytes32(uint32_t value) {
  140. return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) |
  141. ((value >> 8) & 0xFF00) | ((value >> 24) & 0xFF);
  142. }
  143. /**
  144. * @breaf 消息体封装
  145. * Message[] 消息数据; length 消息数据长度; outMessage[] 封装后消息数据; MessageID 消息ID
  146. */
  147. extern BtFrame btframe;
  148. uint8_t Message_bodydata(uint8_t *Message, uint8_t length, uint8_t outMessage[],uint16_t MessageID)
  149. {
  150. int index = 0;
  151. uint8_t temp_length = 0;//加首尾 25/23/crc
  152. if(MessageID==MessageID_authentication || MessageID==MessageID_heartbeat || MessageID==MessageID_Getauth)//不加头尾(鉴权、心跳、获取权限)
  153. {
  154. temp_length = length;//不加首尾
  155. uint8_t temp_Message[temp_length];
  156. for (int i = 0; i < length; i++) {
  157. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  158. }
  159. temp_length = index;
  160. for(int i =0;i<temp_length;i++)
  161. {
  162. outMessage[i]=temp_Message[i];
  163. }
  164. }
  165. else if(MessageID==MessageID_location)//终端地址0200 补传0704
  166. {
  167. temp_length = length;//不加首尾
  168. //状态标志/纬度/经度/速度
  169. uint8_t temp_Message[temp_length];
  170. for (int i = 0; i < length; i++) {
  171. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  172. }
  173. for(int i =0;i<temp_length;i++)
  174. {
  175. outMessage[i]=temp_Message[i];
  176. }
  177. }
  178. else if(MessageID==MessageID_Retrans)// 补传0704+5
  179. {
  180. temp_length = length;//不加首尾
  181. uint8_t temp_Message[temp_length];
  182. temp_Message[index++] = 0x00;
  183. temp_Message[index++] = 0x01;
  184. temp_Message[index++] = 0x00;
  185. temp_Message[index++] = 0x00;
  186. temp_Message[index++] = 0x22;
  187. for (int i = 0; i < length; i++) {
  188. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  189. }
  190. for(int i =0;i<temp_length;i++)
  191. {
  192. outMessage[i]=temp_Message[i];
  193. }
  194. }
  195. else if(MessageID==MessageID_uploadLock)//上传锁信息
  196. {
  197. temp_length = length+9;//加4字节操作时间+4字节userIDbtframe.userId+1byte操作类型
  198. uint8_t temp_Message[temp_length];
  199. for (int i = 0; i < length; i++) {
  200. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  201. }
  202. uint32_t ntime = dblocptr->location.timeStamp;//现在时间
  203. temp_Message[index++]=(ntime>>24) & 0xff;
  204. temp_Message[index++]=(ntime>>16) & 0xff;
  205. temp_Message[index++]=(ntime>>8) & 0xff;
  206. temp_Message[index++]= ntime & 0xff;
  207. temp_Message[index++]=(btframe.userId>>24) & 0xff;
  208. temp_Message[index++]=(btframe.userId>>16) & 0xff;
  209. temp_Message[index++]=(btframe.userId>>8) & 0xff;
  210. temp_Message[index++]=(btframe.userId) & 0xff;
  211. temp_Message[index++] = macsetptr->sta;
  212. for(int i =0;i<temp_length;i++)
  213. {
  214. outMessage[i]=temp_Message[i];
  215. }
  216. }
  217. else//加首尾
  218. {
  219. temp_length = length+3;//加首尾 25/23/crc
  220. uint8_t temp_Message[temp_length];
  221. temp_Message[index++]=temp_length;
  222. temp_Message[index++]=0x2A;
  223. for (int i = 0; i < length; i++) {
  224. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  225. }
  226. temp_Message[index++]=0x23;
  227. temp_Message[index++]=xorBytes(&temp_Message[1],temp_length-1);
  228. temp_length = index;
  229. for(int i =0;i<temp_length;i++)
  230. {
  231. outMessage[i]=temp_Message[i];
  232. }
  233. }
  234. return temp_length;
  235. }
  236. /*需要传入消息体长度sizeof(Message)/sizeof(Message[0]);*/
  237. void tt808FsFunc(uint8_t* Message, uint8_t length, uint16_t MessageID)
  238. {
  239. HAL_Delay(200);
  240. Messagetemp_length = Message_bodydata(Message, length, MessageHandleData, MessageID);
  241. MessageHandle(MessageID);
  242. }
  243. void printBytes(uint8_t *data, size_t length) {
  244. for (size_t i = 0; i < length; i++) {
  245. printf("%02x ", data[i]);
  246. }
  247. printf("\n");
  248. }
  249. // 函数用于提取第7到第8个逗号之间的字符串
  250. void extractString(char* receiveStr, char* result, uint8_t start, uint16_t end)
  251. {
  252. int comma_count = 0;
  253. int start_index = -1;
  254. int end_index = -1;
  255. int length = strlen(receiveStr);
  256. // 遍历字符串,统计逗号数量和记录起始和结束索引
  257. for (int i = 0; i < length; i++)
  258. {
  259. if (receiveStr[i] == ',')
  260. {
  261. comma_count++;
  262. if (comma_count == start)
  263. {
  264. start_index = i + 1; // 第start个逗号之后的字符
  265. }
  266. else if (comma_count == end)
  267. {
  268. if(start==1 && end==2)//*纬度去尾符号
  269. {
  270. end_index = i-1; // 第end个逗号之前的字符
  271. }
  272. else if(start==2 && end==3)//*经度去尾符号
  273. {
  274. end_index = i-1; // 第end个逗号之前的字符
  275. }
  276. else
  277. {
  278. end_index = i; // 第end个逗号之前的字符
  279. }
  280. break;
  281. }
  282. }
  283. }
  284. // 提取字符串并存储在result数组中
  285. if (start_index != -1 && end_index != -1 && start_index < end_index)
  286. {
  287. strncpy(result, receiveStr + start_index, end_index - start_index);
  288. result[end_index - start_index] = '\0'; // 添加字符串结束符
  289. }
  290. }
  291. void extractStringPoint(char* receiveStr, char* result, uint8_t start, uint16_t end)
  292. {
  293. int comma_count = 0;
  294. int start_index = -1;
  295. int end_index = -1;
  296. int length = strlen(receiveStr);
  297. // 遍历字符串,统计逗号数量和记录起始和结束索引
  298. for (int i = 0; i < length; i++)
  299. {
  300. if(start==0 && end==1)// = -> .
  301. {
  302. start_index = start; // 第start个逗号之后的字符
  303. if (receiveStr[i] == '.')
  304. {
  305. comma_count++;
  306. }
  307. else if (comma_count == end)
  308. {
  309. end_index = i-1; // 第end个逗号之前的字符
  310. break;
  311. }
  312. }
  313. else if(start==3 && end==4)
  314. {
  315. if (receiveStr[i] == '.')
  316. {
  317. comma_count++;
  318. if (comma_count == start)
  319. {
  320. start_index = i + 1; // 第start个逗号之后的字符
  321. }
  322. }
  323. else if(receiveStr[i] == ':')
  324. {
  325. end_index = i;
  326. }
  327. }
  328. else if(start==4 && end==5)
  329. {
  330. if (receiveStr[i] == ':')
  331. {
  332. comma_count=4;
  333. if (comma_count == start)
  334. {
  335. start_index = i+1; // 第start个逗号之后的字符
  336. }
  337. end_index = start_index+4;
  338. break;
  339. }
  340. }
  341. else
  342. {
  343. if (receiveStr[i] == '.')
  344. {
  345. comma_count++;
  346. if (comma_count == start)
  347. {
  348. start_index = i + 1; // 第start个逗号之后的字符
  349. }
  350. else if (comma_count == end)
  351. {
  352. end_index = i; // 第end个逗号之前的字符
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. // 提取字符串并存储在result数组中
  359. if (start_index != -1 && end_index != -1 && start_index < end_index)
  360. {
  361. strncpy(result, receiveStr + start_index, end_index - start_index);
  362. result[end_index - start_index] = '\0'; // 添加字符串结束符
  363. }
  364. }
  365. double convertToDouble(char* str) {
  366. if(str == csatellitesNum)
  367. {
  368. uint8_t result;
  369. sscanf(str, "%hhu", &result);
  370. return result;
  371. }
  372. else
  373. {
  374. double result;
  375. sscanf(str, "%lf", &result);
  376. return result;
  377. }
  378. }
  379. char hex2asc(const char *hex)
  380. {
  381. char asc = 0;
  382. char a = (hex[0]>='A' && hex[0]<='F')?(hex[0]^32):hex[0];
  383. char b = (hex[1]>='A' && hex[1]<='F')?(hex[1]^32):hex[1];
  384. if ((a>='0' && a<='9' || a>='a' && a<='f') && (b>='0' && b<='9' || b>='a' && b<='f')){
  385. asc = ((('a'<=a && a<='f')?(a-'a')+10:a-'0')<<4)|(('a'<=b && b<='f')?(b-'a')+10:b-'0');
  386. }
  387. return asc;
  388. }
  389. uint8_t testHex2Asc(const char *hex) {
  390. char asc = hex2asc(hex);
  391. printf("Hex: %s\tASC: %d\n", hex, asc);
  392. return asc;
  393. }
  394. char *bin2hex(char *hex, const unsigned char *bin, int size)
  395. {
  396. size_t i;
  397. for(i=0; i<size; i++)
  398. {
  399. sprintf(hex+i*2, "%02x", bin[i]);
  400. }
  401. hex[i+i] = 0;
  402. return hex;
  403. }
  404. int hex2bin(unsigned char *bin, const char *hex)
  405. {
  406. const char *h = hex;
  407. unsigned char *b = bin;
  408. int rlt = 0;
  409. while(*h){
  410. *b++ = hex2asc(h);
  411. h = h + 2;
  412. rlt++;
  413. }
  414. return rlt;//长度
  415. }
  416. // 将单个十六进制数转换为 BCD 码
  417. uint8_t hexToBcd(uint8_t hex) {
  418. return ((hex / 10) << 4) | (hex % 10);
  419. }
  420. // 将整数数组中的所有十六进制数转换为 BCD 码
  421. void hexArrayToBcd(uint8_t* array, size_t length, uint8_t* out) {//时间用
  422. array[3]+=8;
  423. for (size_t i = 0; i < length; i++) {
  424. out[i] = hexToBcd(array[i]);
  425. }
  426. }
  427. void hexToBcd_arr(uint8_t* array, size_t length, uint8_t* out) {//通用
  428. for (size_t i = 0; i < length; i++) {
  429. out[i] = hexToBcd(array[i]);
  430. }
  431. }
  432. char input_strings[20]={0};
  433. char reIp0[10];
  434. char reIp1[10];
  435. char reIp2[10];
  436. char reIp3[10];
  437. char reIpP[10];
  438. Ipaddr ipaddr;
  439. MacSet macset;
  440. MacSet *macsetptr = &macset;
  441. char Newip[30];
  442. extern bool wdiFlag;
  443. char MqttReBuff[200];
  444. char MqttWorkBuff[200];
  445. char Mqtttotal_len[10];
  446. char payload_len[10];
  447. uint8_t Mqtttotal_lenu8=0;
  448. uint8_t payload_lenu8=0;
  449. void Process_str_collections(void) {
  450. // 循环遍历从0到macCount-1的每个索引
  451. for (int i = 0; i < macset.macCount; i++) {
  452. AtPRocess(
  453. lockinfo.SNname[i],
  454. macset.SNCONN[i],
  455. macset.SNED[i],
  456. macset.SNDIS[i]
  457. );
  458. }
  459. }
  460. char versionc[20]={0};
  461. void MqttRe(void)
  462. {
  463. if(strncmp((const char *)U4_4GrecvBuff, "+MQTTURC: \"publish\"", strlen("+MQTTURC: \"publish\""))==0)//接收到发布信息
  464. {
  465. memcpy(MqttReBuff,&U4_4GrecvBuff[2],U4_4GrecvLength-2);//全部copy
  466. //printf("@接收到Mqtt消息%s\r\n",MqttReBuff);
  467. extractString(MqttReBuff, Mqtttotal_len,4 ,5);//采集<total_len>
  468. extractString(MqttReBuff, payload_len,5 ,6);//采集<payload_len>
  469. Mqtttotal_lenu8 = atoi(Mqtttotal_len);
  470. payload_lenu8 = atoi(payload_len);
  471. //printf("%d,%d\r\n",Mqtttotal_lenu8,payload_lenu8);
  472. if(Mqtttotal_lenu8==payload_lenu8)
  473. {
  474. memcpy(MqttWorkBuff, &MqttReBuff[U4_4GrecvLength - payload_lenu8-4], payload_lenu8);
  475. printf("%s\r\n",MqttWorkBuff);
  476. }
  477. //*处理摘取字段
  478. if(strncmp((const char*)&MqttWorkBuff, "DTU+FLASH=deleteloc", strlen("DTU+FLASH=deleteloc"))==0)
  479. {
  480. dblocptr->headinfo.loc_pushIndex=0;
  481. dblocptr->headinfo.loc_pullIndex=0;
  482. Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度
  483. printf("更新刻度成功\r\n");
  484. }
  485. else if(strncmp((char*)MqttWorkBuff, "DTU+JT808_IMEI=", strlen("DTU+JT808_IMEI="))==0)//修改IME号
  486. {
  487. memcpy(DTUurc1,MqttWorkBuff+15,12);
  488. const char *hex = (const char *)DTUurc1; // 将 uint8_t * 转换为 const char *
  489. int size = strlen(hex) / 2; // 计算二进制数据的长度
  490. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  491. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  492. memcpy(DTUurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  493. free(bin); // 释放内存
  494. reIME = true;
  495. HAL_Delay(500);
  496. }
  497. else if(strncmp((char*)MqttWorkBuff, "DTU+JT808_SERVER=", strlen("DTU+JT808_SERVER="))==0)
  498. {
  499. memcpy(input_strings,MqttWorkBuff+17,20);
  500. extractStringPoint(input_strings,reIp0,0,1);
  501. extractStringPoint(input_strings,reIp1,1,2);
  502. extractStringPoint(input_strings,reIp2,2,3);
  503. extractStringPoint(input_strings,reIp3,3,4);
  504. extractStringPoint(input_strings,reIpP,4,5);
  505. ipaddr.IP0 = atoi(reIp0);
  506. ipaddr.IP1 = atoi(reIp1);
  507. ipaddr.IP2 = atoi(reIp2);
  508. ipaddr.IP3 = atoi(reIp3);
  509. ipaddr.IPp = atoi(reIpP);
  510. //printf("%d.%d.%d.%d:%d",ipaddr.IP0,ipaddr.IP1,ipaddr.IP2,ipaddr.IP3,ipaddr.IPp);
  511. HAL_StatusTypeDef writeSta = Flash_WriteBytes((uint16_t*)&(ipaddr.IP0),ReIPAddr,5);
  512. if(writeSta == HAL_OK)
  513. {
  514. char *ptr = Newip;
  515. ptr += sprintf(ptr, "%d.", ipaddr.IP0);
  516. ptr += sprintf(ptr, "%d.", ipaddr.IP1);
  517. ptr += sprintf(ptr, "%d.", ipaddr.IP2);
  518. ptr += sprintf(ptr, "%d", ipaddr.IP3);
  519. printf("更换IP成功\r\n");
  520. }
  521. //ptr += sprintf(ptr, ":%d", ipaddr.IPp);
  522. //printf("%s\r\n",Newip);
  523. HAL_Delay(500);
  524. printf("即将重启\r\n");
  525. wdiFlag = true;
  526. }
  527. else if(strncmp((char*)MqttWorkBuff, "DTU+VER=?", strlen("DTU+VER=?"))==0)
  528. {
  529. printf("%s\r\n",version);
  530. }
  531. else if(strncmp((char*)MqttWorkBuff, "DTU+MANUALMODE=1", strlen("DTU+MANUALMODE=1"))==0)
  532. {
  533. ManualWork = 1;
  534. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  535. printf("手动模式开启\r\n");
  536. }
  537. else if(strncmp((char*)MqttWorkBuff, "DTU+MANUALMODE=0", strlen("DTU+MANUALMODE=0"))==0)
  538. {
  539. ManualWork = 0;
  540. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  541. printf("手动模式关闭\r\n");
  542. }
  543. HAL_StatusTypeDef WriteSta;
  544. //*PDA需求 485写入MAC号
  545. if(strncmp((char*)MqttWorkBuff, "DTU+ADDMAC", strlen("DTU+ADDMAC"))==0)
  546. {
  547. macset.macCount=0;
  548. workedLock=0;
  549. memset(devices,0,8);
  550. erase_flash(MacDbBodyAddr);
  551. if(payload_lenu8>12){
  552. memcpy(macset.MACtemp[0],MqttWorkBuff+11,12);
  553. processPdadata(macset.MACtemp[0],macset.mactemp[0],macset.mac[0]);
  554. WriteSta= Flash_WriteBytes(macsetptr->mac[0],MacDbBodyAddr,3);
  555. if(WriteSta== HAL_OK) macset.macCount += 1;}
  556. if(payload_lenu8>25){
  557. memcpy(macset.MACtemp[1],MqttWorkBuff+24,12);
  558. processPdadata(macset.MACtemp[1],macset.mactemp[1],macset.mac[1]);
  559. WriteSta= Flash_WriteBytes(macsetptr->mac[1],MacDbBodyAddr+(macset.macCount*6),3);
  560. if(WriteSta== HAL_OK) macset.macCount += 1;}
  561. if(payload_lenu8>38){
  562. memcpy(macset.MACtemp[2],MqttWorkBuff+37,12);
  563. processPdadata(macset.MACtemp[2],macset.mactemp[2],macset.mac[2]);
  564. WriteSta= Flash_WriteBytes(macsetptr->mac[2],MacDbBodyAddr+(macset.macCount*6),3);
  565. if(WriteSta== HAL_OK) macset.macCount += 1;}
  566. if(payload_lenu8>51){
  567. memcpy(macset.MACtemp[3],MqttWorkBuff+50,12);
  568. processPdadata(macset.MACtemp[3],macset.mactemp[3],macset.mac[3]);
  569. WriteSta= Flash_WriteBytes(macsetptr->mac[3],MacDbBodyAddr+(macset.macCount*6),3);
  570. if(WriteSta== HAL_OK) macset.macCount += 1;}
  571. if(payload_lenu8>64){
  572. memcpy(macset.MACtemp[4],MqttWorkBuff+63,12);
  573. processPdadata(macset.MACtemp[4],macset.mactemp[4],macset.mac[4]);
  574. WriteSta= Flash_WriteBytes(macsetptr->mac[4],MacDbBodyAddr+(macset.macCount*6),3);
  575. if(WriteSta== HAL_OK) macset.macCount += 1;}
  576. if(payload_lenu8>77){
  577. memcpy(macset.MACtemp[5],MqttWorkBuff+76,12);
  578. processPdadata(macset.MACtemp[5],macset.mactemp[5],macset.mac[5]);
  579. WriteSta= Flash_WriteBytes(macsetptr->mac[5],MacDbBodyAddr+(macset.macCount*6),3);
  580. if(WriteSta== HAL_OK) macset.macCount += 1;}
  581. if(payload_lenu8>90){
  582. memcpy(macset.MACtemp[6],MqttWorkBuff+89,12);
  583. processPdadata(macset.MACtemp[6],macset.mactemp[6],macset.mac[6]);
  584. WriteSta= Flash_WriteBytes(macsetptr->mac[6],MacDbBodyAddr+(macset.macCount*6),3);
  585. if(WriteSta== HAL_OK) macset.macCount += 1;}
  586. if(payload_lenu8>103){
  587. memcpy(macset.MACtemp[7],MqttWorkBuff+102,12);
  588. processPdadata(macset.MACtemp[7],macset.mactemp[7],macset.mac[7]);
  589. WriteSta= Flash_WriteBytes(macsetptr->mac[7],MacDbBodyAddr+(macset.macCount*6),3);
  590. if(WriteSta== HAL_OK) macset.macCount += 1;}
  591. for(int i = 0; i < macset.macCount; i++) {
  592. memset(lockinfo.SNname[i], 0, 13);
  593. }
  594. if(WriteSta == HAL_OK)
  595. {
  596. printf("更新MAC存储完成共->%d\r\n",macset.macCount);
  597. }
  598. WriteSta = Flash_WriteBytes(&macsetptr->macCount,MacDbHeadAddr,1);
  599. if(WriteSta == HAL_OK)
  600. {
  601. printf("更新存储刻度完成\r\n");
  602. }
  603. ReadSNname();//更新
  604. Process_str_collections();//处理连接字符串
  605. enterInterface_time = HAL_GetTick();
  606. menu.current=6;//提示mac
  607. refresh=true;
  608. }
  609. else if(strncmp((char*)MqttWorkBuff, "DTU+WORK=ALLOPEN", strlen("DTU+WORK=ALLOPEN"))==0)
  610. {
  611. enterInterface_time = HAL_GetTick();
  612. SET_MENU_STATUS(2,2,0,0);
  613. Allopen=true;
  614. }
  615. else if(strncmp((char*)MqttWorkBuff, "DTU+WORK=ALLCLOSE", strlen("DTU+WORK=ALLCLOSE"))==0)
  616. {
  617. enterInterface_time = HAL_GetTick();
  618. SET_MENU_STATUS(2,1,1,1);
  619. Allopen=true;
  620. }
  621. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  622. memset(MqttReBuff,0,U4_4GrecvLength);
  623. memset(MqttWorkBuff,0,payload_lenu8);
  624. }
  625. }
  626. //*485测试命令
  627. void Test485Re(void)
  628. {
  629. //*test485 模拟
  630. if(strncmp((char*)U1_485recvBuff, "+MIPURC: \"disconn\",1,1", strlen("+MIPURC: \"disconn\",1,1"))==0)//"disconn" 平台断连
  631. {
  632. Offline=true;
  633. uploaweork=false;
  634. printf("@U1与平台断开连接\r\n");
  635. memset(U1_485recvBuff,0,BUFFER_SIZE);
  636. }
  637. else if(strncmp((char*)U1_485recvBuff, "+MIPOPEN: 1,0", strlen("+MIPOPEN: 1,0"))==0)//连接平台
  638. {
  639. Offline=false;
  640. Online = true;
  641. uploaweork=true;
  642. printf("@U1设备已上线!!\r\n");
  643. refresh=true;
  644. memset(U1_485recvBuff,0,BUFFER_SIZE);
  645. }
  646. else if(strncmp((char*)&U1_485recvBuff, "DTU+FLASH=deleteloc", strlen("DTU+FLASH=deleteloc"))==0)//连接平台
  647. {
  648. dblocptr->headinfo.loc_pushIndex=0;
  649. dblocptr->headinfo.loc_pullIndex=0;
  650. Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度
  651. memset(U1_485recvBuff,0,BUFFER_SIZE);
  652. printf("更新刻度成功\r\n");
  653. }
  654. else if(strncmp((char*)U1_485recvBuff, "DTU+JT808_IMEI=", strlen("DTU+JT808_IMEI="))==0)//修改IME号
  655. {
  656. memcpy(DTUurc1,U1_485recvBuff+15,12);
  657. const char *hex = (const char *)DTUurc1; // 将 uint8_t * 转换为 const char *
  658. int size = strlen(hex) / 2; // 计算二进制数据的长度
  659. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  660. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  661. memcpy(DTUurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  662. free(bin); // 释放内存
  663. //memset(DTUurc1,0,100);
  664. memset(U1_485recvBuff,0,BUFFER_SIZE);
  665. reIME = true;
  666. HAL_Delay(500);
  667. }
  668. else if(strncmp((char*)U1_485recvBuff, "DTU+JT808_SERVER=", strlen("DTU+JT808_SERVER="))==0)
  669. {
  670. memcpy(input_strings,U1_485recvBuff+17,20);
  671. extractStringPoint(input_strings,reIp0,0,1);
  672. extractStringPoint(input_strings,reIp1,1,2);
  673. extractStringPoint(input_strings,reIp2,2,3);
  674. extractStringPoint(input_strings,reIp3,3,4);
  675. extractStringPoint(input_strings,reIpP,4,5);
  676. ipaddr.IP0 = atoi(reIp0);
  677. ipaddr.IP1 = atoi(reIp1);
  678. ipaddr.IP2 = atoi(reIp2);
  679. ipaddr.IP3 = atoi(reIp3);
  680. ipaddr.IPp = atoi(reIpP);
  681. //printf("%d.%d.%d.%d:%d",ipaddr.IP0,ipaddr.IP1,ipaddr.IP2,ipaddr.IP3,ipaddr.IPp);
  682. HAL_StatusTypeDef writeSta = Flash_WriteBytes((uint16_t*)&(ipaddr.IP0),ReIPAddr,5);
  683. if(writeSta == HAL_OK)
  684. {
  685. char *ptr = Newip;
  686. ptr += sprintf(ptr, "%d.", ipaddr.IP0);
  687. ptr += sprintf(ptr, "%d.", ipaddr.IP1);
  688. ptr += sprintf(ptr, "%d.", ipaddr.IP2);
  689. ptr += sprintf(ptr, "%d", ipaddr.IP3);
  690. printf("更换IP成功\r\n");
  691. }
  692. //ptr += sprintf(ptr, ":%d", ipaddr.IPp);
  693. //printf("%s\r\n",Newip);
  694. memset(U1_485recvBuff,0,BUFFER_SIZE);
  695. HAL_Delay(500);
  696. printf("即将重启\r\n");
  697. wdiFlag = true;
  698. }
  699. else if(strncmp((char*)U1_485recvBuff, "DTU+VER=?", strlen("DTU+VER=?"))==0)
  700. {
  701. printf("%s\r\n",version);
  702. memset(U1_485recvBuff,0,BUFFER_SIZE);
  703. }
  704. else if(strncmp((char*)U1_485recvBuff, "DTU+WORK=ALLOPEN", strlen("DTU+WORK=ALLOPEN"))==0)
  705. {
  706. enterInterface_time = HAL_GetTick();
  707. SET_MENU_STATUS(2,2,0,0);
  708. Allopen=true;
  709. memset(U1_485recvBuff,0,BUFFER_SIZE);
  710. }
  711. else if(strncmp((char*)U1_485recvBuff, "DTU+WORK=ALLCLOSE", strlen("DTU+WORK=ALLCLOSE"))==0)
  712. {
  713. enterInterface_time = HAL_GetTick();
  714. SET_MENU_STATUS(2,1,1,1);
  715. Allopen=true;
  716. memset(U1_485recvBuff,0,BUFFER_SIZE);
  717. }
  718. else if(strncmp((char*)U1_485recvBuff, "DTU+MANUALMODE=1", strlen("DTU+MANUALMODE=1"))==0)
  719. {
  720. ManualWork = 1;
  721. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  722. printf("手动模式开启\r\n");
  723. memset(U1_485recvBuff,0,BUFFER_SIZE);
  724. }
  725. else if(strncmp((char*)U1_485recvBuff, "DTU+MANUALMODE=0", strlen("DTU+MANUALMODE=0"))==0)
  726. {
  727. ManualWork = 0;
  728. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  729. printf("手动模式关闭\r\n");
  730. memset(U1_485recvBuff,0,BUFFER_SIZE);
  731. }
  732. REPdaMode();//PDA 485 MQTT
  733. }
  734. void AtPRocess(char *value, char conn[],char connected[],char disconn[]) {
  735. // 使用snprintf代替sprintf以确保不会发生缓冲区溢出
  736. snprintf(conn, strlen(value) + 12, "AT+CONN=%s\r\n", value);
  737. snprintf(connected, strlen(value) + 14, "+CONNECTED:0,%s", value);
  738. snprintf(disconn, strlen(value) + 12, "+DISCONN:0,%s", value);
  739. // printf("%s\r\n", conn);
  740. // printf("%s\r\n", connected);
  741. // printf("%s\r\n", disconn);
  742. }
  743. void processPdadata(char MACtemp[], uint8_t mactemp[],uint16_t mac[])
  744. {
  745. const char *hex = (const char *)MACtemp; // 将 uint8_t * 转换为 const char *
  746. int size = strlen(hex) / 2; // 计算二进制数据的长度
  747. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  748. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  749. memcpy(mactemp, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  750. free(bin); // 释放内存
  751. mac[0] = mactemp[0]<<8|mactemp[1];
  752. mac[1] = mactemp[2]<<8|mactemp[3];
  753. mac[2] = mactemp[4]<<8|mactemp[5];
  754. }
  755. #define MAC_LENGTH 12
  756. #define MAC_DB_ENTRY_SIZE 3
  757. void REPdaMode(void)
  758. {
  759. HAL_StatusTypeDef WriteSta;
  760. //*PDA需求 485写入MAC号
  761. if(strncmp((char*)U1_485recvBuff, "DTU+ADDMAC", strlen("DTU+ADDMAC"))==0)
  762. {
  763. macset.macCount=0;
  764. workedLock=0;
  765. memset(devices,0,8);
  766. erase_flash(MacDbBodyAddr);
  767. if(U1_485recvLength>12){
  768. memcpy(macset.MACtemp[0],U1_485recvBuff+11,12);
  769. processPdadata(macset.MACtemp[0],macset.mactemp[0],macset.mac[0]);
  770. WriteSta= Flash_WriteBytes(macsetptr->mac[0],MacDbBodyAddr,3);
  771. if(WriteSta== HAL_OK) macset.macCount += 1;}
  772. if(U1_485recvLength>25){
  773. memcpy(macset.MACtemp[1],U1_485recvBuff+24,12);
  774. processPdadata(macset.MACtemp[1],macset.mactemp[1],macset.mac[1]);
  775. WriteSta= Flash_WriteBytes(macsetptr->mac[1],MacDbBodyAddr+(macset.macCount*6),3);
  776. if(WriteSta== HAL_OK) macset.macCount += 1;}
  777. if(U1_485recvLength>38){
  778. memcpy(macset.MACtemp[2],U1_485recvBuff+37,12);
  779. processPdadata(macset.MACtemp[2],macset.mactemp[2],macset.mac[2]);
  780. WriteSta= Flash_WriteBytes(macsetptr->mac[2],MacDbBodyAddr+(macset.macCount*6),3);
  781. if(WriteSta== HAL_OK) macset.macCount += 1;}
  782. if(U1_485recvLength>51){
  783. memcpy(macset.MACtemp[3],U1_485recvBuff+50,12);
  784. processPdadata(macset.MACtemp[3],macset.mactemp[3],macset.mac[3]);
  785. WriteSta= Flash_WriteBytes(macsetptr->mac[3],MacDbBodyAddr+(macset.macCount*6),3);
  786. if(WriteSta== HAL_OK) macset.macCount += 1;}
  787. if(U1_485recvLength>64){
  788. memcpy(macset.MACtemp[4],U1_485recvBuff+63,12);
  789. processPdadata(macset.MACtemp[4],macset.mactemp[4],macset.mac[4]);
  790. WriteSta= Flash_WriteBytes(macsetptr->mac[4],MacDbBodyAddr+(macset.macCount*6),3);
  791. if(WriteSta== HAL_OK) macset.macCount += 1;}
  792. if(U1_485recvLength>77){
  793. memcpy(macset.MACtemp[5],U1_485recvBuff+76,12);
  794. processPdadata(macset.MACtemp[5],macset.mactemp[5],macset.mac[5]);
  795. WriteSta= Flash_WriteBytes(macsetptr->mac[5],MacDbBodyAddr+(macset.macCount*6),3);
  796. if(WriteSta== HAL_OK) macset.macCount += 1;}
  797. if(U1_485recvLength>90){
  798. memcpy(macset.MACtemp[6],U1_485recvBuff+89,12);
  799. processPdadata(macset.MACtemp[6],macset.mactemp[6],macset.mac[6]);
  800. WriteSta= Flash_WriteBytes(macsetptr->mac[6],MacDbBodyAddr+(macset.macCount*6),3);
  801. if(WriteSta== HAL_OK) macset.macCount += 1;}
  802. if(U1_485recvLength>103){
  803. memcpy(macset.MACtemp[7],U1_485recvBuff+102,12);
  804. processPdadata(macset.MACtemp[7],macset.mactemp[7],macset.mac[7]);
  805. WriteSta= Flash_WriteBytes(macsetptr->mac[7],MacDbBodyAddr+(macset.macCount*6),3);
  806. if(WriteSta== HAL_OK) macset.macCount += 1;}
  807. for(int i = 0; i < macset.macCount; i++) {
  808. memset(lockinfo.SNname[i], 0, 13);
  809. }
  810. if(WriteSta == HAL_OK)
  811. {
  812. printf("更新MAC存储完成共->%d\r\n",macset.macCount);
  813. }
  814. WriteSta = Flash_WriteBytes(&macsetptr->macCount,MacDbHeadAddr,1);
  815. if(WriteSta == HAL_OK)
  816. {
  817. printf("更新存储刻度完成\r\n");
  818. }
  819. ReadSNname();//更新
  820. Process_str_collections();//处理连接字符串
  821. enterInterface_time = HAL_GetTick();
  822. menu.current=6;//提示mac
  823. refresh=true;
  824. memset(U1_485recvBuff,0,BUFFER_SIZE);
  825. }
  826. }
  827. bool snConnected[NUM_OF_DEVICES] = {false};
  828. void ReBTcall(void)
  829. {
  830. for (int i = 0; i < NUM_OF_DEVICES; ++i) {
  831. if (strncmp((char*)autoRe, macsetptr->SNED[i], strlen(macsetptr->SNED[i])) == 0) {
  832. snConnected[i] = true;
  833. memset(autoRe, 0, BUFFER_SIZE);
  834. break; // Exit the loop once a match is found
  835. }
  836. }
  837. }
  838. //*工作命令
  839. void ReMIPURC(void)//DTU
  840. {
  841. //*设备上线,发送成功
  842. if(strncmp((char*)U4_4GrecvBuff, "+MIPOPEN: 1,0", strlen("+MIPOPEN: 1,0"))==0)//连接平台
  843. {
  844. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  845. }
  846. ///*定位信息(MGNSS)
  847. else if(strncmp((char*)U4_4GrecvBuff, "+MGNSSURC: \"state\",1", strlen("+MGNSSURC: \"state\",1"))==0)
  848. {
  849. printf("@MGNSS设置成功\r\n");
  850. memset(U4_4GrecvBuff,0,strlen("+MGNSSURC: \"state\",1"));
  851. }
  852. else if(strncmp((char*)U4_4GrecvBuff, "+MGNSSURC: \"state\",0", strlen("+MGNSSURC: \"state\",0"))==0)//断开平台
  853. {
  854. printf("@主动取消GNSS\r\n");
  855. memset(U4_4GrecvBuff,0,strlen("+MGNSSURC: \"state\",0"));
  856. }
  857. ///*定位信息(基站)
  858. else if(((strncmp((char*)U4_4GrecvBuff, "+MGNSSLOC", strlen("+MGNSSLOC"))==0) || (strncmp((char*)U4_4GrecvBuff, "OK", strlen("OK"))==0)) && MgnssFlag) //监测平台回复+MGNSSLOC OK4f4b
  859. {
  860. memcpy(MGNSSLOC,&U4_4GrecvBuff[11],75);
  861. GnssFlag=true;
  862. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  863. }
  864. ///*掉线
  865. #if open4G ==1
  866. else if(strncmp((char*)U4_4GrecvBuff, "+MIPURC: \"disconn\",1,1", strlen("+MIPURC: \"disconn\",1,1"))==0)//"disconn" 平台断连
  867. {
  868. Offline=true;
  869. printf("@未发送成功,平台断开连接\r\n");
  870. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  871. uploaweork = false;
  872. reconn();//重连
  873. }
  874. else if((strncmp((char*)U4_4GrecvBuff, "+CPIN: SIM REMOVED", strlen("+CPIN: SIM REMOVED"))==0) || (strncmp((char*)U4_4GrecvBuff, "+CME ERROR: 10", strlen("+CME ERROR: 10"))==0) )//无SIM
  875. {
  876. //TODO
  877. printf("@SIM卡缺失\r\n");
  878. Offline=true;
  879. signal = false;
  880. menu.current = 0xBB;
  881. tipsflag = NoSIM;
  882. refresh=true;
  883. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  884. uploaweork = false;
  885. reconn();//重连
  886. }
  887. else if((strncmp((char*)U4_4GrecvBuff, "+MIPCALL: 1,0", strlen("+MIPCALL: 1,0"))==0))//意外断开平台连接!!!!//TCP未知错误
  888. {
  889. //TODO
  890. Offline=true;
  891. signal = false;
  892. printf("@与平台断开连接\r\n");
  893. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  894. uploaweork = false;
  895. reconn();//重连
  896. }
  897. else if( (strncmp((char*)U4_4GrecvBuff, "+CME ERROR: 550", strlen("+CME ERROR: 550"))==0))
  898. {
  899. Offline=true;
  900. signal = false;
  901. printf("@未发送成功->TCP/IP未知错误\r\n");
  902. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  903. uploaweork = false;
  904. reconn();//重连
  905. }
  906. else if((strncmp((char*)U4_4GrecvBuff, "+MIPOPEN: 1,571", strlen("+MIPOPEN: 1,571"))==0))//PDP激活失败
  907. {
  908. //TODO
  909. Offline=true;
  910. printf("@未发送成功,PDP激活失败\r\n");
  911. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  912. uploaweork = false;
  913. reconn();//重连
  914. }
  915. #endif
  916. else if(U4_4GrecvBuff[1]== 0x43 && U4_4GrecvBuff[2]== 0x53 && U4_4GrecvBuff[3]== 0x51)//CSQ 信号
  917. {
  918. memcpy(CSQ,U4_4GrecvBuff,U4_4GrecvLength);
  919. dblocptr->location.CSQ = (uint8_t)(CSQ[6]&0xf*10)+(CSQ[7]&0xf);
  920. memset(CSQ,0,12);
  921. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  922. }
  923. else if(strncmp((char*)U4_4GrecvBuff, "+MIPURC", strlen("+MIPURC"))==0) //监测平台回复+MIPURC:
  924. {
  925. uint8_t high_byte = (U4_4GrecvBuff[18]&0x0f) *10;
  926. uint8_t low_byte = U4_4GrecvBuff[19]&0x0f;
  927. hccd = high_byte + low_byte;
  928. memcpy(MIPurc1,&U4_4GrecvBuff[21],hccd*2);
  929. const char *hex = (const char *)MIPurc1; // 将 uint8_t * 转换为 const char *
  930. int size = strlen(hex) / 2; // 计算二进制数据的长度
  931. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  932. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  933. memcpy(MIPurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  934. free(bin); // 释放内存
  935. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  936. memset(MIPurc1,0,100);
  937. }
  938. }
  939. void MIPURCHandle(void)
  940. {
  941. #if open4G == 1
  942. if(MIPurc[0]==0x7E)
  943. {
  944. uint16_t PTXXID = MIPurc[1]<<8 | MIPurc[2]; //消息ID
  945. uint16_t xxtcLen = MIPurc[3]<<8 | MIPurc[4]; //数据长度
  946. uint16_t xxRE = MIPurc[15]<<8 | MIPurc[16]; //回复ID
  947. uint8_t YESorNO = MIPurc[17]; //00 success ; 01 fail
  948. if(PTXXID==XXID_pttyyd) //收到平台通用应答0x8001
  949. {
  950. if(xxRE == MessageID_authentication)//回复鉴权ID
  951. {
  952. if(YESorNO==YES)
  953. {
  954. printf("@鉴权应答yes\r\n");
  955. authFlag = true;
  956. Offline=false; //待判定假消息
  957. Online = true; //待判定假消息
  958. uploaweork=true;
  959. }
  960. else if(YESorNO==NO)
  961. {
  962. printf("@鉴权应答no\r\n");
  963. }
  964. memset(MIPurc,0,rlt);//rlt下发长度
  965. }
  966. else if(xxRE == MessageID_heartbeat)//回复鉴权ID
  967. {
  968. if(YESorNO==YES)
  969. {
  970. printf("@心跳应答yes\r\n");
  971. printf("@TCP连接成功!!\r\n");
  972. menu.current=0;
  973. refresh=true;
  974. }
  975. else if(YESorNO==NO)
  976. {
  977. printf("@心跳应答no\r\n");
  978. }
  979. memset(MIPurc,0,rlt);//rlt下发长度
  980. }
  981. else if(xxRE == MessageID_location)//回复鉴权ID
  982. {
  983. if(YESorNO==YES)
  984. {
  985. printf("@地址应答yes\r\n");
  986. }
  987. else if(YESorNO==NO)
  988. {
  989. printf("@地址应答no\r\n");
  990. }
  991. memset(MIPurc,0,rlt);//rlt下发长度
  992. }
  993. else if(xxRE == MessageID_Getauth)//获取权限
  994. {
  995. if(YESorNO==YES)
  996. {
  997. printf("@权限应答yes\r\n");
  998. reAuth=true;
  999. }
  1000. else if(YESorNO==NO)
  1001. {
  1002. printf("@权限应答no\r\n");
  1003. reAuth=false;
  1004. }
  1005. memset(MIPurc,0,rlt);//rlt下发长度
  1006. }
  1007. else if(xxRE == MessageID_uploadLock)//锁信息上传
  1008. {
  1009. if(YESorNO==YES)
  1010. {
  1011. printf("@锁信息上传应答yes\r\n");
  1012. if(sendCmd_BT("AT+DISCONN=0\r\n","ERROR",1,2))
  1013. {
  1014. printf("已断开连接\r\n");
  1015. }
  1016. }
  1017. else if(YESorNO==NO)
  1018. {
  1019. printf("@锁信息上传应答no\r\n");
  1020. }
  1021. memset(MIPurc,0,rlt);//rlt下发长度
  1022. }
  1023. }
  1024. else if(PTXXID == XXID_ptxxtc) //收到平台消息透传 8900
  1025. {
  1026. authFlag = true;
  1027. Offline=false; //待判定假消息
  1028. Online = true; //待判定假消息
  1029. uploaweork=true;
  1030. uint8_t tcsjLen = MIPurc[13];
  1031. memcpy(MIPurcXXTCSJ,&MIPurc[13],xxtcLen);
  1032. printf("@得到透传数据,%d\r\n",tcsjLen);
  1033. memset(MIPurc,0,rlt);//rlt下发长度
  1034. }
  1035. }
  1036. else if(authFlag)
  1037. {
  1038. authFlag = false;
  1039. HAL_Delay(500);
  1040. uint8_t test[] = {0x05}; //test心跳
  1041. tt808FsFunc(test,1,MessageID_heartbeat); //test心跳
  1042. }
  1043. #endif
  1044. else if(reIME)
  1045. {
  1046. reIME=false;
  1047. phonenum[0] = DTUurc[0]<<8|DTUurc[1];
  1048. phonenum[1] = DTUurc[2]<<8|DTUurc[3];
  1049. phonenum[2] = DTUurc[4]<<8|DTUurc[5];
  1050. phonenull =false;
  1051. Flash_WriteBytes(phonenum,phoneNumADDR,3);//更新ime
  1052. //memset(DTUurc,0,100);
  1053. printf("IMEI设置完成\r\n");
  1054. printf("即将重启\r\n");
  1055. wdiFlag = true;
  1056. }
  1057. }
  1058. time_t timestamp ;
  1059. void time_bj(void)
  1060. {
  1061. struct tm timeinfo;
  1062. timeinfo.tm_year = tt808.Rtime[0]+2000-1900;
  1063. timeinfo.tm_mon = tt808.Rtime[1]-1;
  1064. timeinfo.tm_mday = tt808.Rtime[2];
  1065. timeinfo.tm_hour = tt808.Rtime[3]-8;
  1066. timeinfo.tm_min = tt808.Rtime[4];
  1067. timeinfo.tm_sec = tt808.Rtime[5];
  1068. timestamp = mktime(&timeinfo);
  1069. dblocptr->location.timeStamp = timestamp;
  1070. }
  1071. void ReU4Proces(void)
  1072. {
  1073. if(GnssFlag)
  1074. {
  1075. GnssFlag=false;
  1076. RTCtime();//同步时间
  1077. // 调用函数提取字符串
  1078. extractString(MGNSSLOC, cSpeed,7 ,8);//采集
  1079. dSpeed = convertToDouble(cSpeed); //转换浮点数
  1080. dSpeed*=10;
  1081. // 调用函数提取字符串
  1082. extractString(MGNSSLOC, clatitude,1 ,2);//采集
  1083. dlatitude = convertToDouble(clatitude); //转换浮点数
  1084. uint32_t latitudetemp=convertGPS(dlatitude);
  1085. dblocptr->location.latitudeH = (latitudetemp>>16)&0xFFFF;
  1086. dblocptr->location.latitudeH = swapBytes16(dblocptr->location.latitudeH);//转换H字节
  1087. dblocptr->location.latitudeL = (latitudetemp)&0xFFFF;
  1088. dblocptr->location.latitudeL = swapBytes16(dblocptr->location.latitudeL);//转换L字节
  1089. extractString(MGNSSLOC, clongitude,2 ,3);//采集
  1090. dlongitude = convertToDouble(clongitude); //转换浮点数
  1091. uint32_t longitudetemp=convertGPS(dlongitude);
  1092. dblocptr->location.longitudeH = (longitudetemp>>16)&0xFFFF;
  1093. dblocptr->location.longitudeH = swapBytes16(dblocptr->location.longitudeH);//转换H字节
  1094. dblocptr->location.longitudeL = (longitudetemp)&0xFFFF;
  1095. dblocptr->location.longitudeL = swapBytes16(dblocptr->location.longitudeL);//转换L字节
  1096. // printf("%d\r\n",dblocptr->location.latitude);
  1097. // printf("%d\r\n",dblocptr->location.longitude);
  1098. // printf("%08X\r\n",dblocptr->location.latitude);
  1099. // printf("%08X\r\n",dblocptr->location.longitude);
  1100. extractString(MGNSSLOC, csatellitesNum,10 ,11);//采集卫星个数
  1101. dblocptr->location.satellitesNum = convertToDouble(csatellitesNum); //转换浮点数
  1102. //printf("%d",tt808.satellitesNum);
  1103. dblocptr->location.speed = (uint16_t)dSpeed;
  1104. memset(MGNSSLOC,0,100);
  1105. }
  1106. }
  1107. //*离线蓝牙锁