diff --git a/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs b/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs index 03a93e9..f3d4d30 100644 --- a/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs +++ b/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs @@ -36,6 +36,6 @@ namespace JT809.Protocol.Encrypt Config.Key = Config.IA1 * (Config.Key % mkey) + Config.IC1; buffer[idx] ^= (byte)((Config.Key >> 20) & 0xFF); } - } + } } } diff --git a/src/JT809.Protocol/Enums/ErrorCode.cs b/src/JT809.Protocol/Enums/ErrorCode.cs new file mode 100644 index 0000000..667418a --- /dev/null +++ b/src/JT809.Protocol/Enums/ErrorCode.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT809.Protocol.Enums +{ + public enum ErrorCode + { + CRC16CheckInvalid = 1001 + } +} diff --git a/src/JT809.Protocol/Exceptions/JT809Exception.cs b/src/JT809.Protocol/Exceptions/JT809Exception.cs new file mode 100644 index 0000000..96a9d17 --- /dev/null +++ b/src/JT809.Protocol/Exceptions/JT809Exception.cs @@ -0,0 +1,25 @@ +using JT809.Protocol.Enums; +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT809.Protocol.Exceptions +{ + public class JT809Exception:Exception + { + private readonly ErrorCode errorCode; + + public JT809Exception(ErrorCode errorCode) : base(errorCode.ToString()) + { + this.errorCode = errorCode; + } + + public JT809Exception(ErrorCode errorCode, string message) : base(message) + { + this.errorCode = errorCode; + } + + + public ErrorCode ErrorCode => errorCode; + } +} diff --git a/src/JT809.Protocol/ProtocolPacket/Header.cs b/src/JT809.Protocol/ProtocolPacket/Header.cs index 876760b..05fd481 100644 --- a/src/JT809.Protocol/ProtocolPacket/Header.cs +++ b/src/JT809.Protocol/ProtocolPacket/Header.cs @@ -42,12 +42,15 @@ namespace JT809.Protocol.ProtocolPacket /// public uint GNSSCENTERID { get; set; } public Version Version { get; private set; } - public EncryptOpitions EncryptOpitions { get; private set; } + public EncryptOpitions EncryptOpition { get; private set; } /// /// 数据加密的密匙,长度为 4 个字节。 /// public uint EncryptKey { get; private set; } = 0X00; + public Header(byte[] buffer) : base(buffer) { CounterOnRecieveGenerater++; } + + protected override void InitializeProperties(object[] properties, int startIndex) { throw new NotImplementedException(); diff --git a/src/JT809.Protocol/ProtocolPacket/Package.cs b/src/JT809.Protocol/ProtocolPacket/Package.cs index 15b67ca..0d141aa 100644 --- a/src/JT809.Protocol/ProtocolPacket/Package.cs +++ b/src/JT809.Protocol/ProtocolPacket/Package.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Text; +using JT809.Protocol.Enums; +using JT809.Protocol.Exceptions; using JT809.Protocol.ProtocolPacket.Extensions; namespace JT809.Protocol.ProtocolPacket @@ -39,7 +41,20 @@ namespace JT809.Protocol.ProtocolPacket var content01 = this.UnEscape(content00); var crc16 = this.CRC16_CCITT(content01, 0, content01.Length - Crc16ByteLength); CRC16 = BitConverter.ToUInt16(new[] { content01[content01.Length - 1], content01[content01.Length - 2] }, 0); - if (CRC16 != crc16) throw new InvalidDataException("CRC16 check invalid."); + if (CRC16 != crc16) throw new JT809Exception(ErrorCode.CRC16CheckInvalid); + Header = new Header(content01); + var bodyBuffer00 = new byte[content01.Length - Header.HeaderFixedByteLength - Crc16ByteLength]; + Array.Copy(content01, Header.HeaderFixedByteLength, bodyBuffer00, 0, bodyBuffer00.Length); + //Default + byte[] bodyBuffer01 = bodyBuffer00; + switch (Header.EncryptOpition) + { + case EncryptOpitions.None: + break; + case EncryptOpitions.Common: + // bodyBuffer01 = this.EncryptExt(Header.KeyForCommonEncrypt, bodyBuffer01, bodyBuffer01.Length, Config.Value.ServiceGB809Info.MasterLinkInfo.M1, Config.Value.ServiceGB809Info.MasterLinkInfo.IA1, Config.Value.ServiceGB809Info.MasterLinkInfo.IC1); + break; + } } protected override void OnWriteToBuffer(BinaryWriter writer)