Using C MAVLink Libraries (mavgen)
The MAVLink C library generated by mavgen is a header-only implementation that is highly optimized for resource-constrained systems with limited RAM and flash memory. It is field-proven and deployed in many products where it serves as interoperability interface between components of different manufacturers.
This topic explains how to get and use the library.
The MAVLink 2 library supports both MAVLink 2 and MAVLink 1, and has been built with all standard dialects in the mavlink/mavlink repo. It supersedes the MAVLink 1 library (c_library_v1), and should be used by preference.
The libraries can be placed/generated anywhere in your project tree. The example below shows them located in: generated/include/mavlink/v2.0/.
To use MAVLink in your C project, include the mavlink.h header file in your project:
If headers for multiple dialects and/or versions are installed, your include path might instead look similar to the following:
Do not include the individual message files. If you generate your own headers, you will have to add their output location to your C compiler's search path.
When compiling the project, we recommend that you specify the top-level output directory AND all generated dialects and versions (this will give the greatest compatibility with existing code and examples):
$ gcc ... -I generated/include -I generated/include/mavlink/v2.0/common ...
Multiple Streams ("channels")
The C MAVLink library utilizes a "channel" metaphor to allow for simultaneous processing of multiple, independent MAVLink streams in the same program. It is therefore important to use the correct channel for each operation as all receiving and transmitting functions provided by MAVLink require a channel.
If only one MAVLink stream exists, channel 0 should be used by specifying the
MAVLink reception is then done using
Transmitting messages can be done by using the
mavlink_msg_*_pack() function, where one is defined for every message. The packed message can then be serialized with
mavlink_helpers.h:mavlink_msg_to_send_buffer() and then writing the resultant byte array out over the appropriate serial interface.
It is possible to simplify the above by writing wrappers around the transmitting/receiving code. A multi-byte writing macro can be defined,
MAVLINK_SEND_UART_BYTES(), or a single-byte function can be defined,
comm_send_ch(), that wrap the low-level driver for transmitting the data. If this is done,
MAVLINK_USE_CONVENIENCE_FUNCTIONS must be defined.
Message Signing (authentication) allows a MAVLink 2 system to verify that messages originate from a trusted source.
The C libraries generated by mavgen provide almost everything needed to support message signing in your MAVLink system. The topic C Message Signing explains the remaining code that a system must implement to enable signing using the C library.