use crate::im::{AttributeId, ClusterId, CommandId, EventId};
#[must_use]
pub const fn prefix(mei: u32) -> u16 {
(mei >> 16) as u16
}
#[must_use]
pub const fn suffix(mei: u32) -> u16 {
(mei & 0xFFFF) as u16
}
#[must_use]
pub const fn prefix_is_defined(mei: u32) -> bool {
prefix(mei) <= 0xFFF4
}
#[must_use]
pub const fn is_standard(mei: u32) -> bool {
prefix(mei) == 0x0000
}
#[must_use]
pub const fn cluster_is_valid(cluster: ClusterId) -> bool {
if !prefix_is_defined(cluster) {
return false;
}
if is_standard(cluster) {
suffix(cluster) <= 0x7FFF
} else {
matches!(suffix(cluster), 0xFC00..=0xFFFE)
}
}
#[must_use]
pub const fn device_type_is_valid(device_type: u32) -> bool {
prefix_is_defined(device_type) && suffix(device_type) <= 0xBFFF
}
#[must_use]
pub const fn attribute_is_valid(attribute: AttributeId) -> bool {
if !prefix_is_defined(attribute) {
return false;
}
if is_standard(attribute) {
matches!(suffix(attribute), 0x0000..=0x4FFF | 0xF000..=0xFFFE)
} else {
suffix(attribute) <= 0x4FFF
}
}
#[must_use]
pub const fn command_is_valid(command: CommandId) -> bool {
prefix_is_defined(command) && suffix(command) <= 0x00FF
}
#[must_use]
pub const fn event_is_valid(event: EventId) -> bool {
prefix_is_defined(event) && suffix(event) <= 0x00FF
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table_103s_examples_decode_as_it_says() {
assert_eq!((prefix(0x0000_0000), suffix(0x0000_0000)), (0x0000, 0x0000));
assert_eq!((prefix(0x0000_FFFE), suffix(0x0000_FFFE)), (0x0000, 0xFFFE));
assert_eq!((prefix(0x0001_0002), suffix(0x0001_0002)), (0x0001, 0x0002));
assert_eq!((prefix(0x0002_FFFE), suffix(0x0002_FFFE)), (0x0002, 0xFFFE));
assert!(!prefix_is_defined(0xFFFF_0000));
}
#[test]
fn the_prefix_boundary_is_where_table_100_puts_it() {
assert!(prefix_is_defined(0x0000_0000), "Standard OR Scoped");
assert!(prefix_is_defined(0xFFF0_0000), "the last Manufacturer Code");
assert!(prefix_is_defined(0xFFF4_0000), "the last Test Vendor MC");
assert!(!prefix_is_defined(0xFFF5_0000), "past every defined source");
assert!(!prefix_is_defined(0xFFFF_FFFF));
}
#[test]
fn a_cluster_id_is_standard_low_or_manufacturer_high_and_nothing_between() {
assert!(cluster_is_valid(0x0000_0000));
assert!(cluster_is_valid(0x0000_7FFF), "the last standard cluster");
assert!(!cluster_is_valid(0x0000_8000), "one past it");
assert!(
!cluster_is_valid(0x0000_FC00),
"the MC range, standard prefix"
);
assert!(!cluster_is_valid(0xFFF1_0000), "below the MC cluster range");
assert!(cluster_is_valid(0xFFF1_FC00), "the first MC cluster");
assert!(cluster_is_valid(0xFFF1_FFFE), "the last one");
assert!(!cluster_is_valid(0xFFF1_FFFF), "Table 101 stops at 0xFFFE");
assert!(!cluster_is_valid(0xFFFF_FFFF));
}
#[test]
fn a_device_type_id_stops_at_bfff() {
assert!(device_type_is_valid(0x0000_0000));
assert!(device_type_is_valid(0x0000_0100), "On/Off Light");
assert!(device_type_is_valid(0x0000_BFFF));
assert!(!device_type_is_valid(0x0000_C000));
assert!(device_type_is_valid(0xFFF1_0001), "a test vendor's own");
assert!(!device_type_is_valid(0xFFFF_FFFF));
}
#[test]
fn an_attribute_id_has_two_ranges_under_the_standard_prefix() {
assert!(attribute_is_valid(0x0000_0000));
assert!(attribute_is_valid(0x0000_4FFF), "the last non-global");
assert!(!attribute_is_valid(0x0000_5000));
assert!(!attribute_is_valid(0x0000_EFFF));
assert!(attribute_is_valid(0x0000_F000), "the first global");
assert!(attribute_is_valid(0x0000_FFFD), "AttributeList");
assert!(!attribute_is_valid(0x0000_FFFF));
assert!(attribute_is_valid(0xFFF1_0001));
assert!(!attribute_is_valid(0xFFF1_F000));
}
#[test]
fn commands_and_events_carry_one_octet_of_suffix() {
assert!(command_is_valid(0x0000_00FF));
assert!(!command_is_valid(0x0000_0100));
assert!(event_is_valid(0x0000_00FF));
assert!(!event_is_valid(0x0000_0100));
assert!(!command_is_valid(0xFFFF_FFFF));
assert!(!event_is_valid(0xFFFF_FFFF));
}
}