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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// by Michael J. Simms
// Copyright (c) 2021 Michael J. Simms

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
 #![allow(dead_code)]

pub mod fit_file;

#[cfg(test)]
mod tests {
    use std::ffi::c_void;
    use std::collections::HashMap;
    extern crate csv;

    /// Called for each record message as it is processed.
    fn callback(timestamp: u32, global_message_num: u16, local_msg_type: u8, _message_index: u16, fields: Vec<crate::fit_file::FitFieldValue>, context: *mut c_void) {

        if global_message_num == crate::fit_file::GLOBAL_MSG_NUM_SESSION {
            let msg = crate::fit_file::FitSessionMsg::new(fields);
            let sport_names = crate::fit_file::init_sport_name_map();
            let sport_id = msg.sport.unwrap();

            println!("[Sport Message] {}", sport_names.get(&sport_id).unwrap());
        }
        else if global_message_num == crate::fit_file::GLOBAL_MSG_NUM_RECORD {
            let msg = crate::fit_file::FitRecordMsg::new(fields);
            let mut latitude = 0.0;
            let mut longitude = 0.0;
            let mut altitude = 0.0;
            let mut power = 0;
            let mut valid_location = true;

            match msg.position_lat {
                Some(res) => {

                    // Make sure we have a valid reading.
                    if res != 0x7FFFFFFF {
                        latitude = crate::fit_file::semicircles_to_degrees(res);
                    }
                    else {
                        valid_location = false;
                    }
                }
                None => {
                    valid_location = false;
                }
            }
            match msg.position_long {
                Some(res) => {

                    // Make sure we have a valid reading.
                    if res != 0x7FFFFFFF {
                        longitude = crate::fit_file::semicircles_to_degrees(res);
                    }
                    else {
                        valid_location = false;
                    }
                }
                None => {
                    valid_location = false;
                }
            }
            match msg.altitude {
                Some(res) => {

                    // Make sure we have a valid reading.
                    if res != 0xFFFF {
                        altitude = (res as f64 / 5.0) - 500.0;
                    }
                }
                None => {
                }
            }
            match msg.power {
                Some(res) => {
                    if res != 0xFFFF {
                        power = res;
                    }
                }
                None => {
                }
            }

            // Increment the number of records processed.
            let data: &mut Context = unsafe { &mut *(context as *mut Context) };
            data.num_records_processed = data.num_records_processed + 1;
            data.accumulated_power = data.accumulated_power + power as u64;

            if valid_location {
                println!("[Record Message] Timestamp: {} Latitude: {} Longitude: {} Altitude: {}", timestamp, latitude, longitude, altitude);
            }
            else {
                println!("[Record Message] Invalid location data");
            }
        }
        else {
            let global_message_names = crate::fit_file::init_global_msg_name_map();
            let mut field_num = 1;

            match global_message_names.get(&global_message_num) {
                Some(name) => print!("[{} Message] Timestamp {}, Values: ", name, timestamp),
                None => print!("[Global Message Num {} Local Message Type {}] Timestamp {}, Values: ", global_message_num, local_msg_type, timestamp)
            }

            for field in fields {
                print!("({}) Type: {}, Value: ", field_num, field.field_def);

                match field.field_type {
                    crate::fit_file::FieldType::FieldTypeNotSet => { print!("[not set] "); },
                    crate::fit_file::FieldType::FieldTypeUInt => { print!("{} ", field.value_uint); },
                    crate::fit_file::FieldType::FieldTypeSInt => { print!("{} ", field.value_sint); },
                    crate::fit_file::FieldType::FieldTypeFloat => { print!("{} ", field.value_float); },
                    crate::fit_file::FieldType::FieldTypeByteArray => {
                        for byte in field.value_byte_array.iter() {
                            print!("{:#04x} ", byte);
                        }
                    },
                    crate::fit_file::FieldType::FieldTypeStr => { print!("\"{}\" ", field.value_string); },
                }

                field_num = field_num + 1;
            }
            println!("");
        }
    }

    /// Context structure. An instance of this will be passed to the parser and ultimately to the callback function so we can use it for whatever.
    struct Context {
        num_records_processed: u16,
        accumulated_power: u64
    }

    impl Context {
        pub fn new() -> Self {
            let context = Context{ num_records_processed: 0, accumulated_power: 0 };
            context
        }
    }

    #[test]
    fn file1_zwift() {
        let file = std::fs::File::open("tests/20210218_zwift.fit").unwrap();
        let mut reader = std::io::BufReader::new(file);
        let mut context = Context::new();
        let context_ptr: *mut c_void = &mut context as *mut _ as *mut c_void;
        let fit = crate::fit_file::read(&mut reader, callback, context_ptr);

        match fit {
            Ok(fit) => {
                print!("FIT File Header: ");
                fit.header.print();
                println!("");
                println!("Num records processed: {}", context.num_records_processed);
                assert!(context.num_records_processed == 1163);
            }
            _ => { println!("Error"); },
        }
    }

    #[test]
    fn file2_bike() {
        let file = std::fs::File::open("tests/20191117_bike_wahoo_elemnt.fit").unwrap();
        let mut reader = std::io::BufReader::new(file);
        let mut context = Context::new();
        let context_ptr: *mut c_void = &mut context as *mut _ as *mut c_void;
        let fit = crate::fit_file::read(&mut reader, callback, context_ptr);

        match fit {
            Ok(fit) => {
                print!("FIT File Header: ");
                fit.header.print();
                println!("");
                println!("Num records processed: {}", context.num_records_processed);
                assert!(context.num_records_processed == 4876);
            }
            _ => { println!("Error"); },
        }
    }

    #[test]
    fn file3_swim() {
        let file = std::fs::File::open("tests/20200529_short_ocean_swim.fit").unwrap();
        let mut reader = std::io::BufReader::new(file);
        let mut context = Context::new();
        let context_ptr: *mut c_void = &mut context as *mut _ as *mut c_void;
        let fit = crate::fit_file::read(&mut reader, callback, context_ptr);

        match fit {
            Ok(fit) => {
                print!("FIT File Header: ");
                fit.header.print();
                println!("");
                println!("Num records processed: {}", context.num_records_processed);
                assert!(context.num_records_processed == 179);
            }
            _ => (),
        }
    }

    #[test]
    fn file4_run_with_power() {
        let file = std::fs::File::open("tests/20210507_run_coros_pace_2.fit").unwrap();
        let mut reader = std::io::BufReader::new(file);
        let mut context = Context::new();
        let context_ptr: *mut c_void = &mut context as *mut _ as *mut c_void;
        let fit = crate::fit_file::read(&mut reader, callback, context_ptr);

        match fit {
            Ok(fit) => {
                print!("FIT File Header: ");
                fit.header.print();
                println!("");
                println!("Num records processed: {}", context.num_records_processed);
                println!("Accumulated power: {}", context.accumulated_power);
                assert!(context.num_records_processed == 2364);
                assert!(context.accumulated_power == 634203);
            }
            _ => (),
        }
    }

    #[test]
    fn file5_track_run() {
        let file = std::fs::File::open("tests/20210610_track_garmin_fenix_6.fit").unwrap();
        let mut reader = std::io::BufReader::new(file);
        let mut context = Context::new();
        let context_ptr: *mut c_void = &mut context as *mut _ as *mut c_void;
        let fit = crate::fit_file::read(&mut reader, callback, context_ptr);

        match fit {
            Ok(fit) => {
                print!("FIT File Header: ");
                fit.header.print();
                println!("");
                println!("Num records processed: {}", context.num_records_processed);
                assert!(context.num_records_processed == 1672);
            }
            _ => (),
        }
    }

    fn convert_to_camel_case(name: &String) -> String {
        let mut new_name = String::new();
        let mut need_upper_case = true;

        for c in name.chars() { 
            if need_upper_case {
                new_name.push(c.to_ascii_uppercase());
                need_upper_case = false;
            }
            else if c == '_' {
                need_upper_case = true;
            }
            else {
                new_name.push(c);
            }
        }
        new_name
    }

    fn print_message_struct(name: String, field_map: &HashMap::<String, (u8, String)>) {
        let mut struct_name: String = "Fit".to_string();
        struct_name.push_str(&convert_to_camel_case(&name));
        struct_name.push_str("Msg");

        println!("pub struct {} {{", struct_name);
        for (field_name, (_field_id, field_type)) in field_map {
            println!("    pub {}: Option<{}>,", field_name, *field_type);
        }
        println!("}}");
        println!("");
        println!("impl {} {{", struct_name);
        println!("");
        println!("    /// Constructor: Takes the fields that were read by the file parser and puts them into a structure.");
        println!("    pub fn new(fields: Vec<FitFieldValue>) -> Self {{");
        print!("        let mut msg = {} {{ ", struct_name);
        let mut split_count = 0;
        for (field_name, _field_details) in field_map {
            print!("{}: None, ", field_name);
            if split_count % 3 == 0 {
                println!("");
                print!("            ");
            }
            split_count = split_count + 1;
        }
        println!("");
        println!("        }};");
        println!("");
        println!("        for field in fields {{");
        println!("            if !field.is_dev_field {{");
        println!("                match field.field_def {{");
        for (field_name, (field_id, field_type)) in field_map.iter() {
            println!("                    {} => {{ msg.{} = Some(field.get_{}()); }},", field_id, field_name, *field_type);
        }
        println!("");
        println!("                }}");
        println!("            }}");
        println!("        }}");
        println!("        msg");
        println!("    }}");
        println!("}}");
        println!("");
    }

    #[test]
    fn create_message_structs() {
        let file_path = "tests/Messages-Table.csv";
        let file = match std::fs::File::open(&file_path) {
            Err(why) => panic!("Couldn't open {} {}", file_path, why),
            Ok(file) => file,
        };

        let mut reader = csv::Reader::from_reader(file);
        let mut current_msg_name = String::new();
        let mut field_map = HashMap::<String, (u8, String)>::new();

        for record in reader.records() {
            let record = record.unwrap();

            // First column is the message name.
            let msg_name: String = record[0].parse().unwrap();
            if msg_name.len() > 0 {

                // Print the previous definition, if there is one.
                if current_msg_name.len() > 0 {
                    print_message_struct(current_msg_name, &field_map);
                }

                current_msg_name = String::from(msg_name);
                field_map.clear();
            }
            else {
                let field_id = &record[1];

                if field_id.len() > 0 {
                    let field_id_num: u8 = field_id.parse::<u8>().unwrap();
                    let field_name: String = record[2].parse().unwrap();
                    let mut field_type_str: String = record[3].parse().unwrap();

                    // Normalize the field type string.
                    if field_type_str == "byte" {
                        field_type_str = "u8".to_string();
                    }
                    else if field_type_str == "uint8" {
                        field_type_str = "u8".to_string();
                    }
                    else if field_type_str == "uint8z" {
                        field_type_str = "u8".to_string();
                    }
                    else if field_type_str == "uint16" {
                        field_type_str = "u16".to_string();
                    }
                    else if field_type_str == "uint16z" {
                        field_type_str = "u16".to_string();
                    }
                    else if field_type_str == "uint32" {
                        field_type_str = "u32".to_string();
                    }
                    else if field_type_str == "uint32z" {
                        field_type_str = "u32".to_string();
                    }
                    else if field_type_str == "sint8" {
                        field_type_str = "i8".to_string();
                    }
                    else if field_type_str == "sint16" {
                        field_type_str = "i16".to_string();
                    }
                    else if field_type_str == "sint32" {
                        field_type_str = "i32".to_string();
                    }
                    else if field_type_str == "float32" {
                        field_type_str = "f32".to_string();
                    }
                    else if field_type_str == "float64" {
                        field_type_str = "f64".to_string();
                    }

                    field_map.insert(field_name, (field_id_num, field_type_str));
                }
            }
        }
    }
}