Process RFKILL events after opening device
[framework/connectivity/connman.git] / src / rfkill.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 #include <stdint.h>
32
33 #include "connman.h"
34
35 enum rfkill_type {
36         RFKILL_TYPE_ALL = 0,
37         RFKILL_TYPE_WLAN,
38         RFKILL_TYPE_BLUETOOTH,
39         RFKILL_TYPE_UWB,
40         RFKILL_TYPE_WIMAX,
41         RFKILL_TYPE_WWAN,
42 };
43
44 enum rfkill_operation {
45         RFKILL_OP_ADD = 0,
46         RFKILL_OP_DEL,
47         RFKILL_OP_CHANGE,
48         RFKILL_OP_CHANGE_ALL,
49 };
50
51 struct rfkill_event {
52         uint32_t idx;
53         uint8_t  type;
54         uint8_t  op;
55         uint8_t  soft;
56         uint8_t  hard;
57 };
58
59 static GIOStatus rfkill_process(GIOChannel *chan)
60 {
61         GIOStatus status = G_IO_STATUS_NORMAL;
62         unsigned char buf[32];
63         struct rfkill_event *event = (void *) buf;
64         char sysname[32];
65         connman_bool_t blocked;
66         gsize len;
67
68         DBG("");
69
70         memset(buf, 0, sizeof(buf));
71
72         status = g_io_channel_read_chars(chan, (gchar *) buf,
73                         sizeof(struct rfkill_event), &len, NULL);
74
75         if (status != G_IO_STATUS_NORMAL)
76                 return status;
77
78         if (len != sizeof(struct rfkill_event))
79                 return status;
80
81         DBG("idx %u type %u op %u soft %u hard %u",
82                                 event->idx, event->type, event->op,
83                                         event->soft, event->hard);
84
85         snprintf(sysname, sizeof(sysname) - 1, "rfkill%d", event->idx);
86
87         blocked = (event->soft || event->hard) ? TRUE : FALSE;
88
89         switch (event->type) {
90         case RFKILL_TYPE_ALL:
91         case RFKILL_TYPE_WLAN:
92                 __connman_udev_rfkill(sysname, blocked);
93                 break;
94         default:
95                 break;
96         }
97
98         return status;
99 }
100
101 static gboolean rfkill_event(GIOChannel *chan,
102                                 GIOCondition cond, gpointer data)
103 {
104         GIOStatus status;
105
106         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
107                 return FALSE;
108
109         status = rfkill_process(chan);
110         if (status == G_IO_STATUS_ERROR)
111                 return FALSE;
112
113         return TRUE;
114 }
115
116 static GIOChannel *channel = NULL;
117
118 int __connman_rfkill_init(void)
119 {
120         GIOFlags flags;
121         int fd;
122
123         DBG("");
124
125         fd = open("/dev/rfkill", O_RDWR);
126         if (fd < 0) {
127                 connman_error("Failed to open RFKILL control device");
128                 return -EIO;
129         }
130
131         channel = g_io_channel_unix_new(fd);
132         g_io_channel_set_close_on_unref(channel, TRUE);
133
134         flags = g_io_channel_get_flags(channel);
135         flags |= G_IO_FLAG_NONBLOCK;
136         g_io_channel_set_flags(channel, flags, NULL);
137
138         /* Process current RFKILL events sent on device open */
139         while (rfkill_process(channel) == G_IO_STATUS_NORMAL)
140                 ;
141
142         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
143                                                         rfkill_event, NULL);
144
145         return 0;
146 }
147
148 void __connman_rfkill_cleanup(void)
149 {
150         DBG("");
151
152         if (channel == NULL)
153                 return;
154
155         g_io_channel_shutdown(channel, TRUE, NULL);
156         g_io_channel_unref(channel);
157
158         channel = NULL;
159 }