1use std::collections::{HashMap, HashSet};
2use std::sync::Arc;
3use std::sync::atomic::AtomicU32;
4
5use anyhow::{Context, Result};
6
7use crate::fabric;
8
9use super::Device;
10use super::types::{AttributeOverride, DeviceConfig, FabricInfo, PersistedDeviceState, PersistedFabricState};
11
12static PERSISTED_ATTRIBUTES: &[(u16, u32, u32)] = &[];
13
14impl Device {
15 pub(crate) fn collect_attribute_overrides(&self) -> Vec<AttributeOverride> {
16 let mut overrides = Vec::new();
17 for &(endpoint, cluster, attribute) in PERSISTED_ATTRIBUTES.iter().chain(self.extra_persisted.iter()) {
18 if let Some(tlv) = self.attributes.get(&(endpoint, cluster, attribute)) {
19 overrides.push(AttributeOverride {
20 endpoint,
21 cluster,
22 attribute,
23 tlv_hex: hex::encode(tlv),
24 });
25 }
26 }
27 overrides
28 }
29
30 pub fn add_persisted_attribute(&mut self, endpoint: u16, cluster: u32, attribute: u32) {
32 let key = (endpoint, cluster, attribute);
33 if !self.extra_persisted.contains(&key) {
34 self.extra_persisted.push(key);
35 }
36 }
37
38 pub(crate) fn save_state(&self, state_dir: &str) -> Result<()> {
40 if self.fabrics.is_empty() {
41 let path = format!("{}/device_state.json", state_dir);
42 if std::path::Path::new(&path).exists() {
43 std::fs::remove_file(&path)?;
44 log::info!("All fabrics removed - deleted {}", path);
45 }
46 return Ok(());
47 }
48
49 let fabrics: Vec<PersistedFabricState> = self
50 .fabrics
51 .iter()
52 .map(|fi| PersistedFabricState {
53 fabric_index: fi.fabric_index,
54 trusted_root_cert: fi.trusted_root_cert.clone(),
55 noc: fi.noc.clone(),
56 icac: fi.icac.clone(),
57 ipk: fi.ipk.clone(),
58 controller_id: fi.controller_id,
59 vendor_id: fi.vendor_id,
60 device_matter_cert: fi.device_matter_cert.clone(),
61 label: fi.label.clone(),
62 })
63 .collect();
64
65 let state = PersistedDeviceState {
66 operational_key_hex: hex::encode(self.operational_key.to_bytes()),
67 next_fabric_index: self.next_fabric_index,
68 fabrics,
69 attribute_overrides: self.collect_attribute_overrides(),
70 };
71
72 std::fs::create_dir_all(state_dir)?;
73 let path = format!("{}/device_state.json", state_dir);
74 let json = serde_json::to_string_pretty(&state)?;
75 std::fs::write(&path, json)?;
76 log::info!("Device state saved to {}", path);
77 Ok(())
78 }
79
80 pub(crate) async fn register_operational_mdns(&self, fabric_idx: usize) -> Result<()> {
83 let fi = &self.fabrics[fabric_idx];
84 let nod_id = fi.device_node_id()?;
85 let ca_public_key = fi.ca_public_key()?;
86 let fabric_id = fi.fabric_id()?;
87 let ca_id = fi.ca_id()?;
88 let fabric = fabric::Fabric::new(fabric_id, ca_id, &ca_public_key);
89
90 let iname = format!(
91 "{}-{:016X}",
92 hex::encode_upper(fabric.compressed()?),
93 nod_id
94 );
95 let op_port: u16 = self
96 .config
97 .listen_address
98 .rsplit(':')
99 .next()
100 .and_then(|p| p.parse().ok())
101 .unwrap_or(5540);
102 let (adv_v4, adv_v6) = self.config.split_advertise_ips();
103 let svc = crate::mdns2::ServiceRegistration {
104 instance_name: iname,
105 service_type: "_matter._tcp.local".to_string(),
106 port: op_port,
107 txt_records: vec![],
108 hostname: self.config.hostname.clone(),
109 ttl: 120,
110 subtypes: vec![],
111 ips_v4: adv_v4,
112 ips_v6: adv_v6,
113 };
114 self.mdns.register_service(svc).await;
115 Ok(())
116 }
117
118 pub async fn from_persisted_state(
123 config: DeviceConfig,
124 mdns: Arc<crate::mdns2::MdnsService>,
125 state_dir: &str,
126 ) -> Result<Self> {
127 let path = format!("{}/device_state.json", state_dir);
128 let json = std::fs::read_to_string(&path)
129 .with_context(|| format!("Cannot read persisted state from {}", path))?;
130 let state: PersistedDeviceState = serde_json::from_str(&json)?;
131
132 let key_bytes = hex::decode(&state.operational_key_hex)
134 .context("Invalid hex in operational_key_hex")?;
135 let operational_key = p256::SecretKey::from_slice(&key_bytes)
136 .context("Invalid P-256 scalar in persisted operational key")?;
137
138 let socket = tokio::net::UdpSocket::bind(&config.listen_address).await?;
139
140 let mut salt = vec![0u8; 32];
141 rand::RngCore::fill_bytes(&mut rand::thread_rng(), &mut salt);
142
143 let fabrics: Vec<FabricInfo> = state
144 .fabrics
145 .iter()
146 .map(|pf| FabricInfo {
147 fabric_index: pf.fabric_index,
148 ipk: pf.ipk.clone(),
149 fabric: None,
150 device_matter_cert: pf.device_matter_cert.clone(),
151 controller_id: pf.controller_id,
152 vendor_id: pf.vendor_id,
153 trusted_root_cert: pf.trusted_root_cert.clone(),
154 noc: pf.noc.clone(),
155 icac: pf.icac.clone(),
156 label: pf.label.clone(),
157 })
158 .collect();
159
160 let mut device = Self {
161 config,
162 socket,
163 salt,
164 pbkdf_iterations: 1000,
165 operational_key,
166 message_counter: AtomicU32::new(rand::random()),
167 pase_state: None,
168 pase_session: None,
169 case_states: HashMap::new(),
170 case_sessions: Vec::new(),
171 subscribe_states: Vec::new(),
172 active_subscriptions: Vec::new(),
173 pending_chunks: Vec::new(),
174 fabrics,
175 next_fabric_index: state.next_fabric_index,
176 pending_root_cert: None,
177 received_counters: HashSet::new(),
178 endpoints: vec![0],
179 attributes: HashMap::new(),
180 dirty_attributes: HashSet::new(),
181 mdns,
182 extra_persisted: Vec::new(),
183 };
184
185 device.setup_default_attributes()?;
186
187 for ov in &state.attribute_overrides {
191 let tlv = hex::decode(&ov.tlv_hex)
192 .with_context(|| format!("Bad tlv_hex for ({},{},{})", ov.endpoint, ov.cluster, ov.attribute))?;
193 device.attributes.insert((ov.endpoint, ov.cluster, ov.attribute), tlv);
194 }
195
196 device.rebuild_fabrics_attribute()?;
198 device.dirty_attributes.clear();
199
200 for i in 0..device.fabrics.len() {
202 device.register_operational_mdns(i).await?;
203 }
204
205 log::info!(
206 "Device state restored from {} ({} fabric(s))",
207 path,
208 device.fabrics.len()
209 );
210 Ok(device)
211 }
212}