3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
46 RFKILL_TYPE_BLUETOOTH,
52 enum rfkill_operation {
67 static gboolean rfkill_event(GIOChannel *chan,
68 GIOCondition cond, gpointer data)
70 unsigned char buf[32];
71 struct rfkill_event *event = (void *) buf;
72 struct btd_adapter *adapter;
73 char sysname[PATH_MAX];
78 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
81 memset(buf, 0, sizeof(buf));
83 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
85 if (err == G_IO_ERROR_AGAIN)
90 if (len != sizeof(struct rfkill_event))
93 debug("RFKILL event idx %u type %u op %u soft %u hard %u",
94 event->idx, event->type, event->op,
95 event->soft, event->hard);
97 if (event->soft || event->hard)
100 if (event->op != RFKILL_OP_CHANGE)
103 if (event->type != RFKILL_TYPE_BLUETOOTH &&
104 event->type != RFKILL_TYPE_ALL)
107 snprintf(sysname, sizeof(sysname) - 1,
108 "/sys/class/rfkill/rfkill%u/name", event->idx);
110 fd = open(sysname, O_RDONLY);
114 memset(sysname, 0, sizeof(sysname));
116 if (read(fd, sysname, sizeof(sysname)) < 4) {
123 if (g_str_has_prefix(sysname, "hci") == FALSE)
126 id = atoi(sysname + 3);
130 adapter = manager_find_adapter_by_id(id);
134 debug("RFKILL unblock for hci%d", id);
139 static GIOChannel *channel = NULL;
141 void rfkill_init(void)
145 if (!main_opts.remember_powered)
148 fd = open("/dev/rfkill", O_RDWR);
150 error("Failed to open RFKILL control device");
154 channel = g_io_channel_unix_new(fd);
155 g_io_channel_set_close_on_unref(channel, TRUE);
157 g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
161 void rfkill_exit(void)
166 g_io_channel_shutdown(channel, TRUE, NULL);
167 g_io_channel_unref(channel);