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,
46 enum rfkill_operation {
61 static enum connman_service_type convert_type(uint8_t 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;
74 return CONNMAN_SERVICE_TYPE_UNKNOWN;
77 static GIOStatus rfkill_process(GIOChannel *chan)
79 GIOStatus status = G_IO_STATUS_NORMAL;
80 unsigned char buf[32];
81 struct rfkill_event *event = (void *) buf;
83 enum connman_service_type type;
84 connman_bool_t blocked;
89 memset(buf, 0, sizeof(buf));
91 status = g_io_channel_read_chars(chan, (gchar *) buf,
92 sizeof(struct rfkill_event), &len, NULL);
94 if (status != G_IO_STATUS_NORMAL)
97 if (len != sizeof(struct rfkill_event))
100 DBG("idx %u type %u op %u soft %u hard %u", event->idx,
101 event->type, event->op,
102 event->soft, event->hard);
106 type = convert_type(event->type);
107 __connman_technology_add_rfkill(event->idx, type,
108 event->soft, event->hard);
111 __connman_technology_remove_rfkill(event->idx);
113 case RFKILL_OP_CHANGE:
114 __connman_technology_update_rfkill(event->idx, event->soft,
121 snprintf(sysname, sizeof(sysname) - 1, "rfkill%d", event->idx);
123 blocked = (event->soft || event->hard) ? TRUE : FALSE;
125 switch (event->type) {
126 case RFKILL_TYPE_ALL:
127 case RFKILL_TYPE_WLAN:
128 __connman_udev_rfkill(sysname, blocked);
137 static gboolean rfkill_event(GIOChannel *chan,
138 GIOCondition cond, gpointer data)
140 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
143 if (rfkill_process(chan) == G_IO_STATUS_ERROR)
149 static GIOChannel *channel = NULL;
151 int __connman_rfkill_init(void)
158 fd = open("/dev/rfkill", O_RDWR);
160 connman_error("Failed to open RFKILL control device");
164 channel = g_io_channel_unix_new(fd);
165 g_io_channel_set_close_on_unref(channel, TRUE);
167 flags = g_io_channel_get_flags(channel);
168 flags |= G_IO_FLAG_NONBLOCK;
169 g_io_channel_set_flags(channel, flags, NULL);
171 /* Process current RFKILL events sent on device open */
172 while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
174 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
180 void __connman_rfkill_cleanup(void)
187 g_io_channel_shutdown(channel, TRUE, NULL);
188 g_io_channel_unref(channel);