Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
903729ccbe
|
||
|
|
2b7bdb5628
|
||
|
|
3871772d9b | ||
|
|
0b9fe272d3 |
No files matched your search
@@ -1,5 +1,27 @@
|
||||
# Changelog
|
||||
|
||||
## 0.2.0 — 2026-04-07
|
||||
|
||||
### Changed
|
||||
|
||||
- Upgraded to `donglora-client` 0.2 (published crate, no more monorepo path dep).
|
||||
- Wire-level protocol constants now imported from `donglora_client::protocol::*`.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Repository URL now points to `github.com/donglora/mux-rs`.
|
||||
- README links updated (removed stale monorepo paths).
|
||||
- Added `rust-version`, `homepage`, and `exclude` to Cargo.toml.
|
||||
|
||||
## 0.1.1 — 2026-04-06
|
||||
|
||||
### Fixed
|
||||
|
||||
- Prevent multiple mux instances from running simultaneously via file-based
|
||||
exclusive lock (`fd-lock`); stale sockets from crashed instances are cleaned
|
||||
up automatically
|
||||
- Clearer error message when TCP port is already in use
|
||||
|
||||
## 0.1.0 — 2026-04-06
|
||||
|
||||
Initial release.
|
||||
|
||||
Generated
+44
-2
@@ -161,7 +161,9 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
|
||||
[[package]]
|
||||
name = "donglora-client"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1936935d31ab56a8cd70ceecfd6d447f2628a73a123e1cfe37ee4987eab0fa70"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"serialport",
|
||||
@@ -171,11 +173,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "donglora-mux"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"donglora-client",
|
||||
"fd-lock",
|
||||
"tokio",
|
||||
"tokio-serial",
|
||||
"tokio-util",
|
||||
@@ -193,6 +196,17 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fd-lock"
|
||||
version = "4.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ce92ff622d6dadf7349484f42c93271a0d49b7cc4d466a936405bacbe10aa78"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"rustix",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.32"
|
||||
@@ -335,6 +349,12 @@ dependencies = [
|
||||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.14"
|
||||
@@ -522,6 +542,19 @@ version = "0.8.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
|
||||
dependencies = [
|
||||
"bitflags 2.11.0",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
@@ -826,6 +859,15 @@ dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.59.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
|
||||
+7
-3
@@ -1,16 +1,19 @@
|
||||
[package]
|
||||
name = "donglora-mux"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.85"
|
||||
authors = ["Stephen Waits <steve@waits.net>"]
|
||||
license = "MIT"
|
||||
description = "DongLoRa USB Multiplexer — share one dongle with multiple applications"
|
||||
repository = "https://github.com/swaits/donglora"
|
||||
repository = "https://github.com/donglora/mux-rs"
|
||||
homepage = "https://github.com/donglora/mux-rs"
|
||||
keywords = ["lora", "radio", "usb", "multiplexer"]
|
||||
categories = ["hardware-support"]
|
||||
exclude = [".jj/", "deny.toml", "justfile", "mise.toml", "rustfmt.toml"]
|
||||
|
||||
[dependencies]
|
||||
donglora-client = { version = "0.1.0", path = "../../clients/rust" }
|
||||
donglora-client = "0.2"
|
||||
anyhow = "1"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
@@ -18,6 +21,7 @@ tokio-serial = "5"
|
||||
tokio-util = "0.7"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
fd-lock = "4"
|
||||
|
||||
[lints.clippy]
|
||||
unwrap_used = "deny"
|
||||
|
||||
@@ -3,6 +3,12 @@
|
||||
USB multiplexer daemon — lets multiple applications share one DongLoRa
|
||||
dongle simultaneously.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
cargo install donglora-mux
|
||||
```
|
||||
|
||||
## What It Does
|
||||
|
||||
- Owns the USB serial connection exclusively
|
||||
@@ -11,18 +17,19 @@ dongle simultaneously.
|
||||
- RxPacket frames broadcast to all connected clients
|
||||
- StartRx/StopRx reference-counted across clients
|
||||
- SetConfig locked once set (single client can change freely)
|
||||
- Single-instance enforcement via file lock (stale sockets auto-cleaned)
|
||||
- Automatic dongle reconnect on hot-plug
|
||||
- No panics — enforced by clippy deny lints
|
||||
|
||||
## Running
|
||||
|
||||
```sh
|
||||
just run # start the mux daemon
|
||||
just verbose # start with verbose logging
|
||||
just run -- --tcp 5741 --port /dev/ttyACM0 # with options
|
||||
donglora-mux # start the mux daemon
|
||||
donglora-mux --verbose # start with verbose logging
|
||||
donglora-mux --tcp 5741 --port /dev/ttyACM0 # with options
|
||||
```
|
||||
|
||||
## Depends On
|
||||
|
||||
- [clients/rust](../../clients/rust/) — the `donglora-client` library
|
||||
(protocol types, COBS framing, device discovery)
|
||||
- [`donglora-client`](https://crates.io/crates/donglora-client) — protocol
|
||||
types, COBS framing, device discovery
|
||||
@@ -0,0 +1,34 @@
|
||||
# cargo-deny configuration (v0.19+)
|
||||
# Run with: cargo deny check
|
||||
|
||||
[licenses]
|
||||
version = 2
|
||||
|
||||
# Permissive licenses only. No copyleft.
|
||||
allow = [
|
||||
"MIT",
|
||||
"MIT-0",
|
||||
"Apache-2.0",
|
||||
"BSD-1-Clause",
|
||||
"BSD-2-Clause",
|
||||
"BSD-3-Clause",
|
||||
"BSL-1.0",
|
||||
"ISC",
|
||||
"Zlib",
|
||||
"Unlicense",
|
||||
"CC0-1.0",
|
||||
"CDLA-Permissive-2.0",
|
||||
"Unicode-3.0",
|
||||
"MPL-2.0",
|
||||
]
|
||||
|
||||
[advisories]
|
||||
|
||||
[bans]
|
||||
multiple-versions = "warn"
|
||||
wildcards = "warn"
|
||||
|
||||
[sources]
|
||||
unknown-registry = "deny"
|
||||
unknown-git = "warn"
|
||||
allow-git = []
|
||||
@@ -2,18 +2,49 @@ set shell := ["bash", "-c"]
|
||||
|
||||
default: run
|
||||
|
||||
[private]
|
||||
_ensure_tools:
|
||||
@mise trust --yes . 2>/dev/null; mise install --quiet
|
||||
|
||||
# Run the mux daemon
|
||||
run *args:
|
||||
run *args: _ensure_tools
|
||||
cargo run -- {{args}}
|
||||
|
||||
# Run with verbose logging
|
||||
verbose *args:
|
||||
verbose *args: _ensure_tools
|
||||
cargo run -- --verbose {{args}}
|
||||
|
||||
# Run clippy and tests
|
||||
check:
|
||||
cargo clippy -- -D warnings && cargo test
|
||||
# Run all checks (fmt, clippy, test, audit)
|
||||
check: fmt-check clippy test audit
|
||||
|
||||
# Format code
|
||||
fmt: _ensure_tools
|
||||
cargo fmt
|
||||
|
||||
# Check formatting without changing files
|
||||
fmt-check: _ensure_tools
|
||||
cargo fmt --check
|
||||
|
||||
# Lint with clippy
|
||||
clippy: _ensure_tools
|
||||
cargo clippy --all-targets -- -D warnings
|
||||
|
||||
# Run tests
|
||||
test: _ensure_tools
|
||||
cargo test
|
||||
|
||||
# Security + license audit
|
||||
audit: _ensure_tools
|
||||
cargo deny check
|
||||
|
||||
# Build release
|
||||
build:
|
||||
build: _ensure_tools
|
||||
cargo build --release
|
||||
|
||||
# Check for outdated dependencies
|
||||
outdated: _ensure_tools
|
||||
cargo outdated
|
||||
|
||||
# Clean build artifacts
|
||||
clean:
|
||||
cargo clean
|
||||
@@ -0,0 +1,3 @@
|
||||
edition = "2024"
|
||||
max_width = 120
|
||||
use_small_heuristics = "Max"
|
||||
+43
-61
@@ -9,13 +9,13 @@ use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use donglora_client::{
|
||||
encode_frame, FrameReader, CMD_TAG_SET_CONFIG, CMD_TAG_START_RX, CMD_TAG_STOP_RX,
|
||||
RADIO_CONFIG_SIZE, RESP_TAG_OK, RESP_TAG_RX_PACKET,
|
||||
use donglora_client::protocol::{
|
||||
CMD_TAG_SET_CONFIG, CMD_TAG_START_RX, CMD_TAG_STOP_RX, RADIO_CONFIG_SIZE, RESP_TAG_OK, RESP_TAG_RX_PACKET,
|
||||
};
|
||||
use donglora_client::{FrameReader, encode_frame};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpListener, UnixListener};
|
||||
use tokio::sync::{mpsc, oneshot, Mutex};
|
||||
use tokio::sync::{Mutex, mpsc, oneshot};
|
||||
|
||||
/// A queued command: (client_id, raw_cmd_bytes).
|
||||
type QueuedCmd = (u64, Vec<u8>);
|
||||
@@ -63,35 +63,44 @@ impl MuxDaemon {
|
||||
tcp_addr: Option<(String, u16)>,
|
||||
shutdown: CancellationToken,
|
||||
) -> Self {
|
||||
Self {
|
||||
serial_port,
|
||||
socket_path,
|
||||
tcp_addr,
|
||||
shutdown,
|
||||
}
|
||||
Self { serial_port, socket_path, tcp_addr, shutdown }
|
||||
}
|
||||
|
||||
/// Run the mux daemon. Returns when shutdown is signalled.
|
||||
pub async fn run(&self) -> anyhow::Result<()> {
|
||||
let state: SharedState = Arc::new(Mutex::new(MuxInner {
|
||||
sessions: HashMap::new(),
|
||||
mux_state: MuxState::new(),
|
||||
}));
|
||||
let state: SharedState =
|
||||
Arc::new(Mutex::new(MuxInner { sessions: HashMap::new(), mux_state: MuxState::new() }));
|
||||
|
||||
// Command channel: client handlers → dongle writer
|
||||
let (cmd_tx, cmd_rx) = mpsc::channel::<(u64, Vec<u8>)>(64);
|
||||
let cmd_rx = Arc::new(Mutex::new(cmd_rx));
|
||||
|
||||
// Pending response slot: dongle writer installs, dongle reader resolves
|
||||
let pending_response: Arc<Mutex<Option<oneshot::Sender<Vec<u8>>>>> =
|
||||
Arc::new(Mutex::new(None));
|
||||
let pending_response: Arc<Mutex<Option<oneshot::Sender<Vec<u8>>>>> = Arc::new(Mutex::new(None));
|
||||
|
||||
// Prepare socket directory
|
||||
let sock_path = Path::new(&self.socket_path);
|
||||
if let Some(parent) = sock_path.parent() {
|
||||
tokio::fs::create_dir_all(parent).await.ok();
|
||||
}
|
||||
// Acquire an exclusive lock to prevent multiple mux instances.
|
||||
// The lock is held for the process lifetime and auto-released on exit (even SIGKILL).
|
||||
let lock_path = format!("{}.lock", self.socket_path);
|
||||
let lock_file = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(false)
|
||||
.open(&lock_path)
|
||||
.map_err(|e| anyhow::anyhow!("failed to open lock file {lock_path}: {e}"))?;
|
||||
let mut lock = fd_lock::RwLock::new(lock_file);
|
||||
let _lock_guard = match lock.try_write() {
|
||||
Ok(guard) => guard,
|
||||
Err(_) => anyhow::bail!("another donglora-mux is already running (lock held on {lock_path})"),
|
||||
};
|
||||
|
||||
if sock_path.exists() {
|
||||
// Lock acquired but socket file exists — stale from a crashed instance.
|
||||
info!("removing stale socket {}", self.socket_path);
|
||||
tokio::fs::remove_file(sock_path).await.ok();
|
||||
}
|
||||
|
||||
@@ -103,9 +112,13 @@ impl MuxDaemon {
|
||||
// Start TCP server (optional)
|
||||
let tcp_listener = if let Some((ref host, port)) = self.tcp_addr {
|
||||
let addr = format!("{host}:{port}");
|
||||
let listener = TcpListener::bind(&addr)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to bind TCP {addr}: {e}"))?;
|
||||
let listener = TcpListener::bind(&addr).await.map_err(|e| {
|
||||
if e.kind() == std::io::ErrorKind::AddrInUse {
|
||||
anyhow::anyhow!("another donglora-mux is already listening on TCP {addr}")
|
||||
} else {
|
||||
anyhow::anyhow!("failed to bind TCP {addr}: {e}")
|
||||
}
|
||||
})?;
|
||||
info!("TCP listening on {addr}");
|
||||
Some(listener)
|
||||
} else {
|
||||
@@ -113,24 +126,13 @@ impl MuxDaemon {
|
||||
};
|
||||
|
||||
// Spawn client accept tasks
|
||||
tokio::spawn(accept_unix_clients(
|
||||
unix_listener,
|
||||
state.clone(),
|
||||
cmd_tx.clone(),
|
||||
self.shutdown.clone(),
|
||||
));
|
||||
tokio::spawn(accept_unix_clients(unix_listener, state.clone(), cmd_tx.clone(), self.shutdown.clone()));
|
||||
if let Some(listener) = tcp_listener {
|
||||
tokio::spawn(accept_tcp_clients(
|
||||
listener,
|
||||
state.clone(),
|
||||
cmd_tx.clone(),
|
||||
self.shutdown.clone(),
|
||||
));
|
||||
tokio::spawn(accept_tcp_clients(listener, state.clone(), cmd_tx.clone(), self.shutdown.clone()));
|
||||
}
|
||||
|
||||
// Connect/reconnect loop
|
||||
self.reconnect_loop(state, cmd_tx, cmd_rx, pending_response)
|
||||
.await;
|
||||
self.reconnect_loop(state, cmd_tx, cmd_rx, pending_response).await;
|
||||
|
||||
// Cleanup
|
||||
info!("shutting down...");
|
||||
@@ -224,8 +226,8 @@ impl MuxDaemon {
|
||||
|
||||
async fn open_and_ping(port: &str) -> anyhow::Result<tokio_serial::SerialStream> {
|
||||
let builder = tokio_serial::new(port, 115_200).timeout(Duration::from_secs(2));
|
||||
let mut serial = tokio_serial::SerialStream::open(&builder)
|
||||
.map_err(|e| anyhow::anyhow!("failed to open {port}: {e}"))?;
|
||||
let mut serial =
|
||||
tokio_serial::SerialStream::open(&builder).map_err(|e| anyhow::anyhow!("failed to open {port}: {e}"))?;
|
||||
|
||||
let ping_frame = encode_frame(&[0]);
|
||||
AsyncWriteExt::write_all(&mut serial, &ping_frame)
|
||||
@@ -339,9 +341,7 @@ async fn dispatch_frame(
|
||||
}
|
||||
}
|
||||
|
||||
async fn resolve_pending_with_error(
|
||||
pending_response: &Arc<Mutex<Option<oneshot::Sender<Vec<u8>>>>>,
|
||||
) {
|
||||
async fn resolve_pending_with_error(pending_response: &Arc<Mutex<Option<oneshot::Sender<Vec<u8>>>>>) {
|
||||
let mut slot = pending_response.lock().await;
|
||||
if let Some(sender) = slot.take() {
|
||||
let _ = sender.send(vec![5, 1]); // Error(RadioBusy)
|
||||
@@ -376,12 +376,7 @@ async fn dongle_writer(
|
||||
let intercepted = {
|
||||
let mut inner = state.lock().await;
|
||||
let locked = inner.mux_state.locked_config;
|
||||
intercept::maybe_intercept(
|
||||
&raw_cmd,
|
||||
client_id,
|
||||
&mut inner.sessions,
|
||||
&locked,
|
||||
)
|
||||
intercept::maybe_intercept(&raw_cmd, client_id, &mut inner.sessions, &locked)
|
||||
};
|
||||
if let Some(synthetic) = intercepted {
|
||||
let inner = state.lock().await;
|
||||
@@ -586,27 +581,16 @@ async fn client_writer<W: AsyncWriteExt + Unpin>(
|
||||
() = shutdown.cancelled() => return,
|
||||
};
|
||||
|
||||
if AsyncWriteExt::write_all(&mut write_half, &frame)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
if AsyncWriteExt::write_all(&mut write_half, &frame).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn remove_client(
|
||||
client_id: u64,
|
||||
state: &SharedState,
|
||||
label: &str,
|
||||
cmd_tx: &mpsc::Sender<(u64, Vec<u8>)>,
|
||||
) {
|
||||
async fn remove_client(client_id: u64, state: &SharedState, label: &str, cmd_tx: &mpsc::Sender<(u64, Vec<u8>)>) {
|
||||
let mut inner = state.lock().await;
|
||||
|
||||
let was_interested = inner
|
||||
.sessions
|
||||
.get(&client_id)
|
||||
.is_some_and(|s| s.rx_interested);
|
||||
let was_interested = inner.sessions.get(&client_id).is_some_and(|s| s.rx_interested);
|
||||
|
||||
inner.sessions.remove(&client_id);
|
||||
info!("{label} disconnected ({} remain)", inner.sessions.len());
|
||||
@@ -616,9 +600,7 @@ async fn remove_client(
|
||||
info!("last RX-interested client gone — sending StopRx");
|
||||
// Drop the lock before sending to avoid deadlock
|
||||
drop(inner);
|
||||
let _ = cmd_tx
|
||||
.send((SYNTHETIC_CLIENT_ID, vec![CMD_TAG_STOP_RX]))
|
||||
.await;
|
||||
let _ = cmd_tx.send((SYNTHETIC_CLIENT_ID, vec![CMD_TAG_STOP_RX])).await;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+12
-34
@@ -6,9 +6,9 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use donglora_client::{
|
||||
CMD_TAG_SET_CONFIG, CMD_TAG_START_RX, CMD_TAG_STOP_RX, ERROR_INVALID_CONFIG, RADIO_CONFIG_SIZE,
|
||||
RESP_TAG_ERROR, RESP_TAG_OK,
|
||||
use donglora_client::protocol::{
|
||||
CMD_TAG_SET_CONFIG, CMD_TAG_START_RX, CMD_TAG_STOP_RX, ERROR_INVALID_CONFIG, RADIO_CONFIG_SIZE, RESP_TAG_ERROR,
|
||||
RESP_TAG_OK,
|
||||
};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
@@ -22,9 +22,7 @@ pub struct MuxState {
|
||||
|
||||
impl MuxState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
locked_config: None,
|
||||
}
|
||||
Self { locked_config: None }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,27 +65,18 @@ fn intercept_set_config(
|
||||
|
||||
// Same config — reply Ok without hitting the dongle
|
||||
if config_bytes == locked.as_slice() {
|
||||
let label = sessions
|
||||
.get(&client_id)
|
||||
.map(ClientSession::label)
|
||||
.unwrap_or_else(|| format!("id-{client_id}"));
|
||||
let label = sessions.get(&client_id).map(ClientSession::label).unwrap_or_else(|| format!("id-{client_id}"));
|
||||
debug!("{label}: SetConfig matches locked config — Ok");
|
||||
return Some(vec![RESP_TAG_OK]);
|
||||
}
|
||||
|
||||
// Different config — reject
|
||||
let label = sessions
|
||||
.get(&client_id)
|
||||
.map(ClientSession::label)
|
||||
.unwrap_or_else(|| format!("id-{client_id}"));
|
||||
let label = sessions.get(&client_id).map(ClientSession::label).unwrap_or_else(|| format!("id-{client_id}"));
|
||||
warn!("{label}: SetConfig rejected (conflicts with locked config)");
|
||||
Some(vec![RESP_TAG_ERROR, ERROR_INVALID_CONFIG])
|
||||
}
|
||||
|
||||
fn intercept_start_rx(
|
||||
client_id: u64,
|
||||
sessions: &mut HashMap<u64, ClientSession>,
|
||||
) -> Option<Vec<u8>> {
|
||||
fn intercept_start_rx(client_id: u64, sessions: &mut HashMap<u64, ClientSession>) -> Option<Vec<u8>> {
|
||||
let client = sessions.get(&client_id)?;
|
||||
|
||||
// Already interested — no-op
|
||||
@@ -107,10 +96,7 @@ fn intercept_start_rx(
|
||||
None
|
||||
}
|
||||
|
||||
fn intercept_stop_rx(
|
||||
client_id: u64,
|
||||
sessions: &mut HashMap<u64, ClientSession>,
|
||||
) -> Option<Vec<u8>> {
|
||||
fn intercept_stop_rx(client_id: u64, sessions: &mut HashMap<u64, ClientSession>) -> Option<Vec<u8>> {
|
||||
let client = sessions.get(&client_id)?;
|
||||
|
||||
// Wasn't interested — no-op
|
||||
@@ -140,6 +126,7 @@ pub fn rx_interest_count(sessions: &HashMap<u64, ClientSession>) -> usize {
|
||||
// ── Tests ──────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -201,10 +188,7 @@ mod tests {
|
||||
let id = first_id(&sessions);
|
||||
let mut cmd = vec![CMD_TAG_SET_CONFIG];
|
||||
cmd.extend_from_slice(&[99, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
|
||||
assert_eq!(
|
||||
maybe_intercept(&cmd, id, &mut sessions, &locked),
|
||||
Some(vec![RESP_TAG_ERROR, ERROR_INVALID_CONFIG])
|
||||
);
|
||||
assert_eq!(maybe_intercept(&cmd, id, &mut sessions, &locked), Some(vec![RESP_TAG_ERROR, ERROR_INVALID_CONFIG]));
|
||||
}
|
||||
|
||||
// ── StartRx ────────────────────────────────────────────────────
|
||||
@@ -223,10 +207,7 @@ mod tests {
|
||||
let locked = None;
|
||||
let id = first_id(&sessions);
|
||||
sessions.get_mut(&id).unwrap().rx_interested = true;
|
||||
assert_eq!(
|
||||
maybe_intercept(&[CMD_TAG_START_RX], id, &mut sessions, &locked),
|
||||
Some(vec![RESP_TAG_OK])
|
||||
);
|
||||
assert_eq!(maybe_intercept(&[CMD_TAG_START_RX], id, &mut sessions, &locked), Some(vec![RESP_TAG_OK]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -248,10 +229,7 @@ mod tests {
|
||||
let mut sessions = make_sessions(1);
|
||||
let locked = None;
|
||||
let id = first_id(&sessions);
|
||||
assert_eq!(
|
||||
maybe_intercept(&[CMD_TAG_STOP_RX], id, &mut sessions, &locked),
|
||||
Some(vec![RESP_TAG_OK])
|
||||
);
|
||||
assert_eq!(maybe_intercept(&[CMD_TAG_STOP_RX], id, &mut sessions, &locked), Some(vec![RESP_TAG_OK]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+12
-18
@@ -50,22 +50,18 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Resolve serial port
|
||||
let port = match args.port {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
tokio::task::spawn_blocking(|| {
|
||||
donglora_client::find_port().unwrap_or_else(|| {
|
||||
info!("waiting for DongLoRa device...");
|
||||
donglora_client::wait_for_device()
|
||||
})
|
||||
None => tokio::task::spawn_blocking(|| {
|
||||
donglora_client::find_port().unwrap_or_else(|| {
|
||||
info!("waiting for DongLoRa device...");
|
||||
donglora_client::wait_for_device()
|
||||
})
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("device discovery task failed: {e}"))?
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("device discovery task failed: {e}"))?,
|
||||
};
|
||||
|
||||
// Resolve socket path
|
||||
let socket_path = args
|
||||
.socket
|
||||
.unwrap_or_else(donglora_client::default_socket_path);
|
||||
let socket_path = args.socket.unwrap_or_else(donglora_client::default_socket_path);
|
||||
|
||||
// Parse TCP address
|
||||
let tcp_addr = parse_tcp_addr(&args.tcp);
|
||||
@@ -102,12 +98,10 @@ fn parse_tcp_addr(tcp: &str) -> Option<(String, u16)> {
|
||||
}
|
||||
|
||||
async fn signal_handler(shutdown: CancellationToken) -> anyhow::Result<()> {
|
||||
let mut sigint =
|
||||
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())
|
||||
.map_err(|e| anyhow::anyhow!("failed to register SIGINT handler: {e}"))?;
|
||||
let mut sigterm =
|
||||
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||
.map_err(|e| anyhow::anyhow!("failed to register SIGTERM handler: {e}"))?;
|
||||
let mut sigint = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())
|
||||
.map_err(|e| anyhow::anyhow!("failed to register SIGINT handler: {e}"))?;
|
||||
let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||
.map_err(|e| anyhow::anyhow!("failed to register SIGTERM handler: {e}"))?;
|
||||
|
||||
tokio::select! {
|
||||
_ = sigint.recv() => info!("received SIGINT"),
|
||||
|
||||
+1
-5
@@ -30,11 +30,7 @@ impl ClientSession {
|
||||
pub fn new() -> (Self, mpsc::Receiver<Vec<u8>>) {
|
||||
let id = NEXT_CLIENT_ID.fetch_add(1, Ordering::Relaxed);
|
||||
let (tx, rx) = mpsc::channel(SEND_QUEUE_CAP);
|
||||
let session = Self {
|
||||
id,
|
||||
rx_interested: false,
|
||||
tx,
|
||||
};
|
||||
let session = Self { id, rx_interested: false, tx };
|
||||
(session, rx)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user