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 = 0x0051;
28
29pub const REVISION: u16 = 3;
32
33pub const PICS: &str = "LWM";
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 Delicate = 16385,
107 Heavy = 16386,
109 Whites = 16387,
111}
112
113impl ModeTag {
114 #[must_use]
116 pub const fn from_value(value: u16) -> Option<Self> {
117 match value {
118 0 => Some(Self::Auto),
119 1 => Some(Self::Quick),
120 2 => Some(Self::Quiet),
121 3 => Some(Self::LowNoise),
122 4 => Some(Self::LowEnergy),
123 5 => Some(Self::Vacation),
124 6 => Some(Self::Min),
125 7 => Some(Self::Max),
126 8 => Some(Self::Night),
127 9 => Some(Self::Day),
128 16384 => Some(Self::Normal),
129 16385 => Some(Self::Delicate),
130 16386 => Some(Self::Heavy),
131 16387 => Some(Self::Whites),
132 _ => None,
133 }
134 }
135
136 #[must_use]
138 pub const fn value(self) -> u16 {
139 self as u16
140 }
141}
142
143impl ToTlv for ModeTag {
144 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
145 self.value().to_tlv(w, tag)
146 }
147}
148
149impl<'a> FromTlv<'a> for ModeTag {
150 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
151 let value = <u16 as FromTlv<'a>>::from_tlv(reader, element)?;
152 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
153 }
154}
155
156#[derive(Debug, Clone, Copy, PartialEq)]
158pub struct ModeOptionStruct<'a> {
159 pub label: &'a str,
161 pub mode: u8,
163 pub mode_tags: TlvList<'a, ModeTagStruct>,
165}
166
167impl<'a> ToTlv for ModeOptionStruct<'a> {
168 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
169 w.start_structure(tag)?;
170 self.label.to_tlv(w, Tag::Context(0))?;
171 self.mode.to_tlv(w, Tag::Context(1))?;
172 self.mode_tags.to_tlv(w, Tag::Context(2))?;
173 w.end_container()
174 }
175}
176
177impl<'a> FromTlv<'a> for ModeOptionStruct<'a> {
178 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
179 if element.value.container() != Some(ContainerKind::Structure) {
180 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
181 }
182 let mut label = None;
183 let mut mode = None;
184 let mut mode_tags = None;
185 loop {
186 let Some(field) = reader.next_element()? else {
187 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
188 };
189 if field.value == Value::EndOfContainer {
190 break;
191 }
192 match field.tag.context() {
193 Some(0) => label = Some(<&'a str>::from_tlv(reader, &field)?),
194 Some(1) => mode = Some(<u8>::from_tlv(reader, &field)?),
195 Some(2) => {
196 mode_tags = Some(<TlvList<'a, ModeTagStruct>>::from_tlv(reader, &field)?)
197 }
198 _ => reader.skip_value(&field)?,
202 }
203 }
204 Ok(Self {
205 label: label.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
206 mode: mode.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
207 mode_tags: mode_tags.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
208 })
209 }
210}
211
212#[derive(Debug, Clone, Copy, PartialEq)]
214pub struct ModeTagStruct {
215 pub mfg_code: Option<crate::msg::VendorId>,
217 pub value: u16,
219}
220
221impl ToTlv for ModeTagStruct {
222 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
223 w.start_structure(tag)?;
224 if let Some(value) = &self.mfg_code {
225 value.to_tlv(w, Tag::Context(0))?;
226 }
227 self.value.to_tlv(w, Tag::Context(1))?;
228 w.end_container()
229 }
230}
231
232impl<'a> FromTlv<'a> for ModeTagStruct {
233 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
234 if element.value.container() != Some(ContainerKind::Structure) {
235 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
236 }
237 let mut mfg_code = None;
238 let mut value = None;
239 loop {
240 let Some(field) = reader.next_element()? else {
241 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
242 };
243 if field.value == Value::EndOfContainer {
244 break;
245 }
246 match field.tag.context() {
247 Some(0) => mfg_code = Some(<crate::msg::VendorId>::from_tlv(reader, &field)?),
248 Some(1) => value = Some(<u16>::from_tlv(reader, &field)?),
249 _ => reader.skip_value(&field)?,
253 }
254 }
255 Ok(Self {
256 mfg_code,
257 value: value.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
258 })
259 }
260}
261
262#[derive(Debug, Clone, Copy, PartialEq)]
264pub struct ChangeToModeFields {
265 pub new_mode: u8,
267}
268
269impl ToTlv for ChangeToModeFields {
270 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
271 w.start_structure(tag)?;
272 self.new_mode.to_tlv(w, Tag::Context(0))?;
273 w.end_container()
274 }
275}
276
277impl<'a> FromTlv<'a> for ChangeToModeFields {
278 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
279 if element.value.container() != Some(ContainerKind::Structure) {
280 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
281 }
282 let mut new_mode = None;
283 loop {
284 let Some(field) = reader.next_element()? else {
285 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
286 };
287 if field.value == Value::EndOfContainer {
288 break;
289 }
290 match field.tag.context() {
291 Some(0) => new_mode = Some(<u8>::from_tlv(reader, &field)?),
292 _ => reader.skip_value(&field)?,
296 }
297 }
298 Ok(Self {
299 new_mode: new_mode.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
300 })
301 }
302}
303
304#[derive(Debug, Clone, Copy, PartialEq)]
306pub struct ChangeToModeResponseFields<'a> {
307 pub status: u8,
309 pub status_text: &'a str,
311}
312
313impl<'a> ToTlv for ChangeToModeResponseFields<'a> {
314 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
315 w.start_structure(tag)?;
316 self.status.to_tlv(w, Tag::Context(0))?;
317 self.status_text.to_tlv(w, Tag::Context(1))?;
318 w.end_container()
319 }
320}
321
322impl<'a> FromTlv<'a> for ChangeToModeResponseFields<'a> {
323 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
324 if element.value.container() != Some(ContainerKind::Structure) {
325 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
326 }
327 let mut status = None;
328 let mut status_text = None;
329 loop {
330 let Some(field) = reader.next_element()? else {
331 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
332 };
333 if field.value == Value::EndOfContainer {
334 break;
335 }
336 match field.tag.context() {
337 Some(0) => status = Some(<u8>::from_tlv(reader, &field)?),
338 Some(1) => status_text = Some(<&'a str>::from_tlv(reader, &field)?),
339 _ => reader.skip_value(&field)?,
343 }
344 }
345 Ok(Self {
346 status: status.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
347 status_text: status_text.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
348 })
349 }
350}
351
352pub const ATTRIBUTES: &[Attribute] = &[
354 Attribute {
355 id: 0x0000,
356 name: "SupportedModes",
357 kind: "list[ModeOptionStruct]",
358 access: Access::read_only(Privilege::View),
359 qualities: AttributeQualities::empty(),
360 reporting: Reporting::OnChange,
361 conform: Conform::MANDATORY,
362 },
363 Attribute {
364 id: 0x0001,
365 name: "CurrentMode",
366 kind: "uint8",
367 access: Access::read_only(Privilege::View),
368 qualities: AttributeQualities::empty(),
369 reporting: Reporting::OnChange,
370 conform: Conform::MANDATORY,
371 },
372 Attribute {
373 id: 0x0002,
374 name: "StartUpMode",
375 kind: "uint8",
376 access: Access::read_only(Privilege::View),
377 qualities: AttributeQualities::empty(),
378 reporting: Reporting::OnChange,
379 conform: Conform::DISALLOWED,
380 },
381 Attribute {
382 id: 0x0003,
383 name: "OnMode",
384 kind: "uint8",
385 access: Access::read_only(Privilege::View),
386 qualities: AttributeQualities::empty(),
387 reporting: Reporting::OnChange,
388 conform: Conform::DISALLOWED,
389 },
390];
391
392pub const COMMANDS: &[Command] = &[
394 Command {
395 id: 0x00,
396 name: "ChangeToMode",
397 to_server: true,
398 response: Some(0x01),
399 access: Access::invoke(Privilege::Operate),
400 conform: Conform::MANDATORY,
401 },
402 Command {
403 id: 0x01,
404 name: "ChangeToModeResponse",
405 to_server: false,
406 response: None,
407 access: Access::invoke(Privilege::Operate),
408 conform: Conform::MANDATORY,
409 },
410];
411
412pub const EVENTS: &[Event] = &[];
414
415pub const CLUSTER: Cluster = Cluster {
417 id: ID,
418 name: "Laundry Washer Mode",
419 revision: REVISION,
420 pics: "LWM",
421 derived_from: Some("Mode Base"),
422 features: FEATURES,
423 attributes: ATTRIBUTES,
424 commands: COMMANDS,
425 events: EVENTS,
426};