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

Add logradio BLE characteristic #1110

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class BluetoothInterface @AssistedInject constructor(
UUID.fromString("f75c76d2-129e-4dad-a1dd-7866124401e7")
val BTM_FROMNUM_CHARACTER: UUID =
UUID.fromString("ed9da18c-a800-4f66-a670-aa7547e34453")
val BTM_LOGRADIO_CHARACTER: UUID =
UUID.fromString("6c6fd238-78fa-436b-aacf-15c5be1ef2e2")

/**
* this is created in onCreate()
Expand All @@ -115,6 +117,7 @@ class BluetoothInterface @AssistedInject constructor(
?: throw RadioNotConnectedException("BLE service not found")

private lateinit var fromNum: BluetoothGattCharacteristic
private lateinit var logRadio: BluetoothGattCharacteristic

/**
* With the new rev2 api, our first send is to start the configure readbacks. In that case,
Expand Down Expand Up @@ -258,6 +261,37 @@ class BluetoothInterface @AssistedInject constructor(
}
}

private fun startWatchingLogRadio() {
safe?.setNotify(logRadio, true) {
service.serviceScope.handledLaunch {
safe?.asyncReadCharacteristic(logRadio) {
try {
val b = it.getOrThrow()
.value.clone() // We clone the array just in case, I'm not sure if they keep reusing the array

if (b.isNotEmpty()) {
val logMessage = b.toString(Charsets.UTF_8)

if (logMessage.startsWith("INFO"))
info(logMessage.replace("INFO | ", ""))
else if (logMessage.startsWith("DEBUG"))
debug(logMessage.replace("DEBUG | ", ""))
else if (logMessage.startsWith("WARN"))
warn(logMessage.replace("WARN | ", ""))
else if (logMessage.startsWith("ERROR"))
error(logMessage.replace("ERROR | ", ""))
else
debug(logMessage)
}

} catch (ex: BLEException) {
scheduleReconnect("error during doReadFromRadio - disconnecting, ${ex.message}")
}
}
}
}
}

/**
* Some buggy BLE stacks can fail on initial connect, with either missing services or missing characteristics. If that happens we
* disconnect and try again when the device reenumerates.
Expand Down Expand Up @@ -321,6 +355,7 @@ class BluetoothInterface @AssistedInject constructor(
} */

fromNum = getCharacteristic(BTM_FROMNUM_CHARACTER)
logRadio = getCharacteristic(BTM_LOGRADIO_CHARACTER)

// We treat the first send by a client as special
isFirstSend = true
Expand All @@ -330,6 +365,7 @@ class BluetoothInterface @AssistedInject constructor(

// Immediately broadcast any queued packets sitting on the device
doReadFromRadio(true)
startWatchingLogRadio()
} catch (ex: BLEException) {
scheduleReconnect(
"Unexpected error in initial device enumeration, forcing disconnect $ex"
Expand Down
Loading