technology: Remove global rfkill table
[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 };
45
46 enum rfkill_operation {
47         RFKILL_OP_ADD = 0,
48         RFKILL_OP_DEL,
49         RFKILL_OP_CHANGE,
50         RFKILL_OP_CHANGE_ALL,
51 };
52
53 struct rfkill_event {
54         uint32_t idx;
55         uint8_t  type;
56         uint8_t  op;
57         uint8_t  soft;
58         uint8_t  hard;
59 };
60
61 static enum connman_service_type convert_type(uint8_t type)
62 {
63         switch (type) {
64         case RFKILL_TYPE_WLAN:
65                 return CONNMAN_SERVICE_TYPE_WIFI;
66         case RFKILL_TYPE_BLUETOOTH:
67                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
68         case RFKILL_TYPE_WIMAX:
69                 return CONNMAN_SERVICE_TYPE_WIMAX;
70         case RFKILL_TYPE_WWAN:
71                 return CONNMAN_SERVICE_TYPE_CELLULAR;
72         }
73
74         return CONNMAN_SERVICE_TYPE_UNKNOWN;
75 }
76
77 static GIOStatus rfkill_process(GIOChannel *chan)
78 {
79         unsigned char buf[32];
80         struct rfkill_event *event = (void *) buf;
81         enum connman_service_type type;
82         gsize len;
83         GIOStatus status;
84
85         DBG("");
86
87         memset(buf, 0, sizeof(buf));
88
89         status = g_io_channel_read_chars(chan, (gchar *) buf,
90                         sizeof(struct rfkill_event), &len, NULL);
91
92         if (status != G_IO_STATUS_NORMAL)
93                 return status;
94
95         if (len != sizeof(struct rfkill_event))
96                 return status;
97
98         DBG("idx %u type %u op %u soft %u hard %u", event->idx,
99                                                 event->type, event->op,
100                                                 event->soft, event->hard);
101
102         type = convert_type(event->type);
103
104         switch (event->op) {
105         case RFKILL_OP_ADD:
106                 __connman_technology_add_rfkill(event->idx, type,
107                                                 event->soft, event->hard);
108                 break;
109         case RFKILL_OP_DEL:
110                 __connman_technology_remove_rfkill(event->idx, type);
111                 break;
112         case RFKILL_OP_CHANGE:
113                 __connman_technology_update_rfkill(event->idx, type,
114                                                 event->soft, event->hard);
115                 break;
116         default:
117                 break;
118         }
119
120         return status;
121 }
122
123 static gboolean rfkill_event(GIOChannel *chan,
124                                 GIOCondition cond, gpointer data)
125 {
126         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
127                 return FALSE;
128
129         if (rfkill_process(chan) == G_IO_STATUS_ERROR)
130                 return FALSE;
131
132         return TRUE;
133 }
134
135 static GIOChannel *channel = NULL;
136
137 int __connman_rfkill_init(void)
138 {
139         GIOFlags flags;
140         int fd;
141
142         DBG("");
143
144         fd = open("/dev/rfkill", O_RDWR);
145         if (fd < 0) {
146                 connman_error("Failed to open RFKILL control device");
147                 return -EIO;
148         }
149
150         channel = g_io_channel_unix_new(fd);
151         g_io_channel_set_close_on_unref(channel, TRUE);
152
153         g_io_channel_set_encoding(channel, NULL, NULL);
154         g_io_channel_set_buffered(channel, FALSE);
155
156         flags = g_io_channel_get_flags(channel);
157         flags |= G_IO_FLAG_NONBLOCK;
158         g_io_channel_set_flags(channel, flags, NULL);
159
160         /* Process current RFKILL events sent on device open */
161         while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
162
163         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
164                                                         rfkill_event, NULL);
165
166         return 0;
167 }
168
169 void __connman_rfkill_cleanup(void)
170 {
171         DBG("");
172
173         if (channel == NULL)
174                 return;
175
176         g_io_channel_shutdown(channel, TRUE, NULL);
177         g_io_channel_unref(channel);
178
179         channel = NULL;
180 }