use crate::frame::{DataFrame, FrameType};
use crate::function::FunctionId as F;
use crate::NodeId;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("malformed {0} payload")]
pub struct Malformed(pub &'static str);
type Result<T> = std::result::Result<T, Malformed>;
fn need(p: &[u8], n: usize, what: &'static str) -> Result<()> {
if p.len() < n {
Err(Malformed(what))
} else {
Ok(())
}
}
pub fn node_bytes(node: NodeId, len: usize, out: &mut Vec<u8>) {
if len == 2 {
out.extend_from_slice(&node.to_be_bytes());
} else {
out.push(node as u8);
}
}
pub fn parse_node_id(p: &[u8], len: usize) -> Option<NodeId> {
node_id(p, 0, len, "node id").ok()
}
fn node_id(p: &[u8], at: usize, len: usize, what: &'static str) -> Result<NodeId> {
need(p, at + len, what)?;
Ok(if len == 2 {
u16::from_be_bytes([p[at], p[at + 1]])
} else {
u16::from(p[at])
})
}
pub fn parse_cc_list(p: &[u8]) -> (Vec<u16>, Vec<u16>) {
let mut supported = Vec::new();
let mut controlled = Vec::new();
let mut after_mark = false;
let mut i = 0;
while i < p.len() {
let b = p[i];
if b == 0xef {
after_mark = true;
i += 1;
continue;
}
let cc = if b >= 0xf1 {
let Some(&lo) = p.get(i + 1) else { break };
i += 2;
(u16::from(b) << 8) | u16::from(lo)
} else {
i += 1;
u16::from(b)
};
if after_mark {
controlled.push(cc);
} else {
supported.push(cc);
}
}
(supported, controlled)
}
pub fn encode_cc_list(ccs: &[u16], out: &mut Vec<u8>) {
for &cc in ccs {
if cc > 0xff {
out.extend_from_slice(&cc.to_be_bytes());
} else {
out.push(cc as u8);
}
}
}
pub fn parse_node_mask(mask: &[u8], first: NodeId) -> Vec<NodeId> {
let mut v = Vec::new();
for (i, b) in mask.iter().enumerate() {
for bit in 0..8 {
if b & (1 << bit) != 0 {
v.push(first + (i * 8 + bit) as NodeId);
}
}
}
v
}
pub fn encode_node_mask(nodes: &[NodeId], len: usize) -> Vec<u8> {
let mut m = vec![0u8; len];
for &n in nodes {
let i = usize::from(n.saturating_sub(1));
if n >= 1 && i / 8 < len {
m[i / 8] |= 1 << (i % 8);
}
}
m
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SerialApiCapabilities {
pub firmware_major: u8,
pub firmware_minor: u8,
pub manufacturer_id: u16,
pub product_type: u16,
pub product_id: u16,
pub functions: [u8; 32],
}
impl SerialApiCapabilities {
pub fn parse(p: &[u8]) -> Result<Self> {
need(p, 8, "serial API capabilities")?;
let mut functions = [0u8; 32];
let mask = &p[8..p.len().min(40)];
functions[..mask.len()].copy_from_slice(mask);
Ok(SerialApiCapabilities {
firmware_major: p[0],
firmware_minor: p[1],
manufacturer_id: u16::from_be_bytes([p[2], p[3]]),
product_type: u16::from_be_bytes([p[4], p[5]]),
product_id: u16::from_be_bytes([p[6], p[7]]),
functions,
})
}
pub fn encode(&self) -> Vec<u8> {
let mut v = vec![self.firmware_major, self.firmware_minor];
v.extend_from_slice(&self.manufacturer_id.to_be_bytes());
v.extend_from_slice(&self.product_type.to_be_bytes());
v.extend_from_slice(&self.product_id.to_be_bytes());
v.extend_from_slice(&self.functions);
v
}
pub fn supports(&self, f: F) -> bool {
let i = usize::from(f.0.wrapping_sub(1));
f.0 != 0 && self.functions[i / 8] & (1 << (i % 8)) != 0
}
pub fn mask(functions: &[F]) -> [u8; 32] {
let mut m = [0u8; 32];
for f in functions {
if f.0 != 0 {
let i = usize::from(f.0 - 1);
m[i / 8] |= 1 << (i % 8);
}
}
m
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InitData {
pub api_version: u8,
pub capabilities: u8,
pub nodes: Vec<NodeId>,
pub chip_type: Option<u8>,
pub chip_version: Option<u8>,
}
impl InitData {
pub fn is_end_node(&self) -> bool {
self.capabilities & 0x01 != 0
}
pub fn is_secondary(&self) -> bool {
self.capabilities & 0x04 != 0
}
pub fn is_sis(&self) -> bool {
self.capabilities & 0x08 != 0
}
pub fn parse(p: &[u8]) -> Result<Self> {
need(p, 3, "init data")?;
let len = usize::from(p[2]).min(p.len() - 3);
let nodes = parse_node_mask(&p[3..3 + len], 1);
Ok(InitData {
api_version: p[0],
capabilities: p[1],
nodes,
chip_type: p.get(3 + len).copied(),
chip_version: p.get(4 + len).copied(),
})
}
pub fn encode(&self) -> Vec<u8> {
let mut v = vec![self.api_version, self.capabilities, 29];
v.extend_from_slice(&encode_node_mask(&self.nodes, 29));
v.extend(self.chip_type);
v.extend(self.chip_version);
v
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ControllerVersion {
pub version: String,
pub library_type: u8,
}
impl ControllerVersion {
pub fn parse(p: &[u8]) -> Result<Self> {
let nul = p
.iter()
.position(|&b| b == 0)
.ok_or(Malformed("controller version"))?;
let version = String::from_utf8_lossy(&p[..nul]).trim().to_string();
Ok(ControllerVersion {
version,
library_type: p.get(nul + 1).copied().unwrap_or(0),
})
}
pub fn encode(&self) -> Vec<u8> {
let mut v = self.version.as_bytes().to_vec();
v.push(0);
v.push(self.library_type);
v
}
pub fn library_name(&self) -> &'static str {
match self.library_type {
0x01 => "static controller",
0x02 => "controller",
0x03 => "enhanced end node",
0x04 => "end node",
0x05 => "installer",
0x06 => "routing end node",
0x07 => "bridge controller",
0x08 => "device under test",
_ => "unknown",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ControllerId {
pub home_id: u32,
pub node_id: NodeId,
}
impl ControllerId {
pub fn parse(p: &[u8], node_id_len: usize) -> Result<Self> {
need(p, 4 + node_id_len, "controller id")?;
Ok(ControllerId {
home_id: u32::from_be_bytes([p[0], p[1], p[2], p[3]]),
node_id: node_id(p, 4, node_id_len, "controller id")?,
})
}
pub fn encode(&self, node_id_len: usize) -> Vec<u8> {
let mut v = self.home_id.to_be_bytes().to_vec();
node_bytes(self.node_id, node_id_len, &mut v);
v
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NodeProtocolInfo {
pub listening: bool,
pub routing: bool,
pub flirs_1000: bool,
pub flirs_250: bool,
pub beaming: bool,
pub controller: bool,
pub security: bool,
pub protocol_version: u8,
pub speeds: u8,
pub basic: u8,
pub generic: u8,
pub specific: u8,
}
impl NodeProtocolInfo {
pub fn reachable(&self) -> bool {
self.listening || self.flirs_250 || self.flirs_1000
}
pub fn parse(p: &[u8]) -> Result<Option<Self>> {
need(p, 6, "node protocol info")?;
if p[3] == 0 && p[4] == 0 {
return Ok(None);
}
let cap = p[0];
let sec = p[1];
let max_baud = (cap >> 3) & 0x07;
let mut speeds = match max_baud {
0b010 => 0b011,
0b001 => 0b001,
_ => 0b001,
};
if p[2] & 0x01 != 0 {
speeds |= 0b100;
}
Ok(Some(NodeProtocolInfo {
listening: cap & 0x80 != 0,
routing: cap & 0x40 != 0,
flirs_1000: sec & 0x40 != 0,
flirs_250: sec & 0x20 != 0,
beaming: sec & 0x10 != 0,
controller: sec & 0x02 != 0,
security: sec & 0x01 != 0,
protocol_version: cap & 0x07,
speeds,
basic: p[3],
generic: p[4],
specific: p[5],
}))
}
pub fn encode(&self) -> Vec<u8> {
let cap = (if self.listening { 0x80 } else { 0 })
| (if self.routing { 0x40 } else { 0 })
| (0b010 << 3)
| (self.protocol_version & 0x07);
let sec = (if self.flirs_1000 { 0x40 } else { 0 })
| (if self.flirs_250 { 0x20 } else { 0 })
| (if self.beaming { 0x10 } else { 0 })
| (if self.controller { 0x02 } else { 0 })
| (if self.security { 0x01 } else { 0 })
| 0x04;
vec![
cap,
sec,
if self.speeds & 0b100 != 0 { 1 } else { 0 },
self.basic,
self.generic,
self.specific,
]
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct NodeInfo {
pub basic: u8,
pub generic: u8,
pub specific: u8,
pub supported: Vec<u16>,
pub controlled: Vec<u16>,
}
impl NodeInfo {
pub fn parse(p: &[u8]) -> Result<Self> {
need(p, 3, "node info")?;
let (supported, controlled) = parse_cc_list(&p[3..]);
Ok(NodeInfo {
basic: p[0],
generic: p[1],
specific: p[2],
supported,
controlled,
})
}
pub fn encode(&self) -> Vec<u8> {
let mut v = vec![self.basic, self.generic, self.specific];
encode_cc_list(&self.supported, &mut v);
if !self.controlled.is_empty() {
v.push(0xef);
encode_cc_list(&self.controlled, &mut v);
}
v
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RxStatus(pub u8);
impl RxStatus {
pub fn low_power(self) -> bool {
self.0 & 0x02 != 0
}
pub fn broadcast(self) -> bool {
self.0 & 0x0c == 0x04
}
pub fn multicast(self) -> bool {
self.0 & 0x0c == 0x08
}
pub fn routed(self) -> bool {
self.0 & 0x01 != 0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ApplicationCommand {
pub rx_status: RxStatus,
pub source: NodeId,
pub destination: Option<NodeId>,
pub command: Vec<u8>,
pub rssi: Option<i8>,
}
fn rssi(b: Option<&u8>) -> Option<i8> {
b.map(|&v| v as i8).filter(|v| *v < 0x7d)
}
impl ApplicationCommand {
pub fn parse(p: &[u8], node_id_len: usize) -> Result<Self> {
need(p, 2 + node_id_len, "application command")?;
let source = node_id(p, 1, node_id_len, "application command")?;
let at = 1 + node_id_len;
let len = usize::from(p[at]);
let body = p
.get(at + 1..at + 1 + len)
.ok_or(Malformed("application command"))?;
Ok(ApplicationCommand {
rx_status: RxStatus(p[0]),
source,
destination: None,
command: body.to_vec(),
rssi: rssi(p.get(at + 1 + len)),
})
}
pub fn parse_bridge(p: &[u8], node_id_len: usize) -> Result<Self> {
need(p, 2 + 2 * node_id_len, "bridge application command")?;
let destination = node_id(p, 1, node_id_len, "bridge application command")?;
let source = node_id(
p,
1 + node_id_len,
node_id_len,
"bridge application command",
)?;
let at = 1 + 2 * node_id_len;
let len = usize::from(p[at]);
let body = p
.get(at + 1..at + 1 + len)
.ok_or(Malformed("bridge application command"))?;
let mut rest = at + 1 + len;
if let Some(&mlen) = p.get(rest) {
rest += 1 + usize::from(mlen);
}
Ok(ApplicationCommand {
rx_status: RxStatus(p[0]),
source,
destination: Some(destination),
command: body.to_vec(),
rssi: rssi(p.get(rest)),
})
}
pub fn to_frame(&self, node_id_len: usize) -> DataFrame {
let mut v = vec![self.rx_status.0];
node_bytes(self.source, node_id_len, &mut v);
v.push(self.command.len() as u8);
v.extend_from_slice(&self.command);
if let Some(r) = self.rssi {
v.push(r as u8);
}
DataFrame::request(F::APPLICATION_COMMAND_HANDLER, v)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApplicationUpdate {
NodeInfoReceived {
node: NodeId,
info: NodeInfo,
},
NodeInfoRequestFailed,
SmartStartHomeIdReceived {
nwi_home_id: [u8; 4],
long_range: bool,
info: NodeInfo,
},
SmartStartIncludedNodeInfo {
nwi_home_id: [u8; 4],
},
NodeAdded {
node: NodeId,
},
NodeRemoved {
node: NodeId,
},
SucIdChanged,
Other {
kind: u8,
},
}
impl ApplicationUpdate {
pub fn parse(p: &[u8], node_id_len: usize) -> Result<Self> {
need(p, 1, "application update")?;
let kind = p[0];
let rest = &p[1..];
let node_info = |rest: &[u8]| -> Result<(NodeId, NodeInfo)> {
let node = node_id(rest, 0, node_id_len, "node info update")?;
let len_at = node_id_len;
need(rest, len_at + 1, "node info update")?;
let len = usize::from(rest[len_at]);
let body = &rest[len_at + 1..rest.len().min(len_at + 1 + len)];
Ok((node, NodeInfo::parse(body)?))
};
Ok(match kind {
0x84 => {
let (node, info) = node_info(rest)?;
ApplicationUpdate::NodeInfoReceived { node, info }
}
0x81 => ApplicationUpdate::NodeInfoRequestFailed,
0x85 | 0x87 => {
let at = node_id_len + 1;
need(rest, at + 5, "smartstart request")?;
let nwi: [u8; 4] = rest[at..at + 4].try_into().expect("4 bytes");
let body = &rest[at + 5..];
let info = NodeInfo::parse(body).unwrap_or_default();
ApplicationUpdate::SmartStartHomeIdReceived {
nwi_home_id: nwi,
long_range: kind == 0x87,
info,
}
}
0x86 => {
let at = node_id_len + 1;
need(rest, at + 4, "smartstart node info")?;
ApplicationUpdate::SmartStartIncludedNodeInfo {
nwi_home_id: rest[at..at + 4].try_into().expect("4 bytes"),
}
}
0x40 => ApplicationUpdate::NodeAdded {
node: node_id(rest, 0, node_id_len, "node added")?,
},
0x20 => ApplicationUpdate::NodeRemoved {
node: node_id(rest, 0, node_id_len, "node removed")?,
},
0x10 => ApplicationUpdate::SucIdChanged,
k => ApplicationUpdate::Other { kind: k },
})
}
pub fn to_frame(&self, node_id_len: usize) -> DataFrame {
let zero = |kind: u8| {
let mut v = vec![kind];
node_bytes(0, node_id_len, &mut v);
v
};
let p = match self {
ApplicationUpdate::NodeInfoReceived { node, info } => {
let body = info.encode();
let mut v = vec![0x84];
node_bytes(*node, node_id_len, &mut v);
v.push(body.len() as u8);
v.extend_from_slice(&body);
v
}
ApplicationUpdate::NodeInfoRequestFailed => {
let mut v = zero(0x81);
v.push(0);
v
}
ApplicationUpdate::SmartStartHomeIdReceived {
nwi_home_id,
long_range,
info,
} => {
let body = info.encode();
let mut v = zero(if *long_range { 0x87 } else { 0x85 });
v.push(0);
v.extend_from_slice(nwi_home_id);
v.push(body.len().saturating_sub(3) as u8);
v.extend_from_slice(&body);
v
}
ApplicationUpdate::SmartStartIncludedNodeInfo { nwi_home_id } => {
let mut v = zero(0x86);
v.push(0);
v.extend_from_slice(nwi_home_id);
v
}
ApplicationUpdate::NodeAdded { node } | ApplicationUpdate::NodeRemoved { node } => {
let mut v = vec![if matches!(self, ApplicationUpdate::NodeAdded { .. }) {
0x40
} else {
0x20
}];
node_bytes(*node, node_id_len, &mut v);
v.push(0);
v
}
ApplicationUpdate::SucIdChanged => {
let mut v = zero(0x10);
v.push(0);
v
}
ApplicationUpdate::Other { kind } => {
let mut v = zero(*kind);
v.push(0);
v
}
};
DataFrame::request(F::APPLICATION_UPDATE, p)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransmitStatus {
Ok,
NoAck,
Fail,
NotIdle,
NoRoute,
Other(u8),
}
impl TransmitStatus {
pub fn from_u8(v: u8) -> Self {
match v {
0x00 => TransmitStatus::Ok,
0x01 => TransmitStatus::NoAck,
0x02 => TransmitStatus::Fail,
0x03 => TransmitStatus::NotIdle,
0x04 => TransmitStatus::NoRoute,
o => TransmitStatus::Other(o),
}
}
pub fn to_u8(self) -> u8 {
match self {
TransmitStatus::Ok => 0,
TransmitStatus::NoAck => 1,
TransmitStatus::Fail => 2,
TransmitStatus::NotIdle => 3,
TransmitStatus::NoRoute => 4,
TransmitStatus::Other(o) => o,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TxReport {
pub ticks: u16,
pub repeaters: Vec<NodeId>,
pub ack_rssi: Option<i8>,
pub repeater_rssi: Vec<i8>,
pub ack_channel: Option<u8>,
pub tx_channel: u8,
pub route_scheme: u8,
pub speed: u8,
pub attempts: u8,
pub failed_from: Option<NodeId>,
pub failed_to: Option<NodeId>,
}
impl TxReport {
pub fn parse(p: &[u8]) -> Option<Self> {
if p.len() < 17 {
return None;
}
let n = usize::from(p[2]).min(4);
let repeaters: Vec<NodeId> = p[11..11 + n].iter().map(|&b| NodeId::from(b)).collect();
let repeater_rssi = p[4..4 + n]
.iter()
.map(|&b| b as i8)
.filter(|v| *v < 0x7d)
.collect();
let failed = |i: usize| p.get(i).copied().filter(|&b| b != 0).map(NodeId::from);
Some(TxReport {
ticks: u16::from_be_bytes([p[0], p[1]]),
repeaters,
ack_rssi: rssi(p.get(3)),
repeater_rssi,
ack_channel: Some(p[8]),
tx_channel: p[9],
route_scheme: p[10],
speed: p[15] & 0x07,
attempts: p[16],
failed_from: failed(17),
failed_to: failed(18),
})
}
pub fn encode(&self) -> Vec<u8> {
let mut v = self.ticks.to_be_bytes().to_vec();
v.push(self.repeaters.len() as u8);
v.push(self.ack_rssi.map(|r| r as u8).unwrap_or(0x7f));
for i in 0..4 {
v.push(self.repeater_rssi.get(i).map(|r| *r as u8).unwrap_or(0x7f));
}
v.push(self.ack_channel.unwrap_or(0));
v.push(self.tx_channel);
v.push(self.route_scheme);
for i in 0..4 {
v.push(self.repeaters.get(i).map(|r| *r as u8).unwrap_or(0));
}
v.push(self.speed);
v.push(self.attempts);
v.push(self.failed_from.unwrap_or(0) as u8);
v.push(self.failed_to.unwrap_or(0) as u8);
v
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SendDataResult {
pub status: TransmitStatus,
pub report: Option<TxReport>,
}
impl SendDataResult {
pub fn parse(p: &[u8]) -> Result<Self> {
need(p, 2, "send data callback")?;
Ok(SendDataResult {
status: TransmitStatus::from_u8(p[1]),
report: TxReport::parse(&p[2..]),
})
}
}
pub mod tx_options {
pub const ACK: u8 = 0x01;
pub const LOW_POWER: u8 = 0x02;
pub const AUTO_ROUTE: u8 = 0x04;
pub const NO_ROUTE: u8 = 0x10;
pub const EXPLORE: u8 = 0x20;
pub const DEFAULT: u8 = ACK | AUTO_ROUTE | EXPLORE;
}
pub fn send_data(
node: NodeId,
node_id_len: usize,
command: &[u8],
options: u8,
callback_id: u8,
) -> DataFrame {
let mut v = Vec::with_capacity(command.len() + 5);
node_bytes(node, node_id_len, &mut v);
v.push(command.len() as u8);
v.extend_from_slice(command);
v.push(options);
v.push(callback_id);
DataFrame::request(F::SEND_DATA, v)
}
pub mod add_mode {
pub const ANY: u8 = 0x01;
pub const STOP: u8 = 0x05;
pub const SMART_START_DSK: u8 = 0x08;
pub const SMART_START_LISTEN: u8 = 0x09;
pub const HIGH_POWER: u8 = 0x80;
pub const PROTOCOL_LONG_RANGE: u8 = 0x20;
pub const NETWORK_WIDE: u8 = 0x40;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AddNodeStatus {
Ready,
NodeFound,
AddingEndNode {
node: NodeId,
info: NodeInfo,
},
AddingController {
node: NodeId,
info: NodeInfo,
},
ProtocolDone,
Done {
node: NodeId,
},
Failed,
Other(u8),
}
impl AddNodeStatus {
pub fn parse(p: &[u8], node_id_len: usize) -> Result<Self> {
need(p, 2, "add node status")?;
let node = || node_id(p, 2, node_id_len, "add node status");
let info = || -> Result<NodeInfo> {
let at = 2 + node_id_len;
need(p, at + 1, "add node status")?;
let len = usize::from(p[at]);
NodeInfo::parse(&p[at + 1..p.len().min(at + 1 + len)])
};
Ok(match p[1] {
0x01 => AddNodeStatus::Ready,
0x02 => AddNodeStatus::NodeFound,
0x03 => AddNodeStatus::AddingEndNode {
node: node()?,
info: info()?,
},
0x04 => AddNodeStatus::AddingController {
node: node()?,
info: info()?,
},
0x05 => AddNodeStatus::ProtocolDone,
0x06 => AddNodeStatus::Done {
node: node().unwrap_or(0),
},
0x07 => AddNodeStatus::Failed,
o => AddNodeStatus::Other(o),
})
}
pub fn to_frame(&self, callback_id: u8, node_id_len: usize) -> DataFrame {
let with_info = |status: u8, node: NodeId, info: Option<&NodeInfo>| {
let body = info.map(NodeInfo::encode).unwrap_or_default();
let mut v = vec![callback_id, status];
node_bytes(node, node_id_len, &mut v);
v.push(body.len() as u8);
v.extend_from_slice(&body);
v
};
let p = match self {
AddNodeStatus::Ready => with_info(0x01, 0, None),
AddNodeStatus::NodeFound => with_info(0x02, 0, None),
AddNodeStatus::AddingEndNode { node, info } => with_info(0x03, *node, Some(info)),
AddNodeStatus::AddingController { node, info } => with_info(0x04, *node, Some(info)),
AddNodeStatus::ProtocolDone => with_info(0x05, 0, None),
AddNodeStatus::Done { node } => with_info(0x06, *node, None),
AddNodeStatus::Failed => with_info(0x07, 0, None),
AddNodeStatus::Other(o) => with_info(*o, 0, None),
};
DataFrame::request(F::ADD_NODE_TO_NETWORK, p)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RemoveNodeStatus {
Ready,
NodeFound,
RemovingEndNode {
node: NodeId,
},
RemovingController {
node: NodeId,
},
Done {
node: NodeId,
},
Failed,
Other(u8),
}
impl RemoveNodeStatus {
pub fn parse(p: &[u8], node_id_len: usize) -> Result<Self> {
need(p, 2, "remove node status")?;
let node = || node_id(p, 2, node_id_len, "remove node status").unwrap_or(0);
Ok(match p[1] {
0x01 => RemoveNodeStatus::Ready,
0x02 => RemoveNodeStatus::NodeFound,
0x03 => RemoveNodeStatus::RemovingEndNode { node: node() },
0x04 => RemoveNodeStatus::RemovingController { node: node() },
0x06 => RemoveNodeStatus::Done { node: node() },
0x07 => RemoveNodeStatus::Failed,
o => RemoveNodeStatus::Other(o),
})
}
pub fn to_frame(&self, callback_id: u8, node_id_len: usize) -> DataFrame {
let (s, n) = match *self {
RemoveNodeStatus::Ready => (0x01, 0),
RemoveNodeStatus::NodeFound => (0x02, 0),
RemoveNodeStatus::RemovingEndNode { node } => (0x03, node),
RemoveNodeStatus::RemovingController { node } => (0x04, node),
RemoveNodeStatus::Done { node } => (0x06, node),
RemoveNodeStatus::Failed => (0x07, 0),
RemoveNodeStatus::Other(o) => (o, 0),
};
let mut v = vec![callback_id, s];
node_bytes(n, node_id_len, &mut v);
v.push(0);
DataFrame::request(F::REMOVE_NODE_FROM_NETWORK, v)
}
}
pub mod setup {
pub const GET_SUPPORTED: u8 = 0x01;
pub const SET_TX_STATUS_REPORT: u8 = 0x02;
pub const GET_RF_REGION: u8 = 0x20;
pub const SET_RF_REGION: u8 = 0x40;
pub const SET_NODE_ID_TYPE: u8 = 0x80;
pub const NODE_ID_SHORT: u8 = 0x01;
pub const NODE_ID_LONG: u8 = 0x02;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RfRegion {
Europe,
Usa,
AustraliaNewZealand,
HongKong,
India,
Israel,
Russia,
China,
UsaLongRange,
EuropeLongRange,
Japan,
Korea,
Other(u8),
}
impl RfRegion {
pub fn from_u8(v: u8) -> Self {
match v {
0x00 => RfRegion::Europe,
0x01 => RfRegion::Usa,
0x02 => RfRegion::AustraliaNewZealand,
0x03 => RfRegion::HongKong,
0x05 => RfRegion::India,
0x06 => RfRegion::Israel,
0x07 => RfRegion::Russia,
0x08 => RfRegion::China,
0x09 => RfRegion::UsaLongRange,
0x0b => RfRegion::EuropeLongRange,
0x20 => RfRegion::Japan,
0x21 => RfRegion::Korea,
o => RfRegion::Other(o),
}
}
pub fn to_u8(self) -> u8 {
match self {
RfRegion::Europe => 0x00,
RfRegion::Usa => 0x01,
RfRegion::AustraliaNewZealand => 0x02,
RfRegion::HongKong => 0x03,
RfRegion::India => 0x05,
RfRegion::Israel => 0x06,
RfRegion::Russia => 0x07,
RfRegion::China => 0x08,
RfRegion::UsaLongRange => 0x09,
RfRegion::EuropeLongRange => 0x0b,
RfRegion::Japan => 0x20,
RfRegion::Korea => 0x21,
RfRegion::Other(o) => o,
}
}
}
pub fn parse_routing_info(p: &[u8]) -> Result<Vec<NodeId>> {
need(p, 29, "routing info")?;
Ok(parse_node_mask(&p[..29], 1))
}
pub fn get_routing_info(node: NodeId, node_id_len: usize) -> DataFrame {
let mut v = Vec::with_capacity(5);
node_bytes(node, node_id_len, &mut v);
v.extend_from_slice(&[0, 0, 0]);
DataFrame::request(F::GET_ROUTING_INFO, v)
}
pub const LONG_RANGE_NODES_PER_SEGMENT: u16 = 1024;
pub fn parse_long_range_nodes(p: &[u8]) -> Result<(bool, u8, Vec<NodeId>)> {
need(p, 3, "long range nodes")?;
let segment = p[1];
let len = usize::from(p[2]).min(p.len() - 3).min(128);
let first = 256 + LONG_RANGE_NODES_PER_SEGMENT * NodeId::from(segment);
Ok((p[0] != 0, segment, parse_node_mask(&p[3..3 + len], first)))
}
pub fn encode_long_range_nodes(more: bool, segment: u8, nodes: &[NodeId]) -> Vec<u8> {
let first = 256 + LONG_RANGE_NODES_PER_SEGMENT * NodeId::from(segment);
let mut mask = vec![0u8; 128];
for &n in nodes {
if n >= first && n < first + LONG_RANGE_NODES_PER_SEGMENT {
let i = usize::from(n - first);
mask[i / 8] |= 1 << (i % 8);
}
}
let mut v = vec![u8::from(more), segment, 128];
v.extend_from_slice(&mask);
v
}
pub mod nvm {
pub const OPEN: u8 = 0x00;
pub const READ: u8 = 0x01;
pub const WRITE: u8 = 0x02;
pub const CLOSE: u8 = 0x03;
pub const STATUS_OK: u8 = 0x00;
pub const STATUS_EOF: u8 = 0xff;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NvmResponse {
pub status: u8,
pub offset_or_size: u32,
pub data: Vec<u8>,
}
impl NvmResponse {
pub fn parse(p: &[u8], extended: bool) -> Result<Self> {
let off_len = if extended { 4 } else { 2 };
need(p, 2, "NVM response")?;
let status = p[0];
let len = usize::from(p[1]);
let offset_or_size = if p.len() >= 2 + off_len {
if extended {
u32::from_be_bytes([p[2], p[3], p[4], p[5]])
} else {
u32::from(u16::from_be_bytes([p[2], p[3]]))
}
} else {
0
};
let start = (2 + off_len).min(p.len());
let data = p[start..p.len().min(start + len)].to_vec();
Ok(NvmResponse {
status,
offset_or_size,
data,
})
}
pub fn encode(&self, extended: bool) -> Vec<u8> {
let mut v = vec![self.status, self.data.len() as u8];
if extended {
v.extend_from_slice(&self.offset_or_size.to_be_bytes());
} else {
v.extend_from_slice(&(self.offset_or_size as u16).to_be_bytes());
}
v.extend_from_slice(&self.data);
v
}
}
pub fn nvm_request(extended: bool, op: u8, length: u8, offset: u32) -> DataFrame {
let f = if extended {
F::EXTENDED_NVM_OPERATIONS
} else {
F::NVM_OPERATIONS
};
let mut v = vec![op];
if op == nvm::READ {
v.push(length);
if extended {
v.extend_from_slice(&offset.to_be_bytes());
} else {
v.extend_from_slice(&(offset as u16).to_be_bytes());
}
}
DataFrame::request(f, v)
}
pub fn nvm_write(extended: bool, offset: u32, data: &[u8]) -> DataFrame {
let f = if extended {
F::EXTENDED_NVM_OPERATIONS
} else {
F::NVM_OPERATIONS
};
let mut v = vec![nvm::WRITE, data.len() as u8];
if extended {
v.extend_from_slice(&offset.to_be_bytes());
} else {
v.extend_from_slice(&(offset as u16).to_be_bytes());
}
v.extend_from_slice(data);
DataFrame::request(f, v)
}
pub fn is_response(f: &DataFrame, function: crate::FunctionId) -> bool {
f.kind == FrameType::Response && f.function == function.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capabilities_mask() {
let c = SerialApiCapabilities {
firmware_major: 7,
firmware_minor: 21,
manufacturer_id: 0,
product_type: 4,
product_id: 1,
functions: SerialApiCapabilities::mask(&[
F::SEND_DATA,
F::GET_ROUTING_INFO,
F::NVM_OPERATIONS,
]),
};
let back = SerialApiCapabilities::parse(&c.encode()).unwrap();
assert!(back.supports(F::SEND_DATA));
assert!(back.supports(F::GET_ROUTING_INFO));
assert!(!back.supports(F::EXTENDED_NVM_OPERATIONS));
assert_eq!(back, c);
}
#[test]
fn init_data_and_masks() {
let d = InitData {
api_version: 9,
capabilities: 0x08,
nodes: vec![1, 2, 5, 232],
chip_type: Some(8),
chip_version: Some(0),
};
let back = InitData::parse(&d.encode()).unwrap();
assert_eq!(back, d);
assert!(back.is_sis());
assert!(InitData::parse(&[1, 2]).is_err());
assert!(InitData::parse(&[1, 2, 200, 0xff]).unwrap().nodes.len() == 8);
}
#[test]
fn cc_list_with_mark_and_extended() {
let (s, c) = parse_cc_list(&[0x5e, 0x25, 0xf1, 0x00, 0xef, 0x20, 0xf2]);
assert_eq!(s, vec![0x5e, 0x25, 0xf100]);
assert_eq!(c, vec![0x20]);
}
#[test]
fn application_command_variants() {
let a = ApplicationCommand {
rx_status: RxStatus(0),
source: 4,
destination: None,
command: vec![0x31, 0x05, 0x01, 0x22, 0x00, 0xe1],
rssi: Some(-60),
};
let f = a.to_frame(1);
assert_eq!(ApplicationCommand::parse(&f.payload, 1).unwrap(), a);
assert!(ApplicationCommand::parse(&[0, 4, 9, 0x20], 1).is_err());
let b = ApplicationCommand::parse_bridge(&[0, 1, 4, 2, 0x20, 0x02, 0, 0xc4], 1).unwrap();
assert_eq!(b.source, 4);
assert_eq!(b.destination, Some(1));
assert_eq!(b.command, vec![0x20, 0x02]);
assert_eq!(b.rssi, Some(-60));
}
#[test]
fn tx_report_roundtrip() {
let r = TxReport {
ticks: 3,
repeaters: vec![7, 9],
ack_rssi: Some(-71),
repeater_rssi: vec![-50, -60],
ack_channel: Some(1),
tx_channel: 1,
route_scheme: 3,
speed: 3,
attempts: 1,
failed_from: None,
failed_to: None,
};
let mut p = vec![0x01, 0x00];
p.extend_from_slice(&r.encode());
let res = SendDataResult::parse(&p).unwrap();
assert_eq!(res.status, TransmitStatus::Ok);
assert_eq!(res.report.unwrap(), r);
assert_eq!(SendDataResult::parse(&[1, 1]).unwrap().report, None);
}
#[test]
fn add_node_status() {
let info = NodeInfo {
basic: 4,
generic: 0x21,
specific: 1,
supported: vec![0x5e, 0x9f],
controlled: vec![],
};
let s = AddNodeStatus::AddingEndNode { node: 7, info };
assert_eq!(
AddNodeStatus::parse(&s.to_frame(3, 1).payload, 1).unwrap(),
s
);
assert_eq!(
AddNodeStatus::parse(&[3, 6, 7, 0], 1).unwrap(),
AddNodeStatus::Done { node: 7 }
);
}
#[test]
fn application_updates() {
let info = NodeInfo {
basic: 4,
generic: 0x07,
specific: 1,
supported: vec![0x5e, 0x71],
controlled: vec![],
};
let u = ApplicationUpdate::NodeInfoReceived {
node: 3,
info: info.clone(),
};
assert_eq!(
ApplicationUpdate::parse(&u.to_frame(1).payload, 1).unwrap(),
u
);
let s = ApplicationUpdate::SmartStartHomeIdReceived {
nwi_home_id: [0xc1, 2, 3, 4],
long_range: false,
info,
};
assert_eq!(
ApplicationUpdate::parse(&s.to_frame(1).payload, 1).unwrap(),
s
);
}
#[test]
fn protocol_info() {
let i = NodeProtocolInfo {
listening: false,
routing: true,
flirs_1000: false,
flirs_250: false,
beaming: true,
controller: false,
security: true,
protocol_version: 3,
speeds: 0b111,
basic: 4,
generic: 0x21,
specific: 1,
};
assert_eq!(NodeProtocolInfo::parse(&i.encode()).unwrap(), Some(i));
assert_eq!(NodeProtocolInfo::parse(&[0; 6]).unwrap(), None);
}
#[test]
fn nvm_responses() {
let r = NvmResponse {
status: 0,
offset_or_size: 0x1000,
data: vec![],
};
assert_eq!(NvmResponse::parse(&r.encode(false), false).unwrap(), r);
let r = NvmResponse {
status: 0xff,
offset_or_size: 0x20,
data: vec![1, 2, 3],
};
assert_eq!(NvmResponse::parse(&r.encode(true), true).unwrap(), r);
}
}