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 = 0x0202;
28
29pub const REVISION: u16 = 6;
32
33pub const PICS: &str = "FAN";
35
36pub mod feature {
41 pub const MULTI_SPEED: u32 = 1 << 0;
45 pub const AUTO: u32 = 1 << 1;
49 pub const ROCKING: u32 = 1 << 2;
53 pub const WIND: u32 = 1 << 3;
57 pub const STEP: u32 = 1 << 4;
61 pub const AIRFLOW_DIRECTION: u32 = 1 << 5;
65}
66
67pub const FEATURES: &[Feature] = &[
69 Feature {
70 bit: 0,
71 code: "SPD",
72 name: "MultiSpeed",
73 conform: Conform::OPTIONAL,
74 },
75 Feature {
76 bit: 1,
77 code: "AUT",
78 name: "Auto",
79 conform: Conform::OPTIONAL,
80 },
81 Feature {
82 bit: 2,
83 code: "RCK",
84 name: "Rocking",
85 conform: Conform::OPTIONAL,
86 },
87 Feature {
88 bit: 3,
89 code: "WND",
90 name: "Wind",
91 conform: Conform::OPTIONAL,
92 },
93 Feature {
94 bit: 4,
95 code: "STEP",
96 name: "Step",
97 conform: Conform::OPTIONAL,
98 },
99 Feature {
100 bit: 5,
101 code: "DIR",
102 name: "AirflowDirection",
103 conform: Conform::OPTIONAL,
104 },
105];
106
107pub mod attribute {
109 use crate::im::AttributeId;
110
111 pub const FAN_MODE: AttributeId = 0x00;
113 pub const FAN_MODE_SEQUENCE: AttributeId = 0x01;
115 pub const PERCENT_SETTING: AttributeId = 0x02;
117 pub const PERCENT_CURRENT: AttributeId = 0x03;
119 pub const SPEED_MAX: AttributeId = 0x04;
121 pub const SPEED_SETTING: AttributeId = 0x05;
123 pub const SPEED_CURRENT: AttributeId = 0x06;
125 pub const ROCK_SUPPORT: AttributeId = 0x07;
127 pub const ROCK_SETTING: AttributeId = 0x08;
129 pub const WIND_SUPPORT: AttributeId = 0x09;
131 pub const WIND_SETTING: AttributeId = 0x0A;
133 pub const AIRFLOW_DIRECTION: AttributeId = 0x0B;
135}
136
137pub mod command {
139 use crate::im::CommandId;
140
141 pub const STEP: CommandId = 0x00;
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
147#[repr(u8)]
148pub enum AirflowDirectionEnum {
149 Forward = 0,
151 Reverse = 1,
153}
154
155impl AirflowDirectionEnum {
156 #[must_use]
158 pub const fn from_value(value: u8) -> Option<Self> {
159 match value {
160 0 => Some(Self::Forward),
161 1 => Some(Self::Reverse),
162 _ => None,
163 }
164 }
165
166 #[must_use]
168 pub const fn value(self) -> u8 {
169 self as u8
170 }
171}
172
173impl ToTlv for AirflowDirectionEnum {
174 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
175 self.value().to_tlv(w, tag)
176 }
177}
178
179impl<'a> FromTlv<'a> for AirflowDirectionEnum {
180 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
181 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
182 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
183 }
184}
185
186#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
188#[repr(u8)]
189pub enum FanModeEnum {
190 Off = 0,
192 Low = 1,
194 Medium = 2,
196 High = 3,
198 On = 4,
200 Auto = 5,
202 Smart = 6,
204}
205
206impl FanModeEnum {
207 #[must_use]
209 pub const fn from_value(value: u8) -> Option<Self> {
210 match value {
211 0 => Some(Self::Off),
212 1 => Some(Self::Low),
213 2 => Some(Self::Medium),
214 3 => Some(Self::High),
215 4 => Some(Self::On),
216 5 => Some(Self::Auto),
217 6 => Some(Self::Smart),
218 _ => None,
219 }
220 }
221
222 #[must_use]
224 pub const fn value(self) -> u8 {
225 self as u8
226 }
227}
228
229impl ToTlv for FanModeEnum {
230 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
231 self.value().to_tlv(w, tag)
232 }
233}
234
235impl<'a> FromTlv<'a> for FanModeEnum {
236 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
237 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
238 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
239 }
240}
241
242#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
244#[repr(u8)]
245pub enum FanModeSequenceEnum {
246 OffLowMedHigh = 0,
248 OffLowHigh = 1,
250 OffLowMedHighAuto = 2,
252 OffLowHighAuto = 3,
254 OffHighAuto = 4,
256 OffHigh = 5,
258}
259
260impl FanModeSequenceEnum {
261 #[must_use]
263 pub const fn from_value(value: u8) -> Option<Self> {
264 match value {
265 0 => Some(Self::OffLowMedHigh),
266 1 => Some(Self::OffLowHigh),
267 2 => Some(Self::OffLowMedHighAuto),
268 3 => Some(Self::OffLowHighAuto),
269 4 => Some(Self::OffHighAuto),
270 5 => Some(Self::OffHigh),
271 _ => None,
272 }
273 }
274
275 #[must_use]
277 pub const fn value(self) -> u8 {
278 self as u8
279 }
280}
281
282impl ToTlv for FanModeSequenceEnum {
283 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
284 self.value().to_tlv(w, tag)
285 }
286}
287
288impl<'a> FromTlv<'a> for FanModeSequenceEnum {
289 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
290 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
291 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
292 }
293}
294
295#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
297#[repr(u8)]
298pub enum StepDirectionEnum {
299 Increase = 0,
301 Decrease = 1,
303}
304
305impl StepDirectionEnum {
306 #[must_use]
308 pub const fn from_value(value: u8) -> Option<Self> {
309 match value {
310 0 => Some(Self::Increase),
311 1 => Some(Self::Decrease),
312 _ => None,
313 }
314 }
315
316 #[must_use]
318 pub const fn value(self) -> u8 {
319 self as u8
320 }
321}
322
323impl ToTlv for StepDirectionEnum {
324 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
325 self.value().to_tlv(w, tag)
326 }
327}
328
329impl<'a> FromTlv<'a> for StepDirectionEnum {
330 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
331 let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
332 Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
333 }
334}
335
336bitflags::bitflags! {
337 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
339 pub struct RockBitmap: u8 {
340 const ROCK_LEFT_RIGHT = 1 << 0;
342 const ROCK_UP_DOWN = 1 << 1;
344 const ROCK_ROUND = 1 << 2;
346 }
347}
348
349impl ToTlv for RockBitmap {
350 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
351 self.bits().to_tlv(w, tag)
352 }
353}
354
355impl<'a> FromTlv<'a> for RockBitmap {
356 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
357 let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
358 Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
359 }
360}
361
362bitflags::bitflags! {
363 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
365 pub struct WindBitmap: u8 {
366 const SLEEP_WIND = 1 << 0;
368 const NATURAL_WIND = 1 << 1;
370 }
371}
372
373impl ToTlv for WindBitmap {
374 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
375 self.bits().to_tlv(w, tag)
376 }
377}
378
379impl<'a> FromTlv<'a> for WindBitmap {
380 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
381 let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
382 Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
383 }
384}
385
386#[derive(Debug, Clone, Copy, PartialEq)]
388pub struct StepFields {
389 pub direction: StepDirectionEnum,
391 pub wrap: Option<bool>,
393 pub lowest_off: Option<bool>,
395}
396
397impl ToTlv for StepFields {
398 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
399 w.start_structure(tag)?;
400 self.direction.to_tlv(w, Tag::Context(0))?;
401 if let Some(value) = &self.wrap {
402 value.to_tlv(w, Tag::Context(1))?;
403 }
404 if let Some(value) = &self.lowest_off {
405 value.to_tlv(w, Tag::Context(2))?;
406 }
407 w.end_container()
408 }
409}
410
411impl<'a> FromTlv<'a> for StepFields {
412 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
413 if element.value.container() != Some(ContainerKind::Structure) {
414 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
415 }
416 let mut direction = None;
417 let mut wrap = None;
418 let mut lowest_off = None;
419 loop {
420 let Some(field) = reader.next_element()? else {
421 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
422 };
423 if field.value == Value::EndOfContainer {
424 break;
425 }
426 match field.tag.context() {
427 Some(0) => direction = Some(<StepDirectionEnum>::from_tlv(reader, &field)?),
428 Some(1) => wrap = Some(<bool>::from_tlv(reader, &field)?),
429 Some(2) => lowest_off = Some(<bool>::from_tlv(reader, &field)?),
430 _ => reader.skip_value(&field)?,
434 }
435 }
436 Ok(Self {
437 direction: direction.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
438 wrap,
439 lowest_off,
440 })
441 }
442}
443
444pub const ATTRIBUTES: &[Attribute] = &[
446 Attribute {
447 id: 0x0000,
448 name: "FanMode",
449 kind: "FanModeEnum",
450 access: Access::read_write_with(Privilege::View, Privilege::Operate),
451 qualities: AttributeQualities::NON_VOLATILE,
452 reporting: Reporting::OnChange,
453 conform: Conform::MANDATORY,
454 },
455 Attribute {
456 id: 0x0001,
457 name: "FanModeSequence",
458 kind: "FanModeSequenceEnum",
459 access: Access::read_only(Privilege::View),
460 qualities: AttributeQualities::FIXED,
461 reporting: Reporting::OnChange,
462 conform: Conform::MANDATORY,
463 },
464 Attribute {
465 id: 0x0002,
466 name: "PercentSetting",
467 kind: "percent",
468 access: Access::read_write_with(Privilege::View, Privilege::Operate),
469 qualities: AttributeQualities::NULLABLE,
470 reporting: Reporting::OnChange,
471 conform: Conform::MANDATORY,
472 },
473 Attribute {
474 id: 0x0003,
475 name: "PercentCurrent",
476 kind: "percent",
477 access: Access::read_only(Privilege::View),
478 qualities: AttributeQualities::empty(),
479 reporting: Reporting::OnChange,
480 conform: Conform::MANDATORY,
481 },
482 Attribute {
483 id: 0x0004,
484 name: "SpeedMax",
485 kind: "uint8",
486 access: Access::read_only(Privilege::View),
487 qualities: AttributeQualities::FIXED,
488 reporting: Reporting::OnChange,
489 conform: Conform {
490 clauses: &[Clause {
491 verdict: Conformance::Mandatory,
492 when: Condition::Feature(0),
493 }],
494 },
495 },
496 Attribute {
497 id: 0x0005,
498 name: "SpeedSetting",
499 kind: "uint8",
500 access: Access::read_write_with(Privilege::View, Privilege::Operate),
501 qualities: AttributeQualities::NULLABLE,
502 reporting: Reporting::OnChange,
503 conform: Conform {
504 clauses: &[Clause {
505 verdict: Conformance::Mandatory,
506 when: Condition::Feature(0),
507 }],
508 },
509 },
510 Attribute {
511 id: 0x0006,
512 name: "SpeedCurrent",
513 kind: "uint8",
514 access: Access::read_only(Privilege::View),
515 qualities: AttributeQualities::empty(),
516 reporting: Reporting::OnChange,
517 conform: Conform {
518 clauses: &[Clause {
519 verdict: Conformance::Mandatory,
520 when: Condition::Feature(0),
521 }],
522 },
523 },
524 Attribute {
525 id: 0x0007,
526 name: "RockSupport",
527 kind: "RockBitmap",
528 access: Access::read_only(Privilege::View),
529 qualities: AttributeQualities::FIXED,
530 reporting: Reporting::OnChange,
531 conform: Conform {
532 clauses: &[Clause {
533 verdict: Conformance::Mandatory,
534 when: Condition::Feature(2),
535 }],
536 },
537 },
538 Attribute {
539 id: 0x0008,
540 name: "RockSetting",
541 kind: "RockBitmap",
542 access: Access::read_write_with(Privilege::View, Privilege::Operate),
543 qualities: AttributeQualities::empty(),
544 reporting: Reporting::OnChange,
545 conform: Conform {
546 clauses: &[Clause {
547 verdict: Conformance::Mandatory,
548 when: Condition::Feature(2),
549 }],
550 },
551 },
552 Attribute {
553 id: 0x0009,
554 name: "WindSupport",
555 kind: "WindBitmap",
556 access: Access::read_only(Privilege::View),
557 qualities: AttributeQualities::FIXED,
558 reporting: Reporting::OnChange,
559 conform: Conform {
560 clauses: &[Clause {
561 verdict: Conformance::Mandatory,
562 when: Condition::Feature(3),
563 }],
564 },
565 },
566 Attribute {
567 id: 0x000A,
568 name: "WindSetting",
569 kind: "WindBitmap",
570 access: Access::read_write_with(Privilege::View, Privilege::Operate),
571 qualities: AttributeQualities::empty(),
572 reporting: Reporting::OnChange,
573 conform: Conform {
574 clauses: &[Clause {
575 verdict: Conformance::Mandatory,
576 when: Condition::Feature(3),
577 }],
578 },
579 },
580 Attribute {
581 id: 0x000B,
582 name: "AirflowDirection",
583 kind: "AirflowDirectionEnum",
584 access: Access::read_write_with(Privilege::View, Privilege::Operate),
585 qualities: AttributeQualities::empty(),
586 reporting: Reporting::OnChange,
587 conform: Conform {
588 clauses: &[Clause {
589 verdict: Conformance::Mandatory,
590 when: Condition::Feature(5),
591 }],
592 },
593 },
594];
595
596pub const COMMANDS: &[Command] = &[Command {
598 id: 0x00,
599 name: "Step",
600 to_server: true,
601 response: None,
602 access: Access::invoke(Privilege::Operate),
603 conform: Conform {
604 clauses: &[Clause {
605 verdict: Conformance::Mandatory,
606 when: Condition::Feature(4),
607 }],
608 },
609}];
610
611pub const EVENTS: &[Event] = &[];
613
614pub const CLUSTER: Cluster = Cluster {
616 id: ID,
617 name: "Fan Control",
618 revision: REVISION,
619 pics: "FAN",
620 derived_from: None,
621 features: FEATURES,
622 attributes: ATTRIBUTES,
623 commands: COMMANDS,
624 events: EVENTS,
625};