Switch to COBS
This commit is contained in:
parent
b6d594d93d
commit
722b591839
3 changed files with 94 additions and 21 deletions
92
app/src/main/java/CobsUtils.kt
Normal file
92
app/src/main/java/CobsUtils.kt
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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) {
|
fun powerOffClick(item: MenuItem) {
|
||||||
val r: fx.Fx.Root.Builder = fx.Fx.Root.newBuilder()
|
val r: fx.Fx.Root.Builder = fx.Fx.Root.newBuilder()
|
||||||
r.halt = fx.Fx.halt_msg.newBuilder().build()
|
r.halt = fx.Fx.halt_msg.newBuilder().build()
|
||||||
|
@ -220,7 +201,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendToBleUart(msgRaw: ByteArray) {
|
fun sendToBleUart(msgRaw: ByteArray) {
|
||||||
val arr: ByteArray = encodeSlip(msgRaw)
|
val arr: ByteArray = CobsUtils.encode(msgRaw)
|
||||||
fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
|
fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
|
||||||
Log.i("BLEtail", "tx:" + arr.toHexString())
|
Log.i("BLEtail", "tx:" + arr.toHexString())
|
||||||
this.rxChar!!.value = arr
|
this.rxChar!!.value = arr
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
<string name="seg2">2</string>
|
<string name="seg2">2</string>
|
||||||
<string name="seg1">1</string>
|
<string name="seg1">1</string>
|
||||||
<string name="number_of_segments">Number of segments</string>
|
<string name="number_of_segments">Number of segments</string>
|
||||||
<string name="segment">Segment %1$d</string>
|
<string name="segment">Segment $1%d</string>
|
||||||
<string name="segment_color_2">Segment Color 2</string>
|
<string name="segment_color_2">Segment Color 2</string>
|
||||||
<string name="segment_color_1">Segment Color 1</string>
|
<string name="segment_color_1">Segment Color 1</string>
|
||||||
<string name="segment_color_0">Segment Color 0</string>
|
<string name="segment_color_0">Segment Color 0</string>
|
||||||
|
|
Loading…
Reference in a new issue