#![allow(clippy::manual_range_patterns)]
use crate::profile::typedef::{self, FitBaseType};
use crate::proto::*;
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};
fn is_expanded(state: &[u8], num: u8) -> bool {
match num {
0 | 9 => (state[num as usize >> 3] >> (num & 7)) & 1 == 1,
_ => false,
}
}
#[cfg_attr(feature = "serde", derive(Deserialize), serde(from = "De"))]
#[derive(Debug, Clone)]
pub struct Hr {
pub timestamp: typedef::DateTime,
pub fractional_timestamp: u16,
pub time256: u8,
pub filtered_bpm: Vec<u8>,
pub event_timestamp: Vec<u32>,
pub event_timestamp_12: Vec<u8>,
state: [u8; 2], pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl Hr {
pub const TIMESTAMP: u8 = 253;
pub const FRACTIONAL_TIMESTAMP: u8 = 0;
pub const TIME256: u8 = 1;
pub const FILTERED_BPM: u8 = 6;
pub const EVENT_TIMESTAMP: u8 = 9;
pub const EVENT_TIMESTAMP_12: u8 = 10;
pub const fn new() -> Self {
Self {
timestamp: typedef::DateTime(u32::MAX),
fractional_timestamp: u16::MAX,
time256: u8::MAX,
filtered_bpm: Vec::new(),
event_timestamp: Vec::new(),
event_timestamp_12: Vec::new(),
state: [0u8; 2],
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn fractional_timestamp_scaled(&self) -> Option<f64> {
if self.fractional_timestamp == u16::MAX {
return None;
}
Some(self.fractional_timestamp as f64 / 32768.0 - 0.0)
}
pub fn set_fractional_timestamp_scaled(&mut self, v: f64) -> &mut Self {
let unscaled = (v + 0.0) * 32768.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
self.fractional_timestamp = u16::MAX;
return self;
}
self.fractional_timestamp = unscaled as u16;
self
}
pub fn time256_scaled(&self) -> Option<f64> {
if self.time256 == u8::MAX {
return None;
}
Some(self.time256 as f64 / 256.0 - 0.0)
}
pub fn set_time256_scaled(&mut self, v: f64) -> &mut Self {
let unscaled = (v + 0.0) * 256.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u8::MAX as f64 {
self.time256 = u8::MAX;
return self;
}
self.time256 = unscaled as u8;
self
}
pub fn event_timestamp_scaled(&self) -> Option<Vec<f64>> {
if self.event_timestamp.is_empty() {
return None;
}
let mut v = Vec::with_capacity(self.event_timestamp.len());
for &x in &self.event_timestamp {
v.push(x as f64 / 1024.0 - 0.0)
}
Some(v)
}
pub fn set_event_timestamp_scaled(&mut self, v: &[f64]) -> &mut Self {
self.event_timestamp = Vec::with_capacity(v.len());
if v.is_empty() {
return self;
}
for &x in v {
let unscaled = (x + 0.0) * 1024.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64 {
self.event_timestamp.push(u32::MAX);
continue;
}
self.event_timestamp.push(unscaled as u32);
}
self
}
pub fn mark_as_expanded(&mut self, num: u8, flag: bool) -> bool {
match num {
0 | 9 => {
if flag {
self.state[num as usize >> 3] |= 1 << (num & 7)
} else {
self.state[num as usize >> 3] &= !(1 << (num & 7))
}
true
}
_ => false,
}
}
pub fn is_expanded(&self, num: u8) -> bool {
is_expanded(&self.state, num)
}
fn count_valid_fields(&self) -> usize {
(self.timestamp.0 != u32::MAX) as usize
+ (self.fractional_timestamp != u16::MAX) as usize
+ (self.time256 != u8::MAX) as usize
+ (!self.filtered_bpm.is_empty()) as usize
+ (!self.event_timestamp.is_empty()) as usize
+ (!self.event_timestamp_12.is_empty()) as usize
}
}
impl Default for Hr {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for Hr {
fn from(mesg: &Message) -> Self {
const KNOWN_NUMS: [u64; 4] = [1603, 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 v = Self::new();
v.unknown_fields = Vec::<Field>::with_capacity(n as usize);
v.developer_fields = mesg.developer_fields.clone();
for field in &mesg.fields {
match field.num {
253 => v.timestamp = typedef::DateTime(field.value.as_u32()),
0 => v.fractional_timestamp = field.value.as_u16(),
1 => v.time256 = field.value.as_u8(),
6 => v.filtered_bpm = field.value.to_vec_u8(),
9 => v.event_timestamp = field.value.to_vec_u32(),
10 => v.event_timestamp_12 = field.value.to_vec_u8(),
_ => {
v.unknown_fields.push(field.clone());
continue;
}
};
if field.is_expanded && field.num < 10 {
v.state[field.num as usize >> 3] |= 1 << (field.num & 7)
}
}
v
}
}
impl From<Hr> for Message {
fn from(m: Hr) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if m.timestamp.0 != u32::MAX {
fields.push(Field {
num: 253,
base_type: FitBaseType::UINT32,
value: Value::Uint32(m.timestamp.0),
is_expanded: false,
});
};
if m.fractional_timestamp != u16::MAX {
fields.push(Field {
num: 0,
base_type: FitBaseType::UINT16,
value: Value::Uint16(m.fractional_timestamp),
is_expanded: is_expanded(&m.state, 0),
});
};
if m.time256 != u8::MAX {
fields.push(Field {
num: 1,
base_type: FitBaseType::UINT8,
value: Value::Uint8(m.time256),
is_expanded: false,
});
};
if !m.filtered_bpm.is_empty() {
fields.push(Field {
num: 6,
base_type: FitBaseType::UINT8,
value: Value::VecUint8(m.filtered_bpm),
is_expanded: false,
});
};
if !m.event_timestamp.is_empty() {
fields.push(Field {
num: 9,
base_type: FitBaseType::UINT32,
value: Value::VecUint32(m.event_timestamp),
is_expanded: is_expanded(&m.state, 9),
});
};
if !m.event_timestamp_12.is_empty() {
fields.push(Field {
num: 10,
base_type: FitBaseType::BYTE,
value: Value::VecUint8(m.event_timestamp_12),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::HR,
fields,
developer_fields: m.developer_fields,
}
}
}
#[cfg(feature = "serde")]
impl Serialize for Hr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let n = self.count_valid_fields() + 2;
let mut state = serializer.serialize_struct("Hr", n)?;
if let Some(v) = self.timestamp.unix_timestamp() {
state.serialize_field("timestamp", &v)?;
}
if let Some(v) = self.fractional_timestamp_scaled() {
state.serialize_field("fractional_timestamp", &v)?;
}
if let Some(v) = self.time256_scaled() {
state.serialize_field("time256", &v)?;
}
if !self.filtered_bpm.is_empty() {
state.serialize_field("filtered_bpm", &self.filtered_bpm)?;
}
if let Some(v) = self.event_timestamp_scaled() {
state.serialize_field("event_timestamp", &v)?;
}
if !self.event_timestamp_12.is_empty() {
state.serialize_field("event_timestamp_12", &self.event_timestamp_12)?;
}
if !self.unknown_fields.is_empty() {
state.serialize_field("unknown_fields", &self.unknown_fields)?;
}
if !self.developer_fields.is_empty() {
state.serialize_field("developer_fields", &self.developer_fields)?;
}
state.end()
}
}
#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(Deserialize), serde(default))]
struct De {
timestamp: Option<i64>,
fractional_timestamp: f64,
time256: f64,
filtered_bpm: Vec<u8>,
event_timestamp: Vec<f64>,
event_timestamp_12: Vec<u8>,
unknown_fields: Vec<Field>,
developer_fields: Vec<DeveloperField>,
}
#[cfg(feature = "serde")]
impl From<De> for Hr {
fn from(m: De) -> Self {
Self {
timestamp: m.timestamp.map_or_else(
|| typedef::DateTime(u32::MAX),
typedef::DateTime::from_unix_timestamp,
),
fractional_timestamp: {
let unscaled = (m.fractional_timestamp + 0.0) * 32768.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u16::MAX as f64 {
u16::MAX
} else {
unscaled as u16
}
},
time256: {
let unscaled = (m.time256 + 0.0) * 256.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u8::MAX as f64 {
u8::MAX
} else {
unscaled as u8
}
},
filtered_bpm: m.filtered_bpm,
event_timestamp: {
if m.event_timestamp.is_empty() {
Vec::new()
} else {
let mut vals = Vec::with_capacity(m.event_timestamp.len());
for &x in m.event_timestamp.iter() {
let unscaled = (x + 0.0) * 1024.0;
if unscaled.is_nan() || unscaled.is_infinite() || unscaled > u32::MAX as f64
{
vals.push(u32::MAX);
continue;
}
vals.push(unscaled as u32);
}
vals
}
},
event_timestamp_12: m.event_timestamp_12,
state: [0u8; 2],
unknown_fields: m.unknown_fields,
developer_fields: m.developer_fields,
}
}
}
#[cfg(feature = "serde")]
impl Default for De {
fn default() -> Self {
Self {
timestamp: None,
fractional_timestamp: f64::from_bits(u64::MAX),
time256: f64::from_bits(u64::MAX),
filtered_bpm: Vec::new(),
event_timestamp: Vec::new(),
event_timestamp_12: Vec::new(),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
}