store pairing key per pump
This commit is contained in:
parent
8bbee37b70
commit
97f69c50fd
6 changed files with 142 additions and 112 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -706,7 +706,7 @@
|
|||
<string name="pairingok">Paring OK</string>
|
||||
<string name="pairingtimedout">Paring timed out</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_name" translatable="false">danars_name</string>
|
||||
<string name="danars_nodeviceavailable">No Device Available</string>
|
||||
|
@ -732,5 +732,6 @@
|
|||
<string name="gettingtempbasalstatus">Getting temporary basal status</string>
|
||||
<string name="gettingpumpsettings">Gettings pump settings</string>
|
||||
<string name="gettingpumptime">Getting pump time</string>
|
||||
<string name="unsupportedpumpfirmware">Unsupported pump firmware</string>
|
||||
</resources>
|
||||
|
||||
|
|
Loading…
Reference in a new issue