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 = 0x005E;
28
29pub const REVISION: u16 = 2;
32
33pub const PICS: &str = "MWOM";
35
36pub mod feature {
41 pub const ON_OFF: u32 = 1 << 0;
45}
46
47pub const FEATURES: &[Feature] = &[Feature {
49 bit: 0,
50 code: "DEPONOFF",
51 name: "OnOff",
52 conform: Conform::DISALLOWED,
53}];
54
55pub mod attribute {
57 use crate::im::AttributeId;
58
59 pub const SUPPORTED_MODES: AttributeId = 0x00;
61 pub const CURRENT_MODE: AttributeId = 0x01;
63 pub const START_UP_MODE: AttributeId = 0x02;
65 pub const ON_MODE: AttributeId = 0x03;
67}
68
69pub mod command {
71 use crate::im::CommandId;
72
73 pub const CHANGE_TO_MODE: CommandId = 0x00;
75 pub const CHANGE_TO_MODE_RESPONSE: CommandId = 0x01;
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
81#[repr(u16)]
82pub enum ModeTag {
83 Auto = 0,
85 Quick = 1,
87 Quiet = 2,
89 LowNoise = 3,
91 LowEnergy = 4,
93 Vacation = 5,
95 Min = 6,
97 Max = 7,
99 Night = 8,
101 Day = 9,
103 Normal = 16384,
105 Defrost = 16385,
107}
108
109impl ModeTag {
110 #[must_use]
112 pub const fn from_value(value: u16) -> Option<Self> {
113 match value {
114 0 => Some(Self::Auto),
115 1 => Some(Self::Quick),
116 2 => Some(Self::Quiet),
117 3 => Some(Self::LowNoise),
118 4 => Some(Self::LowEnergy),
119 5 => Some(Self::Vacation),
120 6 => Some(Self::Min),
121 7 => Some(Self::Max),
122 8 => Some(Self::Night),
123 9 => Some(Self::Day),
124 16384 => Some(Self::Normal),
125 16385 => Some(Self::Defrost),
126 _ => None,
127 }
128 }
129
130 #[must_use]
132 pub const fn value(self) -> u16 {
133 self as u16
134 }
135}
136
137impl ToTlv for ModeTag {
138 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
139 self.value().to_tlv(w, tag)
140 }
141}
142
143impl<'a> FromTlv<'a> for ModeTag {
144 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
145 let value = <u16 as FromTlv<'a>>::from_tlv(reader, element)?;
146 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
147 }
148}
149
150#[derive(Debug, Clone, Copy, PartialEq)]
152pub struct ModeOptionStruct<'a> {
153 pub label: &'a str,
155 pub mode: u8,
157 pub mode_tags: TlvList<'a, ModeTagStruct>,
159}
160
161impl<'a> ToTlv for ModeOptionStruct<'a> {
162 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
163 w.start_structure(tag)?;
164 self.label.to_tlv(w, Tag::Context(0))?;
165 self.mode.to_tlv(w, Tag::Context(1))?;
166 self.mode_tags.to_tlv(w, Tag::Context(2))?;
167 w.end_container()
168 }
169}
170
171impl<'a> FromTlv<'a> for ModeOptionStruct<'a> {
172 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
173 if element.value.container() != Some(ContainerKind::Structure) {
174 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
175 }
176 let mut label = None;
177 let mut mode = None;
178 let mut mode_tags = None;
179 loop {
180 let Some(field) = reader.next_element()? else {
181 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
182 };
183 if field.value == Value::EndOfContainer {
184 break;
185 }
186 match field.tag.context() {
187 Some(0) => label = Some(<&'a str>::from_tlv(reader, &field)?),
188 Some(1) => mode = Some(<u8>::from_tlv(reader, &field)?),
189 Some(2) => {
190 mode_tags = Some(<TlvList<'a, ModeTagStruct>>::from_tlv(reader, &field)?)
191 }
192 _ => reader.skip_value(&field)?,
196 }
197 }
198 Ok(Self {
199 label: label.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
200 mode: mode.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
201 mode_tags: mode_tags.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
202 })
203 }
204}
205
206#[derive(Debug, Clone, Copy, PartialEq)]
208pub struct ModeTagStruct {
209 pub mfg_code: Option<crate::msg::VendorId>,
211 pub value: u16,
213}
214
215impl ToTlv for ModeTagStruct {
216 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
217 w.start_structure(tag)?;
218 if let Some(value) = &self.mfg_code {
219 value.to_tlv(w, Tag::Context(0))?;
220 }
221 self.value.to_tlv(w, Tag::Context(1))?;
222 w.end_container()
223 }
224}
225
226impl<'a> FromTlv<'a> for ModeTagStruct {
227 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
228 if element.value.container() != Some(ContainerKind::Structure) {
229 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
230 }
231 let mut mfg_code = None;
232 let mut value = None;
233 loop {
234 let Some(field) = reader.next_element()? else {
235 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
236 };
237 if field.value == Value::EndOfContainer {
238 break;
239 }
240 match field.tag.context() {
241 Some(0) => mfg_code = Some(<crate::msg::VendorId>::from_tlv(reader, &field)?),
242 Some(1) => value = Some(<u16>::from_tlv(reader, &field)?),
243 _ => reader.skip_value(&field)?,
247 }
248 }
249 Ok(Self {
250 mfg_code,
251 value: value.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
252 })
253 }
254}
255
256#[derive(Debug, Clone, Copy, PartialEq)]
258pub struct ChangeToModeFields {
259 pub new_mode: u8,
261}
262
263impl ToTlv for ChangeToModeFields {
264 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
265 w.start_structure(tag)?;
266 self.new_mode.to_tlv(w, Tag::Context(0))?;
267 w.end_container()
268 }
269}
270
271impl<'a> FromTlv<'a> for ChangeToModeFields {
272 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
273 if element.value.container() != Some(ContainerKind::Structure) {
274 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
275 }
276 let mut new_mode = None;
277 loop {
278 let Some(field) = reader.next_element()? else {
279 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
280 };
281 if field.value == Value::EndOfContainer {
282 break;
283 }
284 match field.tag.context() {
285 Some(0) => new_mode = Some(<u8>::from_tlv(reader, &field)?),
286 _ => reader.skip_value(&field)?,
290 }
291 }
292 Ok(Self {
293 new_mode: new_mode.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
294 })
295 }
296}
297
298#[derive(Debug, Clone, Copy, PartialEq)]
300pub struct ChangeToModeResponseFields<'a> {
301 pub status: u8,
303 pub status_text: &'a str,
305}
306
307impl<'a> ToTlv for ChangeToModeResponseFields<'a> {
308 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
309 w.start_structure(tag)?;
310 self.status.to_tlv(w, Tag::Context(0))?;
311 self.status_text.to_tlv(w, Tag::Context(1))?;
312 w.end_container()
313 }
314}
315
316impl<'a> FromTlv<'a> for ChangeToModeResponseFields<'a> {
317 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
318 if element.value.container() != Some(ContainerKind::Structure) {
319 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
320 }
321 let mut status = None;
322 let mut status_text = None;
323 loop {
324 let Some(field) = reader.next_element()? else {
325 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
326 };
327 if field.value == Value::EndOfContainer {
328 break;
329 }
330 match field.tag.context() {
331 Some(0) => status = Some(<u8>::from_tlv(reader, &field)?),
332 Some(1) => status_text = Some(<&'a str>::from_tlv(reader, &field)?),
333 _ => reader.skip_value(&field)?,
337 }
338 }
339 Ok(Self {
340 status: status.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
341 status_text: status_text.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
342 })
343 }
344}
345
346pub const ATTRIBUTES: &[Attribute] = &[
348 Attribute {
349 id: 0x0000,
350 name: "SupportedModes",
351 kind: "list[ModeOptionStruct]",
352 access: Access::read_only(Privilege::View),
353 qualities: AttributeQualities::empty(),
354 reporting: Reporting::OnChange,
355 conform: Conform::MANDATORY,
356 },
357 Attribute {
358 id: 0x0001,
359 name: "CurrentMode",
360 kind: "uint8",
361 access: Access::read_only(Privilege::View),
362 qualities: AttributeQualities::empty(),
363 reporting: Reporting::OnChange,
364 conform: Conform::MANDATORY,
365 },
366 Attribute {
367 id: 0x0002,
368 name: "StartUpMode",
369 kind: "uint8",
370 access: Access::read_only(Privilege::View),
371 qualities: AttributeQualities::empty(),
372 reporting: Reporting::OnChange,
373 conform: Conform::DISALLOWED,
374 },
375 Attribute {
376 id: 0x0003,
377 name: "OnMode",
378 kind: "uint8",
379 access: Access::read_only(Privilege::View),
380 qualities: AttributeQualities::empty(),
381 reporting: Reporting::OnChange,
382 conform: Conform::DISALLOWED,
383 },
384];
385
386pub const COMMANDS: &[Command] = &[
388 Command {
389 id: 0x00,
390 name: "ChangeToMode",
391 to_server: true,
392 response: None,
393 access: Access::invoke(Privilege::Operate),
394 conform: Conform::DISALLOWED,
395 },
396 Command {
397 id: 0x01,
398 name: "ChangeToModeResponse",
399 to_server: true,
400 response: None,
401 access: Access::invoke(Privilege::Operate),
402 conform: Conform::DISALLOWED,
403 },
404];
405
406pub const EVENTS: &[Event] = &[];
408
409pub const CLUSTER: Cluster = Cluster {
411 id: ID,
412 name: "Microwave Oven Mode",
413 revision: REVISION,
414 pics: "MWOM",
415 derived_from: Some("Mode Base"),
416 features: FEATURES,
417 attributes: ATTRIBUTES,
418 commands: COMMANDS,
419 events: EVENTS,
420};