matc/clusters/codec/
ota_requestor.rs1use crate::tlv;
7use anyhow;
8use serde_json;
9
10
11#[derive(Debug, serde::Serialize)]
14pub struct ProviderLocation {
15 pub provider_node_id: Option<u64>,
16 pub endpoint: Option<u16>,
17}
18
19pub fn encode_announce_ota_provider(provider_node_id: u64, vendor_id: u16, announcement_reason: u8, metadata_for_node: Vec<u8>, endpoint: u16) -> anyhow::Result<Vec<u8>> {
23 let tlv = tlv::TlvItemEnc {
24 tag: 0,
25 value: tlv::TlvItemValueEnc::StructInvisible(vec![
26 (0, tlv::TlvItemValueEnc::UInt64(provider_node_id)).into(),
27 (1, tlv::TlvItemValueEnc::UInt16(vendor_id)).into(),
28 (2, tlv::TlvItemValueEnc::UInt8(announcement_reason)).into(),
29 (3, tlv::TlvItemValueEnc::OctetString(metadata_for_node)).into(),
30 (4, tlv::TlvItemValueEnc::UInt16(endpoint)).into(),
31 ]),
32 };
33 Ok(tlv.encode()?)
34}
35
36pub fn decode_default_ota_providers(inp: &tlv::TlvItemValue) -> anyhow::Result<Vec<ProviderLocation>> {
40 let mut res = Vec::new();
41 if let tlv::TlvItemValue::List(v) = inp {
42 for item in v {
43 res.push(ProviderLocation {
44 provider_node_id: item.get_int(&[1]),
45 endpoint: item.get_int(&[2]).map(|v| v as u16),
46 });
47 }
48 }
49 Ok(res)
50}
51
52pub fn decode_update_possible(inp: &tlv::TlvItemValue) -> anyhow::Result<bool> {
54 if let tlv::TlvItemValue::Bool(v) = inp {
55 Ok(*v)
56 } else {
57 Err(anyhow::anyhow!("Expected Bool"))
58 }
59}
60
61pub fn decode_update_state(inp: &tlv::TlvItemValue) -> anyhow::Result<u8> {
63 if let tlv::TlvItemValue::Int(v) = inp {
64 Ok(*v as u8)
65 } else {
66 Err(anyhow::anyhow!("Expected Integer"))
67 }
68}
69
70pub fn decode_update_state_progress(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u8>> {
72 if let tlv::TlvItemValue::Int(v) = inp {
73 Ok(Some(*v as u8))
74 } else {
75 Ok(None)
76 }
77}
78
79
80pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
92 if cluster_id != 0x002A {
94 return format!("{{\"error\": \"Invalid cluster ID. Expected 0x002A, got {}\"}}", cluster_id);
95 }
96
97 match attribute_id {
98 0x0000 => {
99 match decode_default_ota_providers(tlv_value) {
100 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
101 Err(e) => format!("{{\"error\": \"{}\"}}", e),
102 }
103 }
104 0x0001 => {
105 match decode_update_possible(tlv_value) {
106 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
107 Err(e) => format!("{{\"error\": \"{}\"}}", e),
108 }
109 }
110 0x0002 => {
111 match decode_update_state(tlv_value) {
112 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
113 Err(e) => format!("{{\"error\": \"{}\"}}", e),
114 }
115 }
116 0x0003 => {
117 match decode_update_state_progress(tlv_value) {
118 Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
119 Err(e) => format!("{{\"error\": \"{}\"}}", e),
120 }
121 }
122 _ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
123 }
124}
125
126pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
131 vec![
132 (0x0000, "DefaultOTAProviders"),
133 (0x0001, "UpdatePossible"),
134 (0x0002, "UpdateState"),
135 (0x0003, "UpdateStateProgress"),
136 ]
137}
138