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.java8
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2FrameEncoder.java48
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2Protocol.java1
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java12
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java48
5 files changed, 66 insertions, 51 deletions
diff --git a/src/main/java/org/traccar/helper/Checksum.java b/src/main/java/org/traccar/helper/Checksum.java
index e660790ef..db5817275 100644
--- a/src/main/java/org/traccar/helper/Checksum.java
+++ b/src/main/java/org/traccar/helper/Checksum.java
@@ -200,18 +200,14 @@ public final class Checksum {
return (10 - (checksum % 10)) % 10;
}
- public static int udp(ByteBuffer data) {
+ public static int ip(ByteBuffer data) {
int sum = 0;
- int len = data.capacity();
- for (int j = 0; len > 1; len--) {
+ while (data.remaining() > 0) {
sum += data.get() & 0xff;
if ((sum & 0x80000000) > 0) {
sum = (sum & 0xffff) + (sum >> 16);
}
}
- if (len == 1) {
- sum += data.get() & 0xff;
- }
while ((sum >> 16) > 0) {
sum = (sum & 0xffff) + sum >> 16;
}
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 1d5038a22..52cf731f0 100644
--- a/src/main/java/org/traccar/protocol/Xexun2Protocol.java
+++ b/src/main/java/org/traccar/protocol/Xexun2Protocol.java
@@ -35,6 +35,7 @@ public class Xexun2Protocol extends BaseProtocol {
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 f0158e6ce..913dfaf28 100644
--- a/src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/Xexun2ProtocolDecoder.java
@@ -42,12 +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.writeShort(Xexun2ProtocolEncoder.FLAG);
+ response.writeShort(FLAG);
response.writeShort(type);
response.writeShort(index);
@@ -56,7 +58,7 @@ public class Xexun2ProtocolDecoder extends BaseProtocolDecoder {
response.writeShort(0xfffe); // checksum
response.writeByte(1); // response
- response.writeShort(Xexun2ProtocolEncoder.FLAG);
+ response.writeShort(FLAG);
channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
@@ -99,13 +101,13 @@ public class Xexun2ProtocolDecoder extends BaseProtocolDecoder {
}
int payloadSize = buf.readUnsignedShort() & 0x03ff;
- int checksum = buf.readUnsignedShort(); // checksum
+ int checksum = buf.readUnsignedShort();
- if (checksum != Checksum.udp(buf.nioBuffer(buf.readerIndex(), payloadSize))) {
+ if (checksum != Checksum.ip(buf.nioBuffer(buf.readerIndex(), payloadSize))) {
return null;
}
- if (type != Xexun2ProtocolEncoder.MSG_COMMAND) {
+ if (type != MSG_COMMAND) {
sendResponse(channel, type, index, imei);
}
diff --git a/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java b/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
index 6e1e1d68d..c315cab30 100644
--- a/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
+++ b/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
@@ -23,61 +23,29 @@ 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);
}
- public static final int FLAG = 0xfaaf;
- public static final int MSG_COMMAND = 0x07;
-
- private static ByteBuf encodeFrame(ByteBuf buf) {
- int bufLength = buf.readableBytes();
- if (bufLength < 5) {
- return null;
- }
-
- ByteBuf result = Unpooled.buffer();
-
- result.writeBytes(buf.readBytes(2));
-
- while (buf.readerIndex() < bufLength - 2) {
- int b = buf.readUnsignedByte();
- if (b == 0xfa && buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) == 0xaf) {
- buf.readUnsignedByte();
- result.writeByte(0xfb);
- result.writeByte(0xbf);
- result.writeByte(0x01);
- } else if (b == 0xfb && buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) == 0xbf) {
- buf.readUnsignedByte();
- result.writeByte(0xfb);
- result.writeByte(0xbf);
- result.writeByte(0x02);
- } else {
- result.writeByte(b);
- }
- }
- result.writeBytes(buf.readBytes(2));
-
- return result;
- }
-
private static ByteBuf encodeContent(String uniqueId, String content) {
ByteBuf buf = Unpooled.buffer();
- ByteBuf message = Unpooled.copiedBuffer(content.getBytes());
+ ByteBuf message = Unpooled.copiedBuffer(content.getBytes(StandardCharsets.US_ASCII));
- buf.writeShort(FLAG);
- buf.writeShort(MSG_COMMAND);
+ buf.writeShort(Xexun2ProtocolDecoder.FLAG);
+ buf.writeShort(Xexun2ProtocolDecoder.MSG_COMMAND);
buf.writeShort(1); // index
buf.writeBytes(DataConverter.parseHex(uniqueId + "0"));
buf.writeShort(message.capacity());
- buf.writeShort(Checksum.udp(message.nioBuffer()));
+ buf.writeShort(Checksum.ip(message.nioBuffer()));
buf.writeBytes(message);
- buf.writeShort(FLAG);
+ buf.writeShort(Xexun2ProtocolDecoder.FLAG);
- return encodeFrame(buf);
+ return buf;
}
@Override