#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
mod controller;
pub mod device_config;
pub mod firmware;
mod inclusion;
mod interview;
pub mod node;
mod nvm;
pub mod nvm3;
pub mod store;
pub mod values;
pub use controller::{
Controller, ControllerInfo, Events, HostConfig, Topology, TopologyNode, MAX_PENDING_EVENTS,
};
pub use inclusion::{GrantPolicy, InclusionOptions, InclusionResult};
pub use node::{NodeRecord, NodeStatus};
pub use values::{ValueId, ValueKind, ValueUpdate};
pub use zwave_security::SecurityClass;
pub use zwave_serial::NodeId;
use zwave_security::provisioning::Entry as ProvisioningEntry;
pub fn is_long_range(node: NodeId) -> bool {
node > 255
}
#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct NetworkKeys {
#[serde(with = "hex_key")]
pub s0: [u8; 16],
#[serde(with = "hex_key")]
pub s2_unauthenticated: [u8; 16],
#[serde(with = "hex_key")]
pub s2_authenticated: [u8; 16],
#[serde(with = "hex_key")]
pub s2_access_control: [u8; 16],
#[serde(with = "hex_key")]
pub ecdh_private: [u8; 32],
#[serde(default, with = "hex_key_opt", skip_serializing_if = "Option::is_none")]
pub lr_s2_authenticated: Option<[u8; 16]>,
#[serde(default, with = "hex_key_opt", skip_serializing_if = "Option::is_none")]
pub lr_s2_access_control: Option<[u8; 16]>,
}
impl std::fmt::Debug for NetworkKeys {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("NetworkKeys(..)")
}
}
impl NetworkKeys {
pub fn generate() -> Result<Self, zwave_security::Error> {
use zwave_security::random_bytes;
Ok(NetworkKeys {
s0: random_bytes()?,
s2_unauthenticated: random_bytes()?,
s2_authenticated: random_bytes()?,
s2_access_control: random_bytes()?,
ecdh_private: random_bytes()?,
lr_s2_authenticated: Some(random_bytes()?),
lr_s2_access_control: Some(random_bytes()?),
})
}
pub fn fill_long_range(&mut self) -> Result<bool, zwave_security::Error> {
let mut added = false;
if self.lr_s2_authenticated.is_none() {
self.lr_s2_authenticated = Some(zwave_security::random_bytes()?);
added = true;
}
if self.lr_s2_access_control.is_none() {
self.lr_s2_access_control = Some(zwave_security::random_bytes()?);
added = true;
}
Ok(added)
}
pub fn long_range_key(&self, class: SecurityClass) -> Option<&[u8; 16]> {
match class {
SecurityClass::S2Authenticated => self.lr_s2_authenticated.as_ref(),
SecurityClass::S2AccessControl => self.lr_s2_access_control.as_ref(),
_ => None,
}
}
pub fn key(&self, class: SecurityClass) -> &[u8; 16] {
match class {
SecurityClass::S0Legacy => &self.s0,
SecurityClass::S2Unauthenticated => &self.s2_unauthenticated,
SecurityClass::S2Authenticated => &self.s2_authenticated,
SecurityClass::S2AccessControl => &self.s2_access_control,
}
}
pub fn controller_dsk(&self) -> zwave_security::dsk::Dsk {
let kp = zwave_security::kex::KeyPair::from_private(self.ecdh_private);
zwave_security::dsk::Dsk::from_public_key(kp.public())
}
}
mod hex_key_opt {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(k: &Option<[u8; 16]>, s: S) -> Result<S::Ok, S::Error> {
match k {
Some(k) => s.serialize_str(&hex::encode(k)),
None => s.serialize_none(),
}
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Option<[u8; 16]>, D::Error> {
let s: Option<String> = Option::deserialize(d)?;
match s {
None => Ok(None),
Some(s) => {
let v = hex::decode(s.trim()).map_err(serde::de::Error::custom)?;
v.try_into()
.map(Some)
.map_err(|_| serde::de::Error::custom("expected 16 bytes"))
}
}
}
}
mod hex_key {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer, const N: usize>(k: &[u8; N], s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&hex::encode(k))
}
pub fn deserialize<'de, D: Deserializer<'de>, const N: usize>(
d: D,
) -> Result<[u8; N], D::Error> {
let s = String::deserialize(d)?;
let v = hex::decode(s.trim()).map_err(serde::de::Error::custom)?;
v.try_into()
.map_err(|_| serde::de::Error::custom(format!("expected {N} bytes")))
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("serial: {0}")]
Serial(#[from] zwave_serial::SerialError),
#[error("controller: {0}")]
Controller(String),
#[error("node {0} did not acknowledge")]
NoAck(NodeId),
#[error("transmission to node {0} failed: {1}")]
TransmitFailed(NodeId, String),
#[error("node {0} did not answer in time")]
Timeout(NodeId),
#[error("security: {0}")]
Security(String),
#[error("no node {0}")]
UnknownNode(NodeId),
#[error("the controller is busy with another inclusion or exclusion")]
Busy,
#[error("{0}")]
Inclusion(String),
#[error("store: {0}")]
Store(String),
#[error("controller stopped")]
Stopped,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
Ready(ControllerInfo),
InclusionStarted,
InclusionStopped,
ExclusionStarted,
ExclusionStopped,
NodeAdded {
node: NodeId,
},
NodeRemoved {
node: NodeId,
},
GrantSecurityClasses {
node: NodeId,
requested: Vec<SecurityClass>,
},
ValidateDsk {
node: NodeId,
dsk: String,
},
SecurityBootstrapped {
node: NodeId,
classes: Vec<SecurityClass>,
},
SecurityFailed {
node: NodeId,
reason: String,
},
InterviewStage {
node: NodeId,
stage: node::InterviewStage,
},
InterviewCompleted {
node: NodeId,
},
InterviewFailed {
node: NodeId,
reason: String,
},
NodeUpdated {
node: NodeId,
},
NodeStatus {
node: NodeId,
status: NodeStatus,
},
Value(ValueUpdate),
Route {
node: NodeId,
repeaters: Vec<NodeId>,
rssi: Option<i8>,
},
Command {
node: NodeId,
endpoint: u8,
command: zwave_cc::Command,
},
FirmwareUpdateProgress {
node: NodeId,
sent: u16,
total: u16,
},
FirmwareUpdateFinished {
node: NodeId,
status: u8,
success: bool,
},
ProvisioningChanged {
entries: Vec<ProvisioningEntry>,
},
}
#[cfg(doctest)]
#[doc = include_str!("../../../README.md")]
struct ReadmeDoctests;