mirror of
https://github.com/fossasia/badgemagic-rs
synced 2025-06-24 07:43:58 +00:00
Address additional PR review comments for code simplification
This commit is contained in:
parent
ba41bf3a65
commit
19db575be2
1 changed files with 27 additions and 37 deletions
64
src/ble.rs
64
src/ble.rs
|
@ -4,7 +4,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use btleplug::{
|
use btleplug::{
|
||||||
api::{Central as _, Manager as _, Peripheral as _, ScanFilter, WriteType},
|
api::{bleuuid, Central as _, Manager as _, Peripheral as _, ScanFilter, WriteType},
|
||||||
platform::{Manager, Peripheral},
|
platform::{Manager, Peripheral},
|
||||||
};
|
};
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
|
@ -13,9 +13,9 @@ use uuid::Uuid;
|
||||||
use crate::protocol::PayloadBuffer;
|
use crate::protocol::PayloadBuffer;
|
||||||
|
|
||||||
/// `0000fee0-0000-1000-8000-00805f9b34fb`
|
/// `0000fee0-0000-1000-8000-00805f9b34fb`
|
||||||
const BADGE_SERVICE_UUID: Uuid = btleplug::api::bleuuid::uuid_from_u16(0xfee0);
|
const BADGE_SERVICE_UUID: Uuid = bleuuid::uuid_from_u16(0xfee0);
|
||||||
/// `0000fee1-0000-1000-8000-00805f9b34fb`
|
/// `0000fee1-0000-1000-8000-00805f9b34fb`
|
||||||
const BADGE_CHAR_UUID: Uuid = btleplug::api::bleuuid::uuid_from_u16(0xfee1);
|
const BADGE_CHAR_UUID: Uuid = bleuuid::uuid_from_u16(0xfee1);
|
||||||
|
|
||||||
const BADGE_BLE_DEVICE_NAME: &str = "LSLED";
|
const BADGE_BLE_DEVICE_NAME: &str = "LSLED";
|
||||||
const BLE_CHAR_CHUNK_SIZE: usize = 16;
|
const BLE_CHAR_CHUNK_SIZE: usize = 16;
|
||||||
|
@ -75,26 +75,19 @@ impl Device {
|
||||||
// and also the correct name.
|
// and also the correct name.
|
||||||
// The service uuid is also by devices that are not LED badges, so
|
// The service uuid is also by devices that are not LED badges, so
|
||||||
// the name check is also necessary.
|
// the name check is also necessary.
|
||||||
let props = peripheral.properties().await;
|
let props = peripheral.properties().await.ok()??;
|
||||||
if props.is_err() {
|
|
||||||
|
let local_name = props.local_name.as_ref()?;
|
||||||
|
if local_name != BADGE_BLE_DEVICE_NAME {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(props) = props.unwrap() {
|
if props
|
||||||
let local_name = props.local_name.as_ref()?;
|
.services
|
||||||
if local_name != BADGE_BLE_DEVICE_NAME {
|
.iter()
|
||||||
return None;
|
.any(|uuid| *uuid == BADGE_SERVICE_UUID)
|
||||||
}
|
{
|
||||||
|
Some(Self { peripheral })
|
||||||
if props
|
|
||||||
.services
|
|
||||||
.iter()
|
|
||||||
.any(|uuid| *uuid == BADGE_SERVICE_UUID)
|
|
||||||
{
|
|
||||||
Some(Self { peripheral })
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -128,14 +121,15 @@ impl Device {
|
||||||
.context("bluetooth device connect")?;
|
.context("bluetooth device connect")?;
|
||||||
|
|
||||||
let result = self.write_connected(payload).await;
|
let result = self.write_connected(payload).await;
|
||||||
if result.is_ok() {
|
let disconnect_result = self.peripheral.disconnect().await;
|
||||||
self.peripheral
|
|
||||||
.disconnect()
|
|
||||||
.await
|
|
||||||
.context("bluetooth device disconnect")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
if result.is_ok() {
|
||||||
|
// Write succesful, return disconnect result
|
||||||
|
Ok(disconnect_result?)
|
||||||
|
} else {
|
||||||
|
// Write failed, return write result and ignore disconnect result
|
||||||
|
result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn write_connected(&self, payload: PayloadBuffer) -> Result<()> {
|
async fn write_connected(&self, payload: PayloadBuffer) -> Result<()> {
|
||||||
|
@ -145,12 +139,10 @@ impl Device {
|
||||||
.await
|
.await
|
||||||
.context("discovering services")?;
|
.context("discovering services")?;
|
||||||
let characteristics = self.peripheral.characteristics();
|
let characteristics = self.peripheral.characteristics();
|
||||||
let badge_char = characteristics.iter().find(|c| c.uuid == BADGE_CHAR_UUID);
|
let badge_char = characteristics
|
||||||
|
.iter()
|
||||||
if badge_char.is_none() {
|
.find(|c| c.uuid == BADGE_CHAR_UUID)
|
||||||
return Err(anyhow::anyhow!("Badge characteristic not found"));
|
.context("badge characteristic not found")?;
|
||||||
}
|
|
||||||
let badge_char = badge_char.unwrap();
|
|
||||||
|
|
||||||
// Write payload
|
// Write payload
|
||||||
let bytes = payload.into_padded_bytes();
|
let bytes = payload.into_padded_bytes();
|
||||||
|
@ -158,10 +150,8 @@ impl Device {
|
||||||
|
|
||||||
anyhow::ensure!(
|
anyhow::ensure!(
|
||||||
data.len() % BLE_CHAR_CHUNK_SIZE == 0,
|
data.len() % BLE_CHAR_CHUNK_SIZE == 0,
|
||||||
format!(
|
"Payload size must be a multiple of {} bytes",
|
||||||
"Payload size must be a multiple of {} bytes",
|
BLE_CHAR_CHUNK_SIZE
|
||||||
BLE_CHAR_CHUNK_SIZE
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// the device will brick itself if the payload is too long (more then 8192 bytes)
|
// the device will brick itself if the payload is too long (more then 8192 bytes)
|
||||||
|
|
Loading…
Reference in a new issue