Skip to content

Commit a37d8b0

Browse files
committed
added old work source files. Trying to get started again
1 parent 64e5be8 commit a37d8b0

6 files changed

Lines changed: 28 additions & 31 deletions

File tree

cli/src/commands/ble/hci/dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub async fn dump_adapter_pcap(
4848
which_adapter: &'_ str,
4949
pcap_file: Option<&'_ str>,
5050
) -> Result<(), CLIError> {
51-
let adapter = helper::hci_adapter(which_adapter)?;
51+
let adapter = helper::hci_adapter(which_adapter).await?;
5252
println!("using adapter `{:?}`", adapter);
5353
match pcap_file {
5454
Some(pcap_file) => {

cli/src/commands/provisioner.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,13 @@ pub async fn provision(
7373
const BEARER_CHANNEL_SIZE: usize = 16;
7474
let dsm = crate::helper::load_device_state(device_state_path)?;
7575
println!("opening HCI adapter...");
76-
let adapter = crate::helper::hci_adapter(which_adapter)?;
76+
let adapter = crate::helper::hci_adapter(which_adapter).await?;
7777
println!("HCI adapter (`{:?}`) open!", adapter);
78-
// If using USB HCI adapter, `read_event()` blocks instead of `Poll::Pending` so this will
79-
// block the current thread
78+
// TODO: I wrote a async usb driver (`usbw`) so a BufferedHCIAdvertiser might not be needed anymore!
8079
let (mut adapter, mut bearer_rx, bearer_tx) =
8180
BufferedHCIAdvertiser::new_with_channel_size(adapter, BEARER_CHANNEL_SIZE);
82-
// Workaround is to spawn a thread for the adapter. In the future, hopefully there will be a
83-
// async rust USB library.
84-
let _adapter_thread = std::thread::spawn(move || {
85-
tokio_runtime().block_on(adapter.run_loop_send_error());
81+
let _adapter_task = task::spawn(async move {
82+
adapter.run_loop_send_error().await;
8683
});
8784
async move {
8885
let early_end_error =

cli/src/helper.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub fn write_device_state(
129129
.map_err(CLIError::SerdeJSON)
130130
}
131131
pub fn tokio_runtime() -> tokio::runtime::Runtime {
132-
tokio::runtime::Builder::new_current_thread()
132+
tokio::runtime::Builder::new_multi_thread()
133133
.enable_all()
134134
.build()
135135
.expect("can't make async runtime")
@@ -169,16 +169,17 @@ impl Adapter for HCIAdapter {
169169
pub fn libusb_context() -> &'static AsyncContext {
170170
static CONTEXT: once_cell::sync::OnceCell<AsyncContext> = once_cell::sync::OnceCell::new();
171171
CONTEXT.get_or_init(|| {
172-
usbw::libusb::context::default_context()
173-
.expect("unable to start libusb context")
174-
.start_async()
172+
let context =
173+
usbw::libusb::context::default_context().expect("unable to start libusb context");
174+
context.set_debug_level(usbw::libusb::context::LogLevel::Info);
175+
context.start_async()
175176
})
176177
}
177-
pub fn usb_adapter(adapter_id: u16) -> Result<btle::hci::usb::adapter::Adapter, CLIError> {
178+
pub async fn usb_adapter(adapter_id: u16) -> Result<btle::hci::usb::adapter::Adapter, CLIError> {
178179
let context = libusb_context();
179180
let device_list = context.context_ref().device_list();
180181
let mut adapters = btle::hci::usb::device::bluetooth_adapters(device_list.iter());
181-
let mut adapter = adapters
182+
let adapter = adapters
182183
.nth(adapter_id.into())
183184
.ok_or_else(|| CLIError::OtherMessage("no usb bluetooth adapters found".to_owned()))??
184185
.open()
@@ -191,20 +192,23 @@ pub fn usb_adapter(adapter_id: u16) -> Result<btle::hci::usb::adapter::Adapter,
191192
btle::hci::usb::Error::from(e).into(),
192193
)),
193194
})?;
194-
adapter.reset().map_err(btle::hci::usb::Error::from)?;
195-
Ok(btle::hci::usb::adapter::Adapter::open(
196-
context.make_async_device(adapter),
197-
)?)
198-
}
199-
pub fn hci_adapter(which_adapter: &str) -> Result<HCIAdapter, CLIError> {
195+
let mut adapter = btle::hci::usb::adapter::Adapter::open(context.make_async_device(adapter))?;
196+
adapter
197+
.flush_event_buffer()
198+
.await
199+
.map_err(btle::hci::usb::Error::from)?;
200+
Ok(adapter)
201+
}
202+
pub async fn hci_adapter(which_adapter: &str) -> Result<HCIAdapter, CLIError> {
200203
pub const USB_PREFIX: &'static str = "usb:";
201204
pub const BLUEZ_PREFIX: &'static str = "bluez:";
202205
if which_adapter.starts_with(USB_PREFIX) {
203-
Ok(HCIAdapter::Usb(usb_adapter(
204-
(&which_adapter[USB_PREFIX.len()..]).parse().map_err(|_| {
206+
Ok(HCIAdapter::Usb(
207+
usb_adapter((&which_adapter[USB_PREFIX.len()..]).parse().map_err(|_| {
205208
CLIError::OtherMessage(format!("bad usb adapter {}", which_adapter))
206-
})?,
207-
)?))
209+
})?)
210+
.await?,
211+
))
208212
} else if which_adapter.starts_with(BLUEZ_PREFIX) {
209213
Ok(HCIAdapter::BlueZ(bluez_adapter(
210214
(&which_adapter[BLUEZ_PREFIX.len()..])

src/provisioning/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<B: Storage<u8>> Link<B> {
127127
link_id: LinkID,
128128
uuid: &UUID,
129129
) -> Link<B> {
130-
let mut link = Link {
130+
let link = Link {
131131
link_id,
132132
my_transaction_number: TransactionNumber::new_provisioner().next(),
133133
other_transaction_number: TransactionNumber::new_provisionee(),

src/stack/bearers/advertiser.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,13 @@ impl<A: btle::hci::adapter::Adapter> BufferedHCIAdvertiser<A> {
235235
self.bearer
236236
.set_advertising_data(advertisement.as_ref())
237237
.await?;
238-
println!("start send");
239238
// Enable advertising
240239
self.bearer.set_advertising_enable(true).await?;
241-
println!("wait");
242240
// Scan for advertisements while advertising for `advertisement_duration`
243241
self.handle_incoming_for(advertisement_duration * u32::from(transmit_count))
244242
.await?;
245243
// Disable advertising
246-
println!("stop send");
247244
self.bearer.set_advertising_enable(false).await?;
248-
println!("done");
249245
Ok(())
250246
}
251247
}

src/stack/incoming.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Incoming {
6363
async fn handle_encrypted_access_loop(
6464
internals: Arc<RwLock<StackInternals>>,
6565
mut incoming_encrypted_access: mpsc::Receiver<EncryptedIncomingMessage<Box<[u8]>>>,
66-
mut outgoing_encrypted_access: mpsc::Sender<IncomingMessage<Box<[u8]>>>,
66+
outgoing_encrypted_access: mpsc::Sender<IncomingMessage<Box<[u8]>>>,
6767
) -> Result<(), RecvError> {
6868
loop {
6969
let next = incoming_encrypted_access
@@ -175,7 +175,7 @@ impl Incoming {
175175
replay_cache: Arc<Mutex<replay::Cache>>,
176176
mut outgoing_relay: Option<mpsc::Sender<RelayPDU>>,
177177
mut incoming: mpsc::Receiver<IncomingEncryptedNetworkPDU>,
178-
mut outgoing: mpsc::Sender<IncomingNetworkPDU>,
178+
outgoing: mpsc::Sender<IncomingNetworkPDU>,
179179
) -> Result<(), RecvError> {
180180
loop {
181181
let next = incoming.recv().await.ok_or(RecvError::ChannelClosed)?;

0 commit comments

Comments
 (0)