#include "tt808.h" #include "usart.h" #include "key.h" #include #include #include #include #include "rtc.h" #include "database.h" //*整型 int rlt; uint16_t uploadtime=0; //上传时间 uint16_t hccd = 0;//ascii个数(*2) uint8_t header[12]; //处理头部 uint8_t index_2c = 0; uint8_t MIPurc[100]; //接收平台数据 uint8_t MIPurc1[100]; //接收平台数据 uint8_t MIPurcXXTCSJ[100];//平台下行透传数据 uint8_t speedBegin=0; uint8_t speedEnd=0; uint8_t speed[10]; //处理头部 int pushpullcount = 0; double dSpeed=0; double dlatitude=0; double dlongitude=0; char cSpeed[10]; char clatitude[15]; char clongitude[15]; char csatellitesNum[5]; char MGNSSLOC[100]; //接收定位数据 char CSQ[12]; //接收定位数据 char MGNSSLOC_time[6]; //接收定位数据 uint32_t Stamp =0; uint8_t GNSSdata[40]={0}; //*结构体 TT808 tt808; extern TipsFlag tipsflag; extern Menu_table menu; extern bool refresh; //*布尔 bool authFlag = false; bool GnssFlag = false; bool Offline = false; bool Retrans = false; bool Online = false; /** 时间 纬度(北纬) 经度(东经) 水平精度因子 海拔高度 定位类型(1无,2 2d,3 3d) 运动角度 水平运动速度(KM/h) 水平运动速度(Knots) 日月年 卫星数量 差分定位标识(1单点,2差分) eg.+MGNSSLOC: 015032.301,3014.8605N,12001.9250E,1.8, 17.6, 3, 0.00, 3.6, 1.9, 080424, 12, 1 */ uint8_t Messagetemp_length=0; uint8_t MessageHandleData[100]; //放入封装后数据 uint8_t tcppacket[100]; //消息数据 void MessageHandle(uint16_t MessageID) { uint16_t MessageAttributeData = Messagetemp_length; if(MessageID ==MessageID_Retrans) { MessageAttributeData+=3; } MessageAttributeData |= ( Messagetemp_length | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密) header[0] = ((MessageID>>8)&0xff); header[1] = (MessageID&0xff); //消息ID header[2] = ((MessageAttributeData>>8)&0xff); header[3] = (MessageAttributeData&0xff); //消息属性 header[4] = 0x00; header[5] = 0x00; header[6] = 0x16; header[7] = 0x00; header[8] = 0x00; header[9] = 0x08; //“手机号” header[10] = ((tt808.LSH>>8)&0xff);; header[11] = (tt808.LSH&0xff);; int index = 0; tcppacket[index++] = TT808FLAG; // Set the headflag 0x7E for (int i = 0; i < 12; i++) { tcppacket[index++] = header[i]; } // Copy message body to packet for (size_t i = 0; i < Messagetemp_length; i++) { tcppacket[index++] = MessageHandleData[i]; } tcppacket[index++] = xorBytes(&tcppacket[1],12+Messagetemp_length);; // Set the checksum tcppacket[index++] = TT808FLAG; // Set the flag at the end for (int i = 0; i < index; i++) { printf("%02X ", tcppacket[i]); // 以十六进制格式打印每个元素 } printf("\r\n"); char TCPvalue[100]; char *ptr = TCPvalue; ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",index); for (int i = 0; i < index; i++) { ptr += sprintf(ptr, "%02X", tcppacket[i]); } sprintf(ptr, "\"\r\n");//TCPvalue if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送 { printf("#发送完成\r\n"); } tt808.LSH+=1;//需做写入处理 } uint16_t swapBytes16(uint16_t value) { return (value >> 8) | (value << 8); } uint32_t swapBytes32(uint32_t value) { return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) | ((value >> 8) & 0xFF00) | ((value >> 24) & 0xFF); } /** * @breaf 消息体封装 * Message[] 消息数据; length 消息数据长度; outMessage[] 封装后消息数据; MessageID 消息ID */ uint8_t Message_bodydata(uint8_t *Message, uint8_t length, uint8_t outMessage[],uint16_t MessageID) { int index = 0; uint8_t temp_length = 0;//加首尾 25/23/crc if(MessageID==MessageID_authentication || MessageID==MessageID_heartbeat)//不加头尾(鉴权、心跳) { temp_length = length;//不加首尾 uint8_t temp_Message[temp_length]; for (int i = 0; i < length; i++) { temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中 } temp_length = index; for(int i =0;ilocation.statusFlag = swapBytes32(dblocptr->location.statusFlag); dblocptr->location.latitude = swapBytes32(dblocptr->location.latitude); dblocptr->location.longitude = swapBytes32(dblocptr->location.longitude); dblocptr->location.speed = swapBytes16(dblocptr->location.speed); for (int i = 0; i < length; i++) { temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中 } for(int i =0;itemppull.statusFlag = swapBytes32(dblocptr->temppull.statusFlag); dblocptr->temppull.latitude = swapBytes32(dblocptr->temppull.latitude); dblocptr->temppull.longitude = swapBytes32(dblocptr->temppull.longitude); dblocptr->temppull.speed = swapBytes16(dblocptr->temppull.speed); uint8_t temp_Message[temp_length]; temp_Message[index++] = 0x00; temp_Message[index++] = 0x01; temp_Message[index++] = 0x00; temp_Message[index++] = 0x00; temp_Message[index++] = 0x22; for (int i = 0; i < length; i++) { temp_Message[index++] = Message[i]; // 将消息头拷贝到报文中 } for(int i =0;i='A' && hex[0]<='F')?(hex[0]^32):hex[0]; char b = (hex[1]>='A' && hex[1]<='F')?(hex[1]^32):hex[1]; if ((a>='0' && a<='9' || a>='a' && a<='f') && (b>='0' && b<='9' || b>='a' && b<='f')){ asc = ((('a'<=a && a<='f')?(a-'a')+10:a-'0')<<4)|(('a'<=b && b<='f')?(b-'a')+10:b-'0'); } return asc; } uint8_t testHex2Asc(const char *hex) { char asc = hex2asc(hex); printf("Hex: %s\tASC: %d\n", hex, asc); return asc; } char *bin2hex(char *hex, const unsigned char *bin, int size) { size_t i; for(i=0; ilocation.CSQ = (uint8_t)(CSQ[6]&0xf*10)+(CSQ[7]&0xf); // memset(CSQ,0,12); // memset(U4_4GrecvBuff,0,BUFFER_SIZE); // } else if(U4_4GrecvBuff[1]== 0x43 && U4_4GrecvBuff[2]== 0x53 && U4_4GrecvBuff[3]== 0x51)//CSQ 信号 { memcpy(CSQ,U4_4GrecvBuff,U4_4GrecvLength); dblocptr->location.CSQ = (uint8_t)(CSQ[6]&0xf*10)+(CSQ[7]&0xf); memset(CSQ,0,12); memset(U4_4GrecvBuff,0,BUFFER_SIZE); } } void MIPURCHandle(void) { if(MIPurc[0]==0x7E) { uint16_t PTXXID = MIPurc[1]<<8 | MIPurc[2]; //消息ID uint16_t xxtcLen = MIPurc[3]<<8 | MIPurc[4]; //数据长度 uint16_t xxRE = MIPurc[15]<<8 | MIPurc[16]; //回复ID uint8_t YESorNO = MIPurc[17]; //00 success ; 01 fail if(PTXXID==XXID_pttyyd) //收到平台通用应答0x8001 { if(xxRE == MessageID_authentication)//回复鉴权ID { if(YESorNO==YES) { printf("@鉴权应答yes\r\n"); authFlag = true; } else if(YESorNO==NO) { printf("@鉴权应答no\r\n"); } memset(MIPurc,0,rlt);//rlt下发长度 } else if(xxRE == MessageID_heartbeat)//回复鉴权ID { if(YESorNO==YES) { printf("@心跳应答yes\r\n"); } else if(YESorNO==NO) { printf("@心跳应答no\r\n"); } memset(MIPurc,0,rlt);//rlt下发长度 } else if(xxRE == MessageID_location)//回复鉴权ID { if(YESorNO==YES) { printf("@地址应答yes\r\n"); } else if(YESorNO==NO) { printf("@地址应答no\r\n"); } memset(MIPurc,0,rlt);//rlt下发长度 } } else if(PTXXID == XXID_ptxxtc) //收到平台消息透传 8900 { uint8_t tcsjLen = MIPurc[13]; memcpy(MIPurcXXTCSJ,&MIPurc[13],xxtcLen); printf("@得到透传数据,%d\r\n",tcsjLen); memset(MIPurc,0,rlt);//rlt下发长度 } } else if(authFlag) { authFlag = false; HAL_Delay(500); uint8_t test[] = {0x05}; //test心跳 tt808FsFunc(test,1,MessageID_heartbeat); //test心跳 } } time_t timestamp ; void time_bj(void) { struct tm timeinfo; timeinfo.tm_year = tt808.Rtime[0]+2000-1900; timeinfo.tm_mon = tt808.Rtime[1]-1; timeinfo.tm_mday = tt808.Rtime[2]; timeinfo.tm_hour = tt808.Rtime[3]-8; timeinfo.tm_min = tt808.Rtime[4]; timeinfo.tm_sec = tt808.Rtime[5]; timestamp = mktime(&timeinfo); dblocptr->location.timeStamp = timestamp; } void ReU4Proces(void) { if(GnssFlag) { GnssFlag=false; RTCtime();//同步时间 // 调用函数提取字符串 extractString(MGNSSLOC, cSpeed,7 ,8);//采集 dSpeed = convertToDouble(cSpeed); //转换浮点数 dSpeed*=10; // 调用函数提取字符串 extractString(MGNSSLOC, clatitude,1 ,2);//采集 dlatitude = convertToDouble(clatitude); //转换浮点数 dblocptr->location.latitude = convertGPS(dlatitude); extractString(MGNSSLOC, clongitude,2 ,3);//采集 dlongitude = convertToDouble(clongitude); //转换浮点数 dblocptr->location.longitude = convertGPS(dlongitude); // printf("%d\r\n",dblocptr->location.latitude); // printf("%d\r\n",dblocptr->location.longitude); // printf("%08X\r\n",dblocptr->location.latitude); // printf("%08X\r\n",dblocptr->location.longitude); extractString(MGNSSLOC, csatellitesNum,10 ,11);//采集卫星个数 dblocptr->location.satellitesNum = convertToDouble(csatellitesNum); //转换浮点数 //printf("%d",tt808.satellitesNum); dblocptr->location.speed = (uint16_t)dSpeed; if(dSpeed !=0 && dlongitude!=0 && dlatitude!=0) //有数据进行更新! { signal = true; if(menu.current == 0xBB && tipsflag!= ConnOK) { menu.current = 0; refresh=true; } else if(tipsflag == Back && signal) { menu.current = 0; refresh=true; } } else { if(signal && sys_mode[0]!=0xFFFF) { signal = false; menu.current = 0xBB; tipsflag = Nosignal; refresh=true; } } memset(MGNSSLOC,0,100); } } int countregular = 0; void Regular_reporting_Loc(void) { countregular+=1; if(countregular==3 && !Offline )//在线状态10s更新状态,30s上传 (各种状态正常在线,直接上传信息) { countregular=0; tt808FsFunc((uint8_t*)&(dblocptr->location.alarmFlag), 34, MessageID_location); } } void pushpullWork(void) { pushpullcount++; if(Offline && pushpullcount==10000)//掉线状态10s入库定位记录 { RTCtime();//写入做参考 printf("@@@设备掉线,执行入库操作!!%d\r\n",dblocptr->temppush.loc_pushIndex); Database_Push(0); //*每10s重连平台 char TCPconnvalue[100]; TcpConn(TCPconnvalue, 1, JLPTIP, JLPTPORT,0); //cid=1 tcpConnmode=0 普通模式 if(sendCmd_4G(TCPconnvalue, "+MIPOPEN: 1,0", 1, 1)) //TCP连接 0普通模式 2缓存模式 1透传模式 { printf("@@@TCP连接完成\r\n"); } else { printf("@@@重连失败!\r\n"); } } else if(Retrans && MiddleObj)//重传 7e0704002701820200777700060001000022000000000000000301cd8b7007278acd00000000000023051918174830011e310103b77e { tt808FsFunc((uint8_t*)&(dblocptr->temppull.alarmFlag), 39, MessageID_Retrans); printf("出库%d操作完成,当前索引%d\r\n",dblocptr->headinfo.loc_pullIndex-1,dblocptr->headinfo.loc_pullIndex); MiddleObj = false; } else if(Online && !MiddleObj && pushpullcount==1000) //上线出库 { pushpullcount=0; if(dblocptr->headinfo.loc_pushIndex==0) { printf("@@@更新上线标识,库内无数据\r\n"); Online = false; } else { RTCtime();//写入做参考 printf("@@@库内有数据,执行操作\r\n"); Database_Pull(0); MiddleObj=true; } } else if(pushpullcount>10001) { pushpullcount=0; } }