@@ -36,6 +36,6 @@ namespace JT809.Protocol.Encrypt | |||||
Config.Key = Config.IA1 * (Config.Key % mkey) + Config.IC1; | Config.Key = Config.IA1 * (Config.Key % mkey) + Config.IC1; | ||||
buffer[idx] ^= (byte)((Config.Key >> 20) & 0xFF); | buffer[idx] ^= (byte)((Config.Key >> 20) & 0xFF); | ||||
} | } | ||||
} | |||||
} | |||||
} | } | ||||
} | } |
@@ -0,0 +1,11 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace JT809.Protocol.Enums | |||||
{ | |||||
public enum ErrorCode | |||||
{ | |||||
CRC16CheckInvalid = 1001 | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} |
@@ -42,12 +42,15 @@ namespace JT809.Protocol.ProtocolPacket | |||||
/// </summary> | /// </summary> | ||||
public uint GNSSCENTERID { get; set; } | public uint GNSSCENTERID { get; set; } | ||||
public Version Version { get; private set; } | public Version Version { get; private set; } | ||||
public EncryptOpitions EncryptOpitions { get; private set; } | |||||
public EncryptOpitions EncryptOpition { get; private set; } | |||||
/// <summary> | /// <summary> | ||||
/// 数据加密的密匙,长度为 4 个字节。 | /// 数据加密的密匙,长度为 4 个字节。 | ||||
/// </summary> | /// </summary> | ||||
public uint EncryptKey { get; private set; } = 0X00; | public uint EncryptKey { get; private set; } = 0X00; | ||||
public Header(byte[] buffer) : base(buffer) { CounterOnRecieveGenerater++; } | |||||
protected override void InitializeProperties(object[] properties, int startIndex) | protected override void InitializeProperties(object[] properties, int startIndex) | ||||
{ | { | ||||
throw new NotImplementedException(); | throw new NotImplementedException(); | ||||
@@ -2,6 +2,8 @@ | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.IO; | using System.IO; | ||||
using System.Text; | using System.Text; | ||||
using JT809.Protocol.Enums; | |||||
using JT809.Protocol.Exceptions; | |||||
using JT809.Protocol.ProtocolPacket.Extensions; | using JT809.Protocol.ProtocolPacket.Extensions; | ||||
namespace JT809.Protocol.ProtocolPacket | namespace JT809.Protocol.ProtocolPacket | ||||
@@ -39,7 +41,20 @@ namespace JT809.Protocol.ProtocolPacket | |||||
var content01 = this.UnEscape(content00); | var content01 = this.UnEscape(content00); | ||||
var crc16 = this.CRC16_CCITT(content01, 0, content01.Length - Crc16ByteLength); | var crc16 = this.CRC16_CCITT(content01, 0, content01.Length - Crc16ByteLength); | ||||
CRC16 = BitConverter.ToUInt16(new[] { content01[content01.Length - 1], content01[content01.Length - 2] }, 0); | 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) | protected override void OnWriteToBuffer(BinaryWriter writer) | ||||