#![allow(unused, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
use alloc::vec::Vec;
#[derive(Debug, Clone)]
pub struct AntChannelId {
pub channel_number: u8,
pub device_type: u8,
pub device_number: u16,
pub transmission_type: u8,
pub device_index: typedef::DeviceIndex,
pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl AntChannelId {
pub const CHANNEL_NUMBER: u8 = 0;
pub const DEVICE_TYPE: u8 = 1;
pub const DEVICE_NUMBER: u8 = 2;
pub const TRANSMISSION_TYPE: u8 = 3;
pub const DEVICE_INDEX: u8 = 4;
pub const fn new() -> Self {
Self {
channel_number: u8::MAX,
device_type: u8::MIN,
device_number: u16::MIN,
transmission_type: u8::MIN,
device_index: typedef::DeviceIndex(u8::MAX),
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
fn count_valid_fields(&self) -> usize {
(self.channel_number != u8::MAX) as usize
+ (self.device_type != u8::MIN) as usize
+ (self.device_number != u16::MIN) as usize
+ (self.transmission_type != u8::MIN) as usize
+ (self.device_index != typedef::DeviceIndex(u8::MAX)) as usize
}
}
impl Default for AntChannelId {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for AntChannelId {
fn from(mesg: &Message) -> Self {
const KNOWN_NUMS: [u64; 4] = [31, 0, 0, 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 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 {
0 => v.channel_number = field.value.as_u8(),
1 => v.device_type = field.value.as_u8z(),
2 => v.device_number = field.value.as_u16z(),
3 => v.transmission_type = field.value.as_u8z(),
4 => v.device_index = typedef::DeviceIndex(field.value.as_u8()),
_ => v.unknown_fields.push(field.clone()),
};
}
v
}
}
impl From<AntChannelId> for Message {
fn from(m: AntChannelId) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if m.channel_number != u8::MAX {
fields.push(Field {
num: 0,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.channel_number),
is_expanded: false,
});
};
if m.device_type != u8::MIN {
fields.push(Field {
num: 1,
profile_type: ProfileType::UINT8Z,
value: Value::Uint8(m.device_type),
is_expanded: false,
});
};
if m.device_number != u16::MIN {
fields.push(Field {
num: 2,
profile_type: ProfileType::UINT16Z,
value: Value::Uint16(m.device_number),
is_expanded: false,
});
};
if m.transmission_type != u8::MIN {
fields.push(Field {
num: 3,
profile_type: ProfileType::UINT8Z,
value: Value::Uint8(m.transmission_type),
is_expanded: false,
});
};
if m.device_index != typedef::DeviceIndex(u8::MAX) {
fields.push(Field {
num: 4,
profile_type: ProfileType::DEVICE_INDEX,
value: Value::Uint8(m.device_index.0),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::ANT_CHANNEL_ID,
fields,
developer_fields: m.developer_fields,
}
}
}