This commit is contained in:
Valentin Weber 2025-07-20 10:57:19 +00:00 committed by GitHub
commit 18630263ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 10 deletions

View file

@ -73,8 +73,8 @@ impl Device {
/// Return all supported devices that are found in two seconds.
///
/// Returns all badges that are in BLE range and are in Bluetooth transfer mode.
pub async fn enumerate() -> Result<Vec<Self>> {
Self::enumerate_duration(Duration::from_secs(2)).await
pub async fn enumerate(device_name: &str) -> Result<Vec<Self>> {
Self::enumerate_duration(Duration::from_secs(2), device_name).await
}
/// Return all supported devices that are found in the given duration.
@ -82,7 +82,10 @@ impl Device {
/// Returns all badges that are in BLE range and are in Bluetooth transfer mode.
/// # Panics
/// This function panics if it is unable to access the Bluetooth adapter.
pub async fn enumerate_duration(scan_duration: Duration) -> Result<Vec<Self>> {
pub async fn enumerate_duration(
scan_duration: Duration,
device_name: &str,
) -> Result<Vec<Self>> {
// Run device scan
let manager = Manager::new().await.context("create BLE manager")?;
let adapters = manager
@ -106,7 +109,7 @@ impl Device {
.await
.context("enumerating bluetooth devices")?
{
if let Some(badge) = Self::from_peripheral(p).await {
if let Some(badge) = Self::from_peripheral(p, device_name).await {
led_badges.push(badge);
}
}
@ -114,7 +117,7 @@ impl Device {
Ok(led_badges)
}
async fn from_peripheral(peripheral: Peripheral) -> Option<Self> {
async fn from_peripheral(peripheral: Peripheral, device_name: &str) -> Option<Self> {
// The existance of the service with the correct UUID
// exists is already checked by the scan filter.
// But we also need to check the device name to make sure
@ -123,7 +126,7 @@ impl Device {
let props = peripheral.properties().await.ok()??;
let local_name = props.local_name.as_ref()?;
if local_name == BADGE_BLE_DEVICE_NAME {
if local_name == device_name {
Some(Self { peripheral })
} else {
None
@ -134,8 +137,9 @@ impl Device {
///
/// This function returns an error if no device could be found
/// or if multiple devices would match.
pub async fn single() -> Result<Self> {
let mut devices = Self::enumerate()
pub async fn single(device_name: Option<&str>) -> Result<Self> {
let device_name = device_name.unwrap_or(BADGE_BLE_DEVICE_NAME);
let mut devices = Self::enumerate(device_name)
.await
.context("enumerating badges")?
.into_iter();

View file

@ -44,6 +44,10 @@ struct Args {
#[clap(long)]
transport: TransportProtocol,
/// Device name for BLE device discovery
#[clap(long)]
device_name: Option<String>,
/// List all devices visible to a transport and exit
#[clap(long)]
list_devices: bool,
@ -105,7 +109,7 @@ fn main() -> Result<()> {
let payload = gnerate_payload(&mut args)?;
write_payload(&args.transport, payload)
write_payload(&args.transport, Option::from(&args.device_name), payload)
}
fn list_devices(transport: &TransportProtocol) -> Result<()> {
@ -232,6 +236,7 @@ fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {
fn write_payload(
transport: &TransportProtocol,
device_name: Option<&String>,
payload: PayloadBuffer,
) -> Result<(), anyhow::Error> {
match transport {
@ -239,6 +244,11 @@ fn write_payload(
TransportProtocol::Ble => tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?
.block_on(async { BleDevice::single().await?.write(payload).await }),
.block_on(async {
BleDevice::single(device_name.map(String::as_str))
.await?
.write(payload)
.await
}),
}
}