tt808.c 36 KB

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