Files
svc-core/src/orin-status/elm.rs
T

35 lines
1.1 KiB
Rust
Raw Normal View History

//! The Bluetooth link to the ELM327 (the OBD-II adapter on the K-line), as BlueZ sees
//! it. Only watched, never opened: the adapter takes one connection, and obd-poller
//! holds it while the ECU answers.
use std::process::Command;
use serde_json::{json, Value};
pub struct Elm {
/// "00:0C:BF:50:AF:3F"; None when no adapter is configured.
addr: Option<String>,
}
/// `bluetoothctl info` says "Connected: yes" while any link to the device is up,
/// the poller's serial link included. An unknown device is simply not connected.
fn connected(addr: &str) -> bool {
Command::new("bluetoothctl")
.args(["info", addr])
.output()
.map(|o| String::from_utf8_lossy(&o.stdout).lines().any(|l| l.trim() == "Connected: yes"))
.unwrap_or(false)
}
impl Elm {
pub fn new(addr: &str) -> Self {
Elm { addr: Some(addr.trim().to_string()).filter(|a| !a.is_empty()) }
}
pub fn read(&mut self, out: &mut Vec<(&'static str, Value)>) {
if let Some(a) = &self.addr {
out.push(("elm_connected", json!(connected(a))));
}
}
}