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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! Decoder for motion packets sent by F1 2019
//!
//! The motion packets by F1 2018 and F1 2019 differ only in their packet headers, the rest of the
//! packet format is identical.

use std::io::{Cursor, Error};

use bytes::{Buf, BytesMut};

use crate::nineteen::header::decode_header;
use crate::packet::ensure_packet_size;
use crate::packet::motion::{Motion, MotionPacket};
use crate::types::{CornerProperty, Property3D};

/// Size of the motion packet in bytes
pub const PACKET_SIZE: usize = 1343;

/// Decode a motion packet sent by F1 2019
///
/// F1 2018 and F1 2019 publish the same data in their motion packets, but with different packet
/// headers.
pub fn decode_motion(cursor: &mut Cursor<&mut BytesMut>) -> Result<MotionPacket, Error> {
    ensure_packet_size(PACKET_SIZE, cursor)?;

    let header = decode_header(cursor)?;
    let mut cars = Vec::with_capacity(20);

    for _ in 0..20 {
        cars.push(Motion::new(
            decode_position(cursor),
            decode_velocity(cursor),
            decode_forward_direction(cursor),
            decode_right_direction(cursor),
            decode_g_force(cursor),
            cursor.get_f32_le(),
            cursor.get_f32_le(),
            cursor.get_f32_le(),
        ))
    }

    Ok(MotionPacket::new(
        header,
        cars,
        decode_suspension_position(cursor),
        decode_suspension_velocity(cursor),
        decode_suspension_acceleration(cursor),
        decode_wheel_speed(cursor),
        decode_wheel_slip(cursor),
        decode_local_velocity(cursor),
        decode_angular_velocity(cursor),
        decode_angular_acceleration(cursor),
        cursor.get_f32_le(),
    ))
}

/// Decode position of the car
fn decode_position(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<f32> {
    Property3D::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode velocity of the car
fn decode_velocity(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<f32> {
    Property3D::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode forward direction of the car
fn decode_forward_direction(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<i16> {
    Property3D::new(
        cursor.get_i16_le(),
        cursor.get_i16_le(),
        cursor.get_i16_le(),
    )
}

/// Decode right direction of the car
fn decode_right_direction(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<i16> {
    Property3D::new(
        cursor.get_i16_le(),
        cursor.get_i16_le(),
        cursor.get_i16_le(),
    )
}

/// Decode G forces on the car
fn decode_g_force(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<f32> {
    Property3D::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode suspension position of the player's car
fn decode_suspension_position(cursor: &mut Cursor<&mut BytesMut>) -> CornerProperty<f32> {
    CornerProperty::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode suspension velocity of the player's car
fn decode_suspension_velocity(cursor: &mut Cursor<&mut BytesMut>) -> CornerProperty<f32> {
    CornerProperty::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode suspension acceleration of the player's car
fn decode_suspension_acceleration(cursor: &mut Cursor<&mut BytesMut>) -> CornerProperty<f32> {
    CornerProperty::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode the wheel speed of the player's car
fn decode_wheel_speed(cursor: &mut Cursor<&mut BytesMut>) -> CornerProperty<f32> {
    CornerProperty::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode the wheel slip of the player's car
fn decode_wheel_slip(cursor: &mut Cursor<&mut BytesMut>) -> CornerProperty<f32> {
    CornerProperty::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode the local velocity of the player's car
fn decode_local_velocity(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<f32> {
    Property3D::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

/// Decode the angular velocity of the player's car
fn decode_angular_velocity(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<f32> {
    Property3D::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}
/// Decode the angular acceleration of the player's car
fn decode_angular_acceleration(cursor: &mut Cursor<&mut BytesMut>) -> Property3D<f32> {
    Property3D::new(
        cursor.get_f32_le(),
        cursor.get_f32_le(),
        cursor.get_f32_le(),
    )
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use assert_approx_eq::assert_approx_eq;
    use bytes::{BufMut, BytesMut};

    use crate::nineteen::motion::{decode_motion, PACKET_SIZE};

    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_motion_with_error() {
        let mut bytes = BytesMut::with_capacity(0);
        let mut cursor = Cursor::new(&mut bytes);

        let packet = decode_motion(&mut cursor);
        assert!(packet.is_err());
    }

    #[test]
    fn decode_motion_with_success() {
        let mut bytes = BytesMut::with_capacity(PACKET_SIZE);
        bytes = put_packet_header(bytes);

        bytes.put_f32_le(1.0);
        bytes.put_f32_le(2.0);
        bytes.put_f32_le(3.0);
        bytes.put_f32_le(4.0);
        bytes.put_f32_le(5.0);
        bytes.put_f32_le(6.0);
        bytes.put_i16_le(7);
        bytes.put_i16_le(8);
        bytes.put_i16_le(9);
        bytes.put_i16_le(10);
        bytes.put_i16_le(11);
        bytes.put_i16_le(12);
        bytes.put_f32_le(13.0);
        bytes.put_f32_le(14.0);
        bytes.put_f32_le(15.0);
        bytes.put_f32_le(16.0);
        bytes.put_f32_le(17.0);
        bytes.put_f32_le(18.0);

        let padding = vec![0u8; 1140];
        bytes.put(padding.as_slice());

        bytes.put_f32_le(19.0);
        bytes.put_f32_le(20.0);
        bytes.put_f32_le(21.0);
        bytes.put_f32_le(22.0);
        bytes.put_f32_le(23.0);
        bytes.put_f32_le(24.0);
        bytes.put_f32_le(25.0);
        bytes.put_f32_le(26.0);
        bytes.put_f32_le(27.0);
        bytes.put_f32_le(28.0);
        bytes.put_f32_le(29.0);
        bytes.put_f32_le(30.0);
        bytes.put_f32_le(31.0);
        bytes.put_f32_le(32.0);
        bytes.put_f32_le(33.0);
        bytes.put_f32_le(34.0);
        bytes.put_f32_le(35.0);
        bytes.put_f32_le(36.0);
        bytes.put_f32_le(37.0);
        bytes.put_f32_le(38.0);
        bytes.put_f32_le(39.0);
        bytes.put_f32_le(40.0);
        bytes.put_f32_le(41.0);
        bytes.put_f32_le(42.0);
        bytes.put_f32_le(43.0);
        bytes.put_f32_le(44.0);
        bytes.put_f32_le(45.0);
        bytes.put_f32_le(46.0);
        bytes.put_f32_le(47.0);
        bytes.put_f32_le(48.0);

        let mut cursor = Cursor::new(&mut bytes);
        let packet = decode_motion(&mut cursor).unwrap();

        let motion = packet.cars()[0];
        assert_approx_eq!(1.0, motion.position().x());
        assert_approx_eq!(4.0, motion.velocity().x());
        assert_eq!(7, motion.forward_direction().x());
        assert_eq!(10, motion.right_direction().x());
        assert_approx_eq!(13.0, motion.g_force().x());
        assert_approx_eq!(16.0, motion.yaw());
        assert_approx_eq!(17.0, motion.pitch());
        assert_approx_eq!(18.0, motion.roll());
        assert_approx_eq!(19.0, packet.suspension_position().front_left());
        assert_approx_eq!(23.0, packet.suspension_velocity().front_left());
        assert_approx_eq!(27.0, packet.suspension_acceleration().front_left());
        assert_approx_eq!(31.0, packet.wheel_speed().front_left());
        assert_approx_eq!(35.0, packet.wheel_slip().front_left());
        assert_approx_eq!(39.0, packet.local_velocity().x());
        assert_approx_eq!(42.0, packet.angular_velocity().x());
        assert_approx_eq!(45.0, packet.angular_acceleration().x());
        assert_approx_eq!(48.0, packet.front_wheels_angle());
    }
}