@@ -129,7 +129,7 @@ pub fn write_device_state(
129129 . map_err ( CLIError :: SerdeJSON )
130130}
131131pub 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 {
169169pub 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 ( ) ..] )
0 commit comments