~stgraber/+junk/nm-ofono

« back to all changes in this revision

Viewing changes to src/bluez-manager/nm-bluez-adapter.c

  • Committer: Stéphane Graber
  • Date: 2013-03-15 18:27:57 UTC
  • Revision ID: stgraber@ubuntu.com-20130315182757-7bcjymubsn3lsshs
Import of NM 0.9.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
/* NetworkManager -- Network link manager
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
17
 *
 
18
 * Copyright (C) 2009 - 2012 Red Hat, Inc.
 
19
 */
 
20
 
 
21
#include <glib.h>
 
22
#include <string.h>
 
23
 
 
24
#include "NetworkManager.h"
 
25
#include "nm-dbus-manager.h"
 
26
#include "nm-bluez-adapter.h"
 
27
#include "nm-bluez-device.h"
 
28
#include "nm-bluez-common.h"
 
29
#include "nm-dbus-glib-types.h"
 
30
#include "nm-logging.h"
 
31
 
 
32
 
 
33
G_DEFINE_TYPE (NMBluezAdapter, nm_bluez_adapter, G_TYPE_OBJECT)
 
34
 
 
35
#define NM_BLUEZ_ADAPTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_BLUEZ_ADAPTER, NMBluezAdapterPrivate))
 
36
 
 
37
typedef struct {
 
38
        char *path;
 
39
        DBusGProxy *proxy;
 
40
        gboolean initialized;
 
41
 
 
42
        char *address;
 
43
        GHashTable *devices;
 
44
 
 
45
        /* Cached for devices */
 
46
        NMConnectionProvider *provider;
 
47
} NMBluezAdapterPrivate;
 
48
 
 
49
 
 
50
enum {
 
51
        PROP_0,
 
52
        PROP_PATH,
 
53
        PROP_ADDRESS,
 
54
 
 
55
        LAST_PROP
 
56
};
 
57
 
 
58
/* Signals */
 
59
enum {
 
60
        INITIALIZED,
 
61
        DEVICE_ADDED,
 
62
        DEVICE_REMOVED,
 
63
        LAST_SIGNAL
 
64
};
 
65
static guint signals[LAST_SIGNAL] = { 0 };
 
66
 
 
67
const char *
 
68
nm_bluez_adapter_get_path (NMBluezAdapter *self)
 
69
{
 
70
        g_return_val_if_fail (NM_IS_BLUEZ_ADAPTER (self), NULL);
 
71
 
 
72
        return NM_BLUEZ_ADAPTER_GET_PRIVATE (self)->path;
 
73
}
 
74
 
 
75
const char *
 
76
nm_bluez_adapter_get_address (NMBluezAdapter *self)
 
77
{
 
78
        g_return_val_if_fail (NM_IS_BLUEZ_ADAPTER (self), NULL);
 
79
 
 
80
        return NM_BLUEZ_ADAPTER_GET_PRIVATE (self)->address;
 
81
}
 
82
 
 
83
gboolean
 
84
nm_bluez_adapter_get_initialized (NMBluezAdapter *self)
 
85
{
 
86
        g_return_val_if_fail (NM_IS_BLUEZ_ADAPTER (self), FALSE);
 
87
 
 
88
        return NM_BLUEZ_ADAPTER_GET_PRIVATE (self)->initialized;
 
89
}
 
90
 
 
91
GSList *
 
92
nm_bluez_adapter_get_devices (NMBluezAdapter *self)
 
93
{
 
94
        GSList *devices = NULL;
 
95
        GHashTableIter iter;
 
96
        NMBluezDevice *device;
 
97
 
 
98
        g_hash_table_iter_init (&iter, NM_BLUEZ_ADAPTER_GET_PRIVATE (self)->devices);
 
99
        while (g_hash_table_iter_next (&iter, NULL, (gpointer) &device)) {
 
100
                if (nm_bluez_device_get_usable (device))
 
101
                        devices = g_slist_append (devices, device);
 
102
        }
 
103
        return devices;
 
104
}
 
105
 
 
106
static void
 
107
device_usable (NMBluezDevice *device, GParamSpec *pspec, gpointer user_data)
 
108
{
 
109
        NMBluezAdapter *self = NM_BLUEZ_ADAPTER (user_data);
 
110
        gboolean usable = nm_bluez_device_get_usable (device);
 
111
 
 
112
        nm_log_dbg (LOGD_BT, "(%s): bluez device now %s",
 
113
                    nm_bluez_device_get_path (device),
 
114
                    usable ? "usable" : "unusable");
 
115
 
 
116
        if (usable) {
 
117
                nm_log_dbg (LOGD_BT, "(%s): bluez device address %s",
 
118
                                    nm_bluez_device_get_path (device),
 
119
                                    nm_bluez_device_get_address (device));
 
120
                g_signal_emit (self, signals[DEVICE_ADDED], 0, device);
 
121
        } else
 
122
                g_signal_emit (self, signals[DEVICE_REMOVED], 0, device);
 
123
}
 
124
 
 
125
static void
 
126
device_initialized (NMBluezDevice *device, gboolean success, gpointer user_data)
 
127
{
 
128
        NMBluezAdapter *self = NM_BLUEZ_ADAPTER (user_data);
 
129
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
130
 
 
131
        nm_log_dbg (LOGD_BT, "(%s): bluez device %s",
 
132
                    nm_bluez_device_get_path (device),
 
133
                    success ? "initialized" : "failed to initialize");
 
134
        if (!success)
 
135
                g_hash_table_remove (priv->devices, nm_bluez_device_get_path (device));
 
136
}
 
137
 
 
138
static void
 
139
device_created (DBusGProxy *proxy, const char *path, gpointer user_data)
 
140
{
 
141
        NMBluezAdapter *self = NM_BLUEZ_ADAPTER (user_data);
 
142
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
143
        NMBluezDevice *device;
 
144
 
 
145
        device = nm_bluez_device_new (path, priv->provider);
 
146
        g_signal_connect (device, "initialized", G_CALLBACK (device_initialized), self);
 
147
        g_signal_connect (device, "notify::usable", G_CALLBACK (device_usable), self);
 
148
        g_hash_table_insert (priv->devices, (gpointer) nm_bluez_device_get_path (device), device);
 
149
 
 
150
        nm_log_dbg (LOGD_BT, "(%s): new bluez device found", path);
 
151
}
 
152
 
 
153
static void
 
154
device_removed (DBusGProxy *proxy, const char *path, gpointer user_data)
 
155
{
 
156
        NMBluezAdapter *self = NM_BLUEZ_ADAPTER (user_data);
 
157
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
158
        NMBluezDevice *device;
 
159
 
 
160
        nm_log_dbg (LOGD_BT, "(%s): bluez device removed", path);
 
161
 
 
162
        device = g_hash_table_lookup (priv->devices, path);
 
163
        if (device) {
 
164
                g_object_ref (device);
 
165
                g_hash_table_remove (priv->devices, nm_bluez_device_get_path (device));
 
166
                g_signal_emit (self, signals[DEVICE_REMOVED], 0, device);
 
167
                g_object_unref (device);
 
168
        }
 
169
}
 
170
 
 
171
 
 
172
static void
 
173
get_properties_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
 
174
{
 
175
        NMBluezAdapter *self = NM_BLUEZ_ADAPTER (user_data);
 
176
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
177
        GHashTable *properties = NULL;
 
178
        GError *err = NULL;
 
179
        GValue *value;
 
180
        GPtrArray *devices;
 
181
        int i;
 
182
 
 
183
        if (!dbus_g_proxy_end_call (proxy, call, &err,
 
184
                                    DBUS_TYPE_G_MAP_OF_VARIANT, &properties,
 
185
                                    G_TYPE_INVALID)) {
 
186
                nm_log_warn (LOGD_BT, "bluez error getting adapter properties: %s",
 
187
                             err && err->message ? err->message : "(unknown)");
 
188
                g_error_free (err);
 
189
                goto done;
 
190
        }
 
191
 
 
192
        value = g_hash_table_lookup (properties, "Address");
 
193
        priv->address = value ? g_value_dup_string (value) : NULL;
 
194
 
 
195
        value = g_hash_table_lookup (properties, "Devices");
 
196
        devices = value ? g_value_get_boxed (value) : NULL;
 
197
 
 
198
        for (i = 0; devices && i < devices->len; i++)
 
199
                device_created (priv->proxy, g_ptr_array_index (devices, i), self);
 
200
 
 
201
        g_hash_table_unref (properties);
 
202
 
 
203
        priv->initialized = TRUE;
 
204
 
 
205
done:
 
206
        g_signal_emit (self, signals[INITIALIZED], 0, priv->initialized);
 
207
}
 
208
 
 
209
static void
 
210
query_properties (NMBluezAdapter *self)
 
211
{
 
212
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
213
        DBusGProxyCall *call;
 
214
 
 
215
        call = dbus_g_proxy_begin_call (priv->proxy, "GetProperties",
 
216
                                        get_properties_cb,
 
217
                                        self,
 
218
                                        NULL, G_TYPE_INVALID);
 
219
        if (!call) {
 
220
                nm_log_warn (LOGD_BT, "failed to request Bluetooth adapter properties for %s.",
 
221
                             priv->path);
 
222
        }
 
223
}
 
224
 
 
225
/***********************************************************/
 
226
 
 
227
NMBluezAdapter *
 
228
nm_bluez_adapter_new (const char *path, NMConnectionProvider *provider)
 
229
{
 
230
        NMBluezAdapter *self;
 
231
        NMBluezAdapterPrivate *priv;
 
232
        NMDBusManager *dbus_mgr;
 
233
        DBusGConnection *connection;
 
234
 
 
235
        self = (NMBluezAdapter *) g_object_new (NM_TYPE_BLUEZ_ADAPTER,
 
236
                                                NM_BLUEZ_ADAPTER_PATH, path,
 
237
                                                NULL);
 
238
        if (!self)
 
239
                return NULL;
 
240
 
 
241
        priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
242
 
 
243
        priv->provider = provider;
 
244
 
 
245
        dbus_mgr = nm_dbus_manager_get ();
 
246
        connection = nm_dbus_manager_get_connection (dbus_mgr);
 
247
 
 
248
        priv->proxy = dbus_g_proxy_new_for_name (connection,
 
249
                                                 BLUEZ_SERVICE,
 
250
                                                 priv->path,
 
251
                                                 BLUEZ_ADAPTER_INTERFACE);
 
252
        g_object_unref (dbus_mgr);
 
253
 
 
254
        dbus_g_proxy_add_signal (priv->proxy, "DeviceCreated",
 
255
                                 DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID);
 
256
        dbus_g_proxy_connect_signal (priv->proxy, "DeviceCreated",
 
257
                                     G_CALLBACK (device_created), self, NULL);
 
258
 
 
259
        dbus_g_proxy_add_signal (priv->proxy, "DeviceRemoved",
 
260
                                 DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID);
 
261
        dbus_g_proxy_connect_signal (priv->proxy, "DeviceRemoved",
 
262
                                     G_CALLBACK (device_removed), self, NULL);
 
263
 
 
264
        query_properties (self);
 
265
        return self;
 
266
}
 
267
 
 
268
static void
 
269
nm_bluez_adapter_init (NMBluezAdapter *self)
 
270
{
 
271
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
272
 
 
273
        priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
 
274
                                               NULL, g_object_unref);
 
275
}
 
276
 
 
277
static void
 
278
dispose (GObject *object)
 
279
{
 
280
        NMBluezAdapter *self = NM_BLUEZ_ADAPTER (object);
 
281
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (self);
 
282
        GHashTableIter iter;
 
283
        NMBluezDevice *device;
 
284
 
 
285
        g_hash_table_iter_init (&iter, priv->devices);
 
286
        while (g_hash_table_iter_next (&iter, NULL, (gpointer) &device))
 
287
                g_signal_emit (self, signals[DEVICE_REMOVED], 0, device);
 
288
        g_hash_table_remove_all (priv->devices);
 
289
 
 
290
        G_OBJECT_CLASS (nm_bluez_adapter_parent_class)->dispose (object);
 
291
}
 
292
 
 
293
static void
 
294
finalize (GObject *object)
 
295
{
 
296
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (object);
 
297
 
 
298
        g_hash_table_destroy (priv->devices);
 
299
        g_free (priv->address);
 
300
        g_free (priv->path);
 
301
        g_object_unref (priv->proxy);
 
302
 
 
303
        G_OBJECT_CLASS (nm_bluez_adapter_parent_class)->finalize (object);
 
304
}
 
305
 
 
306
static void
 
307
get_property (GObject *object, guint prop_id,
 
308
              GValue *value, GParamSpec *pspec)
 
309
{
 
310
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (object);
 
311
 
 
312
        switch (prop_id) {
 
313
        case PROP_PATH:
 
314
                g_value_set_string (value, priv->path);
 
315
                break;
 
316
        case PROP_ADDRESS:
 
317
                g_value_set_string (value, priv->address);
 
318
                break;
 
319
        default:
 
320
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
321
                break;
 
322
        }
 
323
}
 
324
 
 
325
static void
 
326
set_property (GObject *object, guint prop_id,
 
327
              const GValue *value, GParamSpec *pspec)
 
328
{
 
329
        NMBluezAdapterPrivate *priv = NM_BLUEZ_ADAPTER_GET_PRIVATE (object);
 
330
 
 
331
        switch (prop_id) {
 
332
        case PROP_PATH:
 
333
                /* construct only */
 
334
                priv->path = g_value_dup_string (value);
 
335
                break;
 
336
        default:
 
337
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
338
                break;
 
339
        }
 
340
}
 
341
 
 
342
static void
 
343
nm_bluez_adapter_class_init (NMBluezAdapterClass *config_class)
 
344
{
 
345
        GObjectClass *object_class = G_OBJECT_CLASS (config_class);
 
346
 
 
347
        g_type_class_add_private (config_class, sizeof (NMBluezAdapterPrivate));
 
348
 
 
349
        /* virtual methods */
 
350
        object_class->get_property = get_property;
 
351
        object_class->set_property = set_property;
 
352
        object_class->dispose = dispose;
 
353
        object_class->finalize = finalize;
 
354
 
 
355
        /* Properties */
 
356
        g_object_class_install_property
 
357
                (object_class, PROP_PATH,
 
358
                 g_param_spec_string (NM_BLUEZ_ADAPTER_PATH,
 
359
                                      "DBus Path",
 
360
                                      "DBus Path",
 
361
                                      NULL,
 
362
                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
363
 
 
364
        g_object_class_install_property
 
365
                (object_class, PROP_ADDRESS,
 
366
                 g_param_spec_string (NM_BLUEZ_ADAPTER_ADDRESS,
 
367
                                      "Address",
 
368
                                      "Address",
 
369
                                      NULL,
 
370
                                      G_PARAM_READABLE));
 
371
 
 
372
        /* Signals */
 
373
        signals[INITIALIZED] = g_signal_new ("initialized",
 
374
                                             G_OBJECT_CLASS_TYPE (object_class),
 
375
                                             G_SIGNAL_RUN_LAST,
 
376
                                             G_STRUCT_OFFSET (NMBluezAdapterClass, initialized),
 
377
                                             NULL, NULL,
 
378
                                             g_cclosure_marshal_VOID__BOOLEAN,
 
379
                                             G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
 
380
 
 
381
        signals[DEVICE_ADDED] = g_signal_new ("device-added",
 
382
                                              G_OBJECT_CLASS_TYPE (object_class),
 
383
                                              G_SIGNAL_RUN_LAST,
 
384
                                              G_STRUCT_OFFSET (NMBluezAdapterClass, device_added),
 
385
                                              NULL, NULL,
 
386
                                              g_cclosure_marshal_VOID__OBJECT,
 
387
                                              G_TYPE_NONE, 1, G_TYPE_OBJECT);
 
388
 
 
389
        signals[DEVICE_REMOVED] = g_signal_new ("device-removed",
 
390
                                                G_OBJECT_CLASS_TYPE (object_class),
 
391
                                                G_SIGNAL_RUN_LAST,
 
392
                                                G_STRUCT_OFFSET (NMBluezAdapterClass, device_removed),
 
393
                                                NULL, NULL,
 
394
                                                g_cclosure_marshal_VOID__OBJECT,
 
395
                                                G_TYPE_NONE, 1, G_TYPE_OBJECT);
 
396
}
 
397