private static string GetSignedUnsigned64(byte[] data) { var signed64 = BytesSwapper.Swap(BitConverter.ToInt64(data, 0)).ToString(); var unsigned64 = BytesSwapper.Swap(BitConverter.ToUInt64(data, 0)).ToString(); return(signed64 == unsigned64 ? signed64 : string.Format("{0} / {1}", signed64, unsigned64)); }
private static string GetSignedUnsigned32(byte[] data) { var signed32 = BytesSwapper.Swap(BitConverter.ToInt32(data, 0)).ToString(); var unsigned = BytesSwapper.Swap(BitConverter.ToUInt32(data, 0)).ToString(); return(signed32 == unsigned ? signed32 : string.Format("{0} / {1}", signed32, unsigned)); }
private static string GetSignedUnsigned16(byte[] data) { var signed16 = BytesSwapper.Swap(BitConverter.ToInt16(data, 0)).ToString(); var unsigned16 = BytesSwapper.Swap(BitConverter.ToUInt16(data, 0)).ToString(); return(signed16 == unsigned16 ? signed16 : string.Format("{0} / {1}", signed16, unsigned16)); }
public static string GetStringValue(ArraySegment <byte> arraySegment, DataType dataType) { var subArray = arraySegment.Array.Skip(arraySegment.Offset).Take(arraySegment.Count).ToArray(); switch (dataType) { // Avl Data types case DataType.CodecId: case DataType.AvlDataCount: case DataType.Priority: case DataType.Satellites: case DataType.EventIoId: case DataType.IoCount: case DataType.IoCount1B: case DataType.IoId1B: case DataType.IoValue1B: case DataType.IoCount2B: case DataType.IoId2B: case DataType.IoCount4B: case DataType.IoId4B: case DataType.IoCount8B: case DataType.IoId8B: return(subArray[0].ToString()); case DataType.Latitude: case DataType.Longitude: return(BytesSwapper.Swap(BitConverter.ToInt32(subArray, 0)).ToString()); case DataType.IoValue4B: return(GetSignedUnsigned32(subArray)); case DataType.Altitude: case DataType.Angle: case DataType.Speed: return(BytesSwapper.Swap(BitConverter.ToInt16(subArray, 0)).ToString()); case DataType.IoValue2B: return(GetSignedUnsigned16(subArray)); case DataType.IoValue8B: return(GetSignedUnsigned64(subArray)); case DataType.Timestamp: return(AvlEpoch.AddMilliseconds(BytesSwapper.Swap(BitConverter.ToInt64(subArray, 0))).ToString()); // Codec7 types case DataType.TimestampGh: { var priorityAndTimeStamp = BytesSwapper.Swap(BitConverter.ToInt32(subArray, 0)); var timeStamp = (long)(priorityAndTimeStamp & 0x3FFFFFFF); return(GHepoch.AddSeconds(timeStamp).ToString()); } case DataType.PriorityGh: { string priorityAndTimestampBits = string.Join(" ", subArray.Select(x => Convert.ToString(x, 2).PadLeft(8, '0'))); return(((GhAvlDataPriority)Convert.ToInt16(priorityAndTimestampBits.Substring(0, 2))).ToString()); } case DataType.GlobalMask: return(((GlobalMaskCodec7)subArray[0]).ToString()); case DataType.GpsElementMask: return(((GpsElementMaskCodec7)subArray[0]).ToString()); case DataType.LongitudeGh: case DataType.LatitudeGh: { var coordinate = EndianBitConverter.ToSingle(subArray, 0); if (!GpsElement.IsLatValid(coordinate)) { return(0.ToString()); } return(coordinate.ToString()); } case DataType.AngleGh: case DataType.SpeedGh: return(subArray[0].ToString()); case DataType.CellIdAndLocalAreaGh: case DataType.OperatorCodeGh: return(BytesSwapper.Swap(BitConverter.ToInt32(subArray, 0)).ToString()); case DataType.SignalQualityGh: return(subArray[0].ToString()); // Tcp types case DataType.Preamble: case DataType.AvlDataArrayLength: case DataType.Crc: return(GetSignedUnsigned32(subArray)); // Udp types case DataType.Length: case DataType.PacketId: case DataType.ImeiLength: return(GetSignedUnsigned16(subArray)); case DataType.PacketType: case DataType.AvlPacketId: return(subArray[0].ToString()); case DataType.Imei: return(Encoding.UTF8.GetString(subArray)); default: throw new ArgumentOutOfRangeException(); } }
public async Task Run() { using (_client) using (var stream = _client.GetStream()) { Log.Info(DateTime.Now + " Received connection request from " + _client.Client.RemoteEndPoint); var fullPacket = new List <byte>(); int?avlDataLength = null; var bytes = new byte[4096]; var connected = false; int length; // Loop to receive all the data sent by the client. while ((length = await stream.ReadAsync(bytes, 0, bytes.Length)) != 0) { Log.Info(string.Format("{0} - received [{1}]", DateTime.Now, String.Join("", bytes.Take(length).Select(x => x.ToString("X2")).ToArray()))); byte[] response; if (!connected) { // Accept imei response = new byte[] { 01 }; connected = true; await stream.WriteAsync(response, 0, response.Length); Array.Clear(bytes, 0, bytes.Length); Log.Info(string.Format("{0} - responded [{1}]", DateTime.Now, String.Join("", response.Select(x => x.ToString("X2")).ToArray()))); } else { fullPacket.AddRange(bytes.Take(length)); Array.Clear(bytes, 0, bytes.Length); var count = fullPacket.Count; // continue if there is not enough bytes to get avl data array length if (count < 8) { continue; } avlDataLength = avlDataLength ?? BytesSwapper.Swap(BitConverter.ToInt32(fullPacket.GetRange(4, 4).ToArray(), 0)); var packetLength = 8 + avlDataLength + 4; if (count > packetLength) { Log.Error("Too much data received."); throw new ArgumentException("Too much data received."); } // continue if not all data received if (count != packetLength) { continue; } // Decode tcp packet var decodedData = DecodeTcpPacket(fullPacket.ToArray()); response = BitConverter.GetBytes(BytesSwapper.Swap(decodedData.AvlData.DataCount)); await stream.WriteAsync(response, 0, response.Length); avlDataLength = null; fullPacket.Clear(); Log.Info(string.Format("{0} - responded [{1}]", DateTime.Now, String.Join("", response.Select(x => x.ToString("X2")).ToArray()))); } } } }
static void Main() { XmlConfigurator.Configure(); IPAddress ip; if (!IPAddress.TryParse(ConfigurationManager.AppSettings["ipAddress"], out ip)) { Log.Error("Ip is not valid."); throw new ArgumentException("Ip is not valid."); } int port; if (!int.TryParse(ConfigurationManager.AppSettings["port"], out port)) { Log.Error("Port is not valid."); throw new ArgumentException("Port is not valid."); } Task.Run(async() => { using (var udpClient = new UdpClient(new IPEndPoint(ip, port))) { Log.Info("Listening..."); while (true) { //IPEndPoint object will allow us to read datagrams sent from any source. var receivedResults = await udpClient.ReceiveAsync(); byte[] data = receivedResults.Buffer; Log.Info(string.Format("Received connection from: {0}", receivedResults.RemoteEndPoint)); Log.Info(string.Format("{0} - received [{1}]", DateTime.Now, String.Join("", data.Select(x => x.ToString("X2")).ToArray()))); var reader = new ReverseBinaryReader(new MemoryStream(data)); // Decode data var avlData = new DataDecoder(reader).DecodeUdpData(); // Create response var bytes = new List <byte>(); const short packetLength = 2 /* Id */ + 1 /* Type */ + 1 /* Avl packet id */ + 1 /* num of accepted elems */; bytes.AddRange(BitConverter.GetBytes(BytesSwapper.Swap(packetLength))); bytes.AddRange(BitConverter.GetBytes(BytesSwapper.Swap(avlData.PacketId))); bytes.Add(avlData.PacketType); bytes.Add(avlData.AvlPacketId); bytes.Add((byte)avlData.AvlData.DataCount); var response = bytes.ToArray(); Log.Info(string.Format("{0} - received [{1}]", DateTime.Now, String.Join("", bytes.Select(x => x.ToString("X2")).ToArray()))); await udpClient.SendAsync(response, response.Length, receivedResults.RemoteEndPoint); } } }); Console.ReadLine(); }