# HG changeset patch # User Paula Stanciu # Date 1211740147 -10800 # Node ID 7fbff3287a568bf04b6fc4dcf6254a28ca85418a # Parent 5c769ade286a4978834254d1b95eb3c59e6f57f6 added device discovery in the plugin's prefs window diff -r 5c769ade286a -r 7fbff3287a56 src/bluetooth/Makefile --- a/src/bluetooth/Makefile Wed May 21 16:04:24 2008 +0300 +++ b/src/bluetooth/Makefile Sun May 25 21:29:07 2008 +0300 @@ -1,6 +1,6 @@ PLUGIN = bluetooth${PLUGIN_SUFFIX} -SRCS = bluetooth.c gui.c +SRCS = bluetooth.c gui.c marshal.c include ../../buildsys.mk include ../../extra.mk diff -r 5c769ade286a -r 7fbff3287a56 src/bluetooth/bluetooth.c --- a/src/bluetooth/bluetooth.c Wed May 21 16:04:24 2008 +0300 +++ b/src/bluetooth/bluetooth.c Sun May 25 21:29:07 2008 +0300 @@ -1,11 +1,31 @@ +#include +#include +#include +#include #include "bluetooth.h" +#include "marshal.h" +#include "gui.h" + #define DEBUG 1 static gboolean plugin_active = FALSE,exiting=FALSE; +GList * current_device = NULL; +DBusGConnection * bus = NULL; +gint discover_finish =0; +GStaticMutex mutex = G_STATIC_MUTEX_INIT; void bluetooth_init ( void ); void bluetooth_cleanup ( void ); void bt_cfg(void); void bt_about(void); +static void remote_device_found(DBusGProxy *object, char *address, const unsigned int class, const int rssi, gpointer user_data); +static void discovery_started(DBusGProxy *object, gpointer user_data); +static void remote_name_updated(DBusGProxy *object, const char *address, char *name, gpointer user_data); +static void print_results(void); +static void discovery_completed(DBusGProxy *object, gpointer user_data); +void discover_devices(void); + + + GeneralPlugin bluetooth_gp = { .description = "Bluetooth audio suport", @@ -16,21 +36,24 @@ }; GeneralPlugin *bluetooth_gplist[] = { &bluetooth_gp, NULL }; DECLARE_PLUGIN(bluetooth_gp, NULL, NULL, NULL, NULL, NULL, bluetooth_gplist, NULL, NULL) - void bluetooth_init ( void ) { - + audio_devices = NULL; + discover_devices(); } void bluetooth_cleanup ( void ) { + printf("bluetooth: exit\n"); + dbus_g_connection_flush (bus); + dbus_g_connection_unref(bus); } /*void bt_cfg( void ) -{ + { -} -*/ + } + */ void bt_about( void ) { @@ -38,10 +61,129 @@ void refresh_call(void){ + discover_devices() printf("refresh function called\n"); } void connect_call(void){ - printf("connect function \n"); + printf("connect function \n"); +} + + +static void remote_device_found(DBusGProxy *object, char *address, const unsigned int class, const int rssi, gpointer user_data) +{ + int found_in_list=FALSE; + g_static_mutex_lock(&mutex); + current_device = audio_devices; + if((class & 0x200404)==0x200404) + { + + while(current_device != NULL) + { + if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) + { + found_in_list = TRUE; + break; + } + current_device=g_list_next(current_device); + } + if(!found_in_list) + { + DeviceData *dev= g_new0(DeviceData, 1); + dev->class = class; + dev->address = g_strdup(address); + dev->name = NULL; + audio_devices=g_list_prepend(audio_devices, dev); + + } + } + g_static_mutex_unlock(&mutex); +} + +static void discovery_started(DBusGProxy *object, gpointer user_data) +{ + g_print("Signal: DiscoveryStarted()\n"); } +static void remote_name_updated(DBusGProxy *object, const char *address, char *name, gpointer user_data) +{ + g_static_mutex_lock(&mutex); + current_device=audio_devices; + while(current_device != NULL) + { + if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) + { + ((DeviceData*)(current_device->data))->name=g_strdup(name); + break; + } + current_device=g_list_next(current_device); + } + g_static_mutex_unlock(&mutex); +} +static void print_results() +{ + int i=0; + g_print("Final Scan results:\n"); + g_print("Number of audio devices: %d \n",g_list_length(audio_devices)); + current_device=audio_devices; + while(current_device != NULL) + { + g_print("Device %d: Name: %s, Class: 0x%x, Address: %s\n",++i, + ((DeviceData*)(current_device->data))-> name, + ((DeviceData*)(current_device->data))-> class, + ((DeviceData*)(current_device->data))-> address); + + current_device=g_list_next(current_device); + } + + refresh_tree(); +} + +static void discovery_completed(DBusGProxy *object, gpointer user_data) +{ + g_print("Signal: DiscoveryCompleted()\n"); + print_results(); + + +} + + +void discover_devices(void){ + GError *error = NULL; + DBusGProxy * obj = NULL; + g_type_init(); + g_log_set_always_fatal (G_LOG_LEVEL_WARNING); + + bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); + if (error != NULL) + { + g_printerr("Connecting to system bus failed: %s\n", error->message); + g_error_free(error); + exit(EXIT_FAILURE); + } + obj = dbus_g_proxy_new_for_name(bus, "org.bluez", "/org/bluez/hci0", "org.bluez.Adapter"); + + dbus_g_object_register_marshaller(marshal_VOID__STRING_UINT_INT, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID); + dbus_g_proxy_add_signal(obj, "RemoteDeviceFound", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID); + dbus_g_proxy_connect_signal(obj, "RemoteDeviceFound", G_CALLBACK(remote_device_found), bus, NULL); + + dbus_g_proxy_add_signal(obj, "DiscoveryStarted", G_TYPE_INVALID); + dbus_g_proxy_connect_signal(obj, "DiscoveryStarted", G_CALLBACK(discovery_started), bus, NULL); + + dbus_g_proxy_add_signal(obj, "DiscoveryCompleted", G_TYPE_INVALID); + dbus_g_proxy_connect_signal(obj, "DiscoveryCompleted", G_CALLBACK(discovery_completed), bus, NULL); + + dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); + dbus_g_proxy_add_signal(obj, "RemoteNameUpdated", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); + dbus_g_proxy_connect_signal(obj, "RemoteNameUpdated", G_CALLBACK(remote_name_updated), NULL, NULL); + + dbus_g_proxy_call(obj, "DiscoverDevices", &error, G_TYPE_INVALID, G_TYPE_INVALID); + if (error != NULL) + { + g_printerr("Failed to discover devices: %s\n", error->message); + g_error_free(error); + exit(EXIT_FAILURE); + } + // / dbus_g_connection_flush (bus); + // dbus_g_connection_unref(bus); +} diff -r 5c769ade286a -r 7fbff3287a56 src/bluetooth/bluetooth.h --- a/src/bluetooth/bluetooth.h Wed May 21 16:04:24 2008 +0300 +++ b/src/bluetooth/bluetooth.h Sun May 25 21:29:07 2008 +0300 @@ -8,5 +8,15 @@ #include #include #include "gui.h" +typedef struct { + guint class; + gchar* address; + gchar* name; +}DeviceData; + + + void refresh_call(void); void connect_call(void); +GList * audio_devices; + diff -r 5c769ade286a -r 7fbff3287a56 src/bluetooth/gui.c --- a/src/bluetooth/gui.c Wed May 21 16:04:24 2008 +0300 +++ b/src/bluetooth/gui.c Sun May 25 21:29:07 2008 +0300 @@ -1,7 +1,5 @@ #include "gui.h" #include "bluetooth.h" -#include -#include static GtkWidget *window = NULL; static GtkTreeModel *model; @@ -18,53 +16,97 @@ GtkWidget *close_button; GtkWidget *treeview; GtkWidget *label_p; -GtkWidget *label_m; +GtkWidget *label_c; GtkWidget *label_a; GtkWidget *label_prod; -GtkWidget *label_model; +GtkWidget *label_class; GtkWidget *label_address; - - -typedef struct -{ - gchar *producer; - gchar *model; -}Headset; - +GList * dev = NULL; enum{ COLUMN_PRODUCER, - COLUMN_MODEL, NUM_COLUMNS }; +static DeviceData test_data[]= +{ + {0,"00:00:00:00:00","Scanning"} +}; -static Headset test_data[]= -{ - {"Motorola", "S9"}, - {"Nokia", "BH-503"}, - {"Blueant","Stereo X5"} -}; static GtkTreeModel * create_model(void) { - gint i = 0; GtkListStore *store; GtkTreeIter iter; - /* create list store */ + gint i=0; + /* create list store */ store = gtk_list_store_new(NUM_COLUMNS, - G_TYPE_STRING, G_TYPE_STRING); + /* add data to the list store */ for(i = 0;idata))-> name; + test_data[i].class = ((DeviceData*)(dev->data))-> class; + test_data[i].address = ((DeviceData*)(dev->data))-> address; + i++; + dev=g_list_next(dev); } - return GTK_TREE_MODEL(store); + if (dev_no == 0) + { + test_data[0].name = "No devices found!"; + test_data[0].class = 0; + test_data[0].address = "00:00:00:00:00"; + } + + /* add data to the list store */ + for(i = 0;i +#include +#include +void refresh_tree(void);