From 97f69c50fd526f9b25d8b2536136692430cd30af Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 2 Oct 2017 22:21:11 +0200 Subject: [PATCH] store pairing key per pump --- .../plugins/Overview/Notification.java | 1 + .../plugins/PumpDanaRS/DanaRSPlugin.java | 2 +- .../PumpDanaRS/comm/DanaRS_Packet.java | 227 ++++++++++-------- .../DanaRS_Packet_General_Get_Pump_Check.java | 14 +- .../plugins/PumpDanaRS/services/BLEComm.java | 7 +- app/src/main/res/values/strings.xml | 3 +- 6 files changed, 142 insertions(+), 112 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java index 8ffca4515b..ee99c153ed 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java @@ -51,6 +51,7 @@ public class Notification { public static final int TOAST_ALARM = 22; public static final int WRONGBASALSTEP = 23; public static final int BOLUS_DELIVERY_ERROR = 24; + public static final int UNSUPPORTED_FIRMWARE = 25; public int id; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java index 9d45ff8132..1f3b99bd1a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java @@ -232,7 +232,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } pumpDescription.basalStep = pump.basalStep; pumpDescription.bolusStep = pump.bolusStep; - log.debug("RS connected"); + log.debug("RS connected:" + from); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet.java index eb9a91698a..18747b59ce 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet.java @@ -3,49 +3,51 @@ package info.nightscout.androidaps.plugins.PumpDanaRS.comm; import android.annotation.TargetApi; import android.os.Build; +import com.cozmo.danar.util.BleCommandUtil; + import java.nio.charset.StandardCharsets; import java.util.Date; -import com.cozmo.danar.util.BleCommandUtil; - public class DanaRS_Packet { - protected static final int TYPE_START = 0; - protected static final int OPCODE_START = 1; - protected static final int DATA_START = 2; + protected static final int TYPE_START = 0; + protected static final int OPCODE_START = 1; + protected static final int DATA_START = 2; - private boolean received; - protected int type = BleCommandUtil.DANAR_PACKET__TYPE_RESPONSE; // most of the messages, should be changed for others - protected int opCode; + private boolean received; + protected int type = BleCommandUtil.DANAR_PACKET__TYPE_RESPONSE; // most of the messages, should be changed for others + protected int opCode; - public DanaRS_Packet() { - received = false; - } + public DanaRS_Packet() { + received = false; + } - public void setReceived() { - received = true; - } + public void setReceived() { + received = true; + } - public boolean isReceived() { - return received; - } + public boolean isReceived() { + return received; + } - public int getType() { - return type; - } + public int getType() { + return type; + } - public int getOpCode() { - return opCode; - } + public int getOpCode() { + return opCode; + } - public int getCommand() { - return ((type & 0xFF) << 8) + (opCode & 0xFF); - } + public int getCommand() { + return ((type & 0xFF) << 8) + (opCode & 0xFF); + } - public byte[] getRequestParams() { - return null; - }; + public byte[] getRequestParams() { + return null; + } - // STATIC FUNCTIONS + ; + + // STATIC FUNCTIONS public static int getCommand(byte[] data) { int type = byteArrayToInt(getBytes(data, TYPE_START, 1)); @@ -56,99 +58,112 @@ public class DanaRS_Packet { public void handleMessage(byte[] data) { } - public String getFriendlyName() { + public String getFriendlyName() { return "UNKNOWN_PACKET"; } - protected static byte[] getBytes(byte[] data, int srcStart, int srcLength) { - try { - byte[] ret = new byte[srcLength]; + protected static byte[] getBytes(byte[] data, int srcStart, int srcLength) { + try { + byte[] ret = new byte[srcLength]; - System.arraycopy(data, srcStart, ret, 0, srcLength); + System.arraycopy(data, srcStart, ret, 0, srcLength); - return ret; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } + return ret; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } - protected static int byteArrayToInt(byte[] b) { - int ret; + protected static int byteArrayToInt(byte[] b) { + int ret; - switch (b.length) { - case 1: - ret = b[0] & 0x000000FF; - break; - case 2: - ret = ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); - break; - case 3: - ret = ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); - break; - case 4: - ret = ((b[3] & 0x000000FF) << 24) + ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); - break; - default: - ret = -1; - break; - } - return ret; - } + switch (b.length) { + case 1: + ret = b[0] & 0x000000FF; + break; + case 2: + ret = ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); + break; + case 3: + ret = ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); + break; + case 4: + ret = ((b[3] & 0x000000FF) << 24) + ((b[2] & 0x000000FF) << 16) + ((b[1] & 0x000000FF) << 8) + (b[0] & 0x000000FF); + break; + default: + ret = -1; + break; + } + return ret; + } - @TargetApi(Build.VERSION_CODES.KITKAT) - public static String stringFromBuff(byte[] buff, int offset, int length) { - byte[] strbuff = new byte[length]; - System.arraycopy(buff, offset, strbuff, 0, length); - return new String(strbuff, StandardCharsets.UTF_8); - } + @TargetApi(Build.VERSION_CODES.KITKAT) + public static String stringFromBuff(byte[] buff, int offset, int length) { + byte[] strbuff = new byte[length]; + System.arraycopy(buff, offset, strbuff, 0, length); + return new String(strbuff, StandardCharsets.UTF_8); + } - public static Date dateFromBuff(byte[] buff, int offset) { - Date date = - new Date( - 100 + byteArrayToInt(getBytes(buff, offset, 1)), - byteArrayToInt(getBytes(buff, offset + 1, 1)) - 1, - byteArrayToInt(getBytes(buff, offset + 2, 1)) - ); - return date; - } + public static Date dateFromBuff(byte[] buff, int offset) { + Date date = + new Date( + 100 + byteArrayToInt(getBytes(buff, offset, 1)), + byteArrayToInt(getBytes(buff, offset + 1, 1)) - 1, + byteArrayToInt(getBytes(buff, offset + 2, 1)) + ); + return date; + } - @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); - } + @TargetApi(Build.VERSION_CODES.KITKAT) - 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) return ""; - StringBuffer sb = new StringBuffer(); + StringBuffer sb = new StringBuffer(); - int count = 0; - for (byte element : buff) { - sb.append(String.format("%02X ", element)); - if (++count % 4 == 0) sb.append(" "); - } + int count = 0; + for (byte element : buff) { + sb.append(String.format("%02X ", element)); + if (++count % 4 == 0) sb.append(" "); + } - return sb.toString(); - } + return sb.toString(); + } - 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; - } + final private static char[] hexArray = "0123456789ABCDEF".toCharArray(); - public static int ByteToInt(byte b) { - return b & 0x000000FF; - } + public static String bytesToHex(byte[] bytes) { + 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; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Get_Pump_Check.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Get_Pump_Check.java index d82d6229b9..5d5fc8ee99 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Get_Pump_Check.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_General_Get_Pump_Check.java @@ -1,10 +1,16 @@ package info.nightscout.androidaps.plugins.PumpDanaRS.comm; +import com.cozmo.danar.util.BleCommandUtil; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; 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("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 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java index 872ea69fa7..abd7049364 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/BLEComm.java @@ -31,6 +31,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventPumpStatusChanged; 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.PairingProgressDialog; 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') { log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (OK)" + " " + DanaRS_Packet.toHexString(inputBuffer)); // 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); if (pairingKey != null) { byte[] encodedPairingKey = DanaRS_Packet.hexToBytes(pairingKey); @@ -504,8 +505,8 @@ public class BLEComm { SendTimeInfo(); byte[] pairingKey = {inputBuffer[2], inputBuffer[3]}; // store pairing key to preferences - SP.putString(R.string.key_danars_pairingkey, DanaRS_Packet.toHexString(pairingKey)); - log.debug("Got pairing key: " + 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.bytesToHex(pairingKey)); break; // time and user password information. last packet in handshake case (byte) BleCommandUtil.DANAR_PACKET__OPCODE_ENCRYPTION__TIME_INFORMATION: diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 36c174b2c4..0a37090d94 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -706,7 +706,7 @@ Paring OK Paring timed out PAIRING - danars_paring_key + danars_paring_key_ danars_address danars_name No Device Available @@ -732,5 +732,6 @@ Getting temporary basal status Gettings pump settings Getting pump time + Unsupported pump firmware