diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-04-13 22:25:50 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-04-13 22:25:50 -0700 |
commit | b595ec1bede3e30447b49bb7340f88c9b3a3f97f (patch) | |
tree | 7fa7a21fe803d68032a9ad3301ffb12eb3733b1f /src/main/java/org/traccar/protocol | |
parent | a3269fda8b77c6af0e810222dae4aff6faf1df3f (diff) | |
download | traccar-server-b595ec1bede3e30447b49bb7340f88c9b3a3f97f.tar.gz traccar-server-b595ec1bede3e30447b49bb7340f88c9b3a3f97f.tar.bz2 traccar-server-b595ec1bede3e30447b49bb7340f88c9b3a3f97f.zip |
Support ITS binary checksum
Diffstat (limited to 'src/main/java/org/traccar/protocol')
-rw-r--r-- | src/main/java/org/traccar/protocol/ItsFrameDecoder.java | 48 | ||||
-rw-r--r-- | src/main/java/org/traccar/protocol/ItsProtocol.java | 3 |
2 files changed, 49 insertions, 2 deletions
diff --git a/src/main/java/org/traccar/protocol/ItsFrameDecoder.java b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java new file mode 100644 index 000000000..79b341760 --- /dev/null +++ b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java @@ -0,0 +1,48 @@ +/* + * Copyright 2019 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 ItsFrameDecoder extends BaseFrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + + if (buf.writerIndex() > delimiterIndex + 1 && buf.getByte(delimiterIndex + 1) == '*') { + delimiterIndex += 1; + } + + ByteBuf frame; + if (buf.getByte(delimiterIndex - 2) == ',') { + frame = buf.readRetainedSlice(delimiterIndex - 1 - buf.readerIndex()); + buf.skipBytes(1); // binary checksum + } else { + frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); + } + + buf.skipBytes(1); // delimiter + + return frame; + } + +} diff --git a/src/main/java/org/traccar/protocol/ItsProtocol.java b/src/main/java/org/traccar/protocol/ItsProtocol.java index f53600dc9..e074f9277 100644 --- a/src/main/java/org/traccar/protocol/ItsProtocol.java +++ b/src/main/java/org/traccar/protocol/ItsProtocol.java @@ -18,7 +18,6 @@ package org.traccar.protocol; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; -import org.traccar.CharacterDelimiterFrameDecoder; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; @@ -28,7 +27,7 @@ public class ItsProtocol extends BaseProtocol { addServer(new TrackerServer(false, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { - pipeline.addLast(new CharacterDelimiterFrameDecoder(1024, '*')); + pipeline.addLast(new ItsFrameDecoder()); pipeline.addLast(new StringEncoder()); pipeline.addLast(new StringDecoder()); pipeline.addLast(new ItsProtocolDecoder(ItsProtocol.this)); |