rustyfit 0.5.0

This project hosts the Rust implementation for The Flexible and Interoperable Data Transfer (FIT) Protocol
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Code generated by fitgen/main.go. DO NOT EDIT.

// Copyright 2025 The RustyFIT Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#![allow(unused, clippy::comparison_to_empty, clippy::manual_range_patterns)]

use crate::profile::{ProfileType, typedef};
use crate::proto::*;

#[derive(Debug, Clone)]
/// DeviceSettings is a DeviceSettings message.
pub struct DeviceSettings {
    /// Index into time zone arrays.
    pub active_time_zone: u8,
    /// Offset from system time. Required to convert timestamp from system time to UTC.
    pub utc_offset: u32,
    /// Units: s; Offset from system time.
    pub time_offset: Vec<u32>,
    /// Display mode for the time
    pub time_mode: Vec<typedef::TimeMode>,
    /// Scale: 4; Units: hr; timezone offset in 1/4 hour increments
    pub time_zone_offset: Vec<i8>,
    /// Mode for backlight
    pub backlight_mode: typedef::BacklightMode,
    /// Enabled state of the activity tracker functionality
    pub activity_tracker_enabled: typedef::Bool,
    /// UTC timestamp used to set the devices clock and date
    pub clock_time: typedef::DateTime,
    /// Bitfield to configure enabled screens for each supported loop
    pub pages_enabled: Vec<u16>,
    /// Enabled state of the move alert
    pub move_alert_enabled: typedef::Bool,
    /// Display mode for the date
    pub date_mode: typedef::DateMode,
    pub display_orientation: typedef::DisplayOrientation,
    pub mounting_side: typedef::Side,
    /// Bitfield to indicate one page as default for each supported loop
    pub default_page: Vec<u16>,
    /// Units: steps; Minimum steps before an autosync can occur
    pub autosync_min_steps: u16,
    /// Units: minutes; Minimum minutes before an autosync can occur
    pub autosync_min_time: u16,
    /// Enable auto-detect setting for the lactate threshold feature.
    pub lactate_threshold_autodetect_enabled: typedef::Bool,
    /// Automatically upload using BLE
    pub ble_auto_upload_enabled: typedef::Bool,
    /// Helps to conserve battery by changing modes
    pub auto_sync_frequency: typedef::AutoSyncFrequency,
    /// Allows setting specific activities auto-activity detect enabled/disabled settings
    pub auto_activity_detect: typedef::AutoActivityDetect,
    /// Number of screens configured to display
    pub number_of_screens: u8,
    /// Smart Notification display orientation
    pub smart_notification_display_orientation: typedef::DisplayOrientation,
    pub tap_interface: typedef::Switch,
    /// Used to hold the tap threshold setting
    pub tap_sensitivity: typedef::TapSensitivity,
    /// unknown_fields are fields that are exist but they are not defined in Profile.xlsx
    pub unknown_fields: Vec<Field>,
    /// developer_fields are custom data fields (Added since protocol version 2.0)
    pub developer_fields: Vec<DeveloperField>,
}

impl DeviceSettings {
    /// Value's type: `u8`
    pub const ACTIVE_TIME_ZONE: u8 = 0;
    /// Value's type: `u32`
    pub const UTC_OFFSET: u8 = 1;
    /// Value's type: `Vec<u32>`; Units: `s`
    pub const TIME_OFFSET: u8 = 2;
    /// Value's type: `Vec<u8>`
    pub const TIME_MODE: u8 = 4;
    /// Value's type: `Vec<i8>`; Scale: `4`; Units: `hr`
    pub const TIME_ZONE_OFFSET: u8 = 5;
    /// Value's type: `u8`
    pub const BACKLIGHT_MODE: u8 = 12;
    /// Value's type: `u8`
    pub const ACTIVITY_TRACKER_ENABLED: u8 = 36;
    /// Value's type: `u32`
    pub const CLOCK_TIME: u8 = 39;
    /// Value's type: `Vec<u16>`
    pub const PAGES_ENABLED: u8 = 40;
    /// Value's type: `u8`
    pub const MOVE_ALERT_ENABLED: u8 = 46;
    /// Value's type: `u8`
    pub const DATE_MODE: u8 = 47;
    /// Value's type: `u8`
    pub const DISPLAY_ORIENTATION: u8 = 55;
    /// Value's type: `u8`
    pub const MOUNTING_SIDE: u8 = 56;
    /// Value's type: `Vec<u16>`
    pub const DEFAULT_PAGE: u8 = 57;
    /// Value's type: `u16`; Units: `steps`
    pub const AUTOSYNC_MIN_STEPS: u8 = 58;
    /// Value's type: `u16`; Units: `minutes`
    pub const AUTOSYNC_MIN_TIME: u8 = 59;
    /// Value's type: `u8`
    pub const LACTATE_THRESHOLD_AUTODETECT_ENABLED: u8 = 80;
    /// Value's type: `u8`
    pub const BLE_AUTO_UPLOAD_ENABLED: u8 = 86;
    /// Value's type: `u8`
    pub const AUTO_SYNC_FREQUENCY: u8 = 89;
    /// Value's type: `u32`
    pub const AUTO_ACTIVITY_DETECT: u8 = 90;
    /// Value's type: `u8`
    pub const NUMBER_OF_SCREENS: u8 = 94;
    /// Value's type: `u8`
    pub const SMART_NOTIFICATION_DISPLAY_ORIENTATION: u8 = 95;
    /// Value's type: `u8`
    pub const TAP_INTERFACE: u8 = 134;
    /// Value's type: `u8`
    pub const TAP_SENSITIVITY: u8 = 174;

    /// Create new DeviceSettings with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            active_time_zone: u8::MAX,
            utc_offset: u32::MAX,
            time_offset: Vec::<u32>::new(),
            time_mode: Vec::<typedef::TimeMode>::new(),
            time_zone_offset: Vec::<i8>::new(),
            backlight_mode: typedef::BacklightMode(u8::MAX),
            activity_tracker_enabled: typedef::Bool(u8::MAX),
            clock_time: typedef::DateTime(u32::MAX),
            pages_enabled: Vec::<u16>::new(),
            move_alert_enabled: typedef::Bool(u8::MAX),
            date_mode: typedef::DateMode(u8::MAX),
            display_orientation: typedef::DisplayOrientation(u8::MAX),
            mounting_side: typedef::Side(u8::MAX),
            default_page: Vec::<u16>::new(),
            autosync_min_steps: u16::MAX,
            autosync_min_time: u16::MAX,
            lactate_threshold_autodetect_enabled: typedef::Bool(u8::MAX),
            ble_auto_upload_enabled: typedef::Bool(u8::MAX),
            auto_sync_frequency: typedef::AutoSyncFrequency(u8::MAX),
            auto_activity_detect: typedef::AutoActivityDetect(u32::MAX),
            number_of_screens: u8::MAX,
            smart_notification_display_orientation: typedef::DisplayOrientation(u8::MAX),
            tap_interface: typedef::Switch(u8::MAX),
            tap_sensitivity: typedef::TapSensitivity(u8::MAX),
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `time_zone_offset` in its scaled value. It returns invalid f64 when value is valid.
    pub fn time_zone_offset_scaled(&self) -> Vec<f64> {
        if self.time_zone_offset == Vec::<i8>::new() {
            return Vec::new();
        }
        let mut v = Vec::with_capacity(self.time_zone_offset.len());
        for &x in &self.time_zone_offset {
            v.push(x as f64 / 4.0 - 0.0)
        }
        v
    }

    /// Set `time_zone_offset` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_time_zone_offset_scaled(&mut self, v: &Vec<f64>) -> &mut DeviceSettings {
        if v.is_empty() {
            self.time_zone_offset = Vec::new();
            return self;
        }
        self.time_zone_offset = Vec::with_capacity(v.len());
        for &x in v {
            let unscaled = (x + 0.0) * 4.0;
            if unscaled.is_nan() || unscaled.is_infinite() || unscaled > i8::MAX as f64 {
                self.time_zone_offset.push(i8::MAX);
                continue;
            }
            self.time_zone_offset.push(unscaled as i8);
        }
        self
    }
}

impl Default for DeviceSettings {
    fn default() -> Self {
        Self::new()
    }
}

impl From<&Message> for DeviceSettings {
    /// from creates new DeviceSettings struct based on given mesg.
    fn from(mesg: &Message) -> Self {
        let mut vals: [&Value; 175] = [const { &Value::Invalid }; 175];

        const KNOWN_NUMS: [u64; 4] = [1117105531807338551, 3326148608, 70368744177728, 0];
        let mut n = 0u64;
        for field in &mesg.fields {
            n += (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 ^ 1
        }
        let mut unknown_fields: Vec<Field> = Vec::with_capacity(n as usize);

        for field in &mesg.fields {
            if (KNOWN_NUMS[field.num as usize >> 6] >> (field.num & 63)) & 1 == 0 {
                unknown_fields.push(field.clone());
                continue;
            }
            vals[field.num as usize] = &field.value;
        }

        Self {
            active_time_zone: vals[0].as_u8(),
            utc_offset: vals[1].as_u32(),
            time_offset: vals[2].as_vec_u32(),
            time_mode: match &vals[4] {
                Value::VecUint8(v) => {
                    let mut vs = Vec::with_capacity(v.len());
                    for x in v {
                        vs.push(typedef::TimeMode(*x))
                    }
                    vs
                }
                _ => Vec::new(),
            },
            time_zone_offset: vals[5].as_vec_i8(),
            backlight_mode: typedef::BacklightMode(vals[12].as_u8()),
            activity_tracker_enabled: typedef::Bool(vals[36].as_u8()),
            clock_time: typedef::DateTime(vals[39].as_u32()),
            pages_enabled: vals[40].as_vec_u16(),
            move_alert_enabled: typedef::Bool(vals[46].as_u8()),
            date_mode: typedef::DateMode(vals[47].as_u8()),
            display_orientation: typedef::DisplayOrientation(vals[55].as_u8()),
            mounting_side: typedef::Side(vals[56].as_u8()),
            default_page: vals[57].as_vec_u16(),
            autosync_min_steps: vals[58].as_u16(),
            autosync_min_time: vals[59].as_u16(),
            lactate_threshold_autodetect_enabled: typedef::Bool(vals[80].as_u8()),
            ble_auto_upload_enabled: typedef::Bool(vals[86].as_u8()),
            auto_sync_frequency: typedef::AutoSyncFrequency(vals[89].as_u8()),
            auto_activity_detect: typedef::AutoActivityDetect(vals[90].as_u32()),
            number_of_screens: vals[94].as_u8(),
            smart_notification_display_orientation: typedef::DisplayOrientation(vals[95].as_u8()),
            tap_interface: typedef::Switch(vals[134].as_u8()),
            tap_sensitivity: typedef::TapSensitivity(vals[174].as_u8()),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

impl From<DeviceSettings> for Message {
    fn from(m: DeviceSettings) -> Self {
        let mut arr = [const {
            Field {
                num: 0,
                profile_type: ProfileType(0),
                value: Value::Invalid,
                is_expanded: false,
            }
        }; 24];
        let mut len = 0usize;

        if m.active_time_zone != u8::MAX {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.active_time_zone),
                is_expanded: false,
            };
            len += 1;
        }
        if m.utc_offset != u32::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.utc_offset),
                is_expanded: false,
            };
            len += 1;
        }
        if m.time_offset != Vec::<u32>::new() {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::UINT32,
                value: Value::VecUint32(m.time_offset),
                is_expanded: false,
            };
            len += 1;
        }
        if m.time_mode != Vec::<typedef::TimeMode>::new() {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::TIME_MODE,
                value: Value::VecUint8({
                    let mut v = Vec::with_capacity(m.time_mode.len());
                    for x in &m.time_mode {
                        v.push(x.0)
                    }
                    v
                }),
                is_expanded: false,
            };
            len += 1;
        }
        if m.time_zone_offset != Vec::<i8>::new() {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::SINT8,
                value: Value::VecInt8(m.time_zone_offset),
                is_expanded: false,
            };
            len += 1;
        }
        if m.backlight_mode != typedef::BacklightMode(u8::MAX) {
            arr[len] = Field {
                num: 12,
                profile_type: ProfileType::BACKLIGHT_MODE,
                value: Value::Uint8(m.backlight_mode.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.activity_tracker_enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 36,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.activity_tracker_enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.clock_time != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 39,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.clock_time.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.pages_enabled != Vec::<u16>::new() {
            arr[len] = Field {
                num: 40,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.pages_enabled),
                is_expanded: false,
            };
            len += 1;
        }
        if m.move_alert_enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 46,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.move_alert_enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.date_mode != typedef::DateMode(u8::MAX) {
            arr[len] = Field {
                num: 47,
                profile_type: ProfileType::DATE_MODE,
                value: Value::Uint8(m.date_mode.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.display_orientation != typedef::DisplayOrientation(u8::MAX) {
            arr[len] = Field {
                num: 55,
                profile_type: ProfileType::DISPLAY_ORIENTATION,
                value: Value::Uint8(m.display_orientation.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.mounting_side != typedef::Side(u8::MAX) {
            arr[len] = Field {
                num: 56,
                profile_type: ProfileType::SIDE,
                value: Value::Uint8(m.mounting_side.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.default_page != Vec::<u16>::new() {
            arr[len] = Field {
                num: 57,
                profile_type: ProfileType::UINT16,
                value: Value::VecUint16(m.default_page),
                is_expanded: false,
            };
            len += 1;
        }
        if m.autosync_min_steps != u16::MAX {
            arr[len] = Field {
                num: 58,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.autosync_min_steps),
                is_expanded: false,
            };
            len += 1;
        }
        if m.autosync_min_time != u16::MAX {
            arr[len] = Field {
                num: 59,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.autosync_min_time),
                is_expanded: false,
            };
            len += 1;
        }
        if m.lactate_threshold_autodetect_enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 80,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.lactate_threshold_autodetect_enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.ble_auto_upload_enabled != typedef::Bool(u8::MAX) {
            arr[len] = Field {
                num: 86,
                profile_type: ProfileType::BOOL,
                value: Value::Uint8(m.ble_auto_upload_enabled.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.auto_sync_frequency != typedef::AutoSyncFrequency(u8::MAX) {
            arr[len] = Field {
                num: 89,
                profile_type: ProfileType::AUTO_SYNC_FREQUENCY,
                value: Value::Uint8(m.auto_sync_frequency.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.auto_activity_detect != typedef::AutoActivityDetect(u32::MAX) {
            arr[len] = Field {
                num: 90,
                profile_type: ProfileType::AUTO_ACTIVITY_DETECT,
                value: Value::Uint32(m.auto_activity_detect.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.number_of_screens != u8::MAX {
            arr[len] = Field {
                num: 94,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.number_of_screens),
                is_expanded: false,
            };
            len += 1;
        }
        if m.smart_notification_display_orientation != typedef::DisplayOrientation(u8::MAX) {
            arr[len] = Field {
                num: 95,
                profile_type: ProfileType::DISPLAY_ORIENTATION,
                value: Value::Uint8(m.smart_notification_display_orientation.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.tap_interface != typedef::Switch(u8::MAX) {
            arr[len] = Field {
                num: 134,
                profile_type: ProfileType::SWITCH,
                value: Value::Uint8(m.tap_interface.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.tap_sensitivity != typedef::TapSensitivity(u8::MAX) {
            arr[len] = Field {
                num: 174,
                profile_type: ProfileType::TAP_SENSITIVITY,
                value: Value::Uint8(m.tap_sensitivity.0),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::DEVICE_SETTINGS,
            fields: {
                let mut fields: Vec<Field> = Vec::with_capacity(len + m.unknown_fields.len());
                fields.extend_from_slice(&arr[..len]);
                fields.extend_from_slice(&m.unknown_fields);
                fields
            },
            developer_fields: m.developer_fields,
        }
    }
}