store pairing key per pump

This commit is contained in:
Milos Kozak 2017-10-02 22:21:11 +02:00
parent 8bbee37b70
commit 97f69c50fd
6 changed files with 142 additions and 112 deletions

View file

@ -51,6 +51,7 @@ public class Notification {
public static final int TOAST_ALARM = 22; public static final int TOAST_ALARM = 22;
public static final int WRONGBASALSTEP = 23; public static final int WRONGBASALSTEP = 23;
public static final int BOLUS_DELIVERY_ERROR = 24; public static final int BOLUS_DELIVERY_ERROR = 24;
public static final int UNSUPPORTED_FIRMWARE = 25;
public int id; public int id;

View file

@ -232,7 +232,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface,
} }
pumpDescription.basalStep = pump.basalStep; pumpDescription.basalStep = pump.basalStep;
pumpDescription.bolusStep = pump.bolusStep; pumpDescription.bolusStep = pump.bolusStep;
log.debug("RS connected"); log.debug("RS connected:" + from);
} }
} }

View file

@ -3,49 +3,51 @@ package info.nightscout.androidaps.plugins.PumpDanaRS.comm;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.os.Build; import android.os.Build;
import com.cozmo.danar.util.BleCommandUtil;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import com.cozmo.danar.util.BleCommandUtil;
public class DanaRS_Packet { public class DanaRS_Packet {
protected static final int TYPE_START = 0; protected static final int TYPE_START = 0;
protected static final int OPCODE_START = 1; protected static final int OPCODE_START = 1;
protected static final int DATA_START = 2; protected static final int DATA_START = 2;
private boolean received; private boolean received;
protected int type = BleCommandUtil.DANAR_PACKET__TYPE_RESPONSE; // most of the messages, should be changed for others protected int type = BleCommandUtil.DANAR_PACKET__TYPE_RESPONSE; // most of the messages, should be changed for others
protected int opCode; protected int opCode;
public DanaRS_Packet() { public DanaRS_Packet() {
received = false; received = false;
} }
public void setReceived() { public void setReceived() {
received = true; received = true;
} }
public boolean isReceived() { public boolean isReceived() {
return received; return received;
} }
public int getType() { public int getType() {
return type; return type;
} }
public int getOpCode() { public int getOpCode() {
return opCode; return opCode;
} }
public int getCommand() { public int getCommand() {
return ((type & 0xFF) << 8) + (opCode & 0xFF); return ((type & 0xFF) << 8) + (opCode & 0xFF);
} }
public byte[] getRequestParams() { public byte[] getRequestParams() {
return null; return null;
}; }
// STATIC FUNCTIONS ;
// STATIC FUNCTIONS
public static int getCommand(byte[] data) { public static int getCommand(byte[] data) {
int type = byteArrayToInt(getBytes(data, TYPE_START, 1)); int type = byteArrayToInt(getBytes(data, TYPE_START, 1));
@ -56,99 +58,112 @@ public class DanaRS_Packet {
public void handleMessage(byte[] data) { public void handleMessage(byte[] data) {
} }
public String getFriendlyName() { public String getFriendlyName() {
return "UNKNOWN_PACKET"; return "UNKNOWN_PACKET";
} }
protected static byte[] getBytes(byte[] data, int srcStart, int srcLength) { protected static byte[] getBytes(byte[] data, int srcStart, int srcLength) {
try { try {
byte[] ret = new byte[srcLength]; byte[] ret = new byte[srcLength];
System.arraycopy(data, srcStart, ret, 0, srcLength); System.arraycopy(data, srcStart, ret, 0, srcLength);
return ret; return ret;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
protected static int byteArrayToInt(byte[] b) { protected static int byteArrayToInt(byte[] b) {
int ret; int ret;
switch (b.length) { switch (b.length) {
case 1: case 1:
ret = b[0] & 0x000000FF; ret = b[0] & 0x000000FF;
break; break;
case 2: case 2:
ret = ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); ret = ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF);
break; break;
case 3: case 3:
ret = ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); ret = ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF);
break; break;
case 4: case 4:
ret = ((b[3] & 0x000000FF) << 24) + ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); ret = ((b[3] & 0x000000FF) << 24) + ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF);
break; break;
default: default:
ret = -1; ret = -1;
break; break;
} }
return ret; return ret;
} }
@TargetApi(Build.VERSION_CODES.KITKAT) @TargetApi(Build.VERSION_CODES.KITKAT)
public static String stringFromBuff(byte[] buff, int offset, int length) { public static String stringFromBuff(byte[] buff, int offset, int length) {
byte[] strbuff = new byte[length]; byte[] strbuff = new byte[length];
System.arraycopy(buff, offset, strbuff, 0, length); System.arraycopy(buff, offset, strbuff, 0, length);
return new String(strbuff, StandardCharsets.UTF_8); return new String(strbuff, StandardCharsets.UTF_8);
} }
public static Date dateFromBuff(byte[] buff, int offset) { public static Date dateFromBuff(byte[] buff, int offset) {
Date date = Date date =
new Date( new Date(
100 + byteArrayToInt(getBytes(buff, offset, 1)), 100 + byteArrayToInt(getBytes(buff, offset, 1)),
byteArrayToInt(getBytes(buff, offset + 1, 1)) - 1, byteArrayToInt(getBytes(buff, offset + 1, 1)) - 1,
byteArrayToInt(getBytes(buff, offset + 2, 1)) byteArrayToInt(getBytes(buff, offset + 2, 1))
); );
return date; return date;
} }
@TargetApi(Build.VERSION_CODES.KITKAT) @TargetApi(Build.VERSION_CODES.KITKAT)
public static String asciiStringFromBuff(byte[] buff, int offset, int length) {
byte[] strbuff = new byte[length];
System.arraycopy(buff, offset, strbuff, 0, length);
for (int pos = 0; pos < length; pos++)
strbuff[pos] += 65; // "A"
return new String(strbuff, StandardCharsets.UTF_8);
}
public static String toHexString(byte[] buff) { public static String asciiStringFromBuff(byte[] buff, int offset, int length) {
byte[] strbuff = new byte[length];
System.arraycopy(buff, offset, strbuff, 0, length);
for (int pos = 0; pos < length; pos++)
strbuff[pos] += 65; // "A"
return new String(strbuff, StandardCharsets.UTF_8);
}
public static String toHexString(byte[] buff) {
if (buff == null) if (buff == null)
return ""; return "";
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
int count = 0; int count = 0;
for (byte element : buff) { for (byte element : buff) {
sb.append(String.format("%02X ", element)); sb.append(String.format("%02X ", element));
if (++count % 4 == 0) sb.append(" "); if (++count % 4 == 0) sb.append(" ");
} }
return sb.toString(); return sb.toString();
} }
public static byte[] hexToBytes(String s) { final private static char[] hexArray = "0123456789ABCDEF".toCharArray();
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static int ByteToInt(byte b) { public static String bytesToHex(byte[] bytes) {
return b & 0x000000FF; char[] hexChars = new char[bytes.length * 2];
} for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static byte[] hexToBytes(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static int ByteToInt(byte b) {
return b & 0x000000FF;
}
} }

View file

@ -1,10 +1,16 @@
package info.nightscout.androidaps.plugins.PumpDanaRS.comm; package info.nightscout.androidaps.plugins.PumpDanaRS.comm;
import com.cozmo.danar.util.BleCommandUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import com.cozmo.danar.util.BleCommandUtil; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
public class DanaRS_Packet_General_Get_Pump_Check extends DanaRS_Packet { public class DanaRS_Packet_General_Get_Pump_Check extends DanaRS_Packet {
@ -36,6 +42,12 @@ public class DanaRS_Packet_General_Get_Pump_Check extends DanaRS_Packet {
log.debug("Protocol: " + String.format("%02X ", pump.protocol)); log.debug("Protocol: " + String.format("%02X ", pump.protocol));
log.debug("Product Code: " + String.format("%02X ", pump.productCode)); log.debug("Product Code: " + String.format("%02X ", pump.productCode));
} }
if (pump.protocol == 1) {
Notification notification = new Notification(Notification.UNSUPPORTED_FIRMWARE, MainApp.sResources.getString(R.string.unsupportedpumpfirmware), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
} else {
MainApp.bus().post(new EventDismissNotification(Notification.UNSUPPORTED_FIRMWARE));
}
} }
@Override @Override

View file

@ -31,6 +31,7 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingHelperActivity; import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingHelperActivity;
import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingProgressDialog; import info.nightscout.androidaps.plugins.PumpDanaRS.activities.PairingProgressDialog;
import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRSMessageHashTable; import info.nightscout.androidaps.plugins.PumpDanaRS.comm.DanaRSMessageHashTable;
@ -457,7 +458,7 @@ public class BLEComm {
if (inputBuffer.length == 4 && inputBuffer[2] == 'O' && inputBuffer[3] == 'K') { if (inputBuffer.length == 4 && inputBuffer[2] == 'O' && inputBuffer[3] == 'K') {
log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (OK)" + " " + DanaRS_Packet.toHexString(inputBuffer)); log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (OK)" + " " + DanaRS_Packet.toHexString(inputBuffer));
// Grab pairing key from preferences if exists // Grab pairing key from preferences if exists
String pairingKey = SP.getString(R.string.key_danars_pairingkey, null); String pairingKey = SP.getString(MainApp.sResources.getString(R.string.key_danars_pairingkey) + DanaRSPlugin.mDeviceName, null);
log.debug("Using stored pairing key: " + pairingKey); log.debug("Using stored pairing key: " + pairingKey);
if (pairingKey != null) { if (pairingKey != null) {
byte[] encodedPairingKey = DanaRS_Packet.hexToBytes(pairingKey); byte[] encodedPairingKey = DanaRS_Packet.hexToBytes(pairingKey);
@ -504,8 +505,8 @@ public class BLEComm {
SendTimeInfo(); SendTimeInfo();
byte[] pairingKey = {inputBuffer[2], inputBuffer[3]}; byte[] pairingKey = {inputBuffer[2], inputBuffer[3]};
// store pairing key to preferences // store pairing key to preferences
SP.putString(R.string.key_danars_pairingkey, DanaRS_Packet.toHexString(pairingKey)); SP.putString(MainApp.sResources.getString(R.string.key_danars_pairingkey) + DanaRSPlugin.mDeviceName, DanaRS_Packet.bytesToHex(pairingKey));
log.debug("Got pairing key: " + DanaRS_Packet.toHexString(pairingKey)); log.debug("Got pairing key: " + DanaRS_Packet.bytesToHex(pairingKey));
break; break;
// time and user password information. last packet in handshake // time and user password information. last packet in handshake
case (byte) BleCommandUtil.DANAR_PACKET__OPCODE_ENCRYPTION__TIME_INFORMATION: case (byte) BleCommandUtil.DANAR_PACKET__OPCODE_ENCRYPTION__TIME_INFORMATION:

View file

@ -706,7 +706,7 @@
<string name="pairingok">Paring OK</string> <string name="pairingok">Paring OK</string>
<string name="pairingtimedout">Paring timed out</string> <string name="pairingtimedout">Paring timed out</string>
<string name="pairing">PAIRING</string> <string name="pairing">PAIRING</string>
<string name="key_danars_pairingkey" translatable="false">danars_paring_key</string> <string name="key_danars_pairingkey" translatable="false">danars_paring_key_</string>
<string name="key_danars_address" translatable="false">danars_address</string> <string name="key_danars_address" translatable="false">danars_address</string>
<string name="key_danars_name" translatable="false">danars_name</string> <string name="key_danars_name" translatable="false">danars_name</string>
<string name="danars_nodeviceavailable">No Device Available</string> <string name="danars_nodeviceavailable">No Device Available</string>
@ -732,5 +732,6 @@
<string name="gettingtempbasalstatus">Getting temporary basal status</string> <string name="gettingtempbasalstatus">Getting temporary basal status</string>
<string name="gettingpumpsettings">Gettings pump settings</string> <string name="gettingpumpsettings">Gettings pump settings</string>
<string name="gettingpumptime">Getting pump time</string> <string name="gettingpumptime">Getting pump time</string>
<string name="unsupportedpumpfirmware">Unsupported pump firmware</string>
</resources> </resources>