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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
mod consts;
pub mod data_field;
mod get_field_offset;
mod get_field_scale;
mod get_field_string_value;
mod get_field_type;
pub mod io;
pub mod macros;
pub mod message_type;
pub mod value;

use crate::protocol::consts::{
    COMPRESSED_HEADER_LOCAL_MESSAGE_NUMBER_MASK, COMPRESSED_HEADER_MASK,
    COMPRESSED_HEADER_TIME_OFFSET_MASK, CRC_TABLE, DEFINITION_HEADER_MASK, DEVELOPER_FIELDS_MASK,
    FIELD_DEFINITION_BASE_ENDIAN, FIELD_DEFINITION_BASE_NUMBER, LOCAL_MESSAGE_NUMBER_MASK,
};
use crate::protocol::data_field::DataField;
use crate::protocol::get_field_string_value::FieldType;
use crate::protocol::message_type::MessageType;
use binrw::{binrw, BinRead, BinResult, BinWrite, Endian};
use std::fmt::Debug;
use std::io::{Seek, Write};

pub type MatchScaleFn = fn(usize) -> Option<f32>;
pub type MatchOffsetFn = fn(usize) -> Option<i16>;
pub type MatchFieldTypeFn = fn(usize) -> FieldType;

#[derive(Debug, Clone)]
#[binrw]
#[brw(little)]
pub struct FitHeader {
    /// https://developer.garmin.com/fit/protocol/
    /// Indicates the length of this file header including header size. Minimum size is 12.
    /// This may be increased in future to add additional optional information
    pub header_size: u8,

    /// Protocol version number as provided in SDK
    pub protocol_version: u8,

    /// Profile version number as provided in SDK
    pub profile_version: u16,

    /// Length of the Data Records section in bytesDoes not include Header or CRC
    pub data_size: u32,

    /// ASCII values for “.FIT”. A FIT binary file opened with a text editor will contain a readable “.FIT” in the first line.
    #[br(map = | x: [u8;4] | String::from_utf8_lossy(&x).to_string())]
    #[bw(map = | _ | ".FIT".as_bytes())]
    pub data_type: String,

    /// Contains the value of the CRC (see CRC ) of Bytes 0 through 11, or may be set to 0x0000. This field is optional.
    #[br(if(header_size == 14))]
    pub crc: Option<u16>,
}

#[derive(Clone, Debug)]
pub enum FitMessage {
    Definition(FitDefinitionMessage),
    Data(FitDataMessage),
}

#[derive(BinWrite, Debug, Clone, PartialEq)]
#[bw(little)]
pub struct FitDefinitionMessage {
    pub header: FitMessageHeader,
    pub data: DefinitionMessage,
}

#[derive(Clone, Debug, PartialEq)]
#[binrw]
#[br(import(dev_fields: bool))]
#[bw(little)]
pub struct DefinitionMessage {
    pub reserved: u8,

    #[br(map = DefinitionMessage::read_endian)]
    #[bw(map = DefinitionMessage::write_endian)]
    pub endian: Endian,

    #[br(is_little = (endian == Endian::Little))]
    pub global_message_number: u16,

    pub num_fields: u8,

    #[br(count = num_fields)]
    pub fields: Vec<FieldDefinition>,

    #[br(if(dev_fields))]
    pub dev_num_fields: Option<u8>,

    #[br(if(dev_fields), count = dev_num_fields.unwrap_or_default())]
    pub dev_fields: Option<Vec<DevFieldDefinition>>,
}

impl DefinitionMessage {
    fn read_endian(x: u8) -> Endian {
        if x == 0x0 {
            return Endian::Little;
        }
        Endian::Big
    }
    fn write_endian(b: &Endian) -> u8 {
        match b {
            Endian::Big => 0x1,
            Endian::Little => 0x0,
        }
    }

    fn new(
        is_big: bool,
        num_fields: u8,
        fields: Vec<FieldDefinition>,
        msg_type: MessageType,
    ) -> Self {
        let endian = match is_big {
            true => Endian::Big,
            false => Endian::Little,
        };
        Self {
            reserved: 0,
            endian,
            global_message_number: msg_type.to_primitive(),
            num_fields,
            fields,
            dev_num_fields: None,
            dev_fields: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct FitDataMessage {
    pub header: FitMessageHeader,
    pub data: DataMessage,
}

#[derive(Clone, Debug, PartialEq, BinRead)]
#[br(import(definition: &FitDefinitionMessage))]
pub struct DataMessage {
    #[br(parse_with = message_type::parse_message_type, args(definition.data.global_message_number))]
    pub message_type: MessageType,

    #[br(parse_with = DataField::parse_data_field, args(message_type, &definition.data.fields), is_little = (definition.data.endian == Endian::Little))]
    pub values: Vec<DataField>,
}

impl DataMessage {
    pub fn write<W>(&self, writer: &mut W, def_msg: &DefinitionMessage) -> BinResult<()>
    where
        W: Write + Seek,
    {
        DataField::write_data_field(
            &self.values,
            writer,
            Endian::Little,
            (self.message_type, def_msg),
        )
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[binrw]
pub struct FieldDefinition {
    pub definition_number: u8,

    pub size: u8,

    pub base_type: FieldDefBaseType,
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[binrw]
#[br(map = FieldDefBaseType::from_bytes)]
#[bw(map = FieldDefBaseType::to_bytes)]
pub struct FieldDefBaseType {
    pub val: u8,

    pub endian: Endian,
}

impl FieldDefBaseType {
    fn to_bytes(&self) -> u8 {
        if self.endian == Endian::Little {
            self.val
        } else {
            self.val | FIELD_DEFINITION_BASE_ENDIAN
        }
    }

    fn from_bytes(x: u8) -> FieldDefBaseType {
        let mut endian = Endian::Little;
        if x & FIELD_DEFINITION_BASE_ENDIAN == FIELD_DEFINITION_BASE_ENDIAN {
            endian = Endian::Big;
        }
        Self {
            val: x & FIELD_DEFINITION_BASE_NUMBER,
            endian,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[binrw]
pub struct DevFieldDefinition {
    pub field_number: u8,
    pub size: u8,
    pub dev_data_index: u8,
}

#[derive(Clone, Debug, PartialEq)]
#[binrw]
#[br(map = FitMessageHeader::from_bytes)]
#[bw(map = FitMessageHeader::to_bytes)]
pub struct FitMessageHeader {
    pub compressed_header: bool,
    pub definition: bool,
    pub dev_fields: bool,
    pub local_num: u8,
    pub time_offset: Option<u8>,
}

impl FitMessageHeader {
    fn to_bytes(&self) -> u8 {
        if self.compressed_header {
            let mut x = self.time_offset.unwrap_or_default();
            x |= self.local_num << 5;
            x |= COMPRESSED_HEADER_MASK;
            x
        } else {
            let mut x = self.local_num;
            if self.definition {
                x |= DEFINITION_HEADER_MASK;
            }
            if self.dev_fields {
                x |= DEVELOPER_FIELDS_MASK;
            }
            x
        }
    }

    fn from_bytes(x: u8) -> FitMessageHeader {
        if (x & COMPRESSED_HEADER_MASK) == COMPRESSED_HEADER_MASK {
            Self {
                compressed_header: true,
                definition: false,
                dev_fields: false,
                local_num: (x & COMPRESSED_HEADER_LOCAL_MESSAGE_NUMBER_MASK) >> 5,
                time_offset: Some(x & COMPRESSED_HEADER_TIME_OFFSET_MASK),
            }
        } else {
            Self {
                compressed_header: false,
                definition: x & DEFINITION_HEADER_MASK == DEFINITION_HEADER_MASK,
                dev_fields: x & DEVELOPER_FIELDS_MASK == DEVELOPER_FIELDS_MASK,
                local_num: x & LOCAL_MESSAGE_NUMBER_MASK,
                time_offset: None,
            }
        }
    }

    fn new(is_def: bool, local_num: u8) -> Self {
        Self {
            compressed_header: false,
            definition: is_def,
            dev_fields: false,
            local_num,
            time_offset: None,
        }
    }
}

pub(crate) fn fit_crc_get16(crc: u16, byte: u8) -> u16 {
    // Compute checksum of lower four bits of byte
    let mut tmp = CRC_TABLE[(crc & 0xF) as usize];
    let mut crc = (crc >> 4) & 0x0FFF;
    crc = crc ^ tmp ^ CRC_TABLE[(byte & 0xF) as usize];

    // Now compute checksum of upper four bits of byte
    tmp = CRC_TABLE[(crc & 0xF) as usize];
    crc = (crc >> 4) & 0x0FFF;
    crc = crc ^ tmp ^ CRC_TABLE[((byte >> 4) & 0xF) as usize];

    crc
}

pub(crate) fn calculate_fit_crc(header: &[u8]) -> u16 {
    let mut crc: u16 = 0;
    for &byte in header {
        crc = fit_crc_get16(crc, byte);
    }
    crc
}

#[cfg(test)]
mod tests {
    use crate::protocol::calculate_fit_crc;

    #[test]
    fn fit_crc_get16_test() {
        let example_header: [u8; 12] = [
            0x0E, 0x10, 0x90, 0x05, 0xB3, 0x10, 0x00, 0x00, 0x2E, 0x46, 0x49, 0x54,
        ];
        let header_crc = calculate_fit_crc(&example_header);
        println!("The CRC of the FIT header is: 0x{:X}", header_crc);
        assert_eq!(0x800, header_crc);
    }
}