Skip to content

Commit 19db575

Browse files
hlxidmgjm
authored andcommitted
Address additional PR review comments for code simplification
1 parent ba41bf3 commit 19db575

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

src/ble.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::time::Duration;
44

55
use anyhow::{Context, Result};
66
use btleplug::{
7-
api::{Central as _, Manager as _, Peripheral as _, ScanFilter, WriteType},
7+
api::{bleuuid, Central as _, Manager as _, Peripheral as _, ScanFilter, WriteType},
88
platform::{Manager, Peripheral},
99
};
1010
use tokio::time;
@@ -13,9 +13,9 @@ use uuid::Uuid;
1313
use crate::protocol::PayloadBuffer;
1414

1515
/// `0000fee0-0000-1000-8000-00805f9b34fb`
16-
const BADGE_SERVICE_UUID: Uuid = btleplug::api::bleuuid::uuid_from_u16(0xfee0);
16+
const BADGE_SERVICE_UUID: Uuid = bleuuid::uuid_from_u16(0xfee0);
1717
/// `0000fee1-0000-1000-8000-00805f9b34fb`
18-
const BADGE_CHAR_UUID: Uuid = btleplug::api::bleuuid::uuid_from_u16(0xfee1);
18+
const BADGE_CHAR_UUID: Uuid = bleuuid::uuid_from_u16(0xfee1);
1919

2020
const BADGE_BLE_DEVICE_NAME: &str = "LSLED";
2121
const BLE_CHAR_CHUNK_SIZE: usize = 16;
@@ -75,26 +75,19 @@ impl Device {
7575
// and also the correct name.
7676
// The service uuid is also by devices that are not LED badges, so
7777
// the name check is also necessary.
78-
let props = peripheral.properties().await;
79-
if props.is_err() {
78+
let props = peripheral.properties().await.ok()??;
79+
80+
let local_name = props.local_name.as_ref()?;
81+
if local_name != BADGE_BLE_DEVICE_NAME {
8082
return None;
8183
}
8284

83-
if let Some(props) = props.unwrap() {
84-
let local_name = props.local_name.as_ref()?;
85-
if local_name != BADGE_BLE_DEVICE_NAME {
86-
return None;
87-
}
88-
89-
if props
90-
.services
91-
.iter()
92-
.any(|uuid| *uuid == BADGE_SERVICE_UUID)
93-
{
94-
Some(Self { peripheral })
95-
} else {
96-
None
97-
}
85+
if props
86+
.services
87+
.iter()
88+
.any(|uuid| *uuid == BADGE_SERVICE_UUID)
89+
{
90+
Some(Self { peripheral })
9891
} else {
9992
None
10093
}
@@ -128,14 +121,15 @@ impl Device {
128121
.context("bluetooth device connect")?;
129122

130123
let result = self.write_connected(payload).await;
124+
let disconnect_result = self.peripheral.disconnect().await;
125+
131126
if result.is_ok() {
132-
self.peripheral
133-
.disconnect()
134-
.await
135-
.context("bluetooth device disconnect")?;
127+
// Write succesful, return disconnect result
128+
Ok(disconnect_result?)
129+
} else {
130+
// Write failed, return write result and ignore disconnect result
131+
result
136132
}
137-
138-
result
139133
}
140134

141135
async fn write_connected(&self, payload: PayloadBuffer) -> Result<()> {
@@ -145,23 +139,19 @@ impl Device {
145139
.await
146140
.context("discovering services")?;
147141
let characteristics = self.peripheral.characteristics();
148-
let badge_char = characteristics.iter().find(|c| c.uuid == BADGE_CHAR_UUID);
149-
150-
if badge_char.is_none() {
151-
return Err(anyhow::anyhow!("Badge characteristic not found"));
152-
}
153-
let badge_char = badge_char.unwrap();
142+
let badge_char = characteristics
143+
.iter()
144+
.find(|c| c.uuid == BADGE_CHAR_UUID)
145+
.context("badge characteristic not found")?;
154146

155147
// Write payload
156148
let bytes = payload.into_padded_bytes();
157149
let data = bytes.as_ref();
158150

159151
anyhow::ensure!(
160152
data.len() % BLE_CHAR_CHUNK_SIZE == 0,
161-
format!(
162-
"Payload size must be a multiple of {} bytes",
163-
BLE_CHAR_CHUNK_SIZE
164-
)
153+
"Payload size must be a multiple of {} bytes",
154+
BLE_CHAR_CHUNK_SIZE
165155
);
166156

167157
// the device will brick itself if the payload is too long (more then 8192 bytes)

0 commit comments

Comments
 (0)