eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

rfkill.c (6439B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2016-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <sys/types.h>
     29 #include <sys/stat.h>
     30 #include <fcntl.h>
     31 #include <errno.h>
     32 #include <linux/rfkill.h>
     33 #include <stdio.h>
     34 #include <unistd.h>
     35 
     36 #include <ell/ell.h>
     37 
     38 #include "src/iwd.h"
     39 #include "src/module.h"
     40 #include "src/common.h"
     41 #include "src/rfkill.h"
     42 
     43 struct rfkill_map_entry {
     44 	unsigned int wiphy_id;
     45 	unsigned int rfkill_id;
     46 	bool soft_state : 1;
     47 	bool hard_state : 1;
     48 };
     49 
     50 struct rfkill_watch {
     51 	uint32_t id;
     52 	rfkill_state_cb_t callback;
     53 	void *user_data;
     54 };
     55 
     56 static struct l_queue *rfkill_map;
     57 static struct l_io *rfkill_io;
     58 
     59 static struct l_queue *rfkill_watches;
     60 static uint32_t next_watch_id;
     61 
     62 static bool rfkill_id_match(const void *a, const void *b)
     63 {
     64 	const struct rfkill_map_entry *entry = a;
     65 	uint32_t id = L_PTR_TO_UINT(b);
     66 
     67 	return entry->rfkill_id == id;
     68 }
     69 
     70 static bool wiphy_id_match(const void *a, const void *b)
     71 {
     72 	const struct rfkill_map_entry *entry = a;
     73 	uint32_t id = L_PTR_TO_UINT(b);
     74 
     75 	return entry->wiphy_id == id;
     76 }
     77 
     78 static struct rfkill_map_entry *map_wiphy(unsigned int rfkill_id)
     79 {
     80 	int fd, bytes, consumed;
     81 	char *path;
     82 	char buf[32];
     83 	unsigned int wiphy_id;
     84 	struct rfkill_map_entry *entry;
     85 
     86 	path = l_strdup_printf("/sys/class/rfkill/rfkill%u/device/index", rfkill_id);
     87 
     88 	fd = L_TFR(open(path, O_RDONLY));
     89 
     90 	l_free(path);
     91 
     92 	if (fd < 0)
     93 		return NULL;
     94 
     95 	bytes = L_TFR(read(fd, buf, sizeof(buf) - 1));
     96 
     97 	close(fd);
     98 
     99 	if (bytes <= 0)
    100 		return NULL;
    101 
    102 	buf[bytes] = '\0';
    103 	if (sscanf(buf, "%u %n", &wiphy_id, &consumed) != 1 ||
    104 			consumed != bytes)
    105 		return NULL;
    106 
    107 	entry = l_new(struct rfkill_map_entry, 1);
    108 	entry->rfkill_id = rfkill_id;
    109 	entry->wiphy_id = wiphy_id;
    110 
    111 	return entry;
    112 }
    113 
    114 static void rfkill_watch_notify(void *data, void *user_data)
    115 {
    116 	struct rfkill_watch *watch = data;
    117 	struct rfkill_map_entry *entry = user_data;
    118 
    119 	watch->callback(entry->wiphy_id, entry->soft_state, entry->hard_state,
    120 			watch->user_data);
    121 }
    122 
    123 static bool rfkill_read(struct l_io *io, void *user_data)
    124 {
    125 	int fd = l_io_get_fd(rfkill_io);
    126 	struct rfkill_event e;
    127 	int bytes;
    128 	struct rfkill_map_entry *entry;
    129 
    130 	bytes = L_TFR(read(fd, &e, sizeof(e)));
    131 	if (bytes < (int) sizeof(e)) {
    132 		if (bytes <= 0)
    133 			l_error("rfkill read: %s", strerror(errno));
    134 		else
    135 			l_error("rfkill read of %i bytes", bytes);
    136 
    137 		return false;
    138 	}
    139 
    140 	if (e.type != RFKILL_TYPE_WLAN)
    141 		return true;
    142 
    143 	entry = l_queue_find(rfkill_map, rfkill_id_match, L_UINT_TO_PTR(e.idx));
    144 
    145 	if (e.op == RFKILL_OP_ADD) {
    146 		if (entry) {
    147 			l_error("rfkill id %u already known", e.idx);
    148 
    149 			return true;
    150 		}
    151 
    152 		entry = map_wiphy(e.idx);
    153 		if (!entry) {
    154 			l_error("rfkill id %u can't be matched to a wiphy",
    155 					e.idx);
    156 
    157 			return true;
    158 		}
    159 
    160 		l_queue_push_tail(rfkill_map, entry);
    161 	} else if (e.op != RFKILL_OP_DEL && e.op != RFKILL_OP_CHANGE)
    162 		return true;
    163 
    164 	if (!entry) {
    165 		l_error("rfkill id %u not found in a %s event", e.idx,
    166 				e.op == RFKILL_OP_DEL ? "RFKILL_OP_DEL" :
    167 				"RFKILL_OP_CHANGE");
    168 
    169 		return true;
    170 	}
    171 
    172 	if (e.op == RFKILL_OP_DEL) {
    173 		l_queue_remove(rfkill_map, entry);
    174 
    175 		l_free(entry);
    176 
    177 		return true;
    178 	}
    179 
    180 	entry->soft_state = e.soft != 0;
    181 	entry->hard_state = e.hard != 0;
    182 
    183 	l_queue_foreach(rfkill_watches, rfkill_watch_notify, entry);
    184 
    185 	return true;
    186 }
    187 
    188 bool rfkill_set_soft_state(unsigned int wiphy_id, bool state)
    189 {
    190 	int fd = l_io_get_fd(rfkill_io);
    191 	struct rfkill_event e;
    192 	struct rfkill_map_entry *entry;
    193 	int bytes;
    194 
    195 	entry = l_queue_find(rfkill_map, wiphy_id_match,
    196 				L_UINT_TO_PTR(wiphy_id));
    197 	if (!entry)
    198 		return false;
    199 
    200 	memset(&e, 0, sizeof(e));
    201 
    202 	e.idx = entry->rfkill_id;
    203 	e.type = RFKILL_TYPE_WLAN;
    204 	e.op = RFKILL_OP_CHANGE;
    205 	e.soft = state ? 1 : 0;
    206 
    207 	bytes = L_TFR(write(fd, &e, sizeof(e)));
    208 	if (bytes < (int) sizeof(e)) {
    209 		if (bytes <= 0)
    210 			l_error("rfkill write: %s", strerror(errno));
    211 		else
    212 			l_error("rfkill write of %i bytes", bytes);
    213 
    214 		return false;
    215 	}
    216 
    217 	return true;
    218 }
    219 
    220 static bool rfkill_watch_match(const void *a, const void *b)
    221 {
    222 	const struct rfkill_watch *item = a;
    223 	uint32_t id = L_PTR_TO_UINT(b);
    224 
    225 	return item->id == id;
    226 }
    227 
    228 uint32_t rfkill_watch_add(rfkill_state_cb_t func, void *user_data)
    229 {
    230 	struct rfkill_watch *item;
    231 
    232 	item = l_new(struct rfkill_watch, 1);
    233 	item->id = ++next_watch_id;
    234 	item->callback = func;
    235 	item->user_data = user_data;
    236 
    237 	if (!rfkill_watches)
    238 		rfkill_watches = l_queue_new();
    239 
    240 	l_queue_push_tail(rfkill_watches, item);
    241 
    242 	return item->id;
    243 }
    244 
    245 bool rfkill_watch_remove(uint32_t watch_id)
    246 {
    247 	struct rfkill_watch *item;
    248 
    249 	item = l_queue_remove_if(rfkill_watches, rfkill_watch_match,
    250 					L_UINT_TO_PTR(watch_id));
    251 	if (!item)
    252 		return false;
    253 
    254 	l_free(item);
    255 
    256 	return true;
    257 }
    258 
    259 bool rfkill_get_soft_state(unsigned int wiphy_id)
    260 {
    261 	struct rfkill_map_entry *entry;
    262 
    263 	entry = l_queue_find(rfkill_map, wiphy_id_match,
    264 				L_UINT_TO_PTR(wiphy_id));
    265 
    266 	return entry ? entry->soft_state : false;
    267 }
    268 
    269 bool rfkill_get_hard_state(unsigned int wiphy_id)
    270 {
    271 	struct rfkill_map_entry *entry;
    272 
    273 	entry = l_queue_find(rfkill_map, wiphy_id_match,
    274 				L_UINT_TO_PTR(wiphy_id));
    275 
    276 	return entry ? entry->hard_state : false;
    277 }
    278 
    279 static int rfkill_init(void)
    280 {
    281 	int fd;
    282 
    283 	fd = L_TFR(open("/dev/rfkill", O_RDWR | O_CLOEXEC));
    284 	if (fd < 0)
    285 		return -errno;
    286 
    287 	rfkill_io = l_io_new(fd);
    288 	if (!rfkill_io) {
    289 		close(fd);
    290 		return -EIO;
    291 	}
    292 
    293 	l_io_set_close_on_destroy(rfkill_io, true);
    294 	l_io_set_read_handler(rfkill_io, rfkill_read, NULL, NULL);
    295 
    296 	rfkill_map = l_queue_new();
    297 
    298 	return 0;
    299 }
    300 
    301 static void rfkill_exit(void)
    302 {
    303 	l_io_destroy(rfkill_io);
    304 	l_queue_destroy(rfkill_map, l_free);
    305 	l_queue_destroy(rfkill_watches, l_free);
    306 }
    307 
    308 IWD_MODULE(rfkill, rfkill_init, rfkill_exit)