]> err.no Git - mapper/blob - src/gps-bluetooth-bluez-dbus.c
More map widget integration changes
[mapper] / src / gps-bluetooth-bluez-dbus.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  * Copyright (C) 2006-2007 John Costigan.
6  *
7  * POI and GPS-Info code originally written by Cezary Jackiewicz.
8  *
9  * Default map data provided by http://www.openstreetmap.org/
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27
28 #include <stdlib.h>
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib.h>
31 #include <glib-object.h>
32
33 #include "mapper.h"
34 #include "bluetooth-scan.h"
35 #include "gps-bluetooth-bluez-marshal.h"
36 #include "gps.h"
37 #include "settings.h"
38 #include "ui-common.h"
39 #include "gps-conn.h"
40
41 /**
42  * Bluetooth device scanning functions below
43  */
44 static void
45 scan_cb_dev_found(DBusGProxy *object, const char *address, const unsigned int class, const int rssi, ScanInfo *scan_info)
46 {
47 GtkTreeIter iter;
48
49 gtk_list_store_append(scan_info->store, &iter);
50 gtk_list_store_set(scan_info->store, &iter, 0, g_strdup(address), -1);
51 }
52
53 static void 
54 scan_cb_dev_name_found(DBusGProxy *object, const char *address, const char *name, ScanInfo *scan_info)
55 {
56 GtkTreeIter iter;
57 gboolean c;
58
59 c=gtk_tree_model_get_iter_first(scan_info->store, &iter);
60 while (c == TRUE) {
61         gchar *value;
62
63         gtk_tree_model_get(GTK_TREE_MODEL(scan_info->store), &iter, 0, &value, -1);
64         if (g_ascii_strcasecmp(address, value) == 0) {
65                 gtk_list_store_set(scan_info->store, &iter, 1, g_strdup(name), -1);
66                 return;
67         }
68         c=gtk_tree_model_iter_next(GTK_TREE_MODEL(scan_info->store), &iter);
69 }
70
71 }
72
73 static void 
74 scan_cb_search_complete(DBusGProxy *object, ScanInfo *scan_info)
75 {
76 gtk_widget_destroy(scan_info->banner);
77 dbus_g_proxy_disconnect_signal(object, "RemoteDeviceFound", G_CALLBACK(scan_cb_dev_found), scan_info);
78 dbus_g_proxy_disconnect_signal(object, "DiscoveryCompleted", G_CALLBACK(scan_cb_search_complete), scan_info);
79 dbus_g_proxy_disconnect_signal(object, "RemoteNameUpdated", G_CALLBACK(scan_cb_dev_name_found), scan_info);
80 }
81
82 gint 
83 scan_start_search(ScanInfo *scan_info)
84 {
85 GError *error = NULL;
86
87 if (NULL == (scan_info->req_proxy = dbus_g_proxy_new_for_name(dbus_conn, "org.bluez", "/org/bluez/hci0", "org.bluez.Adapter"))) {
88         g_printerr("Failed to create D-Bus request proxy for btsearch.");
89         return 2;
90 }
91
92 dbus_g_object_register_marshaller(_bt_bluez_VOID__STRING_UINT_INT, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID);
93 dbus_g_proxy_add_signal(scan_info->req_proxy, "RemoteDeviceFound", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID);
94 dbus_g_proxy_connect_signal(scan_info->req_proxy, "RemoteDeviceFound", G_CALLBACK(scan_cb_dev_found), scan_info, NULL);
95
96 dbus_g_proxy_add_signal(scan_info->req_proxy, "DiscoveryCompleted", G_TYPE_INVALID);
97 dbus_g_proxy_connect_signal(scan_info->req_proxy, "DiscoveryCompleted", G_CALLBACK(scan_cb_search_complete), scan_info, NULL);
98
99 dbus_g_object_register_marshaller(_bt_bluez_VOID__STRING_STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
100 dbus_g_proxy_add_signal(scan_info->req_proxy, "RemoteNameUpdated", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
101 dbus_g_proxy_connect_signal(scan_info->req_proxy, "RemoteNameUpdated", G_CALLBACK(scan_cb_dev_name_found), scan_info, NULL);
102
103 error = NULL;
104 if (!dbus_g_proxy_call(scan_info->req_proxy, "DiscoverDevices", &error, G_TYPE_INVALID, G_TYPE_INVALID)) {
105         if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) {
106                 g_printerr("Caught remote method exception %s: %s", dbus_g_error_get_name(error), error->message);
107         } else {
108                 g_printerr("Error: %s\n", error->message);
109         }
110         return 3;
111 }
112
113 return 0;
114 }
115