Skip to content

Commit b014ac7

Browse files
committed
feat: replace wpactl with nmcli
BREAKING CHANGE: This will require Raspbian Bookworm or newer.
1 parent 5147b2f commit b014ac7

File tree

11 files changed

+285
-455
lines changed

11 files changed

+285
-455
lines changed

Cargo.lock

Lines changed: 223 additions & 408 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,31 @@ readme = "README.md"
88
license-file = "LICENSE"
99

1010
[dependencies]
11-
env_logger = "0.9.0"
11+
env_logger = "0.10.0"
1212
anyhow = "1.0.57"
1313
log = "0.4.16"
1414
clap = { version = "4.1.4", features = ["derive"] }
1515
tokio = { version = "1.17.0", features = ["full"] }
16-
tokio-graceful-shutdown = "0.7.0"
16+
tokio-graceful-shutdown = "0.13.0"
1717
async-trait = "0.1.53"
18-
rppal = "0.13.1"
18+
rppal = "0.14.1"
1919
untildify = "0.1.0"
20-
toml = "0.5.9"
20+
toml = "0.8.2"
2121
serde = { version = "1.0.136", features = ["derive"] }
2222
rand = "0.8.5"
23-
mfrc522 = "0.4.0"
23+
mfrc522 = "0.6.0"
2424
serde_json = "1.0.85"
25-
wpactrl = "0.5.1"
26-
tokio-rustls = "0.23.4"
27-
rustls = { version = "0.20.8", features = ["dangerous_configuration"] }
28-
webpki-roots = "0.22.5"
25+
tokio-rustls = "0.24.1"
26+
rustls = { version = "0.21.7", features = ["dangerous_configuration"] }
27+
webpki-roots = "0.25.2"
2928
tokio-io-timeout = "1.2.0"
3029
thiserror = "1.0.37"
3130
futures-util = "0.3.24"
3231
glob = "0.3.0"
3332
hex = "0.4.3"
3433
linux-embedded-hal = "0.3.2"
3534
embedded-hal = "0.2.7"
36-
rodio = { version = "0.16.0", features = ["symphonia-mp3"] }
35+
rodio = { version = "0.17.1", features = ["symphonia-mp3"] }
3736
aw2013 = "1.0.0"
3837
rand_distr = "0.4.3"
3938
regex = "1.7.1"
@@ -44,7 +43,7 @@ local-ip-address = "0.5.6"
4443
strip = "debuginfo"
4544

4645
[package.metadata.deb]
47-
depends = "adduser, bloop-box-data, systemd"
46+
depends = "adduser, bloop-box-data, systemd, network-manager"
4847
conf-files = ["/etc/bloop-box.conf"]
4948
maintainer-scripts = "debian/"
5049
systemd-units = { enable = true }
@@ -57,6 +56,6 @@ assets = [
5756
["target/arm-unknown-linux-gnueabihf/release/bloop-box", "usr/bin/", "755"],
5857
["README.md", "usr/share/doc/bloop-box/", "644"],
5958
["etc/bloop-box.conf", "etc/", "644"],
60-
["etc/011_bloop-box-shutdown", "etc/sudoers.d/", "440"],
59+
["etc/011_bloop-box", "etc/sudoers.d/", "440"],
6160
["etc/99-systemd-spidev.rules", "lib/udev/rules.d/", "644"]
6261
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bloop-box ALL=(ALL) NOPASSWD:/usr/sbin/shutdown
2+
bloop-box ALL=(ALL) NOPASSWD:/usr/bin/nmcli

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ fn main() -> Result<()> {
127127
)
128128
.catch_signals()
129129
.handle_shutdown_requests(Duration::from_millis(1000))
130-
.await
130+
.await?;
131+
132+
Ok(())
131133
})
132134
}

src/nfc/reader.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
use anyhow::{anyhow, Result};
2-
use embedded_hal as hal;
3-
use hal::blocking::spi;
4-
use hal::digital::v2::OutputPin;
5-
use mfrc522::Mfrc522;
2+
use mfrc522::comm::Interface;
3+
use mfrc522::{Initialized, Mfrc522};
64

75
use crate::nfc::ndef::{parse_ndef_text_record, NdefMessageParser};
86

97
pub type Uid = [u8; 7];
108

11-
pub struct NfcReader<SPI, NSS> {
12-
mfrc522: Mfrc522<SPI, NSS>,
9+
pub struct NfcReader<COMM: Interface> {
10+
mfrc522: Mfrc522<COMM, Initialized>,
1311
}
1412

15-
impl<E, SPI, NSS> NfcReader<SPI, NSS>
16-
where
17-
SPI: spi::Transfer<u8, Error = E> + spi::Write<u8, Error = E>,
18-
NSS: OutputPin,
19-
{
20-
pub fn new(mfrc522: Mfrc522<SPI, NSS>) -> Self {
13+
impl<E, COMM: Interface<Error = E>> NfcReader<COMM> {
14+
pub fn new(mfrc522: Mfrc522<COMM, Initialized>) -> Self {
2115
Self { mfrc522 }
2216
}
2317

src/nfc/thread.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rppal::gpio::Gpio;
88
use std::thread;
99
use std::thread::sleep;
1010
use std::time::Duration;
11+
use mfrc522::comm::eh02::spi::SpiInterface;
1112
use tokio::sync::mpsc;
1213
use tokio::sync::oneshot;
1314
use tokio::sync::oneshot::error::TryRecvError;
@@ -47,7 +48,8 @@ pub fn start_nfc_listener(mut nfc_rx: mpsc::Receiver<NfcCommand>, config: NfcCon
4748
reset_pin.set_high();
4849
sleep(Duration::from_micros(50));
4950

50-
let mfrc522 = Mfrc522::new(spi).unwrap();
51+
let interface = SpiInterface::new(spi);
52+
let mfrc522 = Mfrc522::new(interface).init().unwrap();
5153
let mut nfc_reader = NfcReader::new(mfrc522);
5254

5355
'command: while let Some(command) = nfc_rx.blocking_recv() {

src/subsystems/controller.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::subsystems::audio_player::PlayerCommand;
1818
use crate::subsystems::config_manager::{ConfigCommand, ConnectionConfig};
1919
use crate::subsystems::led::{LedState, BLUE, CYAN, GREEN, MAGENTA, RED, YELLOW};
2020
use crate::subsystems::networker::{CheckUidResponse, NetworkerCommand, NetworkerStatus};
21-
use crate::wifi::wpa_supplicant::set_wifi;
21+
use crate::wifi::nw::set_wifi;
2222

2323
pub struct Controller {
2424
etc_config: EtcConfig,
@@ -85,10 +85,17 @@ impl Controller {
8585
if config_uids.contains(&uid) {
8686
self.led.send(LedState::On { color: YELLOW }).await?;
8787

88-
if self.process_config_command(uid, &mut config_uids, nfc.clone()).await.is_ok() {
89-
self.led.send(LedState::On { color: CYAN }).await?;
90-
} else {
91-
self.led.send(LedState::On { color: RED }).await?;
88+
match self.process_config_command(uid, &mut config_uids, nfc.clone()).await {
89+
Ok(shutdown) => {
90+
if shutdown {
91+
return Ok(());
92+
}
93+
94+
self.led.send(LedState::On { color: CYAN }).await?;
95+
},
96+
Err(_) => {
97+
self.led.send(LedState::On { color: RED }).await?;
98+
},
9299
}
93100

94101
sleep(Duration::from_millis(500)).await;
@@ -199,7 +206,7 @@ impl Controller {
199206
uid: Uid,
200207
config_uids: &mut Vec<Uid>,
201208
nfc: mpsc::Sender<NfcCommand>,
202-
) -> Result<()> {
209+
) -> Result<bool> {
203210
let (value_tx, value_rx) = oneshot::channel();
204211
nfc.send(NfcCommand::Read {
205212
responder: value_tx,
@@ -269,13 +276,15 @@ impl Controller {
269276
.args(["shutdown", "now"])
270277
.output()
271278
.expect("Failed to shut down system");
279+
280+
return Ok(true);
272281
}
273282
_ => {
274283
return Err(anyhow!("Value too short"));
275284
}
276285
}
277286

278-
Ok(())
287+
Ok(false)
279288
}
280289

281290
async fn wait_for_release(&self, nfc: &mpsc::Sender<NfcCommand>) -> Result<()> {

src/subsystems/networker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl Networker {
219219
}
220220

221221
let mut root_cert_store = rustls::RootCertStore::empty();
222-
root_cert_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(
222+
root_cert_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(
223223
|ta| {
224224
OwnedTrustAnchor::from_subject_spki_name_constraints(
225225
ta.subject,

src/wifi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub mod wpa_supplicant;
1+
pub mod nw;

src/wifi/nw.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::process::Command;
2+
use anyhow::Result;
3+
4+
pub fn set_wifi(ssid: String, password: String) -> Result<()> {
5+
Command::new("sudo")
6+
.args([
7+
&"nwcli",
8+
&"wifi",
9+
&"connect",
10+
ssid.as_str(),
11+
&"password",
12+
password.as_str(),
13+
&"ifname",
14+
&"wlan0",
15+
])
16+
.output()
17+
.expect("Failed to set WiFi credentials");
18+
19+
Ok(())
20+
}

src/wifi/wpa_supplicant.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)