diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-02-02 15:04:47 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-02-02 15:04:47 -0800 |
commit | 468051a44054ce97f28220176e90983825c8d077 (patch) | |
tree | ca764289163994a1249bf590eff68484d902eed2 /src/main/java/org/traccar/handler/protocol/Arnavi4FrameDecoder.java | |
parent | 230227fde27da2db785d97c6894160b256ad6c10 (diff) | |
parent | 4e07d664cac49fcbb47376471ef9f4d770293ba9 (diff) | |
download | traccar-server-468051a44054ce97f28220176e90983825c8d077.tar.gz traccar-server-468051a44054ce97f28220176e90983825c8d077.tar.bz2 traccar-server-468051a44054ce97f28220176e90983825c8d077.zip |
Merge branch 'master' of https://github.com/binakot/traccar into binakot-master
Diffstat (limited to 'src/main/java/org/traccar/handler/protocol/Arnavi4FrameDecoder.java')
-rw-r--r-- | src/main/java/org/traccar/handler/protocol/Arnavi4FrameDecoder.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/handler/protocol/Arnavi4FrameDecoder.java b/src/main/java/org/traccar/handler/protocol/Arnavi4FrameDecoder.java new file mode 100644 index 000000000..b13f3fd7d --- /dev/null +++ b/src/main/java/org/traccar/handler/protocol/Arnavi4FrameDecoder.java @@ -0,0 +1,74 @@ +/* + * Copyright 2017 Ivan Muratov (binakot@gmail.com) + * + * 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class Arnavi4FrameDecoder extends FrameDecoder { + + private static final int MIN_LENGTH = 4; + private static final int HEADER_LENGTH = 10; + private static final int PACKET_WRAPPER_LENGTH = 8; + private static final int COMMAND_ANSWER_PACKET_LENGTH = 4; + private static final int COMMAND_ANSWER_PARCEL_NUMBER = 0xfd; + private static final byte PACKAGE_END_SIGN = 0x5d; + + private boolean firstPacket = true; + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < MIN_LENGTH) { + return null; + } + + int length; + if (firstPacket) { + firstPacket = false; + length = HEADER_LENGTH; + } else { + int index = buf.getUnsignedByte(1); // parcel number + if (index == COMMAND_ANSWER_PARCEL_NUMBER) { + length = COMMAND_ANSWER_PACKET_LENGTH; + } else { + int pos = 2; // start sign + parcel number + while (pos + PACKET_WRAPPER_LENGTH < buf.readableBytes() + && buf.getByte(pos) != PACKAGE_END_SIGN) { + + int dataLength = buf.getUnsignedShort(pos + 1); + pos += PACKET_WRAPPER_LENGTH + dataLength; // packet type + data length + unixtime + data + crc + } + + if (buf.getByte(pos) != PACKAGE_END_SIGN) { // end sign + return null; + } + + length = pos + 1; + } + } + + if (buf.readableBytes() >= length) { + return buf.readBytes(length); + } + + return null; + } + +} |