core: Add O_CLOEXEC to open()
[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         RFKILL_TYPE_GPS,
43         RFKILL_TYPE_FM,
44         NUM_RFKILL_TYPES,
45 };
46
47 enum rfkill_operation {
48         RFKILL_OP_ADD = 0,
49         RFKILL_OP_DEL,
50         RFKILL_OP_CHANGE,
51         RFKILL_OP_CHANGE_ALL,
52 };
53
54 struct rfkill_event {
55         uint32_t idx;
56         uint8_t  type;
57         uint8_t  op;
58         uint8_t  soft;
59         uint8_t  hard;
60 };
61
62 static enum connman_service_type convert_type(uint8_t type)
63 {
64         switch (type) {
65         case RFKILL_TYPE_WLAN:
66                 return CONNMAN_SERVICE_TYPE_WIFI;
67         case RFKILL_TYPE_BLUETOOTH:
68                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
69         case RFKILL_TYPE_WIMAX:
70                 return CONNMAN_SERVICE_TYPE_WIMAX;
71         case RFKILL_TYPE_WWAN:
72                 return CONNMAN_SERVICE_TYPE_CELLULAR;
73         }
74
75         return CONNMAN_SERVICE_TYPE_UNKNOWN;
76 }
77
78 static enum rfkill_type convert_service_type(enum connman_service_type type)
79 {
80         switch (type) {
81         case CONNMAN_SERVICE_TYPE_WIFI:
82                 return RFKILL_TYPE_WLAN;
83         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
84                 return RFKILL_TYPE_BLUETOOTH;
85         case CONNMAN_SERVICE_TYPE_WIMAX:
86                 return RFKILL_TYPE_WIMAX;
87         case CONNMAN_SERVICE_TYPE_CELLULAR:
88                 return RFKILL_TYPE_WWAN;
89         case CONNMAN_SERVICE_TYPE_GPS:
90                 return RFKILL_TYPE_GPS;
91         case CONNMAN_SERVICE_TYPE_SYSTEM:
92         case CONNMAN_SERVICE_TYPE_ETHERNET:
93         case CONNMAN_SERVICE_TYPE_VPN:
94         case CONNMAN_SERVICE_TYPE_GADGET:
95         case CONNMAN_SERVICE_TYPE_UNKNOWN:
96                 return NUM_RFKILL_TYPES;
97         }
98
99         return NUM_RFKILL_TYPES;
100 }
101
102 static GIOStatus rfkill_process(GIOChannel *chan)
103 {
104         unsigned char buf[32];
105         struct rfkill_event *event = (void *) buf;
106         enum connman_service_type type;
107         gsize len;
108         GIOStatus status;
109
110         DBG("");
111
112         memset(buf, 0, sizeof(buf));
113
114         status = g_io_channel_read_chars(chan, (gchar *) buf,
115                         sizeof(struct rfkill_event), &len, NULL);
116
117         if (status != G_IO_STATUS_NORMAL)
118                 return status;
119
120         if (len != sizeof(struct rfkill_event))
121                 return status;
122
123         DBG("idx %u type %u op %u soft %u hard %u", event->idx,
124                                                 event->type, event->op,
125                                                 event->soft, event->hard);
126
127         type = convert_type(event->type);
128
129         switch (event->op) {
130         case RFKILL_OP_ADD:
131                 __connman_technology_add_rfkill(event->idx, type,
132                                                 event->soft, event->hard);
133                 break;
134         case RFKILL_OP_DEL:
135                 __connman_technology_remove_rfkill(event->idx, type);
136                 break;
137         case RFKILL_OP_CHANGE:
138                 __connman_technology_update_rfkill(event->idx, type,
139                                                 event->soft, event->hard);
140                 break;
141         default:
142                 break;
143         }
144
145         return status;
146 }
147
148 static gboolean rfkill_event(GIOChannel *chan,
149                                 GIOCondition cond, gpointer data)
150 {
151         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
152                 return FALSE;
153
154         if (rfkill_process(chan) == G_IO_STATUS_ERROR)
155                 return FALSE;
156
157         return TRUE;
158 }
159
160 static GIOChannel *channel = NULL;
161
162 int __connman_rfkill_block(enum connman_service_type type, connman_bool_t block)
163 {
164         uint8_t rfkill_type;
165         struct rfkill_event event;
166         ssize_t len;
167         int fd;
168
169         DBG("type %d block %d", type, block);
170
171         rfkill_type = convert_service_type(type);
172         if (rfkill_type == NUM_RFKILL_TYPES)
173                 return -EINVAL;
174
175         fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
176         if (fd < 0)
177                 return fd;
178
179         memset(&event, 0, sizeof(event));
180         event.op = RFKILL_OP_CHANGE_ALL;
181         event.type = rfkill_type;
182         event.soft = block;
183
184         len = write(fd, &event, sizeof(event));
185         if (len < 0)
186                 connman_error("Failed to change RFKILL state");
187
188         close(fd);
189
190         return 0;
191 }
192
193 int __connman_rfkill_init(void)
194 {
195         GIOFlags flags;
196         int fd;
197
198         DBG("");
199
200         fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
201         if (fd < 0) {
202                 connman_error("Failed to open RFKILL control device");
203                 return -EIO;
204         }
205
206         channel = g_io_channel_unix_new(fd);
207         g_io_channel_set_close_on_unref(channel, TRUE);
208
209         g_io_channel_set_encoding(channel, NULL, NULL);
210         g_io_channel_set_buffered(channel, FALSE);
211
212         flags = g_io_channel_get_flags(channel);
213         flags |= G_IO_FLAG_NONBLOCK;
214         g_io_channel_set_flags(channel, flags, NULL);
215
216         /* Process current RFKILL events sent on device open */
217         while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
218
219         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
220                                                         rfkill_event, NULL);
221
222         return 0;
223 }
224
225 void __connman_rfkill_cleanup(void)
226 {
227         DBG("");
228
229         if (channel == NULL)
230                 return;
231
232         g_io_channel_shutdown(channel, TRUE, NULL);
233         g_io_channel_unref(channel);
234
235         channel = NULL;
236 }