From 4e8180881aff51219a121591041d5c6061f724c5 Mon Sep 17 00:00:00 2001
From: "SmallChi(Koike)" <564952747@qq.com>
Date: Thu, 2 Apr 2020 18:25:13 +0800
Subject: [PATCH] =?UTF-8?q?1.=E9=9B=86=E6=88=90github=20actions=202.?=
=?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A3=80=E9=AA=8C=E7=A0=81=E8=87=AA=E8=BA=AB?=
=?UTF-8?q?=E9=9C=80=E8=A6=81=E8=BD=AC=E4=B9=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.github/workflows/dotnetcore.yml | 23 ++++++
.travis.yml | 15 ----
README.md | 2 +-
.../JT809.Protocol.Benchmark.csproj | 4 +-
.../JT809.Protocol.Test.csproj | 4 +-
.../MessagePack/JT809MessagePackReaderTest.cs | 34 ++++++++
src/JT809.Protocol/JT809.Protocol.csproj | 6 +-
.../MessagePack/JT809MessagePackReader.cs | 81 +++++++++++++------
8 files changed, 123 insertions(+), 46 deletions(-)
create mode 100644 .github/workflows/dotnetcore.yml
delete mode 100644 .travis.yml
diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml
new file mode 100644
index 0000000..bd50b9c
--- /dev/null
+++ b/.github/workflows/dotnetcore.yml
@@ -0,0 +1,23 @@
+name: .NET Core
+
+on: [push]
+
+jobs:
+ build:
+
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@master
+ - name: Setup .NET Core
+ uses: actions/setup-dotnet@master
+ with:
+ dotnet-version: 3.1.101
+ - name: dotnet info
+ run: dotnet --info
+ - name: dotnet restore
+ run: dotnet restore ./src/JT809.Protocol.sln
+ - name: dotnet build
+ run: dotnet build ./src/JT809.Protocol.Test/JT809.Protocol.Test.csproj
+ - name: dotnet test
+ run: dotnet test ./src/JT809.Protocol.Test/JT809.Protocol.Test.csproj
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 104ea0c..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,15 +0,0 @@
-language: csharp
-solution: JT809.Protocol.sln
-dotnet: 3.1.100
-os: linux
-mono: none
-dist: trusty2
-script:
- - dotnet restore src/JT809.Protocol.sln
- - dotnet build src/JT809.Protocol.Test/JT809.Protocol.Test.csproj
- - dotnet test src/JT809.Protocol.Test/JT809.Protocol.Test.csproj
-after_success:
- - echo successful build!
-branches:
- only:
- - master
diff --git a/README.md b/README.md
index e1976fc..b98fcf0 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# JT809协议
-[](https://github.com/SmallChi/JT809/blob/master/LICENSE)[](https://travis-ci.org/SmallChi/JT809)
+[](https://github.com/SmallChi/JT809/blob/master/LICENSE)
## 前提条件
diff --git a/src/JT809.Protocol.Benchmark/JT809.Protocol.Benchmark.csproj b/src/JT809.Protocol.Benchmark/JT809.Protocol.Benchmark.csproj
index 97e68b0..6aabb38 100644
--- a/src/JT809.Protocol.Benchmark/JT809.Protocol.Benchmark.csproj
+++ b/src/JT809.Protocol.Benchmark/JT809.Protocol.Benchmark.csproj
@@ -7,8 +7,8 @@
-
-
+
+
diff --git a/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj b/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj
index beebe3e..1668952 100644
--- a/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj
+++ b/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj
@@ -11,8 +11,8 @@
-
-
+
+
all
diff --git a/src/JT809.Protocol.Test/MessagePack/JT809MessagePackReaderTest.cs b/src/JT809.Protocol.Test/MessagePack/JT809MessagePackReaderTest.cs
index 990f56b..85101de 100644
--- a/src/JT809.Protocol.Test/MessagePack/JT809MessagePackReaderTest.cs
+++ b/src/JT809.Protocol.Test/MessagePack/JT809MessagePackReaderTest.cs
@@ -26,5 +26,39 @@ namespace JT809.Protocol.Test.MessagePack
jT809MessagePackReader.FullDecode();
Assert.Equal("5B 00 5A 48 5E 5E 00 5D".ToHexBytes(), jT809MessagePackReader.Reader.ToArray());
}
+
+ [Fact]
+ public void DecodeTest1()
+ {
+ //5B
+ //0000006E00000002140000000000010000000000000014020000004E31323334353637383931320001000000005E7B1E28000000005E7B1E28000000005E7B1E28B4A8413132333435000000000000000000000000000131323334353637383931320000007B00000000
+ //00 5A 02 4A
+ //5D
+ var data = "5B0000006E00000002140000000000010000000000000014020000004E31323334353637383931320001000000005E7B1E28000000005E7B1E28000000005E7B1E28B4A8413132333435000000000000000000000000000131323334353637383931320000007B000000005A024A5D".ToHexBytes();
+ JT809MessagePackReader jT809MessagePackReader = new JT809MessagePackReader(data);
+ jT809MessagePackReader.Decode();
+ Assert.True(jT809MessagePackReader.CheckXorCodeVali);
+ Assert.Equal(jT809MessagePackReader.Reader.Length, data.Length-1);
+ }
+
+ [Fact]
+ public void DecodeTest2()
+ {
+ //手动测试
+ var data = "5B0000006E00000002140000000000010000000000000014020000004E31323334353637383931320001000000005E7B1E28000000005E7B1E28000000005E7B1E28B4A8413132333435000000000000000000000000000131323334353637383931320000007B000000005A025A025D".ToHexBytes();
+ JT809MessagePackReader jT809MessagePackReader = new JT809MessagePackReader(data);
+ jT809MessagePackReader.Decode();
+ Assert.Equal(jT809MessagePackReader.Reader.Length, data.Length - 2);
+ }
+
+ [Fact]
+ public void DecodeTest3()
+ {
+ //手动测试
+ var data = "5B0000006E00000002140000000000010000000000000014020000004E31323334353637383931320001000000005E7B1E28000000005E7B1E28000000005E7B1E28B4A8413132333435000000000000000000000000000131323334353637383931320000007B00000000A45A025D".ToHexBytes();
+ JT809MessagePackReader jT809MessagePackReader = new JT809MessagePackReader(data);
+ jT809MessagePackReader.Decode();
+ Assert.Equal(jT809MessagePackReader.Reader.Length, data.Length - 1);
+ }
}
}
diff --git a/src/JT809.Protocol/JT809.Protocol.csproj b/src/JT809.Protocol/JT809.Protocol.csproj
index 4994561..ec0c144 100644
--- a/src/JT809.Protocol/JT809.Protocol.csproj
+++ b/src/JT809.Protocol/JT809.Protocol.csproj
@@ -104,16 +104,16 @@
-
+
-
+
-
+
diff --git a/src/JT809.Protocol/MessagePack/JT809MessagePackReader.cs b/src/JT809.Protocol/MessagePack/JT809MessagePackReader.cs
index 511ee90..4163888 100644
--- a/src/JT809.Protocol/MessagePack/JT809MessagePackReader.cs
+++ b/src/JT809.Protocol/MessagePack/JT809MessagePackReader.cs
@@ -63,42 +63,77 @@ namespace JT809.Protocol.MessagePack
int len = SrcBuffer.Length;
allocateBuffer[offset++] = SrcBuffer[0];
// 取出校验码看是否需要转义
- ReadOnlySpan checkCodeBufferSpan1 = SrcBuffer.Slice(len - 3, 2);
- int checkCodeLen = 0;
- if (TryDecode(checkCodeBufferSpan1, out byte value1))
+ // 正常的 4A 4A 5D
+ // 单个转义1 00 5A 02 4A 5D
+ // 单个转义2 00 4A 5A 02 5D
+ // 两个转义 5A 02 5A 02 5D
+ // 两个转义 5A 02 02 5A 02 5D
+ ReadOnlySpan checkCodeBufferSpan1 = SrcBuffer.Slice(len - 5, 4);
+ byte checkCodeLen = 0;
+ byte crcIndex = 0;
+ (int Index, byte Value) Crc1 =(-1,0);
+ (int Index, byte Value) Crc2 =(-1,0);
+ for (int i = 0; i < checkCodeBufferSpan1.Length; i++)
{
- //最后两位是转义的
- byte[] tmpCrc2 = new byte[2];
- checkCodeLen += 2;
- tmpCrc2[1] = value1;
- //从最后往前在取两位进行转义
- ReadOnlySpan checkCodeBufferSpan2 = SrcBuffer.Slice(len - 5, 2);
- if (TryDecode(checkCodeBufferSpan2, out byte value2))
+ if ((checkCodeBufferSpan1.Length - i) >= 2)
{
- //转义成功
- tmpCrc2[0] = value2;
- checkCodeLen += 2;
+ if (TryDecode(checkCodeBufferSpan1.Slice(i, 2), out byte tmp))
+ {
+ if (crcIndex > 0)
+ {
+ Crc2.Index = i;
+ Crc2.Value = tmp;
+ }
+ else
+ {
+ Crc1.Index = i;
+ Crc1.Value = tmp;
+ }
+ crcIndex++;
+ i++;
+ }
+ }
+ }
+ if (Crc1.Index >= 0 && Crc2.Index > 0)
+ {
+ byte[] crc = new byte[2];
+ //有两个需要转义的
+ crc[0] = Crc1.Value;
+ crc[1] = Crc2.Value;
+ checkCodeLen += 4;
+ _realCheckCRCCode = ReadUInt16(crc);
+ }
+ else if(Crc1.Index >= 0 && Crc2.Index == -1)
+ {
+ byte[] crc = new byte[2];
+ if ((checkCodeBufferSpan1.Length - Crc1.Index) > 2)
+ {
+ //最开始有一个需要转义的
+ crc[0] = Crc1.Value;
+ crc[1] = checkCodeBufferSpan1.Slice(Crc1.Index+2, 1)[0];
}
else
{
- //转义不成功取当前最后一位
- tmpCrc2[0] = checkCodeBufferSpan2[1];
- checkCodeLen += 1;
+ //最末尾有一个需要转义的
+ crc[0] = checkCodeBufferSpan1.Slice(Crc1.Index-1, 1)[0];
+ crc[1] = Crc1.Value;
}
- _realCheckCRCCode = ReadUInt16(tmpCrc2);
+ _realCheckCRCCode = ReadUInt16(crc);
+ //存在一个转义的
+ checkCodeLen += 3;
}
else
- {
+ {
//最后两位不是转义的
- _realCheckCRCCode=ReadUInt16(checkCodeBufferSpan1);
checkCodeLen += 2;
+ _realCheckCRCCode=ReadUInt16(SrcBuffer.Slice(len - 3, 2));
}
- //转义数据长度
+ //转义数据长度=len-校验码长度-头-尾
len = len - checkCodeLen - 1 - 1;
ReadOnlySpan tmpBufferSpan = SrcBuffer.Slice(1, len);
for (int i = 0; i < tmpBufferSpan.Length; i++)
{
- byte tmp = 0;
+ byte tmp;
if ((tmpBufferSpan.Length - i) >= 2)
{
if (TryDecode(tmpBufferSpan.Slice(i, 2), out tmp))
@@ -115,7 +150,7 @@ namespace JT809.Protocol.MessagePack
}
allocateBuffer[offset++] = (byte)(_calculateCheckCRCCode >> 8);
allocateBuffer[offset++] = (byte)_calculateCheckCRCCode;
- allocateBuffer[offset++] = SrcBuffer[SrcBuffer.Length- 1];
+ allocateBuffer[offset++] = SrcBuffer[SrcBuffer.Length - 1];
_checkCRCCodeVali = (_calculateCheckCRCCode == _realCheckCRCCode);
Reader = allocateBuffer.Slice(0, offset);
_decoded = true;
@@ -128,7 +163,7 @@ namespace JT809.Protocol.MessagePack
int len = SrcBuffer.Length;
for (int i = 0; i < len; i++)
{
- byte tmp = 0;
+ byte tmp;
if ((SrcBuffer.Length - i) >= 2)
{
if (TryDecode(SrcBuffer.Slice(i, 2), out tmp))