diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponse.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponse.java index 9b3749d1de..c62add6732 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponse.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponse.java @@ -7,45 +7,51 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.MessageBlockType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodProgressStatus; public class VersionResponse extends MessageBlock { + private static final int ASSIGN_ADDRESS_VERSION_RESPONSE_LENGTH = 0x15; + private static final int SETUP_POD_VERSION_RESPONSE_LENGTH = 0x1b; + private final PodProgressStatus podProgressStatus; private final FirmwareVersion pmVersion; private final FirmwareVersion piVersion; private final int lot; private final int tid; + private Byte gain; // Only in the assign address version response + private Byte rssi; // Only in the assign address version response private final int address; - public VersionResponse(byte[] encodedData) { - int length = ByteUtil.convertUnsignedByteToInt(encodedData[1]) + 2; - this.encodedData = ByteUtil.substring(encodedData, 2, length - 2); - - boolean extraByte; - byte[] truncatedData; + public VersionResponse(byte[] data) { + int length = ByteUtil.convertUnsignedByteToInt(data[1]); + this.encodedData = ByteUtil.substring(data, 2, length); switch (length) { - case 0x17: - truncatedData = ByteUtil.substring(encodedData, 2); - extraByte = true; + case ASSIGN_ADDRESS_VERSION_RESPONSE_LENGTH: + podProgressStatus = PodProgressStatus.fromByte(data[9]); + pmVersion = new FirmwareVersion(data[2], data[3], data[4]); + piVersion = new FirmwareVersion(data[5], data[6], data[7]); + lot = ByteUtil.toInt((int) data[10], (int) data[11], + (int) data[12], (int) data[13], ByteUtil.BitConversion.BIG_ENDIAN); + tid = ByteUtil.toInt((int) data[14], (int) data[15], + (int) data[16], (int) data[17], ByteUtil.BitConversion.BIG_ENDIAN); + gain = (byte) ((data[18] & 0xc0) >>> 6); + rssi = (byte) (data[18] & 0x3f); + address = ByteUtil.toInt((int) data[19], (int) data[20], + (int) data[21], (int) data[22], ByteUtil.BitConversion.BIG_ENDIAN); break; - case 0x1D: - truncatedData = ByteUtil.substring(encodedData, 9); - extraByte = false; + case SETUP_POD_VERSION_RESPONSE_LENGTH: + podProgressStatus = PodProgressStatus.fromByte(data[16]); + pmVersion = new FirmwareVersion(data[9], data[10], data[11]); + piVersion = new FirmwareVersion(data[12], data[13], data[14]); + lot = ByteUtil.toInt((int) data[17], (int) data[18], + (int) data[19], (int) data[20], ByteUtil.BitConversion.BIG_ENDIAN); + tid = ByteUtil.toInt((int) data[21], (int) data[22], + (int) data[23], (int) data[24], ByteUtil.BitConversion.BIG_ENDIAN); + + address = ByteUtil.toInt((int) data[25], (int) data[26], + (int) data[27], (int) data[28], ByteUtil.BitConversion.BIG_ENDIAN); break; default: throw new IllegalArgumentException("Unrecognized VersionResponse message length: " + length); } - - podProgressStatus = PodProgressStatus.fromByte(truncatedData[7]); - pmVersion = new FirmwareVersion(truncatedData[0], truncatedData[1], truncatedData[2]); - piVersion = new FirmwareVersion(truncatedData[3], truncatedData[4], truncatedData[5]); - lot = ByteUtil.toInt((int) truncatedData[8], (int) truncatedData[9], - (int) truncatedData[10], (int) truncatedData[11], ByteUtil.BitConversion.BIG_ENDIAN); - tid = ByteUtil.toInt((int) truncatedData[12], (int) truncatedData[13], - (int) truncatedData[14], (int) truncatedData[15], ByteUtil.BitConversion.BIG_ENDIAN); - - int indexIncrement = extraByte ? 1 : 0; - - address = ByteUtil.toInt((int) truncatedData[16 + indexIncrement], (int) truncatedData[17 + indexIncrement], - (int) truncatedData[18 + indexIncrement], (int) truncatedData[19 + indexIncrement], ByteUtil.BitConversion.BIG_ENDIAN); } @Override @@ -73,6 +79,22 @@ public class VersionResponse extends MessageBlock { return tid; } + public Byte getGain() { + return gain; + } + + public Byte getRssi() { + return rssi; + } + + public boolean isAssignAddressVersionResponse() { + return encodedData.length == ASSIGN_ADDRESS_VERSION_RESPONSE_LENGTH; + } + + public boolean isSetupPodVersionResponse() { + return encodedData.length == SETUP_POD_VERSION_RESPONSE_LENGTH; + } + public int getAddress() { return address; } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponseTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponseTest.java index 62afe4f090..891ad81329 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponseTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/comm/message/response/VersionResponseTest.java @@ -7,10 +7,14 @@ import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodProgressStatus; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class VersionResponseTest { @Test - public void testRawDataShortResponse() { + public void testRawDataAssignAddressVersionResponse() { byte[] encodedData = ByteUtil.fromHexString("011502070002070002020000a64000097c279c1f08ced2"); VersionResponse versionResponse = new VersionResponse(encodedData); @@ -18,7 +22,7 @@ public class VersionResponseTest { } @Test - public void testRawDataShortResponseWithLongerMessage() { + public void testRawDataAssignAddressVersionResponseWithLongerMessage() { byte[] encodedData = ByteUtil.fromHexString("011502070002070002020000a64000097c279c1f08ced201"); byte[] expected = ByteUtil.fromHexString("011502070002070002020000a64000097c279c1f08ced2"); @@ -27,7 +31,7 @@ public class VersionResponseTest { } @Test - public void testRawDataLongResponse() { + public void testRawDataSetupPodVersionResponse() { byte[] encodedData = ByteUtil.fromHexString("011b13881008340a5002070002070002030000a3770003ab371f00ee87"); VersionResponse versionResponse = new VersionResponse(encodedData); @@ -36,7 +40,7 @@ public class VersionResponseTest { } @Test - public void testRawDataLongResponseWithLongerMessage() { + public void testRawDataSetupPodVersionResponseWithLongerMessage() { byte[] encodedData = ByteUtil.fromHexString("011b13881008340a5002070002070002030000a3770003ab371f00ee8701"); byte[] expected = ByteUtil.fromHexString("011b13881008340a5002070002070002030000a3770003ab371f00ee87"); @@ -45,25 +49,33 @@ public class VersionResponseTest { } @Test - public void testVersionResponse() { + public void testAssignAddressVersionResponse() { VersionResponse versionResponse = new VersionResponse(ByteUtil.fromHexString("011502070002070002020000a64000097c279c1f08ced2")); + assertTrue(versionResponse.isAssignAddressVersionResponse()); + assertFalse(versionResponse.isSetupPodVersionResponse()); assertEquals(0x1f08ced2, versionResponse.getAddress()); assertEquals(42560, versionResponse.getLot()); assertEquals(621607, versionResponse.getTid()); assertEquals("2.7.0", versionResponse.getPiVersion().toString()); assertEquals("2.7.0", versionResponse.getPmVersion().toString()); + assertNotNull(versionResponse.getRssi()); + assertNotNull(versionResponse.getGain()); } @Test - public void testLongVersionResponse() { + public void testSetupPodVersionResponse() { VersionResponse versionResponse = new VersionResponse(ByteUtil.fromHexString("011b13881008340a5002070002070002030000a3770003ab371f00ee87")); + assertFalse(versionResponse.isAssignAddressVersionResponse()); + assertTrue(versionResponse.isSetupPodVersionResponse()); assertEquals(0x1f00ee87, versionResponse.getAddress()); assertEquals(41847, versionResponse.getLot()); assertEquals(240439, versionResponse.getTid()); assertEquals(PodProgressStatus.PAIRING_SUCCESS, versionResponse.getPodProgressStatus()); assertEquals("2.7.0", versionResponse.getPiVersion().toString()); assertEquals("2.7.0", versionResponse.getPmVersion().toString()); + assertNull(versionResponse.getRssi()); + assertNull(versionResponse.getGain()); } }