How nus/dart_mavlink works
Project type and purpose
• This is an open-source Dart package (dart_mavlink) whose sole purpose is to parse and serialize MAVLink v1/v2 packets.
• It combines a code-generation step (to produce Dart bindings for MAVLink dialect XML definitions) and a runtime library (to do parsing, framing, CRC, and serialization).
Main components
A. MAVLink Dialect Definitions (Data Layer)
• Directory: mavlink/
• Contains XML files (e.g., common.xml, ardupilotmega.xml) that define messages, enums, and fields for each dialect.
B. Code Generator (Build/Tool Layer)
• File: tool/generate.dart
• Reads all XML dialect definitions under mavlink/
• Generates Dart source files under lib/dialects/ and updates lib/mavlink_dialect.dart
C. Generated Dart Dialect Modules (Source Layer)
• Directory: lib/dialects/
• For each dialect, a Dart file containing message classes and metadata
D. Core Runtime Library (API Layer)
• lib/crc.dart – cyclic redundancy check functions
• lib/mavlink_frame.dart – MAVLink packet framing/parsing logic
• lib/mavlink_parser.dart – state-machine parser for incoming byte streams
• lib/mavlink_message.dart – generic message wrapper and base class
• lib/mavlink_serialization (.serialize…) – converting messages to byte arrays
• lib/types.dart – common type definitions
• lib/mavlink_version.dart – version constants
E. Consumers
• example/ (example/parameter.dart, example/parser.dart) – demonstration code showing how to use the library
• test/ – suite of automated tests (unit and integration) that validate parsing and serialization
F. CI/CD and Configuration
• .github/workflows/dart.yml – GitHub Action to run tests on each push/pull request
• pubspec.yaml – package metadata and dependencies
• analysis_options.yaml – linting rules
Relationships and data flow
• The Code Generator reads the MAVLink XML files → emits Dart code under lib/dialects/
• The Core Runtime Library references the generated dialect modules to lookup message definitions at runtime
• The example code and the test suite both import the Core Runtime Library (and transitively the generated dialects) to parse or serialize messages
• CI/CD invokes test scripts which ensure correctness of parsing/serialization
Architectural patterns and design principles
• Clear separation of concerns:
• Data (XML dialects) vs. Code generation vs. Runtime library vs. Consumers
• Code-generation pipeline: XML → Dart source → compiled library
• Single crate/package: no microservices, but layered architecture
• Clear component names (use directory/file names as labels)
• Annotations for key technologies (Dart, XML) and processes (code generation, parsing, serialization)