#![allow(unused_imports)]
use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
use crate::dm::meta::{EventPriority, Reporting};
use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
use crate::error::Result;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
use crate::tlv::{
ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
};
pub const ID: ClusterId = 0x0097;
pub const REVISION: u16 = 3;
pub const PICS: &str = "MESS";
pub mod feature {
pub const RECEIVED_CONFIRMATION: u32 = 1 << 0;
pub const CONFIRMATION_RESPONSE: u32 = 1 << 1;
pub const CONFIRMATION_REPLY: u32 = 1 << 2;
pub const PROTECTED_MESSAGES: u32 = 1 << 3;
}
pub const FEATURES: &[Feature] = &[
Feature {
bit: 0,
code: "CONF",
name: "ReceivedConfirmation",
conform: Conform::OPTIONAL,
},
Feature {
bit: 1,
code: "RESP",
name: "ConfirmationResponse",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(0),
}],
},
},
Feature {
bit: 2,
code: "RPLY",
name: "ConfirmationReply",
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(0),
}],
},
},
Feature {
bit: 3,
code: "PROT",
name: "ProtectedMessages",
conform: Conform::OPTIONAL,
},
];
pub mod attribute {
use crate::im::AttributeId;
pub const MESSAGES: AttributeId = 0x00;
pub const ACTIVE_MESSAGE_I_DS: AttributeId = 0x01;
}
pub mod command {
use crate::im::CommandId;
pub const PRESENT_MESSAGES_REQUEST: CommandId = 0x00;
pub const CANCEL_MESSAGES_REQUEST: CommandId = 0x01;
}
pub mod event {
use crate::im::EventId;
pub const MESSAGE_QUEUED: EventId = 0x00;
pub const MESSAGE_PRESENTED: EventId = 0x01;
pub const MESSAGE_COMPLETE: EventId = 0x02;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum FutureMessagePreferenceEnum {
Allowed = 0,
Increased = 1,
Reduced = 2,
Disallowed = 3,
Banned = 4,
}
impl FutureMessagePreferenceEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Allowed),
1 => Some(Self::Increased),
2 => Some(Self::Reduced),
3 => Some(Self::Disallowed),
4 => Some(Self::Banned),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for FutureMessagePreferenceEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for FutureMessagePreferenceEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum MessagePriorityEnum {
Low = 0,
Medium = 1,
High = 2,
Critical = 3,
}
impl MessagePriorityEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Low),
1 => Some(Self::Medium),
2 => Some(Self::High),
3 => Some(Self::Critical),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for MessagePriorityEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for MessagePriorityEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct MessageControlBitmap: u8 {
const CONFIRMATION_REQUIRED = 1 << 0;
const RESPONSE_REQUIRED = 1 << 1;
const REPLY_MESSAGE = 1 << 2;
const MESSAGE_CONFIRMED = 1 << 3;
const MESSAGE_PROTECTED = 1 << 4;
}
}
impl ToTlv for MessageControlBitmap {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.bits().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for MessageControlBitmap {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MessageResponseOptionStruct<'a> {
pub message_response_id: u32,
pub label: &'a str,
}
impl<'a> ToTlv for MessageResponseOptionStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.message_response_id.to_tlv(w, Tag::Context(0))?;
self.label.to_tlv(w, Tag::Context(1))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for MessageResponseOptionStruct<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut message_response_id = None;
let mut label = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => message_response_id = Some(<u32>::from_tlv(reader, &field)?),
Some(1) => label = Some(<&'a str>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
message_response_id: message_response_id
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
label: label.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MessageStruct<'a> {
pub message_id: u16,
pub priority: MessagePriorityEnum,
pub message_control: MessageControlBitmap,
pub start_time: Nullable<u32>,
pub duration: Nullable<u64>,
pub message_text: &'a str,
pub responses: Option<TlvList<'a, MessageResponseOptionStruct<'a>>>,
pub fabric_index: crate::msg::FabricIndex,
}
impl<'a> ToTlv for MessageStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.message_id.to_tlv(w, Tag::Context(0))?;
self.priority.to_tlv(w, Tag::Context(1))?;
self.message_control.to_tlv(w, Tag::Context(2))?;
self.start_time.to_tlv(w, Tag::Context(3))?;
self.duration.to_tlv(w, Tag::Context(4))?;
self.message_text.to_tlv(w, Tag::Context(5))?;
if let Some(value) = &self.responses {
value.to_tlv(w, Tag::Context(6))?;
}
self.fabric_index.to_tlv(w, Tag::Context(254))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for MessageStruct<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut message_id = None;
let mut priority = None;
let mut message_control = None;
let mut start_time = None;
let mut duration = None;
let mut message_text = None;
let mut responses = None;
let mut fabric_index = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => message_id = Some(<u16>::from_tlv(reader, &field)?),
Some(1) => priority = Some(<MessagePriorityEnum>::from_tlv(reader, &field)?),
Some(2) => {
message_control = Some(<MessageControlBitmap>::from_tlv(reader, &field)?)
}
Some(3) => start_time = Some(<Nullable<u32>>::from_tlv(reader, &field)?),
Some(4) => duration = Some(<Nullable<u64>>::from_tlv(reader, &field)?),
Some(5) => message_text = Some(<&'a str>::from_tlv(reader, &field)?),
Some(6) => {
responses = Some(<TlvList<'a, MessageResponseOptionStruct<'a>>>::from_tlv(
reader, &field,
)?)
}
Some(254) => {
fabric_index = Some(<crate::msg::FabricIndex>::from_tlv(reader, &field)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
message_id: message_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
priority: priority.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
message_control: message_control
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
start_time: start_time.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
duration: duration.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
message_text: message_text.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
responses,
fabric_index: fabric_index.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PresentMessagesRequestFields<'a> {
pub message_id: u16,
pub priority: MessagePriorityEnum,
pub message_control: MessageControlBitmap,
pub start_time: Nullable<u32>,
pub duration: Nullable<u64>,
pub message_text: &'a str,
pub responses: Option<TlvList<'a, MessageResponseOptionStruct<'a>>>,
}
impl<'a> ToTlv for PresentMessagesRequestFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.message_id.to_tlv(w, Tag::Context(0))?;
self.priority.to_tlv(w, Tag::Context(1))?;
self.message_control.to_tlv(w, Tag::Context(2))?;
self.start_time.to_tlv(w, Tag::Context(3))?;
self.duration.to_tlv(w, Tag::Context(4))?;
self.message_text.to_tlv(w, Tag::Context(5))?;
if let Some(value) = &self.responses {
value.to_tlv(w, Tag::Context(6))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for PresentMessagesRequestFields<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut message_id = None;
let mut priority = None;
let mut message_control = None;
let mut start_time = None;
let mut duration = None;
let mut message_text = None;
let mut responses = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => message_id = Some(<u16>::from_tlv(reader, &field)?),
Some(1) => priority = Some(<MessagePriorityEnum>::from_tlv(reader, &field)?),
Some(2) => {
message_control = Some(<MessageControlBitmap>::from_tlv(reader, &field)?)
}
Some(3) => start_time = Some(<Nullable<u32>>::from_tlv(reader, &field)?),
Some(4) => duration = Some(<Nullable<u64>>::from_tlv(reader, &field)?),
Some(5) => message_text = Some(<&'a str>::from_tlv(reader, &field)?),
Some(6) => {
responses = Some(<TlvList<'a, MessageResponseOptionStruct<'a>>>::from_tlv(
reader, &field,
)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
message_id: message_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
priority: priority.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
message_control: message_control
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
start_time: start_time.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
duration: duration.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
message_text: message_text.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
responses,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CancelMessagesRequestFields<'a> {
pub message_i_ds: TlvList<'a, u16>,
}
impl<'a> ToTlv for CancelMessagesRequestFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.message_i_ds.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for CancelMessagesRequestFields<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut message_i_ds = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => message_i_ds = Some(<TlvList<'a, u16>>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
message_i_ds: message_i_ds.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "Messages",
kind: "list[MessageStruct]",
access: Access::read_only(Privilege::View).with_qualities(AccessQualities::FABRIC_SCOPED),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "ActiveMessageIDs",
kind: "list[message-id]",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
];
pub const COMMANDS: &[Command] = &[
Command {
id: 0x00,
name: "PresentMessagesRequest",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::FABRIC_SCOPED),
conform: Conform::MANDATORY,
},
Command {
id: 0x01,
name: "CancelMessagesRequest",
to_server: true,
response: None,
access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::FABRIC_SCOPED),
conform: Conform::MANDATORY,
},
];
pub const EVENTS: &[Event] = &[
Event {
id: 0x00,
name: "MessageQueued",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View)
.with_qualities(AccessQualities::FABRIC_SENSITIVE),
conform: Conform::MANDATORY,
},
Event {
id: 0x01,
name: "MessagePresented",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View)
.with_qualities(AccessQualities::FABRIC_SENSITIVE),
conform: Conform::MANDATORY,
},
Event {
id: 0x02,
name: "MessageComplete",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View)
.with_qualities(AccessQualities::FABRIC_SENSITIVE),
conform: Conform::MANDATORY,
},
];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Messages",
revision: REVISION,
pics: "MESS",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};