ble: add meaninful constants in the PayloadSplitter and getPodId

This commit is contained in:
Andrei Vereha 2021-02-26 19:10:11 +01:00
parent 358748654e
commit fb7a84bdf7
8 changed files with 44 additions and 18 deletions

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm
import info.nightscout.androidaps.utils.extensions.toHex
import java.nio.ByteBuffer
@ -10,6 +10,8 @@ data class Id(val address: ByteArray) {
/**
* Used to obtain podId from controllerId
* The original PDM seems to rotate over 3 Ids:
* controllerID+1, controllerID+2 and controllerID+3
*/
fun increment(): Id {
val nodeId = address.copyOf()

View file

@ -13,4 +13,6 @@ interface OmnipodDashBleManager {
fun connect()
fun disconnect()
fun getPodId(): Id
}

View file

@ -89,6 +89,12 @@ class OmnipodDashBleManagerImpl @Inject constructor(private val context: Context
TODO("not implemented")
}
override fun getPodId(): Id {
// TODO: return something meaningful here
return Id.fromInt(4243);
}
companion object {
private const val CONNECT_TIMEOUT_MS = 5000

View file

@ -2,7 +2,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.ltk
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.OmnipodDashBleManagerImpl
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.Id
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.StringLengthPrefixEncoding
import info.nightscout.androidaps.utils.extensions.hexStringToByteArray
@ -18,6 +18,7 @@ internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgI
var sp1sp2 = sp1sp2(nodeId.address, sp2(), seq, controllerId, nodeId)
msgIO.sendMesssage(sp1sp2.messagePacket)
/*
var sps1 =
msgIO.sendMesssage(sps1.messagePacket)

View file

@ -1,6 +1,6 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.ltk
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.Id
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessagePacket
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageType

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id
import java.nio.ByteBuffer
/***

View file

@ -13,15 +13,15 @@ internal class PayloadSplitter(private val payload: ByteArray) {
fun splitInPackets(): List<BlePacket> {
val ret = ArrayList<BlePacket>()
val crc32 = payload.crc32()
if (payload.size <= 18) {
val end = min(14, payload.size)
if (payload.size <= FirstBlePacket.CAPACITY_WITH_THE_OPTIONAL_PLUS_ONE_PACKET) {
val end = min(FirstBlePacket.CAPACITY_WITHOUT_MIDDLE_PACKETS, payload.size)
ret.add(FirstBlePacket(
totalFragments = 0,
payload = payload.copyOfRange(0, end),
size = payload.size.toByte(),
crc32 = crc32,
))
if (payload.size > 14) {
if (payload.size > FirstBlePacket.CAPACITY_WITHOUT_MIDDLE_PACKETS) {
ret.add(LastOptionalPlusOneBlePacket(
index = 1,
payload = payload.copyOfRange(end, payload.size),
@ -29,34 +29,34 @@ internal class PayloadSplitter(private val payload: ByteArray) {
}
return ret
}
val middleFragments = (payload.size - 18) / 19
val rest = ((payload.size - middleFragments.toInt() * 19) - 18).toByte()
val middleFragments = (payload.size - FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS) / MiddleBlePacket.CAPACITY
val rest = ((payload.size - middleFragments * MiddleBlePacket.CAPACITY) - FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS).toByte()
ret.add(FirstBlePacket(
totalFragments = (middleFragments + 1).toByte(),
payload = payload.copyOfRange(0, 18),
payload = payload.copyOfRange(0, FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS),
))
for (i in 1..middleFragments) {
val p = if (i == 1) {
payload.copyOfRange(18, 37)
payload.copyOfRange(FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS, FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS + MiddleBlePacket.CAPACITY)
} else {
payload.copyOfRange((i - 1) * 19 + 18, (i - 1) * 19 + 18 + 19)
payload.copyOfRange(FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS + (i - 1) * MiddleBlePacket.CAPACITY, FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS + i * MiddleBlePacket.CAPACITY)
}
ret.add(MiddleBlePacket(
index = i.toByte(),
payload = p,
))
}
val end = min(14, rest.toInt())
val end = min(LastBlePacket.CAPACITY, rest.toInt())
ret.add(LastBlePacket(
index = (middleFragments + 1).toByte(),
size = rest,
payload = payload.copyOfRange(middleFragments * 19 + 18, middleFragments * 19 + 18 + end),
payload = payload.copyOfRange(middleFragments * MiddleBlePacket.CAPACITY + FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS, middleFragments * MiddleBlePacket.CAPACITY + FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS + end),
crc32 = crc32,
))
if (rest > 14) {
ret.add(LastOptionalPlusOneBlePacket(
index = (middleFragments + 2).toByte(),
payload = payload.copyOfRange(middleFragments * 19 + 18 + 14, payload.size),
payload = payload.copyOfRange(middleFragments * MiddleBlePacket.CAPACITY + FirstBlePacket.CAPACITY_WITH_MIDDLE_PACKETS + LastBlePacket.CAPACITY, payload.size),
))
}
return ret

View file

@ -7,8 +7,8 @@ sealed class BlePacket {
abstract fun asByteArray(): ByteArray
companion object {
const val MAX_BLE_PACKET_LEN = 30 // we use this as the size allocated for the ByteBuffer
const val MAX_BLE_PACKET_LEN = 20
const val MAX_BLE_BUFFER_LEN = MAX_BLE_PACKET_LEN + 1 // we use this as the size allocated for the ByteBuffer
}
}
@ -16,7 +16,7 @@ data class FirstBlePacket(val totalFragments: Byte, val payload: ByteArray, val
override fun asByteArray(): ByteArray {
val bb = ByteBuffer
.allocate(MAX_BLE_PACKET_LEN)
.allocate(MAX_BLE_BUFFER_LEN)
.put(0) // index
.put(totalFragments) // # of fragments except FirstBlePacket and LastOptionalPlusOneBlePacket
crc32?.let {
@ -31,6 +31,12 @@ data class FirstBlePacket(val totalFragments: Byte, val payload: ByteArray, val
bb.get(ret)
return ret
}
companion object {
internal const val CAPACITY_WITHOUT_MIDDLE_PACKETS = 13 // we are using all fields
internal const val CAPACITY_WITH_MIDDLE_PACKETS = 18 // we are not using crc32 or size
internal const val CAPACITY_WITH_THE_OPTIONAL_PLUS_ONE_PACKET = 18
}
}
data class MiddleBlePacket(val index: Byte, val payload: ByteArray) : BlePacket() {
@ -38,13 +44,17 @@ data class MiddleBlePacket(val index: Byte, val payload: ByteArray) : BlePacket(
override fun asByteArray(): ByteArray {
return byteArrayOf(index) + payload
}
companion object {
internal const val CAPACITY = 19
}
}
data class LastBlePacket(val index: Byte, val size: Byte, val payload: ByteArray, val crc32: Long) : BlePacket() {
override fun asByteArray(): ByteArray {
val bb = ByteBuffer
.allocate(MAX_BLE_PACKET_LEN)
.allocate(MAX_BLE_BUFFER_LEN)
.put(index)
.put(size)
.putInt(crc32.toInt())
@ -54,6 +64,10 @@ data class LastBlePacket(val index: Byte, val size: Byte, val payload: ByteArray
bb.get(ret)
return ret
}
companion object {
internal const val CAPACITY = 14
}
}
data class LastOptionalPlusOneBlePacket(val index: Byte, val payload: ByteArray) : BlePacket() {