diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2014-03-19 23:12:50 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2014-03-19 23:12:50 +1300 |
commit | 57a7b6beaade9a51690d0cc89149eca0dea8294a (patch) | |
tree | 341d468c1c0ec30fc6206cc382f67f2cf6a185c9 | |
parent | 289609b483d0b6188f8536f692363eb91e38288c (diff) | |
download | trackermap-server-57a7b6beaade9a51690d0cc89149eca0dea8294a.tar.gz trackermap-server-57a7b6beaade9a51690d0cc89149eca0dea8294a.tar.bz2 trackermap-server-57a7b6beaade9a51690d0cc89149eca0dea8294a.zip |
Fix Noran decoder (fix #610)
-rw-r--r-- | src/org/traccar/ServerManager.java | 6 | ||||
-rw-r--r-- | src/org/traccar/TrackerServer.java | 11 | ||||
-rw-r--r-- | src/org/traccar/protocol/NoranProtocolDecoder.java | 31 | ||||
-rw-r--r-- | test/org/traccar/protocol/NoranProtocolDecoderTest.java | 9 |
4 files changed, 33 insertions, 24 deletions
diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index 7505561e9..4e5b283d4 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -974,12 +974,14 @@ public class ServerManager { private void initNoranServer(String protocol) throws SQLException { if (isProtocolEnabled(properties, protocol)) { - serverList.add(new TrackerServer(this, new ConnectionlessBootstrap(), protocol) { + TrackerServer server = new TrackerServer(this, new ConnectionlessBootstrap(), protocol) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("objectDecoder", new NoranProtocolDecoder(ServerManager.this)); } - }); + }; + server.setEndianness(ByteOrder.LITTLE_ENDIAN); + serverList.add(server); } } diff --git a/src/org/traccar/TrackerServer.java b/src/org/traccar/TrackerServer.java index 1b3677836..9fcc2f5b6 100644 --- a/src/org/traccar/TrackerServer.java +++ b/src/org/traccar/TrackerServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 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. @@ -33,9 +33,9 @@ import org.jboss.netty.channel.group.DefaultChannelGroup; */ public abstract class TrackerServer { - private ServerManager serverManager; - private Bootstrap bootstrap; - private String protocol; + private final ServerManager serverManager; + private final Bootstrap bootstrap; + private final String protocol; public String getProtocol() { return protocol; @@ -97,13 +97,14 @@ public abstract class TrackerServer { * Set endianness */ void setEndianness(ByteOrder byteOrder) { + bootstrap.setOption("bufferFactory", new HeapChannelBufferFactory(byteOrder)); bootstrap.setOption("child.bufferFactory", new HeapChannelBufferFactory(byteOrder)); } /** * Opened channels */ - private ChannelGroup allChannels = new DefaultChannelGroup(); + private final ChannelGroup allChannels = new DefaultChannelGroup(); public ChannelGroup getChannelGroup() { return allChannels; diff --git a/src/org/traccar/protocol/NoranProtocolDecoder.java b/src/org/traccar/protocol/NoranProtocolDecoder.java index 372312610..7fecf18e1 100644 --- a/src/org/traccar/protocol/NoranProtocolDecoder.java +++ b/src/org/traccar/protocol/NoranProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2013 - 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. @@ -15,10 +15,12 @@ */ package org.traccar.protocol; +import java.nio.ByteOrder; import java.nio.charset.Charset; import java.util.Calendar; import java.util.TimeZone; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.traccar.BaseProtocolDecoder; @@ -33,22 +35,11 @@ public class NoranProtocolDecoder extends BaseProtocolDecoder { super(serverManager); } - private String readImei(ChannelBuffer buf) { - int b = buf.readUnsignedByte(); - StringBuilder imei = new StringBuilder(); - imei.append(b & 0x0F); - for (int i = 0; i < 7; i++) { - b = buf.readUnsignedByte(); - imei.append((b & 0xF0) >> 4); - imei.append(b & 0x0F); - } - return imei.toString(); - } - private static final int MSG_UPLOAD_POSITION = 0x0008; private static final int MSG_CONTROL_RESPONSE = 0x8009; private static final int MSG_ALARM = 0x0003; private static final int MSG_SHAKE_HAND = 0x0000; + private static final int MSG_SHAKE_HAND_RESPONSE = 0x8000; private static final int MSG_IMAGE_SIZE = 0x0200; private static final int MSG_IMAGE_PACKET = 0x0201; @@ -62,8 +53,17 @@ public class NoranProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // length int type = buf.readUnsignedShort(); - if (type == MSG_SHAKE_HAND) { - // TODO send response + if (type == MSG_SHAKE_HAND && channel != null) { + + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 13); + response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "\r\n*KW", Charset.defaultCharset())); + response.writeByte(0); + response.writeShort(response.capacity()); + response.writeShort(MSG_SHAKE_HAND_RESPONSE); + response.writeByte(1); // status + response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "\r\n", Charset.defaultCharset())); + + channel.write(response); } else if (type == MSG_UPLOAD_POSITION || @@ -91,6 +91,7 @@ public class NoranProtocolDecoder extends BaseProtocolDecoder { position.setCourse((double) buf.readUnsignedShort()); position.setLongitude((double) buf.readFloat()); position.setLatitude((double) buf.readFloat()); + position.setAltitude(0.0); // Time long timeValue = buf.readUnsignedInt(); diff --git a/test/org/traccar/protocol/NoranProtocolDecoderTest.java b/test/org/traccar/protocol/NoranProtocolDecoderTest.java index d8f228cac..8d6025b47 100644 --- a/test/org/traccar/protocol/NoranProtocolDecoderTest.java +++ b/test/org/traccar/protocol/NoranProtocolDecoderTest.java @@ -1,7 +1,9 @@ package org.traccar.protocol; +import java.nio.ByteOrder; import org.traccar.helper.TestDataManager; import org.jboss.netty.buffer.ChannelBuffers; +import static org.junit.Assert.assertNull; import static org.traccar.helper.DecoderVerifier.verify; import org.junit.Test; import org.traccar.helper.ChannelBufferTools; @@ -14,8 +16,11 @@ public class NoranProtocolDecoderTest { NoranProtocolDecoder decoder = new NoranProtocolDecoder(null); decoder.setDataManager(new TestDataManager()); - //int[] buf1 = {0x00}; - //verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertArray(buf1)))); + int[] buf1 = {0x0f,0x00,0x00,0x00,0x4e,0x52,0x30,0x39,0x46,0x30,0x34,0x31,0x35,0x35,0x00}; + assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertArray(buf1)))); + + int[] buf2 = {0x22,0x00,0x08,0x00,0x01,0x0c,0x00,0x8a,0x00,0x7e,0x9d,0xaa,0x42,0x31,0x7b,0xdd,0x41,0xa7,0xf3,0xe2,0x38,0x4e,0x52,0x30,0x39,0x46,0x30,0x34,0x31,0x35,0x35,0x00,0x00,0x00}; + verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertArray(buf2)))); } |