matc/clusters/codec/
mode_oven.rs

1//! Matter TLV encoders and decoders for Oven Mode Cluster
2//! Cluster ID: 0x0049
3//!
4//! This file is automatically generated from Mode_Oven.xml
5
6use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11// Enum definitions
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14#[repr(u16)]
15pub enum ModeTag {
16    Auto = 0,
17    Quick = 1,
18    Quiet = 2,
19    Lownoise = 3,
20    Lowenergy = 4,
21    Vacation = 5,
22    Min = 6,
23    Max = 7,
24    Night = 8,
25    Day = 9,
26    Bake = 16384,
27    Convection = 16385,
28    Grill = 16386,
29    Roast = 16387,
30    Clean = 16388,
31    ConvectionBake = 16389,
32    ConvectionRoast = 16390,
33    Warming = 16391,
34    Proofing = 16392,
35    Steam = 16393,
36    AirFry = 16394,
37    AirSousVide = 16395,
38    FrozenFood = 16396,
39}
40
41impl ModeTag {
42    /// Convert from u8 value (promoted to u16)
43    pub fn from_u8(value: u8) -> Option<Self> {
44        Self::from_u16(value as u16)
45    }
46
47    /// Convert from u16 value
48    pub fn from_u16(value: u16) -> Option<Self> {
49        match value {
50            0 => Some(ModeTag::Auto),
51            1 => Some(ModeTag::Quick),
52            2 => Some(ModeTag::Quiet),
53            3 => Some(ModeTag::Lownoise),
54            4 => Some(ModeTag::Lowenergy),
55            5 => Some(ModeTag::Vacation),
56            6 => Some(ModeTag::Min),
57            7 => Some(ModeTag::Max),
58            8 => Some(ModeTag::Night),
59            9 => Some(ModeTag::Day),
60            16384 => Some(ModeTag::Bake),
61            16385 => Some(ModeTag::Convection),
62            16386 => Some(ModeTag::Grill),
63            16387 => Some(ModeTag::Roast),
64            16388 => Some(ModeTag::Clean),
65            16389 => Some(ModeTag::ConvectionBake),
66            16390 => Some(ModeTag::ConvectionRoast),
67            16391 => Some(ModeTag::Warming),
68            16392 => Some(ModeTag::Proofing),
69            16393 => Some(ModeTag::Steam),
70            16394 => Some(ModeTag::AirFry),
71            16395 => Some(ModeTag::AirSousVide),
72            16396 => Some(ModeTag::FrozenFood),
73            _ => None,
74        }
75    }
76
77    /// Convert to u8 value (truncated if value > 255)
78    pub fn to_u8(self) -> u8 {
79        self as u8
80    }
81
82    /// Convert to u16 value
83    pub fn to_u16(self) -> u16 {
84        self as u16
85    }
86}
87
88impl From<ModeTag> for u16 {
89    fn from(val: ModeTag) -> Self {
90        val as u16
91    }
92}
93
94// Struct definitions
95
96#[derive(Debug, serde::Serialize)]
97pub struct ModeOption {
98    pub label: Option<u8>,
99    pub mode: Option<u8>,
100    pub mode_tags: Option<u8>,
101}
102
103// Attribute decoders
104
105/// Decode SupportedModes attribute (0x0000)
106pub fn decode_supported_modes(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
107    if let tlv::TlvItemValue::Int(v) = inp {
108        Ok(*v as u8)
109    } else {
110        Err(anyhow::anyhow!("Expected UInt8"))
111    }
112}
113
114/// Decode CurrentMode attribute (0x0001)
115pub fn decode_current_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
116    if let tlv::TlvItemValue::Int(v) = inp {
117        Ok(*v as u8)
118    } else {
119        Err(anyhow::anyhow!("Expected UInt8"))
120    }
121}
122
123/// Decode StartUpMode attribute (0x0002)
124pub fn decode_start_up_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
125    if let tlv::TlvItemValue::Int(v) = inp {
126        Ok(*v as u8)
127    } else {
128        Err(anyhow::anyhow!("Expected UInt8"))
129    }
130}
131
132/// Decode OnMode attribute (0x0003)
133pub fn decode_on_mode(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
134    if let tlv::TlvItemValue::Int(v) = inp {
135        Ok(*v as u8)
136    } else {
137        Err(anyhow::anyhow!("Expected UInt8"))
138    }
139}
140
141
142// JSON dispatcher function
143
144/// Decode attribute value and return as JSON string
145///
146/// # Parameters
147/// * `cluster_id` - The cluster identifier
148/// * `attribute_id` - The attribute identifier
149/// * `tlv_value` - The TLV value to decode
150///
151/// # Returns
152/// JSON string representation of the decoded value or error
153pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
154    // Verify this is the correct cluster
155    if cluster_id != 0x0049 {
156        return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0049, got {}\"}}", cluster_id);
157    }
158
159    match attribute_id {
160        0x0000 => {
161            match decode_supported_modes(tlv_value) {
162                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
163                Err(e) => format!("{{\"error\": \"{}\"}}", e),
164            }
165        }
166        0x0001 => {
167            match decode_current_mode(tlv_value) {
168                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
169                Err(e) => format!("{{\"error\": \"{}\"}}", e),
170            }
171        }
172        0x0002 => {
173            match decode_start_up_mode(tlv_value) {
174                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
175                Err(e) => format!("{{\"error\": \"{}\"}}", e),
176            }
177        }
178        0x0003 => {
179            match decode_on_mode(tlv_value) {
180                Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
181                Err(e) => format!("{{\"error\": \"{}\"}}", e),
182            }
183        }
184        _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
185    }
186}
187
188/// Get list of all attributes supported by this cluster
189///
190/// # Returns
191/// Vector of tuples containing (attribute_id, attribute_name)
192pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
193    vec![
194        (0x0000, "SupportedModes"),
195        (0x0001, "CurrentMode"),
196        (0x0002, "StartUpMode"),
197        (0x0003, "OnMode"),
198    ]
199}
200