Proper handling of setCharacteristicNotification
This commit is contained in:
parent
baa4376f68
commit
f31f6c1649
|
@ -111,7 +111,7 @@ android {
|
|||
defaultConfig {
|
||||
multiDexEnabled true
|
||||
versionCode 1500
|
||||
version "3.2.0-dev-i"
|
||||
version "3.2.0-dev-i-medtrum"
|
||||
buildConfigField "String", "VERSION", '"' + version + '"'
|
||||
buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"'
|
||||
buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"'
|
||||
|
|
|
@ -90,6 +90,15 @@ class MedtrumPlugin @Inject constructor(
|
|||
.toObservable(EventAppExit::class.java)
|
||||
.observeOn(aapsSchedulers.io)
|
||||
.subscribe({ context.unbindService(mConnection) }, fabricPrivacy::logException)
|
||||
disposable += rxBus
|
||||
.toObservable(EventPreferenceChange::class.java)
|
||||
.observeOn(aapsSchedulers.io)
|
||||
.subscribe({ event ->
|
||||
if (event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_snInput))) {
|
||||
pumpSync.connectNewPump()
|
||||
changePump()
|
||||
}
|
||||
}, fabricPrivacy::logException)
|
||||
changePump()
|
||||
}
|
||||
|
||||
|
@ -113,13 +122,15 @@ class MedtrumPlugin @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun changePump() { // TODO: Call this on inputfield change?
|
||||
fun changePump() {
|
||||
aapsLogger.debug(LTag.PUMP, "changePump: called!")
|
||||
try {
|
||||
mDeviceSN = sp.getString(info.nightscout.pump.medtrum.R.string.key_snInput, " ").toLong(radix = 16)
|
||||
commandQueue.readStatus(rh.gs(info.nightscout.core.ui.R.string.device_changed), null)
|
||||
} catch (e: NumberFormatException) {
|
||||
aapsLogger.debug(LTag.PUMP, "changePump: invalid input!")
|
||||
aapsLogger.debug(LTag.PUMP, "changePump: Invalid input!")
|
||||
}
|
||||
// TODO: add medtrumPump.reset()
|
||||
commandQueue.readStatus(rh.gs(info.nightscout.core.ui.R.string.device_changed), null)
|
||||
}
|
||||
|
||||
override fun isInitialized(): Boolean {
|
||||
|
@ -162,7 +173,7 @@ class MedtrumPlugin @Inject constructor(
|
|||
}
|
||||
|
||||
override fun getPumpStatus(reason: String) {
|
||||
// TODO
|
||||
medtrumService?.readPumpStatus()
|
||||
}
|
||||
|
||||
override fun setNewBasalProfile(profile: Profile): PumpEnactResult {
|
||||
|
|
|
@ -3,19 +3,13 @@ package info.nightscout.pump.medtrum.comm
|
|||
import info.nightscout.pump.medtrum.encryption.Crypt
|
||||
|
||||
|
||||
class WriteCommandPackets(private val command: ByteArray) {
|
||||
class WriteCommandPackets() {
|
||||
|
||||
val crypt = Crypt()
|
||||
private val CRC_8_TABLE: IntArray = intArrayOf(0, 155, 173, 54, 193, 90, 108, 247, 25, 130, 180, 47, 216, 67, 117, 238, 50, 169, 159, 4, 243, 104, 94, 197, 43, 176, 134, 29, 234, 113, 71, 220, 100, 255, 201, 82, 165, 62, 8, 147, 125, 230, 208, 75, 188, 39, 17, 138, 86, 205, 251, 96, 151, 12, 58, 161, 79, 212, 226, 121, 142, 21, 35, 184, 200, 83, 101, 254, 9, 146, 164, 63, 209, 74, 124, 231, 16, 139, 189, 38, 250, 97, 87, 204, 59, 160, 150, 13, 227, 120, 78, 213, 34, 185, 143, 20, 172, 55, 1, 154, 109, 246, 192, 91, 181, 46, 24, 131, 116, 239, 217, 66, 158, 5, 51, 168, 95, 196, 242, 105, 135, 28, 42, 177, 70, 221, 235, 112, 11, 144, 166, 61, 202, 81, 103, 252, 18, 137, 191, 36, 211, 72, 126, 229, 57, 162, 148, 15, 248, 99, 85, 206, 32, 187, 141, 22, 225, 122, 76, 215, 111, 244, 194, 89, 174, 53, 3, 152, 118, 237, 219, 64, 183, 44, 26, 129, 93, 198, 240, 107, 156, 7, 49, 170, 68, 223, 233, 114, 133, 30, 40, 179, 195, 88, 110, 245, 2, 153, 175, 52, 218, 65, 119, 236, 27, 128, 182, 45, 241, 106, 92, 199, 48, 171, 157, 6, 232, 115, 69, 222, 41, 178, 132, 31, 167, 60, 10, 145, 102, 253, 203, 80, 190, 37, 19, 136, 127, 228, 210, 73, 149, 14, 56, 163, 84, 207, 249, 98, 140, 23, 33, 186, 77, 214, 224, 123)
|
||||
|
||||
private val packages = mutableListOf<ByteArray>()
|
||||
private var index = 0
|
||||
private var writeCommandIndex = 0
|
||||
private var allPacketsConsumed = false
|
||||
|
||||
|
||||
init {
|
||||
setData(command)
|
||||
}
|
||||
private var sequenceNumber = 0
|
||||
|
||||
fun setData(inputData: ByteArray) {
|
||||
resetPackets()
|
||||
|
@ -24,12 +18,12 @@ class WriteCommandPackets(private val command: ByteArray) {
|
|||
var header = byteArrayOf(
|
||||
(inputData.size + 4).toByte(),
|
||||
inputData[0],
|
||||
writeCommandIndex.toByte(),
|
||||
sequenceNumber.toByte(),
|
||||
pkgIndex.toByte()
|
||||
)
|
||||
|
||||
var tmp: ByteArray = header + inputData.copyOfRange(1, inputData.size)
|
||||
var totalCommand: ByteArray = tmp + crypt.calcCrc8(tmp, tmp.size).toByte()
|
||||
var totalCommand: ByteArray = tmp + calcCrc8(tmp, tmp.size).toByte()
|
||||
|
||||
if ((totalCommand.size - header.size) <= 15) {
|
||||
packages.add(totalCommand + 0.toByte())
|
||||
|
@ -40,7 +34,7 @@ class WriteCommandPackets(private val command: ByteArray) {
|
|||
while (remainingCommand.size > 15) {
|
||||
header[3] = pkgIndex.toByte()
|
||||
tmp = header + remainingCommand.copyOfRange(0, 15)
|
||||
packages.add(tmp + crypt.calcCrc8(tmp, tmp.size).toByte())
|
||||
packages.add(tmp + calcCrc8(tmp, tmp.size).toByte())
|
||||
|
||||
remainingCommand = remainingCommand.copyOfRange(15, remainingCommand.size)
|
||||
pkgIndex = (pkgIndex + 1) % 256
|
||||
|
@ -49,14 +43,9 @@ class WriteCommandPackets(private val command: ByteArray) {
|
|||
// Add last package
|
||||
header[3] = pkgIndex.toByte()
|
||||
tmp = header + remainingCommand
|
||||
packages.add(tmp + crypt.calcCrc8(tmp, tmp.size).toByte())
|
||||
packages.add(tmp + calcCrc8(tmp, tmp.size).toByte())
|
||||
}
|
||||
writeCommandIndex = (writeCommandIndex % 255) + 1
|
||||
}
|
||||
|
||||
|
||||
fun allPacketsConsumed(): Boolean {
|
||||
return allPacketsConsumed
|
||||
sequenceNumber = (sequenceNumber % 255) + 1
|
||||
}
|
||||
|
||||
fun getNextPacket(): ByteArray? {
|
||||
|
@ -65,15 +54,23 @@ class WriteCommandPackets(private val command: ByteArray) {
|
|||
ret = packages[index]
|
||||
index++
|
||||
}
|
||||
if (index >= packages.size) {
|
||||
allPacketsConsumed = true
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
fun allPacketsConsumed(): Boolean {
|
||||
return !(index < packages.size)
|
||||
}
|
||||
|
||||
private fun resetPackets() {
|
||||
packages.clear()
|
||||
index = 0
|
||||
allPacketsConsumed = false
|
||||
}
|
||||
|
||||
private fun calcCrc8(value: ByteArray, size: Int): Int {
|
||||
var crc8 = 0
|
||||
for (i in 0 until size) {
|
||||
crc8 = CRC_8_TABLE[(value[i].toInt() and 255) xor (crc8 and 255)].toInt() and 255
|
||||
}
|
||||
return crc8
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,9 +4,8 @@ import info.nightscout.pump.medtrum.extension.toByteArray
|
|||
import info.nightscout.pump.medtrum.extension.toLong
|
||||
|
||||
class Crypt {
|
||||
val RIJNDEAL_S_BOX: IntArray = intArrayOf(99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22)
|
||||
val RIJNDEAL_INVERSE_S_BOX: IntArray = intArrayOf(82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97, 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125)
|
||||
val CRC_8_TABLE: IntArray = intArrayOf(0, 155, 173, 54, 193, 90, 108, 247, 25, 130, 180, 47, 216, 67, 117, 238, 50, 169, 159, 4, 243, 104, 94, 197, 43, 176, 134, 29, 234, 113, 71, 220, 100, 255, 201, 82, 165, 62, 8, 147, 125, 230, 208, 75, 188, 39, 17, 138, 86, 205, 251, 96, 151, 12, 58, 161, 79, 212, 226, 121, 142, 21, 35, 184, 200, 83, 101, 254, 9, 146, 164, 63, 209, 74, 124, 231, 16, 139, 189, 38, 250, 97, 87, 204, 59, 160, 150, 13, 227, 120, 78, 213, 34, 185, 143, 20, 172, 55, 1, 154, 109, 246, 192, 91, 181, 46, 24, 131, 116, 239, 217, 66, 158, 5, 51, 168, 95, 196, 242, 105, 135, 28, 42, 177, 70, 221, 235, 112, 11, 144, 166, 61, 202, 81, 103, 252, 18, 137, 191, 36, 211, 72, 126, 229, 57, 162, 148, 15, 248, 99, 85, 206, 32, 187, 141, 22, 225, 122, 76, 215, 111, 244, 194, 89, 174, 53, 3, 152, 118, 237, 219, 64, 183, 44, 26, 129, 93, 198, 240, 107, 156, 7, 49, 170, 68, 223, 233, 114, 133, 30, 40, 179, 195, 88, 110, 245, 2, 153, 175, 52, 218, 65, 119, 236, 27, 128, 182, 45, 241, 106, 92, 199, 48, 171, 157, 6, 232, 115, 69, 222, 41, 178, 132, 31, 167, 60, 10, 145, 102, 253, 203, 80, 190, 37, 19, 136, 127, 228, 210, 73, 149, 14, 56, 163, 84, 207, 249, 98, 140, 23, 33, 186, 77, 214, 224, 123)
|
||||
private val RIJNDEAL_S_BOX: IntArray = intArrayOf(99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22)
|
||||
private val RIJNDEAL_INVERSE_S_BOX: IntArray = intArrayOf(82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97, 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125)
|
||||
|
||||
val MED_CIPHER: Long = 1344751489
|
||||
|
||||
|
@ -27,14 +26,6 @@ class Crypt {
|
|||
return ret
|
||||
}
|
||||
|
||||
fun calcCrc8(value: ByteArray, size: Int): Int {
|
||||
var crc8 = 0
|
||||
for (i in 0 until size) {
|
||||
crc8 = CRC_8_TABLE[(value[i].toInt() and 255) xor (crc8 and 255)].toInt() and 255
|
||||
}
|
||||
return crc8
|
||||
}
|
||||
|
||||
private fun simpleCrypt(inputData: Long): Long {
|
||||
var temp = inputData xor MED_CIPHER
|
||||
for (i in 0 until 32) {
|
||||
|
|
|
@ -19,6 +19,8 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.HandlerThread
|
||||
import android.os.SystemClock
|
||||
import androidx.core.app.ActivityCompat
|
||||
import dagger.android.HasAndroidInjector
|
||||
|
@ -64,13 +66,19 @@ class BLEComm @Inject internal constructor(
|
|||
private const val WRITE_DELAY_MILLIS: Long = 50
|
||||
private const val SERVICE_UUID = "669A9001-0008-968F-E311-6050405558B3"
|
||||
private const val READ_UUID = "669a9120-0008-968f-e311-6050405558b3"
|
||||
private const val WRITE_UUID = "669a9101-0008-968f-e311-6050405558b"
|
||||
private const val CHARACTERISTIC_CONFIG_UUID = "00002902-0000-1000-8000-00805f9b34fb"
|
||||
private const val WRITE_UUID = "669a9101-0008-968f-e311-6050405558b3"
|
||||
private const val CONFIG_UUID = "00002902-0000-1000-8000-00805f9b34fb"
|
||||
|
||||
private const val NEEDS_ENABLE_NOTIFICATION = 0x10
|
||||
private const val NEEDS_ENABLE_INDICATION = 0x20
|
||||
private const val NEEDS_ENABLE = 0x30
|
||||
|
||||
private const val MANUFACTURER_ID = 18305
|
||||
private const val COMMAND_AUTH_REQ: Byte = 5
|
||||
}
|
||||
|
||||
private val handler =
|
||||
Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
|
||||
private val mBluetoothAdapter: BluetoothAdapter? get() = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter
|
||||
private var mBluetoothGatt: BluetoothGatt? = null
|
||||
private val mCrypt = Crypt()
|
||||
|
@ -78,9 +86,12 @@ class BLEComm @Inject internal constructor(
|
|||
var isConnected = false
|
||||
var isConnecting = false
|
||||
private var uartWrite: BluetoothGattCharacteristic? = null
|
||||
private var uartRead: BluetoothGattCharacteristic? = null
|
||||
|
||||
private var mDeviceSN: Long = 0
|
||||
|
||||
private var mWritePackets = WriteCommandPackets()
|
||||
|
||||
/** Connect flow: 1. Start scanning for our device (SN entered in settings) */
|
||||
@SuppressLint("MissingPermission")
|
||||
@Synchronized
|
||||
|
@ -210,30 +221,29 @@ class BLEComm @Inject internal constructor(
|
|||
|
||||
override fun onCharacteristicRead(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicRead status = " + status)
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
readDataParsing(characteristic.value)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicChanged")
|
||||
// TODO: Make split between Notif and Indication
|
||||
// Notif contains pump status and alarms
|
||||
readDataParsing(characteristic.value)
|
||||
}
|
||||
|
||||
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicWrite status = " + status)
|
||||
// TODO (note also queue, note that in danars there is no response, so check where to handle multiple packets)
|
||||
// aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicWrite: " + DanaRS_Packet.toHexString(characteristic.value))
|
||||
// Thread {
|
||||
// synchronized(mSendQueue) {
|
||||
// // after message sent, check if there is the rest of the message waiting and send it
|
||||
// if (mSendQueue.size > 0) {
|
||||
// val bytes = mSendQueue[0]
|
||||
// mSendQueue.removeAt(0)
|
||||
// writeCharacteristicNoResponse(uartWriteBTGattChar, bytes)
|
||||
// }
|
||||
// }
|
||||
// }.start()
|
||||
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
// Check if we need to finish our command!
|
||||
synchronized(mWritePackets) {
|
||||
val value: ByteArray? = mWritePackets.getNextPacket()
|
||||
if (value != null) {
|
||||
writeCharacteristic(uartWriteBTGattChar, value)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: What to do here?
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDescriptorWrite(gatt: BluetoothGatt?, descriptor: BluetoothGattDescriptor?, status: Int) {
|
||||
|
@ -286,7 +296,7 @@ class BLEComm @Inject internal constructor(
|
|||
val characteristics = service.characteristics
|
||||
for (j in 0 until characteristics.size) {
|
||||
val configDescriptor =
|
||||
characteristics[j].getDescriptor(UUID.fromString(CHARACTERISTIC_CONFIG_UUID))
|
||||
characteristics[j].getDescriptor(UUID.fromString(CONFIG_UUID))
|
||||
if (configDescriptor.value == null || configDescriptor.value.toInt() <= 0) {
|
||||
notificationEnabled = false
|
||||
}
|
||||
|
@ -310,11 +320,11 @@ class BLEComm @Inject internal constructor(
|
|||
return
|
||||
}
|
||||
mBluetoothGatt?.setCharacteristicNotification(characteristic, enabled)
|
||||
characteristic?.getDescriptor(UUID.fromString(CHARACTERISTIC_CONFIG_UUID))?.let {
|
||||
if (characteristic.properties and 0x10 > 0) {
|
||||
characteristic?.getDescriptor(UUID.fromString(CONFIG_UUID))?.let {
|
||||
if (characteristic.properties and NEEDS_ENABLE_NOTIFICATION > 0) {
|
||||
it.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
|
||||
mBluetoothGatt?.writeDescriptor(it)
|
||||
} else if (characteristic.properties and 0x20 > 0) {
|
||||
} else if (characteristic.properties and NEEDS_ENABLE_INDICATION > 0) {
|
||||
it.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
|
||||
mBluetoothGatt?.writeDescriptor(it)
|
||||
} else {
|
||||
|
@ -329,7 +339,9 @@ class BLEComm @Inject internal constructor(
|
|||
private fun onConnectionStateChangeSynchronized(gatt: BluetoothGatt, status: Int, newState: Int) {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "onConnectionStateChange newState: " + newState + " status: " + status)
|
||||
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||
gatt.discoverServices()
|
||||
handler.postDelayed({
|
||||
mBluetoothGatt?.discoverServices()
|
||||
}, WRITE_DELAY_MILLIS)
|
||||
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
||||
close()
|
||||
isConnected = false
|
||||
|
@ -340,9 +352,12 @@ class BLEComm @Inject internal constructor(
|
|||
}
|
||||
|
||||
private fun readDataParsing(receivedData: ByteArray) {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "<<<readDataParsing>>> " + Arrays.toString(receivedData))
|
||||
// TODO
|
||||
/** Connect flow: 6. Authorized */ // TODO place this at the correct place
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "<<<<< readDataParsing " + Arrays.toString(receivedData))
|
||||
// TODO Implement
|
||||
// TODO place this at the correct place
|
||||
/** Connect flow: 6. Authorized */
|
||||
isConnected = true
|
||||
isConnecting = false
|
||||
}
|
||||
|
||||
private fun authorize() {
|
||||
|
@ -354,13 +369,20 @@ class BLEComm @Inject internal constructor(
|
|||
}
|
||||
|
||||
fun sendMessage(message: ByteArray) {
|
||||
// TODO: Handle packages which consist of multiple, Create a queue of packages
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "sendMessage message = " + Arrays.toString(message))
|
||||
val writePacket = WriteCommandPackets(message)
|
||||
val value: ByteArray? = writePacket.getNextPacket()
|
||||
|
||||
// TODO: queue
|
||||
if (!mWritePackets.allPacketsConsumed()) {
|
||||
aapsLogger.error(LTag.PUMPBTCOMM, "sendMessage not all packets consumed!! unable to sent message!")
|
||||
return
|
||||
}
|
||||
synchronized(mWritePackets) {
|
||||
mWritePackets.setData(message)
|
||||
val value: ByteArray? = mWritePackets.getNextPacket()
|
||||
if (value != null) {
|
||||
writeCharacteristic(uartWriteBTGattChar, value)
|
||||
} else {
|
||||
aapsLogger.error(LTag.PUMPBTCOMM, "sendMessage error in writePacket!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getGattService(): BluetoothGattService? {
|
||||
|
@ -390,20 +412,18 @@ class BLEComm @Inject internal constructor(
|
|||
@SuppressLint("MissingPermission")
|
||||
@Synchronized
|
||||
private fun writeCharacteristic(characteristic: BluetoothGattCharacteristic, data: ByteArray?) {
|
||||
Thread(Runnable {
|
||||
SystemClock.sleep(WRITE_DELAY_MILLIS)
|
||||
handler.postDelayed({
|
||||
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
|
||||
aapsLogger.error("BluetoothAdapter not initialized_ERROR")
|
||||
isConnecting = false
|
||||
isConnected = false
|
||||
return@Runnable
|
||||
}
|
||||
} else {
|
||||
characteristic.value = data
|
||||
characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
|
||||
aapsLogger.debug("writeCharacteristic:" + Arrays.toString(data))
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "writeCharacteristic:" + Arrays.toString(data))
|
||||
mBluetoothGatt?.writeCharacteristic(characteristic)
|
||||
}).start()
|
||||
SystemClock.sleep(WRITE_DELAY_MILLIS)
|
||||
}
|
||||
}, WRITE_DELAY_MILLIS)
|
||||
}
|
||||
|
||||
private val uartWriteBTGattChar: BluetoothGattCharacteristic
|
||||
|
@ -413,9 +433,23 @@ class BLEComm @Inject internal constructor(
|
|||
/** Connect flow: 4. When services are discovered find characteristics and set notifications*/
|
||||
private fun findCharacteristic() {
|
||||
val gattService = getGattService() ?: return
|
||||
var uuid: String
|
||||
val gattCharacteristics = gattService.characteristics
|
||||
for (gattCharacteristic in gattCharacteristics) {
|
||||
for (i in 0..gattCharacteristics.size - 1) {
|
||||
val gattCharacteristic = gattCharacteristics.get(i)
|
||||
// Check whether read or write properties is set, the pump needs us to enable notifications on all characteristics that have these properties
|
||||
if (gattCharacteristic.properties and NEEDS_ENABLE > 0) {
|
||||
handler.postDelayed({
|
||||
uuid = gattCharacteristic.uuid.toString()
|
||||
setCharacteristicNotification(gattCharacteristic, true)
|
||||
if (READ_UUID == uuid) {
|
||||
uartRead = gattCharacteristic
|
||||
}
|
||||
if (WRITE_UUID == uuid) {
|
||||
uartWrite = gattCharacteristic
|
||||
}
|
||||
}, (i * 600).toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -179,13 +179,7 @@ class MedtrumService : DaggerService() {
|
|||
return false
|
||||
}
|
||||
|
||||
fun loadHistory(type: Byte): PumpEnactResult {
|
||||
val result = PumpEnactResult(injector)
|
||||
if (!isConnected) return result
|
||||
// TODO
|
||||
return result
|
||||
}
|
||||
|
||||
/** Service stuff */
|
||||
inner class LocalBinder : Binder() {
|
||||
val serviceInstance: MedtrumService
|
||||
get() = this@MedtrumService
|
||||
|
|
|
@ -9,7 +9,8 @@ class WriteCommandPacketsTest {
|
|||
fun Given14LongCommandExpectOnePacket() {
|
||||
val input = byteArrayOf(5, 2, 0, 0, 0, 0, -21, 57, -122, -56)
|
||||
val expect = byteArrayOf(14, 5, 0, 0, 2, 0, 0, 0, 0, -21, 57, -122, -56, -93, 0)
|
||||
val cmdPackets = WriteCommandPackets(input)
|
||||
val cmdPackets = WriteCommandPackets()
|
||||
cmdPackets.setData(input)
|
||||
val output = cmdPackets.getNextPacket()
|
||||
|
||||
assertEquals(expect.contentToString(), output.contentToString())
|
||||
|
@ -22,7 +23,8 @@ class WriteCommandPacketsTest {
|
|||
val expect2 = byteArrayOf(41, 18, 0, 2, 0, -96, 2, -16, 96, 2, 104, 33, 2, -32, -31, 1, -64, 3, 2, -3)
|
||||
val expect3 = byteArrayOf(41, 18, 0, 3, -20, 36, 2, 100, -123, 2, -125, -89)
|
||||
|
||||
val cmdPackets = WriteCommandPackets(input)
|
||||
val cmdPackets = WriteCommandPackets()
|
||||
cmdPackets.setData(input)
|
||||
val output1 = cmdPackets.getNextPacket()
|
||||
val output2 = cmdPackets.getNextPacket()
|
||||
val output3 = cmdPackets.getNextPacket()
|
||||
|
@ -34,7 +36,6 @@ class WriteCommandPacketsTest {
|
|||
assertEquals(expect3.contentToString(), output3.contentToString())
|
||||
assertNull(output4)
|
||||
assertEquals(true, cmdPackets.allPacketsConsumed())
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -45,7 +46,8 @@ class WriteCommandPacketsTest {
|
|||
val expect1 = byteArrayOf(5, 66, 0, 0, -25, 0)
|
||||
val expect2 = byteArrayOf(5, 99, 1, 0, 64, 0)
|
||||
|
||||
val cmdPackets = WriteCommandPackets(input1)
|
||||
val cmdPackets = WriteCommandPackets()
|
||||
cmdPackets.setData(input1)
|
||||
|
||||
val output1 = cmdPackets.getNextPacket()
|
||||
|
||||
|
@ -58,7 +60,7 @@ class WriteCommandPacketsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun GivenWriteIndexOverflowExpectWriteIndex1() {
|
||||
fun GivenSequenceNumberOverflowExpectSequenceNumber1() {
|
||||
val input1 = byteArrayOf(55)
|
||||
val input2 = byteArrayOf(66)
|
||||
val input3 = byteArrayOf(99)
|
||||
|
@ -67,12 +69,12 @@ class WriteCommandPacketsTest {
|
|||
val expect2 = byteArrayOf(5, 66, -1, 0, 86, 0)
|
||||
val expect3 = byteArrayOf(5, 99, 1, 0, 64, 0)
|
||||
|
||||
val cmdPackets = WriteCommandPackets(byteArrayOf(0.toByte()))
|
||||
val cmdPackets = WriteCommandPackets()
|
||||
|
||||
// All this stuff to set the private field ^^
|
||||
val writeCommandIndex = WriteCommandPackets::class.java.getDeclaredField("writeCommandIndex")
|
||||
writeCommandIndex.isAccessible = true
|
||||
writeCommandIndex.setInt(cmdPackets, 254)
|
||||
val sequenceNumber = WriteCommandPackets::class.java.getDeclaredField("sequenceNumber")
|
||||
sequenceNumber.isAccessible = true
|
||||
sequenceNumber.setInt(cmdPackets, 254)
|
||||
|
||||
cmdPackets.setData(input1)
|
||||
val output1 = cmdPackets.getNextPacket()
|
||||
|
|
Loading…
Reference in a new issue