1use crate::tlv;
7use anyhow;
8
9
10use crate::clusters::helpers::{serialize_opt_bytes_as_hex};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16#[repr(u8)]
17pub enum ApplyUpdateAction {
18 Proceed = 0,
20 Awaitnextaction = 1,
22 Discontinue = 2,
24}
25
26impl ApplyUpdateAction {
27 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 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 Bdxsynchronous = 0,
54 Bdxasynchronous = 1,
56 Https = 2,
58 Vendorspecific = 3,
60}
61
62impl DownloadProtocol {
63 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 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 Updateavailable = 0,
91 Busy = 1,
93 Notavailable = 2,
95 Downloadprotocolnotsupported = 3,
97}
98
99impl Status {
100 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 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
123pub 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
137pub 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
155pub 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
167pub 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
199pub 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
220pub 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