diff options
author | Anton Tananaev <anton@traccar.org> | 2024-05-21 06:41:44 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2024-05-21 06:41:44 -0700 |
commit | c2c27617fa4392c3a25a6c4ad62bd24060f1c0c1 (patch) | |
tree | 537f577aa32a5e7c484b4afb0b5c02c1b35eaab3 | |
parent | 445a2c5d0763bb42880ff1f9db93d755f6fdfa62 (diff) | |
download | trackermap-server-c2c27617fa4392c3a25a6c4ad62bd24060f1c0c1.tar.gz trackermap-server-c2c27617fa4392c3a25a6c4ad62bd24060f1c0c1.tar.bz2 trackermap-server-c2c27617fa4392c3a25a6c4ad62bd24060f1c0c1.zip |
Handle Snapper protocol ping
5 files changed, 80 insertions, 5 deletions
diff --git a/src/main/java/org/traccar/protocol/SnapperFrameDecoder.java b/src/main/java/org/traccar/protocol/SnapperFrameDecoder.java new file mode 100644 index 000000000..be45346a6 --- /dev/null +++ b/src/main/java/org/traccar/protocol/SnapperFrameDecoder.java @@ -0,0 +1,44 @@ +/* + * Copyright 2024 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; + +public class SnapperFrameDecoder extends BaseFrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + byte header = buf.getByte(buf.readerIndex()); + if (header == 'P') { + if (buf.readableBytes() >= 2) { + return buf.readRetainedSlice(2); + } + } else if (buf.readableBytes() >= 16) { + int length = buf.getIntLE(buf.readerIndex() + 12) + 12 + 4 + 9; + if (buf.readableBytes() >= length) { + return buf.readRetainedSlice(length); + } + } + + return null; + } + +} diff --git a/src/main/java/org/traccar/protocol/SnapperProtocol.java b/src/main/java/org/traccar/protocol/SnapperProtocol.java index 2e294eac6..25a11ed3a 100644 --- a/src/main/java/org/traccar/protocol/SnapperProtocol.java +++ b/src/main/java/org/traccar/protocol/SnapperProtocol.java @@ -15,15 +15,12 @@ */ package org.traccar.protocol; -import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import jakarta.inject.Inject; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import org.traccar.config.Config; -import java.nio.ByteOrder; - public class SnapperProtocol extends BaseProtocol { @Inject @@ -31,7 +28,7 @@ public class SnapperProtocol extends BaseProtocol { addServer(new TrackerServer(config, getName(), false) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline, Config config) { - pipeline.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 1024, 12, 4, 9, 0, true)); + pipeline.addLast(new SnapperFrameDecoder()); pipeline.addLast(new SnapperProtocolDecoder(SnapperProtocol.this)); } }); diff --git a/src/main/java/org/traccar/protocol/SnapperProtocolDecoder.java b/src/main/java/org/traccar/protocol/SnapperProtocolDecoder.java index e60736667..ef1a4426a 100644 --- a/src/main/java/org/traccar/protocol/SnapperProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/SnapperProtocolDecoder.java @@ -150,7 +150,15 @@ public class SnapperProtocolDecoder extends BaseProtocolDecoder { ByteBuf buf = (ByteBuf) msg; - buf.readUnsignedByte(); // header + byte header = buf.readByte(); + if (header == 'P') { + if (channel != null) { + ByteBuf response = Unpooled.wrappedBuffer(new byte[] {0x50, 0x4f}); + channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); + } + return null; + } + buf.readUnsignedByte(); // protocol version buf.readUnsignedIntLE(); // system bonus identifier diff --git a/src/test/java/org/traccar/protocol/SnapperFrameDecoderTest.java b/src/test/java/org/traccar/protocol/SnapperFrameDecoderTest.java new file mode 100644 index 000000000..260a18032 --- /dev/null +++ b/src/test/java/org/traccar/protocol/SnapperFrameDecoderTest.java @@ -0,0 +1,23 @@ +package org.traccar.protocol; + +import org.junit.jupiter.api.Test; +import org.traccar.ProtocolTest; + +public class SnapperFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + var decoder = inject(new SnapperFrameDecoder()); + + verifyFrame( + binary("4b0341a6b0c608000040000005000000000000007d5e14010068656c6c6f"), + decoder.decode(null, null, binary("4b0341a6b0c608000040000005000000000000007d5e14010068656c6c6f"))); + + verifyFrame( + binary("5012"), + decoder.decode(null, null, binary("5012"))); + + } + +} diff --git a/src/test/java/org/traccar/protocol/SnapperProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/SnapperProtocolDecoderTest.java index bb79194eb..43df24316 100644 --- a/src/test/java/org/traccar/protocol/SnapperProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/SnapperProtocolDecoderTest.java @@ -22,6 +22,9 @@ public class SnapperProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, binary( "4b044daff87aff5b8aad0000430100000000bb870c005b37000500210008003e0100ffffffff7f2300190000007f000000018080000002deff080003404000000400000034008c007b2273223a22303034303438222c226332223a2230303030303030303030303030303030222c226132223a2230303030303030303030303030303030222c226f223a2230303030222c2274223a2230303030222c227a223a223030222c2277223a223030222c2272223a222d222c226d223a223030303030303030222c2262223a223030303030303030227d320079007b2266223a224445222c2274223a22303832303335222c2264223a22313730343234222c226c61223a22353334312e34323635222c226c6f223a2230303935342e30373935222c2261223a22362e32222c2273223a22322e3237222c2263223a2233302e3135222c227376223a223038222c2270223a22227d330007007b2262223a5d7d")); + verifyNull(decoder, binary( + "5003")); + } } |