From c2c27617fa4392c3a25a6c4ad62bd24060f1c0c1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 21 May 2024 06:41:44 -0700 Subject: Handle Snapper protocol ping --- .../org/traccar/protocol/SnapperFrameDecoder.java | 44 ++++++++++++++++++++++ .../java/org/traccar/protocol/SnapperProtocol.java | 5 +-- .../traccar/protocol/SnapperProtocolDecoder.java | 10 ++++- 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/traccar/protocol/SnapperFrameDecoder.java (limited to 'src/main') 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 -- cgit v1.2.3