1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 160 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2011 Gary Ching-Pang Lin <glin@suse.com>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:urf-killswitch
* @short_description: Client object for accessing information about killswitches
* @title: UrfKillswitch
* @include: urfkill.h
* @see_also: #UrfClient, #UrfDevice
*
* A helper GObject for accessing killswitches
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <gio/gio.h>
#include "urf-killswitch.h"
#include "urf-enum.h"
#define URF_KILLSWITCH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
URF_TYPE_KILLSWITCH, UrfKillswitchPrivate))
#define BASE_OBJECT_PATH "/org/freedesktop/URfkill/"
struct _UrfKillswitchPrivate
{
GDBusProxy *proxy;
UrfEnumType type;
UrfEnumState state;
char *object_path;
};
enum {
URF_KILLSWITCH_STATE_CHANGED,
URF_KILLSWITCH_LAST_SIGNAL
};
enum {
PROP_0,
PROP_KILLSWITCH_STATE,
PROP_LAST
};
static guint signals [URF_KILLSWITCH_LAST_SIGNAL] = { 0 };
static gpointer urf_killswitch_object[URF_ENUM_TYPE_NUM] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
G_DEFINE_TYPE (UrfKillswitch, urf_killswitch, G_TYPE_OBJECT)
/**
* urf_killswitch_get_switch_type:
* @killswitch: a #UrfKillswitch instance
*
* Get the type of the killswitch.
*
* Return value: The type of the killswitch
*
* Since: 0.3.0
**/
UrfEnumType
urf_killswitch_get_switch_type (UrfKillswitch *killswitch)
{
g_return_val_if_fail (URF_IS_KILLSWITCH (killswitch), URF_ENUM_TYPE_NUM);
return killswitch->priv->type;
}
/**
* urf_killswitch_changed_cb:
**/
static void
urf_killswitch_changed_cb (GDBusProxy *proxy,
GVariant *changed_properties,
GStrv invalidated_properties,
gpointer user_data)
{
UrfKillswitch *killswitch = URF_KILLSWITCH (user_data);
UrfKillswitchPrivate *priv = killswitch->priv;
GVariant *value;
int state;
value = g_dbus_proxy_get_cached_property (priv->proxy, "state");
state = g_variant_get_int32 (value);
if (priv->state != state) {
priv->state = state;
g_signal_emit (killswitch,
signals[URF_KILLSWITCH_STATE_CHANGED],
0, state);
}
}
/**
* urf_killswitch_set_object_path_sync:
**/
static gboolean
urf_killswitch_set_object_path_sync (UrfKillswitch *killswitch,
const char *object_path,
GError **error)
{
UrfKillswitchPrivate *priv = killswitch->priv;
GVariant *value;
GError *error_local = NULL;
if (priv->object_path != NULL)
return FALSE;
if (object_path == NULL)
return FALSE;
/* invalid */
if (object_path == NULL || object_path[0] != '/') {
g_set_error (error, 1, 0, "Object path %s invalid", object_path);
goto out;
}
/* connect to the correct path for properties */
priv->proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.URfkill",
object_path,
"org.freedesktop.URfkill.Killswitch",
NULL,
&error_local);
if (error_local) {
g_warning ("Couldn't connect to proxy: %s", error_local->message);
g_set_error (error, 1, 0, "%s", error_local->message);
return FALSE;
}
priv->object_path = g_strdup (object_path);
value = g_dbus_proxy_get_cached_property (priv->proxy, "state");
priv->state = g_variant_get_int32 (value);
/* connect signals */
g_signal_connect (priv->proxy, "g-properties-changed",
G_CALLBACK (urf_killswitch_changed_cb), killswitch);
out:
return TRUE;
}
/**
* urf_killswitch_startup:
**/
static gboolean
urf_killswitch_startup (UrfKillswitch *killswitch)
{
const char *object_path;
GError *error = NULL;
gboolean ret = FALSE;
switch (killswitch->priv->type) {
case URF_ENUM_TYPE_WLAN:
object_path = BASE_OBJECT_PATH"WLAN";
break;
case URF_ENUM_TYPE_BLUETOOTH:
object_path = BASE_OBJECT_PATH"BLUETOOTH";
break;
case URF_ENUM_TYPE_UWB:
object_path = BASE_OBJECT_PATH"UWB";
break;
case URF_ENUM_TYPE_WIMAX:
object_path = BASE_OBJECT_PATH"WIMAX";
break;
case URF_ENUM_TYPE_WWAN:
object_path = BASE_OBJECT_PATH"WWAN";
break;
case URF_ENUM_TYPE_GPS:
object_path = BASE_OBJECT_PATH"GPS";
break;
case URF_ENUM_TYPE_FM:
object_path = BASE_OBJECT_PATH"FM";
break;
case URF_ENUM_TYPE_NFC:
object_path = BASE_OBJECT_PATH"NFC";
break;
default:
object_path = NULL;
break;
}
ret = urf_killswitch_set_object_path_sync (killswitch,
object_path,
&error);
if (error) {
g_warning ("UrfKillswitch: %s", error->message);
g_error_free (error);
}
return ret;
}
/**
* set_block_cb:
**/
static void
set_block_cb (GDBusProxy *proxy,
GAsyncResult *res,
gpointer user_data)
{
GVariant *retval;
gboolean status;
GError *error = NULL;
retval = g_dbus_proxy_call_finish (proxy, res, &error);
if (retval)
g_variant_get (retval, "(b)", &status);
if (error) {
g_warning ("Failed to set BLOCK: %s", error->message);
g_error_free (error);
} else if (!status) {
g_warning ("Failed to set BLOCK");
}
g_object_unref (proxy);
}
/**
* urf_killswitch_set_block:
**/
static void
urf_killswitch_set_block (UrfKillswitch *killswitch,
UrfEnumState state)
{
UrfKillswitchPrivate *priv = killswitch->priv;
GDBusProxy *proxy;
gboolean block;
GError *error = NULL;
if (state == URF_ENUM_STATE_UNBLOCKED)
block = FALSE;
else
block = TRUE;
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.URfkill",
"/org/freedesktop/URfkill",
"org.freedesktop.URfkill",
NULL,
&error);
if (error) {
g_warning ("Couldn't connect to proxy to set block: %s",
error->message);
g_error_free (error);
return;
}
g_dbus_proxy_call (proxy, "Block",
g_variant_new ("(ub)",
priv->type,
block),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL,
(GAsyncReadyCallback) set_block_cb,
killswitch);
}
/**
* urf_killswitch_set_property:
**/
static void
urf_killswitch_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
UrfKillswitch *killswitch = URF_KILLSWITCH (object);
int state;
switch (prop_id) {
case PROP_KILLSWITCH_STATE:
state = g_value_get_int (value);
if (state == URF_ENUM_STATE_UNBLOCKED ||
state == URF_ENUM_STATE_SOFT_BLOCKED)
urf_killswitch_set_block (killswitch, state);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* urf_killswitch_get_property:
**/
static void
urf_killswitch_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
UrfKillswitch *killswitch = URF_KILLSWITCH (object);
UrfKillswitchPrivate *priv = killswitch->priv;
switch (prop_id) {
case PROP_KILLSWITCH_STATE:
g_value_set_int (value, priv->state);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* urf_killswitch_finalize:
**/
static void
urf_killswitch_finalize (GObject *object)
{
UrfKillswitchPrivate *priv;
priv = URF_KILLSWITCH (object)->priv;
g_free (priv->object_path);
G_OBJECT_CLASS(urf_killswitch_parent_class)->finalize(object);
}
/**
* urf_killswitch_dispose:
**/
static void
urf_killswitch_dispose (GObject *object)
{
UrfKillswitchPrivate *priv;
g_return_if_fail (URF_IS_KILLSWITCH (object));
priv = URF_KILLSWITCH (object)->priv;
if (priv->proxy) {
g_object_unref (priv->proxy);
priv->proxy = NULL;
}
G_OBJECT_CLASS(urf_killswitch_parent_class)->dispose(object);
}
/**
* urf_killswitch_class_init:
* @klass: The UrfKillswitchClass
**/
static void
urf_killswitch_class_init (UrfKillswitchClass *klass)
{
GObjectClass *object_class = (GObjectClass *) klass;
GParamSpec *pspec;
object_class->set_property = urf_killswitch_set_property;
object_class->get_property = urf_killswitch_get_property;
object_class->finalize = urf_killswitch_finalize;
object_class->dispose = urf_killswitch_dispose;
/**
* UrfKillswitch:state:
*
* The state of the killswitch. See #UrfEnumState.
* <note>
* <para>
* Writing the states other than #URF_ENUM_STATE_UNBLOCKED
* or #URF_ENUM_STATE_SOFT_BLOCKED will be ignored. Also,
* the state writing may not take effect since it depends on
* the state of the hardware.
* </para>
* </note>
*
* Since: 0.3.0
*/
pspec = g_param_spec_int ("state",
"State", "The state of the killswitch",
URF_ENUM_STATE_NO_ADAPTER,
URF_ENUM_STATE_HARD_BLOCKED,
URF_ENUM_STATE_NO_ADAPTER,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_KILLSWITCH_STATE, pspec);
/**
* UrfKillswitch::state-changed:
* @client: the #UrfKillswitch instance that emitted the signal
* @state: the new state
*
* The state-changed signal is emitted when the killswitch state is changed.
* See #UrfEnumState.
*
* Since 0.3.0
**/
signals[URF_KILLSWITCH_STATE_CHANGED] =
g_signal_new ("state-changed",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (UrfKillswitchClass, state_changed),
NULL, NULL, g_cclosure_marshal_VOID__INT,
G_TYPE_NONE, 1, G_TYPE_INT);
g_type_class_add_private(klass, sizeof(UrfKillswitchPrivate));
}
/**
* urf_killswitch_init:
* @client: This class instance
**/
static void
urf_killswitch_init (UrfKillswitch *killswitch)
{
killswitch->priv = URF_KILLSWITCH_GET_PRIVATE (killswitch);
killswitch->priv->object_path = NULL;
}
/**
* urf_killswitch_new:
* @type: The killswitch type
*
* Creates a new #UrfKillswitch object.
*
* Return value: a new #UrfKillswitch object.
*
* Since: 0.3.0
**/
UrfKillswitch *
urf_killswitch_new (UrfEnumType type)
{
UrfKillswitch *killswitch;
if (type == URF_ENUM_TYPE_ALL)
return NULL;
if (urf_killswitch_object[type] != NULL) {
g_object_ref (urf_killswitch_object[type]);
} else {
killswitch = URF_KILLSWITCH (g_object_new (URF_TYPE_KILLSWITCH, NULL));
killswitch->priv->type = type;
if (urf_killswitch_startup (killswitch) == FALSE)
return NULL;
urf_killswitch_object[type] = (gpointer)killswitch;
g_object_add_weak_pointer (urf_killswitch_object[type], &urf_killswitch_object[type]);
}
return URF_KILLSWITCH (urf_killswitch_object[type]);
}
|