From 722b591839ec84db1de0aeac48adf686892f5cb3 Mon Sep 17 00:00:00 2001 From: Patrick Moessler Date: Sun, 20 Oct 2019 23:49:17 +0200 Subject: [PATCH] Switch to COBS --- app/src/main/java/CobsUtils.kt | 92 +++++++++++++++++++ .../java/de/asaril/bletail/MainActivity.kt | 21 +---- app/src/main/res/values/strings.xml | 2 +- 3 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/CobsUtils.kt diff --git a/app/src/main/java/CobsUtils.kt b/app/src/main/java/CobsUtils.kt new file mode 100644 index 0000000..e9be5b9 --- /dev/null +++ b/app/src/main/java/CobsUtils.kt @@ -0,0 +1,92 @@ +import kotlin.experimental.and + +//This file was converted to Kotlin. Original Java file license below: + +//The MIT License (MIT) +// +//Copyright (c) 2016 Suresh Joshi +// +//Permission is hereby granted, free of charge, to any person obtaining a copy +//of this software and associated documentation files (the "Software"), to deal +//in the Software without restriction, including without limitation the rights +//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +//copies of the Software, and to permit persons to whom the Software is +//furnished to do so, subject to the following conditions: +// +//The above copyright notice and this permission notice shall be included in all +//copies or substantial portions of the Software. +// +//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +//SOFTWARE. + +class CobsUtils { + + companion object { + // Expected to be the entire packet to encode + fun encode(packet: ByteArray?): ByteArray { + if (packet == null || packet.isEmpty()) { + return byteArrayOf() + } + + val output = ByteArray(packet.size + 2) + var blockStartValue: Byte = 1 + var lastZeroIndex = 0 + var srcIndex = 0 + var destIndex = 1 + + while (srcIndex < packet.size) { + if (packet[srcIndex].toInt() == 0) { + output[lastZeroIndex] = blockStartValue + lastZeroIndex = destIndex++ + blockStartValue = 1 + } else { + output[destIndex++] = packet[srcIndex] + if ((++blockStartValue).toInt() == 255) { + output[lastZeroIndex] = blockStartValue + lastZeroIndex = destIndex++ + blockStartValue = 1 + } + } + + ++srcIndex + } + + output[lastZeroIndex] = blockStartValue + return output + } + + // Expected to be the entire packet to decode with trailing 0 + fun decode(packet: ByteArray?): ByteArray { + if (packet == null + || packet.isEmpty() + || packet[packet.size - 1].toInt() != 0 + ) { + return byteArrayOf() + } + + val output = ByteArray(packet.size - 2) + val srcPacketLength = packet.size - 1 + var srcIndex = 0 + var destIndex = 0 + + while (srcIndex < srcPacketLength) { + val code = packet[srcIndex++] and 0xff.toByte() + var i = 1 + while (srcIndex < srcPacketLength && i < code) { + output[destIndex++] = packet[srcIndex++] + ++i + } + if (code != 0xff.toByte() && srcIndex != srcPacketLength) { + output[destIndex++] = 0 + } + } + + return output + } + } +} \ No newline at end of file diff --git a/app/src/main/java/de/asaril/bletail/MainActivity.kt b/app/src/main/java/de/asaril/bletail/MainActivity.kt index b239a80..b129b35 100644 --- a/app/src/main/java/de/asaril/bletail/MainActivity.kt +++ b/app/src/main/java/de/asaril/bletail/MainActivity.kt @@ -181,25 +181,6 @@ class MainActivity : AppCompatActivity() { } } - private fun encodeSlip(payload: ByteArray): ByteArray { - var output = ByteArray(0) - for (b in payload) { - when (b) { - END -> { - output += ESC - output += ESC_END - } - ESC -> { - output += ESC - output += ESC_ESC - } - else -> output += b - } - } - output += END - return output - } - fun powerOffClick(item: MenuItem) { val r: fx.Fx.Root.Builder = fx.Fx.Root.newBuilder() r.halt = fx.Fx.halt_msg.newBuilder().build() @@ -220,7 +201,7 @@ class MainActivity : AppCompatActivity() { } fun sendToBleUart(msgRaw: ByteArray) { - val arr: ByteArray = encodeSlip(msgRaw) + val arr: ByteArray = CobsUtils.encode(msgRaw) fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) } Log.i("BLEtail", "tx:" + arr.toHexString()) this.rxChar!!.value = arr diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 25e62e4..0e7f791 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,7 +7,7 @@ 2 1 Number of segments - Segment %1$d + Segment $1%d Segment Color 2 Segment Color 1 Segment Color 0