1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::nineteen::header::decode_header;
use crate::packet::ensure_packet_size;
use crate::packet::lap::{DriverStatus, Lap, LapPacket, PitStatus, ResultStatus, Sector};
use bytes::{Buf, BytesMut};
use std::io::{Cursor, Error, ErrorKind};
use std::time::Duration;
pub const PACKET_SIZE: usize = 843;
pub fn decode_lap_data(cursor: &mut Cursor<&mut BytesMut>) -> Result<LapPacket, Error> {
ensure_packet_size(PACKET_SIZE, cursor)?;
let header = decode_header(cursor)?;
let mut laps = Vec::with_capacity(20);
for _ in 0..20 {
laps.push(Lap::new(
Duration::from_secs_f32(cursor.get_f32_le()),
Duration::from_secs_f32(cursor.get_f32_le()),
Duration::from_secs_f32(cursor.get_f32_le()),
Duration::from_secs_f32(cursor.get_f32_le()),
Duration::from_secs_f32(cursor.get_f32_le()),
cursor.get_f32_le(),
cursor.get_f32_le(),
Duration::from_secs_f32(cursor.get_f32_le()),
cursor.get_u8(),
cursor.get_u8(),
decode_pit_status(cursor)?,
decode_sector(cursor)?,
cursor.get_u8() < 1,
cursor.get_u8(),
cursor.get_u8(),
decode_driver_status(cursor)?,
decode_result_status(cursor)?,
));
}
Ok(LapPacket::new(header, laps))
}
fn decode_sector(cursor: &mut Cursor<&mut BytesMut>) -> Result<Sector, Error> {
let value = cursor.get_u8();
match value {
0 => Ok(Sector::First),
1 => Ok(Sector::Second),
2 => Ok(Sector::Third),
_ => Err(Error::new(
ErrorKind::InvalidData,
"Failed to decode sector.",
)),
}
}
fn decode_pit_status(cursor: &mut Cursor<&mut BytesMut>) -> Result<PitStatus, Error> {
let value = cursor.get_u8();
match value {
0 => Ok(PitStatus::None),
1 => Ok(PitStatus::Pitting),
2 => Ok(PitStatus::InPits),
_ => Err(Error::new(
ErrorKind::InvalidData,
"Failed to decode pit status.",
)),
}
}
fn decode_driver_status(cursor: &mut Cursor<&mut BytesMut>) -> Result<DriverStatus, Error> {
let value = cursor.get_u8();
match value {
0 => Ok(DriverStatus::InGarage),
1 => Ok(DriverStatus::FlyingLap),
2 => Ok(DriverStatus::InLap),
3 => Ok(DriverStatus::OutLap),
4 => Ok(DriverStatus::OnTrack),
_ => Err(Error::new(
ErrorKind::InvalidData,
"Failed to decode driver status.",
)),
}
}
fn decode_result_status(cursor: &mut Cursor<&mut BytesMut>) -> Result<ResultStatus, Error> {
let value = cursor.get_u8();
match value {
0 => Ok(ResultStatus::Invalid),
1 => Ok(ResultStatus::Inactive),
2 => Ok(ResultStatus::Active),
3 => Ok(ResultStatus::Finished),
4 => Ok(ResultStatus::Disqualified),
5 => Ok(ResultStatus::NotClassified),
6 => Ok(ResultStatus::Retired),
_ => Err(Error::new(
ErrorKind::InvalidData,
"Failed to decode result status.",
)),
}
}
#[cfg(test)]
mod tests {
use crate::nineteen::lap::{decode_lap_data, PACKET_SIZE};
use crate::packet::lap::{DriverStatus, PitStatus, ResultStatus, Sector};
use bytes::{BufMut, BytesMut};
use std::io::Cursor;
fn put_packet_header(mut bytes: BytesMut) -> BytesMut {
bytes.put_u16_le(2019);
bytes.put_u8(1);
bytes.put_u8(2);
bytes.put_u8(3);
bytes.put_u8(0);
bytes.put_u64_le(u64::max_value());
bytes.put_f32_le(1.0);
bytes.put_u32_le(u32::max_value());
bytes.put_u8(0);
bytes
}
#[test]
fn decode_lap_data_with_error() {
let mut bytes = BytesMut::with_capacity(0);
let mut cursor = Cursor::new(&mut bytes);
let packet = decode_lap_data(&mut cursor);
assert!(packet.is_err());
}
#[test]
fn decode_lap_data_with_success() {
let mut bytes = BytesMut::with_capacity(PACKET_SIZE);
bytes = put_packet_header(bytes);
bytes.put_f32_le(62.0);
bytes.put_f32_le(60.0);
bytes.put_f32_le(58.1);
bytes.put_f32_le(21.1);
bytes.put_f32_le(19.0);
bytes.put_f32_le(543.0);
bytes.put_f32_le(2048.4);
bytes.put_f32_le(0.0);
bytes.put_u8(1);
bytes.put_u8(4);
bytes.put_u8(0);
bytes.put_u8(2);
bytes.put_u8(0);
bytes.put_u8(0);
bytes.put_u8(3);
bytes.put_u8(1);
bytes.put_u8(2);
let padding = vec![0u8; 779];
bytes.put(padding.as_slice());
let mut cursor = Cursor::new(&mut bytes);
let packet = decode_lap_data(&mut cursor).unwrap();
let lap = packet.laps()[0];
assert_eq!(62, lap.last_lap_time().as_secs());
assert_eq!(60, lap.current_lap_time().as_secs());
assert_eq!(58, lap.best_lap_time().as_secs());
assert_eq!(21, lap.sector1_time().as_secs());
assert_eq!(19, lap.sector2_time().as_secs());
assert_eq!(543, lap.lap_distance() as usize);
assert_eq!(2048, lap.total_distance() as usize);
assert_eq!(0, lap.safety_car_delta().as_secs());
assert_eq!(1, lap.position());
assert_eq!(4, lap.current_lap_number());
assert_eq!(PitStatus::None, lap.pit_status());
assert_eq!(Sector::Third, lap.sector());
assert!(lap.is_valid_lap());
assert_eq!(0, lap.penalties());
assert_eq!(3, lap.grid_position());
assert_eq!(DriverStatus::FlyingLap, lap.driver_status());
assert_eq!(ResultStatus::Active, lap.result_status())
}
}