f1_api/packet/header.rs
1//! Header prefixing packets from modern F1 games
2
3use std::fmt;
4use std::fmt::Display;
5use std::time::Duration;
6
7use derive_new::new;
8use getset::{CopyGetters, Getters};
9
10use crate::types::VehicleIndex;
11
12/// Supported API specifications
13///
14/// The modern F1 games have their own API specifications, each an evolution of the previous one.
15/// Since the data published by each game is unique in one way or another, support for additional
16/// API specs has to be implemented manually.
17#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
18pub enum ApiSpec {
19 Nineteen,
20}
21
22/// Packets sent by F1 games
23///
24/// The modern F1 games have divided their telemetry output into multiple packets, which can be sent
25/// at different intervals based on how quickly their data changes.
26#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
27pub enum PacketType {
28 Event,
29 Lap,
30 Motion,
31 Participants,
32 Session,
33 Setup,
34 Status,
35 Telemetry,
36}
37
38/// Version number of the game
39///
40/// The modern F1 games include their version number in the packet header. The games are versioned
41/// using the scheme `MAJOR.MINOR`.
42///
43/// TODO Test that partial order works correctly with version numbers
44#[derive(
45 new, Debug, Getters, CopyGetters, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash, Default,
46)]
47pub struct GameVersion {
48 /// Returns the major version of the game.
49 #[getset(get_copy = "pub")]
50 major: u8,
51
52 /// Returns the minor version of the game.
53 #[getset(get_copy = "pub")]
54 minor: u8,
55}
56
57impl Display for GameVersion {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 write!(f, "{}.{}", self.major, self.minor)
60 }
61}
62
63/// Header prefixing each packet
64///
65/// The modern F1 games use versioned API specifications. Each packet is prefixed with a header that
66/// declares which version of the specification the packet adheres to. This information is required
67/// to decode the packet correctly. Because it is only relevant for decoding the packet, the packet
68/// format, type, and version from the specifications are not republished.
69///
70/// The header also contains information about the session the packet belongs to, and about the time
71/// the packet was created.
72///
73/// TODO Verify that the session tie can be represented as a duration
74#[derive(new, Debug, Getters, CopyGetters, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
75pub struct Header {
76 /// Returns the API specification that was used to decode the packet.
77 #[getset(get_copy = "pub")]
78 api_spec: ApiSpec,
79
80 /// Returns the version of the game.
81 #[getset(get = "pub")]
82 game_version: Option<GameVersion>,
83
84 /// Returns the type of the packet.
85 ///
86 /// The packet type is only required to determine how to decode the packet. After decoding it,
87 /// the packet type is represented by Rust's type system.
88 #[getset(get_copy = "pub")]
89 packet_type: PacketType,
90
91 /// Returns the unique session UID.
92 #[getset(get_copy = "pub")]
93 session_uid: u64,
94
95 /// Returns the session time at the time the packet was sent.
96 #[getset(get = "pub")]
97 session_time: Duration,
98
99 /// Returns the frame identifier at the time the packet was sent.
100 #[getset(get_copy = "pub")]
101 frame_identifier: u32,
102
103 /// Returns the player's car index.
104 ///
105 /// The setups and status of cars are published as arrays. This field indicates which position
106 /// in these arrays the player's car has.
107 #[getset(get_copy = "pub")]
108 player_car_index: VehicleIndex,
109}
110
111impl Display for Header {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113 let game_version = match self.game_version {
114 Some(version) => format!("{}", version),
115 None => String::from("None"),
116 };
117
118 write!(
119 f,
120 "Header {{ game_version: {}, session: {}, time: {}s, frame: {}, player_car_index: {} }}",
121 game_version,
122 self.session_uid,
123 self.session_time.as_secs(),
124 self.frame_identifier,
125 self.player_car_index
126 )
127 }
128}