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 unsigned char buf[32];
80 struct rfkill_event *event = (void *) buf;
81 enum connman_service_type type;
87 memset(buf, 0, sizeof(buf));
89 status = g_io_channel_read_chars(chan, (gchar *) buf,
90 sizeof(struct rfkill_event), &len, NULL);
92 if (status != G_IO_STATUS_NORMAL)
95 if (len != sizeof(struct rfkill_event))
98 DBG("idx %u type %u op %u soft %u hard %u", event->idx,
99 event->type, event->op,
100 event->soft, event->hard);
104 type = convert_type(event->type);
105 __connman_technology_add_rfkill(event->idx, type,
106 event->soft, event->hard);
109 __connman_technology_remove_rfkill(event->idx);
111 case RFKILL_OP_CHANGE:
112 __connman_technology_update_rfkill(event->idx, event->soft,
122 static gboolean rfkill_event(GIOChannel *chan,
123 GIOCondition cond, gpointer data)
125 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
128 if (rfkill_process(chan) == G_IO_STATUS_ERROR)
134 static GIOChannel *channel = NULL;
136 int __connman_rfkill_init(void)
143 fd = open("/dev/rfkill", O_RDWR);
145 connman_error("Failed to open RFKILL control device");
149 channel = g_io_channel_unix_new(fd);
150 g_io_channel_set_close_on_unref(channel, TRUE);
152 g_io_channel_set_encoding(channel, NULL, NULL);
153 g_io_channel_set_buffered(channel, FALSE);
155 flags = g_io_channel_get_flags(channel);
156 flags |= G_IO_FLAG_NONBLOCK;
157 g_io_channel_set_flags(channel, flags, NULL);
159 /* Process current RFKILL events sent on device open */
160 while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
162 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
168 void __connman_rfkill_cleanup(void)
175 g_io_channel_shutdown(channel, TRUE, NULL);
176 g_io_channel_unref(channel);