From d9885b967f353a44d378ddcf2d8332620088ecb4 Mon Sep 17 00:00:00 2001 From: Maks Verver Date: Sat, 13 Oct 2018 15:01:42 +0200 Subject: [PATCH] Create overloads of Converters.toInt* that write to an existing buffer. This is symmetric with Converters.from*Int* which take a buffer and offset too. --- .../bluetooth/BluetoothTrisaBodyAnalyze.java | 12 +++--- .../openscale/core/utils/Converters.java | 40 +++++++++++++------ .../com/health/openscale/ConvertersTest.java | 21 +++++++++- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothTrisaBodyAnalyze.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothTrisaBodyAnalyze.java index 6e846e5c..78bd03be 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothTrisaBodyAnalyze.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothTrisaBodyAnalyze.java @@ -26,6 +26,7 @@ import com.health.openscale.R; import com.health.openscale.core.OpenScale; import com.health.openscale.core.datatypes.ScaleMeasurement; import com.health.openscale.core.datatypes.ScaleUser; +import com.health.openscale.core.utils.Converters; import java.util.UUID; @@ -275,13 +276,10 @@ public class BluetoothTrisaBodyAnalyze extends BluetoothCommunication { * encoded in little-endian byte order.

*/ private void writeCommand(byte commandByte, int argument) { - writeCommandBytes(new byte[]{ - commandByte, - (byte) (argument >> 0), - (byte) (argument >> 8), - (byte) (argument >> 16), - (byte) (argument >> 24), - }); + byte[] bytes = new byte[5]; + bytes[0] = commandByte; + Converters.toInt32Le(bytes, 1, argument); + writeCommandBytes(bytes); } private void writeCommandBytes(byte[] bytes) { diff --git a/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java b/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java index 01c2b31a..8461d920 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java +++ b/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java @@ -247,17 +247,25 @@ public class Converters { return value; } + public static void toInt16Le(byte[] data, int offset, int value) { + data[offset + 0] = (byte) (value & 0xFF); + data[offset + 1] = (byte) ((value >> 8) & 0xFF); + } + + public static void toInt16Be(byte[] data, int offset, int value) { + data[offset + 0] = (byte) ((value >> 8) & 0xFF); + data[offset + 1] = (byte) (value & 0xFF); + } + public static byte[] toInt16Le(int value) { byte[] data = new byte[2]; - data[0] = (byte) (value & 0xFF); - data[1] = (byte) ((value >> 8) & 0xFF); + toInt16Le(data, 0, value); return data; } public static byte[] toInt16Be(int value) { byte[] data = new byte[2]; - data[0] = (byte) ((value >> 8) & 0xFF); - data[1] = (byte) (value & 0xFF); + toInt16Be(data, 0, value); return data; } @@ -284,21 +292,29 @@ public class Converters { return value; } + public static void toInt32Le(byte[] data, int offset, long value) { + data[offset + 3] = (byte) ((value >> 24) & 0xFF); + data[offset + 2] = (byte) ((value >> 16) & 0xFF); + data[offset + 1] = (byte) ((value >> 8) & 0xFF); + data[offset + 0] = (byte) (value & 0xFF); + } + + public static void toInt32Be(byte[] data, int offset, long value) { + data[offset + 0] = (byte) ((value >> 24) & 0xFF); + data[offset + 1] = (byte) ((value >> 16) & 0xFF); + data[offset + 2] = (byte) ((value >> 8) & 0xFF); + data[offset + 3] = (byte) (value & 0xFF); + } + public static byte[] toInt32Le(long value) { byte[] data = new byte[4]; - data[3] = (byte) ((value >> 24) & 0xFF); - data[2] = (byte) ((value >> 16) & 0xFF); - data[1] = (byte) ((value >> 8) & 0xFF); - data[0] = (byte) (value & 0xFF); + toInt32Le(data, 0, value); return data; } public static byte[] toInt32Be(long value) { byte[] data = new byte[4]; - data[0] = (byte) ((value >> 24) & 0xFF); - data[1] = (byte) ((value >> 16) & 0xFF); - data[2] = (byte) ((value >> 8) & 0xFF); - data[3] = (byte) (value & 0xFF); + toInt32Be(data, 0, value); return data; } } diff --git a/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java b/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java index f90ac557..dd102e8b 100644 --- a/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java +++ b/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java @@ -20,6 +20,8 @@ import com.health.openscale.core.utils.Converters; import org.junit.Test; +import javax.sql.ConnectionEvent; + import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; @@ -102,6 +104,12 @@ public class ConvertersTest { assertArrayEquals(new byte[]{(byte) 0xff, (byte) 0xfe}, Converters.toInt16Be(0xfffe)); assertArrayEquals(new byte[]{(byte) 0x34, (byte) 0x12}, Converters.toInt16Le(0x1234)); assertArrayEquals(new byte[]{(byte) 0xfe, (byte) 0xff}, Converters.toInt16Le(0xfffe)); + + byte[] data = new byte[6]; + Converters.toInt16Be(data, 1, 0x0102); + Converters.toInt16Be(data, 3, 0x0304); + Converters.toInt16Le(data, 2, 0x0506); + assertArrayEquals(new byte[]{ 0, 1, 6, 5, 4, 0}, data); } @Test @@ -114,7 +122,7 @@ public class ConvertersTest { } @Test - public void unsignedInt32Converters() throws Exception { + public void fromUnsignedInt32Converters() throws Exception { byte[] data = new byte[]{(byte) 0xf1, (byte) 0xf2, (byte) 0xf3, (byte) 0x7f, (byte) 0x7e}; assertEquals(0x7ff3f2f1, Converters.fromUnsignedInt32Le(data, 0)); @@ -130,8 +138,11 @@ public class ConvertersTest { assertEquals(0x800001ffL, Converters.fromUnsignedInt32Be(data, 0)); assertEquals(0x1ff00L, Converters.fromUnsignedInt32Be(data, 1)); + } - data = new byte[]{(byte) 0xff, (byte) 0xfe, (byte) 0xfd, (byte) 0xfc}; + @Test + public void toInt32Converters() throws Exception { + byte[] data = new byte[]{(byte) 0xff, (byte) 0xfe, (byte) 0xfd, (byte) 0xfc}; assertArrayEquals(data, Converters.toInt32Le(0xfcfdfeffL)); assertArrayEquals(data, Converters.toInt32Be(0xfffefdfcL)); assertEquals(0xffffffffL, @@ -140,5 +151,11 @@ public class ConvertersTest { assertEquals(0xffffffffL, Converters.fromUnsignedInt32Be( Converters.toInt32Be(0xffffffffL), 0)); + + data = new byte[10]; + Converters.toInt32Be(data, 1, 0x01020304); + Converters.toInt32Be(data, 6, 0x05060708); + Converters.toInt32Le(data, 3, 0x090a0b0c); + assertArrayEquals(new byte[]{ 0, 1, 2, 12, 11, 10, 9, 6, 7, 8}, data); } }