-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/commonMain/kotlin/io/rebble/libpebblecommon/packets/LogDump.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.rebble.libpebblecommon.packets | ||
|
||
import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry | ||
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import io.rebble.libpebblecommon.structmapper.* | ||
|
||
open class LogDump(val message: Message): PebblePacket(ProtocolEndpoint.LOG_DUMP) { | ||
val command = SUByte(m, message.value) | ||
|
||
init { | ||
type = command.get() | ||
} | ||
|
||
enum class Message(val value: UByte) { | ||
RequestLogDump(0x10u), | ||
LogLine(0x80u), | ||
Done(0x81u), | ||
NoLogs(0x82u) | ||
} | ||
|
||
class RequestLogDump(logGeneration: UByte, cookie: UInt): LogDump(Message.RequestLogDump) { | ||
val generation = SUByte(m, logGeneration) | ||
val cookie = SUInt(m, cookie) | ||
} | ||
|
||
open class ReceivedLogDumpMessage(message: Message): LogDump(message) { | ||
val cookie = SUInt(m) | ||
} | ||
|
||
class LogLine: ReceivedLogDumpMessage(Message.LogLine) { | ||
val timestamp = SUInt(m) | ||
val level = SUByte(m) | ||
val length = SUByte(m) | ||
val line = SUShort(m) | ||
val filename = SFixedString(m, 16) | ||
val messageText = SFixedString(m, 0) | ||
|
||
init { | ||
messageText.linkWithSize(length) | ||
} | ||
} | ||
|
||
class Done: ReceivedLogDumpMessage(Message.Done) | ||
|
||
class NoLogs: ReceivedLogDumpMessage(Message.NoLogs) | ||
} | ||
|
||
fun logDumpPacketsRegister() { | ||
PacketRegistry.register(ProtocolEndpoint.LOG_DUMP, LogDump.Message.NoLogs.value) { | ||
LogDump.NoLogs() | ||
} | ||
|
||
PacketRegistry.register(ProtocolEndpoint.LOG_DUMP, LogDump.Message.Done.value) { | ||
LogDump.Done() | ||
} | ||
|
||
PacketRegistry.register(ProtocolEndpoint.LOG_DUMP, LogDump.Message.LogLine.value) { | ||
LogDump.LogLine() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/commonMain/kotlin/io/rebble/libpebblecommon/services/LogDumpService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.rebble.libpebblecommon.services | ||
|
||
import io.rebble.libpebblecommon.ProtocolHandler | ||
import io.rebble.libpebblecommon.packets.LogDump | ||
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import kotlinx.coroutines.channels.Channel | ||
|
||
class LogDumpService(private val protocolHandler: ProtocolHandler) : ProtocolService { | ||
val receivedMessages = Channel<LogDump>(Channel.BUFFERED) | ||
|
||
init { | ||
protocolHandler.registerReceiveCallback(ProtocolEndpoint.LOG_DUMP, this::receive) | ||
} | ||
|
||
suspend fun send(packet: LogDump) { | ||
protocolHandler.send(packet) | ||
} | ||
|
||
fun receive(packet: PebblePacket) { | ||
if (packet !is LogDump) { | ||
throw IllegalStateException("Received invalid packet type: $packet") | ||
} | ||
|
||
receivedMessages.trySend(packet) | ||
} | ||
} |