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
// 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)]
/// DeviceInfo is a DeviceInfo message.
pub struct DeviceInfo {
    /// Units: s
    pub timestamp: typedef::DateTime,
    pub device_index: typedef::DeviceIndex,
    pub device_type: u8,
    pub manufacturer: typedef::Manufacturer,
    /// Base: UINT32Z
    pub serial_number: u32,
    pub product: u16,
    /// Scale: 100
    pub software_version: u16,
    pub hardware_version: u8,
    /// Units: s; Reset by new battery or charge.
    pub cum_operating_time: u32,
    /// Scale: 256; Units: V
    pub battery_voltage: u16,
    pub battery_status: typedef::BatteryStatus,
    /// Indicates the location of the sensor
    pub sensor_position: typedef::BodyLocation,
    /// Used to describe the sensor or location
    pub descriptor: String,
    /// Base: UINT8Z
    pub ant_transmission_type: u8,
    /// Base: UINT16Z
    pub ant_device_number: u16,
    pub ant_network: typedef::AntNetwork,
    pub source_type: typedef::SourceType,
    /// Optional free form string to indicate the devices name or model
    pub product_name: String,
    /// Units: %
    pub battery_level: u8,
    /// 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 DeviceInfo {
    /// Value's type: `u32`; Units: `s`
    pub const TIMESTAMP: u8 = 253;
    /// Value's type: `u8`
    pub const DEVICE_INDEX: u8 = 0;
    /// Value's type: `u8`
    pub const DEVICE_TYPE: u8 = 1;
    /// Value's type: `u16`
    pub const MANUFACTURER: u8 = 2;
    /// Value's type: `u32`; Base: UINT32Z
    pub const SERIAL_NUMBER: u8 = 3;
    /// Value's type: `u16`
    pub const PRODUCT: u8 = 4;
    /// Value's type: `u16`; Scale: `100`
    pub const SOFTWARE_VERSION: u8 = 5;
    /// Value's type: `u8`
    pub const HARDWARE_VERSION: u8 = 6;
    /// Value's type: `u32`; Units: `s`
    pub const CUM_OPERATING_TIME: u8 = 7;
    /// Value's type: `u16`; Scale: `256`; Units: `V`
    pub const BATTERY_VOLTAGE: u8 = 10;
    /// Value's type: `u8`
    pub const BATTERY_STATUS: u8 = 11;
    /// Value's type: `u8`
    pub const SENSOR_POSITION: u8 = 18;
    /// Value's type: `String`
    pub const DESCRIPTOR: u8 = 19;
    /// Value's type: `u8`; Base: UINT8Z
    pub const ANT_TRANSMISSION_TYPE: u8 = 20;
    /// Value's type: `u16`; Base: UINT16Z
    pub const ANT_DEVICE_NUMBER: u8 = 21;
    /// Value's type: `u8`
    pub const ANT_NETWORK: u8 = 22;
    /// Value's type: `u8`
    pub const SOURCE_TYPE: u8 = 25;
    /// Value's type: `String`
    pub const PRODUCT_NAME: u8 = 27;
    /// Value's type: `u8`; Units: `%`
    pub const BATTERY_LEVEL: u8 = 32;

    /// Create new DeviceInfo with all fields being set to its corresponding invalid value.
    pub const fn new() -> Self {
        Self {
            timestamp: typedef::DateTime(u32::MAX),
            device_index: typedef::DeviceIndex(u8::MAX),
            device_type: u8::MAX,
            manufacturer: typedef::Manufacturer(u16::MAX),
            serial_number: u32::MIN,
            product: u16::MAX,
            software_version: u16::MAX,
            hardware_version: u8::MAX,
            cum_operating_time: u32::MAX,
            battery_voltage: u16::MAX,
            battery_status: typedef::BatteryStatus(u8::MAX),
            sensor_position: typedef::BodyLocation(u8::MAX),
            descriptor: String::new(),
            ant_transmission_type: u8::MIN,
            ant_device_number: u16::MIN,
            ant_network: typedef::AntNetwork(u8::MAX),
            source_type: typedef::SourceType(u8::MAX),
            product_name: String::new(),
            battery_level: u8::MAX,
            unknown_fields: Vec::new(),
            developer_fields: Vec::new(),
        }
    }

    /// Returns `software_version` in its scaled value. It returns invalid f64 when value is valid.
    pub fn software_version_scaled(&self) -> f64 {
        if self.software_version == u16::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.software_version as f64 / 100.0 - 0.0
    }

    /// Set `software_version` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_software_version_scaled(&mut self, v: f64) -> &mut DeviceInfo {
        let unscaled = (v + 0.0) * 100.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
            self.software_version = u16::MAX;
            return self;
        }
        self.software_version = unscaled as u16;
        self
    }

    /// Returns `battery_voltage` in its scaled value. It returns invalid f64 when value is valid.
    pub fn battery_voltage_scaled(&self) -> f64 {
        if self.battery_voltage == u16::MAX {
            return f64::from_bits(u64::MAX);
        }
        self.battery_voltage as f64 / 256.0 - 0.0
    }

    /// Set `battery_voltage` with scaled value, it will automatically be converted to its corresponding integer value.
    pub fn set_battery_voltage_scaled(&mut self, v: f64) -> &mut DeviceInfo {
        let unscaled = (v + 0.0) * 256.0;
        if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
            self.battery_voltage = u16::MAX;
            return self;
        }
        self.battery_voltage = unscaled as u16;
        self
    }
}

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

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

        const KNOWN_NUMS: [u64; 4] = [4470869247, 0, 0, 2305843009213693952];
        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 {
            timestamp: typedef::DateTime(vals[253].as_u32()),
            device_index: typedef::DeviceIndex(vals[0].as_u8()),
            device_type: vals[1].as_u8(),
            manufacturer: typedef::Manufacturer(vals[2].as_u16()),
            serial_number: vals[3].as_u32z(),
            product: vals[4].as_u16(),
            software_version: vals[5].as_u16(),
            hardware_version: vals[6].as_u8(),
            cum_operating_time: vals[7].as_u32(),
            battery_voltage: vals[10].as_u16(),
            battery_status: typedef::BatteryStatus(vals[11].as_u8()),
            sensor_position: typedef::BodyLocation(vals[18].as_u8()),
            descriptor: vals[19].as_string(),
            ant_transmission_type: vals[20].as_u8z(),
            ant_device_number: vals[21].as_u16z(),
            ant_network: typedef::AntNetwork(vals[22].as_u8()),
            source_type: typedef::SourceType(vals[25].as_u8()),
            product_name: vals[27].as_string(),
            battery_level: vals[32].as_u8(),
            unknown_fields,
            developer_fields: mesg.developer_fields.clone(),
        }
    }
}

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

        if m.timestamp != typedef::DateTime(u32::MAX) {
            arr[len] = Field {
                num: 253,
                profile_type: ProfileType::DATE_TIME,
                value: Value::Uint32(m.timestamp.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.device_index != typedef::DeviceIndex(u8::MAX) {
            arr[len] = Field {
                num: 0,
                profile_type: ProfileType::DEVICE_INDEX,
                value: Value::Uint8(m.device_index.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.device_type != u8::MAX {
            arr[len] = Field {
                num: 1,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.device_type),
                is_expanded: false,
            };
            len += 1;
        }
        if m.manufacturer != typedef::Manufacturer(u16::MAX) {
            arr[len] = Field {
                num: 2,
                profile_type: ProfileType::MANUFACTURER,
                value: Value::Uint16(m.manufacturer.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.serial_number != u32::MIN {
            arr[len] = Field {
                num: 3,
                profile_type: ProfileType::UINT32Z,
                value: Value::Uint32(m.serial_number),
                is_expanded: false,
            };
            len += 1;
        }
        if m.product != u16::MAX {
            arr[len] = Field {
                num: 4,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.product),
                is_expanded: false,
            };
            len += 1;
        }
        if m.software_version != u16::MAX {
            arr[len] = Field {
                num: 5,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.software_version),
                is_expanded: false,
            };
            len += 1;
        }
        if m.hardware_version != u8::MAX {
            arr[len] = Field {
                num: 6,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.hardware_version),
                is_expanded: false,
            };
            len += 1;
        }
        if m.cum_operating_time != u32::MAX {
            arr[len] = Field {
                num: 7,
                profile_type: ProfileType::UINT32,
                value: Value::Uint32(m.cum_operating_time),
                is_expanded: false,
            };
            len += 1;
        }
        if m.battery_voltage != u16::MAX {
            arr[len] = Field {
                num: 10,
                profile_type: ProfileType::UINT16,
                value: Value::Uint16(m.battery_voltage),
                is_expanded: false,
            };
            len += 1;
        }
        if m.battery_status != typedef::BatteryStatus(u8::MAX) {
            arr[len] = Field {
                num: 11,
                profile_type: ProfileType::BATTERY_STATUS,
                value: Value::Uint8(m.battery_status.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.sensor_position != typedef::BodyLocation(u8::MAX) {
            arr[len] = Field {
                num: 18,
                profile_type: ProfileType::BODY_LOCATION,
                value: Value::Uint8(m.sensor_position.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.descriptor != String::new() {
            arr[len] = Field {
                num: 19,
                profile_type: ProfileType::STRING,
                value: Value::String(m.descriptor),
                is_expanded: false,
            };
            len += 1;
        }
        if m.ant_transmission_type != u8::MIN {
            arr[len] = Field {
                num: 20,
                profile_type: ProfileType::UINT8Z,
                value: Value::Uint8(m.ant_transmission_type),
                is_expanded: false,
            };
            len += 1;
        }
        if m.ant_device_number != u16::MIN {
            arr[len] = Field {
                num: 21,
                profile_type: ProfileType::UINT16Z,
                value: Value::Uint16(m.ant_device_number),
                is_expanded: false,
            };
            len += 1;
        }
        if m.ant_network != typedef::AntNetwork(u8::MAX) {
            arr[len] = Field {
                num: 22,
                profile_type: ProfileType::ANT_NETWORK,
                value: Value::Uint8(m.ant_network.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.source_type != typedef::SourceType(u8::MAX) {
            arr[len] = Field {
                num: 25,
                profile_type: ProfileType::SOURCE_TYPE,
                value: Value::Uint8(m.source_type.0),
                is_expanded: false,
            };
            len += 1;
        }
        if m.product_name != String::new() {
            arr[len] = Field {
                num: 27,
                profile_type: ProfileType::STRING,
                value: Value::String(m.product_name),
                is_expanded: false,
            };
            len += 1;
        }
        if m.battery_level != u8::MAX {
            arr[len] = Field {
                num: 32,
                profile_type: ProfileType::UINT8,
                value: Value::Uint8(m.battery_level),
                is_expanded: false,
            };
            len += 1;
        }

        Message {
            header: 0,
            num: typedef::MesgNum::DEVICE_INFO,
            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,
        }
    }
}