~ubuntu-branches/ubuntu/precise/gnome-bluetooth/precise-updates

« back to all changes in this revision

Viewing changes to lib/bluetooth-killswitch.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2009-08-05 13:34:38 UTC
  • mto: (2.2.1 experimental) (1.4.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090805133438-791u4ywsppj71d9y
Tags: upstream-2.27.8
ImportĀ upstreamĀ versionĀ 2.27.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
 
6
 *  Copyright (C) 2006-2009  Bastien Nocera <hadess@hadess.net>
 
7
 *
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  (at your option) any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program; if not, write to the Free Software
 
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 *
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include <config.h>
 
27
#endif
 
28
 
 
29
#include <dbus/dbus-glib-lowlevel.h>
 
30
#include <hal/libhal.h>
 
31
 
 
32
#include "bluetooth-killswitch.h"
 
33
 
 
34
enum {
 
35
        STATE_CHANGED,
 
36
        LAST_SIGNAL
 
37
};
 
38
 
 
39
static int signals[LAST_SIGNAL] = { 0 };
 
40
 
 
41
#define BLUETOOTH_KILLSWITCH_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
 
42
                                BLUETOOTH_TYPE_KILLSWITCH, BluetoothKillswitchPrivate))
 
43
 
 
44
typedef struct _BluetoothIndKillswitch BluetoothIndKillswitch;
 
45
struct _BluetoothIndKillswitch {
 
46
        char *udi;
 
47
        DBusPendingCall *call;
 
48
        KillswitchState state, target_state;
 
49
};
 
50
 
 
51
typedef struct _BluetoothKillswitchPrivate BluetoothKillswitchPrivate;
 
52
struct _BluetoothKillswitchPrivate {
 
53
        LibHalContext *halctx;
 
54
        DBusConnection *connection;
 
55
        guint num_remaining_answers;
 
56
        GList *killswitches; /* a GList of BluetoothIndKillswitch */
 
57
};
 
58
 
 
59
G_DEFINE_TYPE(BluetoothKillswitch, bluetooth_killswitch, G_TYPE_OBJECT)
 
60
 
 
61
static int
 
62
power_to_state (int power)
 
63
{
 
64
        switch (power) {
 
65
        case 1: /* RFKILL_STATE_UNBLOCKED */
 
66
                return KILLSWITCH_STATE_UNBLOCKED;
 
67
        case 0: /* RFKILL_STATE_SOFT_BLOCKED */
 
68
        case 2: /* RFKILL_STATE_HARD_BLOCKED */
 
69
                return KILLSWITCH_STATE_SOFT_BLOCKED;
 
70
        default:
 
71
                g_warning ("Unknown power state %d, please file a bug at "PACKAGE_BUGREPORT, power);
 
72
                return KILLSWITCH_STATE_UNKNOWN;
 
73
        }
 
74
}
 
75
 
 
76
static void
 
77
setpower_reply (DBusPendingCall *call, void *user_data)
 
78
{
 
79
        BluetoothKillswitch *killswitch = BLUETOOTH_KILLSWITCH (user_data);
 
80
        BluetoothKillswitchPrivate *priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
81
        DBusMessage *reply;
 
82
        gint32 retval;
 
83
        GList *l;
 
84
 
 
85
        priv->num_remaining_answers--;
 
86
 
 
87
        reply = dbus_pending_call_steal_reply(call);
 
88
 
 
89
        if (dbus_message_get_args(reply, NULL, DBUS_TYPE_INT32, &retval,
 
90
                                  DBUS_TYPE_INVALID) == FALSE) {
 
91
                dbus_message_unref(reply);
 
92
                goto done;
 
93
        }
 
94
 
 
95
        /* Look for the killswitch */
 
96
        for (l = priv->killswitches; l ; l = l->next) {
 
97
                BluetoothIndKillswitch *ind = l->data;
 
98
                if (call != ind->call)
 
99
                        continue;
 
100
                ind->call = NULL;
 
101
                if (retval == 0)
 
102
                        ind->state = ind->target_state;
 
103
                break;
 
104
        }
 
105
 
 
106
        dbus_message_unref(reply);
 
107
        dbus_pending_call_unref (call);
 
108
 
 
109
done:
 
110
        if (priv->num_remaining_answers == 0)
 
111
                g_signal_emit (G_OBJECT (killswitch),
 
112
                               signals[STATE_CHANGED],
 
113
                               0, bluetooth_killswitch_get_state (killswitch));
 
114
}
 
115
 
 
116
static void
 
117
getpower_reply (DBusPendingCall *call, void *user_data)
 
118
{
 
119
        BluetoothKillswitch *killswitch = BLUETOOTH_KILLSWITCH (user_data);
 
120
        BluetoothKillswitchPrivate *priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
121
        DBusMessage *reply;
 
122
        GList *l;
 
123
        gint32 power;
 
124
 
 
125
        priv->num_remaining_answers--;
 
126
 
 
127
        reply = dbus_pending_call_steal_reply (call);
 
128
 
 
129
        if (dbus_message_get_args(reply, NULL, DBUS_TYPE_INT32, &power,
 
130
                                  DBUS_TYPE_INVALID) == FALSE) {
 
131
                dbus_message_unref(reply);
 
132
                goto done;
 
133
        }
 
134
 
 
135
        /* Look for the killswitch */
 
136
        for (l = priv->killswitches; l ; l = l->next) {
 
137
                BluetoothIndKillswitch *ind = l->data;
 
138
                if (call != ind->call)
 
139
                        continue;
 
140
                ind->state = power_to_state (power);
 
141
                ind->call = NULL;
 
142
                break;
 
143
        }
 
144
 
 
145
        dbus_message_unref(reply);
 
146
        dbus_pending_call_unref (call);
 
147
 
 
148
done:
 
149
        if (priv->num_remaining_answers == 0)
 
150
                g_signal_emit (G_OBJECT (killswitch),
 
151
                               signals[STATE_CHANGED],
 
152
                               0, bluetooth_killswitch_get_state (killswitch));
 
153
}
 
154
 
 
155
void
 
156
bluetooth_killswitch_update_state (BluetoothKillswitch *killswitch)
 
157
{
 
158
        BluetoothKillswitchPrivate *priv;
 
159
        GList *l;
 
160
 
 
161
        g_return_if_fail (BLUETOOTH_IS_KILLSWITCH (killswitch));
 
162
 
 
163
        priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
164
 
 
165
        if (priv->num_remaining_answers > 0)
 
166
                return;
 
167
 
 
168
        for (l = priv->killswitches ; l ; l = l->next) {
 
169
                BluetoothIndKillswitch *ind = l->data;
 
170
                DBusPendingCall *call;
 
171
                DBusMessage *message;
 
172
 
 
173
                /* Already checking status */
 
174
                if (ind->call != NULL)
 
175
                        continue;
 
176
 
 
177
                message = dbus_message_new_method_call ("org.freedesktop.Hal",
 
178
                                                        ind->udi,
 
179
                                                        "org.freedesktop.Hal.Device.KillSwitch",
 
180
                                                        "GetPower");
 
181
 
 
182
                if (message == NULL)
 
183
                        continue;
 
184
 
 
185
                if (dbus_connection_send_with_reply (priv->connection, message, &call, -1) == FALSE) {
 
186
                        dbus_message_unref(message);
 
187
                        continue;
 
188
                }
 
189
 
 
190
                priv->num_remaining_answers++;
 
191
                dbus_pending_call_set_notify (call, getpower_reply, killswitch, NULL);
 
192
 
 
193
                dbus_message_unref (message);
 
194
        }
 
195
}
 
196
 
 
197
void
 
198
bluetooth_killswitch_set_state (BluetoothKillswitch *killswitch, KillswitchState state)
 
199
{
 
200
        BluetoothKillswitchPrivate *priv;
 
201
        gboolean value;
 
202
        GList *l;
 
203
 
 
204
        g_return_if_fail (BLUETOOTH_IS_KILLSWITCH (killswitch));
 
205
        g_return_if_fail (state == KILLSWITCH_STATE_SOFT_BLOCKED || state == KILLSWITCH_STATE_UNBLOCKED);
 
206
 
 
207
        priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
208
 
 
209
        if (priv->num_remaining_answers > 0)
 
210
                return;
 
211
 
 
212
        value = (state == KILLSWITCH_STATE_UNBLOCKED);
 
213
 
 
214
        for (l = priv->killswitches ; l ; l = l->next) {
 
215
                BluetoothIndKillswitch *ind = l->data;
 
216
                DBusPendingCall *call;
 
217
                DBusMessage *message;
 
218
 
 
219
                message = dbus_message_new_method_call("org.freedesktop.Hal",
 
220
                                                       ind->udi,
 
221
                                                       "org.freedesktop.Hal.Device.KillSwitch",
 
222
                                                       "SetPower");
 
223
                if (message == NULL)
 
224
                        return;
 
225
 
 
226
                dbus_message_append_args(message,
 
227
                                         DBUS_TYPE_BOOLEAN, &value,
 
228
                                         DBUS_TYPE_INVALID);
 
229
 
 
230
                if (dbus_connection_send_with_reply(priv->connection, message,
 
231
                                                    &call, -1) == FALSE) {
 
232
                        dbus_message_unref(message);
 
233
                        return;
 
234
                }
 
235
 
 
236
                ind->call = call;
 
237
                ind->target_state = state;
 
238
                priv->num_remaining_answers++;
 
239
 
 
240
                dbus_pending_call_set_notify(call, setpower_reply, killswitch, NULL);
 
241
 
 
242
                dbus_message_unref(message);
 
243
        }
 
244
}
 
245
 
 
246
KillswitchState
 
247
bluetooth_killswitch_get_state (BluetoothKillswitch *killswitch)
 
248
{
 
249
        BluetoothKillswitchPrivate *priv;
 
250
        int state = KILLSWITCH_STATE_UNKNOWN;
 
251
        GList *l;
 
252
 
 
253
        g_return_val_if_fail (BLUETOOTH_IS_KILLSWITCH (killswitch), KILLSWITCH_STATE_UNKNOWN);
 
254
 
 
255
        priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
256
        if (priv->connection == NULL)
 
257
                return KILLSWITCH_STATE_UNKNOWN;
 
258
 
 
259
        for (l = priv->killswitches ; l ; l = l->next) {
 
260
                BluetoothIndKillswitch *ind = l->data;
 
261
 
 
262
                if (ind->state == KILLSWITCH_STATE_SOFT_BLOCKED) {
 
263
                        if (state == KILLSWITCH_STATE_UNKNOWN)
 
264
                                state = KILLSWITCH_STATE_SOFT_BLOCKED;
 
265
                        if (state != KILLSWITCH_STATE_SOFT_BLOCKED)
 
266
                                state = KILLSWITCH_STATE_MIXED;
 
267
                } else {
 
268
                        if (state == KILLSWITCH_STATE_UNKNOWN)
 
269
                                state = KILLSWITCH_STATE_UNBLOCKED;
 
270
                        if (state != KILLSWITCH_STATE_UNBLOCKED)
 
271
                                state = KILLSWITCH_STATE_MIXED;
 
272
                }
 
273
        }
 
274
 
 
275
        if (state == KILLSWITCH_STATE_UNKNOWN)
 
276
                return KILLSWITCH_STATE_UNKNOWN;
 
277
 
 
278
        return state;
 
279
}
 
280
 
 
281
gboolean
 
282
bluetooth_killswitch_has_killswitches (BluetoothKillswitch *killswitch)
 
283
{
 
284
        BluetoothKillswitchPrivate *priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
285
 
 
286
        g_return_val_if_fail (BLUETOOTH_IS_KILLSWITCH (killswitch), FALSE);
 
287
 
 
288
        return (priv->killswitches != NULL);
 
289
}
 
290
 
 
291
static void
 
292
add_killswitch (BluetoothKillswitch *killswitch, const char *udi)
 
293
{
 
294
        BluetoothKillswitchPrivate *priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
295
        DBusPendingCall *call;
 
296
        DBusMessage *message;
 
297
        BluetoothIndKillswitch *ind;
 
298
        char *type;
 
299
 
 
300
        type = libhal_device_get_property_string (priv->halctx,
 
301
                                                  udi,
 
302
                                                  "killswitch.type",
 
303
                                                  NULL);
 
304
        if (type == NULL || g_str_equal(type, "bluetooth") == FALSE) {
 
305
                g_free (type);
 
306
                return;
 
307
        }
 
308
        g_free (type);
 
309
 
 
310
        message = dbus_message_new_method_call ("org.freedesktop.Hal",
 
311
                                                udi,
 
312
                                                "org.freedesktop.Hal.Device.KillSwitch",
 
313
                                                "GetPower");
 
314
 
 
315
        if (message == NULL)
 
316
                return;
 
317
 
 
318
        if (dbus_connection_send_with_reply (priv->connection, message, &call, -1) == FALSE) {
 
319
                dbus_message_unref(message);
 
320
                return;
 
321
        }
 
322
 
 
323
        ind = g_new0 (BluetoothIndKillswitch, 1);
 
324
        ind->udi = g_strdup (udi);
 
325
        ind->call = call;
 
326
        priv->killswitches = g_list_append (priv->killswitches, ind);
 
327
        priv->num_remaining_answers++;
 
328
 
 
329
        dbus_pending_call_set_notify (call, getpower_reply, killswitch, NULL);
 
330
 
 
331
        dbus_message_unref (message);
 
332
}
 
333
 
 
334
static void
 
335
bluetooth_killswitch_init (BluetoothKillswitch *killswitch)
 
336
{
 
337
        BluetoothKillswitchPrivate *priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (killswitch);
 
338
        char **list;
 
339
        int num;
 
340
 
 
341
        priv->connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
 
342
        if (priv->connection == NULL)
 
343
                return;
 
344
 
 
345
        priv->halctx = libhal_ctx_new();
 
346
        if (priv->halctx == NULL) {
 
347
                dbus_connection_unref (priv->connection);
 
348
                return;
 
349
        }
 
350
 
 
351
        if (libhal_ctx_set_dbus_connection(priv->halctx, priv->connection) == FALSE) {
 
352
                libhal_ctx_free(priv->halctx);
 
353
                dbus_connection_unref (priv->connection);
 
354
                priv->halctx = NULL;
 
355
                return;
 
356
        }
 
357
 
 
358
        if (libhal_ctx_init(priv->halctx, NULL) == FALSE) {
 
359
                g_printerr("Couldn't init HAL context\n");
 
360
                dbus_connection_unref (priv->connection);
 
361
                libhal_ctx_free(priv->halctx);
 
362
                priv->halctx = NULL;
 
363
                return;
 
364
        }
 
365
 
 
366
        list = libhal_find_device_by_capability(priv->halctx, "killswitch", &num, NULL);
 
367
        if (list) {
 
368
                char **tmp = list;
 
369
 
 
370
                while (*tmp) {
 
371
                        add_killswitch (killswitch, *tmp);
 
372
                        tmp++;
 
373
                }
 
374
 
 
375
                libhal_free_string_array (list);
 
376
        }
 
377
}
 
378
 
 
379
static void
 
380
free_ind_killswitch (BluetoothIndKillswitch *ind)
 
381
{
 
382
        g_free (ind->udi);
 
383
}
 
384
 
 
385
static void
 
386
bluetooth_killswitch_finalize (GObject *object)
 
387
{
 
388
        BluetoothKillswitchPrivate *priv = BLUETOOTH_KILLSWITCH_GET_PRIVATE (object);
 
389
 
 
390
        if (priv->halctx != NULL) {
 
391
                libhal_ctx_shutdown(priv->halctx, NULL);
 
392
                libhal_ctx_free(priv->halctx);
 
393
                priv->halctx = NULL;
 
394
        }
 
395
 
 
396
        if (priv->connection != NULL) {
 
397
                dbus_connection_unref(priv->connection);
 
398
                priv->connection = NULL;
 
399
        }
 
400
 
 
401
        g_list_foreach (priv->killswitches, (GFunc) free_ind_killswitch, NULL);
 
402
        g_list_free (priv->killswitches);
 
403
        priv->killswitches = NULL;
 
404
 
 
405
        G_OBJECT_CLASS(bluetooth_killswitch_parent_class)->finalize(object);
 
406
}
 
407
 
 
408
static void
 
409
bluetooth_killswitch_class_init(BluetoothKillswitchClass *klass)
 
410
{
 
411
        GObjectClass *object_class = (GObjectClass *) klass;
 
412
 
 
413
        g_type_class_add_private(klass, sizeof(BluetoothKillswitchPrivate));
 
414
        object_class->finalize = bluetooth_killswitch_finalize;
 
415
 
 
416
        signals[STATE_CHANGED] =
 
417
                g_signal_new ("state-changed",
 
418
                              G_TYPE_FROM_CLASS (klass),
 
419
                              G_SIGNAL_RUN_LAST,
 
420
                              G_STRUCT_OFFSET (BluetoothKillswitchClass, state_changed),
 
421
                              NULL, NULL,
 
422
                              g_cclosure_marshal_VOID__INT,
 
423
                              G_TYPE_NONE, 1, G_TYPE_INT);
 
424
 
 
425
}
 
426
 
 
427
BluetoothKillswitch *
 
428
bluetooth_killswitch_new (void)
 
429
{
 
430
        return BLUETOOTH_KILLSWITCH(g_object_new (BLUETOOTH_TYPE_KILLSWITCH, NULL));
 
431
}
 
432