From: Vibhav Pant vibhavp@gmail.com
--- dlls/winebth.sys/dbus.c | 155 +++++++++++++++++++++++++++++-- dlls/winebth.sys/unixlib.c | 11 +++ dlls/winebth.sys/unixlib.h | 8 ++ dlls/winebth.sys/unixlib_priv.h | 2 + dlls/winebth.sys/winebluetooth.c | 12 +++ dlls/winebth.sys/winebth.c | 27 ++++++ dlls/winebth.sys/winebth_priv.h | 10 ++ include/wine/winebth.h | 8 ++ 8 files changed, 224 insertions(+), 9 deletions(-)
diff --git a/dlls/winebth.sys/dbus.c b/dlls/winebth.sys/dbus.c index 35ac0ff32fe..19f25e517d9 100644 --- a/dlls/winebth.sys/dbus.c +++ b/dlls/winebth.sys/dbus.c @@ -178,6 +178,22 @@ static const char *bluez_next_dict_entry( DBusMessageIter *iter, DBusMessageIter return name; }
+/* Adds an entry to a dict iterator of type {sv} */ +static void bluez_variant_dict_add_entry( DBusMessageIter *dict, const char *key, int value_type, + const char *value_type_str, const void *value ) +{ + DBusMessageIter entry, variant; + + p_dbus_message_iter_open_container( dict, DBUS_TYPE_DICT_ENTRY, NULL, &entry ); + p_dbus_message_iter_append_basic( &entry, DBUS_TYPE_STRING, &key ); + + p_dbus_message_iter_open_container( &entry, DBUS_TYPE_VARIANT, value_type_str, &variant ); + p_dbus_message_iter_append_basic( &variant, value_type, value ); + p_dbus_message_iter_close_container( &entry, &variant ); + + p_dbus_message_iter_close_container( dict, &entry ); +} + static const char *dbgstr_dbus_message( DBusMessage *message ) { const char *interface; @@ -265,6 +281,103 @@ static void parse_mac_address( const char *addr_str, BYTE dest[6] ) dest[i] = addr[i]; }
+static NTSTATUS bluez_adapter_set_discovery_filter( void *connection, const char *adapter_path, + const char *transport_str ) +{ + DBusMessage *request, *reply; + DBusMessageIter iter, dict_iter; + DBusError error; + + TRACE( "(%p, %s, %s)\n", connection, debugstr_a( adapter_path ), debugstr_a( transport_str ) ); + + request = p_dbus_message_new_method_call( BLUEZ_DEST, adapter_path, BLUEZ_INTERFACE_ADAPTER, + "SetDiscoveryFilter" ); + if (!request) return STATUS_NO_MEMORY; + + p_dbus_message_iter_init_append( request, &iter ); + p_dbus_message_iter_open_container( &iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter ); + bluez_variant_dict_add_entry( &dict_iter, "Transport", DBUS_TYPE_STRING, + DBUS_TYPE_STRING_AS_STRING, &transport_str ); + p_dbus_message_iter_close_container( &iter, &dict_iter ); + + p_dbus_error_init ( &error ); + + TRACE( "Setting discovery filter on %s to use transport %s\n", debugstr_a( adapter_path ), + debugstr_a( transport_str ) ); + reply = + p_dbus_connection_send_with_reply_and_block( connection, request, bluez_timeout, &error ); + p_dbus_message_unref( request ); + if (!reply) + { + NTSTATUS status; + WARN( "Failed to set discovery filter: %s: %s\n", debugstr_a( error.name ), + debugstr_a( error.message ) ); + status = bluez_dbus_error_to_ntstatus( &error ); + p_dbus_error_free( &error ); + return status; + } + p_dbus_message_unref( reply ); + p_dbus_error_free( &error ); + + return STATUS_SUCCESS; + +} + +NTSTATUS bluez_adapter_start_discovery( void *connection, const char *adapter_path, + enum winebluetooth_discovery_transport transport ) +{ + DBusMessage *request, *reply; + DBusError error; + + TRACE( "(%p, %s, %d)\n", connection, debugstr_a( adapter_path ), transport ); + + if (transport != WINEBLUETOOTH_DISCOVERY_TRANSPORT_DEFAULT) + { + const char *transport_str; + NTSTATUS status; + + switch ( transport ) + { + case WINEBLUETOOTH_DISCOVERY_TRANSPORT_AUTO: + transport_str = "auto"; + break; + case WINEBLUETOOTH_DISCOVERY_TRANSPORT_BR_EDR: + transport_str = "bredr"; + break; + case WINEBLUETOOTH_DISCOVERY_TRANSPORT_LE: + transport_str = "le"; + break; + default: + return STATUS_INVALID_PARAMETER; + } + + status = bluez_adapter_set_discovery_filter( connection, adapter_path, transport_str ); + if (status != STATUS_SUCCESS) return status; + } + + request = p_dbus_message_new_method_call( BLUEZ_DEST, adapter_path, BLUEZ_INTERFACE_ADAPTER, + "StartDiscovery" ); + if (!request) return STATUS_NO_MEMORY; + + TRACE( "Starting discovery on %s\n", debugstr_a( adapter_path ) ); + p_dbus_error_init( &error ); + reply = + p_dbus_connection_send_with_reply_and_block( connection, request, bluez_timeout, &error ); + p_dbus_message_unref( request ); + if (!reply) + { + NTSTATUS status; + ERR( "Failed to start discovery on adapter %s: %s: %s", debugstr_a( adapter_path ), + debugstr_a( error.message ), debugstr_a( error.name ) ); + status = bluez_dbus_error_to_ntstatus( &error ); + p_dbus_error_free( &error ); + return status; + } + p_dbus_error_free( &error ); + p_dbus_message_unref( reply ); + return STATUS_SUCCESS; +} + NTSTATUS bluez_adapter_set_prop( void *connection, struct bluetooth_adapter_set_prop_params *params ) { DBusMessage *request, *reply; @@ -460,6 +573,10 @@ struct bluez_watcher_ctx { void *init_device_list_call;
+ /* Calling dbus_connection_send_to_reply_and_block from other threads will run the DBus dispatch + * loop, which calls bluez_filter. Therefore, we need to protect access to the event list(s) + * with a lock. */ + pthread_mutex_t event_lists_lock; /* struct bluez_init_entry */ struct list initial_radio_list;
@@ -614,13 +731,11 @@ static UINT16 bluez_dbus_get_invalidated_properties_from_iter(
static DBusHandlerResult bluez_filter( DBusConnection *conn, DBusMessage *msg, void *user_data ) { - struct list *event_list; + struct bluez_watcher_ctx *ctx = user_data;
if (TRACE_ON( dbus )) TRACE_( dbus )( "(%s, %s, %p)\n", dbgstr_dbus_connection( conn ), dbgstr_dbus_message( msg ), user_data );
- event_list = &((struct bluez_watcher_ctx *)user_data)->event_list; - if (p_dbus_message_is_signal( msg, DBUS_INTERFACE_OBJECTMANAGER, DBUS_OBJECTMANAGER_SIGNAL_INTERFACESADDED ) && p_dbus_message_has_signature( msg, DBUS_INTERFACES_ADDED_SIGNATURE )) { @@ -667,9 +782,11 @@ static DBusHandlerResult bluez_filter( DBusConnection *conn, DBusMessage *msg, v union winebluetooth_watcher_event_data event = { .radio_added = radio_added }; TRACE( "New BlueZ org.bluez.Adapter1 object added at %s: %p\n", debugstr_a( object_path ), radio ); + pthread_mutex_lock( &ctx->event_lists_lock ); if (!bluez_event_list_queue_new_event( - event_list, BLUETOOTH_WATCHER_EVENT_TYPE_RADIO_ADDED, event )) + &ctx->event_list, BLUETOOTH_WATCHER_EVENT_TYPE_RADIO_ADDED, event )) unix_name_free( radio ); + pthread_mutex_unlock( &ctx->event_lists_lock ); } } p_dbus_message_iter_next( &ifaces_iter ); @@ -713,9 +830,11 @@ static DBusHandlerResult bluez_filter( DBusConnection *conn, DBusMessage *msg, v } radio.handle = (UINT_PTR)radio_name; event.radio_removed = radio; + pthread_mutex_lock( &ctx->event_lists_lock ); if (!bluez_event_list_queue_new_event( - event_list, BLUETOOTH_WATCHER_EVENT_TYPE_RADIO_REMOVED, event )) + &ctx->event_list, BLUETOOTH_WATCHER_EVENT_TYPE_RADIO_REMOVED, event )) unix_name_free( radio_name ); + pthread_mutex_unlock( &ctx->event_lists_lock ); } } p_dbus_free_string_array( interfaces ); @@ -791,27 +910,33 @@ static DBusHandlerResult bluez_filter( DBusConnection *conn, DBusMessage *msg, v return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; }
- if (!bluez_event_list_queue_new_event_with_call( event_list, + pthread_mutex_lock( &ctx->event_lists_lock ); + if (!bluez_event_list_queue_new_event_with_call( &ctx->event_list, BLUETOOTH_WATCHER_EVENT_TYPE_RADIO_PROPERTIES_CHANGED, event, pending_call, bluez_filter_radio_props_changed_callback )) { + pthread_mutex_unlock( &ctx->event_lists_lock ); unix_name_free( radio ); p_dbus_pending_call_cancel( pending_call ); p_dbus_pending_call_unref( pending_call ); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } + pthread_mutex_unlock( &ctx->event_lists_lock ); } else { union winebluetooth_watcher_event_data event = { .radio_props_changed = props_changed }; - if (!bluez_event_list_queue_new_event( event_list, + pthread_mutex_lock( &ctx->event_lists_lock ); + if (!bluez_event_list_queue_new_event( &ctx->event_list, BLUETOOTH_WATCHER_EVENT_TYPE_RADIO_PROPERTIES_CHANGED, event )) { + pthread_mutex_unlock( &ctx->event_lists_lock ); unix_name_free( radio ); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } + pthread_mutex_unlock( &ctx->event_lists_lock ); } } } @@ -848,6 +973,7 @@ NTSTATUS bluez_watcher_init( void *connection, void **ctx ) return status; } watcher_ctx->init_device_list_call = call; + pthread_mutex_init( &watcher_ctx->event_lists_lock, NULL ); list_init( &watcher_ctx->initial_radio_list );
list_init( &watcher_ctx->event_list ); @@ -971,14 +1097,17 @@ static NTSTATUS bluez_build_initial_device_lists( DBusMessage *reply, struct lis
static BOOL bluez_watcher_event_queue_ready( struct bluez_watcher_ctx *ctx, struct winebluetooth_watcher_event *event ) { + pthread_mutex_lock( &ctx->event_lists_lock ); if (!list_empty( &ctx->initial_radio_list )) { struct bluez_init_entry *radio;
radio = LIST_ENTRY( list_head( &ctx->initial_radio_list ), struct bluez_init_entry, entry ); + list_remove( &radio->entry ); + pthread_mutex_unlock( &ctx->event_lists_lock ); + event->event_type = BLUETOOTH_WATCHER_EVENT_TYPE_RADIO_ADDED; event->event_data.radio_added = radio->object.radio; - list_remove( &radio->entry ); free( radio ); return TRUE; } @@ -988,16 +1117,22 @@ static BOOL bluez_watcher_event_queue_ready( struct bluez_watcher_ctx *ctx, stru LIST_ENTRY( list_head( &ctx->event_list ), struct bluez_watcher_event, entry );
if (watcher_event->pending_call && !p_dbus_pending_call_get_completed( watcher_event->pending_call )) + { + pthread_mutex_unlock( &ctx->event_lists_lock); return FALSE; + } + + list_remove( &watcher_event->entry ); + pthread_mutex_unlock( &ctx->event_lists_lock );
event->event_type = watcher_event->event_type; event->event_data = watcher_event->event; - list_remove( &watcher_event->entry ); if (watcher_event->pending_call) p_dbus_pending_call_unref( watcher_event->pending_call ); free( watcher_event ); return TRUE; } + pthread_mutex_unlock( &ctx->event_lists_lock ); return FALSE; }
@@ -1045,7 +1180,9 @@ NTSTATUS bluez_dbus_loop( void *c, void *watcher, p_dbus_connection_unref( connection ); return STATUS_NO_MEMORY; } + pthread_mutex_lock( &watcher_ctx->event_lists_lock ); status = bluez_build_initial_device_lists( reply, &watcher_ctx->initial_radio_list ); + pthread_mutex_unlock( &watcher_ctx->event_lists_lock ); p_dbus_message_unref( reply ); if (status != STATUS_SUCCESS) { diff --git a/dlls/winebth.sys/unixlib.c b/dlls/winebth.sys/unixlib.c index 982652498e5..6009f7f3af1 100644 --- a/dlls/winebth.sys/unixlib.c +++ b/dlls/winebth.sys/unixlib.c @@ -163,6 +163,15 @@ static NTSTATUS bluetooth_adapter_set_prop( void *arg ) return bluez_adapter_set_prop( dbus_connection, params ); }
+static NTSTATUS bluetooth_adapter_start_discovery( void *args ) +{ + struct bluetooth_adapter_start_discovery_params *params = args; + + if (!dbus_connection) return STATUS_NOT_SUPPORTED; + return bluez_adapter_start_discovery( dbus_connection, params->adapter->str, + params->transport ); +} + static NTSTATUS bluetooth_get_event( void *args ) { struct bluetooth_get_event_params *params = args; @@ -180,6 +189,8 @@ const unixlib_entry_t __wine_unix_call_funcs[] = { bluetooth_adapter_get_unique_name, bluetooth_adapter_free,
+ bluetooth_adapter_start_discovery, + bluetooth_get_event, };
diff --git a/dlls/winebth.sys/unixlib.h b/dlls/winebth.sys/unixlib.h index b16f873deef..8d10f1d8adb 100644 --- a/dlls/winebth.sys/unixlib.h +++ b/dlls/winebth.sys/unixlib.h @@ -65,6 +65,12 @@ struct bluetooth_adapter_set_prop_params union winebluetooth_property *prop; };
+struct bluetooth_adapter_start_discovery_params +{ + unix_name_t adapter; + enum winebluetooth_discovery_transport transport; +}; + struct bluetooth_get_event_params { struct winebluetooth_event result; @@ -79,6 +85,8 @@ enum bluetoothapis_funcs unix_bluetooth_adapter_get_unique_name, unix_bluetooth_adapter_free,
+ unix_bluetooth_adapter_start_discovery, + unix_bluetooth_get_event,
unix_funcs_count diff --git a/dlls/winebth.sys/unixlib_priv.h b/dlls/winebth.sys/unixlib_priv.h index 5146799e705..7683af6ef2d 100644 --- a/dlls/winebth.sys/unixlib_priv.h +++ b/dlls/winebth.sys/unixlib_priv.h @@ -50,6 +50,8 @@ extern void bluez_dbus_free( void *connection ); extern NTSTATUS bluez_dbus_loop( void *connection, void *watcher_ctx, struct winebluetooth_event *result ); extern NTSTATUS bluez_adapter_set_prop( void *connection, struct bluetooth_adapter_set_prop_params *params ); +extern NTSTATUS bluez_adapter_start_discovery( void *connection, const char *adapter_path, + enum winebluetooth_discovery_transport transport ); extern NTSTATUS bluez_watcher_init( void *connection, void **ctx ); extern void bluez_watcher_close( void *connection, void *ctx ); #endif /* __WINE_WINEBTH_UNIXLIB_PRIV_H */ diff --git a/dlls/winebth.sys/winebluetooth.c b/dlls/winebth.sys/winebluetooth.c index faf3b387aef..7f321c8348e 100644 --- a/dlls/winebth.sys/winebluetooth.c +++ b/dlls/winebth.sys/winebluetooth.c @@ -82,6 +82,18 @@ NTSTATUS winebluetooth_radio_set_property( winebluetooth_radio_t radio, return STATUS_SUCCESS; }
+NTSTATUS winebluetooth_radio_start_discovery( winebluetooth_radio_t radio, + enum winebluetooth_discovery_transport transport ) +{ + struct bluetooth_adapter_start_discovery_params params = {0}; + + TRACE( "(%p, %d)\n", (void *)radio.handle, transport ); + + params.adapter = radio.handle; + params.transport = transport; + return UNIX_BLUETOOTH_CALL( bluetooth_adapter_start_discovery, ¶ms ); +} + void winebluetooth_radio_free( winebluetooth_radio_t radio ) { struct bluetooth_adapter_free_params args = { 0 }; diff --git a/dlls/winebth.sys/winebth.c b/dlls/winebth.sys/winebth.c index e5ae08d4b32..245268e8a8f 100644 --- a/dlls/winebth.sys/winebth.c +++ b/dlls/winebth.sys/winebth.c @@ -168,6 +168,33 @@ static NTSTATUS WINAPI dispatch_bluetooth( DEVICE_OBJECT *device, IRP *irp ) } break; } + case IOCTL_WINEBTH_RADIO_START_DISCOVERY: + { + struct winebth_radio_start_discovery_params *filter = irp->AssociatedIrp.SystemBuffer; + enum winebluetooth_discovery_transport transport; + + if (!filter) + { + status = STATUS_INVALID_PARAMETER; + break; + } + if (insize < sizeof(*filter)) + { + status = STATUS_BUFFER_TOO_SMALL; + break; + } + + if (filter->le && filter->bredr) + transport = WINEBLUETOOTH_DISCOVERY_TRANSPORT_AUTO; + else if (filter->le) + transport = WINEBLUETOOTH_DISCOVERY_TRANSPORT_LE; + else if (filter->bredr) + transport = WINEBLUETOOTH_DISCOVERY_TRANSPORT_BR_EDR; + else + transport = WINEBLUETOOTH_DISCOVERY_TRANSPORT_DEFAULT; + status = winebluetooth_radio_start_discovery( ext->radio, transport ); + break; + } default: FIXME( "Unimplemented IOCTL code: %#lx\n", code ); break; diff --git a/dlls/winebth.sys/winebth_priv.h b/dlls/winebth.sys/winebth_priv.h index 06dc3a8b799..1f9f74bf5a7 100644 --- a/dlls/winebth.sys/winebth_priv.h +++ b/dlls/winebth.sys/winebth_priv.h @@ -154,6 +154,14 @@ struct winebluetooth_radio_properties BYTE version; };
+enum winebluetooth_discovery_transport +{ + WINEBLUETOOTH_DISCOVERY_TRANSPORT_DEFAULT, + WINEBLUETOOTH_DISCOVERY_TRANSPORT_AUTO, + WINEBLUETOOTH_DISCOVERY_TRANSPORT_BR_EDR, + WINEBLUETOOTH_DISCOVERY_TRANSPORT_LE +}; + NTSTATUS winebluetooth_radio_get_unique_name( winebluetooth_radio_t radio, char *name, SIZE_T *size ); void winebluetooth_radio_free( winebluetooth_radio_t radio ); @@ -164,6 +172,8 @@ static inline BOOL winebluetooth_radio_equal( winebluetooth_radio_t r1, wineblue NTSTATUS winebluetooth_radio_set_property( winebluetooth_radio_t radio, winebluetooth_radio_props_mask_t prop, union winebluetooth_property *property ); +NTSTATUS winebluetooth_radio_start_discovery( winebluetooth_radio_t radio, + enum winebluetooth_discovery_transport transport );
enum winebluetooth_watcher_event_type { diff --git a/include/wine/winebth.h b/include/wine/winebth.h index c26a2100fb9..94812a345a2 100644 --- a/include/wine/winebth.h +++ b/include/wine/winebth.h @@ -25,6 +25,8 @@ /* Set the discoverability or connectable flag for a local radio. Enabling discoverability will also enable incoming * connections, while disabling incoming connections disables discoverability as well. */ #define IOCTL_WINEBTH_RADIO_SET_FLAG CTL_CODE(FILE_DEVICE_BLUETOOTH, 0xa3, METHOD_BUFFERED, FILE_ANY_ACCESS) +/* Start and stop the device inquiry procedure for a local radio. */ +#define IOCTL_WINEBTH_RADIO_START_DISCOVERY CTL_CODE(FILE_DEVICE_BLUETOOTH, 0xa6, METHOD_BUFFERED, FILE_ANY_ACCESS)
#include <pshpack1.h>
@@ -37,6 +39,12 @@ struct winebth_radio_set_flag_params unsigned int enable : 1; };
+struct winebth_radio_start_discovery_params +{ + unsigned int bredr : 1; + unsigned int le : 1; +}; + #include <poppack.h>
#endif /* __WINEBTH_H__ */