Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 87 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion crates/ziggurat-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ embedded-io-adapters = { version = "0.7", features = ["tokio-1"] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "chrono"] }
tokio = { version = "1.53.1", features = ["rt-multi-thread", "macros", "time", "sync", "net", "io-util", "io-std"] }
tokio-serial = "5.5"
tokio-tungstenite = { version = "0.30", default-features = false, features = ["handshake"] }
tokio-tungstenite = { version = "0.30", default-features = false, features = ["handshake", "connect"] } # SPIKE: connect for zbr_router
# SPIKE: Zigbee Border Router
nix = { version = "0.31", features = ["socket", "uio", "net"] }
socket2 = "0.6"
sha2 = "0.10"

[dev-dependencies]
tokio = { version = "1.53.1", features = ["test-util"] }
Expand Down
70 changes: 62 additions & 8 deletions crates/ziggurat-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod reconnect;
mod zbr;
mod zbr_router;

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -89,12 +91,14 @@ pub struct ZigguratServer {
started: AtomicBool,
notification_tx: broadcast::Sender<ZigbeeNotification>,
notification_forwarder: Mutex<Option<JoinHandle<()>>>,
zbr: Option<zbr::Config>,
zbr_task: Mutex<Option<JoinHandle<()>>>,
}

impl ZigguratServer {
/// The RCP transport is not opened and the Zigbee stack is not created until a
/// client sends a command that needs them.
pub fn new(serial: SerialConfig) -> Self {
pub fn new(serial: SerialConfig, zbr: Option<zbr::Config>) -> Self {
let (notification_tx, _) = broadcast::channel(NOTIFICATION_HUB_DEPTH);

Self {
Expand All @@ -104,6 +108,8 @@ impl ZigguratServer {
started: AtomicBool::new(false),
notification_tx,
notification_forwarder: Mutex::new(None),
zbr,
zbr_task: Mutex::new(None),
}
}

Expand Down Expand Up @@ -753,6 +759,17 @@ impl ZigguratServer {
});
*self.notification_forwarder.lock().unwrap() = Some(forwarder);

if let Some(config) = self.zbr.clone() {
let task = tokio::spawn(zbr::run(
stack.clone(),
self.notification_tx.subscribe(),
config,
));
if let Some(old) = self.zbr_task.lock().unwrap().replace(task) {
old.abort();
}
}

self.started.store(true, Ordering::SeqCst);
tracing::info!("Zigbee stack initialized and running.");

Expand All @@ -761,6 +778,9 @@ impl ZigguratServer {

/// Stop and drop the running stack and its notification forwarder, if any.
async fn teardown_stack(&self) {
if let Some(task) = self.zbr_task.lock().unwrap().take() {
task.abort();
}
let old_stack = self.stack.lock().unwrap().take();
if let Some(old_stack) = old_stack {
tracing::info!("Stopping the running Zigbee stack");
Expand Down Expand Up @@ -1109,6 +1129,23 @@ struct Args {
/// Log level (RUST_LOG still overrides, with per-module filters)
#[arg(long, default_value_t = LevelFilter::DEBUG)]
log_level: LevelFilter,

/// SPIKE: serve the Zigbee Border Router (raw ZCL over UDP/IPv6) on this port
#[arg(long)]
zbr_port: Option<u16>,

/// SPIKE: what IP clients may send through the border router
#[arg(long, value_enum, default_value_t = zbr::Policy::ReadOnly)]
zbr_policy: zbr::Policy,

/// SPIKE: interface the border router's multicast copies leave by
#[arg(long)]
zbr_multicast_interface: Option<String>,

/// SPIKE: run as a router of this coordinator's network (its Ziggurat WebSocket
/// URL), configured from its settings instead of joining over the air
#[arg(long)]
zbr_router_of: Option<String>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -1137,13 +1174,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.init();
}

let server = Arc::new(ZigguratServer::new(SerialConfig {
device: args.device,
baudrate: args.baudrate,
flow_control: args.flow_control,
reconnect_timeout: Duration::from_secs(args.reconnect_timeout),
reconnect_on_unresponsive: args.reconnect_on_unresponsive,
}));
let server = Arc::new(ZigguratServer::new(
SerialConfig {
device: args.device,
baudrate: args.baudrate,
flow_control: args.flow_control,
reconnect_timeout: Duration::from_secs(args.reconnect_timeout),
reconnect_on_unresponsive: args.reconnect_on_unresponsive,
},
args.zbr_port.map(|port| zbr::Config {
port,
policy: args.zbr_policy,
multicast_interface: args.zbr_multicast_interface,
}),
));

if let Some(coordinator) = args.zbr_router_of {
let server = server.clone();
tokio::spawn(async move {
if let Err(e) = zbr_router::bootstrap(&server, &coordinator).await {
tracing::error!("ZBR router bootstrap failed: {e}");
std::process::exit(1);
}
});
}

match args.api {
ApiMode::Ws => server.run(&args.listen).await?,
Expand Down
Loading
Loading