aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-29 17:03:23 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-29 17:03:23 -0700
commitaed0411f4ae5cfc007d27e0521a39a242fab7840 (patch)
treea704e82dc4c82e0d128e9619290ebb5cb8a66ea1
parent8921fc12ea0590c290f3780fa20df7176b8c0000 (diff)
downloadtrackermap-server-aed0411f4ae5cfc007d27e0521a39a242fab7840.tar.gz
trackermap-server-aed0411f4ae5cfc007d27e0521a39a242fab7840.tar.bz2
trackermap-server-aed0411f4ae5cfc007d27e0521a39a242fab7840.zip
Unify signed magnitude int decoding
-rw-r--r--src/main/java/org/traccar/helper/BitUtil.java4
-rw-r--r--src/main/java/org/traccar/helper/BufferUtil.java6
-rw-r--r--src/main/java/org/traccar/protocol/NiotProtocolDecoder.java11
-rw-r--r--src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java15
-rw-r--r--src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java13
-rw-r--r--src/main/java/org/traccar/protocol/ThinkPowerProtocolDecoder.java1
-rw-r--r--src/test/java/org/traccar/helper/BufferUtilTest.java6
7 files changed, 24 insertions, 32 deletions
diff --git a/src/main/java/org/traccar/helper/BitUtil.java b/src/main/java/org/traccar/helper/BitUtil.java
index b6108edff..829ddebc9 100644
--- a/src/main/java/org/traccar/helper/BitUtil.java
+++ b/src/main/java/org/traccar/helper/BitUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2022 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.
@@ -21,7 +21,7 @@ public final class BitUtil {
}
public static boolean check(long number, int index) {
- return (number & (1 << index)) != 0;
+ return (number & (1L << index)) != 0;
}
public static int between(int number, int from, int to) {
diff --git a/src/main/java/org/traccar/helper/BufferUtil.java b/src/main/java/org/traccar/helper/BufferUtil.java
index 1e1a687fa..0dbe0a4ad 100644
--- a/src/main/java/org/traccar/helper/BufferUtil.java
+++ b/src/main/java/org/traccar/helper/BufferUtil.java
@@ -27,6 +27,12 @@ public final class BufferUtil {
private BufferUtil() {
}
+ public static int readSignedMagnitudeInt(ByteBuf buffer) {
+ long value = buffer.readUnsignedInt();
+ int result = (int) BitUtil.to(value, 31);
+ return BitUtil.check(value, 31) ? -result : result;
+ }
+
public static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value, int count) {
int startIndex = fromIndex;
for (int i = 0; i < count; i++) {
diff --git a/src/main/java/org/traccar/protocol/NiotProtocolDecoder.java b/src/main/java/org/traccar/protocol/NiotProtocolDecoder.java
index 16d992938..35614ccca 100644
--- a/src/main/java/org/traccar/protocol/NiotProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/NiotProtocolDecoder.java
@@ -20,6 +20,7 @@ import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
+import org.traccar.helper.BufferUtil;
import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
@@ -57,12 +58,6 @@ public class NiotProtocolDecoder extends BaseProtocolDecoder {
}
}
- private double readCoordinate(ByteBuf buf) {
- long value = buf.readUnsignedInt();
- double result = BitUtil.to(value, 31) / 1800000.0;
- return BitUtil.check(value, 31) ? -result : result;
- }
-
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
@@ -96,8 +91,8 @@ public class NiotProtocolDecoder extends BaseProtocolDecoder {
.setSecond(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
- position.setLatitude(readCoordinate(buf));
- position.setLongitude(readCoordinate(buf));
+ position.setLatitude(BufferUtil.readSignedMagnitudeInt(buf) / 1800000.0);
+ position.setLongitude(BufferUtil.readSignedMagnitudeInt(buf) / 1800000.0);
BcdUtil.readInteger(buf, 4); // reserved
position.setCourse(BcdUtil.readInteger(buf, 4));
diff --git a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java
index bbb8bc1cc..4ed2bb51d 100644
--- a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java
@@ -22,6 +22,7 @@ import io.netty.channel.Channel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.traccar.BaseHttpProtocolDecoder;
+import org.traccar.helper.BufferUtil;
import org.traccar.session.DeviceSession;
import org.traccar.Protocol;
import org.traccar.helper.BitUtil;
@@ -159,18 +160,8 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder {
if (event == 0x0f || event == 0x1f) {
position.setValid(event >> 4 > 0);
-
- long value;
- value = buf.readUnsignedInt();
- position.setLatitude(BitUtil.to(value, 31) * 0.000001);
- if (BitUtil.check(value, 31)) {
- position.setLatitude(-position.getLatitude());
- }
- value = buf.readUnsignedInt();
- position.setLongitude(BitUtil.to(value, 31) * 0.000001);
- if (BitUtil.check(value, 31)) {
- position.setLongitude(-position.getLongitude());
- }
+ position.setLatitude(BufferUtil.readSignedMagnitudeInt(buf) * 0.000001);
+ position.setLongitude(BufferUtil.readSignedMagnitudeInt(buf) * 0.000001);
position.set(Position.KEY_BATTERY, (int) buf.readUnsignedByte());
diff --git a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java
index 67a82a688..938d290c0 100644
--- a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java
@@ -21,6 +21,7 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.config.Keys;
+import org.traccar.helper.BufferUtil;
import org.traccar.helper.model.AttributeUtil;
import org.traccar.session.DeviceSession;
import org.traccar.Protocol;
@@ -673,19 +674,11 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder {
}
if (BitUtil.check(mask, 11)) {
- long value = buf.readUnsignedInt();
- if (BitUtil.check(value, 31)) {
- value = -BitUtil.to(value, 31);
- }
- position.setLatitude(value / 1000000.0);
+ position.setLatitude(BufferUtil.readSignedMagnitudeInt(buf) / 1000000.0);
}
if (BitUtil.check(mask, 12)) {
- long value = buf.readUnsignedInt();
- if (BitUtil.check(value, 31)) {
- value = -BitUtil.to(value, 31);
- }
- position.setLongitude(value / 1000000.0);
+ position.setLongitude(BufferUtil.readSignedMagnitudeInt(buf) / 1000000.0);
}
if (BitUtil.check(mask, 13)) {
diff --git a/src/main/java/org/traccar/protocol/ThinkPowerProtocolDecoder.java b/src/main/java/org/traccar/protocol/ThinkPowerProtocolDecoder.java
index 085ce4c91..26d60daba 100644
--- a/src/main/java/org/traccar/protocol/ThinkPowerProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/ThinkPowerProtocolDecoder.java
@@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
+import org.traccar.helper.BitUtil;
import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
diff --git a/src/test/java/org/traccar/helper/BufferUtilTest.java b/src/test/java/org/traccar/helper/BufferUtilTest.java
index b539b5b28..0196cef9d 100644
--- a/src/test/java/org/traccar/helper/BufferUtilTest.java
+++ b/src/test/java/org/traccar/helper/BufferUtilTest.java
@@ -11,6 +11,12 @@ import static org.junit.Assert.assertEquals;
public class BufferUtilTest {
@Test
+ public void testReadSignedMagnitudeInt() {
+ ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex("80000001"));
+ assertEquals(-1, BufferUtil.readSignedMagnitudeInt(buf));
+ }
+
+ @Test
public void test1() {
ByteBuf buf = Unpooled.copiedBuffer("abcdef", StandardCharsets.US_ASCII);
assertEquals(2, BufferUtil.indexOf("cd", buf));