tt808.c 36 KB

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