1use crate::NodeId;
4use serde::{Deserialize, Serialize};
5use std::collections::BTreeMap;
6use zwave_security::SecurityClass;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
10#[serde(rename_all = "snake_case")]
11pub enum InterviewStage {
12 #[default]
14 New,
15 ProtocolInfo,
17 NodeInfo,
19 Security,
21 Identity,
23 Versions,
25 Endpoints,
27 CommandClasses,
29 Complete,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
35#[serde(rename_all = "snake_case")]
36pub enum NodeStatus {
37 #[default]
39 Unknown,
40 Alive,
42 Awake,
44 Asleep,
46 Dead,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
52pub struct ProtocolInfo {
53 pub listening: bool,
55 pub flirs: bool,
57 pub routing: bool,
59 pub controller: bool,
61 pub basic: u8,
63 pub generic: u8,
65 pub specific: u8,
67}
68
69impl ProtocolInfo {
70 pub fn sleeping(&self) -> bool {
72 !self.listening && !self.flirs
73 }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
78pub struct CcSupport {
79 pub version: u8,
81 pub secure: bool,
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
87pub struct Capabilities {
88 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
90 pub sensors: BTreeMap<u8, Vec<u8>>,
91 #[serde(default, skip_serializing_if = "Option::is_none")]
93 pub meter: Option<(u8, Vec<u8>)>,
94 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
96 pub notifications: BTreeMap<u8, Vec<u8>>,
97 #[serde(default, skip_serializing_if = "Vec::is_empty")]
99 pub binary_sensors: Vec<u8>,
100 #[serde(default, skip_serializing_if = "Option::is_none")]
102 pub switch_type: Option<u8>,
103}
104
105#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
107pub struct Endpoint {
108 #[serde(default)]
110 pub generic: u8,
111 #[serde(default)]
113 pub specific: u8,
114 #[serde(default)]
116 pub ccs: BTreeMap<u16, CcSupport>,
117 #[serde(default)]
119 pub caps: Capabilities,
120}
121
122impl Endpoint {
123 pub fn supports(&self, cc: u16) -> bool {
125 self.ccs.contains_key(&cc)
126 }
127
128 pub fn version(&self, cc: u16) -> u8 {
130 self.ccs.get(&cc).map(|c| c.version.max(1)).unwrap_or(0)
131 }
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
136pub struct Manufacturer {
137 pub manufacturer_id: u16,
139 pub product_type: u16,
141 pub product_id: u16,
143}
144
145#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
147pub struct NodeRecord {
148 pub id: NodeId,
150 #[serde(default)]
152 pub protocol: ProtocolInfo,
153 #[serde(default)]
155 pub stage: InterviewStage,
156 #[serde(default)]
158 pub security: Vec<SecurityClass>,
159 #[serde(default)]
161 pub secure_ccs_known: bool,
162 #[serde(default)]
164 pub manufacturer: Option<Manufacturer>,
165 #[serde(default)]
167 pub firmware: Option<String>,
168 #[serde(default)]
170 pub zwave_plus: Option<(u8, u8, u16, u16)>,
171 #[serde(default)]
173 pub endpoints: BTreeMap<u8, Endpoint>,
174 #[serde(default)]
176 pub wake_up_interval: Option<u32>,
177 #[serde(default)]
179 pub neighbors: Vec<NodeId>,
180 #[serde(default)]
182 pub name: Option<String>,
183 #[serde(default)]
185 pub location: Option<String>,
186 #[serde(default)]
188 pub dsk: Option<String>,
189}
190
191impl NodeRecord {
192 pub fn new(id: NodeId) -> Self {
194 let mut r = NodeRecord {
195 id,
196 ..Default::default()
197 };
198 r.endpoints.insert(0, Endpoint::default());
199 r
200 }
201
202 pub fn root(&self) -> &Endpoint {
204 static EMPTY: std::sync::OnceLock<Endpoint> = std::sync::OnceLock::new();
205 self.endpoints
206 .get(&0)
207 .unwrap_or_else(|| EMPTY.get_or_init(Endpoint::default))
208 }
209
210 pub fn root_mut(&mut self) -> &mut Endpoint {
212 self.endpoints.entry(0).or_default()
213 }
214
215 pub fn highest_s2(&self) -> Option<SecurityClass> {
217 self.security.iter().copied().filter(|c| c.is_s2()).max()
218 }
219
220 pub fn s0_only(&self) -> bool {
222 self.highest_s2().is_none() && self.security.contains(&SecurityClass::S0Legacy)
223 }
224
225 pub fn needs_security(&self, endpoint: u8, cc: u16) -> bool {
227 if self.security.is_empty() {
228 return false;
229 }
230 if matches!(cc, 0x98 | 0x9f) {
232 return false;
233 }
234 match self.endpoints.get(&endpoint).and_then(|e| e.ccs.get(&cc)) {
235 Some(s) => s.secure || !self.secure_ccs_known,
236 None => !self.secure_ccs_known || endpoint != 0,
238 }
239 }
240}