Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide additional details to data processor #538

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ val peripheral = scope.peripheral(advertisement) {

// or...

data = DataProcessor { bytes ->
data = DataProcessor { bytes, _, _, _, _ ->
// todo: Convert `bytes` to desired String representation, for example:
bytes.joinToString { byte -> byte.toString() } // Show data as integer representation of bytes.
}
Expand Down
11 changes: 10 additions & 1 deletion core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,16 @@ public final class com/juul/kable/logs/Logging {
}

public abstract interface class com/juul/kable/logs/Logging$DataProcessor {
public abstract fun process ([B)Ljava/lang/String;
public abstract fun process ([BLcom/juul/kable/logs/Logging$DataProcessor$Operation;Ljava/util/UUID;Ljava/util/UUID;Ljava/util/UUID;)Ljava/lang/String;
}

public final class com/juul/kable/logs/Logging$DataProcessor$Operation : java/lang/Enum {
public static final field Change Lcom/juul/kable/logs/Logging$DataProcessor$Operation;
public static final field Read Lcom/juul/kable/logs/Logging$DataProcessor$Operation;
public static final field Write Lcom/juul/kable/logs/Logging$DataProcessor$Operation;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/juul/kable/logs/Logging$DataProcessor$Operation;
public static fun values ()[Lcom/juul/kable/logs/Logging$DataProcessor$Operation;
}

public final class com/juul/kable/logs/Logging$Format : java/lang/Enum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.juul.kable.gatt.Response.OnReadRemoteRssi
import com.juul.kable.gatt.Response.OnServicesDiscovered
import com.juul.kable.logs.Logger
import com.juul.kable.logs.Logging
import com.juul.kable.logs.Logging.DataProcessor.Operation
import com.juul.kable.logs.detail
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -250,7 +251,7 @@ internal class BluetoothDeviceAndroidPeripheral(
message = "write"
detail(characteristic)
detail(writeType)
detail(data)
detail(data, Operation.Write)
}

val platformCharacteristic = discoveredServices.obtain(characteristic, writeType.properties)
Expand Down Expand Up @@ -289,7 +290,7 @@ internal class BluetoothDeviceAndroidPeripheral(
logger.debug {
message = "write"
detail(platformDescriptor)
detail(data)
detail(data, Operation.Write)
}

connection.execute<OnDescriptorWrite> {
Expand Down
8 changes: 5 additions & 3 deletions core/src/androidMain/kotlin/gatt/Callback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import com.juul.kable.gatt.Response.OnReadRemoteRssi
import com.juul.kable.gatt.Response.OnServicesDiscovered
import com.juul.kable.logs.Logger
import com.juul.kable.logs.Logging
import com.juul.kable.logs.Logging.DataProcessor.Operation.Change
import com.juul.kable.logs.Logging.DataProcessor.Operation.Read
import com.juul.kable.logs.detail
import com.juul.kable.toLazyCharacteristic
import kotlinx.coroutines.channels.Channel
Expand Down Expand Up @@ -146,7 +148,7 @@ internal class Callback(
message = "onCharacteristicRead"
detail(characteristic)
detail(event.status)
detail(value)
detail(value, Read)
}
onResponse.trySendOrLog(event)
}
Expand Down Expand Up @@ -183,7 +185,7 @@ internal class Callback(
logger.debug {
message = "onCharacteristicChanged"
detail(characteristic)
detail(value)
detail(value, Change)
}
val event = CharacteristicChange(characteristic.toLazyCharacteristic(), value)
onCharacteristicChanged.tryEmitOrLog(event)
Expand Down Expand Up @@ -211,7 +213,7 @@ internal class Callback(
message = "onDescriptorRead"
detail(descriptor)
detail(event.status)
detail(value)
detail(value, Read)
}
onResponse.trySendOrLog(event)
}
Expand Down
18 changes: 9 additions & 9 deletions core/src/androidMain/kotlin/logs/LogMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package com.juul.kable.logs

import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothGattService
import com.juul.kable.gatt.GattStatus

internal actual val LOG_INDENT: String? = null
Expand All @@ -13,16 +12,17 @@ internal fun LogMessage.detail(status: GattStatus) {
detail("status", status.toString())
}

internal fun LogMessage.detail(service: BluetoothGattService) {
detail("service", service.uuid.toString())
}

internal fun LogMessage.detail(characteristic: BluetoothGattCharacteristic) {
detail(characteristic.service)
detail("characteristic", characteristic.uuid.toString())
detail(
characteristic.service.uuid,
characteristic.uuid,
)
}

internal fun LogMessage.detail(descriptor: BluetoothGattDescriptor) {
detail(descriptor.characteristic)
detail("descriptor", descriptor.uuid.toString())
detail(
descriptor.characteristic.service.uuid,
descriptor.characteristic.uuid,
descriptor.uuid,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.juul.kable.WriteType.WithResponse
import com.juul.kable.WriteType.WithoutResponse
import com.juul.kable.logs.Logger
import com.juul.kable.logs.Logging
import com.juul.kable.logs.Logging.DataProcessor.Operation.Write
import com.juul.kable.logs.detail
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.CoroutineName
Expand Down Expand Up @@ -264,7 +265,7 @@ internal class CBPeripheralCoreBluetoothPeripheral(
message = "write"
detail(characteristic)
detail(writeType)
detail(data)
detail(data, Write)
}

val platformCharacteristic = discoveredServices.obtain(characteristic, writeType.properties)
Expand Down Expand Up @@ -325,7 +326,7 @@ internal class CBPeripheralCoreBluetoothPeripheral(
logger.debug {
message = "write"
detail(descriptor)
detail(data)
detail(data, Write)
}

val platformDescriptor = discoveredServices.obtain(descriptor)
Expand Down
3 changes: 2 additions & 1 deletion core/src/appleMain/kotlin/PeripheralDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.juul.kable.PeripheralDelegate.Response.DidWriteValueForCharacteristic
import com.juul.kable.logs.LogMessage
import com.juul.kable.logs.Logger
import com.juul.kable.logs.Logging
import com.juul.kable.logs.Logging.DataProcessor.Operation.Change
import com.juul.kable.logs.detail
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.BUFFERED
Expand Down Expand Up @@ -155,7 +156,7 @@ internal class PeripheralDelegate(
logger.debug(error) {
message = "didUpdateValueForCharacteristic"
detail(didUpdateValueForCharacteristic)
detail(didUpdateValueForCharacteristic.value)
detail(didUpdateValueForCharacteristic.value, Change)
}

val characteristic = didUpdateValueForCharacteristic.toLazyCharacteristic()
Expand Down
19 changes: 13 additions & 6 deletions core/src/appleMain/kotlin/logs/LogMessage.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.juul.kable.logs

import com.juul.kable.logs.Logging.DataProcessor.Operation
import com.juul.kable.toByteArray
import com.juul.kable.toUuid
import platform.CoreBluetooth.CBCharacteristic
import platform.CoreBluetooth.CBDescriptor
import platform.CoreBluetooth.CBService
Expand All @@ -9,8 +11,8 @@ import platform.Foundation.NSError

internal actual val LOG_INDENT: String? = " "

internal fun LogMessage.detail(data: NSData?) {
if (data != null) detail(data.toByteArray())
internal fun LogMessage.detail(data: NSData?, operation: Operation) {
detail(data?.toByteArray(), operation)
}

internal fun LogMessage.detail(error: NSError?) {
Expand All @@ -22,11 +24,16 @@ internal fun LogMessage.detail(service: CBService) {
}

internal fun LogMessage.detail(characteristic: CBCharacteristic) {
detail(characteristic.service!!)
detail("characteristic", characteristic.UUID.UUIDString)
detail(
characteristic.service!!.UUID.toUuid(),
characteristic.UUID.toUuid(),
)
}

internal fun LogMessage.detail(descriptor: CBDescriptor) {
detail(descriptor.characteristic!!)
detail("descriptor", descriptor.UUID.UUIDString)
detail(
descriptor.characteristic!!.service!!.UUID.toUuid(),
descriptor.characteristic!!.UUID.toUuid(),
descriptor.UUID.toUuid(),
)
}
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/logs/Hex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HexBuilder internal constructor() {

public fun Hex(init: HexBuilder.() -> Unit = {}): Logging.DataProcessor {
val config = HexBuilder().apply(init)
return Logging.DataProcessor { data ->
return Logging.DataProcessor { data, _, _, _, _ ->
data.toHexString(separator = config.separator, lowerCase = config.lowerCase)
}
}
122 changes: 72 additions & 50 deletions core/src/commonMain/kotlin/logs/LogMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,55 @@ package com.juul.kable.logs
import com.benasher44.uuid.Uuid
import com.juul.kable.Characteristic
import com.juul.kable.Descriptor
import com.juul.kable.Service
import com.juul.kable.WriteType
import com.juul.kable.logs.Logging.DataProcessor.Operation
import com.juul.kable.logs.Logging.Format.Compact
import com.juul.kable.logs.Logging.Format.Multiline

internal expect val LOG_INDENT: String?

internal class LogMessage {
internal class LogMessage(
private val logging: Logging,
platformIdentifier: String?,
private val indent: String? = LOG_INDENT,
) {

var message: String = ""
private val prefix = logging.identifier ?: platformIdentifier

var message: String = ""
private var operation: Operation? = null
private var serviceUuid: Uuid? = null
private var characteristicUuid: Uuid? = null
private var descriptorUuid: Uuid? = null
private val details = mutableListOf<Pair<String, Any>>()
private var data: ByteArray? = null

fun detail(key: String, value: String) {
details += key to value
fun detail(serviceUuid: Uuid, characteristicUuid: Uuid) {
this.serviceUuid = serviceUuid
this.characteristicUuid = characteristicUuid
this.descriptorUuid = null
}

fun detail(key: String, value: ByteArray) {
fun detail(serviceUuid: Uuid, characteristicUuid: Uuid, descriptorUuid: Uuid) {
this.serviceUuid = serviceUuid
this.characteristicUuid = characteristicUuid
this.descriptorUuid = descriptorUuid
}

fun detail(characteristic: Characteristic) {
detail(characteristic.serviceUuid, characteristic.characteristicUuid)
}

fun detail(descriptor: Descriptor) {
detail(descriptor.serviceUuid, descriptor.characteristicUuid, descriptor.descriptorUuid)
}

fun detail(data: ByteArray?, operation: Operation) {
this.data = data
this.operation = operation
}

fun detail(key: String, value: String) {
details += key to value
}

Expand All @@ -30,64 +63,53 @@ internal class LogMessage {
detail(key, value.toString())
}

fun build(logging: Logging, platformIdentifier: String?): String = buildString {
val prefix = logging.identifier ?: platformIdentifier
private var isFirst = true

private fun StringBuilder.append(label: String, value: Any) {
when (logging.format) {
Compact -> if (isFirst) append('(') else append(", ")
Multiline -> {
appendLine()
if (indent != null) append(indent)
}
}
isFirst = false

append(label)
when (logging.format) {
Compact -> append("=")
Multiline -> append(": ")
}
append(value)
}

fun build(): String = buildString {
if (!prefix.isNullOrEmpty()) {
append(prefix)
append(' ')
}
append(message)

when (logging.format) {
Logging.Format.Compact -> if (details.isNotEmpty()) append('(')
Logging.Format.Multiline -> appendLine()
isFirst = true

serviceUuid?.let { append("service", it) }
characteristicUuid?.let { append("characteristic", it) }
descriptorUuid?.let { append("descriptor", it) }

details.forEach { (key, value) ->
append(key, value)
}

details.forEachIndexed { index, detail ->
val (key, value) = detail

if (value !is ByteArray || logging.level == Logging.Level.Data) {
if (index > 0) {
when (logging.format) {
Logging.Format.Compact -> append(", ")
Logging.Format.Multiline -> appendLine()
}
}

if (logging.format == Logging.Format.Multiline && LOG_INDENT != null) append(LOG_INDENT)

append(key)
when (logging.format) {
Logging.Format.Compact -> append("=")
Logging.Format.Multiline -> append(": ")
}
if (value is ByteArray) append(logging.data.process(value)) else append(value)
if (logging.level == Logging.Level.Data) {
data?.let {
append("data", logging.data.process(it, operation, serviceUuid, characteristicUuid, descriptorUuid))
}
}

if (logging.format == Logging.Format.Compact && details.isNotEmpty()) append(')')
if (logging.format == Compact && !isFirst) append(')')
}
}

internal fun LogMessage.detail(data: ByteArray?) {
if (data != null) detail("data", data)
}

internal fun LogMessage.detail(service: Service) {
detail("service", service.serviceUuid)
}

internal fun LogMessage.detail(characteristic: Characteristic) {
detail("service", characteristic.serviceUuid)
detail("characteristic", characteristic.characteristicUuid)
}

internal fun LogMessage.detail(descriptor: Descriptor) {
detail("service", descriptor.serviceUuid)
detail("characteristic", descriptor.characteristicUuid)
detail("descriptor", descriptor.descriptorUuid)
}

internal fun LogMessage.detail(writeType: WriteType) {
detail("writeType", writeType.name)
}
Loading