#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct NodeId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NodeIdKind {
Unspecified,
Operational,
PakeKeyIdentifier,
CaseAuthenticatedTag,
TemporaryLocal,
Group,
Reserved,
}
impl NodeId {
pub const UNSPECIFIED: Self = Self(0);
pub const OPERATIONAL_MIN: u64 = 0x0000_0000_0000_0001;
pub const OPERATIONAL_MAX: u64 = 0xFFFF_FFEF_FFFF_FFFF;
#[must_use]
pub const fn kind(self) -> NodeIdKind {
match self.0 {
0 => NodeIdKind::Unspecified,
Self::OPERATIONAL_MIN..=Self::OPERATIONAL_MAX => NodeIdKind::Operational,
0xFFFF_FFFB_0000_0000..=0xFFFF_FFFB_FFFF_FFFF => NodeIdKind::PakeKeyIdentifier,
0xFFFF_FFFD_0000_0000..=0xFFFF_FFFD_FFFF_FFFF => NodeIdKind::CaseAuthenticatedTag,
0xFFFF_FFFE_0000_0000..=0xFFFF_FFFE_FFFF_FFFF => NodeIdKind::TemporaryLocal,
0xFFFF_FFFF_FFFF_0000..=0xFFFF_FFFF_FFFF_FFFF => NodeIdKind::Group,
_ => NodeIdKind::Reserved,
}
}
#[must_use]
pub const fn is_operational(self) -> bool {
matches!(self.kind(), NodeIdKind::Operational)
}
#[must_use]
pub const fn ephemeral(randomness: u64) -> Self {
Self((randomness & 0x0FFF_FFFF_FFFF_FFFF) | 1)
}
#[must_use]
#[expect(
clippy::cast_possible_truncation,
reason = "a Group Node ID is 0xFFFF_FFFF_FFFF_xxxx, so the group id is exactly the low 16 bits"
)]
pub const fn group(self) -> Option<GroupId> {
match self.kind() {
NodeIdKind::Group => Some(GroupId(self.0 as u16)),
_ => None,
}
}
#[must_use]
pub const fn from_group(group: GroupId) -> Self {
Self(0xFFFF_FFFF_FFFF_0000 | group.0 as u64)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CaseAuthenticatedTag(pub u32);
impl CaseAuthenticatedTag {
pub const ADMINISTRATOR_IDENTIFIER: u16 = 0xFFFF;
pub const ANCHOR_IDENTIFIER: u16 = 0xFFFE;
pub const GENERAL_IDENTIFIER_MAX: u16 = 0xEFFF;
#[must_use]
pub const fn new(identifier: u16, version: u16) -> Self {
Self(((identifier as u32) << 16) | version as u32)
}
#[must_use]
pub const fn identifier(self) -> u16 {
(self.0 >> 16) as u16
}
#[must_use]
#[expect(
clippy::cast_possible_truncation,
reason = "a CAT's version is defined as the low 16 bits of the identifier (§6.5.6.3)"
)]
pub const fn version(self) -> u16 {
self.0 as u16
}
#[must_use]
pub const fn is_valid(self) -> bool {
self.version() != 0
}
#[must_use]
pub const fn to_node_id(self) -> NodeId {
NodeId(0xFFFF_FFFD_0000_0000 | self.0 as u64)
}
#[must_use]
#[expect(
clippy::cast_possible_truncation,
reason = "a CASE Authenticated Tag Node ID carries the tag in its low 32 bits"
)]
pub const fn from_node_id(node_id: NodeId) -> Option<Self> {
match node_id.kind() {
NodeIdKind::CaseAuthenticatedTag => Some(Self(node_id.0 as u32)),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct GroupId(pub u16);
impl GroupId {
pub const UNSPECIFIED: Self = Self(0);
#[must_use]
pub const fn is_valid(self) -> bool {
self.0 != 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct FabricId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct FabricIndex(pub u8);
impl FabricIndex {
pub const NONE: Self = Self(0);
#[must_use]
pub const fn is_some(self) -> bool {
self.0 != 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SessionId(pub u16);
impl SessionId {
pub const UNSECURED: Self = Self(0);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct ExchangeId(pub u16);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct VendorId(pub u16);
impl VendorId {
pub const COMMON: Self = Self(0x0000);
pub const TEST_1: Self = Self(0xFFF1);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ProtocolId {
pub vendor: VendorId,
pub id: u16,
}
impl ProtocolId {
#[must_use]
pub const fn common(id: u16) -> Self {
Self {
vendor: VendorId::COMMON,
id,
}
}
pub const SECURE_CHANNEL: Self = Self::common(0x0000);
pub const INTERACTION_MODEL: Self = Self::common(0x0001);
pub const BDX: Self = Self::common(0x0002);
pub const USER_DIRECTED_COMMISSIONING: Self = Self::common(0x0003);
pub const FOR_TESTING: Self = Self::common(0x0004);
#[must_use]
pub const fn is_common(self) -> bool {
self.vendor.0 == VendorId::COMMON.0
}
}
impl Default for ProtocolId {
fn default() -> Self {
Self::SECURE_CHANNEL
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn node_id_ranges_follow_2_5_5() {
assert_eq!(NodeId(0).kind(), NodeIdKind::Unspecified);
assert_eq!(NodeId(1).kind(), NodeIdKind::Operational);
assert_eq!(
NodeId(0xFFFF_FFEF_FFFF_FFFF).kind(),
NodeIdKind::Operational
);
assert_eq!(
NodeId(0xFFFF_FFF0_0000_0000).kind(),
NodeIdKind::Reserved,
"one past the operational range"
);
assert_eq!(
NodeId(0xFFFF_FFFB_0000_0001).kind(),
NodeIdKind::PakeKeyIdentifier
);
assert_eq!(
NodeId(0xFFFF_FFFD_0000_0001).kind(),
NodeIdKind::CaseAuthenticatedTag
);
assert_eq!(
NodeId(0xFFFF_FFFE_0000_0001).kind(),
NodeIdKind::TemporaryLocal
);
assert_eq!(NodeId(0xFFFF_FFFF_FFFF_0001).kind(), NodeIdKind::Group);
}
#[test]
fn group_node_ids_round_trip() {
let g = GroupId(0x1234);
let n = NodeId::from_group(g);
assert_eq!(n.0, 0xFFFF_FFFF_FFFF_1234);
assert_eq!(n.group(), Some(g));
assert_eq!(NodeId(1).group(), None);
}
#[test]
fn the_unspecified_node_id_is_not_operational() {
assert!(!NodeId::UNSPECIFIED.is_operational());
assert!(NodeId(1).is_operational());
}
#[test]
fn group_zero_is_not_addressable() {
assert!(!GroupId::UNSPECIFIED.is_valid());
assert!(GroupId(1).is_valid());
}
#[test]
fn common_protocols_need_no_vendor_field() {
assert!(ProtocolId::SECURE_CHANNEL.is_common());
assert!(ProtocolId::INTERACTION_MODEL.is_common());
assert!(
!ProtocolId {
vendor: VendorId::TEST_1,
id: 1
}
.is_common()
);
}
}