~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/network/rfkill-glib.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  gnome-bluetooth - Bluetooth integration for GNOME
 
4
 *
 
5
 *  Copyright (C) 2012  Bastien Nocera <hadess@hadess.net>
 
6
 *
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 *
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include <config.h>
 
26
#endif
 
27
 
 
28
#include <errno.h>
 
29
#include <unistd.h>
 
30
#include <sys/types.h>
 
31
#include <sys/stat.h>
 
32
#include <fcntl.h>
 
33
#include <string.h>
 
34
 
 
35
#include <glib.h>
 
36
 
 
37
#include "rfkill-glib.h"
 
38
 
 
39
enum {
 
40
        CHANGED,
 
41
        LAST_SIGNAL
 
42
};
 
43
 
 
44
static int signals[LAST_SIGNAL] = { 0 };
 
45
 
 
46
#define CC_RFKILL_GLIB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
 
47
                                CC_RFKILL_TYPE_GLIB, CcRfkillGlibPrivate))
 
48
 
 
49
struct CcRfkillGlibPrivate {
 
50
        int fd;
 
51
        GIOChannel *channel;
 
52
        guint watch_id;
 
53
};
 
54
 
 
55
G_DEFINE_TYPE(CcRfkillGlib, cc_rfkill_glib, G_TYPE_OBJECT)
 
56
 
 
57
int
 
58
cc_rfkill_glib_send_event (CcRfkillGlib *rfkill, struct rfkill_event *event)
 
59
{
 
60
        g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1);
 
61
        g_return_val_if_fail (rfkill->priv->fd > 0, -1);
 
62
 
 
63
        return write (rfkill->priv->fd, event, sizeof(struct rfkill_event));
 
64
}
 
65
 
 
66
static const char *
 
67
type_to_string (unsigned int type)
 
68
{
 
69
        switch (type) {
 
70
        case RFKILL_TYPE_ALL:
 
71
                return "ALL";
 
72
        case RFKILL_TYPE_WLAN:
 
73
                return "WLAN";
 
74
        case RFKILL_TYPE_BLUETOOTH:
 
75
                return "RFKILL";
 
76
        case RFKILL_TYPE_UWB:
 
77
                return "UWB";
 
78
        case RFKILL_TYPE_WIMAX:
 
79
                return "WIMAX";
 
80
        case RFKILL_TYPE_WWAN:
 
81
                return "WWAN";
 
82
        default:
 
83
                return "UNKNOWN";
 
84
        }
 
85
}
 
86
 
 
87
static const char *
 
88
op_to_string (unsigned int op)
 
89
{
 
90
        switch (op) {
 
91
        case RFKILL_OP_ADD:
 
92
                return "ADD";
 
93
        case RFKILL_OP_DEL:
 
94
                return "DEL";
 
95
        case RFKILL_OP_CHANGE:
 
96
                return "CHANGE";
 
97
        case RFKILL_OP_CHANGE_ALL:
 
98
                return "CHANGE_ALL";
 
99
        default:
 
100
                g_assert_not_reached ();
 
101
        }
 
102
}
 
103
 
 
104
static void
 
105
print_event (struct rfkill_event *event)
 
106
{
 
107
        g_debug ("RFKILL event: idx %u type %u (%s) op %u (%s) soft %u hard %u",
 
108
                 event->idx,
 
109
                 event->type, type_to_string (event->type),
 
110
                 event->op, op_to_string (event->op),
 
111
                 event->soft, event->hard);
 
112
}
 
113
 
 
114
static void
 
115
emit_changed_signal_and_free (CcRfkillGlib *rfkill,
 
116
                              GList      *events)
 
117
{
 
118
        if (events == NULL)
 
119
                return;
 
120
 
 
121
        g_signal_emit (G_OBJECT (rfkill),
 
122
                       signals[CHANGED],
 
123
                       0, events);
 
124
        g_list_free_full (events, g_free);
 
125
}
 
126
 
 
127
static gboolean
 
128
event_cb (GIOChannel   *source,
 
129
          GIOCondition  condition,
 
130
          CcRfkillGlib   *rfkill)
 
131
{
 
132
        GList *events;
 
133
 
 
134
        events = NULL;
 
135
 
 
136
        if (condition & G_IO_IN) {
 
137
                GIOStatus status;
 
138
                struct rfkill_event event;
 
139
                gsize read;
 
140
 
 
141
                status = g_io_channel_read_chars (source,
 
142
                                                  (char *) &event,
 
143
                                                  sizeof(event),
 
144
                                                  &read,
 
145
                                                  NULL);
 
146
 
 
147
                while (status == G_IO_STATUS_NORMAL && read == sizeof(event)) {
 
148
                        struct rfkill_event *event_ptr;
 
149
 
 
150
                        print_event (&event);
 
151
 
 
152
                        event_ptr = g_memdup (&event, sizeof(event));
 
153
                        events = g_list_prepend (events, event_ptr);
 
154
 
 
155
                        status = g_io_channel_read_chars (source,
 
156
                                                          (char *) &event,
 
157
                                                          sizeof(event),
 
158
                                                          &read,
 
159
                                                          NULL);
 
160
                }
 
161
                events = g_list_reverse (events);
 
162
        } else {
 
163
                g_debug ("something else happened");
 
164
                return FALSE;
 
165
        }
 
166
 
 
167
        emit_changed_signal_and_free (rfkill, events);
 
168
 
 
169
        return TRUE;
 
170
}
 
171
 
 
172
static void
 
173
cc_rfkill_glib_init (CcRfkillGlib *rfkill)
 
174
{
 
175
        CcRfkillGlibPrivate *priv;
 
176
 
 
177
        priv = CC_RFKILL_GLIB_GET_PRIVATE (rfkill);
 
178
        rfkill->priv = priv;
 
179
        rfkill->priv->fd = -1;
 
180
}
 
181
 
 
182
int
 
183
cc_rfkill_glib_open (CcRfkillGlib *rfkill)
 
184
{
 
185
        CcRfkillGlibPrivate *priv;
 
186
        int fd;
 
187
        int ret;
 
188
        GList *events;
 
189
 
 
190
        g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1);
 
191
        g_return_val_if_fail (rfkill->priv->fd == -1, -1);
 
192
 
 
193
        priv = rfkill->priv;
 
194
 
 
195
        fd = open("/dev/rfkill", O_RDWR);
 
196
        if (fd < 0) {
 
197
                if (errno == EACCES)
 
198
                        g_warning ("Could not open RFKILL control device, please verify your installation");
 
199
                return fd;
 
200
        }
 
201
 
 
202
        ret = fcntl(fd, F_SETFL, O_NONBLOCK);
 
203
        if (ret < 0) {
 
204
                g_debug ("Can't set RFKILL control device to non-blocking");
 
205
                close(fd);
 
206
                return ret;
 
207
        }
 
208
 
 
209
        events = NULL;
 
210
 
 
211
        while (1) {
 
212
                struct rfkill_event event;
 
213
                struct rfkill_event *event_ptr;
 
214
                ssize_t len;
 
215
 
 
216
                len = read(fd, &event, sizeof(event));
 
217
                if (len < 0) {
 
218
                        if (errno == EAGAIN)
 
219
                                break;
 
220
                        g_debug ("Reading of RFKILL events failed");
 
221
                        break;
 
222
                }
 
223
 
 
224
                if (len != RFKILL_EVENT_SIZE_V1) {
 
225
                        g_warning ("Wrong size of RFKILL event\n");
 
226
                        continue;
 
227
                }
 
228
 
 
229
                if (event.op != RFKILL_OP_ADD)
 
230
                        continue;
 
231
 
 
232
                g_debug ("Read killswitch of type '%s' (idx=%d): soft %d hard %d",
 
233
                         type_to_string (event.type),
 
234
                         event.idx, event.soft, event.hard);
 
235
 
 
236
                event_ptr = g_memdup (&event, sizeof(event));
 
237
                events = g_list_prepend (events, event_ptr);
 
238
        }
 
239
 
 
240
        /* Setup monitoring */
 
241
        priv->fd = fd;
 
242
        priv->channel = g_io_channel_unix_new (priv->fd);
 
243
        priv->watch_id = g_io_add_watch (priv->channel,
 
244
                                         G_IO_IN | G_IO_HUP | G_IO_ERR,
 
245
                                         (GIOFunc) event_cb,
 
246
                                         rfkill);
 
247
 
 
248
        events = g_list_reverse (events);
 
249
        emit_changed_signal_and_free (rfkill, events);
 
250
 
 
251
        return fd;
 
252
}
 
253
 
 
254
static void
 
255
cc_rfkill_glib_finalize (GObject *object)
 
256
{
 
257
        CcRfkillGlib *rfkill;
 
258
        CcRfkillGlibPrivate *priv;
 
259
 
 
260
        rfkill = CC_RFKILL_GLIB (object);
 
261
        priv = rfkill->priv;
 
262
 
 
263
        /* cleanup monitoring */
 
264
        if (priv->watch_id > 0) {
 
265
                g_source_remove (priv->watch_id);
 
266
                priv->watch_id = 0;
 
267
                g_io_channel_shutdown (priv->channel, FALSE, NULL);
 
268
                g_io_channel_unref (priv->channel);
 
269
        }
 
270
        close(priv->fd);
 
271
        priv->fd = -1;
 
272
 
 
273
        G_OBJECT_CLASS(cc_rfkill_glib_parent_class)->finalize(object);
 
274
}
 
275
 
 
276
static void
 
277
cc_rfkill_glib_class_init(CcRfkillGlibClass *klass)
 
278
{
 
279
        GObjectClass *object_class = (GObjectClass *) klass;
 
280
 
 
281
        g_type_class_add_private(klass, sizeof(CcRfkillGlibPrivate));
 
282
        object_class->finalize = cc_rfkill_glib_finalize;
 
283
 
 
284
        signals[CHANGED] =
 
285
                g_signal_new ("changed",
 
286
                              G_TYPE_FROM_CLASS (klass),
 
287
                              G_SIGNAL_RUN_LAST,
 
288
                              G_STRUCT_OFFSET (CcRfkillGlibClass, changed),
 
289
                              NULL, NULL,
 
290
                              NULL,
 
291
                              G_TYPE_NONE, 1, G_TYPE_POINTER);
 
292
 
 
293
}
 
294
 
 
295
CcRfkillGlib *
 
296
cc_rfkill_glib_new (void)
 
297
{
 
298
        return CC_RFKILL_GLIB (g_object_new (CC_RFKILL_TYPE_GLIB, NULL));
 
299
}