KOTLink is an open-source MAVLink library developed in Kotlin. The project is structured in several modules and aims to make MAVLink communication seamless and efficient for Kotlin developers. It provides a handy interface to work with MAVLink protocols, parse MAVLink messages, and even auto-generate Kotlin data classes from MAVLink XML definitions.
// The core of the library
implementation("com.mmoczkowski:kotlink-core:1.0.0")
// KTX extensions
implementation("com.mmoczkowski:kotlink-ktx:1.0.0")
// MAVLink Minimal protocol implementation
implementation("com.mmoczkowski:kotlink-protocol-minimal:1.0.0")
// MAVLink Common protocol implementation
implementation("com.mmoczkowski:kotlink-protocol-common:1.0.0")
// MAVLink ArduPilot Mega protocol implementation
implementation("com.mmoczkowski:kotlink-protocol-ardupilotmega:1.0.0")
// MAVLink code generation processor (only if you need to generate a protocol)
implementation("com.mmoczkowski:kotlink-processor:1.0.0")
Utilize the MavLinkParser
class as demonstrated:
val parser = MavLinkParser(MinimalMavlinkProtocol, CommonMavLinkProtocol)
val bytes = ... // your data
val frames = parser.parseNextBytes(bytes)
frames.forEach { frame ->
when (frame) {
is MavLinkFrame.V1 -> // handle MavLink v1 frame
is MavLinkFrame.V2 -> // handle MavLink v2 frame
}
}
The Kotlin Flow extension mapToMavLink
can be applied as follows:
val flow: Flow<Byte> = ...
val mavlinkFlow = flow.mapToMavLink(MinimalMavlinkProtocol, CommonMavLinkProtocol)
mavlinkFlow.collect { frame ->
when (frame) {
is MavLinkFrame.V1 -> // handle MavLink v1 frame
is MavLinkFrame.V2 -> // handle MavLink v2 frame
}
}
The project is organized into the following modules:
:core
:processor
:protocol:minimal
:protocol:common
:protocol:ardupilotmega
:ktx
The :core
module forms the backbone of KOTLink, providing essential MAVLink classes and interfaces, including:
MavLinkFrame
: This sealed class symbolizes a parsed MAVLink frame, containing two subclasses forV1
andV2
MAVLink versions.MavLinkMessage
: A representative interface for a MAVLink message, encapsulating the functiontoBytes(): ByteArray
for serialization of the message into a raw payload.MavLinkProtocol
: This interface depicts a MAVLink subset, furnished with methodsfromBytes(messageId: UInt, payload: ByteArray): MavLinkMessage?
andgetCrcExtra(messageId: UInt): Byte?
.MavLinkParser
: This class, essential for parsing sequences of bytes into aMavLinkFrame
containing aMavLinkMessage
, uses the provided MAVLinkXML
definitions for parsing.
The :processor
module serves as a KSP compiler plugin, translating MAVLink XML
definitions into generated Kotlin data classes that inherit from the MavLinkMessage
interface.
The :protocol
module is further divided into three submodules - :common
, :minimal
, and :ardupilotmega
. These modules consist of MAVLink classes auto-generated from their respective XML files: common.xml
, minimal.xml
, and ardupilotmega.xml
.
The :ktx
module adds functionality to the parser by offering Kotlin Flow extensions.