5 * Copyright (C) 2007-2013 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
39 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_WWAN:
70 return CONNMAN_SERVICE_TYPE_CELLULAR;
73 return CONNMAN_SERVICE_TYPE_UNKNOWN;
76 static enum rfkill_type convert_service_type(enum connman_service_type type)
79 case CONNMAN_SERVICE_TYPE_WIFI:
80 return RFKILL_TYPE_WLAN;
81 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
82 return RFKILL_TYPE_BLUETOOTH;
83 case CONNMAN_SERVICE_TYPE_CELLULAR:
84 return RFKILL_TYPE_WWAN;
85 case CONNMAN_SERVICE_TYPE_GPS:
86 return RFKILL_TYPE_GPS;
87 case CONNMAN_SERVICE_TYPE_SYSTEM:
88 case CONNMAN_SERVICE_TYPE_ETHERNET:
89 case CONNMAN_SERVICE_TYPE_VPN:
90 case CONNMAN_SERVICE_TYPE_GADGET:
91 case CONNMAN_SERVICE_TYPE_P2P:
92 case CONNMAN_SERVICE_TYPE_UNKNOWN:
93 return NUM_RFKILL_TYPES;
96 return NUM_RFKILL_TYPES;
99 static GIOStatus rfkill_process(GIOChannel *chan)
101 unsigned char buf[32];
102 struct rfkill_event *event = (void *) buf;
103 enum connman_service_type type;
109 memset(buf, 0, sizeof(buf));
111 status = g_io_channel_read_chars(chan, (gchar *) buf,
112 sizeof(struct rfkill_event), &len, NULL);
114 if (status != G_IO_STATUS_NORMAL)
117 if (len != sizeof(struct rfkill_event))
120 DBG("idx %u type %u op %u soft %u hard %u", event->idx,
121 event->type, event->op,
122 event->soft, event->hard);
124 type = convert_type(event->type);
128 __connman_technology_add_rfkill(event->idx, type,
129 event->soft, event->hard);
132 __connman_technology_remove_rfkill(event->idx, type);
134 case RFKILL_OP_CHANGE:
135 __connman_technology_update_rfkill(event->idx, type,
136 event->soft, event->hard);
145 static gboolean rfkill_event(GIOChannel *chan, GIOCondition cond, gpointer data)
147 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
150 if (rfkill_process(chan) == G_IO_STATUS_ERROR)
156 static GIOChannel *channel = NULL;
158 int __connman_rfkill_block(enum connman_service_type type, bool block)
161 struct rfkill_event event;
165 DBG("type %d block %d", type, block);
167 rfkill_type = convert_service_type(type);
168 if (rfkill_type == NUM_RFKILL_TYPES)
171 fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
175 memset(&event, 0, sizeof(event));
176 event.op = RFKILL_OP_CHANGE_ALL;
177 event.type = rfkill_type;
180 len = write(fd, &event, sizeof(event));
182 connman_error("Failed to change RFKILL state");
192 int __connman_rfkill_init(void)
199 fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
201 connman_error("Failed to open RFKILL control device");
205 channel = g_io_channel_unix_new(fd);
206 g_io_channel_set_close_on_unref(channel, TRUE);
208 g_io_channel_set_encoding(channel, NULL, NULL);
209 g_io_channel_set_buffered(channel, FALSE);
211 flags = g_io_channel_get_flags(channel);
212 flags |= G_IO_FLAG_NONBLOCK;
213 g_io_channel_set_flags(channel, flags, NULL);
215 /* Process current RFKILL events sent on device open */
216 while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
218 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
224 void __connman_rfkill_cleanup(void)
231 g_io_channel_shutdown(channel, TRUE, NULL);
232 g_io_channel_unref(channel);