#![allow(unused, clippy::manual_range_patterns)]
use crate::profile::{ProfileType, typedef};
use crate::proto::*;
use alloc::vec::Vec;
fn is_expanded(state: &[u8], num: u8) -> bool {
match num {
2 | 3 => (state[num as usize >> 3] >> (num & 7)) & 1 == 1,
_ => false,
}
}
#[derive(Debug, Clone)]
pub struct ExdDataConceptConfiguration {
pub screen_index: u8,
pub concept_field: u8,
pub field_id: u8,
pub concept_index: u8,
pub data_page: u8,
pub concept_key: u8,
pub scaling: u8,
pub data_units: typedef::ExdDataUnits,
pub qualifier: typedef::ExdQualifiers,
pub descriptor: typedef::ExdDescriptors,
pub is_signed: typedef::Bool,
state: [u8; 1], pub unknown_fields: Vec<Field>,
pub developer_fields: Vec<DeveloperField>,
}
impl ExdDataConceptConfiguration {
pub const SCREEN_INDEX: u8 = 0;
pub const CONCEPT_FIELD: u8 = 1;
pub const FIELD_ID: u8 = 2;
pub const CONCEPT_INDEX: u8 = 3;
pub const DATA_PAGE: u8 = 4;
pub const CONCEPT_KEY: u8 = 5;
pub const SCALING: u8 = 6;
pub const DATA_UNITS: u8 = 8;
pub const QUALIFIER: u8 = 9;
pub const DESCRIPTOR: u8 = 10;
pub const IS_SIGNED: u8 = 11;
pub const fn new() -> Self {
Self {
screen_index: u8::MAX,
concept_field: u8::MAX,
field_id: u8::MAX,
concept_index: u8::MAX,
data_page: u8::MAX,
concept_key: u8::MAX,
scaling: u8::MAX,
data_units: typedef::ExdDataUnits(u8::MAX),
qualifier: typedef::ExdQualifiers(u8::MAX),
descriptor: typedef::ExdDescriptors(u8::MAX),
is_signed: typedef::Bool(u8::MAX),
state: [0u8; 1],
unknown_fields: Vec::new(),
developer_fields: Vec::new(),
}
}
pub fn mark_as_expanded(&mut self, num: u8, flag: bool) -> bool {
match num {
2 | 3 => {
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.screen_index != u8::MAX) as usize
+ (self.concept_field != u8::MAX) as usize
+ (self.field_id != u8::MAX) as usize
+ (self.concept_index != u8::MAX) as usize
+ (self.data_page != u8::MAX) as usize
+ (self.concept_key != u8::MAX) as usize
+ (self.scaling != u8::MAX) as usize
+ (self.data_units != typedef::ExdDataUnits(u8::MAX)) as usize
+ (self.qualifier != typedef::ExdQualifiers(u8::MAX)) as usize
+ (self.descriptor != typedef::ExdDescriptors(u8::MAX)) as usize
+ (self.is_signed != typedef::Bool(u8::MAX)) as usize
}
}
impl Default for ExdDataConceptConfiguration {
fn default() -> Self {
Self::new()
}
}
impl From<&Message> for ExdDataConceptConfiguration {
fn from(mesg: &Message) -> Self {
const KNOWN_NUMS: [u64; 4] = [3967, 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.screen_index = field.value.as_u8(),
1 => v.concept_field = field.value.as_u8(),
2 => v.field_id = field.value.as_u8(),
3 => v.concept_index = field.value.as_u8(),
4 => v.data_page = field.value.as_u8(),
5 => v.concept_key = field.value.as_u8(),
6 => v.scaling = field.value.as_u8(),
8 => v.data_units = typedef::ExdDataUnits(field.value.as_u8()),
9 => v.qualifier = typedef::ExdQualifiers(field.value.as_u8()),
10 => v.descriptor = typedef::ExdDescriptors(field.value.as_u8()),
11 => v.is_signed = typedef::Bool(field.value.as_u8()),
_ => {
v.unknown_fields.push(field.clone());
continue;
}
};
if field.is_expanded && field.num < 4 {
v.state[field.num as usize >> 3] |= 1 << (field.num & 7)
}
}
v
}
}
impl From<ExdDataConceptConfiguration> for Message {
fn from(m: ExdDataConceptConfiguration) -> Self {
let mut fields =
Vec::<Field>::with_capacity(m.count_valid_fields() + m.unknown_fields.len());
if m.screen_index != u8::MAX {
fields.push(Field {
num: 0,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.screen_index),
is_expanded: false,
});
};
if m.concept_field != u8::MAX {
fields.push(Field {
num: 1,
profile_type: ProfileType::BYTE,
value: Value::Uint8(m.concept_field),
is_expanded: false,
});
};
if m.field_id != u8::MAX {
fields.push(Field {
num: 2,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.field_id),
is_expanded: is_expanded(&m.state, 2),
});
};
if m.concept_index != u8::MAX {
fields.push(Field {
num: 3,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.concept_index),
is_expanded: is_expanded(&m.state, 3),
});
};
if m.data_page != u8::MAX {
fields.push(Field {
num: 4,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.data_page),
is_expanded: false,
});
};
if m.concept_key != u8::MAX {
fields.push(Field {
num: 5,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.concept_key),
is_expanded: false,
});
};
if m.scaling != u8::MAX {
fields.push(Field {
num: 6,
profile_type: ProfileType::UINT8,
value: Value::Uint8(m.scaling),
is_expanded: false,
});
};
if m.data_units != typedef::ExdDataUnits(u8::MAX) {
fields.push(Field {
num: 8,
profile_type: ProfileType::EXD_DATA_UNITS,
value: Value::Uint8(m.data_units.0),
is_expanded: false,
});
};
if m.qualifier != typedef::ExdQualifiers(u8::MAX) {
fields.push(Field {
num: 9,
profile_type: ProfileType::EXD_QUALIFIERS,
value: Value::Uint8(m.qualifier.0),
is_expanded: false,
});
};
if m.descriptor != typedef::ExdDescriptors(u8::MAX) {
fields.push(Field {
num: 10,
profile_type: ProfileType::EXD_DESCRIPTORS,
value: Value::Uint8(m.descriptor.0),
is_expanded: false,
});
};
if m.is_signed != typedef::Bool(u8::MAX) {
fields.push(Field {
num: 11,
profile_type: ProfileType::BOOL,
value: Value::Uint8(m.is_signed.0),
is_expanded: false,
});
};
fields.extend_from_slice(&m.unknown_fields);
Self {
header: 0,
num: typedef::MesgNum::EXD_DATA_CONCEPT_CONFIGURATION,
fields,
developer_fields: m.developer_fields,
}
}
}