aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar')
-rw-r--r--src/main/java/org/traccar/helper/Checksum.java15
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2FrameEncoder.java48
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2Protocol.java8
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java21
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java70
5 files changed, 155 insertions, 7 deletions
diff --git a/src/main/java/org/traccar/helper/Checksum.java b/src/main/java/org/traccar/helper/Checksum.java
index 8c3d0063a..db5817275 100644
--- a/src/main/java/org/traccar/helper/Checksum.java
+++ b/src/main/java/org/traccar/helper/Checksum.java
@@ -200,4 +200,19 @@ public final class Checksum {
return (10 - (checksum % 10)) % 10;
}
+ public static int ip(ByteBuffer data) {
+ int sum = 0;
+ while (data.remaining() > 0) {
+ sum += data.get() & 0xff;
+ if ((sum & 0x80000000) > 0) {
+ sum = (sum & 0xffff) + (sum >> 16);
+ }
+ }
+ while ((sum >> 16) > 0) {
+ sum = (sum & 0xffff) + sum >> 16;
+ }
+ sum = (sum == 0xffff) ? sum & 0xffff : (~sum) & 0xffff;
+ return sum;
+ }
+
}
diff --git a/src/main/java/org/traccar/protocol/Xexun2FrameEncoder.java b/src/main/java/org/traccar/protocol/Xexun2FrameEncoder.java
new file mode 100644
index 000000000..52d43c36c
--- /dev/null
+++ b/src/main/java/org/traccar/protocol/Xexun2FrameEncoder.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2022 Stefan Clark (stefan@stefanclark.co.uk)
+ *
+ * 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 io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.MessageToByteEncoder;
+
+public class Xexun2FrameEncoder extends MessageToByteEncoder<ByteBuf> {
+
+ @Override
+ protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
+ out.writeBytes(msg.readBytes(2));
+
+ while (msg.readableBytes() > 2) {
+ int b = msg.readUnsignedByte();
+ if (b == 0xfa && msg.isReadable() && msg.getUnsignedByte(msg.readerIndex()) == 0xaf) {
+ msg.readUnsignedByte();
+ out.writeByte(0xfb);
+ out.writeByte(0xbf);
+ out.writeByte(0x01);
+ } else if (b == 0xfb && msg.isReadable() && msg.getUnsignedByte(msg.readerIndex()) == 0xbf) {
+ msg.readUnsignedByte();
+ out.writeByte(0xfb);
+ out.writeByte(0xbf);
+ out.writeByte(0x02);
+ } else {
+ out.writeByte(b);
+ }
+ }
+
+ out.writeBytes(msg.readBytes(2));
+
+ }
+}
diff --git a/src/main/java/org/traccar/protocol/Xexun2Protocol.java b/src/main/java/org/traccar/protocol/Xexun2Protocol.java
index 4630a69e0..52cf731f0 100644
--- a/src/main/java/org/traccar/protocol/Xexun2Protocol.java
+++ b/src/main/java/org/traccar/protocol/Xexun2Protocol.java
@@ -19,6 +19,7 @@ import org.traccar.BaseProtocol;
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;
import org.traccar.config.Config;
+import org.traccar.model.Command;
import javax.inject.Inject;
@@ -26,11 +27,18 @@ public class Xexun2Protocol extends BaseProtocol {
@Inject
public Xexun2Protocol(Config config) {
+ setSupportedDataCommands(
+ Command.TYPE_CUSTOM,
+ Command.TYPE_POSITION_PERIODIC,
+ Command.TYPE_POWER_OFF,
+ Command.TYPE_REBOOT_DEVICE);
addServer(new TrackerServer(config, getName(), false) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline, Config config) {
+ pipeline.addLast(new Xexun2FrameEncoder());
pipeline.addLast(new Xexun2FrameDecoder());
pipeline.addLast(new Xexun2ProtocolDecoder(Xexun2Protocol.this));
+ pipeline.addLast(new Xexun2ProtocolEncoder(Xexun2Protocol.this));
}
});
}
diff --git a/src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java b/src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java
index 28e7fbda3..913dfaf28 100644
--- a/src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java
@@ -24,6 +24,7 @@ import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.helper.BitUtil;
+import org.traccar.helper.Checksum;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.CellTower;
import org.traccar.model.Network;
@@ -41,13 +42,14 @@ public class Xexun2ProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
+ public static final int FLAG = 0xfaaf;
+ public static final int MSG_COMMAND = 0x07;
public static final int MSG_POSITION = 0x14;
private void sendResponse(Channel channel, int type, int index, ByteBuf imei) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
- response.writeByte(0xfa);
- response.writeByte(0xaf);
+ response.writeShort(FLAG);
response.writeShort(type);
response.writeShort(index);
@@ -56,8 +58,7 @@ public class Xexun2ProtocolDecoder extends BaseProtocolDecoder {
response.writeShort(0xfffe); // checksum
response.writeByte(1); // response
- response.writeByte(0xfa);
- response.writeByte(0xaf);
+ response.writeShort(FLAG);
channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
@@ -99,10 +100,16 @@ public class Xexun2ProtocolDecoder extends BaseProtocolDecoder {
return null;
}
- sendResponse(channel, type, index, imei);
+ int payloadSize = buf.readUnsignedShort() & 0x03ff;
+ int checksum = buf.readUnsignedShort();
- buf.readUnsignedShort(); // attributes
- buf.readUnsignedShort(); // checksum
+ if (checksum != Checksum.ip(buf.nioBuffer(buf.readerIndex(), payloadSize))) {
+ return null;
+ }
+
+ if (type != MSG_COMMAND) {
+ sendResponse(channel, type, index, imei);
+ }
if (type == MSG_POSITION) {
List<Integer> lengths = new ArrayList<>();
diff --git a/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java b/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
new file mode 100644
index 000000000..8f3fa5672
--- /dev/null
+++ b/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2022 Stefan Clark (stefan@stefanclark.co.uk)
+ *
+ * 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 io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.traccar.BaseProtocolEncoder;
+import org.traccar.helper.Checksum;
+import org.traccar.helper.DataConverter;
+import org.traccar.model.Command;
+import org.traccar.Protocol;
+
+import java.nio.charset.StandardCharsets;
+
+public class Xexun2ProtocolEncoder extends BaseProtocolEncoder {
+
+ public Xexun2ProtocolEncoder(Protocol protocol) {
+ super(protocol);
+ }
+
+ private static ByteBuf encodeContent(String uniqueId, String content) {
+ ByteBuf buf = Unpooled.buffer();
+
+ ByteBuf message = Unpooled.copiedBuffer(content.getBytes(StandardCharsets.US_ASCII));
+
+ buf.writeShort(Xexun2ProtocolDecoder.FLAG);
+ buf.writeShort(Xexun2ProtocolDecoder.MSG_COMMAND);
+ buf.writeShort(1); // index
+ buf.writeBytes(DataConverter.parseHex(uniqueId + "0"));
+ buf.writeShort(message.readableBytes());
+ buf.writeShort(Checksum.ip(message.nioBuffer()));
+ buf.writeBytes(message);
+ buf.writeShort(Xexun2ProtocolDecoder.FLAG);
+
+ return buf;
+ }
+
+ @Override
+ protected Object encodeCommand(Command command) {
+ String uniqueId = getUniqueId(command.getDeviceId());
+
+ switch (command.getType()) {
+ case Command.TYPE_CUSTOM:
+ return encodeContent(uniqueId, command.getString(Command.KEY_DATA));
+ case Command.TYPE_POSITION_PERIODIC:
+ return encodeContent(uniqueId,
+ String.format("tracking_send=%1$d,%1$d", command.getInteger(Command.KEY_FREQUENCY)));
+ case Command.TYPE_POWER_OFF:
+ return encodeContent(uniqueId, "of=1");
+ case Command.TYPE_REBOOT_DEVICE:
+ return encodeContent(uniqueId, "reset");
+ default:
+ return null;
+ }
+ }
+
+}