5 * Copyright (C) 2007-2012 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,
48 enum rfkill_operation {
63 static enum connman_service_type convert_type(uint8_t type)
66 case RFKILL_TYPE_WLAN:
67 return CONNMAN_SERVICE_TYPE_WIFI;
68 case RFKILL_TYPE_BLUETOOTH:
69 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
70 case RFKILL_TYPE_WIMAX:
71 return CONNMAN_SERVICE_TYPE_WIMAX;
72 case RFKILL_TYPE_WWAN:
73 return CONNMAN_SERVICE_TYPE_CELLULAR;
76 return CONNMAN_SERVICE_TYPE_UNKNOWN;
79 static enum rfkill_type convert_service_type(enum connman_service_type type)
82 case CONNMAN_SERVICE_TYPE_WIFI:
83 return RFKILL_TYPE_WLAN;
84 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
85 return RFKILL_TYPE_BLUETOOTH;
86 case CONNMAN_SERVICE_TYPE_WIMAX:
87 return RFKILL_TYPE_WIMAX;
88 case CONNMAN_SERVICE_TYPE_CELLULAR:
89 return RFKILL_TYPE_WWAN;
90 case CONNMAN_SERVICE_TYPE_GPS:
91 return RFKILL_TYPE_GPS;
92 case CONNMAN_SERVICE_TYPE_SYSTEM:
93 case CONNMAN_SERVICE_TYPE_ETHERNET:
94 case CONNMAN_SERVICE_TYPE_VPN:
95 case CONNMAN_SERVICE_TYPE_GADGET:
96 case CONNMAN_SERVICE_TYPE_UNKNOWN:
97 return NUM_RFKILL_TYPES;
100 return NUM_RFKILL_TYPES;
103 static GIOStatus rfkill_process(GIOChannel *chan)
105 unsigned char buf[32];
106 struct rfkill_event *event = (void *) buf;
107 enum connman_service_type type;
113 memset(buf, 0, sizeof(buf));
115 status = g_io_channel_read_chars(chan, (gchar *) buf,
116 sizeof(struct rfkill_event), &len, NULL);
118 if (status != G_IO_STATUS_NORMAL)
121 if (len != sizeof(struct rfkill_event))
124 DBG("idx %u type %u op %u soft %u hard %u", event->idx,
125 event->type, event->op,
126 event->soft, event->hard);
128 type = convert_type(event->type);
132 __connman_technology_add_rfkill(event->idx, type,
133 event->soft, event->hard);
136 __connman_technology_remove_rfkill(event->idx, type);
138 case RFKILL_OP_CHANGE:
139 __connman_technology_update_rfkill(event->idx, type,
140 event->soft, event->hard);
149 static gboolean rfkill_event(GIOChannel *chan,
150 GIOCondition cond, gpointer data)
152 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
155 if (rfkill_process(chan) == G_IO_STATUS_ERROR)
161 static GIOChannel *channel = NULL;
163 int __connman_rfkill_block(enum connman_service_type type, connman_bool_t block)
166 struct rfkill_event event;
170 DBG("type %d block %d", type, block);
172 rfkill_type = convert_service_type(type);
173 if (rfkill_type == NUM_RFKILL_TYPES)
176 fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
180 memset(&event, 0, sizeof(event));
181 event.op = RFKILL_OP_CHANGE_ALL;
182 event.type = rfkill_type;
185 len = write(fd, &event, sizeof(event));
187 connman_error("Failed to change RFKILL state");
194 int __connman_rfkill_init(void)
201 fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
203 connman_error("Failed to open RFKILL control device");
207 channel = g_io_channel_unix_new(fd);
208 g_io_channel_set_close_on_unref(channel, TRUE);
210 g_io_channel_set_encoding(channel, NULL, NULL);
211 g_io_channel_set_buffered(channel, FALSE);
213 flags = g_io_channel_get_flags(channel);
214 flags |= G_IO_FLAG_NONBLOCK;
215 g_io_channel_set_flags(channel, flags, NULL);
217 /* Process current RFKILL events sent on device open */
218 while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
220 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
226 void __connman_rfkill_cleanup(void)
233 g_io_channel_shutdown(channel, TRUE, NULL);
234 g_io_channel_unref(channel);