5 min read

Protocol

MAVLink

MAVLink is the standard protocol for ArduPilot and PX4 drones. It handles telemetry, commands, and mission uploads - often without authentication.

A v1 frame and a v2 frame look similar enough to be confused, and the difference is the two flag bytes that v2 inserted after the length. Read them in order; the message ID is three bytes little-endian, so a v1-aware parser misreads every v2 frame that follows.

# Field Width Meaning
1 STX 1 Always 0xFD for v2 (0xFE for v1)
2 LEN 1 Payload length in bytes, 0-255
3 INCOMPAT_FLAGS 1 Bit 0 set means a 13-byte signature is appended; other bits must be zero for the parser to accept the frame
4 COMPAT_FLAGS 1 Flags a parser may ignore without misreading the frame
5 SEQ 1 Per-link packet counter, wraps at 256; used for loss detection, not authentication
6 SYSID 1 Sending system ID
7 COMPID 1 Sending component ID (1 = autopilot, 190 = a GCS)
8 MSGID 3 24-bit message type, little-endian
9 PAYLOAD 0-255 Fields in the order the dialect defines, not the order they are documented in the XML
10 CRC 2 CRC-16/MCRF4XX over the frame plus the dialect's seed byte; a checksum, not a signature
11 SIGNATURE 13 Optional. Present only when INCOMPAT_FLAGS bit 0 is set: link ID (1), a 48-bit timestamp in 10-microsecond units since 1 January 2015, and a 48-bit signature that is the first 48 bits of SHA-256 over the 32-byte shared secret, the header, the payload, the CRC, the link ID and that timestamp

The checksum is what catches a corrupted frame, and it is worth being clear that it is not what anybody means by "unauthenticated": a CRC is trivially recomputed by anyone who can write a frame, which is exactly why the signature field exists. The timestamp inside that 13-byte trailer is checked per link and must increase, so a captured signed frame cannot be replayed either - authentication and anti-replay come from the same field.

Two of these have been superseded, and the replacements are not cosmetic: the legacy entries carry narrower fields and are deprecated in the specification, so a payload built against them is the wrong shape for a current autopilot.

Message ID Purpose
HEARTBEAT 0 System alive indicator, and the message that tells a client which system ID to address
COMMAND_LONG 76 Execute a command: 7 float parameters plus a command ID
COMMAND_INT 75 Same, with integer lat/lon in the last three parameters
Change flight mode 176 via 76/75 MAV_CMD_DO_SET_MODE - SET_MODE (11) is a deprecated message that used a bare mode bitmask
MISSION_ITEM_INT 73 Waypoint data with lat/lon as int32 scaled by 1e7 - the current form; MISSION_ITEM (39) carries them as floats and should not be used
RC_CHANNELS_OVERRIDE 70 Take stick input away from the real transmitter, for as long as the stream continues

You May Only Command an Aircraft You Own

Arming and landing are physical acts with a person and a property at the other end. The two calls below spin motors on a bench and cut them in the air; against a vehicle in flight they are an attempt to take the aircraft. In the United States that is 18 U.S.C. § 32 - the Aircraft Sabotage Act, enforced by the Department of Justice - and the transmission carrying the command is separately covered by 47 U.S.C. § 333. In the United Kingdom the Air Navigation Order and the Wireless Telegraphy Act 2006 apply; in the EU, Directive 2014/53/EU.

The lawful target is an airframe you own or have written permission to operate, and the only safe target is a simulator: PX4 SITL (make px4_sitl gz_x500) or ArduPilot SITL (sim_vehicle.py -v ArduCopter --console) accepts every command on this page and cannot hurt anyone. Get the sequence right there first - a MAVLink session does not warn you when it is about to work.

command_long_send takes eleven arguments: the target system, the target component, the command ID, a confirmation counter, and then param1 through param7. Which parameters mean anything depends on the command: for MAV_CMD_COMPONENT_ARM_DISARM (400), param1 is 1 to arm or 0 to disarm and param2 set to 21196 forces the disarm even in flight; for MAV_CMD_NAV_LAND (21) the parameters carry abort altitude, precision-landing mode, yaw, and a lat/lon/alt override - all zero means "land here". The confirmation counter stays at 0 on a first attempt and increments only when you are retrying the same command; some autopilots reject a repeated command that reuses 0.

mavlink-arm-land.py
from pymavlink import mavutil

# SITL only. 14550 is the telemetry port PX4 and ArduPilot SITL publish on.
conn = mavutil.mavlink_connection('udp:127.0.0.1:14550')
conn.wait_heartbeat()
print(f"system {conn.target_system} component {conn.target_component}")

# command_long_send(target_system, target_component, command, confirmation,
#                   param1, param2, param3, param4, param5, param6, param7)
conn.mav.command_long_send(
    conn.target_system, conn.target_component,
    mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,   # 400
    0,        # confirmation
    1,        # param1: 1 = arm, 0 = disarm
    0,        # param2: 21196 forces a disarm even when airborne
    0, 0, 0, 0, 0,
)

conn.mav.command_long_send(
    conn.target_system, conn.target_component,
    mavutil.mavlink.MAV_CMD_NAV_LAND,               # 21
    0,        # confirmation
    0, 0, 0, 0, 0, 0, 0,
)

# Always read the acknowledgement: ACCEPTED is 0, everything else is a refusal
ack = conn.recv_match(type='COMMAND_ACK', blocking=True, timeout=3)
print(ack.command, ack.result)

No Auth by Default

MAVLink v2 supports message signing but it's often disabled. Unsigned connections accept commands from any source.