e21b2a15e34ee885cb54fbe5de6e1150d68389fa
[platform/upstream/connman.git] / src / rfkill.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <stdint.h>
33
34 #include "connman.h"
35
36 enum rfkill_type {
37         RFKILL_TYPE_ALL = 0,
38         RFKILL_TYPE_WLAN,
39         RFKILL_TYPE_BLUETOOTH,
40         RFKILL_TYPE_UWB,
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_WWAN:
70                 return CONNMAN_SERVICE_TYPE_CELLULAR;
71         }
72
73         return CONNMAN_SERVICE_TYPE_UNKNOWN;
74 }
75
76 static enum rfkill_type convert_service_type(enum connman_service_type type)
77 {
78         switch (type) {
79         case CONNMAN_SERVICE_TYPE_WIFI:
80                 return RFKILL_TYPE_WLAN;
81         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
82                 return RFKILL_TYPE_BLUETOOTH;
83         case CONNMAN_SERVICE_TYPE_CELLULAR:
84                 return RFKILL_TYPE_WWAN;
85         case CONNMAN_SERVICE_TYPE_GPS:
86                 return RFKILL_TYPE_GPS;
87         case CONNMAN_SERVICE_TYPE_SYSTEM:
88         case CONNMAN_SERVICE_TYPE_ETHERNET:
89         case CONNMAN_SERVICE_TYPE_VPN:
90         case CONNMAN_SERVICE_TYPE_GADGET:
91         case CONNMAN_SERVICE_TYPE_UNKNOWN:
92                 return NUM_RFKILL_TYPES;
93         }
94
95         return NUM_RFKILL_TYPES;
96 }
97
98 static GIOStatus rfkill_process(GIOChannel *chan)
99 {
100         unsigned char buf[32];
101         struct rfkill_event *event = (void *) buf;
102         enum connman_service_type type;
103         gsize len;
104         GIOStatus status;
105
106         DBG("");
107
108         memset(buf, 0, sizeof(buf));
109
110         status = g_io_channel_read_chars(chan, (gchar *) buf,
111                         sizeof(struct rfkill_event), &len, NULL);
112
113         if (status != G_IO_STATUS_NORMAL)
114                 return status;
115
116         if (len != sizeof(struct rfkill_event))
117                 return status;
118
119         DBG("idx %u type %u op %u soft %u hard %u", event->idx,
120                                                 event->type, event->op,
121                                                 event->soft, event->hard);
122
123         type = convert_type(event->type);
124
125         switch (event->op) {
126         case RFKILL_OP_ADD:
127                 __connman_technology_add_rfkill(event->idx, type,
128                                                 event->soft, event->hard);
129                 break;
130         case RFKILL_OP_DEL:
131                 __connman_technology_remove_rfkill(event->idx, type);
132                 break;
133         case RFKILL_OP_CHANGE:
134                 __connman_technology_update_rfkill(event->idx, type,
135                                                 event->soft, event->hard);
136                 break;
137         default:
138                 break;
139         }
140
141         return status;
142 }
143
144 static gboolean rfkill_event(GIOChannel *chan,
145                                 GIOCondition cond, gpointer data)
146 {
147         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
148                 return FALSE;
149
150         if (rfkill_process(chan) == G_IO_STATUS_ERROR)
151                 return FALSE;
152
153         return TRUE;
154 }
155
156 static GIOChannel *channel = NULL;
157
158 int __connman_rfkill_block(enum connman_service_type type, connman_bool_t block)
159 {
160         uint8_t rfkill_type;
161         struct rfkill_event event;
162         ssize_t len;
163         int fd, err;
164
165         DBG("type %d block %d", type, block);
166
167         rfkill_type = convert_service_type(type);
168         if (rfkill_type == NUM_RFKILL_TYPES)
169                 return -EINVAL;
170
171         fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
172         if (fd < 0)
173                 return fd;
174
175         memset(&event, 0, sizeof(event));
176         event.op = RFKILL_OP_CHANGE_ALL;
177         event.type = rfkill_type;
178         event.soft = block;
179
180         len = write(fd, &event, sizeof(event));
181         if (len < 0) {
182                 connman_error("Failed to change RFKILL state");
183                 err = len;
184         } else
185                 err = 0;
186
187         close(fd);
188
189         return err;
190 }
191
192 int __connman_rfkill_init(void)
193 {
194         GIOFlags flags;
195         int fd;
196
197         DBG("");
198
199         fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
200         if (fd < 0) {
201                 connman_error("Failed to open RFKILL control device");
202                 return -EIO;
203         }
204
205         channel = g_io_channel_unix_new(fd);
206         g_io_channel_set_close_on_unref(channel, TRUE);
207
208         g_io_channel_set_encoding(channel, NULL, NULL);
209         g_io_channel_set_buffered(channel, FALSE);
210
211         flags = g_io_channel_get_flags(channel);
212         flags |= G_IO_FLAG_NONBLOCK;
213         g_io_channel_set_flags(channel, flags, NULL);
214
215         /* Process current RFKILL events sent on device open */
216         while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
217
218         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
219                                                         rfkill_event, NULL);
220
221         return 0;
222 }
223
224 void __connman_rfkill_cleanup(void)
225 {
226         DBG("");
227
228         if (channel == NULL)
229                 return;
230
231         g_io_channel_shutdown(channel, TRUE, NULL);
232         g_io_channel_unref(channel);
233
234         channel = NULL;
235 }