# bluerobotics/mavlink-server architecture

> Architecture diagram of the GitHub repository bluerobotics/mavlink-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bluerobotics/mavlink-server
- Repository: https://github.com/bluerobotics/mavlink-server

## Overview

##### Main System Components
- MAVLink Server Core: At the heart, the main binary (src/main.rs) initializes and coordinates the various components of the service.
- Drivers Module: Organize the different communication interfaces found under src/lib/drivers. This includes protocols for TLog, Serial, TCP, UDP, Fake, Zenoh, and REST. Notice several submodules (like autopilot drivers and their parameter processing) that indicate specialized handling for drone-related commands.
- Hub Module: Under src/lib/hub, this acts like the central messaging or mediation layer that likely routes MAVLink messages between drivers.
- Web and API Components: The web interface (in src/lib/web routes and the separate frontend in src/webpage) supplies the UI (e.g., control panels, logs, dashboards) and REST endpoints (notably v1 endpoints) for software and vehicle control.
- CLI and Logger: Modules providing command line interfaces (src/lib/cli.rs) and logging facilities (src/lib/logger.rs) ensure operability and debugging.
- Stats and Protocol Modules: The stats collection (under src/lib/stats) aggregates performance metrics and message statistics, while protocol files manage MAVLink message formatting and the internal communication language.

##### Relationships and Data Flow
- Central Initialization: The main binary orchestrates the startup, initializing the Hub, Drivers, Web API, and CLI components.
- Messaging and Routing: The Hub mediates between drivers (incoming data from various sources like serial, tcp, etc.) and applies routing to the appropriate subsystem (e.g., forwarding telemetry data, commands, or status).
- Web Interface: The REST API and WebSocket routes provide external clients (like a browser or vehicle control system) with access to functionalities such as TLog, monitoring, and control commands.
- Logging & Metrics: Logger and Stats modules are integrated across other components to track performance, message frequencies, and possible error conditions.

##### Architectural Patterns
- Modular Design and Separation of Concerns: The clear separation of drivers, hub, web routes, and stats modules suggests a design that favors modularity and isolated responsibilities.
- Layered Architecture: There is a distinct separation between the low-level communication drivers, the core routing/mediation layer (hub), and the external interfaces (REST and Web UI).
- Extensibility: With multiple drivers implemented as submodules and clearly delineated REST routes, it’s easy to see how each piece can be replaced or extended without affecting the overall architecture.

##### Relevant Technologies and Details
- Rust and Cargo: Given the Cargo.toml files and Rust source structure, annotate that the implementation is in Rust.
- Communication Protocols: Highlight support for MAVLink communication over various protocols (e.g., serial, TCP, UDP, WebSocket, and REST) and the use of an internal MAVLink bus.
- Web Technologies: Emphasize the built-in web server and API endpoints (with assets in src/webpage) that provide the user interface.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Components
    Core["MAVLink Server Core"]:::core
    Hub["Hub Module"]:::core

    %% Drivers Module Subgraph
    subgraph "Drivers Module"
        Drivers["Drivers Module"]:::driver
        TLog["TLog Driver"]:::driver
        Serial["Serial Driver"]:::driver
        TCP["TCP Driver"]:::driver
        UDP["UDP Driver"]:::driver
        Zenoh["Zenoh Driver"]:::driver
        REST["REST Driver"]:::driver
        Autopilot["Autopilot Submodule"]:::driver
    end

    %% Web/API Interface Subgraph
    subgraph "Web/API Interface"
        BackendAPI["Backend API & Routing"]:::ui
        Frontend["Frontend Web Interface"]:::ui
    end

    %% Other Modules
    CLI["CLI Module"]:::misc
    Logger["Logger Module"]:::misc
    Stats["Stats Module"]:::misc
    ProtocolGlobal["Protocol Handling (Global)"]:::misc
    ProtocolHub["Protocol Handling (Hub)"]:::misc

    %% External Entities
    Clients["External Clients (Vehicles & Users)"]:::external

    %% Relationships
    Core -->|"init"| Hub
    Hub -->|"routes messages to"| Drivers
    Hub -->|"integrates"| ProtocolHub
    Hub -->|"exposes via"| BackendAPI
    Core -->|"executes"| CLI
    Core -->|"logs via"| Logger
    Core -->|"aggregates metrics in"| Stats
    BackendAPI -->|"serves data to"| Frontend
    Frontend -->|"provides interface to"| Clients
    BackendAPI -->|"receivesRequestsFrom"| Clients
    ProtocolGlobal -->|"formatsMessagesFor"| Hub

    %% Internal Drivers Breakdown
    Drivers --> TLog
    Drivers --> Serial
    Drivers --> TCP
    Drivers --> UDP
    Drivers --> Zenoh
    Drivers --> REST
    Drivers --> Autopilot

    %% Click Events
    click Core "https://github.com/bluerobotics/mavlink-server/blob/master/src/main.rs"
    click Drivers "https://github.com/bluerobotics/mavlink-server/tree/master/src/lib/drivers"
    click Hub "https://github.com/bluerobotics/mavlink-server/tree/master/src/lib/hub"
    click BackendAPI "https://github.com/bluerobotics/mavlink-server/tree/master/src/lib/web/routes/v1"
    click Frontend "https://github.com/bluerobotics/mavlink-server/tree/master/src/webpage"
    click CLI "https://github.com/bluerobotics/mavlink-server/blob/master/src/lib/cli.rs"
    click Logger "https://github.com/bluerobotics/mavlink-server/blob/master/src/lib/logger.rs"
    click Stats "https://github.com/bluerobotics/mavlink-server/tree/master/src/lib/stats"
    click ProtocolGlobal "https://github.com/bluerobotics/mavlink-server/blob/master/src/lib/protocol.rs"
    click ProtocolHub "https://github.com/bluerobotics/mavlink-server/blob/master/src/lib/hub/protocol.rs"

    %% Styles
    classDef core fill:#c6f7d0,stroke:#333,stroke-width:2px;
    classDef driver fill:#a0c4ff,stroke:#333,stroke-width:2px;
    classDef ui fill:#ffadad,stroke:#333,stroke-width:2px;
    classDef misc fill:#ffd6a5,stroke:#333,stroke-width:2px;
    classDef external fill:#fdffb6,stroke:#333,stroke-width:2px;
```
