1#![allow(unused_imports)]
15
16use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
17use crate::dm::meta::{EventPriority, Reporting};
18use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
19use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
20use crate::error::Result;
21use crate::im::{AttributeId, ClusterId, CommandId, EventId};
22use crate::tlv::{
23 ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
24};
25
26pub const ID: ClusterId = 0x0097;
28
29pub const REVISION: u16 = 3;
32
33pub const PICS: &str = "MESS";
35
36pub mod feature {
41 pub const RECEIVED_CONFIRMATION: u32 = 1 << 0;
45 pub const CONFIRMATION_RESPONSE: u32 = 1 << 1;
49 pub const CONFIRMATION_REPLY: u32 = 1 << 2;
53 pub const PROTECTED_MESSAGES: u32 = 1 << 3;
57}
58
59pub const FEATURES: &[Feature] = &[
61 Feature {
62 bit: 0,
63 code: "CONF",
64 name: "ReceivedConfirmation",
65 conform: Conform::OPTIONAL,
66 },
67 Feature {
68 bit: 1,
69 code: "RESP",
70 name: "ConfirmationResponse",
71 conform: Conform {
72 clauses: &[Clause {
73 verdict: Conformance::Optional,
74 when: Condition::Feature(0),
75 }],
76 },
77 },
78 Feature {
79 bit: 2,
80 code: "RPLY",
81 name: "ConfirmationReply",
82 conform: Conform {
83 clauses: &[Clause {
84 verdict: Conformance::Optional,
85 when: Condition::Feature(0),
86 }],
87 },
88 },
89 Feature {
90 bit: 3,
91 code: "PROT",
92 name: "ProtectedMessages",
93 conform: Conform::OPTIONAL,
94 },
95];
96
97pub mod attribute {
99 use crate::im::AttributeId;
100
101 pub const MESSAGES: AttributeId = 0x00;
103 pub const ACTIVE_MESSAGE_I_DS: AttributeId = 0x01;
105}
106
107pub mod command {
109 use crate::im::CommandId;
110
111 pub const PRESENT_MESSAGES_REQUEST: CommandId = 0x00;
113 pub const CANCEL_MESSAGES_REQUEST: CommandId = 0x01;
115}
116
117pub mod event {
119 use crate::im::EventId;
120
121 pub const MESSAGE_QUEUED: EventId = 0x00;
123 pub const MESSAGE_PRESENTED: EventId = 0x01;
125 pub const MESSAGE_COMPLETE: EventId = 0x02;
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
131#[repr(u8)]
132pub enum FutureMessagePreferenceEnum {
133 Allowed = 0,
135 Increased = 1,
137 Reduced = 2,
139 Disallowed = 3,
141 Banned = 4,
143}
144
145impl FutureMessagePreferenceEnum {
146 #[must_use]
148 pub const fn from_value(value: u8) -> Option<Self> {
149 match value {
150 0 => Some(Self::Allowed),
151 1 => Some(Self::Increased),
152 2 => Some(Self::Reduced),
153 3 => Some(Self::Disallowed),
154 4 => Some(Self::Banned),
155 _ => None,
156 }
157 }
158
159 #[must_use]
161 pub const fn value(self) -> u8 {
162 self as u8
163 }
164}
165
166impl ToTlv for FutureMessagePreferenceEnum {
167 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
168 self.value().to_tlv(w, tag)
169 }
170}
171
172impl<'a> FromTlv<'a> for FutureMessagePreferenceEnum {
173 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
174 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
175 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
176 }
177}
178
179#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
181#[repr(u8)]
182pub enum MessagePriorityEnum {
183 Low = 0,
185 Medium = 1,
187 High = 2,
189 Critical = 3,
191}
192
193impl MessagePriorityEnum {
194 #[must_use]
196 pub const fn from_value(value: u8) -> Option<Self> {
197 match value {
198 0 => Some(Self::Low),
199 1 => Some(Self::Medium),
200 2 => Some(Self::High),
201 3 => Some(Self::Critical),
202 _ => None,
203 }
204 }
205
206 #[must_use]
208 pub const fn value(self) -> u8 {
209 self as u8
210 }
211}
212
213impl ToTlv for MessagePriorityEnum {
214 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
215 self.value().to_tlv(w, tag)
216 }
217}
218
219impl<'a> FromTlv<'a> for MessagePriorityEnum {
220 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
221 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
222 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
223 }
224}
225
226bitflags::bitflags! {
227 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
229 pub struct MessageControlBitmap: u8 {
230 const CONFIRMATION_REQUIRED = 1 << 0;
232 const RESPONSE_REQUIRED = 1 << 1;
234 const REPLY_MESSAGE = 1 << 2;
236 const MESSAGE_CONFIRMED = 1 << 3;
238 const MESSAGE_PROTECTED = 1 << 4;
240 }
241}
242
243impl ToTlv for MessageControlBitmap {
244 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
245 self.bits().to_tlv(w, tag)
246 }
247}
248
249impl<'a> FromTlv<'a> for MessageControlBitmap {
250 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
251 let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
252 Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
253 }
254}
255
256#[derive(Debug, Clone, Copy, PartialEq)]
258pub struct MessageResponseOptionStruct<'a> {
259 pub message_response_id: u32,
261 pub label: &'a str,
263}
264
265impl<'a> ToTlv for MessageResponseOptionStruct<'a> {
266 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
267 w.start_structure(tag)?;
268 self.message_response_id.to_tlv(w, Tag::Context(0))?;
269 self.label.to_tlv(w, Tag::Context(1))?;
270 w.end_container()
271 }
272}
273
274impl<'a> FromTlv<'a> for MessageResponseOptionStruct<'a> {
275 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
276 if element.value.container() != Some(ContainerKind::Structure) {
277 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
278 }
279 let mut message_response_id = None;
280 let mut label = None;
281 loop {
282 let Some(field) = reader.next_element()? else {
283 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
284 };
285 if field.value == Value::EndOfContainer {
286 break;
287 }
288 match field.tag.context() {
289 Some(0) => message_response_id = Some(<u32>::from_tlv(reader, &field)?),
290 Some(1) => label = Some(<&'a str>::from_tlv(reader, &field)?),
291 _ => reader.skip_value(&field)?,
295 }
296 }
297 Ok(Self {
298 message_response_id: message_response_id
299 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
300 label: label.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
301 })
302 }
303}
304
305#[derive(Debug, Clone, Copy, PartialEq)]
307pub struct MessageStruct<'a> {
308 pub message_id: u16,
310 pub priority: MessagePriorityEnum,
312 pub message_control: MessageControlBitmap,
314 pub start_time: Nullable<u32>,
316 pub duration: Nullable<u64>,
318 pub message_text: &'a str,
320 pub responses: Option<TlvList<'a, MessageResponseOptionStruct<'a>>>,
322 pub fabric_index: crate::msg::FabricIndex,
324}
325
326impl<'a> ToTlv for MessageStruct<'a> {
327 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
328 w.start_structure(tag)?;
329 self.message_id.to_tlv(w, Tag::Context(0))?;
330 self.priority.to_tlv(w, Tag::Context(1))?;
331 self.message_control.to_tlv(w, Tag::Context(2))?;
332 self.start_time.to_tlv(w, Tag::Context(3))?;
333 self.duration.to_tlv(w, Tag::Context(4))?;
334 self.message_text.to_tlv(w, Tag::Context(5))?;
335 if let Some(value) = &self.responses {
336 value.to_tlv(w, Tag::Context(6))?;
337 }
338 self.fabric_index.to_tlv(w, Tag::Context(254))?;
339 w.end_container()
340 }
341}
342
343impl<'a> FromTlv<'a> for MessageStruct<'a> {
344 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
345 if element.value.container() != Some(ContainerKind::Structure) {
346 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
347 }
348 let mut message_id = None;
349 let mut priority = None;
350 let mut message_control = None;
351 let mut start_time = None;
352 let mut duration = None;
353 let mut message_text = None;
354 let mut responses = None;
355 let mut fabric_index = None;
356 loop {
357 let Some(field) = reader.next_element()? else {
358 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
359 };
360 if field.value == Value::EndOfContainer {
361 break;
362 }
363 match field.tag.context() {
364 Some(0) => message_id = Some(<u16>::from_tlv(reader, &field)?),
365 Some(1) => priority = Some(<MessagePriorityEnum>::from_tlv(reader, &field)?),
366 Some(2) => {
367 message_control = Some(<MessageControlBitmap>::from_tlv(reader, &field)?)
368 }
369 Some(3) => start_time = Some(<Nullable<u32>>::from_tlv(reader, &field)?),
370 Some(4) => duration = Some(<Nullable<u64>>::from_tlv(reader, &field)?),
371 Some(5) => message_text = Some(<&'a str>::from_tlv(reader, &field)?),
372 Some(6) => {
373 responses = Some(<TlvList<'a, MessageResponseOptionStruct<'a>>>::from_tlv(
374 reader, &field,
375 )?)
376 }
377 Some(254) => {
378 fabric_index = Some(<crate::msg::FabricIndex>::from_tlv(reader, &field)?)
379 }
380 _ => reader.skip_value(&field)?,
384 }
385 }
386 Ok(Self {
387 message_id: message_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
388 priority: priority.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
389 message_control: message_control
390 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
391 start_time: start_time.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
392 duration: duration.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
393 message_text: message_text.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
394 responses,
395 fabric_index: fabric_index.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
396 })
397 }
398}
399
400#[derive(Debug, Clone, Copy, PartialEq)]
402pub struct PresentMessagesRequestFields<'a> {
403 pub message_id: u16,
405 pub priority: MessagePriorityEnum,
407 pub message_control: MessageControlBitmap,
409 pub start_time: Nullable<u32>,
411 pub duration: Nullable<u64>,
413 pub message_text: &'a str,
415 pub responses: Option<TlvList<'a, MessageResponseOptionStruct<'a>>>,
417}
418
419impl<'a> ToTlv for PresentMessagesRequestFields<'a> {
420 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
421 w.start_structure(tag)?;
422 self.message_id.to_tlv(w, Tag::Context(0))?;
423 self.priority.to_tlv(w, Tag::Context(1))?;
424 self.message_control.to_tlv(w, Tag::Context(2))?;
425 self.start_time.to_tlv(w, Tag::Context(3))?;
426 self.duration.to_tlv(w, Tag::Context(4))?;
427 self.message_text.to_tlv(w, Tag::Context(5))?;
428 if let Some(value) = &self.responses {
429 value.to_tlv(w, Tag::Context(6))?;
430 }
431 w.end_container()
432 }
433}
434
435impl<'a> FromTlv<'a> for PresentMessagesRequestFields<'a> {
436 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
437 if element.value.container() != Some(ContainerKind::Structure) {
438 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
439 }
440 let mut message_id = None;
441 let mut priority = None;
442 let mut message_control = None;
443 let mut start_time = None;
444 let mut duration = None;
445 let mut message_text = None;
446 let mut responses = None;
447 loop {
448 let Some(field) = reader.next_element()? else {
449 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
450 };
451 if field.value == Value::EndOfContainer {
452 break;
453 }
454 match field.tag.context() {
455 Some(0) => message_id = Some(<u16>::from_tlv(reader, &field)?),
456 Some(1) => priority = Some(<MessagePriorityEnum>::from_tlv(reader, &field)?),
457 Some(2) => {
458 message_control = Some(<MessageControlBitmap>::from_tlv(reader, &field)?)
459 }
460 Some(3) => start_time = Some(<Nullable<u32>>::from_tlv(reader, &field)?),
461 Some(4) => duration = Some(<Nullable<u64>>::from_tlv(reader, &field)?),
462 Some(5) => message_text = Some(<&'a str>::from_tlv(reader, &field)?),
463 Some(6) => {
464 responses = Some(<TlvList<'a, MessageResponseOptionStruct<'a>>>::from_tlv(
465 reader, &field,
466 )?)
467 }
468 _ => reader.skip_value(&field)?,
472 }
473 }
474 Ok(Self {
475 message_id: message_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
476 priority: priority.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
477 message_control: message_control
478 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
479 start_time: start_time.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
480 duration: duration.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
481 message_text: message_text.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
482 responses,
483 })
484 }
485}
486
487#[derive(Debug, Clone, Copy, PartialEq)]
489pub struct CancelMessagesRequestFields<'a> {
490 pub message_i_ds: TlvList<'a, u16>,
492}
493
494impl<'a> ToTlv for CancelMessagesRequestFields<'a> {
495 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
496 w.start_structure(tag)?;
497 self.message_i_ds.to_tlv(w, Tag::Context(0))?;
498 w.end_container()
499 }
500}
501
502impl<'a> FromTlv<'a> for CancelMessagesRequestFields<'a> {
503 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
504 if element.value.container() != Some(ContainerKind::Structure) {
505 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
506 }
507 let mut message_i_ds = None;
508 loop {
509 let Some(field) = reader.next_element()? else {
510 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
511 };
512 if field.value == Value::EndOfContainer {
513 break;
514 }
515 match field.tag.context() {
516 Some(0) => message_i_ds = Some(<TlvList<'a, u16>>::from_tlv(reader, &field)?),
517 _ => reader.skip_value(&field)?,
521 }
522 }
523 Ok(Self {
524 message_i_ds: message_i_ds.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
525 })
526 }
527}
528
529pub const ATTRIBUTES: &[Attribute] = &[
531 Attribute {
532 id: 0x0000,
533 name: "Messages",
534 kind: "list[MessageStruct]",
535 access: Access::read_only(Privilege::View).with_qualities(AccessQualities::FABRIC_SCOPED),
536 qualities: AttributeQualities::empty(),
537 reporting: Reporting::OnChange,
538 conform: Conform::MANDATORY,
539 },
540 Attribute {
541 id: 0x0001,
542 name: "ActiveMessageIDs",
543 kind: "list[message-id]",
544 access: Access::read_only(Privilege::View),
545 qualities: AttributeQualities::empty(),
546 reporting: Reporting::OnChange,
547 conform: Conform::MANDATORY,
548 },
549];
550
551pub const COMMANDS: &[Command] = &[
553 Command {
554 id: 0x00,
555 name: "PresentMessagesRequest",
556 to_server: true,
557 response: None,
558 access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::FABRIC_SCOPED),
559 conform: Conform::MANDATORY,
560 },
561 Command {
562 id: 0x01,
563 name: "CancelMessagesRequest",
564 to_server: true,
565 response: None,
566 access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::FABRIC_SCOPED),
567 conform: Conform::MANDATORY,
568 },
569];
570
571pub const EVENTS: &[Event] = &[
573 Event {
574 id: 0x00,
575 name: "MessageQueued",
576 priority: EventPriority::Info,
577 access: Access::read_only(Privilege::View)
578 .with_qualities(AccessQualities::FABRIC_SENSITIVE),
579 conform: Conform::MANDATORY,
580 },
581 Event {
582 id: 0x01,
583 name: "MessagePresented",
584 priority: EventPriority::Info,
585 access: Access::read_only(Privilege::View)
586 .with_qualities(AccessQualities::FABRIC_SENSITIVE),
587 conform: Conform::MANDATORY,
588 },
589 Event {
590 id: 0x02,
591 name: "MessageComplete",
592 priority: EventPriority::Info,
593 access: Access::read_only(Privilege::View)
594 .with_qualities(AccessQualities::FABRIC_SENSITIVE),
595 conform: Conform::MANDATORY,
596 },
597];
598
599pub const CLUSTER: Cluster = Cluster {
601 id: ID,
602 name: "Messages",
603 revision: REVISION,
604 pics: "MESS",
605 derived_from: None,
606 features: FEATURES,
607 attributes: ATTRIBUTES,
608 commands: COMMANDS,
609 events: EVENTS,
610};