f1_api/
nineteen.rs

1//! API specification for F1 2019.
2//!
3//! F1 2019 publishes session and telemetry data through a UDP interface. It defines several
4//! different packet types, each containing a particular set of data. These packets are published at
5//! different intervals depending on how quickly their data changes.
6//!
7//! The full API specification can be found here:
8//! https://forums.codemasters.com/topic/44592-f1-2019-udp-specification/
9
10use std::io::{Cursor, Error};
11
12use bytes::BytesMut;
13
14use crate::nineteen::event::decode_event;
15use crate::nineteen::header::decode_header;
16use crate::nineteen::lap::decode_lap_data;
17use crate::nineteen::motion::decode_motion;
18use crate::nineteen::participants::decode_participants;
19use crate::nineteen::session::decode_session;
20use crate::nineteen::setup::decode_setups;
21use crate::nineteen::status::decode_statuses;
22use crate::nineteen::telemetry::decode_telemetry;
23use crate::packet::header::PacketType;
24use crate::packet::Packet;
25
26mod header;
27
28pub mod event;
29pub mod flag;
30pub mod lap;
31pub mod motion;
32pub mod participants;
33pub mod session;
34pub mod setup;
35pub mod status;
36pub mod telemetry;
37
38/// Flags shown in F1 2019.
39///
40/// Flags are an essential tool to communicate the status of a race to the drivers on track. A green
41/// flag signals the race start or restart, while a yellow flag warns of hazards on track. The red
42/// flag aborts a race or session. The blue flag signals that a faster car is approaching from
43/// behind.
44pub enum Flag {
45    Invalid = -1,
46    None = 0,
47    Green = 1,
48    Blue = 2,
49    Yellow = 3,
50    Red = 4,
51}
52
53/// Index referencing a car in the packet payloads
54///
55/// Data for all vehicles is provided as an array. References to the data in
56/// this array are made in the form of a vehicle index.
57pub type VehicleIndex = u8;
58
59/// Decode a packet sent by F1 2019
60///
61/// F1 2019 defines its own API specification that is implemented in the `nineteen` module. For each
62/// packet type defined in the API specification, a decoder function exists that maps the packet
63/// from F1 2019 to the unified packet format of this crate.
64pub fn decode_nineteen(cursor: &mut Cursor<&mut BytesMut>) -> Result<Packet, Error> {
65    let header = decode_header(cursor)?;
66    cursor.set_position(0);
67
68    let packet = match header.packet_type() {
69        PacketType::Event => Packet::Event(decode_event(cursor)?),
70        PacketType::Lap => Packet::Lap(decode_lap_data(cursor)?),
71        PacketType::Motion => Packet::Motion(decode_motion(cursor)?),
72        PacketType::Participants => Packet::Participants(decode_participants(cursor)?),
73        PacketType::Session => Packet::Session(decode_session(cursor)?),
74        PacketType::Setup => Packet::Setup(decode_setups(cursor)?),
75        PacketType::Status => Packet::Status(decode_statuses(cursor)?),
76        PacketType::Telemetry => Packet::Telemetry(decode_telemetry(cursor)?),
77    };
78
79    Ok(packet)
80}