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 = 0x0060;
28
29pub const REVISION: u16 = 3;
32
33pub const PICS: &str = "OPSTATE";
35
36pub const FEATURES: &[Feature] = &[];
38
39pub mod attribute {
41 use crate::im::AttributeId;
42
43 pub const PHASE_LIST: AttributeId = 0x00;
45 pub const CURRENT_PHASE: AttributeId = 0x01;
47 pub const COUNTDOWN_TIME: AttributeId = 0x02;
49 pub const OPERATIONAL_STATE_LIST: AttributeId = 0x03;
51 pub const OPERATIONAL_STATE: AttributeId = 0x04;
53 pub const OPERATIONAL_ERROR: AttributeId = 0x05;
55}
56
57pub mod command {
59 use crate::im::CommandId;
60
61 pub const PAUSE: CommandId = 0x00;
63 pub const STOP: CommandId = 0x01;
65 pub const START: CommandId = 0x02;
67 pub const RESUME: CommandId = 0x03;
69 pub const OPERATIONAL_COMMAND_RESPONSE: CommandId = 0x04;
71}
72
73pub mod event {
75 use crate::im::EventId;
76
77 pub const OPERATIONAL_ERROR: EventId = 0x00;
79 pub const OPERATION_COMPLETION: EventId = 0x01;
81}
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
85#[repr(u8)]
86pub enum ErrorStateEnum {
87 NoError = 0,
89 UnableToStartOrResume = 1,
91 UnableToCompleteOperation = 2,
93 CommandInvalidInState = 3,
95}
96
97impl ErrorStateEnum {
98 #[must_use]
100 pub const fn from_value(value: u8) -> Option<Self> {
101 match value {
102 0 => Some(Self::NoError),
103 1 => Some(Self::UnableToStartOrResume),
104 2 => Some(Self::UnableToCompleteOperation),
105 3 => Some(Self::CommandInvalidInState),
106 _ => None,
107 }
108 }
109
110 #[must_use]
112 pub const fn value(self) -> u8 {
113 self as u8
114 }
115}
116
117impl ToTlv for ErrorStateEnum {
118 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
119 self.value().to_tlv(w, tag)
120 }
121}
122
123impl<'a> FromTlv<'a> for ErrorStateEnum {
124 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
125 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
126 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
127 }
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
132#[repr(u8)]
133pub enum OperationalStateEnum {
134 Stopped = 0,
136 Running = 1,
138 Paused = 2,
140 Error = 3,
142}
143
144impl OperationalStateEnum {
145 #[must_use]
147 pub const fn from_value(value: u8) -> Option<Self> {
148 match value {
149 0 => Some(Self::Stopped),
150 1 => Some(Self::Running),
151 2 => Some(Self::Paused),
152 3 => Some(Self::Error),
153 _ => None,
154 }
155 }
156
157 #[must_use]
159 pub const fn value(self) -> u8 {
160 self as u8
161 }
162}
163
164impl ToTlv for OperationalStateEnum {
165 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
166 self.value().to_tlv(w, tag)
167 }
168}
169
170impl<'a> FromTlv<'a> for OperationalStateEnum {
171 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
172 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
173 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
174 }
175}
176
177#[derive(Debug, Clone, Copy, PartialEq)]
179pub struct ErrorStateStruct<'a> {
180 pub error_state_id: ErrorStateEnum,
182 pub error_state_label: Option<&'a str>,
184 pub error_state_details: Option<&'a str>,
186}
187
188impl<'a> ToTlv for ErrorStateStruct<'a> {
189 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
190 w.start_structure(tag)?;
191 self.error_state_id.to_tlv(w, Tag::Context(0))?;
192 if let Some(value) = &self.error_state_label {
193 value.to_tlv(w, Tag::Context(1))?;
194 }
195 if let Some(value) = &self.error_state_details {
196 value.to_tlv(w, Tag::Context(2))?;
197 }
198 w.end_container()
199 }
200}
201
202impl<'a> FromTlv<'a> for ErrorStateStruct<'a> {
203 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
204 if element.value.container() != Some(ContainerKind::Structure) {
205 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
206 }
207 let mut error_state_id = None;
208 let mut error_state_label = None;
209 let mut error_state_details = None;
210 loop {
211 let Some(field) = reader.next_element()? else {
212 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
213 };
214 if field.value == Value::EndOfContainer {
215 break;
216 }
217 match field.tag.context() {
218 Some(0) => error_state_id = Some(<ErrorStateEnum>::from_tlv(reader, &field)?),
219 Some(1) => error_state_label = Some(<&'a str>::from_tlv(reader, &field)?),
220 Some(2) => error_state_details = Some(<&'a str>::from_tlv(reader, &field)?),
221 _ => reader.skip_value(&field)?,
225 }
226 }
227 Ok(Self {
228 error_state_id: error_state_id
229 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
230 error_state_label,
231 error_state_details,
232 })
233 }
234}
235
236#[derive(Debug, Clone, Copy, PartialEq)]
238pub struct OperationalStateStruct<'a> {
239 pub operational_state_id: OperationalStateEnum,
241 pub operational_state_label: Option<&'a str>,
243}
244
245impl<'a> ToTlv for OperationalStateStruct<'a> {
246 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
247 w.start_structure(tag)?;
248 self.operational_state_id.to_tlv(w, Tag::Context(0))?;
249 if let Some(value) = &self.operational_state_label {
250 value.to_tlv(w, Tag::Context(1))?;
251 }
252 w.end_container()
253 }
254}
255
256impl<'a> FromTlv<'a> for OperationalStateStruct<'a> {
257 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
258 if element.value.container() != Some(ContainerKind::Structure) {
259 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
260 }
261 let mut operational_state_id = None;
262 let mut operational_state_label = None;
263 loop {
264 let Some(field) = reader.next_element()? else {
265 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
266 };
267 if field.value == Value::EndOfContainer {
268 break;
269 }
270 match field.tag.context() {
271 Some(0) => {
272 operational_state_id = Some(<OperationalStateEnum>::from_tlv(reader, &field)?)
273 }
274 Some(1) => operational_state_label = Some(<&'a str>::from_tlv(reader, &field)?),
275 _ => reader.skip_value(&field)?,
279 }
280 }
281 Ok(Self {
282 operational_state_id: operational_state_id
283 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
284 operational_state_label,
285 })
286 }
287}
288
289#[derive(Debug, Clone, Copy, PartialEq)]
291pub struct OperationalCommandResponseFields<'a> {
292 pub command_response_state: ErrorStateStruct<'a>,
294}
295
296impl<'a> ToTlv for OperationalCommandResponseFields<'a> {
297 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
298 w.start_structure(tag)?;
299 self.command_response_state.to_tlv(w, Tag::Context(0))?;
300 w.end_container()
301 }
302}
303
304impl<'a> FromTlv<'a> for OperationalCommandResponseFields<'a> {
305 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
306 if element.value.container() != Some(ContainerKind::Structure) {
307 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
308 }
309 let mut command_response_state = None;
310 loop {
311 let Some(field) = reader.next_element()? else {
312 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
313 };
314 if field.value == Value::EndOfContainer {
315 break;
316 }
317 match field.tag.context() {
318 Some(0) => {
319 command_response_state = Some(<ErrorStateStruct<'a>>::from_tlv(reader, &field)?)
320 }
321 _ => reader.skip_value(&field)?,
325 }
326 }
327 Ok(Self {
328 command_response_state: command_response_state
329 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
330 })
331 }
332}
333
334pub const ATTRIBUTES: &[Attribute] = &[
336 Attribute {
337 id: 0x0000,
338 name: "PhaseList",
339 kind: "list[string]",
340 access: Access::read_only(Privilege::View),
341 qualities: AttributeQualities::NULLABLE,
342 reporting: Reporting::OnChange,
343 conform: Conform::MANDATORY,
344 },
345 Attribute {
346 id: 0x0001,
347 name: "CurrentPhase",
348 kind: "uint8",
349 access: Access::read_only(Privilege::View),
350 qualities: AttributeQualities::NULLABLE,
351 reporting: Reporting::OnChange,
352 conform: Conform::MANDATORY,
353 },
354 Attribute {
355 id: 0x0002,
356 name: "CountdownTime",
357 kind: "elapsed-s",
358 access: Access::read_only(Privilege::View),
359 qualities: AttributeQualities::NULLABLE,
360 reporting: Reporting::Quieter,
361 conform: Conform::OPTIONAL,
362 },
363 Attribute {
364 id: 0x0003,
365 name: "OperationalStateList",
366 kind: "list[OperationalStateStruct]",
367 access: Access::read_only(Privilege::View),
368 qualities: AttributeQualities::empty(),
369 reporting: Reporting::OnChange,
370 conform: Conform::MANDATORY,
371 },
372 Attribute {
373 id: 0x0004,
374 name: "OperationalState",
375 kind: "OperationalStateEnum",
376 access: Access::read_only(Privilege::View),
377 qualities: AttributeQualities::empty(),
378 reporting: Reporting::OnChange,
379 conform: Conform::MANDATORY,
380 },
381 Attribute {
382 id: 0x0005,
383 name: "OperationalError",
384 kind: "ErrorStateStruct",
385 access: Access::read_only(Privilege::View),
386 qualities: AttributeQualities::empty(),
387 reporting: Reporting::OnChange,
388 conform: Conform::MANDATORY,
389 },
390];
391
392pub const COMMANDS: &[Command] = &[
394 Command {
395 id: 0x00,
396 name: "Pause",
397 to_server: true,
398 response: Some(0x04),
399 access: Access::invoke(Privilege::Operate),
400 conform: Conform {
401 clauses: &[
402 Clause {
403 verdict: Conformance::Mandatory,
404 when: Condition::Command(0x03),
405 },
406 Clause {
407 verdict: Conformance::Optional,
408 when: Condition::Always,
409 },
410 ],
411 },
412 },
413 Command {
414 id: 0x01,
415 name: "Stop",
416 to_server: true,
417 response: Some(0x04),
418 access: Access::invoke(Privilege::Operate),
419 conform: Conform {
420 clauses: &[
421 Clause {
422 verdict: Conformance::Mandatory,
423 when: Condition::Command(0x02),
424 },
425 Clause {
426 verdict: Conformance::Optional,
427 when: Condition::Always,
428 },
429 ],
430 },
431 },
432 Command {
433 id: 0x02,
434 name: "Start",
435 to_server: true,
436 response: Some(0x04),
437 access: Access::invoke(Privilege::Operate),
438 conform: Conform::OPTIONAL,
439 },
440 Command {
441 id: 0x03,
442 name: "Resume",
443 to_server: true,
444 response: Some(0x04),
445 access: Access::invoke(Privilege::Operate),
446 conform: Conform {
447 clauses: &[
448 Clause {
449 verdict: Conformance::Mandatory,
450 when: Condition::Command(0x00),
451 },
452 Clause {
453 verdict: Conformance::Optional,
454 when: Condition::Always,
455 },
456 ],
457 },
458 },
459 Command {
460 id: 0x04,
461 name: "OperationalCommandResponse",
462 to_server: false,
463 response: None,
464 access: Access::invoke(Privilege::Operate),
465 conform: Conform {
466 clauses: &[Clause {
467 verdict: Conformance::Mandatory,
468 when: Condition::Any(&[
469 Condition::Command(0x00),
470 Condition::Command(0x01),
471 Condition::Command(0x02),
472 Condition::Command(0x03),
473 ]),
474 }],
475 },
476 },
477];
478
479pub const EVENTS: &[Event] = &[
481 Event {
482 id: 0x00,
483 name: "OperationalError",
484 priority: EventPriority::Critical,
485 access: Access::read_only(Privilege::View),
486 conform: Conform::MANDATORY,
487 },
488 Event {
489 id: 0x01,
490 name: "OperationCompletion",
491 priority: EventPriority::Info,
492 access: Access::read_only(Privilege::View),
493 conform: Conform::OPTIONAL,
494 },
495];
496
497pub const CLUSTER: Cluster = Cluster {
499 id: ID,
500 name: "Operational State",
501 revision: REVISION,
502 pics: "OPSTATE",
503 derived_from: None,
504 features: FEATURES,
505 attributes: ATTRIBUTES,
506 commands: COMMANDS,
507 events: EVENTS,
508};