aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2024-03-08 21:26:09 -0800
committerAnton Tananaev <anton@traccar.org>2024-03-08 21:26:09 -0800
commit165a6c76e8127bae7bdf16a9e9a4a24247f1dec4 (patch)
treed36cb0abc22b83da09e301d6d9b72f98da274e9e
parent43508913a7f3356324ec5af447980121d9c64dd0 (diff)
downloadtrackermap-server-165a6c76e8127bae7bdf16a9e9a4a24247f1dec4.tar.gz
trackermap-server-165a6c76e8127bae7bdf16a9e9a4a24247f1dec4.tar.bz2
trackermap-server-165a6c76e8127bae7bdf16a9e9a4a24247f1dec4.zip
Fix Huabao frame encoder
-rw-r--r--src/main/java/org/traccar/protocol/HuabaoFrameEncoder.java22
-rw-r--r--src/test/java/org/traccar/protocol/HuabaoFrameEncoderTest.java21
2 files changed, 31 insertions, 12 deletions
diff --git a/src/main/java/org/traccar/protocol/HuabaoFrameEncoder.java b/src/main/java/org/traccar/protocol/HuabaoFrameEncoder.java
index 808529a98..5e48a466a 100644
--- a/src/main/java/org/traccar/protocol/HuabaoFrameEncoder.java
+++ b/src/main/java/org/traccar/protocol/HuabaoFrameEncoder.java
@@ -24,20 +24,18 @@ public class HuabaoFrameEncoder extends MessageToByteEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
+ int startIndex = msg.readerIndex();
while (msg.isReadable()) {
+ int index = msg.readerIndex();
int b = msg.readUnsignedByte();
- switch (b) {
- case 0x7d:
- out.writeByte(0x7d);
- out.writeByte(0x01);
- break;
- case 0x7e:
- out.writeByte(0x7d);
- out.writeByte(0x02);
- break;
- default:
- out.writeByte(b);
- break;
+ if (b == 0x7d) {
+ out.writeByte(0x7d);
+ out.writeByte(0x01);
+ } else if (b == 0x7e && index != startIndex && msg.isReadable()) {
+ out.writeByte(0x7d);
+ out.writeByte(0x02);
+ } else {
+ out.writeByte(b);
}
}
}
diff --git a/src/test/java/org/traccar/protocol/HuabaoFrameEncoderTest.java b/src/test/java/org/traccar/protocol/HuabaoFrameEncoderTest.java
new file mode 100644
index 000000000..a2799e9a8
--- /dev/null
+++ b/src/test/java/org/traccar/protocol/HuabaoFrameEncoderTest.java
@@ -0,0 +1,21 @@
+package org.traccar.protocol;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.jupiter.api.Test;
+import org.traccar.ProtocolTest;
+
+public class HuabaoFrameEncoderTest extends ProtocolTest {
+
+ @Test
+ public void testDecode() throws Exception {
+
+ var encoder = new HuabaoFrameEncoder();
+
+ ByteBuf buf = Unpooled.buffer();
+ encoder.encode(null, binary("7e307e087d557e"), buf);
+ verifyFrame(binary("7e307d02087d01557e"), buf);
+
+ }
+
+}