5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
38 RFKILL_TYPE_BLUETOOTH,
47 enum rfkill_operation {
62 static enum connman_service_type convert_type(uint8_t 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;
75 return CONNMAN_SERVICE_TYPE_UNKNOWN;
78 static enum rfkill_type convert_service_type(enum connman_service_type 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;
99 return NUM_RFKILL_TYPES;
102 static GIOStatus rfkill_process(GIOChannel *chan)
104 unsigned char buf[32];
105 struct rfkill_event *event = (void *) buf;
106 enum connman_service_type type;
112 memset(buf, 0, sizeof(buf));
114 status = g_io_channel_read_chars(chan, (gchar *) buf,
115 sizeof(struct rfkill_event), &len, NULL);
117 if (status != G_IO_STATUS_NORMAL)
120 if (len != sizeof(struct rfkill_event))
123 DBG("idx %u type %u op %u soft %u hard %u", event->idx,
124 event->type, event->op,
125 event->soft, event->hard);
127 type = convert_type(event->type);
131 __connman_technology_add_rfkill(event->idx, type,
132 event->soft, event->hard);
135 __connman_technology_remove_rfkill(event->idx, type);
137 case RFKILL_OP_CHANGE:
138 __connman_technology_update_rfkill(event->idx, type,
139 event->soft, event->hard);
148 static gboolean rfkill_event(GIOChannel *chan,
149 GIOCondition cond, gpointer data)
151 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
154 if (rfkill_process(chan) == G_IO_STATUS_ERROR)
160 static GIOChannel *channel = NULL;
162 int __connman_rfkill_block(enum connman_service_type type, connman_bool_t block)
165 struct rfkill_event event;
169 DBG("type %d block %d", type, block);
171 rfkill_type = convert_service_type(type);
172 if (rfkill_type == NUM_RFKILL_TYPES)
175 fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
179 memset(&event, 0, sizeof(event));
180 event.op = RFKILL_OP_CHANGE_ALL;
181 event.type = rfkill_type;
184 len = write(fd, &event, sizeof(event));
186 connman_error("Failed to change RFKILL state");
193 int __connman_rfkill_init(void)
200 fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
202 connman_error("Failed to open RFKILL control device");
206 channel = g_io_channel_unix_new(fd);
207 g_io_channel_set_close_on_unref(channel, TRUE);
209 g_io_channel_set_encoding(channel, NULL, NULL);
210 g_io_channel_set_buffered(channel, FALSE);
212 flags = g_io_channel_get_flags(channel);
213 flags |= G_IO_FLAG_NONBLOCK;
214 g_io_channel_set_flags(channel, flags, NULL);
216 /* Process current RFKILL events sent on device open */
217 while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
219 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
225 void __connman_rfkill_cleanup(void)
232 g_io_channel_shutdown(channel, TRUE, NULL);
233 g_io_channel_unref(channel);