tt808.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. /**
  51. 时间 纬度(北纬) 经度(东经) 水平精度因子 海拔高度 定位类型(1无,2 2d,3 3d) 运动角度 水平运动速度(KM/h) 水平运动速度(Knots) 日月年 卫星数量 差分定位标识(1单点,2差分)
  52. eg.+MGNSSLOC: 015032.301,3014.8605N,12001.9250E,1.8, 17.6, 3, 0.00, 3.6, 1.9, 080424, 12, 1
  53. */
  54. void reconn(void)
  55. {
  56. char TCPconnvalue[120];
  57. TcpConn(TCPconnvalue, 1, JLPTIP, JLPTPORT,0); //cid=1 tcpConnmode=0 普通模式
  58. if(sendCmd_4G(TCPconnvalue, "+MIPOPEN: 1,0", 1, 1)) //TCP连接 0普通模式 2缓存模式 1透传模式
  59. {
  60. printf("重连成功\r\n");
  61. }
  62. }
  63. uint8_t Messagetemp_length=0;
  64. uint8_t MessageHandleData[100]; //放入封装后数据
  65. uint8_t tcppacket[100]; //消息数据
  66. void MessageHandle(uint16_t MessageID)
  67. {
  68. Flash_ReadBytes((uint16_t*)&(tt808.LSH),tt808serialNum,1);//读取loc头部是否发生写入
  69. if(tt808.LSH==0xFFFF)
  70. {
  71. tt808.LSH=0;//初始未写入
  72. }
  73. uint16_t MessageAttributeData = Messagetemp_length;
  74. MessageAttributeData |= ( Messagetemp_length | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
  75. header[0] = ((MessageID>>8)&0xff);
  76. header[1] = (MessageID&0xff); //消息ID
  77. header[2] = ((MessageAttributeData>>8)&0xff);
  78. header[3] = (MessageAttributeData&0xff); //消息属性
  79. //*test phoneNumxy
  80. //*work phoneNum
  81. if(phonenull)
  82. {
  83. header[4] = 0x00;
  84. header[5] = 0x00;
  85. header[6] = 0x16;
  86. header[7] = 0x00;
  87. header[8] = 0x00;
  88. header[9] = 0x08; //“手机号”
  89. }
  90. else
  91. {
  92. header[4] = (phonenum[0]>>8)&0xff;
  93. header[5] = (phonenum[0])&0xff;
  94. header[6] = (phonenum[1]>>8)&0xff;
  95. header[7] = (phonenum[1])&0xff;
  96. header[8] = (phonenum[2]>>8)&0xff;
  97. header[9] = (phonenum[2])&0xff; //“手机号”
  98. }
  99. header[10] = ((tt808.LSH>>8)&0xff);
  100. header[11] = (tt808.LSH&0xff);
  101. int index = 0;
  102. tcppacket[index++] = TT808FLAG; // Set the headflag 0x7E
  103. for (int i = 0; i < 12; i++) {
  104. tcppacket[index++] = header[i];
  105. }
  106. // Copy message body to packet
  107. for (size_t i = 0; i < Messagetemp_length; i++) {
  108. tcppacket[index++] = MessageHandleData[i];
  109. }
  110. tcppacket[index++] = xorBytes(&tcppacket[1],12+Messagetemp_length);; // Set the checksum
  111. tcppacket[index++] = TT808FLAG; // Set the flag at the end
  112. for (int i = 0; i < index; i++) {
  113. printf("%02X ", tcppacket[i]); // 以十六进制格式打印每个元素
  114. }
  115. printf("\r\n");
  116. //#if open4G ==1
  117. char TCPvalue[120];
  118. char *ptr = TCPvalue;
  119. ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",index);
  120. for (int i = 0; i < index; i++) {
  121. ptr += sprintf(ptr, "%02X", tcppacket[i]);
  122. }
  123. sprintf(ptr, "\"\r\n");//TCPvalue
  124. if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送
  125. {
  126. printf("#等待回应\r\n");
  127. }
  128. //#endif
  129. tt808.LSH+=1;//需做写入处理
  130. Flash_WriteBytes((uint16_t*)&(tt808.LSH),tt808serialNum,1);
  131. }
  132. uint16_t swapBytes16(uint16_t value) {
  133. return (value >> 8) | (value << 8);
  134. }
  135. uint32_t swapBytes32(uint32_t value) {
  136. return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) |
  137. ((value >> 8) & 0xFF00) | ((value >> 24) & 0xFF);
  138. }
  139. /**
  140. * @breaf 消息体封装
  141. * Message[] 消息数据; length 消息数据长度; outMessage[] 封装后消息数据; MessageID 消息ID
  142. */
  143. uint8_t Message_bodydata(uint8_t *Message, uint8_t length, uint8_t outMessage[],uint16_t MessageID)
  144. {
  145. int index = 0;
  146. uint8_t temp_length = 0;//加首尾 25/23/crc
  147. if(MessageID==MessageID_authentication || MessageID==MessageID_heartbeat)//不加头尾(鉴权、心跳)
  148. {
  149. temp_length = length;//不加首尾
  150. uint8_t temp_Message[temp_length];
  151. for (int i = 0; i < length; i++) {
  152. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  153. }
  154. temp_length = index;
  155. for(int i =0;i<temp_length;i++)
  156. {
  157. outMessage[i]=temp_Message[i];
  158. }
  159. }
  160. else if(MessageID==MessageID_location)//终端地址0200 补传0704
  161. {
  162. temp_length = length;//不加首尾
  163. //状态标志/纬度/经度/速度
  164. uint8_t temp_Message[temp_length];
  165. for (int i = 0; i < length; i++) {
  166. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  167. }
  168. for(int i =0;i<temp_length;i++)
  169. {
  170. outMessage[i]=temp_Message[i];
  171. }
  172. }
  173. else if(MessageID==MessageID_Retrans)// 补传0704+5
  174. {
  175. temp_length = length;//不加首尾
  176. uint8_t temp_Message[temp_length];
  177. temp_Message[index++] = 0x00;
  178. temp_Message[index++] = 0x01;
  179. temp_Message[index++] = 0x00;
  180. temp_Message[index++] = 0x00;
  181. temp_Message[index++] = 0x22;
  182. for (int i = 0; i < length; i++) {
  183. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  184. }
  185. for(int i =0;i<temp_length;i++)
  186. {
  187. outMessage[i]=temp_Message[i];
  188. }
  189. }
  190. else//加首尾
  191. {
  192. temp_length = length+3;//加首尾 25/23/crc
  193. uint8_t temp_Message[temp_length];
  194. temp_Message[index++]=temp_length;
  195. temp_Message[index++]=0x2A;
  196. for (int i = 0; i < length; i++) {
  197. temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中
  198. }
  199. temp_Message[index++]=0x23;
  200. temp_Message[index++]=xorBytes(&temp_Message[1],temp_length-1);
  201. temp_length = index;
  202. for(int i =0;i<temp_length;i++)
  203. {
  204. outMessage[i]=temp_Message[i];
  205. }
  206. }
  207. return temp_length;
  208. }
  209. /*需要传入消息体长度sizeof(Message)/sizeof(Message[0]);*/
  210. void tt808FsFunc(uint8_t* Message, uint8_t length, uint16_t MessageID)
  211. {
  212. HAL_Delay(200);
  213. Messagetemp_length = Message_bodydata(Message, length, MessageHandleData, MessageID);
  214. MessageHandle(MessageID);
  215. }
  216. void printBytes(uint8_t *data, size_t length) {
  217. for (size_t i = 0; i < length; i++) {
  218. printf("%02x ", data[i]);
  219. }
  220. printf("\n");
  221. }
  222. // 函数用于提取第7到第8个逗号之间的字符串
  223. void extractString(char* receiveStr, char* result, uint8_t start, uint16_t end)
  224. {
  225. int comma_count = 0;
  226. int start_index = -1;
  227. int end_index = -1;
  228. int length = strlen(receiveStr);
  229. // 遍历字符串,统计逗号数量和记录起始和结束索引
  230. for (int i = 0; i < length; i++)
  231. {
  232. if (receiveStr[i] == ',')
  233. {
  234. comma_count++;
  235. if (comma_count == start)
  236. {
  237. start_index = i + 1; // 第start个逗号之后的字符
  238. }
  239. else if (comma_count == end)
  240. {
  241. if(start==1 && end==2)//*纬度去尾符号
  242. {
  243. end_index = i-1; // 第end个逗号之前的字符
  244. }
  245. else if(start==2 && end==3)//*经度去尾符号
  246. {
  247. end_index = i-1; // 第end个逗号之前的字符
  248. }
  249. else
  250. {
  251. end_index = i; // 第end个逗号之前的字符
  252. }
  253. break;
  254. }
  255. }
  256. }
  257. // 提取字符串并存储在result数组中
  258. if (start_index != -1 && end_index != -1 && start_index < end_index)
  259. {
  260. strncpy(result, receiveStr + start_index, end_index - start_index);
  261. result[end_index - start_index] = '\0'; // 添加字符串结束符
  262. }
  263. }
  264. void extractStringPoint(char* receiveStr, char* result, uint8_t start, uint16_t end)
  265. {
  266. int comma_count = 0;
  267. int start_index = -1;
  268. int end_index = -1;
  269. int length = strlen(receiveStr);
  270. // 遍历字符串,统计逗号数量和记录起始和结束索引
  271. for (int i = 0; i < length; i++)
  272. {
  273. if(start==0 && end==1)// = -> .
  274. {
  275. start_index = start; // 第start个逗号之后的字符
  276. if (receiveStr[i] == '.')
  277. {
  278. comma_count++;
  279. }
  280. else if (comma_count == end)
  281. {
  282. end_index = i-1; // 第end个逗号之前的字符
  283. break;
  284. }
  285. }
  286. else if(start==3 && end==4)
  287. {
  288. if (receiveStr[i] == '.')
  289. {
  290. comma_count++;
  291. if (comma_count == start)
  292. {
  293. start_index = i + 1; // 第start个逗号之后的字符
  294. }
  295. }
  296. else if(receiveStr[i] == ':')
  297. {
  298. end_index = i;
  299. }
  300. }
  301. else if(start==4 && end==5)
  302. {
  303. if (receiveStr[i] == ':')
  304. {
  305. comma_count=4;
  306. if (comma_count == start)
  307. {
  308. start_index = i+1; // 第start个逗号之后的字符
  309. }
  310. end_index = start_index+4;
  311. break;
  312. }
  313. }
  314. else
  315. {
  316. if (receiveStr[i] == '.')
  317. {
  318. comma_count++;
  319. if (comma_count == start)
  320. {
  321. start_index = i + 1; // 第start个逗号之后的字符
  322. }
  323. else if (comma_count == end)
  324. {
  325. end_index = i; // 第end个逗号之前的字符
  326. break;
  327. }
  328. }
  329. }
  330. }
  331. // 提取字符串并存储在result数组中
  332. if (start_index != -1 && end_index != -1 && start_index < end_index)
  333. {
  334. strncpy(result, receiveStr + start_index, end_index - start_index);
  335. result[end_index - start_index] = '\0'; // 添加字符串结束符
  336. }
  337. }
  338. double convertToDouble(char* str) {
  339. if(str == csatellitesNum)
  340. {
  341. uint8_t result;
  342. sscanf(str, "%hhu", &result);
  343. return result;
  344. }
  345. else
  346. {
  347. double result;
  348. sscanf(str, "%lf", &result);
  349. return result;
  350. }
  351. }
  352. char hex2asc(const char *hex)
  353. {
  354. char asc = 0;
  355. char a = (hex[0]>='A' && hex[0]<='F')?(hex[0]^32):hex[0];
  356. char b = (hex[1]>='A' && hex[1]<='F')?(hex[1]^32):hex[1];
  357. if ((a>='0' && a<='9' || a>='a' && a<='f') && (b>='0' && b<='9' || b>='a' && b<='f')){
  358. asc = ((('a'<=a && a<='f')?(a-'a')+10:a-'0')<<4)|(('a'<=b && b<='f')?(b-'a')+10:b-'0');
  359. }
  360. return asc;
  361. }
  362. uint8_t testHex2Asc(const char *hex) {
  363. char asc = hex2asc(hex);
  364. printf("Hex: %s\tASC: %d\n", hex, asc);
  365. return asc;
  366. }
  367. char *bin2hex(char *hex, const unsigned char *bin, int size)
  368. {
  369. size_t i;
  370. for(i=0; i<size; i++)
  371. {
  372. sprintf(hex+i*2, "%02x", bin[i]);
  373. }
  374. hex[i+i] = 0;
  375. return hex;
  376. }
  377. int hex2bin(unsigned char *bin, const char *hex)
  378. {
  379. const char *h = hex;
  380. unsigned char *b = bin;
  381. int rlt = 0;
  382. while(*h){
  383. *b++ = hex2asc(h);
  384. h = h + 2;
  385. rlt++;
  386. }
  387. return rlt;//长度
  388. }
  389. // 将单个十六进制数转换为 BCD 码
  390. uint8_t hexToBcd(uint8_t hex) {
  391. return ((hex / 10) << 4) | (hex % 10);
  392. }
  393. // 将整数数组中的所有十六进制数转换为 BCD 码
  394. void hexArrayToBcd(uint8_t* array, size_t length, uint8_t* out) {//时间用
  395. array[3]+=8;
  396. for (size_t i = 0; i < length; i++) {
  397. out[i] = hexToBcd(array[i]);
  398. }
  399. }
  400. void hexToBcd_arr(uint8_t* array, size_t length, uint8_t* out) {//通用
  401. for (size_t i = 0; i < length; i++) {
  402. out[i] = hexToBcd(array[i]);
  403. }
  404. }
  405. char input_strings[20]={0};
  406. char reIp0[10];
  407. char reIp1[10];
  408. char reIp2[10];
  409. char reIp3[10];
  410. char reIpP[10];
  411. Ipaddr ipaddr;
  412. MacSet macset;
  413. MacSet *macsetptr = &macset;
  414. char Newip[30];
  415. extern bool wdiFlag;
  416. //*485测试命令
  417. void Test485Re(void)
  418. {
  419. //*test485 模拟
  420. if(strncmp((char*)U1_485recvBuff, "+MIPURC: \"disconn\",1,1", strlen("+MIPURC: \"disconn\",1,1"))==0)//"disconn" 平台断连
  421. {
  422. Offline=true;
  423. uploaweork=false;
  424. printf("@U1与平台断开连接\r\n");
  425. memset(U1_485recvBuff,0,BUFFER_SIZE);
  426. }
  427. else if(strncmp((char*)U1_485recvBuff, "+MIPOPEN: 1,0", strlen("+MIPOPEN: 1,0"))==0)//连接平台
  428. {
  429. Offline=false;
  430. Online = true;
  431. uploaweork=true;
  432. printf("@U1设备已上线!!\r\n");
  433. refresh=true;
  434. memset(U1_485recvBuff,0,BUFFER_SIZE);
  435. }
  436. else if(strncmp((char*)&U1_485recvBuff, "DTU+FLASH=deleteloc", strlen("DTU+FLASH=deleteloc"))==0)//连接平台
  437. {
  438. dblocptr->headinfo.loc_pushIndex=0;
  439. dblocptr->headinfo.loc_pullIndex=0;
  440. Flash_WriteBytes((uint16_t*)&(dblocptr->headinfo),LocDbHeadStart,4);//更新刻度
  441. memset(U1_485recvBuff,0,BUFFER_SIZE);
  442. printf("更新刻度成功\r\n");
  443. }
  444. else if(strncmp((char*)U1_485recvBuff, "DTU+JT808_IMEI=", strlen("DTU+JT808_IMEI="))==0)//修改IME号
  445. {
  446. memcpy(DTUurc1,U1_485recvBuff+15,12);
  447. const char *hex = (const char *)DTUurc1; // 将 uint8_t * 转换为 const char *
  448. int size = strlen(hex) / 2; // 计算二进制数据的长度
  449. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  450. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  451. memcpy(DTUurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  452. free(bin); // 释放内存
  453. //memset(DTUurc1,0,100);
  454. memset(U1_485recvBuff,0,BUFFER_SIZE);
  455. reIME = true;
  456. HAL_Delay(500);
  457. }
  458. else if(strncmp((char*)U1_485recvBuff, "DTU+JT808_SERVER=", strlen("DTU+JT808_SERVER="))==0)
  459. {
  460. memcpy(input_strings,U1_485recvBuff+17,20);
  461. extractStringPoint(input_strings,reIp0,0,1);
  462. extractStringPoint(input_strings,reIp1,1,2);
  463. extractStringPoint(input_strings,reIp2,2,3);
  464. extractStringPoint(input_strings,reIp3,3,4);
  465. extractStringPoint(input_strings,reIpP,4,5);
  466. ipaddr.IP0 = atoi(reIp0);
  467. ipaddr.IP1 = atoi(reIp1);
  468. ipaddr.IP2 = atoi(reIp2);
  469. ipaddr.IP3 = atoi(reIp3);
  470. ipaddr.IPp = atoi(reIpP);
  471. //printf("%d.%d.%d.%d:%d",ipaddr.IP0,ipaddr.IP1,ipaddr.IP2,ipaddr.IP3,ipaddr.IPp);
  472. HAL_StatusTypeDef writeSta = Flash_WriteBytes((uint16_t*)&(ipaddr.IP0),ReIPAddr,5);
  473. if(writeSta == HAL_OK)
  474. {
  475. char *ptr = Newip;
  476. ptr += sprintf(ptr, "%d.", ipaddr.IP0);
  477. ptr += sprintf(ptr, "%d.", ipaddr.IP1);
  478. ptr += sprintf(ptr, "%d.", ipaddr.IP2);
  479. ptr += sprintf(ptr, "%d", ipaddr.IP3);
  480. printf("更换IP成功\r\n");
  481. }
  482. //ptr += sprintf(ptr, ":%d", ipaddr.IPp);
  483. //printf("%s\r\n",Newip);
  484. memset(U1_485recvBuff,0,BUFFER_SIZE);
  485. HAL_Delay(500);
  486. printf("即将重启\r\n");
  487. wdiFlag = true;
  488. }
  489. else if(strncmp((char*)U1_485recvBuff, "DTU+VER=?", strlen("DTU+VER=?"))==0)
  490. {
  491. printf("%s\r\n",version);
  492. memset(U1_485recvBuff,0,BUFFER_SIZE);
  493. }
  494. REPdaMode();//PDA 485 MQTT
  495. }
  496. void AtPRocess(char *value, char *conn,char *connected,char *disconn) {
  497. sprintf(conn, "AT+CONN=%s\r\n", value);
  498. sprintf(connected, "+CONNECTED:0,%s", value);
  499. sprintf(disconn, "+DISCONN:0,%s", value);
  500. printf("%s\r\n",conn);
  501. printf("%s\r\n",connected);
  502. printf("%s\r\n",disconn);
  503. }
  504. void REPdaMode(void)
  505. {
  506. //*PDA需求 485写入MAC号
  507. if(strncmp((char*)U1_485recvBuff, "DTU+ADDMAC", strlen("DTU+ADDMAC"))==0)
  508. {
  509. memcpy(macset.MACtemp,U1_485recvBuff+11,12);
  510. const char *hex = (const char *)macset.MACtemp; // 将 uint8_t * 转换为 const char *
  511. int size = strlen(hex) / 2; // 计算二进制数据的长度
  512. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  513. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  514. memcpy(macset.mactemp, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  515. free(bin); // 释放内存
  516. macset.mac[0] = macset.mactemp[0]<<8|macset.mactemp[1];
  517. macset.mac[1] = macset.mactemp[2]<<8|macset.mactemp[3];
  518. macset.mac[2] = macset.mactemp[4]<<8|macset.mactemp[5];
  519. HAL_StatusTypeDef WriteSta;
  520. WriteSta= Flash_WriteBytes(macsetptr->mac,MacDbBodyAddr + (macset.macCount*6),3);
  521. macset.macCount += 1;
  522. if(WriteSta == HAL_OK)
  523. {
  524. printf("更新MAC存储完成\r\n");
  525. }
  526. WriteSta = Flash_WriteBytes(&macsetptr->macCount,MacDbHeadAddr,1);
  527. if(WriteSta == HAL_OK)
  528. {
  529. printf("更新存储刻度完成\r\n");
  530. }
  531. memset(U1_485recvBuff,0,BUFFER_SIZE);
  532. }
  533. }
  534. void ReBTcall(void)
  535. {
  536. if(strncmp((char*)autoRe, "+CONNECTED:0,", strlen("+CONNECTED:0,"))==0)
  537. {
  538. TIPS_MACRO(ConnOK);//连接成功
  539. printf("设备已连接\r\n");
  540. memset(autoRe,0,BUFFER_SIZE);
  541. }
  542. }
  543. //*工作命令
  544. void ReMIPURC(void)//DTU
  545. {
  546. //*设备上线,发送成功
  547. if(strncmp((char*)U4_4GrecvBuff, "+MIPOPEN: 1,0", strlen("+MIPOPEN: 1,0"))==0)//连接平台
  548. {
  549. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  550. }
  551. ///*定位信息(MGNSS)
  552. else if(strncmp((char*)U4_4GrecvBuff, "+MGNSSURC: \"state\",1", strlen("+MGNSSURC: \"state\",1"))==0)
  553. {
  554. printf("@MGNSS设置成功\r\n");
  555. memset(U4_4GrecvBuff,0,strlen("+MGNSSURC: \"state\",1"));
  556. }
  557. else if(strncmp((char*)U4_4GrecvBuff, "+MGNSSURC: \"state\",0", strlen("+MGNSSURC: \"state\",0"))==0)//断开平台
  558. {
  559. printf("@主动取消GNSS\r\n");
  560. memset(U4_4GrecvBuff,0,strlen("+MGNSSURC: \"state\",0"));
  561. }
  562. ///*定位信息(基站)
  563. else if(((strncmp((char*)U4_4GrecvBuff, "+MGNSSLOC", strlen("+MGNSSLOC"))==0) || (strncmp((char*)U4_4GrecvBuff, "OK", strlen("OK"))==0)) && MgnssFlag) //监测平台回复+MGNSSLOC OK4f4b
  564. {
  565. memcpy(MGNSSLOC,&U4_4GrecvBuff[11],75);
  566. GnssFlag=true;
  567. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  568. }
  569. ///*掉线
  570. #if open4G ==1
  571. else if(strncmp((char*)U4_4GrecvBuff, "+MIPURC: \"disconn\",1,1", strlen("+MIPURC: \"disconn\",1,1"))==0)//"disconn" 平台断连
  572. {
  573. Offline=true;
  574. printf("@未发送成功,平台断开连接\r\n");
  575. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  576. uploaweork = false;
  577. reconn();//重连
  578. }
  579. 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
  580. {
  581. //TODO
  582. printf("@SIM卡缺失\r\n");
  583. Offline=true;
  584. signal = false;
  585. menu.current = 0xBB;
  586. tipsflag = NoSIM;
  587. refresh=true;
  588. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  589. uploaweork = false;
  590. reconn();//重连
  591. }
  592. else if((strncmp((char*)U4_4GrecvBuff, "+MIPCALL: 1,0", strlen("+MIPCALL: 1,0"))==0))//意外断开平台连接!!!!//TCP未知错误
  593. {
  594. //TODO
  595. Offline=true;
  596. signal = false;
  597. printf("@与平台断开连接\r\n");
  598. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  599. uploaweork = false;
  600. reconn();//重连
  601. }
  602. else if( (strncmp((char*)U4_4GrecvBuff, "+CME ERROR: 550", strlen("+CME ERROR: 550"))==0))
  603. {
  604. Offline=true;
  605. signal = false;
  606. printf("@未发送成功->TCP/IP未知错误\r\n");
  607. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  608. uploaweork = false;
  609. reconn();//重连
  610. }
  611. else if((strncmp((char*)U4_4GrecvBuff, "+MIPOPEN: 1,571", strlen("+MIPOPEN: 1,571"))==0))//PDP激活失败
  612. {
  613. //TODO
  614. Offline=true;
  615. printf("@未发送成功,PDP激活失败\r\n");
  616. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  617. uploaweork = false;
  618. reconn();//重连
  619. }
  620. #endif
  621. else if(U4_4GrecvBuff[1]== 0x43 && U4_4GrecvBuff[2]== 0x53 && U4_4GrecvBuff[3]== 0x51)//CSQ 信号
  622. {
  623. memcpy(CSQ,U4_4GrecvBuff,U4_4GrecvLength);
  624. dblocptr->location.CSQ = (uint8_t)(CSQ[6]&0xf*10)+(CSQ[7]&0xf);
  625. memset(CSQ,0,12);
  626. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  627. }
  628. else if(strncmp((char*)U4_4GrecvBuff, "+MIPURC", strlen("+MIPURC"))==0) //监测平台回复+MIPURC:
  629. {
  630. uint8_t high_byte = (U4_4GrecvBuff[18]&0x0f) *10;
  631. uint8_t low_byte = U4_4GrecvBuff[19]&0x0f;
  632. hccd = high_byte + low_byte;
  633. memcpy(MIPurc1,&U4_4GrecvBuff[21],hccd*2);
  634. const char *hex = (const char *)MIPurc1; // 将 uint8_t * 转换为 const char *
  635. int size = strlen(hex) / 2; // 计算二进制数据的长度
  636. unsigned char *bin = (unsigned char *)malloc(size); // 分配足够的内存用于存储二进制数据
  637. rlt = hex2bin(bin, hex); // 调用hex2bin函数进行转换
  638. memcpy(MIPurc, bin, rlt); // 将 bin 中的 rlt 个字节复制到 MIPurc1 中 接收到的数据放入MIPurc1中
  639. free(bin); // 释放内存
  640. memset(U4_4GrecvBuff,0,BUFFER_SIZE);
  641. memset(MIPurc1,0,100);
  642. }
  643. }
  644. void MIPURCHandle(void)
  645. {
  646. #if open4G == 1
  647. if(MIPurc[0]==0x7E)
  648. {
  649. uint16_t PTXXID = MIPurc[1]<<8 | MIPurc[2]; //消息ID
  650. uint16_t xxtcLen = MIPurc[3]<<8 | MIPurc[4]; //数据长度
  651. uint16_t xxRE = MIPurc[15]<<8 | MIPurc[16]; //回复ID
  652. uint8_t YESorNO = MIPurc[17]; //00 success ; 01 fail
  653. if(PTXXID==XXID_pttyyd) //收到平台通用应答0x8001
  654. {
  655. if(xxRE == MessageID_authentication)//回复鉴权ID
  656. {
  657. if(YESorNO==YES)
  658. {
  659. printf("@鉴权应答yes\r\n");
  660. authFlag = true;
  661. Offline=false; //待判定假消息
  662. Online = true; //待判定假消息
  663. uploaweork=true;
  664. }
  665. else if(YESorNO==NO)
  666. {
  667. printf("@鉴权应答no\r\n");
  668. }
  669. memset(MIPurc,0,rlt);//rlt下发长度
  670. }
  671. else if(xxRE == MessageID_heartbeat)//回复鉴权ID
  672. {
  673. if(YESorNO==YES)
  674. {
  675. printf("@心跳应答yes\r\n");
  676. printf("@TCP连接成功!!\r\n");
  677. }
  678. else if(YESorNO==NO)
  679. {
  680. printf("@心跳应答no\r\n");
  681. }
  682. memset(MIPurc,0,rlt);//rlt下发长度
  683. }
  684. else if(xxRE == MessageID_location)//回复鉴权ID
  685. {
  686. if(YESorNO==YES)
  687. {
  688. printf("@地址应答yes\r\n");
  689. }
  690. else if(YESorNO==NO)
  691. {
  692. printf("@地址应答no\r\n");
  693. }
  694. memset(MIPurc,0,rlt);//rlt下发长度
  695. }
  696. }
  697. else if(PTXXID == XXID_ptxxtc) //收到平台消息透传 8900
  698. {
  699. authFlag = true;
  700. Offline=false; //待判定假消息
  701. Online = true; //待判定假消息
  702. uploaweork=true;
  703. uint8_t tcsjLen = MIPurc[13];
  704. memcpy(MIPurcXXTCSJ,&MIPurc[13],xxtcLen);
  705. printf("@得到透传数据,%d\r\n",tcsjLen);
  706. memset(MIPurc,0,rlt);//rlt下发长度
  707. }
  708. }
  709. else if(authFlag)
  710. {
  711. authFlag = false;
  712. HAL_Delay(500);
  713. uint8_t test[] = {0x05}; //test心跳
  714. tt808FsFunc(test,1,MessageID_heartbeat); //test心跳
  715. }
  716. #endif
  717. else if(reIME)
  718. {
  719. reIME=false;
  720. phonenum[0] = DTUurc[0]<<8|DTUurc[1];
  721. phonenum[1] = DTUurc[2]<<8|DTUurc[3];
  722. phonenum[2] = DTUurc[4]<<8|DTUurc[5];
  723. phonenull =false;
  724. Flash_WriteBytes(phonenum,phoneNumADDR,3);//更新ime
  725. //memset(DTUurc,0,100);
  726. printf("IMEI设置完成\r\n");
  727. printf("即将重启\r\n");
  728. wdiFlag = true;
  729. }
  730. }
  731. time_t timestamp ;
  732. void time_bj(void)
  733. {
  734. struct tm timeinfo;
  735. timeinfo.tm_year = tt808.Rtime[0]+2000-1900;
  736. timeinfo.tm_mon = tt808.Rtime[1]-1;
  737. timeinfo.tm_mday = tt808.Rtime[2];
  738. timeinfo.tm_hour = tt808.Rtime[3]-8;
  739. timeinfo.tm_min = tt808.Rtime[4];
  740. timeinfo.tm_sec = tt808.Rtime[5];
  741. timestamp = mktime(&timeinfo);
  742. dblocptr->location.timeStamp = timestamp;
  743. }
  744. void ReU4Proces(void)
  745. {
  746. if(GnssFlag)
  747. {
  748. GnssFlag=false;
  749. RTCtime();//同步时间
  750. // 调用函数提取字符串
  751. extractString(MGNSSLOC, cSpeed,7 ,8);//采集
  752. dSpeed = convertToDouble(cSpeed); //转换浮点数
  753. dSpeed*=10;
  754. // 调用函数提取字符串
  755. extractString(MGNSSLOC, clatitude,1 ,2);//采集
  756. dlatitude = convertToDouble(clatitude); //转换浮点数
  757. uint32_t latitudetemp=convertGPS(dlatitude);
  758. dblocptr->location.latitudeH = (latitudetemp>>16)&0xFFFF;
  759. dblocptr->location.latitudeH = swapBytes16(dblocptr->location.latitudeH);//转换H字节
  760. dblocptr->location.latitudeL = (latitudetemp)&0xFFFF;
  761. dblocptr->location.latitudeL = swapBytes16(dblocptr->location.latitudeL);//转换L字节
  762. extractString(MGNSSLOC, clongitude,2 ,3);//采集
  763. dlongitude = convertToDouble(clongitude); //转换浮点数
  764. uint32_t longitudetemp=convertGPS(dlongitude);
  765. dblocptr->location.longitudeH = (longitudetemp>>16)&0xFFFF;
  766. dblocptr->location.longitudeH = swapBytes16(dblocptr->location.longitudeH);//转换H字节
  767. dblocptr->location.longitudeL = (longitudetemp)&0xFFFF;
  768. dblocptr->location.longitudeL = swapBytes16(dblocptr->location.longitudeL);//转换L字节
  769. // printf("%d\r\n",dblocptr->location.latitude);
  770. // printf("%d\r\n",dblocptr->location.longitude);
  771. // printf("%08X\r\n",dblocptr->location.latitude);
  772. // printf("%08X\r\n",dblocptr->location.longitude);
  773. extractString(MGNSSLOC, csatellitesNum,10 ,11);//采集卫星个数
  774. dblocptr->location.satellitesNum = convertToDouble(csatellitesNum); //转换浮点数
  775. //printf("%d",tt808.satellitesNum);
  776. dblocptr->location.speed = (uint16_t)dSpeed;
  777. memset(MGNSSLOC,0,100);
  778. }
  779. }
  780. //*离线蓝牙锁