matc/clusters/codec/
ota_provider.rs

1//! Matter TLV encoders and decoders for OTA Software Update Provider Cluster
2//! Cluster ID: 0x0029
3//!
4//! This file is automatically generated from OTAProvider.xml
5
6use crate::tlv;
7use anyhow;
8
9
10// Import serialization helpers for octet strings
11use crate::clusters::helpers::{serialize_opt_bytes_as_hex};
12
13// Enum definitions
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16#[repr(u8)]
17pub enum ApplyUpdateAction {
18    /// Apply the update.
19    Proceed = 0,
20    /// Wait at least the given delay time.
21    Awaitnextaction = 1,
22    /// The OTA Provider is conveying a desire to rescind a previously provided Software Image.
23    Discontinue = 2,
24}
25
26impl ApplyUpdateAction {
27    /// Convert from u8 value
28    pub fn from_u8(value: u8) -> Option<Self> {
29        match value {
30            0 => Some(ApplyUpdateAction::Proceed),
31            1 => Some(ApplyUpdateAction::Awaitnextaction),
32            2 => Some(ApplyUpdateAction::Discontinue),
33            _ => None,
34        }
35    }
36
37    /// Convert to u8 value
38    pub fn to_u8(self) -> u8 {
39        self as u8
40    }
41}
42
43impl From<ApplyUpdateAction> for u8 {
44    fn from(val: ApplyUpdateAction) -> Self {
45        val as u8
46    }
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
50#[repr(u8)]
51pub enum DownloadProtocol {
52    /// Indicates support for synchronous BDX.
53    Bdxsynchronous = 0,
54    /// Indicates support for asynchronous BDX.
55    Bdxasynchronous = 1,
56    /// Indicates support for HTTPS.
57    Https = 2,
58    /// Indicates support for vendor specific protocol.
59    Vendorspecific = 3,
60}
61
62impl DownloadProtocol {
63    /// Convert from u8 value
64    pub fn from_u8(value: u8) -> Option<Self> {
65        match value {
66            0 => Some(DownloadProtocol::Bdxsynchronous),
67            1 => Some(DownloadProtocol::Bdxasynchronous),
68            2 => Some(DownloadProtocol::Https),
69            3 => Some(DownloadProtocol::Vendorspecific),
70            _ => None,
71        }
72    }
73
74    /// Convert to u8 value
75    pub fn to_u8(self) -> u8 {
76        self as u8
77    }
78}
79
80impl From<DownloadProtocol> for u8 {
81    fn from(val: DownloadProtocol) -> Self {
82        val as u8
83    }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
87#[repr(u8)]
88pub enum Status {
89    /// Indicates that the OTA Provider has an update available.
90    Updateavailable = 0,
91    /// Indicates OTA Provider may have an update, but it is not ready yet.
92    Busy = 1,
93    /// Indicates that there is definitely no update currently available from the OTA Provider.
94    Notavailable = 2,
95    /// Indicates that the requested download protocol is not supported by the OTA Provider.
96    Downloadprotocolnotsupported = 3,
97}
98
99impl Status {
100    /// Convert from u8 value
101    pub fn from_u8(value: u8) -> Option<Self> {
102        match value {
103            0 => Some(Status::Updateavailable),
104            1 => Some(Status::Busy),
105            2 => Some(Status::Notavailable),
106            3 => Some(Status::Downloadprotocolnotsupported),
107            _ => None,
108        }
109    }
110
111    /// Convert to u8 value
112    pub fn to_u8(self) -> u8 {
113        self as u8
114    }
115}
116
117impl From<Status> for u8 {
118    fn from(val: Status) -> Self {
119        val as u8
120    }
121}
122
123// Command encoders
124
125/// Parameters for QueryImage command
126pub struct QueryImageParams {
127    pub vendor_id: u16,
128    pub product_id: u16,
129    pub software_version: u32,
130    pub protocols_supported: Vec<DownloadProtocol>,
131    pub hardware_version: u16,
132    pub location: String,
133    pub requestor_can_consent: bool,
134    pub metadata_for_provider: Vec<u8>,
135}
136
137/// Encode QueryImage command (0x00)
138pub fn encode_query_image(params: QueryImageParams) -> anyhow::Result<Vec<u8>> {
139    let tlv = tlv::TlvItemEnc {
140        tag: 0,
141        value: tlv::TlvItemValueEnc::StructInvisible(vec![
142        (0, tlv::TlvItemValueEnc::UInt16(params.vendor_id)).into(),
143        (1, tlv::TlvItemValueEnc::UInt16(params.product_id)).into(),
144        (2, tlv::TlvItemValueEnc::UInt32(params.software_version)).into(),
145        (3, tlv::TlvItemValueEnc::StructAnon(params.protocols_supported.into_iter().map(|v| (0, tlv::TlvItemValueEnc::UInt8(v.to_u8())).into()).collect())).into(),
146        (4, tlv::TlvItemValueEnc::UInt16(params.hardware_version)).into(),
147        (5, tlv::TlvItemValueEnc::String(params.location)).into(),
148        (6, tlv::TlvItemValueEnc::Bool(params.requestor_can_consent)).into(),
149        (7, tlv::TlvItemValueEnc::OctetString(params.metadata_for_provider)).into(),
150        ]),
151    };
152    Ok(tlv.encode()?)
153}
154
155/// Encode ApplyUpdateRequest command (0x02)
156pub fn encode_apply_update_request(update_token: Vec<u8>, new_version: u32) -> anyhow::Result<Vec<u8>> {
157    let tlv = tlv::TlvItemEnc {
158        tag: 0,
159        value: tlv::TlvItemValueEnc::StructInvisible(vec![
160        (0, tlv::TlvItemValueEnc::OctetString(update_token)).into(),
161        (1, tlv::TlvItemValueEnc::UInt32(new_version)).into(),
162        ]),
163    };
164    Ok(tlv.encode()?)
165}
166
167/// Encode NotifyUpdateApplied command (0x04)
168pub fn encode_notify_update_applied(update_token: Vec<u8>, software_version: u32) -> anyhow::Result<Vec<u8>> {
169    let tlv = tlv::TlvItemEnc {
170        tag: 0,
171        value: tlv::TlvItemValueEnc::StructInvisible(vec![
172        (0, tlv::TlvItemValueEnc::OctetString(update_token)).into(),
173        (1, tlv::TlvItemValueEnc::UInt32(software_version)).into(),
174        ]),
175    };
176    Ok(tlv.encode()?)
177}
178
179#[derive(Debug, serde::Serialize)]
180pub struct QueryImageResponse {
181    pub status: Option<Status>,
182    pub delayed_action_time: Option<u32>,
183    pub image_uri: Option<String>,
184    pub software_version: Option<u32>,
185    pub software_version_string: Option<String>,
186    #[serde(serialize_with = "serialize_opt_bytes_as_hex")]
187    pub update_token: Option<Vec<u8>>,
188    pub user_consent_needed: Option<bool>,
189    #[serde(serialize_with = "serialize_opt_bytes_as_hex")]
190    pub metadata_for_requestor: Option<Vec<u8>>,
191}
192
193#[derive(Debug, serde::Serialize)]
194pub struct ApplyUpdateResponse {
195    pub action: Option<ApplyUpdateAction>,
196    pub delayed_action_time: Option<u32>,
197}
198
199// Command response decoders
200
201/// Decode QueryImageResponse command response (01)
202pub fn decode_query_image_response(inp: &tlv::TlvItemValue) -> anyhow::Result<QueryImageResponse> {
203    if let tlv::TlvItemValue::List(_fields) = inp {
204        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
205        Ok(QueryImageResponse {
206                status: item.get_int(&[0]).and_then(|v| Status::from_u8(v as u8)),
207                delayed_action_time: item.get_int(&[1]).map(|v| v as u32),
208                image_uri: item.get_string_owned(&[2]),
209                software_version: item.get_int(&[3]).map(|v| v as u32),
210                software_version_string: item.get_string_owned(&[4]),
211                update_token: item.get_octet_string_owned(&[5]),
212                user_consent_needed: item.get_bool(&[6]),
213                metadata_for_requestor: item.get_octet_string_owned(&[7]),
214        })
215    } else {
216        Err(anyhow::anyhow!("Expected struct fields"))
217    }
218}
219
220/// Decode ApplyUpdateResponse command response (03)
221pub fn decode_apply_update_response(inp: &tlv::TlvItemValue) -> anyhow::Result<ApplyUpdateResponse> {
222    if let tlv::TlvItemValue::List(_fields) = inp {
223        let item = tlv::TlvItem { tag: 0, value: inp.clone() };
224        Ok(ApplyUpdateResponse {
225                action: item.get_int(&[0]).and_then(|v| ApplyUpdateAction::from_u8(v as u8)),
226                delayed_action_time: item.get_int(&[1]).map(|v| v as u32),
227        })
228    } else {
229        Err(anyhow::anyhow!("Expected struct fields"))
230    }
231}
232