Skip to content
Snippets Groups Projects
Commit 971143cc authored by Volker Richert's avatar Volker Richert
Browse files

refactoring requestLinkKey

parent e5c2d9ee
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,7 @@ import info.nightscout.android.medtronic.message.PumpStatusResponseMessage; ...@@ -38,6 +38,7 @@ import info.nightscout.android.medtronic.message.PumpStatusResponseMessage;
import info.nightscout.android.medtronic.message.PumpTimeRequestMessage; import info.nightscout.android.medtronic.message.PumpTimeRequestMessage;
import info.nightscout.android.medtronic.message.PumpTimeResponseMessage; import info.nightscout.android.medtronic.message.PumpTimeResponseMessage;
import info.nightscout.android.medtronic.message.ReadInfoResponseMessage; import info.nightscout.android.medtronic.message.ReadInfoResponseMessage;
import info.nightscout.android.medtronic.message.RequestLinkKeyRequestMessage;
import info.nightscout.android.medtronic.message.RequestLinkKeyResponseMessage; import info.nightscout.android.medtronic.message.RequestLinkKeyResponseMessage;
import info.nightscout.android.medtronic.message.UnexpectedMessageException; import info.nightscout.android.medtronic.message.UnexpectedMessageException;
import info.nightscout.android.model.medtronicNg.PumpStatusEvent; import info.nightscout.android.model.medtronicNg.PumpStatusEvent;
...@@ -251,7 +252,6 @@ public class MedtronicCnlReader implements ContourNextLinkMessageHandler { ...@@ -251,7 +252,6 @@ public class MedtronicCnlReader implements ContourNextLinkMessageHandler {
public void requestReadInfo() throws IOException, TimeoutException, EncryptionException, ChecksumException { public void requestReadInfo() throws IOException, TimeoutException, EncryptionException, ChecksumException {
Log.d(TAG, "Begin requestReadInfo"); Log.d(TAG, "Begin requestReadInfo");
ReadInfoResponseMessage response = new ReadInfoRequestMessage(mPumpSession).send(mDevice); ReadInfoResponseMessage response = new ReadInfoRequestMessage(mPumpSession).send(mDevice);
long linkMAC = response.getLinkMAC(); long linkMAC = response.getLinkMAC();
...@@ -264,18 +264,9 @@ public class MedtronicCnlReader implements ContourNextLinkMessageHandler { ...@@ -264,18 +264,9 @@ public class MedtronicCnlReader implements ContourNextLinkMessageHandler {
public void requestLinkKey() throws IOException, TimeoutException, EncryptionException, ChecksumException { public void requestLinkKey() throws IOException, TimeoutException, EncryptionException, ChecksumException {
Log.d(TAG, "Begin requestLinkKey"); Log.d(TAG, "Begin requestLinkKey");
new ContourNextLinkBinaryMessage(ContourNextLinkBinaryMessage.CommandType.REQUEST_LINK_KEY, mPumpSession, null).send(this);
ContourNextLinkMessage response = RequestLinkKeyResponseMessage.fromBytes(mPumpSession, readMessage());
// FIXME - this needs to go into RequestLinkKeyResponseMessage
ByteBuffer infoBuffer = ByteBuffer.allocate(55);
infoBuffer.order(ByteOrder.BIG_ENDIAN);
infoBuffer.put(response.encode(), 0x21, 55);
byte[] packedLinkKey = infoBuffer.array();
this.getPumpSession().setPackedLinkKey(packedLinkKey); RequestLinkKeyResponseMessage response = new RequestLinkKeyRequestMessage(mPumpSession).send(mDevice);
this.getPumpSession().setKey(response.getKey());
Log.d(TAG, String.format("Finished requestLinkKey. linkKey = '%s'", this.getPumpSession().getKey())); Log.d(TAG, String.format("Finished requestLinkKey. linkKey = '%s'", this.getPumpSession().getKey()));
} }
......
...@@ -100,26 +100,6 @@ public class MedtronicCnlSession { ...@@ -100,26 +100,6 @@ public class MedtronicCnlSession {
this.key = key; this.key = key;
} }
public void setPackedLinkKey(byte[] packedLinkKey) {
this.key = new byte[16];
int pos = this.stickSerial.charAt(this.stickSerial.length() - 1) & 7;
for (int i = 0; i < this.key.length; i++) {
if ((packedLinkKey[pos + 1] & 1) == 1) {
this.key[i] = (byte) ~packedLinkKey[pos];
} else {
this.key[i] = packedLinkKey[pos];
}
if (((packedLinkKey[pos + 1] >> 1) & 1) == 0) {
pos += 3;
} else {
pos += 2;
}
}
}
public String getStickSerial() { public String getStickSerial() {
return stickSerial; return stickSerial;
} }
......
package info.nightscout.android.medtronic.message;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import info.nightscout.android.USB.UsbHidDriver;
import info.nightscout.android.medtronic.MedtronicCnlSession;
/**
* Created by volker on 10.12.2016.
*/
public class RequestLinkKeyRequestMessage extends ContourNextLinkBinaryMessage {
public RequestLinkKeyRequestMessage(MedtronicCnlSession pumpSession) throws ChecksumException {
super(CommandType.REQUEST_LINK_KEY, pumpSession, null);
}
public RequestLinkKeyResponseMessage send(UsbHidDriver mDevice) throws IOException, TimeoutException, EncryptionException, ChecksumException {
sendMessage(mDevice);
RequestLinkKeyResponseMessage response = new RequestLinkKeyResponseMessage(mPumpSession, readMessage(mDevice));
return response;
}
}
package info.nightscout.android.medtronic.message; package info.nightscout.android.medtronic.message;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import info.nightscout.android.medtronic.MedtronicCnlSession; import info.nightscout.android.medtronic.MedtronicCnlSession;
/** /**
* Created by lgoedhart on 10/05/2016. * Created by lgoedhart on 10/05/2016.
*/ */
public class RequestLinkKeyResponseMessage extends MedtronicResponseMessage { public class RequestLinkKeyResponseMessage extends MedtronicResponseMessage {
private byte[] key;
protected RequestLinkKeyResponseMessage(MedtronicCnlSession pumpSession, byte[] payload) throws EncryptionException, ChecksumException { protected RequestLinkKeyResponseMessage(MedtronicCnlSession pumpSession, byte[] payload) throws EncryptionException, ChecksumException {
super(pumpSession, payload); super(pumpSession, payload);
ByteBuffer infoBuffer = ByteBuffer.allocate(55);
infoBuffer.order(ByteOrder.BIG_ENDIAN);
infoBuffer.put(this.encode(), 0x21, 55);
setPackedLinkKey(infoBuffer.array());
} }
public static ContourNextLinkMessage fromBytes(MedtronicCnlSession pumpSession, byte[] bytes) throws ChecksumException, EncryptionException { public static ContourNextLinkMessage fromBytes(MedtronicCnlSession pumpSession, byte[] bytes) throws ChecksumException, EncryptionException {
...@@ -18,4 +30,28 @@ public class RequestLinkKeyResponseMessage extends MedtronicResponseMessage { ...@@ -18,4 +30,28 @@ public class RequestLinkKeyResponseMessage extends MedtronicResponseMessage {
return message; return message;
} }
public byte[] getKey() {
return key;
}
private void setPackedLinkKey(byte[] packedLinkKey) {
this.key = new byte[16];
int pos = mPumpSession.getStickSerial().charAt(mPumpSession.getStickSerial().length() - 1) & 7;
for (int i = 0; i < this.key.length; i++) {
if ((packedLinkKey[pos + 1] & 1) == 1) {
this.key[i] = (byte) ~packedLinkKey[pos];
} else {
this.key[i] = packedLinkKey[pos];
}
if (((packedLinkKey[pos + 1] >> 1) & 1) == 0) {
pos += 3;
} else {
pos += 2;
}
}
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment