From 2b668f1b1f2e4fc6317ddcb343641616bcbbe94f Mon Sep 17 00:00:00 2001
From: SmallChi <564952747@qq.com>
Date: Fri, 12 Oct 2018 20:26:21 +0800
Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E5=8A=A0=E5=AF=86=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE=E5=8F=8A=E6=8E=A5=E5=8F=A3=EF=BC=88=E5=AF=B9=E5=BA=94?=
=?UTF-8?q?=E6=96=87=E6=A1=A3=EF=BC=89;=202.=E4=BF=AE=E6=94=B9=E5=A4=B4?=
=?UTF-8?q?=E9=83=A8=E7=89=88=E6=9C=AC=E5=8F=B7=E4=B8=BABCD=E5=AD=97?=
=?UTF-8?q?=E7=AC=A6=E4=B8=B2;=203.=E4=BF=AE=E6=94=B9BCD=E6=89=A9=E5=B1=95?=
=?UTF-8?q?=E6=96=B9=E6=B3=95;=204.=E4=BB=A5=E4=B8=8A=E5=BE=85=E6=B5=8B?=
=?UTF-8?q?=E8=AF=95=205.=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80DI=E6=B3=A8?=
=?UTF-8?q?=E5=86=8C=EF=BC=88=E5=BE=85=E5=81=9A=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../JT809SerializerContext.cs | 2 +-
...Extensions.DependencyInjection.Test.csproj | 27 ++++++++++
.../Program.cs | 27 ++++++++++
.../appsettings.json | 29 +++++++++++
.../DependencyInjectionExtensions.cs | 52 +++++++++++++++++++
...ocol.Extensions.DependencyInjection.csproj | 15 ++++++
.../Options/JT809Options.cs | 23 ++++++++
.../JT809Encrypt/JT809EncryptTest.cs | 7 ++-
src/JT809.Protocol.Test/JT809HeaderTest.cs | 18 ++++++-
.../JT809_0x1001EncryptPackageTest.cs | 12 ++---
.../JT809Packages/JT809_0x1001PackageTest.cs | 2 +-
.../JT809_0x9400_0x9401PackageTest.cs | 4 +-
src/JT809.Protocol.sln | 14 ++++-
src/JT809.Protocol/IJT809Encrypt.cs | 4 +-
.../JT809Configs/JT809EncryptOptions.cs | 1 -
.../JT809Configs/JT809HeaderOptions.cs | 2 +-
.../JT809Encrypt/JT809EncryptImpl.cs | 14 ++---
.../JT809Extensions/JT809BCDExtensions.cs | 20 +++++--
.../JT809Formatters/JT809HeaderFormatter.cs | 4 +-
.../JT809Formatters/JT809PackageFormatter.cs | 4 +-
.../JT809_0x9500_0x9505Formatter.cs | 2 +-
src/JT809.Protocol/JT809GlobalConfig.cs | 13 +++++
src/JT809.Protocol/JT809Header.cs | 3 +-
src/JT809.Protocol/JT809Header_Version.cs | 12 +++--
24 files changed, 267 insertions(+), 44 deletions(-)
create mode 100644 src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj
create mode 100644 src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs
create mode 100644 src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json
create mode 100644 src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs
create mode 100644 src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj
create mode 100644 src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs
diff --git a/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs b/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs
index 37492c8..040d128 100644
--- a/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs
+++ b/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs
@@ -50,7 +50,7 @@ namespace JT809.Protocol.Benchmark
MsgSN = 1666,
EncryptKey = 9999,
EncryptFlag = JT809Header_Encrypt.None,
- Version = new JT809Header_Version(1, 0, 0),
+ Version = "010000",
BusinessType = JT809Enums.JT809BusinessType.从链路报警信息交互消息,
MsgGNSSCENTERID = 20180920,
};
diff --git a/src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj b/src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj
new file mode 100644
index 0000000..87d789f
--- /dev/null
+++ b/src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj
@@ -0,0 +1,27 @@
+
+
+
+ Exe
+ netcoreapp2.1
+ 7.3
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Always
+
+
+
+
diff --git a/src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs b/src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs
new file mode 100644
index 0000000..63b1994
--- /dev/null
+++ b/src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs
@@ -0,0 +1,27 @@
+using JT809.Protocol.Extensions.DependencyInjection.Options;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using System;
+using System.Threading.Tasks;
+
+namespace JT809.Protocol.Extensions.DependencyInjection.Test
+{
+ class Program
+ {
+ static async Task Main(string[] args)
+ {
+ var serverHostBuilder = new HostBuilder()
+ .ConfigureAppConfiguration((hostingContext, config) =>
+ {
+ config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
+ config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
+ })
+ .ConfigureServices((hostContext, services) =>
+ {
+ services.AddJT809Configure(hostContext.Configuration.GetSection("JT809Options"));
+ });
+ await serverHostBuilder.RunConsoleAsync();
+ }
+ }
+}
diff --git a/src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json b/src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json
new file mode 100644
index 0000000..726373d
--- /dev/null
+++ b/src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json
@@ -0,0 +1,29 @@
+{
+ "Logging": {
+ "IncludeScopes": false,
+ "Debug": {
+ "LogLevel": {
+ "Default": "Trace"
+ }
+ },
+ "Console": {
+ "LogLevel": {
+ "Default": "Trace"
+ }
+ }
+ },
+ "JT809Options": {
+ "HeaderOptions": {
+ "MsgGNSSCENTERID": 20181012,
+ "EncryptFlag": "None",
+ "EncryptKey": 9999,
+ "Version": "1.0.0"
+ },
+ "EncryptOptions": {
+ "M1": 0,
+ "IA1": 0,
+ "IC1": 0,
+ "Key": 0
+ }
+ }
+}
diff --git a/src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs b/src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs
new file mode 100644
index 0000000..458b219
--- /dev/null
+++ b/src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs
@@ -0,0 +1,52 @@
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using JT809.Protocol.JT809Configs;
+using Microsoft.Extensions.Configuration;
+using JT809.Protocol.JT809Encrypt;
+using JT809.Protocol.Extensions.DependencyInjection.Options;
+using Microsoft.Extensions.Options;
+
+namespace JT809.Protocol.Extensions.DependencyInjection
+{
+ public static class DependencyInjectionExtensions
+ {
+ public static IServiceCollection AddJT809Configure(this IServiceCollection services, IConfiguration configuration)
+ {
+ services.Configure(configuration);
+ ServiceProvider serviceProvider = services.BuildServiceProvider();
+ JT809Options options = serviceProvider.GetRequiredService>().Value;
+ var versions = options.Version.Split(new string []{ "." }, StringSplitOptions.RemoveEmptyEntries);
+ if (versions.Length == 3)
+ {
+ options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), byte.Parse(versions[1]), byte.Parse(versions[2]));
+ }
+ else if(versions.Length == 2)
+ {
+ options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), byte.Parse(versions[1]),0);
+ }
+ else if (versions.Length == 1)
+ {
+ options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), 0, 0);
+ }
+ JT809GlobalConfig.Instance.SetHeaderOptions(options.HeaderOptions);
+ JT809GlobalConfig.Instance.SetSkipCRCCode(options.SkipCRCCode);
+ if (options.HeaderOptions.EncryptFlag == JT809Header_Encrypt.Common)
+ {
+ JT809GlobalConfig.Instance.SetEncrypt(new JT809EncryptImpl(options.EncryptOptions));
+ }
+ try
+ {
+ var msgSNDistributedImpl = services.BuildServiceProvider().GetRequiredService();
+ JT809GlobalConfig.Instance.SetMsgSNDistributed(msgSNDistributedImpl);
+ }
+ catch
+ {
+
+ }
+ return services;
+ }
+ }
+}
diff --git a/src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj b/src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj
new file mode 100644
index 0000000..cafd451
--- /dev/null
+++ b/src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj
@@ -0,0 +1,15 @@
+
+
+
+ netstandard2.0
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs b/src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs
new file mode 100644
index 0000000..faff3dd
--- /dev/null
+++ b/src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs
@@ -0,0 +1,23 @@
+using JT809.Protocol.JT809Configs;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace JT809.Protocol.Extensions.DependencyInjection.Options
+{
+ public class JT809Options
+ {
+ public JT809EncryptOptions EncryptOptions { get; set; }
+ public JT809HeaderOptions HeaderOptions { get; set; }
+ ///
+ /// 版本号:
+ /// 默认 1.0.0
+ ///
+ public string Version { get; set; }
+ ///
+ /// 设置跳过校验码
+ /// 场景:测试的时候,可能需要收到改数据,所以测试的时候有用
+ ///
+ public bool SkipCRCCode { get; set; }
+ }
+}
diff --git a/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs b/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs
index 767be5d..5151ec5 100644
--- a/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs
+++ b/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs
@@ -14,8 +14,7 @@ namespace JT809.Protocol.Test.JT809Encrypt
{
IA1 = 20000000,
IC1 = 20000000,
- M1 = 30000000,
- Key = 256178,
+ M1 = 30000000
};
[Fact]
@@ -26,7 +25,7 @@ namespace JT809.Protocol.Test.JT809Encrypt
01,02,03,04,05,06,07
};
IJT809Encrypt jT809Encrypt = new JT809EncryptImpl(options);
- var data = jT809Encrypt.Encrypt(bytes).ToHexString();
+ var data = jT809Encrypt.Encrypt(bytes, 256178).ToHexString();
//"D3 4C 70 78 A7 3A 41"
}
@@ -35,7 +34,7 @@ namespace JT809.Protocol.Test.JT809Encrypt
{
byte[] bytes = "D3 4C 70 78 A7 3A 41".ToHexBytes();
IJT809Encrypt jT809Encrypt = new JT809EncryptImpl(options);
- var data = jT809Encrypt.Decrypt(bytes);
+ var data = jT809Encrypt.Decrypt(bytes, 256178);
Assert.Equal(new byte[]
{
01,02,03,04,05,06,07
diff --git a/src/JT809.Protocol.Test/JT809HeaderTest.cs b/src/JT809.Protocol.Test/JT809HeaderTest.cs
index 3c4926e..e96f1c4 100644
--- a/src/JT809.Protocol.Test/JT809HeaderTest.cs
+++ b/src/JT809.Protocol.Test/JT809HeaderTest.cs
@@ -19,7 +19,7 @@ namespace JT809.Protocol.Test
jT809Header.MsgSN = 1024;
jT809Header.BusinessType = JT809Enums.JT809BusinessType.从链路静态信息交换消息;
jT809Header.MsgGNSSCENTERID = 1200;
- jT809Header.Version = new JT809Header_Version();
+ jT809Header.Version = "010000";
jT809Header.EncryptFlag = JT809Header_Encrypt.None;
jT809Header.EncryptKey = 0;
var hex = JT809Serializer.Serialize(jT809Header).ToHexString();
@@ -35,11 +35,25 @@ namespace JT809.Protocol.Test
Assert.Equal((uint)1024, jT809Header.MsgSN);
Assert.Equal(JT809Enums.JT809BusinessType.从链路静态信息交换消息, jT809Header.BusinessType);
Assert.Equal((uint)1200, jT809Header.MsgGNSSCENTERID);
- Assert.Equal(new JT809Header_Version().ToString(), jT809Header.Version.ToString());
+ Assert.Equal("010000", jT809Header.Version);
Assert.Equal(JT809Header_Encrypt.None, jT809Header.EncryptFlag);
Assert.Equal((uint)0, jT809Header.EncryptKey);
}
+ [Fact]
+ public void Test4()
+ {
+ JT809Header jT809Header = new JT809Header();
+ jT809Header.MsgLength = 24;
+ jT809Header.MsgSN = 1024;
+ jT809Header.BusinessType = JT809Enums.JT809BusinessType.从链路静态信息交换消息;
+ jT809Header.MsgGNSSCENTERID = 1200;
+ jT809Header.Version = "0F0A0B";
+ jT809Header.EncryptFlag = JT809Header_Encrypt.None;
+ jT809Header.EncryptKey = 0;
+ var hex = JT809Serializer.Serialize(jT809Header).ToHexString();
+ }
+
[Fact]
public void Test3()
{
diff --git a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs
index 62b2ecc..93faa64 100644
--- a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs
+++ b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs
@@ -17,8 +17,7 @@ namespace JT809.Protocol.Test.JT809Packages
{
IA1 = 20000000,
IC1 = 20000000,
- M1 = 30000000,
- Key = 256178,
+ M1 = 30000000
}));
}
@@ -30,7 +29,7 @@ namespace JT809.Protocol.Test.JT809Packages
{
EncryptFlag= JT809Header_Encrypt.Common,
MsgSN= 133,
- EncryptKey=5819,
+ EncryptKey= 256178,
BusinessType= JT809Enums.JT809BusinessType.主链路登录请求消息,
MsgGNSSCENTERID= 20180920,
};
@@ -47,16 +46,15 @@ namespace JT809.Protocol.Test.JT809Packages
[Fact]
public void Test2()
{
- var bytes = "5B 00 00 00 48 00 00 00 85 10 01 01 33 EF B8 01 00 00 01 00 00 16 BB D3 7D 9C C4 90 0C 77 DC 78 F8 67 65 27 D8 AE 12 24 3C FB 64 CC 2F BA 61 9A EF AD 33 AC CB 32 56 F6 7B FF 19 DF 33 09 78 41 09 86 65 70 3F 2E B5 5D".ToHexBytes();
+ var bytes = "5B000000480000008510010133EFB8010000010003E8B2D37D9CC4900C77DC78F8676527D8AE12243CFB64CC2FBA619AEFAD33ACCB3256F67BFF19DF33097841098665703FE36E5D".ToStr2HexBytes();
JT809Package jT809Package = JT809Serializer.Deserialize(bytes);
Assert.Equal(JT809Header_Encrypt.Common, jT809Package.Header.EncryptFlag);
- Assert.Equal((uint)5819, jT809Package.Header.EncryptKey);
+ Assert.Equal((uint)256178, jT809Package.Header.EncryptKey);
Assert.Equal((uint)72, jT809Package.Header.MsgLength);
Assert.Equal((uint)133, jT809Package.Header.MsgSN);
- Assert.Equal((uint)5819, jT809Package.Header.EncryptKey);
Assert.Equal((uint)20180920, jT809Package.Header.MsgGNSSCENTERID);
Assert.Equal(JT809Enums.JT809BusinessType.主链路登录请求消息, jT809Package.Header.BusinessType);
- Assert.Equal(new JT809Header_Version().ToString(), jT809Package.Header.Version.ToString());
+ Assert.Equal("010000", jT809Package.Header.Version);
JT809_0x1001 jT809_0X1001 = (JT809_0x1001)jT809Package.Bodies;
Assert.Equal((uint)20180920, jT809_0X1001.UserId);
Assert.Equal("20180920", jT809_0X1001.Password);
diff --git a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs
index aa4a1e5..61531fd 100644
--- a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs
+++ b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs
@@ -54,7 +54,7 @@ namespace JT809.Protocol.Test.JT809Packages
Assert.Equal((uint)9999, jT809Package.Header.EncryptKey);
Assert.Equal((uint)20180920, jT809Package.Header.MsgGNSSCENTERID);
Assert.Equal(JT809Enums.JT809BusinessType.主链路登录请求消息, jT809Package.Header.BusinessType);
- Assert.Equal(new JT809Header_Version().ToString(), jT809Package.Header.Version.ToString());
+ Assert.Equal("010000", jT809Package.Header.Version);
JT809_0x1001 jT809_0X1001 = (JT809_0x1001)jT809Package.Bodies;
Assert.Equal((uint)20180920, jT809_0X1001.UserId);
Assert.Equal("20180920", jT809_0X1001.Password);
diff --git a/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs b/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs
index 2affc45..60937d9 100644
--- a/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs
+++ b/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs
@@ -22,7 +22,7 @@ namespace JT809.Protocol.Test.JT809Packages
MsgSN = 1666,
EncryptKey = 9999,
EncryptFlag= JT809Header_Encrypt.None,
- Version = new JT809Header_Version(1, 0, 0),
+ Version = "010000",
BusinessType = JT809Enums.JT809BusinessType.从链路报警信息交互消息,
MsgGNSSCENTERID = 20180920,
};
@@ -64,7 +64,7 @@ namespace JT809.Protocol.Test.JT809Packages
Assert.Equal(JT809Header_Encrypt.None, jT809Package.Header.EncryptFlag);
Assert.Equal((uint)20180920, jT809Package.Header.MsgGNSSCENTERID);
Assert.Equal(JT809Enums.JT809BusinessType.从链路报警信息交互消息, jT809Package.Header.BusinessType);
- Assert.Equal(new JT809Header_Version().ToString(), jT809Package.Header.Version.ToString());
+ Assert.Equal("010000", jT809Package.Header.Version);
JT809_0x9400 jT809_0X400 = (JT809_0x9400)jT809Package.Bodies;
Assert.Equal("粤A12345", jT809_0X400.VehicleNo);
diff --git a/src/JT809.Protocol.sln b/src/JT809.Protocol.sln
index 34f4c48..202e29e 100644
--- a/src/JT809.Protocol.sln
+++ b/src/JT809.Protocol.sln
@@ -7,7 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol", "JT809.Pro
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol.Test", "JT809.Protocol.Test\JT809.Protocol.Test.csproj", "{59D2D876-8D81-4CCE-839A-B153912C0C27}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol.Benchmark", "JT809.Protocol.Benchmark\JT809.Protocol.Benchmark.csproj", "{47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol.Benchmark", "JT809.Protocol.Benchmark\JT809.Protocol.Benchmark.csproj", "{47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol.Extensions.DependencyInjection", "JT809.Protocol.Extensions.DependencyInjection\JT809.Protocol.Extensions.DependencyInjection.csproj", "{974E9013-A19E-4643-8AB4-504D916AF4BD}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol.Extensions.DependencyInjection.Test", "JT809.Protocol.Extensions.DependencyInjection.Test\JT809.Protocol.Extensions.DependencyInjection.Test.csproj", "{F3E0933D-13A9-48AA-958A-7903221D5DFA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +31,14 @@ Global
{47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {974E9013-A19E-4643-8AB4-504D916AF4BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {974E9013-A19E-4643-8AB4-504D916AF4BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {974E9013-A19E-4643-8AB4-504D916AF4BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {974E9013-A19E-4643-8AB4-504D916AF4BD}.Release|Any CPU.Build.0 = Release|Any CPU
+ {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/src/JT809.Protocol/IJT809Encrypt.cs b/src/JT809.Protocol/IJT809Encrypt.cs
index f1d497a..8fc4396 100644
--- a/src/JT809.Protocol/IJT809Encrypt.cs
+++ b/src/JT809.Protocol/IJT809Encrypt.cs
@@ -2,7 +2,7 @@
{
public interface IJT809Encrypt
{
- byte[] Encrypt(byte[] buffer);
- byte[] Decrypt(byte[] buffer);
+ byte[] Encrypt(byte[] buffer, uint privateKey);
+ byte[] Decrypt(byte[] buffer, uint privateKey);
}
}
diff --git a/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs b/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs
index bc0f216..e00bc99 100644
--- a/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs
+++ b/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs
@@ -5,6 +5,5 @@
public uint M1 { get; set; }
public uint IA1 { get; set; }
public uint IC1 { get; set; }
- public uint Key { get; set; }
}
}
diff --git a/src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs b/src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs
index ec8be43..169cbaf 100644
--- a/src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs
+++ b/src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs
@@ -15,7 +15,7 @@ namespace JT809.Protocol.JT809Configs
/// 编号;长度为 3 个字节来表示,0x01 0x02 0x0F 标识
/// 的版本号是 v1.2.15,以此类推。
///
- public JT809Header_Version Version { get; set; } = new JT809Header_Version();
+ public string Version { get; set; } = "010000";
///
/// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。
///
diff --git a/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs b/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs
index 4c1e8e7..79e2e49 100644
--- a/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs
+++ b/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs
@@ -14,17 +14,17 @@ namespace JT809.Protocol.JT809Encrypt
this.jT809EncryptOptions = jT809EncryptOptions;
}
- public byte[] Decrypt(byte[] buffer)
+ public byte[] Decrypt(byte[] buffer, uint privateKey)
{
- return Encrypt(buffer);
+ return Encrypt(buffer, privateKey);
}
- public byte[] Encrypt(byte[] buffer)
+ public byte[] Encrypt(byte[] buffer, uint privateKey)
{
byte[] data = new byte[buffer.Length];
- if (0 == jT809EncryptOptions.Key)
+ if (0 == privateKey)
{
- jT809EncryptOptions.Key = 1;
+ privateKey = 1;
}
uint mkey = jT809EncryptOptions.M1;
if (0 == mkey)
@@ -33,8 +33,8 @@ namespace JT809.Protocol.JT809Encrypt
}
for (int idx = 0; idx < buffer.Length; idx++)
{
- jT809EncryptOptions.Key = jT809EncryptOptions.IA1 * (jT809EncryptOptions.Key % mkey) + jT809EncryptOptions.IC1;
- buffer[idx] ^= (byte)((jT809EncryptOptions.Key >> 20) & 0xFF);
+ privateKey = jT809EncryptOptions.IA1 * (privateKey % mkey) + jT809EncryptOptions.IC1;
+ buffer[idx] ^= (byte)((privateKey >> 20) & 0xFF);
data[idx] = buffer[idx];
}
return data;
diff --git a/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs b/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs
index b616839..dc2a6e6 100644
--- a/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs
+++ b/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs
@@ -19,14 +19,24 @@ namespace JT809.Protocol.JT809Extensions
return bcdSb.ToString();
}
- public static int WriteBCDLittle(IMemoryOwner memoryOwner, int offset, string data, int digit, int len)
+ public static int WriteBCDLittle(IMemoryOwner memoryOwner, int offset, string data, int len)
{
- ReadOnlySpan bcd = data.PadLeft(len, '0').AsSpan();
- for (int i = 0; i < digit; i++)
+ string bcdText = data == null ? "" : data;
+ byte[] bytes = new byte[len];
+ int startIndex = 0;
+ int noOfZero = len * 2 - data.Length;
+ if (noOfZero > 0)
{
- memoryOwner.Memory.Span[offset + i] = Convert.ToByte(bcd.Slice(i * 2, 2).ToString(), 16);
+ bcdText = bcdText.Insert(startIndex, new string('0', noOfZero));
}
- return digit;
+ int byteIndex = 0;
+ while (startIndex < bcdText.Length && byteIndex < len)
+ {
+ memoryOwner.Memory.Span[offset + byteIndex] = Convert.ToByte(bcdText.Substring(startIndex, 2), 16);
+ startIndex += 2;
+ byteIndex++;
+ }
+ return len;
}
}
}
diff --git a/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs b/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs
index 0926234..b9fa8e6 100644
--- a/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs
+++ b/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs
@@ -17,7 +17,7 @@ namespace JT809.Protocol.JT809Formatters
jT809Header.MsgSN= JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset);
jT809Header.BusinessType = (JT809BusinessType)JT809BinaryExtensions.ReadUInt16Little(bytes, ref offset);
jT809Header.MsgGNSSCENTERID = JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset);
- jT809Header.Version = new JT809Header_Version(JT809BinaryExtensions.ReadBytesLittle(bytes, ref offset, JT809Header_Version.FixedByteLength));
+ jT809Header.Version = JT809BinaryExtensions.ReadBCDLittle(bytes, ref offset, 3);
jT809Header.EncryptFlag= (JT809Header_Encrypt)JT809BinaryExtensions.ReadByteLittle(bytes, ref offset);
jT809Header.EncryptKey= JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset);
readSize = offset;
@@ -30,7 +30,7 @@ namespace JT809.Protocol.JT809Formatters
offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.MsgSN);
offset += JT809BinaryExtensions.WriteUInt16Little(memoryOwner, offset, (ushort)value.BusinessType);
offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.MsgGNSSCENTERID);
- offset += JT809BinaryExtensions.WriteBytesLittle(memoryOwner, offset, value.Version.Buffer);
+ offset += JT809BinaryExtensions.WriteBCDLittle(memoryOwner, offset, value.Version,3);
offset += JT809BinaryExtensions.WriteByteLittle(memoryOwner, offset, (byte)value.EncryptFlag);
offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.EncryptKey);
return offset;
diff --git a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
index b89e0e3..83b2dc1 100644
--- a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
+++ b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
@@ -65,7 +65,7 @@ namespace JT809.Protocol.JT809Formatters
jT809Package.Bodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809BodiesTypeAttribute.JT809BodiesType), buffer.Slice(offset, checkIndex - offset), out readSize);
break;
case JT809Header_Encrypt.Common:
- byte[] bodiesData = JT809GlobalConfig.Instance.Encrypt.Decrypt(buffer.Slice(offset, checkIndex - offset).ToArray());
+ byte[] bodiesData = JT809GlobalConfig.Instance.Encrypt.Decrypt(buffer.Slice(offset, checkIndex - offset).ToArray(), jT809Package.Header.EncryptKey);
jT809Package.Bodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809BodiesTypeAttribute.JT809BodiesType), bodiesData, out readSize);
break;
}
@@ -104,7 +104,7 @@ namespace JT809.Protocol.JT809Formatters
case JT809Header_Encrypt.None:
break;
case JT809Header_Encrypt.Common:
- messageBodyData = JT809GlobalConfig.Instance.Encrypt.Encrypt(messageBodyData);
+ messageBodyData = JT809GlobalConfig.Instance.Encrypt.Encrypt(messageBodyData, value.Header.EncryptKey);
break;
}
}
diff --git a/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs b/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs
index 9604eb2..7ffab0c 100644
--- a/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs
+++ b/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs
@@ -27,7 +27,7 @@ namespace JT809.Protocol.JT809Formatters.JT809SubMessageBodyFormatters
public int Serialize(IMemoryOwner memoryOwner, int offset, JT809_0x9500_0x9505 value)
{
- offset += JT809BinaryExtensions.WriteBCDLittle(memoryOwner, offset, value.AuthenticationCode,10,20);
+ offset += JT809BinaryExtensions.WriteBCDLittle(memoryOwner, offset, value.AuthenticationCode,20);
offset += JT809BinaryExtensions.WriteStringLittle(memoryOwner, offset, value.AccessPointName,20);
offset += JT809BinaryExtensions.WriteStringLittle(memoryOwner, offset, value.UserName, 49);
offset += JT809BinaryExtensions.WriteStringLittle(memoryOwner, offset, value.Password, 22);
diff --git a/src/JT809.Protocol/JT809GlobalConfig.cs b/src/JT809.Protocol/JT809GlobalConfig.cs
index d772d0d..1764de6 100644
--- a/src/JT809.Protocol/JT809GlobalConfig.cs
+++ b/src/JT809.Protocol/JT809GlobalConfig.cs
@@ -1,4 +1,5 @@
using JT809.Protocol.JT809Configs;
+using JT809.Protocol.JT809Encrypt;
using JT809.Protocol.JT809Internal;
using System;
using System.Collections.Generic;
@@ -36,6 +37,18 @@ namespace JT809.Protocol
///
public bool SkipCRCCode { get; private set; }
///
+ /// 设置加密算法选项值
+ /// 不同的上下级平台之间,加密的算法是一致的,但是针对 M1, IA1, IC1 的不同。
+ /// 数据先经过加密而后解密。
+ ///
+ ///
+ ///
+ public JT809GlobalConfig SetEncryptOptions(JT809EncryptOptions jT809EncryptOptions)
+ {
+ instance.Value.Encrypt = new JT809EncryptImpl(jT809EncryptOptions);
+ return instance.Value;
+ }
+ ///
/// 设置加密算法实现
///
///
diff --git a/src/JT809.Protocol/JT809Header.cs b/src/JT809.Protocol/JT809Header.cs
index 7defbc8..64e6aa2 100644
--- a/src/JT809.Protocol/JT809Header.cs
+++ b/src/JT809.Protocol/JT809Header.cs
@@ -38,8 +38,9 @@ namespace JT809.Protocol
/// 协议版本号标识,上下级平台之间采用的标准协议版
/// 编号;长度为 3 个字节来表示,0x01 0x02 0x0F 标识
/// 的版本号是 v1.2.15,以此类推。
+ /// BCD编码
///
- public JT809Header_Version Version { get; set; } = new JT809Header_Version();
+ public string Version { get; set; } = "010000";
///
/// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。
///
diff --git a/src/JT809.Protocol/JT809Header_Version.cs b/src/JT809.Protocol/JT809Header_Version.cs
index f7958f5..ef75d3e 100644
--- a/src/JT809.Protocol/JT809Header_Version.cs
+++ b/src/JT809.Protocol/JT809Header_Version.cs
@@ -4,9 +4,13 @@ using System.Text;
namespace JT809.Protocol
{
+ ///
+ /// 协议版本号标识
+ /// BCD编码
+ ///
public class JT809Header_Version
{
- public byte[] Buffer { get; } = new byte[3];
+ public byte[] Buffer { get;} = new byte[3];
private const int MajorIndex = 0;
private const int MinorIndex = 1;
private const int BuildIndex = 2;
@@ -15,19 +19,19 @@ namespace JT809.Protocol
public byte Major
{
get { return Buffer[MajorIndex]; }
- private set { Buffer[MajorIndex] = value; }
+ set { Buffer[MajorIndex] = value; }
}
public byte Minor
{
get { return Buffer[MinorIndex]; }
- private set { Buffer[MinorIndex] = value; }
+ set { Buffer[MinorIndex] = value; }
}
public byte Build
{
get { return Buffer[BuildIndex]; }
- private set { Buffer[BuildIndex] = value; }
+ set { Buffer[BuildIndex] = value; }
}
///