aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2018-03-26 06:45:10 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2018-03-26 15:45:34 +1300
commitd847f80334498bde6993b8f3a38c7df05d83b47b (patch)
tree8a3f5f9a61b388f08f6f523394869e93f99d3ce3 /src
parent4938acc7d9d901eed6b33f047a312b9a89bdd957 (diff)
downloadtraccar-server-d847f80334498bde6993b8f3a38c7df05d83b47b.tar.gz
traccar-server-d847f80334498bde6993b8f3a38c7df05d83b47b.tar.bz2
traccar-server-d847f80334498bde6993b8f3a38c7df05d83b47b.zip
Implement EGTS responses
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/helper/Checksum.java35
-rw-r--r--src/org/traccar/protocol/EgtsProtocol.java7
-rw-r--r--src/org/traccar/protocol/EgtsProtocolDecoder.java109
3 files changed, 133 insertions, 18 deletions
diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java
index 43ba6a689..c4141e7d2 100644
--- a/src/org/traccar/helper/Checksum.java
+++ b/src/org/traccar/helper/Checksum.java
@@ -129,6 +129,33 @@ public final class Checksum {
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
};
+ private static final int[] CRC8_TABLE = {
+ 0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97,
+ 0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
+ 0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4,
+ 0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D,
+ 0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11, 0x3F,
+ 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8, 0xC5, 0xF4, 0xA7, 0x96,
+ 0x01, 0x30, 0x63, 0x52, 0x7C, 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0x56, 0x78, 0x49, 0x1A, 0x2B, 0xBC, 0x8D, 0xDE,
+ 0xEF, 0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15, 0x3B, 0x0A,
+ 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC };
+
// More info: http://reveng.sourceforge.net/crc-catalogue/16.htm
public static final String CRC16_IBM = "IBM";
public static final String CRC16_MODBUS = "MODBUS";
@@ -181,6 +208,14 @@ public final class Checksum {
}
}
+ public static int crc8(ByteBuffer buf) {
+ int crc = 0xFF;
+ while (buf.hasRemaining()) {
+ crc = CRC8_TABLE[(crc ^ buf.get()) & 0xFF];
+ }
+ return crc & 0xFF;
+ }
+
public static int crc32(ByteBuffer buf) {
CRC32 checksum = new CRC32();
while (buf.hasRemaining()) {
diff --git a/src/org/traccar/protocol/EgtsProtocol.java b/src/org/traccar/protocol/EgtsProtocol.java
index 0a57f0061..13ec6c9a7 100644
--- a/src/org/traccar/protocol/EgtsProtocol.java
+++ b/src/org/traccar/protocol/EgtsProtocol.java
@@ -20,6 +20,7 @@ import org.jboss.netty.channel.ChannelPipeline;
import org.traccar.BaseProtocol;
import org.traccar.TrackerServer;
+import java.nio.ByteOrder;
import java.util.List;
public class EgtsProtocol extends BaseProtocol {
@@ -30,13 +31,15 @@ public class EgtsProtocol extends BaseProtocol {
@Override
public void initTrackerServers(List<TrackerServer> serverList) {
- serverList.add(new TrackerServer(new ServerBootstrap(), getName()) {
+ TrackerServer server = new TrackerServer(new ServerBootstrap(), getName()) {
@Override
protected void addSpecificHandlers(ChannelPipeline pipeline) {
pipeline.addLast("frameDecoder", new EgtsFrameDecoder());
pipeline.addLast("objectDecoder", new EgtsProtocolDecoder(EgtsProtocol.this));
}
- });
+ };
+ server.setEndianness(ByteOrder.LITTLE_ENDIAN);
+ serverList.add(server);
}
}
diff --git a/src/org/traccar/protocol/EgtsProtocolDecoder.java b/src/org/traccar/protocol/EgtsProtocolDecoder.java
index d05504d81..85376b1a5 100644
--- a/src/org/traccar/protocol/EgtsProtocolDecoder.java
+++ b/src/org/traccar/protocol/EgtsProtocolDecoder.java
@@ -16,13 +16,17 @@
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.BitUtil;
+import org.traccar.helper.Checksum;
import org.traccar.model.Position;
import java.net.SocketAddress;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@@ -33,6 +37,10 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
+ public static final int PT_RESPONSE = 0;
+ public static final int PT_APPDATA = 1;
+ public static final int PT_SIGNED_APPDATA = 2;
+
public static final int SERVICE_AUTH = 1;
public static final int SERVICE_TELEDATA = 2;
public static final int SERVICE_COMMANDS = 4;
@@ -60,6 +68,44 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder {
public static final int MSG_LIQUID_LEVEL_SENSOR = 27;
public static final int MSG_PASSENGERS_COUNTERS = 28;
+ private int packetId;
+
+ private void sendResponse(
+ Channel channel, int packetType, int index, int serviceType, int type, ChannelBuffer content) {
+ if (channel != null) {
+
+ ChannelBuffer data = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
+ data.writeByte(type);
+ data.writeShort(content.readableBytes());
+ data.writeBytes(content);
+
+ ChannelBuffer record = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
+ record.writeShort(data.readableBytes());
+ record.writeShort(index);
+ record.writeByte(1 << 6); // flags
+ record.writeByte(serviceType);
+ record.writeByte(serviceType);
+ record.writeBytes(data);
+ int recordChecksum = Checksum.crc16(Checksum.CRC16_CCITT_FALSE, record.toByteBuffer());
+
+ ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
+ response.writeByte(1); // protocol version
+ response.writeByte(0); // security key id
+ response.writeByte(0); // flags
+ response.writeByte(5 + 2 + 2 + 2); // header length
+ response.writeByte(0); // encoding
+ response.writeShort(record.readableBytes());
+ response.writeShort(packetId++);
+ response.writeByte(packetType);
+ response.writeByte(Checksum.crc8(response.toByteBuffer()));
+ response.writeBytes(record);
+ response.writeShort(recordChecksum);
+
+ channel.write(response);
+
+ }
+ }
+
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
@@ -68,26 +114,16 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder {
buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));
- DeviceSession deviceSession = null;
List<Position> positions = new LinkedList<>();
while (buf.readableBytes() > 2) {
int length = buf.readUnsignedShort();
-
- buf.readUnsignedShort(); // index
-
+ int index = buf.readUnsignedShort();
int recordFlags = buf.readUnsignedByte();
if (BitUtil.check(recordFlags, 0)) {
- String deviceId = String.valueOf(buf.readUnsignedInt());
- if (deviceSession == null) {
- deviceSession = getDeviceSession(channel, remoteAddress, deviceId);
- }
- }
-
- if (deviceSession == null) {
- deviceSession = getDeviceSession(channel, remoteAddress);
+ buf.readUnsignedInt(); // object id
}
if (BitUtil.check(recordFlags, 1)) {
@@ -97,19 +133,58 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder {
buf.readUnsignedInt(); // time
}
- buf.readUnsignedByte(); // source service type
+ int serviceType = buf.readUnsignedByte();
buf.readUnsignedByte(); // recipient service type
int recordEnd = buf.readerIndex() + length;
Position position = new Position(getProtocolName());
- position.setDeviceId(deviceSession.getDeviceId());
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
+ if (deviceSession != null) {
+ position.setDeviceId(deviceSession.getDeviceId());
+ }
+
+ ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
+ response.writeShort(index);
+ response.writeByte(0); // success
+ sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response);
while (buf.readerIndex() < recordEnd) {
int type = buf.readUnsignedByte();
int end = buf.readUnsignedShort() + buf.readerIndex();
- if (type == MSG_POS_DATA) {
+ if (type == MSG_TERM_IDENTITY) {
+
+ buf.readUnsignedInt(); // object id
+ int flags = buf.readUnsignedByte();
+
+ if (BitUtil.check(flags, 0)) {
+ buf.readUnsignedShort(); // home dispatcher identifier
+ }
+ if (BitUtil.check(flags, 1)) {
+ getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
+ }
+ if (BitUtil.check(flags, 2)) {
+ getDeviceSession(channel, remoteAddress, buf.readBytes(16).toString(StandardCharsets.US_ASCII));
+ }
+ if (BitUtil.check(flags, 3)) {
+ buf.skipBytes(3); // language identifier
+ }
+ if (BitUtil.check(flags, 5)) {
+ buf.skipBytes(3); // network identifier
+ }
+ if (BitUtil.check(flags, 6)) {
+ buf.readUnsignedShort(); // buffer size
+ }
+ if (BitUtil.check(flags, 7)) {
+ getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
+ }
+
+ response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
+ response.writeByte(0); // success
+ sendResponse(channel, PT_APPDATA, index, serviceType, MSG_RESULT_CODE, response);
+
+ } else if (type == MSG_POS_DATA) {
position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000)); // since 2010-01-01
position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL);
@@ -166,7 +241,9 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder {
buf.readerIndex(end);
}
- positions.add(position);
+ if (deviceSession != null) {
+ positions.add(position);
+ }
}
return positions.isEmpty() ? null : positions;