nm-rfkill-manager.c 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2009 - 2013 Red Hat, Inc.
 */

21
#include "nm-default.h"
22

23 24
#include "nm-rfkill-manager.h"

25 26
#include <libudev.h>

27
#include "nm-udev-aux/nm-udev-utils.h"
28

29 30 31 32 33 34 35 36
/*****************************************************************************/

enum {
	RFKILL_CHANGED,
	LAST_SIGNAL,
};

static guint signals[LAST_SIGNAL] = { 0 };
37 38

typedef struct {
39
	NMUdevClient *udev_client;
40 41 42 43 44 45

	/* Authoritative rfkill state (RFKILL_* enum) */
	RfKillState rfkill_states[RFKILL_TYPE_MAX];
	GSList *killswitches;
} NMRfkillManagerPrivate;

46 47 48 49
struct _NMRfkillManager {
	GObject parent;
	NMRfkillManagerPrivate _priv;
};
50

51 52
struct _NMRfkillManagerClass {
	GObjectClass parent;
53 54
};

55 56 57
G_DEFINE_TYPE (NMRfkillManager, nm_rfkill_manager, G_TYPE_OBJECT)

#define NM_RFKILL_MANAGER_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMRfkillManager, NM_IS_RFKILL_MANAGER)
58

59
/*****************************************************************************/
60 61 62 63 64 65 66

typedef struct {
	char *name;
	guint64 seqnum;
	char *path;
	char *driver;
	RfKillType rtype;
67
	int state;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	gboolean platform;
} Killswitch;

RfKillState
nm_rfkill_manager_get_rfkill_state (NMRfkillManager *self, RfKillType rtype)
{
	g_return_val_if_fail (self != NULL, RFKILL_UNBLOCKED);
	g_return_val_if_fail (rtype < RFKILL_TYPE_MAX, RFKILL_UNBLOCKED);

	return NM_RFKILL_MANAGER_GET_PRIVATE (self)->rfkill_states[rtype];
}

static const char *
rfkill_type_to_desc (RfKillType rtype)
{
	if (rtype == 0)
84
		return "Wi-Fi";
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	else if (rtype == 1)
		return "WWAN";
	else if (rtype == 2)
		return "WiMAX";
	return "unknown";
}

static const char *
rfkill_state_to_desc (RfKillState rstate)
{
	if (rstate == 0)
		return "unblocked";
	else if (rstate == 1)
		return "soft-blocked";
	else if (rstate == 2)
		return "hard-blocked";
	return "unknown";
}

static Killswitch *
105
killswitch_new (struct udev_device *device, RfKillType rtype)
106 107
{
	Killswitch *ks;
108
	struct udev_device *parent = NULL, *grandparent = NULL;
109 110 111
	const char *driver, *subsys, *parent_subsys = NULL;

	ks = g_malloc0 (sizeof (Killswitch));
112 113 114
	ks->name = g_strdup (udev_device_get_sysname (device));
	ks->seqnum = udev_device_get_seqnum (device);
	ks->path = g_strdup (udev_device_get_syspath (device));
115 116
	ks->rtype = rtype;

117 118
	driver = udev_device_get_property_value (device, "DRIVER");
	subsys = udev_device_get_subsystem (device);
119 120

	/* Check parent for various attributes */
121
	parent = udev_device_get_parent (device);
122
	if (parent) {
123
		parent_subsys = udev_device_get_subsystem (parent);
124
		if (!driver)
125
			driver = udev_device_get_property_value (parent, "DRIVER");
126 127
		if (!driver) {
			/* Sigh; try the grandparent */
128
			grandparent = udev_device_get_parent (parent);
129
			if (grandparent)
130
				driver = udev_device_get_property_value (grandparent, "DRIVER");
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		}
	}

	if (!driver)
		driver = "(unknown)";
	ks->driver = g_strdup (driver);

	if (   g_strcmp0 (subsys, "platform") == 0
	    || g_strcmp0 (parent_subsys, "platform") == 0
	    || g_strcmp0 (subsys, "acpi") == 0
	    || g_strcmp0 (parent_subsys, "acpi") == 0)
		ks->platform = TRUE;

	return ks;
}

static void
killswitch_destroy (Killswitch *ks)
{
	g_return_if_fail (ks != NULL);

	g_free (ks->name);
	g_free (ks->path);
	g_free (ks->driver);
	memset (ks, 0, sizeof (Killswitch));
	g_free (ks);
}

static RfKillState
160
sysfs_state_to_nm_state (int sysfs_state)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
{
	switch (sysfs_state) {
	case 0:
		return RFKILL_SOFT_BLOCKED;
	case 1:
		return RFKILL_UNBLOCKED;
	case 2:
		return RFKILL_HARD_BLOCKED;
	default:
		nm_log_warn (LOGD_RFKILL, "unhandled rfkill state %d", sysfs_state);
		break;
	}
	return RFKILL_UNBLOCKED;
}

static void
recheck_killswitches (NMRfkillManager *self)
{
	NMRfkillManagerPrivate *priv = NM_RFKILL_MANAGER_GET_PRIVATE (self);
	GSList *iter;
	RfKillState poll_states[RFKILL_TYPE_MAX];
	RfKillState platform_states[RFKILL_TYPE_MAX];
	gboolean platform_checked[RFKILL_TYPE_MAX];
	int i;

	/* Default state is unblocked */
	for (i = 0; i < RFKILL_TYPE_MAX; i++) {
		poll_states[i] = RFKILL_UNBLOCKED;
		platform_states[i] = RFKILL_UNBLOCKED;
		platform_checked[i] = FALSE;
	}

	/* Poll the states of all killswitches */
	for (iter = priv->killswitches; iter; iter = g_slist_next (iter)) {
		Killswitch *ks = iter->data;
196
		struct udev_device *device;
197 198 199
		RfKillState dev_state;
		int sysfs_state;

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		device = udev_device_new_from_subsystem_sysname (nm_udev_client_get_udev (priv->udev_client),
		                                                 "rfkill", ks->name);
		if (!device)
			continue;
		sysfs_state = _nm_utils_ascii_str_to_int64 (udev_device_get_property_value (device, "RFKILL_STATE"),
		                                            10, G_MININT, G_MAXINT, -1);
		dev_state = sysfs_state_to_nm_state (sysfs_state);

		nm_log_dbg (LOGD_RFKILL, "%s rfkill%s switch %s state now %d/%u",
		            rfkill_type_to_desc (ks->rtype),
		            ks->platform ? " platform" : "",
		            ks->name,
		            sysfs_state,
		            dev_state);

		if (ks->platform == FALSE) {
			if (dev_state > poll_states[ks->rtype])
				poll_states[ks->rtype] = dev_state;
		} else {
			platform_checked[ks->rtype] = TRUE;
			if (dev_state > platform_states[ks->rtype])
				platform_states[ks->rtype] = dev_state;
222
		}
223
		udev_device_unref (device);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	}

	/* Log and emit change signal for final rfkill states */
	for (i = 0; i < RFKILL_TYPE_MAX; i++) {
		if (platform_checked[i] == TRUE) {
			/* blocked platform switch state overrides device state, otherwise
			 * let the device state stand. (bgo #655773)
			 */
			if (platform_states[i] != RFKILL_UNBLOCKED)
				poll_states[i] = platform_states[i];
		}

		if (poll_states[i] != priv->rfkill_states[i]) {
			nm_log_dbg (LOGD_RFKILL, "%s rfkill state now '%s'",
			            rfkill_type_to_desc (i),
			            rfkill_state_to_desc (poll_states[i]));

			priv->rfkill_states[i] = poll_states[i];
			g_signal_emit (self, signals[RFKILL_CHANGED], 0, i, priv->rfkill_states[i]);
		}
	}
}

static Killswitch *
killswitch_find_by_name (NMRfkillManager *self, const char *name)
{
	NMRfkillManagerPrivate *priv = NM_RFKILL_MANAGER_GET_PRIVATE (self);
	GSList *iter;

	g_return_val_if_fail (name != NULL, NULL);

	for (iter = priv->killswitches; iter; iter = g_slist_next (iter)) {
		Killswitch *candidate = iter->data;

		if (!strcmp (name, candidate->name))
			return candidate;
	}
	return NULL;
}

264
static RfKillType
265 266 267 268 269 270 271 272 273 274 275 276 277
rfkill_type_to_enum (const char *str)
{
	g_return_val_if_fail (str != NULL, RFKILL_TYPE_UNKNOWN);

	if (!strcmp (str, "wlan"))
		return RFKILL_TYPE_WLAN;
	else if (!strcmp (str, "wwan"))
		return RFKILL_TYPE_WWAN;

	return RFKILL_TYPE_UNKNOWN;
}

static void
278
add_one_killswitch (NMRfkillManager *self, struct udev_device *device)
279 280 281 282 283 284
{
	NMRfkillManagerPrivate *priv = NM_RFKILL_MANAGER_GET_PRIVATE (self);
	const char *str_type;
	RfKillType rtype;
	Killswitch *ks;

285
	str_type = udev_device_get_property_value (device, "RFKILL_TYPE");
286 287 288 289 290 291 292 293 294 295 296 297
	rtype = rfkill_type_to_enum (str_type);
	if (rtype == RFKILL_TYPE_UNKNOWN)
		return;

	ks = killswitch_new (device, rtype);
	priv->killswitches = g_slist_prepend (priv->killswitches, ks);

	nm_log_info (LOGD_RFKILL, "%s: found %s radio killswitch (at %s) (%sdriver %s)",
	             ks->name,
	             rfkill_type_to_desc (rtype),
	             ks->path,
	             ks->platform ? "platform " : "",
298
	             ks->driver ?: "<unknown>");
299 300 301
}

static void
302
rfkill_add (NMRfkillManager *self, struct udev_device *device)
303 304 305 306
{
	const char *name;

	g_return_if_fail (device != NULL);
307
	name = udev_device_get_sysname (device);
308 309 310 311 312 313 314 315
	g_return_if_fail (name != NULL);

	if (!killswitch_find_by_name (self, name))
		add_one_killswitch (self, device);
}

static void
rfkill_remove (NMRfkillManager *self,
316
               struct udev_device *device)
317 318 319 320 321 322
{
	NMRfkillManagerPrivate *priv = NM_RFKILL_MANAGER_GET_PRIVATE (self);
	GSList *iter;
	const char *name;

	g_return_if_fail (device != NULL);
323
	name = udev_device_get_sysname (device);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	g_return_if_fail (name != NULL);

	for (iter = priv->killswitches; iter; iter = g_slist_next (iter)) {
		Killswitch *ks = iter->data;

		if (!strcmp (ks->name, name)) {
			nm_log_info (LOGD_RFKILL, "radio killswitch %s disappeared", ks->path);
			priv->killswitches = g_slist_remove (priv->killswitches, ks);
			killswitch_destroy (ks);
			break;
		}
	}
}

static void
339 340
handle_uevent (NMUdevClient *client,
               struct udev_device *device,
341 342 343 344
               gpointer user_data)
{
	NMRfkillManager *self = NM_RFKILL_MANAGER (user_data);
	const char *subsys;
345 346 347
	const char *action;

	action = udev_device_get_action (device);
348 349 350 351

	g_return_if_fail (action != NULL);

	/* A bit paranoid */
352
	subsys = udev_device_get_subsystem (device);
353 354
	g_return_if_fail (!g_strcmp0 (subsys, "rfkill"));

355
	nm_log_dbg (LOGD_PLATFORM, "udev rfkill event: action '%s' device '%s'",
356
	            action, udev_device_get_sysname (device));
357 358 359 360 361 362 363 364 365

	if (!strcmp (action, "add"))
		rfkill_add (self, device);
	else if (!strcmp (action, "remove"))
		rfkill_remove (self, device);

	recheck_killswitches (self);
}

366 367
/*****************************************************************************/

368 369 370 371
static void
nm_rfkill_manager_init (NMRfkillManager *self)
{
	NMRfkillManagerPrivate *priv = NM_RFKILL_MANAGER_GET_PRIVATE (self);
372 373 374
	struct udev_enumerate *enumerate;
	struct udev_list_entry *iter;
	guint i;
375 376 377 378

	for (i = 0; i < RFKILL_TYPE_MAX; i++)
		priv->rfkill_states[i] = RFKILL_UNBLOCKED;

379 380 381 382 383 384 385 386
	priv->udev_client = nm_udev_client_new ((const char *[]) { "rfkill", NULL },
	                                        handle_uevent, self);

	enumerate = nm_udev_client_enumerate_new (priv->udev_client);
	udev_enumerate_scan_devices (enumerate);
	iter = udev_enumerate_get_list_entry (enumerate);
	for (; iter; iter = udev_list_entry_get_next (iter)) {
		struct udev_device *udevice;
387

388 389 390 391 392 393 394
		udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate),
		                                        udev_list_entry_get_name (iter));
		if (!udevice)
			continue;

		add_one_killswitch (self, udevice);
		udev_device_unref (udevice);
395
	}
396
	udev_enumerate_unref (enumerate);
397 398 399 400

	recheck_killswitches (self);
}

401 402 403 404 405 406
NMRfkillManager *
nm_rfkill_manager_new (void)
{
	return NM_RFKILL_MANAGER (g_object_new (NM_TYPE_RFKILL_MANAGER, NULL));
}

407 408 409 410 411 412 413 414 415 416 417
static void
dispose (GObject *object)
{
	NMRfkillManager *self = NM_RFKILL_MANAGER (object);
	NMRfkillManagerPrivate *priv = NM_RFKILL_MANAGER_GET_PRIVATE (self);

	if (priv->killswitches) {
		g_slist_free_full (priv->killswitches, (GDestroyNotify) killswitch_destroy);
		priv->killswitches = NULL;
	}

418 419
	priv->udev_client = nm_udev_client_unref (priv->udev_client);

420
	G_OBJECT_CLASS (nm_rfkill_manager_parent_class)->dispose (object);
421 422 423 424 425 426 427 428 429 430
}

static void
nm_rfkill_manager_class_init (NMRfkillManagerClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = dispose;

	signals[RFKILL_CHANGED] =
431 432 433 434 435 436
	    g_signal_new (NM_RFKILL_MANAGER_SIGNAL_RFKILL_CHANGED,
	                  G_OBJECT_CLASS_TYPE (object_class),
	                  G_SIGNAL_RUN_FIRST,
	                  0,
	                  NULL, NULL, NULL,
	                  G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
437
}