efe979c7d9656ef8c5bd0327161b8b9998986d78
[connman] / src / rfkill.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "connman.h"
33
34 static gboolean rfkill_event(GIOChannel *chan,
35                                 GIOCondition cond, gpointer data)
36 {
37         unsigned char buf[32];
38         gsize len;
39         GIOError err;
40
41         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
42                 return FALSE;
43
44         memset(buf, 0, sizeof(buf));
45
46         err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
47         if (err) {
48                 if (err == G_IO_ERROR_AGAIN)
49                         return TRUE;
50                 return FALSE;
51         }
52
53         /* process RFKILL event */
54
55         return TRUE;
56 }
57
58 static GIOChannel *channel = NULL;
59
60 int __connman_rfkill_init(void)
61 {
62         int fd;
63
64         DBG("");
65
66         fd = open("/dev/rfkill", O_RDWR);
67         if (fd < 0) {
68                 connman_error("Failed to open RFKILL control device");
69                 return -EIO;
70         }
71
72         channel = g_io_channel_unix_new(fd);
73         g_io_channel_set_close_on_unref(channel, TRUE);
74
75         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
76                                                         rfkill_event, NULL);
77
78         return 0;
79 }
80
81 void __connman_rfkill_cleanup(void)
82 {
83         DBG("");
84
85         if (channel == NULL)
86                 return;
87
88         g_io_channel_shutdown(channel, TRUE, NULL);
89         g_io_channel_unref(channel);
90
91         channel = NULL;
92 }