aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-11-24 15:52:52 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2016-11-24 15:53:10 +1300
commit04f33e6ba29b3a73a65b081d2a8b6ac3e0e0ebee (patch)
tree5051340b36b3370ede71d84208189c9aee23f901
parent7269b9b32ef8f75fc0fbd1c64d4d4748c731ce04 (diff)
downloadtraccar-server-04f33e6ba29b3a73a65b081d2a8b6ac3e0e0ebee.tar.gz
traccar-server-04f33e6ba29b3a73a65b081d2a8b6ac3e0e0ebee.tar.bz2
traccar-server-04f33e6ba29b3a73a65b081d2a8b6ac3e0e0ebee.zip
Implement Teltonika AT200 protocol
-rw-r--r--setup/default.xml1
-rw-r--r--src/org/traccar/protocol/At2000FrameDecoder.java57
-rw-r--r--src/org/traccar/protocol/At2000Protocol.java42
-rw-r--r--src/org/traccar/protocol/At2000ProtocolDecoder.java134
-rw-r--r--test/org/traccar/protocol/At2000FrameDecoderTest.java26
-rw-r--r--test/org/traccar/protocol/At2000ProtocolDecoderTest.java24
6 files changed, 284 insertions, 0 deletions
diff --git a/setup/default.xml b/setup/default.xml
index d6f8a1752..869c077cf 100644
--- a/setup/default.xml
+++ b/setup/default.xml
@@ -440,5 +440,6 @@
<entry key='smokey.port'>5125</entry>
<entry key='extremtrac.port'>5126</entry>
<entry key='trakmate.port'>5127</entry>
+ <entry key='at2000.port'>5128</entry>
</properties>
diff --git a/src/org/traccar/protocol/At2000FrameDecoder.java b/src/org/traccar/protocol/At2000FrameDecoder.java
new file mode 100644
index 000000000..d0be3f5f4
--- /dev/null
+++ b/src/org/traccar/protocol/At2000FrameDecoder.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2016 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 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 At2000FrameDecoder extends FrameDecoder {
+
+ private static final int BLOCK_LENGTH = 16;
+
+ private boolean firstPacket = true;
+
+ @Override
+ protected Object decode(
+ ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
+
+ if (buf.readableBytes() < 5) {
+ return null;
+ }
+
+ int length;
+ if (firstPacket) {
+ firstPacket = false;
+ length = buf.getUnsignedMedium(buf.readerIndex() + 2);
+ } else {
+ length = buf.getUnsignedMedium(buf.readerIndex() + 1);
+ }
+
+ length += BLOCK_LENGTH;
+ if (length % BLOCK_LENGTH != 0) {
+ length = (length / BLOCK_LENGTH + 1) * BLOCK_LENGTH;
+ }
+
+ if (buf.readableBytes() >= length) {
+ return buf.readBytes(length);
+ }
+
+ return null;
+ }
+
+}
diff --git a/src/org/traccar/protocol/At2000Protocol.java b/src/org/traccar/protocol/At2000Protocol.java
new file mode 100644
index 000000000..418619cb4
--- /dev/null
+++ b/src/org/traccar/protocol/At2000Protocol.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016 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 org.jboss.netty.bootstrap.ServerBootstrap;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.traccar.BaseProtocol;
+import org.traccar.TrackerServer;
+
+import java.util.List;
+
+public class At2000Protocol extends BaseProtocol {
+
+ public At2000Protocol() {
+ super("at2000");
+ }
+
+ @Override
+ public void initTrackerServers(List<TrackerServer> serverList) {
+ serverList.add(new TrackerServer(new ServerBootstrap(), getName()) {
+ @Override
+ protected void addSpecificHandlers(ChannelPipeline pipeline) {
+ pipeline.addLast("frameDecoder", new At2000FrameDecoder());
+ pipeline.addLast("objectDecoder", new At2000ProtocolDecoder(At2000Protocol.this));
+ }
+ });
+ }
+
+}
diff --git a/src/org/traccar/protocol/At2000ProtocolDecoder.java b/src/org/traccar/protocol/At2000ProtocolDecoder.java
new file mode 100644
index 000000000..17da0eef7
--- /dev/null
+++ b/src/org/traccar/protocol/At2000ProtocolDecoder.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2016 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 org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.channel.Channel;
+import org.traccar.BaseProtocolDecoder;
+import org.traccar.DeviceSession;
+import org.traccar.helper.UnitsConverter;
+import org.traccar.model.Position;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import javax.xml.bind.DatatypeConverter;
+import java.net.SocketAddress;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+public class At2000ProtocolDecoder extends BaseProtocolDecoder {
+
+ private static final int BLOCK_LENGTH = 16;
+
+ public At2000ProtocolDecoder(At2000Protocol protocol) {
+ super(protocol);
+ }
+
+ public static final int MSG_ACKNOWLEDGEMENT = 0x00;
+ public static final int MSG_DEVICE_ID = 0x01;
+ public static final int MSG_TRACK_REQUEST = 0x88;
+ public static final int MSG_TRACK_RESPONSE = 0x89;
+ public static final int MSG_SESSION_END = 0x0c;
+
+ private Cipher cipher;
+
+ private static void sendResponse(Channel channel) {
+ if (channel != null) {
+ ChannelBuffer response = ChannelBuffers.directBuffer(BLOCK_LENGTH);
+ response.writeByte(MSG_ACKNOWLEDGEMENT);
+ response.writeMedium(1);
+ response.writeByte(0x00); // success
+ response.writerIndex(BLOCK_LENGTH);
+ channel.write(response);
+ }
+ }
+
+ @Override
+ protected Object decode(
+ Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
+
+ ChannelBuffer buf = (ChannelBuffer) msg;
+
+ if (buf.getUnsignedByte(buf.readerIndex()) == 0x01) {
+ buf.readUnsignedByte(); // codec id
+ }
+
+ int type = buf.readUnsignedByte();
+ buf.readUnsignedMedium(); // length
+ buf.skipBytes(BLOCK_LENGTH - 1 - 3);
+
+ if (type == MSG_DEVICE_ID) {
+
+ String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII);
+ if (getDeviceSession(channel, remoteAddress, imei) != null) {
+
+ byte[] iv = new byte[BLOCK_LENGTH];
+ buf.readBytes(iv);
+ IvParameterSpec ivSpec = new IvParameterSpec(iv);
+
+ SecretKeySpec keySpec = new SecretKeySpec(
+ DatatypeConverter.parseHexBinary("000102030405060708090a0b0c0d0e0f"), "AES");
+
+ cipher = Cipher.getInstance("AES/CBC/NoPadding");
+ cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
+
+ byte[] data = new byte[BLOCK_LENGTH];
+ buf.readBytes(data);
+ cipher.update(data);
+
+ }
+
+ } else if (type == MSG_TRACK_RESPONSE) {
+
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
+ if (deviceSession == null) {
+ return null;
+ }
+
+ Position position = new Position();
+ position.setProtocol(getProtocolName());
+ position.setDeviceId(deviceSession.getDeviceId());
+
+ byte[] data = new byte[buf.capacity() - BLOCK_LENGTH];
+ buf.readBytes(data);
+ buf = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, cipher.update(data));
+
+ buf.readUnsignedShort(); // index
+ buf.readUnsignedShort(); // reserved
+
+ position.setValid(true);
+
+ position.setTime(new Date(buf.readLong() * 1000));
+
+ position.setLatitude(buf.readFloat());
+ position.setLongitude(buf.readFloat());
+ position.setAltitude(buf.readFloat());
+ position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloat()));
+ position.setCourse(buf.readFloat());
+
+ return position;
+
+ }
+
+ sendResponse(channel);
+
+ return null;
+ }
+
+}
diff --git a/test/org/traccar/protocol/At2000FrameDecoderTest.java b/test/org/traccar/protocol/At2000FrameDecoderTest.java
new file mode 100644
index 000000000..c7a3dc0b1
--- /dev/null
+++ b/test/org/traccar/protocol/At2000FrameDecoderTest.java
@@ -0,0 +1,26 @@
+package org.traccar.protocol;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.traccar.ProtocolTest;
+
+import java.nio.ByteOrder;
+
+public class At2000FrameDecoderTest extends ProtocolTest {
+
+ @Test
+ public void testDecode() throws Exception {
+
+ At2000FrameDecoder decoder = new At2000FrameDecoder();
+
+ Assert.assertEquals(
+ binary(ByteOrder.LITTLE_ENDIAN, "01012f00000000000000000000000000003335363137333036343430373439320fad981997ae8e031fe10c0ea7641903ca32c0331df467233d2a9cd886fbeef8"),
+ decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "01012f00000000000000000000000000003335363137333036343430373439320fad981997ae8e031fe10c0ea7641903ca32c0331df467233d2a9cd886fbeef8")));
+
+ Assert.assertEquals(
+ binary(ByteOrder.LITTLE_ENDIAN, "893f0000000000000000000000000000e048b1a31deba3f5dbe8877f574877e6ed4d022b6611a10d80dfc4c0c11fa8aacf4a9de61528327e2b66843dd9c5d3a7cc9ee1d9c71a34bb482145d88b4fda3e"),
+ decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "893f0000000000000000000000000000e048b1a31deba3f5dbe8877f574877e6ed4d022b6611a10d80dfc4c0c11fa8aacf4a9de61528327e2b66843dd9c5d3a7cc9ee1d9c71a34bb482145d88b4fda3e")));
+
+ }
+
+}
diff --git a/test/org/traccar/protocol/At2000ProtocolDecoderTest.java b/test/org/traccar/protocol/At2000ProtocolDecoderTest.java
new file mode 100644
index 000000000..14c6920d0
--- /dev/null
+++ b/test/org/traccar/protocol/At2000ProtocolDecoderTest.java
@@ -0,0 +1,24 @@
+package org.traccar.protocol;
+
+import org.junit.Test;
+import org.traccar.ProtocolTest;
+
+public class At2000ProtocolDecoderTest extends ProtocolTest {
+
+ @Test
+ public void testDecode() throws Exception {
+
+ At2000ProtocolDecoder decoder = new At2000ProtocolDecoder(new At2000Protocol());
+
+ verifyNothing(decoder, binary(
+ "01012f00000000000000000000000000003335363137333036343430373439320fad981997ae8e031fe10c0ea7641903ca32c0331df467233d2a9cd886fbeef8"));
+
+ verifyPosition(decoder, binary(
+ "893f0000000000000000000000000000e048b1a31deba3f5dbe8877f574877e6ed4d022b6611a10d80dfc4c0c11fa8aacf4a9de61528327e2b66843dd9c5d3a7cc9ee1d9c71a34bb482145d88b4fda3e"));
+
+ verifyNothing(decoder, binary(
+ "01012f00000000000000000000000000003335373435343037313632363831345612da3748bede02ea4faf04ac02f420c0ff37719eccf2864fa2b8191abf8242"));
+
+ }
+
+}