Add GetVersionCommand

This commit is contained in:
Bart Sopers 2021-02-10 01:22:53 +01:00
parent fc4065b71c
commit f1d095d9c8
4 changed files with 142 additions and 2 deletions

View file

@ -1,10 +1,70 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.util.CrcUtil;
abstract class CommandBase implements Command {
CommandBase(CommandType commandType) {
final CommandType commandType;
final short sequenceNumber;
CommandBase(CommandType commandType, short sequenceNumber) {
this.commandType = commandType;
this.sequenceNumber = sequenceNumber;
}
@Override public CommandType getCommandType() {
return null;
return commandType;
}
public short getSequenceNumber() {
return sequenceNumber;
}
static byte[] formatCommand(byte[] command) {
List<Byte> temp = new ArrayList<>();
byte[] prefix = "S0.0=".getBytes(StandardCharsets.UTF_8);
for (byte b : prefix) {
temp.add(b);
}
byte[] length = ByteBuffer.allocate(2).putShort((short) command.length).array();
for (int i = 0; i < 2; i++) {
temp.add(length[i]);
}
// Append command
for (byte b : command) {
temp.add(b);
}
byte[] suffix = ",G0.0".getBytes(StandardCharsets.UTF_8);
for (byte b : suffix) {
temp.add(b);
}
byte[] out = new byte[((short) temp.size())];
for (int i2 = 0; i2 < temp.size(); i2++) {
out[i2] = temp.get(i2);
}
return out;
}
static byte[] appendCrc(byte[] command) {
return ByteBuffer.allocate(command.length + 2) //
.put(command) //
.putShort(CrcUtil.createCrc(command)) //
.array();
}
static byte[] encodeHeader(int address, short sequenceNumber, short length, boolean unknown) {
return ByteBuffer.allocate(6) //
.putInt(address) //
.putShort((short) (((sequenceNumber & 15) << 10) | length | (((unknown ? 1 : 0) & 1) << 15))) //
.array();
}
}

View file

@ -0,0 +1,31 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command;
import java.nio.ByteBuffer;
public class GetVersionCommand extends CommandBase {
private static final int DEFAULT_ADDRESS = -1;
private static final short LENGTH = 6;
private static final byte BODY_LENGTH = 4;
private final int address;
private final boolean unknown;
public GetVersionCommand(short sequenceNumber, boolean unknown) {
this(sequenceNumber, DEFAULT_ADDRESS, unknown);
}
public GetVersionCommand(short sequenceNumber, int address, boolean unknown) {
super(CommandType.GET_VERSION, sequenceNumber);
this.address = address;
this.unknown = unknown;
}
@Override public byte[] getEncoded() {
return appendCrc(ByteBuffer.allocate(12) //
.put(encodeHeader(address, sequenceNumber, LENGTH, unknown)) //
.put(commandType.getValue()) //
.put(BODY_LENGTH) //
.putInt(address) //
.array());
}
}

View file

@ -0,0 +1,32 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.util;
public class CrcUtil {
private static final short[] crc16table = {0, -32763, -32753, 10, -32741, 30, 20, -32751, -32717, 54, 60, -32711, 40, -32723, -32729, 34, -32669, 102, 108, -32663, 120, -32643, -32649, 114, 80, -32683, -32673, 90, -32693, 78, 68, -32703, -32573, 198, 204, -32567, 216, -32547, -32553, 210, 240, -32523, -32513, 250, -32533, 238, 228, -32543, 160, -32603, -32593, 170, -32581, 190, 180, -32591, -32621, 150, 156, -32615, 136, -32627, -32633, 130, -32381, 390, 396, -32375, 408, -32355, -32361, 402, 432, -32331, -32321, 442, -32341, 430, 420, -32351, 480, -32283, -32273, 490, -32261, 510, 500, -32271, -32301, 470, 476, -32295, 456, -32307, -32313, 450, 320, -32443, -32433, 330, -32421, 350, 340, -32431, -32397, 374, 380, -32391, 360, -32403, -32409, 354, -32477, 294, 300, -32471, 312, -32451, -32457, 306, 272, -32491, -32481, 282, -32501, 270, 260, -32511, -31997, 774, 780, -31991, 792, -31971, -31977, 786, 816, -31947, -31937, 826, -31957, 814, 804, -31967, 864, -31899, -31889, 874, -31877, 894, 884, -31887, -31917, 854, 860, -31911, 840, -31923, -31929, 834, 960, -31803, -31793, 970, -31781, 990, 980, -31791, -31757, 1014, 1020, -31751, 1000, -31763, -31769, 994, -31837, 934, 940, -31831, 952, -31811, -31817, 946, 912, -31851, -31841, 922, -31861, 910, 900, -31871, 640, -32123, -32113, 650, -32101, 670, 660, -32111, -32077, 694, 700, -32071, 680, -32083, -32089, 674, -32029, 742, 748, -32023, 760, -32003, -32009, 754, 720, -32043, -32033, 730, -32053, 718, 708, -32063, -32189, 582, 588, -32183, 600, -32163, -32169, 594, 624, -32139, -32129, 634, -32149, 622, 612, -32159, 544, -32219, -32209, 554, -32197, 574, 564, -32207, -32237, 534, 540, -32231, 520, -32243, -32249, 514};
public static int createCrc(short[] sArr) {
int i = 0;
for (short s = 0; s < sArr.length; s = (short) (s + 1)) {
byte b = (byte) sArr[s];
short s2 = b;
if (b < 0) {
s2 = (short) (((byte) (b & Byte.MAX_VALUE)) + 128);
}
i += s2;
}
return i;
}
public static short createCrc(byte[] bArr) {
short s = 0;
for (byte b : bArr) {
byte b2 = (byte) (b ^ (s & 255));
short s2 = b2;
if (b2 < 0) {
s2 = (short) (((byte) (b2 & Byte.MAX_VALUE)) + 128);
}
s = (short) (((short) (((short) (s >> 8)) & 255)) ^ crc16table[s2]);
}
return s;
}
}

View file

@ -0,0 +1,17 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class GetVersionCommandTest {
@Test
public void testEncoding() throws DecoderException {
byte[] encoded = new GetVersionCommand((short) 0, false) //
.getEncoded();
assertArrayEquals(Hex.decodeHex("FFFFFFFF00060704FFFFFFFF82B2"), encoded);
}
}