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);
102 type = convert_type(event->type);
106 __connman_technology_add_rfkill(event->idx, type,
107 event->soft, event->hard);
110 __connman_technology_remove_rfkill(event->idx, type);
112 case RFKILL_OP_CHANGE:
113 __connman_technology_update_rfkill(event->idx, type,
114 event->soft, event->hard);
123 static gboolean rfkill_event(GIOChannel *chan,
124 GIOCondition cond, gpointer data)
126 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
129 if (rfkill_process(chan) == G_IO_STATUS_ERROR)
135 static GIOChannel *channel = NULL;
137 int __connman_rfkill_init(void)
144 fd = open("/dev/rfkill", O_RDWR);
146 connman_error("Failed to open RFKILL control device");
150 channel = g_io_channel_unix_new(fd);
151 g_io_channel_set_close_on_unref(channel, TRUE);
153 g_io_channel_set_encoding(channel, NULL, NULL);
154 g_io_channel_set_buffered(channel, FALSE);
156 flags = g_io_channel_get_flags(channel);
157 flags |= G_IO_FLAG_NONBLOCK;
158 g_io_channel_set_flags(channel, flags, NULL);
160 /* Process current RFKILL events sent on device open */
161 while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
163 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
169 void __connman_rfkill_cleanup(void)
176 g_io_channel_shutdown(channel, TRUE, NULL);
177 g_io_channel_unref(channel);