Browse Source

1.修改加密配置及接口(对应文档);

2.修改头部版本号为BCD字符串;
3.修改BCD扩展方法;
4.以上待测试
5.增加全局DI注册(待做)
tags/v1.3.0
SmallChi 6 years ago
parent
commit
2b668f1b1f
24 changed files with 267 additions and 44 deletions
  1. +1
    -1
      src/JT809.Protocol.Benchmark/JT809SerializerContext.cs
  2. +27
    -0
      src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj
  3. +27
    -0
      src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs
  4. +29
    -0
      src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json
  5. +52
    -0
      src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs
  6. +15
    -0
      src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj
  7. +23
    -0
      src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs
  8. +3
    -4
      src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs
  9. +16
    -2
      src/JT809.Protocol.Test/JT809HeaderTest.cs
  10. +5
    -7
      src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs
  11. +1
    -1
      src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs
  12. +2
    -2
      src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs
  13. +13
    -1
      src/JT809.Protocol.sln
  14. +2
    -2
      src/JT809.Protocol/IJT809Encrypt.cs
  15. +0
    -1
      src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs
  16. +1
    -1
      src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs
  17. +7
    -7
      src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs
  18. +15
    -5
      src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs
  19. +2
    -2
      src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs
  20. +2
    -2
      src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
  21. +1
    -1
      src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs
  22. +13
    -0
      src/JT809.Protocol/JT809GlobalConfig.cs
  23. +2
    -1
      src/JT809.Protocol/JT809Header.cs
  24. +8
    -4
      src/JT809.Protocol/JT809Header_Version.cs

+ 1
- 1
src/JT809.Protocol.Benchmark/JT809SerializerContext.cs View File

@@ -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,
};


+ 27
- 0
src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\JT809.Protocol.Extensions.DependencyInjection\JT809.Protocol.Extensions.DependencyInjection.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

+ 27
- 0
src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs View File

@@ -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();
}
}
}

+ 29
- 0
src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json View File

@@ -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
}
}
}

+ 52
- 0
src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs View File

@@ -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<JT809Options>(configuration);
ServiceProvider serviceProvider = services.BuildServiceProvider();
JT809Options options = serviceProvider.GetRequiredService<IOptions<JT809Options>>().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<IMsgSNDistributed>();
JT809GlobalConfig.Instance.SetMsgSNDistributed(msgSNDistributedImpl);
}
catch
{
}
return services;
}
}
}

+ 15
- 0
src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JT809" Version="1.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
</ItemGroup>

</Project>

+ 23
- 0
src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs View File

@@ -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; }
/// <summary>
/// 版本号:
/// 默认 1.0.0
/// </summary>
public string Version { get; set; }
/// <summary>
/// 设置跳过校验码
/// 场景:测试的时候,可能需要收到改数据,所以测试的时候有用
/// </summary>
public bool SkipCRCCode { get; set; }
}
}

+ 3
- 4
src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs View File

@@ -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


+ 16
- 2
src/JT809.Protocol.Test/JT809HeaderTest.cs View File

@@ -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()
{


+ 5
- 7
src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs View File

@@ -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);


+ 1
- 1
src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs View File

@@ -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);


+ 2
- 2
src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs View File

@@ -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);


+ 13
- 1
src/JT809.Protocol.sln View File

@@ -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


+ 2
- 2
src/JT809.Protocol/IJT809Encrypt.cs View File

@@ -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);
}
}

+ 0
- 1
src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs View File

@@ -5,6 +5,5 @@
public uint M1 { get; set; }
public uint IA1 { get; set; }
public uint IC1 { get; set; }
public uint Key { get; set; }
}
}

+ 1
- 1
src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs View File

@@ -15,7 +15,7 @@ namespace JT809.Protocol.JT809Configs
/// 编号;长度为 3 个字节来表示,0x01 0x02 0x0F 标识
/// 的版本号是 v1.2.15,以此类推。
/// </summary>
public JT809Header_Version Version { get; set; } = new JT809Header_Version();
public string Version { get; set; } = "010000";
/// <summary>
/// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。
/// </summary>


+ 7
- 7
src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs View File

@@ -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;


+ 15
- 5
src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs View File

@@ -19,14 +19,24 @@ namespace JT809.Protocol.JT809Extensions
return bcdSb.ToString();
}

public static int WriteBCDLittle(IMemoryOwner<byte> memoryOwner, int offset, string data, int digit, int len)
public static int WriteBCDLittle(IMemoryOwner<byte> memoryOwner, int offset, string data, int len)
{
ReadOnlySpan<char> 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;
}
}
}

+ 2
- 2
src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs View File

@@ -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;


+ 2
- 2
src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs View File

@@ -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;
}
}


+ 1
- 1
src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs View File

@@ -27,7 +27,7 @@ namespace JT809.Protocol.JT809Formatters.JT809SubMessageBodyFormatters

public int Serialize(IMemoryOwner<byte> 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);


+ 13
- 0
src/JT809.Protocol/JT809GlobalConfig.cs View File

@@ -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
/// </summary>
public bool SkipCRCCode { get; private set; }
/// <summary>
/// 设置加密算法选项值
/// 不同的上下级平台之间,加密的算法是一致的,但是针对 M1, IA1, IC1 的不同。
/// 数据先经过加密而后解密。
/// </summary>
/// <param name="jT809Encrypt"></param>
/// <returns></returns>
public JT809GlobalConfig SetEncryptOptions(JT809EncryptOptions jT809EncryptOptions)
{
instance.Value.Encrypt = new JT809EncryptImpl(jT809EncryptOptions);
return instance.Value;
}
/// <summary>
/// 设置加密算法实现
/// </summary>
/// <param name="jT809Encrypt"></param>


+ 2
- 1
src/JT809.Protocol/JT809Header.cs View File

@@ -38,8 +38,9 @@ namespace JT809.Protocol
/// 协议版本号标识,上下级平台之间采用的标准协议版
/// 编号;长度为 3 个字节来表示,0x01 0x02 0x0F 标识
/// 的版本号是 v1.2.15,以此类推。
/// BCD编码
/// </summary>
public JT809Header_Version Version { get; set; } = new JT809Header_Version();
public string Version { get; set; } = "010000";
/// <summary>
/// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。
/// </summary>


+ 8
- 4
src/JT809.Protocol/JT809Header_Version.cs View File

@@ -4,9 +4,13 @@ using System.Text;

namespace JT809.Protocol
{
/// <summary>
/// 协议版本号标识
/// BCD编码
/// </summary>
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; }
}

/// <summary>


Loading…
Cancel
Save