From 8876e964cf26a090d11fd855642c95bc02a17c7f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 20 Oct 2014 15:05:41 +1300 Subject: Support PT502 binary messages (fix #825) --- src/org/traccar/ServerManager.java | 11 ++-- src/org/traccar/protocol/Pt502FrameDecoder.java | 62 ++++++++++++++++++++++ src/org/traccar/protocol/Pt502ProtocolDecoder.java | 1 - 3 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 src/org/traccar/protocol/Pt502FrameDecoder.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index 2d3788720..33d364cc4 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -497,17 +497,16 @@ public class ServerManager { private void initPt502Server(String protocol) throws SQLException { if (isProtocolEnabled(properties, protocol)) { - serverList.add(new TrackerServer(this, new ServerBootstrap(), protocol) { + TrackerServer server = new TrackerServer(this, new ServerBootstrap(), protocol) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - byte delimiter[] = { (byte) '\r', (byte) '\n' }; - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("frameDecoder", new Pt502FrameDecoder()); pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectDecoder", new Pt502ProtocolDecoder(ServerManager.this)); } - }); + }; + server.setEndianness(ByteOrder.LITTLE_ENDIAN); + serverList.add(server); } } diff --git a/src/org/traccar/protocol/Pt502FrameDecoder.java b/src/org/traccar/protocol/Pt502FrameDecoder.java new file mode 100644 index 000000000..58409ecbf --- /dev/null +++ b/src/org/traccar/protocol/Pt502FrameDecoder.java @@ -0,0 +1,62 @@ +/* + * Copyright 2014 Anton Tananaev (anton.tananaev@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; +import org.traccar.helper.ChannelBufferTools; + +public class Pt502FrameDecoder extends FrameDecoder { + + private static final int BINARY_HEADER = 5; + + @Override + protected Object decode( + ChannelHandlerContext ctx, + Channel channel, + ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < BINARY_HEADER) { + return null; + } + + if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf && buf.getUnsignedByte(buf.readerIndex() + 1) == 0xfb) { + + int length = buf.getShort(buf.readerIndex() + 3); + if (buf.readableBytes() >= length) { + buf.skipBytes(BINARY_HEADER); + ChannelBuffer result = buf.readBytes(length - BINARY_HEADER - 2); + buf.skipBytes(2); + return result; + } + + } else { + + Integer index = ChannelBufferTools.find(buf, 0, buf.readableBytes(), "\n"); + if (index != null) { + ChannelBuffer result = buf.readBytes(index - 1); + buf.skipBytes(2); + return result; + } + + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/org/traccar/protocol/Pt502ProtocolDecoder.java index 179219038..b324e3c55 100644 --- a/src/org/traccar/protocol/Pt502ProtocolDecoder.java +++ b/src/org/traccar/protocol/Pt502ProtocolDecoder.java @@ -58,7 +58,6 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { // Parse message Matcher parser = pattern.matcher(sentence); if (!parser.matches()) { - Log.info("Parsing error"); return null; } -- cgit v1.2.3