tt808.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. void MqttRe(void)
  461. {
  462. if(strncmp((const char *)U4_4GrecvBuff, "+MQTTURC: \"publish\"", strlen("+MQTTURC: \"publish\""))==0)//接收到发布信息
  463. {
  464. memcpy(MqttReBuff,&U4_4GrecvBuff[2],U4_4GrecvLength-2);//全部copy
  465. //printf("@接收到Mqtt消息%s\r\n",MqttReBuff);
  466. extractString(MqttReBuff, Mqtttotal_len,4 ,5);//采集<total_len>
  467. extractString(MqttReBuff, payload_len,5 ,6);//采集<payload_len>
  468. Mqtttotal_lenu8 = atoi(Mqtttotal_len);
  469. payload_lenu8 = atoi(payload_len);
  470. //printf("%d,%d\r\n",Mqtttotal_lenu8,payload_lenu8);
  471. if(Mqtttotal_lenu8==payload_lenu8)
  472. {
  473. memcpy(MqttWorkBuff, &MqttReBuff[U4_4GrecvLength - payload_lenu8-4], payload_lenu8);
  474. printf("%s\r\n",MqttWorkBuff);
  475. }
  476. //*处理摘取字段
  477. if(strncmp((const char*)&MqttWorkBuff, "DTU+FLASH=deleteloc", strlen("DTU+FLASH=deleteloc"))==0)
  478. {
  479. dblocptr->headinfo.loc_pushIndex=0;
  480. dblocptr->headinfo.loc_pullIndex=0;
  481. Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度
  482. printf("更新刻度成功\r\n");
  483. }
  484. else if(strncmp((char*)MqttWorkBuff, "DTU+JT808_IMEI=", strlen("DTU+JT808_IMEI="))==0)//修改IME号
  485. {
  486. memcpy(DTUurc1,MqttWorkBuff+15,12);
  487. const char *hex = (const char *)DTUurc1; // 将 uint8_t * 转换为 const char *
  488. int size = strlen(hex) / 2; // 计算二进制数据的长度
  489. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  490. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  491. memcpy(DTUurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  492. free(bin); // 释放内存
  493. reIME = true;
  494. HAL_Delay(500);
  495. }
  496. else if(strncmp((char*)MqttWorkBuff, "DTU+JT808_SERVER=", strlen("DTU+JT808_SERVER="))==0)
  497. {
  498. memcpy(input_strings,MqttWorkBuff+17,20);
  499. extractStringPoint(input_strings,reIp0,0,1);
  500. extractStringPoint(input_strings,reIp1,1,2);
  501. extractStringPoint(input_strings,reIp2,2,3);
  502. extractStringPoint(input_strings,reIp3,3,4);
  503. extractStringPoint(input_strings,reIpP,4,5);
  504. ipaddr.IP0 = atoi(reIp0);
  505. ipaddr.IP1 = atoi(reIp1);
  506. ipaddr.IP2 = atoi(reIp2);
  507. ipaddr.IP3 = atoi(reIp3);
  508. ipaddr.IPp = atoi(reIpP);
  509. //printf("%d.%d.%d.%d:%d",ipaddr.IP0,ipaddr.IP1,ipaddr.IP2,ipaddr.IP3,ipaddr.IPp);
  510. HAL_StatusTypeDef writeSta = Flash_WriteBytes((uint16_t*)&(ipaddr.IP0),ReIPAddr,5);
  511. if(writeSta == HAL_OK)
  512. {
  513. char *ptr = Newip;
  514. ptr += sprintf(ptr, "%d.", ipaddr.IP0);
  515. ptr += sprintf(ptr, "%d.", ipaddr.IP1);
  516. ptr += sprintf(ptr, "%d.", ipaddr.IP2);
  517. ptr += sprintf(ptr, "%d", ipaddr.IP3);
  518. printf("更换IP成功\r\n");
  519. }
  520. //ptr += sprintf(ptr, ":%d", ipaddr.IPp);
  521. //printf("%s\r\n",Newip);
  522. HAL_Delay(500);
  523. printf("即将重启\r\n");
  524. wdiFlag = true;
  525. }
  526. else if(strncmp((char*)MqttWorkBuff, "DTU+VER=?", strlen("DTU+VER=?"))==0)
  527. {
  528. printf("%s\r\n",version);
  529. }
  530. else if(strncmp((char*)MqttWorkBuff, "DTU+MANUALMODE=1", strlen("DTU+MANUALMODE=1"))==0)
  531. {
  532. ManualWork = 1;
  533. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  534. printf("手动模式开启\r\n");
  535. }
  536. else if(strncmp((char*)MqttWorkBuff, "DTU+MANUALMODE=0", strlen("DTU+MANUALMODE=0"))==0)
  537. {
  538. ManualWork = 0;
  539. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  540. printf("手动模式关闭\r\n");
  541. }
  542. HAL_StatusTypeDef WriteSta;
  543. //*PDA需求 485写入MAC号
  544. if(strncmp((char*)MqttWorkBuff, "DTU+ADDMAC", strlen("DTU+ADDMAC"))==0)
  545. {
  546. macset.macCount=0;
  547. workedLock=0;
  548. memset(devices,0,8);
  549. erase_flash(MacDbBodyAddr);
  550. if(payload_lenu8>12){
  551. memcpy(macset.MACtemp[0],MqttWorkBuff+11,12);
  552. processPdadata(macset.MACtemp[0],macset.mactemp[0],macset.mac[0]);
  553. WriteSta= Flash_WriteBytes(macsetptr->mac[0],MacDbBodyAddr,3);
  554. if(WriteSta== HAL_OK) macset.macCount += 1;}
  555. if(payload_lenu8>25){
  556. memcpy(macset.MACtemp[1],MqttWorkBuff+24,12);
  557. processPdadata(macset.MACtemp[1],macset.mactemp[1],macset.mac[1]);
  558. WriteSta= Flash_WriteBytes(macsetptr->mac[1],MacDbBodyAddr+(macset.macCount*6),3);
  559. if(WriteSta== HAL_OK) macset.macCount += 1;}
  560. if(payload_lenu8>38){
  561. memcpy(macset.MACtemp[2],MqttWorkBuff+37,12);
  562. processPdadata(macset.MACtemp[2],macset.mactemp[2],macset.mac[2]);
  563. WriteSta= Flash_WriteBytes(macsetptr->mac[2],MacDbBodyAddr+(macset.macCount*6),3);
  564. if(WriteSta== HAL_OK) macset.macCount += 1;}
  565. if(payload_lenu8>51){
  566. memcpy(macset.MACtemp[3],MqttWorkBuff+50,12);
  567. processPdadata(macset.MACtemp[3],macset.mactemp[3],macset.mac[3]);
  568. WriteSta= Flash_WriteBytes(macsetptr->mac[3],MacDbBodyAddr+(macset.macCount*6),3);
  569. if(WriteSta== HAL_OK) macset.macCount += 1;}
  570. if(payload_lenu8>64){
  571. memcpy(macset.MACtemp[4],MqttWorkBuff+63,12);
  572. processPdadata(macset.MACtemp[4],macset.mactemp[4],macset.mac[4]);
  573. WriteSta= Flash_WriteBytes(macsetptr->mac[4],MacDbBodyAddr+(macset.macCount*6),3);
  574. if(WriteSta== HAL_OK) macset.macCount += 1;}
  575. if(payload_lenu8>77){
  576. memcpy(macset.MACtemp[5],MqttWorkBuff+76,12);
  577. processPdadata(macset.MACtemp[5],macset.mactemp[5],macset.mac[5]);
  578. WriteSta= Flash_WriteBytes(macsetptr->mac[5],MacDbBodyAddr+(macset.macCount*6),3);
  579. if(WriteSta== HAL_OK) macset.macCount += 1;}
  580. if(payload_lenu8>90){
  581. memcpy(macset.MACtemp[6],MqttWorkBuff+89,12);
  582. processPdadata(macset.MACtemp[6],macset.mactemp[6],macset.mac[6]);
  583. WriteSta= Flash_WriteBytes(macsetptr->mac[6],MacDbBodyAddr+(macset.macCount*6),3);
  584. if(WriteSta== HAL_OK) macset.macCount += 1;}
  585. if(payload_lenu8>103){
  586. memcpy(macset.MACtemp[7],MqttWorkBuff+102,12);
  587. processPdadata(macset.MACtemp[7],macset.mactemp[7],macset.mac[7]);
  588. WriteSta= Flash_WriteBytes(macsetptr->mac[7],MacDbBodyAddr+(macset.macCount*6),3);
  589. if(WriteSta== HAL_OK) macset.macCount += 1;}
  590. for(int i = 0; i < macset.macCount; i++) {
  591. memset(lockinfo.SNname[i], 0, 13);
  592. }
  593. if(WriteSta == HAL_OK)
  594. {
  595. printf("更新MAC存储完成共->%d\r\n",macset.macCount);
  596. }
  597. WriteSta = Flash_WriteBytes(&macsetptr->macCount,MacDbHeadAddr,1);
  598. if(WriteSta == HAL_OK)
  599. {
  600. printf("更新存储刻度完成\r\n");
  601. }
  602. ReadSNname();//更新
  603. Process_str_collections();//处理连接字符串
  604. enterInterface_time = HAL_GetTick();
  605. menu.current=6;//提示mac
  606. refresh=true;
  607. }
  608. else if(strncmp((char*)MqttWorkBuff, "DTU+WORK=ALLOPEN", strlen("DTU+WORK=ALLOPEN"))==0)
  609. {
  610. enterInterface_time = HAL_GetTick();
  611. SET_MENU_STATUS(2,2,0,0);
  612. Allopen=true;
  613. }
  614. else if(strncmp((char*)MqttWorkBuff, "DTU+WORK=ALLCLOSE", strlen("DTU+WORK=ALLCLOSE"))==0)
  615. {
  616. enterInterface_time = HAL_GetTick();
  617. SET_MENU_STATUS(2,1,1,1);
  618. Allopen=true;
  619. }
  620. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  621. memset(MqttReBuff,0,U4_4GrecvLength);
  622. memset(MqttWorkBuff,0,payload_lenu8);
  623. }
  624. }
  625. //*485测试命令
  626. void Test485Re(void)
  627. {
  628. //*test485 模拟
  629. if(strncmp((char*)U1_485recvBuff, "+MIPURC: \"disconn\",1,1", strlen("+MIPURC: \"disconn\",1,1"))==0)//"disconn" 平台断连
  630. {
  631. Offline=true;
  632. uploaweork=false;
  633. printf("@U1与平台断开连接\r\n");
  634. memset(U1_485recvBuff,0,BUFFER_SIZE);
  635. }
  636. else if(strncmp((char*)U1_485recvBuff, "+MIPOPEN: 1,0", strlen("+MIPOPEN: 1,0"))==0)//连接平台
  637. {
  638. Offline=false;
  639. Online = true;
  640. uploaweork=true;
  641. printf("@U1设备已上线!!\r\n");
  642. refresh=true;
  643. memset(U1_485recvBuff,0,BUFFER_SIZE);
  644. }
  645. else if(strncmp((char*)&U1_485recvBuff, "DTU+FLASH=deleteloc", strlen("DTU+FLASH=deleteloc"))==0)//连接平台
  646. {
  647. dblocptr->headinfo.loc_pushIndex=0;
  648. dblocptr->headinfo.loc_pullIndex=0;
  649. Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度
  650. memset(U1_485recvBuff,0,BUFFER_SIZE);
  651. printf("更新刻度成功\r\n");
  652. }
  653. else if(strncmp((char*)U1_485recvBuff, "DTU+JT808_IMEI=", strlen("DTU+JT808_IMEI="))==0)//修改IME号
  654. {
  655. memcpy(DTUurc1,U1_485recvBuff+15,12);
  656. const char *hex = (const char *)DTUurc1; // 将 uint8_t * 转换为 const char *
  657. int size = strlen(hex) / 2; // 计算二进制数据的长度
  658. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  659. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  660. memcpy(DTUurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  661. free(bin); // 释放内存
  662. //memset(DTUurc1,0,100);
  663. memset(U1_485recvBuff,0,BUFFER_SIZE);
  664. reIME = true;
  665. HAL_Delay(500);
  666. }
  667. else if(strncmp((char*)U1_485recvBuff, "DTU+JT808_SERVER=", strlen("DTU+JT808_SERVER="))==0)
  668. {
  669. memcpy(input_strings,U1_485recvBuff+17,20);
  670. extractStringPoint(input_strings,reIp0,0,1);
  671. extractStringPoint(input_strings,reIp1,1,2);
  672. extractStringPoint(input_strings,reIp2,2,3);
  673. extractStringPoint(input_strings,reIp3,3,4);
  674. extractStringPoint(input_strings,reIpP,4,5);
  675. ipaddr.IP0 = atoi(reIp0);
  676. ipaddr.IP1 = atoi(reIp1);
  677. ipaddr.IP2 = atoi(reIp2);
  678. ipaddr.IP3 = atoi(reIp3);
  679. ipaddr.IPp = atoi(reIpP);
  680. //printf("%d.%d.%d.%d:%d",ipaddr.IP0,ipaddr.IP1,ipaddr.IP2,ipaddr.IP3,ipaddr.IPp);
  681. HAL_StatusTypeDef writeSta = Flash_WriteBytes((uint16_t*)&(ipaddr.IP0),ReIPAddr,5);
  682. if(writeSta == HAL_OK)
  683. {
  684. char *ptr = Newip;
  685. ptr += sprintf(ptr, "%d.", ipaddr.IP0);
  686. ptr += sprintf(ptr, "%d.", ipaddr.IP1);
  687. ptr += sprintf(ptr, "%d.", ipaddr.IP2);
  688. ptr += sprintf(ptr, "%d", ipaddr.IP3);
  689. printf("更换IP成功\r\n");
  690. }
  691. //ptr += sprintf(ptr, ":%d", ipaddr.IPp);
  692. //printf("%s\r\n",Newip);
  693. memset(U1_485recvBuff,0,BUFFER_SIZE);
  694. HAL_Delay(500);
  695. printf("即将重启\r\n");
  696. wdiFlag = true;
  697. }
  698. else if(strncmp((char*)U1_485recvBuff, "DTU+VER=?", strlen("DTU+VER=?"))==0)
  699. {
  700. printf("%s\r\n",version);
  701. memset(U1_485recvBuff,0,BUFFER_SIZE);
  702. }
  703. else if(strncmp((char*)U1_485recvBuff, "DTU+WORK=ALLOPEN", strlen("DTU+WORK=ALLOPEN"))==0)
  704. {
  705. enterInterface_time = HAL_GetTick();
  706. SET_MENU_STATUS(2,2,0,0);
  707. Allopen=true;
  708. memset(U1_485recvBuff,0,BUFFER_SIZE);
  709. }
  710. else if(strncmp((char*)U1_485recvBuff, "DTU+WORK=ALLCLOSE", strlen("DTU+WORK=ALLCLOSE"))==0)
  711. {
  712. enterInterface_time = HAL_GetTick();
  713. SET_MENU_STATUS(2,1,1,1);
  714. Allopen=true;
  715. memset(U1_485recvBuff,0,BUFFER_SIZE);
  716. }
  717. else if(strncmp((char*)U1_485recvBuff, "DTU+MANUALMODE=1", strlen("DTU+MANUALMODE=1"))==0)
  718. {
  719. ManualWork = 1;
  720. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  721. printf("手动模式开启\r\n");
  722. memset(U1_485recvBuff,0,BUFFER_SIZE);
  723. }
  724. else if(strncmp((char*)U1_485recvBuff, "DTU+MANUALMODE=0", strlen("DTU+MANUALMODE=0"))==0)
  725. {
  726. ManualWork = 0;
  727. Flash_WriteBytes(&ManualWork,ManualWorkaddr,1);
  728. printf("手动模式关闭\r\n");
  729. memset(U1_485recvBuff,0,BUFFER_SIZE);
  730. }
  731. REPdaMode();//PDA 485 MQTT
  732. }
  733. void AtPRocess(char *value, char conn[],char connected[],char disconn[]) {
  734. // 使用snprintf代替sprintf以确保不会发生缓冲区溢出
  735. snprintf(conn, strlen(value) + 12, "AT+CONN=%s\r\n", value);
  736. snprintf(connected, strlen(value) + 14, "+CONNECTED:0,%s", value);
  737. snprintf(disconn, strlen(value) + 12, "+DISCONN:0,%s", value);
  738. // printf("%s\r\n", conn);
  739. // printf("%s\r\n", connected);
  740. // printf("%s\r\n", disconn);
  741. }
  742. void processPdadata(char MACtemp[], uint8_t mactemp[],uint16_t mac[])
  743. {
  744. const char *hex = (const char *)MACtemp; // 将 uint8_t * 转换为 const char *
  745. int size = strlen(hex) / 2; // 计算二进制数据的长度
  746. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  747. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  748. memcpy(mactemp, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  749. free(bin); // 释放内存
  750. mac[0] = mactemp[0]<<8|mactemp[1];
  751. mac[1] = mactemp[2]<<8|mactemp[3];
  752. mac[2] = mactemp[4]<<8|mactemp[5];
  753. }
  754. #define MAC_LENGTH 12
  755. #define MAC_DB_ENTRY_SIZE 3
  756. void REPdaMode(void)
  757. {
  758. HAL_StatusTypeDef WriteSta;
  759. //*PDA需求 485写入MAC号
  760. if(strncmp((char*)U1_485recvBuff, "DTU+ADDMAC", strlen("DTU+ADDMAC"))==0)
  761. {
  762. macset.macCount=0;
  763. workedLock=0;
  764. memset(devices,0,8);
  765. erase_flash(MacDbBodyAddr);
  766. if(U1_485recvLength>12){
  767. memcpy(macset.MACtemp[0],U1_485recvBuff+11,12);
  768. processPdadata(macset.MACtemp[0],macset.mactemp[0],macset.mac[0]);
  769. WriteSta= Flash_WriteBytes(macsetptr->mac[0],MacDbBodyAddr,3);
  770. if(WriteSta== HAL_OK) macset.macCount += 1;}
  771. if(U1_485recvLength>25){
  772. memcpy(macset.MACtemp[1],U1_485recvBuff+24,12);
  773. processPdadata(macset.MACtemp[1],macset.mactemp[1],macset.mac[1]);
  774. WriteSta= Flash_WriteBytes(macsetptr->mac[1],MacDbBodyAddr+(macset.macCount*6),3);
  775. if(WriteSta== HAL_OK) macset.macCount += 1;}
  776. if(U1_485recvLength>38){
  777. memcpy(macset.MACtemp[2],U1_485recvBuff+37,12);
  778. processPdadata(macset.MACtemp[2],macset.mactemp[2],macset.mac[2]);
  779. WriteSta= Flash_WriteBytes(macsetptr->mac[2],MacDbBodyAddr+(macset.macCount*6),3);
  780. if(WriteSta== HAL_OK) macset.macCount += 1;}
  781. if(U1_485recvLength>51){
  782. memcpy(macset.MACtemp[3],U1_485recvBuff+50,12);
  783. processPdadata(macset.MACtemp[3],macset.mactemp[3],macset.mac[3]);
  784. WriteSta= Flash_WriteBytes(macsetptr->mac[3],MacDbBodyAddr+(macset.macCount*6),3);
  785. if(WriteSta== HAL_OK) macset.macCount += 1;}
  786. if(U1_485recvLength>64){
  787. memcpy(macset.MACtemp[4],U1_485recvBuff+63,12);
  788. processPdadata(macset.MACtemp[4],macset.mactemp[4],macset.mac[4]);
  789. WriteSta= Flash_WriteBytes(macsetptr->mac[4],MacDbBodyAddr+(macset.macCount*6),3);
  790. if(WriteSta== HAL_OK) macset.macCount += 1;}
  791. if(U1_485recvLength>77){
  792. memcpy(macset.MACtemp[5],U1_485recvBuff+76,12);
  793. processPdadata(macset.MACtemp[5],macset.mactemp[5],macset.mac[5]);
  794. WriteSta= Flash_WriteBytes(macsetptr->mac[5],MacDbBodyAddr+(macset.macCount*6),3);
  795. if(WriteSta== HAL_OK) macset.macCount += 1;}
  796. if(U1_485recvLength>90){
  797. memcpy(macset.MACtemp[6],U1_485recvBuff+89,12);
  798. processPdadata(macset.MACtemp[6],macset.mactemp[6],macset.mac[6]);
  799. WriteSta= Flash_WriteBytes(macsetptr->mac[6],MacDbBodyAddr+(macset.macCount*6),3);
  800. if(WriteSta== HAL_OK) macset.macCount += 1;}
  801. if(U1_485recvLength>103){
  802. memcpy(macset.MACtemp[7],U1_485recvBuff+102,12);
  803. processPdadata(macset.MACtemp[7],macset.mactemp[7],macset.mac[7]);
  804. WriteSta= Flash_WriteBytes(macsetptr->mac[7],MacDbBodyAddr+(macset.macCount*6),3);
  805. if(WriteSta== HAL_OK) macset.macCount += 1;}
  806. for(int i = 0; i < macset.macCount; i++) {
  807. memset(lockinfo.SNname[i], 0, 13);
  808. }
  809. if(WriteSta == HAL_OK)
  810. {
  811. printf("更新MAC存储完成共->%d\r\n",macset.macCount);
  812. }
  813. WriteSta = Flash_WriteBytes(&macsetptr->macCount,MacDbHeadAddr,1);
  814. if(WriteSta == HAL_OK)
  815. {
  816. printf("更新存储刻度完成\r\n");
  817. }
  818. ReadSNname();//更新
  819. Process_str_collections();//处理连接字符串
  820. enterInterface_time = HAL_GetTick();
  821. menu.current=6;//提示mac
  822. refresh=true;
  823. memset(U1_485recvBuff,0,BUFFER_SIZE);
  824. }
  825. }
  826. bool snConnected[NUM_OF_DEVICES] = {false};
  827. void ReBTcall(void)
  828. {
  829. for (int i = 0; i < NUM_OF_DEVICES; ++i) {
  830. if (strncmp((char*)autoRe, macsetptr->SNED[i], strlen(macsetptr->SNED[i])) == 0) {
  831. snConnected[i] = true;
  832. memset(autoRe, 0, BUFFER_SIZE);
  833. break; // Exit the loop once a match is found
  834. }
  835. }
  836. }
  837. //*工作命令
  838. void ReMIPURC(void)//DTU
  839. {
  840. //*设备上线,发送成功
  841. if(strncmp((char*)U4_4GrecvBuff, "+MIPOPEN: 1,0", strlen("+MIPOPEN: 1,0"))==0)//连接平台
  842. {
  843. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  844. }
  845. ///*定位信息(MGNSS)
  846. else if(strncmp((char*)U4_4GrecvBuff, "+MGNSSURC: \"state\",1", strlen("+MGNSSURC: \"state\",1"))==0)
  847. {
  848. printf("@MGNSS设置成功\r\n");
  849. memset(U4_4GrecvBuff,0,strlen("+MGNSSURC: \"state\",1"));
  850. }
  851. else if(strncmp((char*)U4_4GrecvBuff, "+MGNSSURC: \"state\",0", strlen("+MGNSSURC: \"state\",0"))==0)//断开平台
  852. {
  853. printf("@主动取消GNSS\r\n");
  854. memset(U4_4GrecvBuff,0,strlen("+MGNSSURC: \"state\",0"));
  855. }
  856. ///*定位信息(基站)
  857. else if(((strncmp((char*)U4_4GrecvBuff, "+MGNSSLOC", strlen("+MGNSSLOC"))==0) || (strncmp((char*)U4_4GrecvBuff, "OK", strlen("OK"))==0)) && MgnssFlag) //监测平台回复+MGNSSLOC OK4f4b
  858. {
  859. memcpy(MGNSSLOC,&U4_4GrecvBuff[11],75);
  860. GnssFlag=true;
  861. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  862. }
  863. ///*掉线
  864. #if open4G ==1
  865. else if(strncmp((char*)U4_4GrecvBuff, "+MIPURC: \"disconn\",1,1", strlen("+MIPURC: \"disconn\",1,1"))==0)//"disconn" 平台断连
  866. {
  867. Offline=true;
  868. printf("@未发送成功,平台断开连接\r\n");
  869. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  870. uploaweork = false;
  871. reconn();//重连
  872. }
  873. 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
  874. {
  875. //TODO
  876. printf("@SIM卡缺失\r\n");
  877. Offline=true;
  878. signal = false;
  879. menu.current = 0xBB;
  880. tipsflag = NoSIM;
  881. refresh=true;
  882. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  883. uploaweork = false;
  884. reconn();//重连
  885. }
  886. else if((strncmp((char*)U4_4GrecvBuff, "+MIPCALL: 1,0", strlen("+MIPCALL: 1,0"))==0))//意外断开平台连接!!!!//TCP未知错误
  887. {
  888. //TODO
  889. Offline=true;
  890. signal = false;
  891. printf("@与平台断开连接\r\n");
  892. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  893. uploaweork = false;
  894. reconn();//重连
  895. }
  896. else if( (strncmp((char*)U4_4GrecvBuff, "+CME ERROR: 550", strlen("+CME ERROR: 550"))==0))
  897. {
  898. Offline=true;
  899. signal = false;
  900. printf("@未发送成功->TCP/IP未知错误\r\n");
  901. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  902. uploaweork = false;
  903. reconn();//重连
  904. }
  905. else if((strncmp((char*)U4_4GrecvBuff, "+MIPOPEN: 1,571", strlen("+MIPOPEN: 1,571"))==0))//PDP激活失败
  906. {
  907. //TODO
  908. Offline=true;
  909. printf("@未发送成功,PDP激活失败\r\n");
  910. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  911. uploaweork = false;
  912. reconn();//重连
  913. }
  914. #endif
  915. else if(U4_4GrecvBuff[1]== 0x43 && U4_4GrecvBuff[2]== 0x53 && U4_4GrecvBuff[3]== 0x51)//CSQ 信号
  916. {
  917. memcpy(CSQ,U4_4GrecvBuff,U4_4GrecvLength);
  918. dblocptr->location.CSQ = (uint8_t)(CSQ[6]&0xf*10)+(CSQ[7]&0xf);
  919. memset(CSQ,0,12);
  920. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  921. }
  922. else if(strncmp((char*)U4_4GrecvBuff, "+MIPURC", strlen("+MIPURC"))==0) //监测平台回复+MIPURC:
  923. {
  924. uint8_t high_byte = (U4_4GrecvBuff[18]&0x0f) *10;
  925. uint8_t low_byte = U4_4GrecvBuff[19]&0x0f;
  926. hccd = high_byte + low_byte;
  927. memcpy(MIPurc1,&U4_4GrecvBuff[21],hccd*2);
  928. const char *hex = (const char *)MIPurc1; // 将 uint8_t * 转换为 const char *
  929. int size = strlen(hex) / 2; // 计算二进制数据的长度
  930. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  931. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  932. memcpy(MIPurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  933. free(bin); // 释放内存
  934. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  935. memset(MIPurc1,0,100);
  936. }
  937. }
  938. void MIPURCHandle(void)
  939. {
  940. #if open4G == 1
  941. if(MIPurc[0]==0x7E)
  942. {
  943. uint16_t PTXXID = MIPurc[1]<<8 | MIPurc[2]; //消息ID
  944. uint16_t xxtcLen = MIPurc[3]<<8 | MIPurc[4]; //数据长度
  945. uint16_t xxRE = MIPurc[15]<<8 | MIPurc[16]; //回复ID
  946. uint8_t YESorNO = MIPurc[17]; //00 success ; 01 fail
  947. if(PTXXID==XXID_pttyyd) //收到平台通用应答0x8001
  948. {
  949. if(xxRE == MessageID_authentication)//回复鉴权ID
  950. {
  951. if(YESorNO==YES)
  952. {
  953. printf("@鉴权应答yes\r\n");
  954. authFlag = true;
  955. Offline=false; //待判定假消息
  956. Online = true; //待判定假消息
  957. uploaweork=true;
  958. }
  959. else if(YESorNO==NO)
  960. {
  961. printf("@鉴权应答no\r\n");
  962. }
  963. memset(MIPurc,0,rlt);//rlt下发长度
  964. }
  965. else if(xxRE == MessageID_heartbeat)//回复鉴权ID
  966. {
  967. if(YESorNO==YES)
  968. {
  969. printf("@心跳应答yes\r\n");
  970. printf("@TCP连接成功!!\r\n");
  971. menu.current=0;
  972. refresh=true;
  973. }
  974. else if(YESorNO==NO)
  975. {
  976. printf("@心跳应答no\r\n");
  977. }
  978. memset(MIPurc,0,rlt);//rlt下发长度
  979. }
  980. else if(xxRE == MessageID_location)//回复鉴权ID
  981. {
  982. if(YESorNO==YES)
  983. {
  984. printf("@地址应答yes\r\n");
  985. }
  986. else if(YESorNO==NO)
  987. {
  988. printf("@地址应答no\r\n");
  989. }
  990. memset(MIPurc,0,rlt);//rlt下发长度
  991. }
  992. else if(xxRE == MessageID_Getauth)//获取权限
  993. {
  994. if(YESorNO==YES)
  995. {
  996. printf("@权限应答yes\r\n");
  997. reAuth=true;
  998. }
  999. else if(YESorNO==NO)
  1000. {
  1001. printf("@权限应答no\r\n");
  1002. reAuth=false;
  1003. }
  1004. memset(MIPurc,0,rlt);//rlt下发长度
  1005. }
  1006. else if(xxRE == MessageID_uploadLock)//锁信息上传
  1007. {
  1008. if(YESorNO==YES)
  1009. {
  1010. printf("@锁信息上传应答yes\r\n");
  1011. if(sendCmd_BT("AT+DISCONN=0\r\n","ERROR",1,2))
  1012. {
  1013. printf("已断开连接\r\n");
  1014. }
  1015. }
  1016. else if(YESorNO==NO)
  1017. {
  1018. printf("@锁信息上传应答no\r\n");
  1019. }
  1020. memset(MIPurc,0,rlt);//rlt下发长度
  1021. }
  1022. }
  1023. else if(PTXXID == XXID_ptxxtc) //收到平台消息透传 8900
  1024. {
  1025. authFlag = true;
  1026. Offline=false; //待判定假消息
  1027. Online = true; //待判定假消息
  1028. uploaweork=true;
  1029. uint8_t tcsjLen = MIPurc[13];
  1030. memcpy(MIPurcXXTCSJ,&MIPurc[13],xxtcLen);
  1031. printf("@得到透传数据,%d\r\n",tcsjLen);
  1032. memset(MIPurc,0,rlt);//rlt下发长度
  1033. }
  1034. }
  1035. else if(authFlag)
  1036. {
  1037. authFlag = false;
  1038. HAL_Delay(500);
  1039. uint8_t test[] = {0x05}; //test心跳
  1040. tt808FsFunc(test,1,MessageID_heartbeat); //test心跳
  1041. }
  1042. #endif
  1043. else if(reIME)
  1044. {
  1045. reIME=false;
  1046. phonenum[0] = DTUurc[0]<<8|DTUurc[1];
  1047. phonenum[1] = DTUurc[2]<<8|DTUurc[3];
  1048. phonenum[2] = DTUurc[4]<<8|DTUurc[5];
  1049. phonenull =false;
  1050. Flash_WriteBytes(phonenum,phoneNumADDR,3);//更新ime
  1051. //memset(DTUurc,0,100);
  1052. printf("IMEI设置完成\r\n");
  1053. printf("即将重启\r\n");
  1054. wdiFlag = true;
  1055. }
  1056. }
  1057. time_t timestamp ;
  1058. void time_bj(void)
  1059. {
  1060. struct tm timeinfo;
  1061. timeinfo.tm_year = tt808.Rtime[0]+2000-1900;
  1062. timeinfo.tm_mon = tt808.Rtime[1]-1;
  1063. timeinfo.tm_mday = tt808.Rtime[2];
  1064. timeinfo.tm_hour = tt808.Rtime[3]-8;
  1065. timeinfo.tm_min = tt808.Rtime[4];
  1066. timeinfo.tm_sec = tt808.Rtime[5];
  1067. timestamp = mktime(&timeinfo);
  1068. dblocptr->location.timeStamp = timestamp;
  1069. }
  1070. void ReU4Proces(void)
  1071. {
  1072. if(GnssFlag)
  1073. {
  1074. GnssFlag=false;
  1075. RTCtime();//同步时间
  1076. // 调用函数提取字符串
  1077. extractString(MGNSSLOC, cSpeed,7 ,8);//采集
  1078. dSpeed = convertToDouble(cSpeed); //转换浮点数
  1079. dSpeed*=10;
  1080. // 调用函数提取字符串
  1081. extractString(MGNSSLOC, clatitude,1 ,2);//采集
  1082. dlatitude = convertToDouble(clatitude); //转换浮点数
  1083. uint32_t latitudetemp=convertGPS(dlatitude);
  1084. dblocptr->location.latitudeH = (latitudetemp>>16)&0xFFFF;
  1085. dblocptr->location.latitudeH = swapBytes16(dblocptr->location.latitudeH);//转换H字节
  1086. dblocptr->location.latitudeL = (latitudetemp)&0xFFFF;
  1087. dblocptr->location.latitudeL = swapBytes16(dblocptr->location.latitudeL);//转换L字节
  1088. extractString(MGNSSLOC, clongitude,2 ,3);//采集
  1089. dlongitude = convertToDouble(clongitude); //转换浮点数
  1090. uint32_t longitudetemp=convertGPS(dlongitude);
  1091. dblocptr->location.longitudeH = (longitudetemp>>16)&0xFFFF;
  1092. dblocptr->location.longitudeH = swapBytes16(dblocptr->location.longitudeH);//转换H字节
  1093. dblocptr->location.longitudeL = (longitudetemp)&0xFFFF;
  1094. dblocptr->location.longitudeL = swapBytes16(dblocptr->location.longitudeL);//转换L字节
  1095. // printf("%d\r\n",dblocptr->location.latitude);
  1096. // printf("%d\r\n",dblocptr->location.longitude);
  1097. // printf("%08X\r\n",dblocptr->location.latitude);
  1098. // printf("%08X\r\n",dblocptr->location.longitude);
  1099. extractString(MGNSSLOC, csatellitesNum,10 ,11);//采集卫星个数
  1100. dblocptr->location.satellitesNum = convertToDouble(csatellitesNum); //转换浮点数
  1101. //printf("%d",tt808.satellitesNum);
  1102. dblocptr->location.speed = (uint16_t)dSpeed;
  1103. memset(MGNSSLOC,0,100);
  1104. }
  1105. }
  1106. //*离线蓝牙锁