2020-02-10 15:31:56 -08:00
|
|
|
package com.geeksville.mesh.service
|
2020-01-21 09:37:39 -08:00
|
|
|
|
2020-01-24 12:49:27 -08:00
|
|
|
import android.bluetooth.BluetoothGattCharacteristic
|
2020-01-21 09:37:39 -08:00
|
|
|
|
2021-01-09 14:50:32 +08:00
|
|
|
/**
|
|
|
|
|
* Some misformatted ESP32s have problems
|
|
|
|
|
*/
|
2021-03-29 20:33:06 +08:00
|
|
|
class DeviceRejectedException : BLEException("Device rejected filesize")
|
2021-01-09 14:50:32 +08:00
|
|
|
|
2020-06-13 16:21:26 -07:00
|
|
|
/**
|
|
|
|
|
* Move this somewhere as a generic network byte order function
|
|
|
|
|
*/
|
|
|
|
|
fun toNetworkByteArray(value: Int, formatType: Int): ByteArray {
|
|
|
|
|
|
2020-11-16 15:57:40 +08:00
|
|
|
val len = when (formatType) {
|
|
|
|
|
BluetoothGattCharacteristic.FORMAT_UINT8 -> 1
|
|
|
|
|
BluetoothGattCharacteristic.FORMAT_UINT32 -> 4
|
|
|
|
|
else -> TODO()
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 16:21:26 -07:00
|
|
|
val mValue = ByteArray(len)
|
|
|
|
|
|
|
|
|
|
when (formatType) {
|
|
|
|
|
/* BluetoothGattCharacteristic.FORMAT_SINT8 -> {
|
|
|
|
|
value = intToSignedBits(value, 8)
|
|
|
|
|
mValue.get(offset) = (value and 0xFF).toByte()
|
|
|
|
|
}
|
|
|
|
|
BluetoothGattCharacteristic.FORMAT_UINT8 -> mValue.get(offset) =
|
|
|
|
|
(value and 0xFF).toByte()
|
|
|
|
|
BluetoothGattCharacteristic.FORMAT_SINT16 -> {
|
|
|
|
|
value = intToSignedBits(value, 16)
|
|
|
|
|
mValue.get(offset++) = (value and 0xFF).toByte()
|
|
|
|
|
mValue.get(offset) = (value shr 8 and 0xFF).toByte()
|
|
|
|
|
}
|
|
|
|
|
BluetoothGattCharacteristic.FORMAT_UINT16 -> {
|
|
|
|
|
mValue.get(offset++) = (value and 0xFF).toByte()
|
|
|
|
|
mValue.get(offset) = (value shr 8 and 0xFF).toByte()
|
|
|
|
|
}
|
|
|
|
|
BluetoothGattCharacteristic.FORMAT_SINT32 -> {
|
|
|
|
|
value = intToSignedBits(value, 32)
|
|
|
|
|
mValue.get(offset++) = (value and 0xFF).toByte()
|
|
|
|
|
mValue.get(offset++) = (value shr 8 and 0xFF).toByte()
|
|
|
|
|
mValue.get(offset++) = (value shr 16 and 0xFF).toByte()
|
|
|
|
|
mValue.get(offset) = (value shr 24 and 0xFF).toByte()
|
|
|
|
|
} */
|
2020-11-16 15:57:40 +08:00
|
|
|
BluetoothGattCharacteristic.FORMAT_UINT8 ->
|
|
|
|
|
mValue[0] = (value and 0xFF).toByte()
|
|
|
|
|
|
2020-06-13 16:21:26 -07:00
|
|
|
BluetoothGattCharacteristic.FORMAT_UINT32 -> {
|
|
|
|
|
mValue[0] = (value and 0xFF).toByte()
|
|
|
|
|
mValue[1] = (value shr 8 and 0xFF).toByte()
|
|
|
|
|
mValue[2] = (value shr 16 and 0xFF).toByte()
|
|
|
|
|
mValue[3] = (value shr 24 and 0xFF).toByte()
|
|
|
|
|
}
|
|
|
|
|
else -> TODO()
|
|
|
|
|
}
|
|
|
|
|
return mValue
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 03:38:51 -07:00
|
|
|
data class UpdateFilenames(val appLoad: String?, val littlefs: String?)
|