| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include "gupnp-connman-manager.h" |
| 22 |
#include "gupnp-context.h" |
| 23 |
|
| 24 |
#define SERVICE_CREATION_TIMEOUT 1000 |
| 25 |
|
| 26 |
typedef enum |
| 27 |
{ |
| 28 |
CM_SERVICE_STATE_ACTIVE = 1, |
| 29 |
CM_SERVICE_STATE_INACTIVE = 2 |
| 30 |
|
| 31 |
} CMServiceState; |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
GUPnPConnmanManager *manager; |
| 35 |
GUPnPContext *context; |
| 36 |
GDBusProxy *proxy; |
| 37 |
GCancellable *cancellable; |
| 38 |
CMServiceState current; |
| 39 |
guint sig_prop_id; |
| 40 |
guint port; |
| 41 |
gchar *iface; |
| 42 |
gchar *name; |
| 43 |
guint timeout; |
| 44 |
} CMService; |
| 45 |
|
| 46 |
struct _GUPnPConnmanManagerPrivate { |
| 47 |
GDBusProxy *manager_proxy; |
| 48 |
GSource *idle_context_creation_src; |
| 49 |
GHashTable *cm_services; |
| 50 |
guint sig_change_id; |
| 51 |
GDBusConnection *system_bus; |
| 52 |
GCancellable *cancellable; |
| 53 |
}; |
| 54 |
|
| 55 |
typedef struct _GUPnPConnmanManagerPrivate GUPnPConnmanManagerPrivate; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
struct _GUPnPConnmanManager { |
| 63 |
GUPnPContextManager parent; |
| 64 |
}; |
| 65 |
|
| 66 |
#define CM_DBUS_CONNMAN_NAME "net.connman" |
| 67 |
#define CM_DBUS_MANAGER_PATH "/" |
| 68 |
#define CM_DBUS_MANAGER_INTERFACE "net.connman.Manager" |
| 69 |
#define CM_DBUS_SERVICE_INTERFACE "net.connman.Service" |
| 70 |
|
| 71 |
#define DBUS_SERVICE_DBUS "org.freedesktop.DBus" |
| 72 |
#define DBUS_PATH_DBUS "/org/freedesktop/DBus" |
| 73 |
#define DBUS_INTERFACE_DBUS "org.freedesktop.DBus" |
| 74 |
|
| 75 |
#define LOOPBACK_IFACE "lo" |
| 76 |
|
| 77 |
G_DEFINE_TYPE_WITH_PRIVATE (GUPnPConnmanManager, |
| 78 |
gupnp_connman_manager, |
| 79 |
GUPNP_TYPE_CONTEXT_MANAGER); |
| 80 |
|
| 81 |
static gboolean |
| 82 |
loopback_context_create (gpointer data) |
| 83 |
{ |
| 84 |
GUPnPConnmanManager *manager; |
| 85 |
GUPnPContext *context; |
| 86 |
GError *error = NULL; |
| 87 |
guint port; |
| 88 |
GUPnPConnmanManagerPrivate *priv; |
| 89 |
|
| 90 |
manager = GUPNP_CONNMAN_MANAGER (data); |
| 91 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 92 |
priv->idle_context_creation_src = NULL; |
| 93 |
|
| 94 |
g_object_get (manager, "port", &port, NULL); |
| 95 |
|
| 96 |
GSocketFamily family = gupnp_context_manager_get_socket_family ( |
| 97 |
GUPNP_CONTEXT_MANAGER (manager)); |
| 98 |
|
| 99 |
if (family == G_SOCKET_FAMILY_INVALID || |
| 100 |
family == G_SOCKET_FAMILY_IPV4) { |
| 101 |
GInetAddress *addr = |
| 102 |
g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); |
| 103 |
|
| 104 |
context = g_initable_new (GUPNP_TYPE_CONTEXT, |
| 105 |
NULL, |
| 106 |
&error, |
| 107 |
"address", |
| 108 |
addr, |
| 109 |
"port", |
| 110 |
port, |
| 111 |
NULL); |
| 112 |
if (error) { |
| 113 |
g_warning ("Error creating GUPnP context: %s\n", |
| 114 |
error->message); |
| 115 |
|
| 116 |
g_clear_error (&error); |
| 117 |
} else { |
| 118 |
g_signal_emit_by_name (manager, |
| 119 |
"context-available", |
| 120 |
context); |
| 121 |
} |
| 122 |
|
| 123 |
g_object_unref (context); |
| 124 |
g_object_unref (addr); |
| 125 |
} |
| 126 |
|
| 127 |
if (family == G_SOCKET_FAMILY_INVALID || |
| 128 |
family == G_SOCKET_FAMILY_IPV6) { |
| 129 |
GInetAddress *addr = |
| 130 |
g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV6); |
| 131 |
context = g_initable_new (GUPNP_TYPE_CONTEXT, |
| 132 |
NULL, |
| 133 |
&error, |
| 134 |
"address", |
| 135 |
addr, |
| 136 |
"port", |
| 137 |
port, |
| 138 |
NULL); |
| 139 |
if (error) { |
| 140 |
g_warning ("Error creating GUPnP context: %s\n", |
| 141 |
error->message); |
| 142 |
|
| 143 |
g_clear_error (&error); |
| 144 |
} else { |
| 145 |
g_signal_emit_by_name (manager, |
| 146 |
"context-available", |
| 147 |
context); |
| 148 |
} |
| 149 |
|
| 150 |
g_object_unref (context); |
| 151 |
g_object_unref (addr); |
| 152 |
} |
| 153 |
|
| 154 |
return FALSE; |
| 155 |
} |
| 156 |
|
| 157 |
static gboolean |
| 158 |
service_context_create (CMService *cm_service) |
| 159 |
{ |
| 160 |
GError *error = NULL; |
| 161 |
|
| 162 |
cm_service->context = g_initable_new ( |
| 163 |
GUPNP_TYPE_CONTEXT, |
| 164 |
NULL, |
| 165 |
&error, |
| 166 |
"interface", |
| 167 |
cm_service->iface, |
| 168 |
"network", |
| 169 |
cm_service->name, |
| 170 |
"port", |
| 171 |
cm_service->port, |
| 172 |
"address-family", |
| 173 |
gupnp_context_manager_get_socket_family ( |
| 174 |
GUPNP_CONTEXT_MANAGER (cm_service->manager)), |
| 175 |
NULL); |
| 176 |
|
| 177 |
if (error != NULL) { |
| 178 |
g_warning ("Error creating GUPnP context: %s", error->message); |
| 179 |
g_error_free (error); |
| 180 |
|
| 181 |
return FALSE; |
| 182 |
} |
| 183 |
|
| 184 |
g_signal_emit_by_name (cm_service->manager, |
| 185 |
"context-available", |
| 186 |
cm_service->context); |
| 187 |
|
| 188 |
return TRUE; |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
service_context_delete (CMService *cm_service) |
| 193 |
{ |
| 194 |
|
| 195 |
g_signal_emit_by_name (cm_service->manager, |
| 196 |
"context-unavailable", |
| 197 |
cm_service->context); |
| 198 |
|
| 199 |
g_object_unref (cm_service->context); |
| 200 |
cm_service->context = NULL; |
| 201 |
} |
| 202 |
|
| 203 |
static gboolean |
| 204 |
service_context_create_timeout (CMService *cm_service) |
| 205 |
{ |
| 206 |
cm_service->timeout = 0; |
| 207 |
|
| 208 |
g_return_val_if_fail (cm_service->current == CM_SERVICE_STATE_ACTIVE, FALSE); |
| 209 |
|
| 210 |
if (service_context_create (cm_service) == FALSE) { |
| 211 |
cm_service->current = CM_SERVICE_STATE_INACTIVE; |
| 212 |
} |
| 213 |
|
| 214 |
return FALSE; |
| 215 |
} |
| 216 |
|
| 217 |
static void |
| 218 |
service_context_remove_creation_timeout (CMService *cm_service) |
| 219 |
{ |
| 220 |
if (cm_service->timeout) { |
| 221 |
g_source_remove (cm_service->timeout); |
| 222 |
cm_service->timeout = 0; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
service_context_install_creation_timeout (CMService *cm_service) |
| 228 |
{ |
| 229 |
service_context_remove_creation_timeout (cm_service); |
| 230 |
|
| 231 |
cm_service->timeout = g_timeout_add (SERVICE_CREATION_TIMEOUT, |
| 232 |
(GSourceFunc) service_context_create_timeout, |
| 233 |
cm_service); |
| 234 |
} |
| 235 |
|
| 236 |
static void |
| 237 |
service_context_update (CMService *cm_service, CMServiceState new_state) |
| 238 |
{ |
| 239 |
if (cm_service->current != new_state) { |
| 240 |
if (new_state == CM_SERVICE_STATE_ACTIVE) { |
| 241 |
service_context_install_creation_timeout (cm_service); |
| 242 |
} else if (new_state == CM_SERVICE_STATE_INACTIVE) { |
| 243 |
service_context_remove_creation_timeout (cm_service); |
| 244 |
if (cm_service->context != NULL) |
| 245 |
service_context_delete (cm_service); |
| 246 |
} |
| 247 |
|
| 248 |
cm_service->current = new_state; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
on_service_property_changed (CMService *cm_service, |
| 254 |
const gchar *name, |
| 255 |
GVariant *value) |
| 256 |
{ |
| 257 |
CMServiceState new_state; |
| 258 |
const gchar *state_str; |
| 259 |
|
| 260 |
if (g_strcmp0 (name, "Name") == 0) { |
| 261 |
g_free (cm_service->name); |
| 262 |
g_variant_get (value, "s", &cm_service->name); |
| 263 |
|
| 264 |
if (cm_service->context != NULL && |
| 265 |
g_strcmp0 (cm_service->name, |
| 266 |
gssdp_client_get_network (GSSDP_CLIENT (cm_service->context))) != 0) |
| 267 |
g_object_set (G_OBJECT (cm_service->context), |
| 268 |
"network", |
| 269 |
cm_service->name, |
| 270 |
NULL); |
| 271 |
|
| 272 |
} else if (g_strcmp0 (name, "Ethernet") == 0) { |
| 273 |
g_free (cm_service->iface); |
| 274 |
g_variant_lookup (value, "Interface", "s", &cm_service->iface); |
| 275 |
|
| 276 |
if (cm_service->context != NULL && |
| 277 |
g_strcmp0 (cm_service->iface, |
| 278 |
gssdp_client_get_interface (GSSDP_CLIENT (cm_service->context))) != 0) { |
| 279 |
service_context_delete (cm_service); |
| 280 |
service_context_create (cm_service); |
| 281 |
} |
| 282 |
|
| 283 |
} else if (g_strcmp0 (name, "State") == 0) { |
| 284 |
state_str = g_variant_get_string (value, 0); |
| 285 |
|
| 286 |
if ((g_strcmp0 (state_str, "online") == 0) || |
| 287 |
(g_strcmp0 (state_str, "ready") == 0)) |
| 288 |
new_state = CM_SERVICE_STATE_ACTIVE; |
| 289 |
else |
| 290 |
new_state = CM_SERVICE_STATE_INACTIVE; |
| 291 |
|
| 292 |
service_context_update (cm_service, new_state); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
static void |
| 297 |
on_service_property_signal (GDBusConnection *connection, |
| 298 |
const gchar *sender_name, |
| 299 |
const gchar *object_path, |
| 300 |
const gchar *interface_name, |
| 301 |
const gchar *signal_name, |
| 302 |
GVariant *parameters, |
| 303 |
gpointer user_data) |
| 304 |
{ |
| 305 |
CMService *cm_service; |
| 306 |
GVariant *value; |
| 307 |
gchar *name; |
| 308 |
|
| 309 |
cm_service = (CMService *) user_data; |
| 310 |
g_variant_get (parameters, "(&sv)", &name, &value); |
| 311 |
|
| 312 |
on_service_property_changed (cm_service, name, value); |
| 313 |
|
| 314 |
g_variant_unref (value); |
| 315 |
} |
| 316 |
|
| 317 |
static CMService * |
| 318 |
cm_service_new (GUPnPConnmanManager *manager, |
| 319 |
GDBusProxy *service_proxy) |
| 320 |
{ |
| 321 |
CMService *cm_service; |
| 322 |
|
| 323 |
cm_service = g_slice_new0 (CMService); |
| 324 |
|
| 325 |
cm_service->manager = manager; |
| 326 |
cm_service->proxy = service_proxy; |
| 327 |
cm_service->current = CM_SERVICE_STATE_INACTIVE; |
| 328 |
|
| 329 |
return cm_service; |
| 330 |
} |
| 331 |
|
| 332 |
static void |
| 333 |
cm_service_free (CMService *cm_service) |
| 334 |
{ |
| 335 |
if (cm_service->proxy != NULL) { |
| 336 |
GDBusConnection *cnx; |
| 337 |
|
| 338 |
cnx = g_dbus_proxy_get_connection (cm_service->proxy); |
| 339 |
|
| 340 |
if (cm_service->sig_prop_id) { |
| 341 |
g_dbus_connection_signal_unsubscribe (cnx, |
| 342 |
cm_service->sig_prop_id); |
| 343 |
cm_service->sig_prop_id = 0; |
| 344 |
} |
| 345 |
|
| 346 |
g_object_unref (cm_service->proxy); |
| 347 |
} |
| 348 |
|
| 349 |
if (cm_service->cancellable != NULL) { |
| 350 |
g_cancellable_cancel (cm_service->cancellable); |
| 351 |
g_object_unref (cm_service->cancellable); |
| 352 |
} |
| 353 |
|
| 354 |
service_context_remove_creation_timeout (cm_service); |
| 355 |
|
| 356 |
if (cm_service->context != NULL) { |
| 357 |
g_signal_emit_by_name (cm_service->manager, |
| 358 |
"context-unavailable", |
| 359 |
cm_service->context); |
| 360 |
|
| 361 |
g_object_unref (cm_service->context); |
| 362 |
} |
| 363 |
|
| 364 |
g_free (cm_service->iface); |
| 365 |
g_free (cm_service->name); |
| 366 |
g_slice_free (CMService, cm_service); |
| 367 |
} |
| 368 |
|
| 369 |
static void |
| 370 |
get_properties_cb (GObject *source_object, |
| 371 |
GAsyncResult *res, |
| 372 |
gpointer user_data) |
| 373 |
{ |
| 374 |
CMService *cm_service; |
| 375 |
GVariant *ret; |
| 376 |
GVariant *dict; |
| 377 |
GVariant *value; |
| 378 |
GVariantIter iter; |
| 379 |
gchar *key; |
| 380 |
GError *error = NULL; |
| 381 |
|
| 382 |
ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), |
| 383 |
res, |
| 384 |
&error); |
| 385 |
|
| 386 |
if (error != NULL) { |
| 387 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 388 |
g_warning ("Error fetching properties: %s", error->message); |
| 389 |
g_error_free (error); |
| 390 |
|
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
if (ret == NULL) { |
| 395 |
g_warning ("Failed fetching properties but no error"); |
| 396 |
|
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
if (g_variant_is_container (ret) != TRUE) |
| 401 |
{ |
| 402 |
g_warning ("Unexpected result type: %s", g_variant_get_type_string (ret)); |
| 403 |
g_variant_unref (ret); |
| 404 |
|
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
cm_service = user_data; |
| 409 |
|
| 410 |
dict = g_variant_get_child_value (ret, 0); |
| 411 |
g_variant_iter_init (&iter, dict); |
| 412 |
|
| 413 |
while (g_variant_iter_loop (&iter, "{sv}", &key, &value)) |
| 414 |
on_service_property_changed (cm_service, key, value); |
| 415 |
|
| 416 |
g_variant_unref (dict); |
| 417 |
g_variant_unref (ret); |
| 418 |
} |
| 419 |
|
| 420 |
static void |
| 421 |
cm_service_use (GUPnPConnmanManager *manager, |
| 422 |
CMService *cm_service) |
| 423 |
{ |
| 424 |
GDBusConnection *connection; |
| 425 |
|
| 426 |
connection = g_dbus_proxy_get_connection (cm_service->proxy); |
| 427 |
|
| 428 |
cm_service->sig_prop_id = g_dbus_connection_signal_subscribe ( |
| 429 |
connection, |
| 430 |
CM_DBUS_CONNMAN_NAME, |
| 431 |
CM_DBUS_SERVICE_INTERFACE, |
| 432 |
"PropertyChanged", |
| 433 |
g_dbus_proxy_get_object_path (cm_service->proxy), |
| 434 |
NULL, |
| 435 |
G_DBUS_SIGNAL_FLAGS_NONE, |
| 436 |
on_service_property_signal, |
| 437 |
cm_service, |
| 438 |
NULL); |
| 439 |
|
| 440 |
g_dbus_proxy_call (cm_service->proxy, |
| 441 |
"GetProperties", |
| 442 |
NULL, |
| 443 |
G_DBUS_CALL_FLAGS_NONE, |
| 444 |
-1, |
| 445 |
cm_service->cancellable, |
| 446 |
get_properties_cb, |
| 447 |
cm_service); |
| 448 |
|
| 449 |
if (cm_service->current == CM_SERVICE_STATE_ACTIVE) |
| 450 |
if (service_context_create (cm_service) == FALSE) |
| 451 |
cm_service->current = CM_SERVICE_STATE_INACTIVE; |
| 452 |
} |
| 453 |
static void |
| 454 |
cm_service_update (CMService *cm_service, GVariant *dict, guint port) |
| 455 |
{ |
| 456 |
CMServiceState new_state; |
| 457 |
GVariant *eth; |
| 458 |
gchar *iface; |
| 459 |
gchar *name; |
| 460 |
gchar *state; |
| 461 |
gboolean is_name; |
| 462 |
gboolean is_iface; |
| 463 |
|
| 464 |
is_iface = FALSE; |
| 465 |
iface = NULL; |
| 466 |
name = NULL; |
| 467 |
|
| 468 |
is_name = g_variant_lookup (dict, "Name", "s", &name); |
| 469 |
|
| 470 |
eth = g_variant_lookup_value (dict, "Ethernet", G_VARIANT_TYPE_VARDICT); |
| 471 |
|
| 472 |
if (eth != NULL) { |
| 473 |
is_iface = g_variant_lookup (eth, "Interface", "s", &iface); |
| 474 |
g_variant_unref (eth); |
| 475 |
} |
| 476 |
|
| 477 |
new_state = CM_SERVICE_STATE_INACTIVE; |
| 478 |
|
| 479 |
if (g_variant_lookup (dict, "State", "&s", &state) != FALSE) |
| 480 |
if ((g_strcmp0 (state, "online") == 0) || |
| 481 |
(g_strcmp0 (state, "ready") == 0)) |
| 482 |
new_state = CM_SERVICE_STATE_ACTIVE; |
| 483 |
|
| 484 |
if (is_name && (g_strcmp0 (cm_service->name, name) != 0)) { |
| 485 |
g_free (cm_service->name); |
| 486 |
cm_service->name = name; |
| 487 |
|
| 488 |
if (cm_service->context != NULL) |
| 489 |
g_object_set (G_OBJECT (cm_service->context), |
| 490 |
"network", |
| 491 |
cm_service->name, |
| 492 |
NULL); |
| 493 |
} |
| 494 |
|
| 495 |
if (is_iface && (g_strcmp0 (cm_service->iface, iface) != 0)) { |
| 496 |
g_free (cm_service->iface); |
| 497 |
cm_service->iface = iface; |
| 498 |
|
| 499 |
if (cm_service->context != NULL) { |
| 500 |
service_context_delete (cm_service); |
| 501 |
service_context_create (cm_service); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
cm_service->port = port; |
| 506 |
service_context_update (cm_service, new_state); |
| 507 |
} |
| 508 |
|
| 509 |
static void |
| 510 |
service_proxy_new_cb (GObject *source_object, |
| 511 |
GAsyncResult *res, |
| 512 |
gpointer user_data) |
| 513 |
{ |
| 514 |
GUPnPConnmanManager *manager; |
| 515 |
GUPnPConnmanManagerPrivate *priv; |
| 516 |
GDBusProxy *service_proxy; |
| 517 |
GError *error = NULL; |
| 518 |
CMService *cm_service; |
| 519 |
const gchar *path; |
| 520 |
|
| 521 |
service_proxy = g_dbus_proxy_new_for_bus_finish (res, &error); |
| 522 |
|
| 523 |
if (error != NULL) { |
| 524 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 525 |
g_warning ("Failed to create D-Bus proxy: %s", error->message); |
| 526 |
g_error_free (error); |
| 527 |
|
| 528 |
return; |
| 529 |
} |
| 530 |
|
| 531 |
manager = GUPNP_CONNMAN_MANAGER (user_data); |
| 532 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 533 |
path = g_dbus_proxy_get_object_path (service_proxy); |
| 534 |
cm_service = g_hash_table_lookup (priv->cm_services, path); |
| 535 |
|
| 536 |
if (cm_service == NULL) { |
| 537 |
g_object_unref (service_proxy); |
| 538 |
|
| 539 |
return; |
| 540 |
} |
| 541 |
|
| 542 |
cm_service->proxy = service_proxy; |
| 543 |
cm_service_use (manager, cm_service); |
| 544 |
} |
| 545 |
|
| 546 |
static void |
| 547 |
cm_service_add (GUPnPConnmanManager *manager, |
| 548 |
GVariant *dict, |
| 549 |
gchar *path, |
| 550 |
guint port) |
| 551 |
{ |
| 552 |
CMServiceState new_state; |
| 553 |
CMService *cm_service; |
| 554 |
GVariant *eth; |
| 555 |
gchar *iface; |
| 556 |
gchar *name; |
| 557 |
gchar *state; |
| 558 |
GUPnPConnmanManagerPrivate *priv; |
| 559 |
|
| 560 |
iface = NULL; |
| 561 |
name = NULL; |
| 562 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 563 |
|
| 564 |
g_variant_lookup (dict, "Name", "s", &name); |
| 565 |
|
| 566 |
eth = g_variant_lookup_value (dict, "Ethernet", G_VARIANT_TYPE_VARDICT); |
| 567 |
|
| 568 |
if (eth != NULL) { |
| 569 |
g_variant_lookup (eth, "Interface", "s", &iface); |
| 570 |
g_variant_unref (eth); |
| 571 |
} |
| 572 |
|
| 573 |
new_state = CM_SERVICE_STATE_INACTIVE; |
| 574 |
|
| 575 |
if (g_variant_lookup (dict, "State", "&s", &state) != FALSE) |
| 576 |
if ((g_strcmp0 (state, "online") == 0) || |
| 577 |
(g_strcmp0 (state, "ready") == 0)) |
| 578 |
new_state = CM_SERVICE_STATE_ACTIVE; |
| 579 |
|
| 580 |
cm_service = cm_service_new (manager, NULL); |
| 581 |
|
| 582 |
cm_service->name = name; |
| 583 |
cm_service->iface = iface; |
| 584 |
cm_service->port = port; |
| 585 |
cm_service->current = new_state; |
| 586 |
|
| 587 |
g_hash_table_insert (priv->cm_services, |
| 588 |
g_strdup (path), |
| 589 |
cm_service); |
| 590 |
|
| 591 |
cm_service->cancellable = g_cancellable_new (); |
| 592 |
|
| 593 |
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, |
| 594 |
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 595 |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 596 |
NULL, |
| 597 |
CM_DBUS_CONNMAN_NAME, |
| 598 |
path, |
| 599 |
CM_DBUS_SERVICE_INTERFACE, |
| 600 |
cm_service->cancellable, |
| 601 |
service_proxy_new_cb, |
| 602 |
manager); |
| 603 |
} |
| 604 |
|
| 605 |
static void |
| 606 |
services_array_add (GUPnPConnmanManager *manager, GVariant *data) |
| 607 |
{ |
| 608 |
GVariant *dict; |
| 609 |
CMService *cm_service; |
| 610 |
gchar *path; |
| 611 |
GVariantIter iter; |
| 612 |
GVariantIter dict_iter; |
| 613 |
guint port; |
| 614 |
GUPnPConnmanManagerPrivate *priv; |
| 615 |
|
| 616 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 617 |
g_object_get (manager, "port", &port, NULL); |
| 618 |
g_variant_iter_init (&iter, data); |
| 619 |
|
| 620 |
while (g_variant_iter_loop (&iter, "(&o@a{sv})", &path, &dict)) { |
| 621 |
|
| 622 |
if (path == NULL) |
| 623 |
continue; |
| 624 |
|
| 625 |
if (dict == NULL) |
| 626 |
continue; |
| 627 |
|
| 628 |
if (g_variant_iter_init (&dict_iter, dict) == 0) |
| 629 |
continue; |
| 630 |
|
| 631 |
cm_service = g_hash_table_lookup (priv->cm_services, |
| 632 |
path); |
| 633 |
|
| 634 |
if (cm_service == NULL) |
| 635 |
cm_service_add (manager, dict, path, port); |
| 636 |
else |
| 637 |
cm_service_update (cm_service, dict, port); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
static void |
| 642 |
services_array_remove (GUPnPConnmanManager *manager, GVariant *data) |
| 643 |
{ |
| 644 |
GUPnPConnmanManagerPrivate *priv; |
| 645 |
CMService *cm_service; |
| 646 |
char *path; |
| 647 |
GVariantIter iter; |
| 648 |
|
| 649 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 650 |
g_variant_iter_init (&iter, data); |
| 651 |
|
| 652 |
while (g_variant_iter_next (&iter, "&o", &path)) { |
| 653 |
if (path == NULL) |
| 654 |
continue; |
| 655 |
|
| 656 |
cm_service = g_hash_table_lookup (priv->cm_services, path); |
| 657 |
|
| 658 |
if (cm_service == NULL) |
| 659 |
continue; |
| 660 |
|
| 661 |
g_hash_table_remove (priv->cm_services, path); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
static void |
| 666 |
on_manager_svc_changed_signal (GDBusConnection *connection, |
| 667 |
const gchar *sender_name, |
| 668 |
const gchar *object_path, |
| 669 |
const gchar *interface_name, |
| 670 |
const gchar *signal_name, |
| 671 |
GVariant *parameters, |
| 672 |
gpointer user_data) |
| 673 |
{ |
| 674 |
GUPnPConnmanManager *manager; |
| 675 |
GVariant *add_array; |
| 676 |
GVariant *remove_array; |
| 677 |
|
| 678 |
manager = GUPNP_CONNMAN_MANAGER (user_data); |
| 679 |
|
| 680 |
add_array = g_variant_get_child_value (parameters, 0); |
| 681 |
remove_array = g_variant_get_child_value (parameters, 1); |
| 682 |
|
| 683 |
if ((add_array != NULL) && (g_variant_n_children (add_array) > 0)) |
| 684 |
services_array_add (manager, add_array); |
| 685 |
|
| 686 |
if ((remove_array != NULL) && (g_variant_n_children (remove_array) > 0)) |
| 687 |
services_array_remove (manager, remove_array); |
| 688 |
|
| 689 |
g_variant_unref (add_array); |
| 690 |
g_variant_unref (remove_array); |
| 691 |
} |
| 692 |
|
| 693 |
static void |
| 694 |
get_services_cb (GObject *source_object, |
| 695 |
GAsyncResult *res, |
| 696 |
gpointer user_data) |
| 697 |
{ |
| 698 |
GUPnPConnmanManager *manager; |
| 699 |
GVariant *ret; |
| 700 |
GVariant *services_array; |
| 701 |
GError *error = NULL; |
| 702 |
|
| 703 |
ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), |
| 704 |
res, |
| 705 |
&error); |
| 706 |
|
| 707 |
if (error != NULL) { |
| 708 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 709 |
g_warning ("Error fetching service list: %s", error->message); |
| 710 |
g_error_free (error); |
| 711 |
|
| 712 |
return; |
| 713 |
} |
| 714 |
|
| 715 |
if (ret == NULL) { |
| 716 |
g_warning ("Failed fetching list of services but no error"); |
| 717 |
|
| 718 |
return; |
| 719 |
} |
| 720 |
|
| 721 |
if (g_variant_is_container (ret) != TRUE) |
| 722 |
{ |
| 723 |
g_warning ("Wrong format result"); |
| 724 |
g_variant_unref (ret); |
| 725 |
|
| 726 |
return; |
| 727 |
} |
| 728 |
|
| 729 |
manager = GUPNP_CONNMAN_MANAGER (user_data); |
| 730 |
services_array = g_variant_get_child_value (ret, 0); |
| 731 |
services_array_add (manager, services_array); |
| 732 |
|
| 733 |
g_variant_unref (services_array); |
| 734 |
g_variant_unref (ret); |
| 735 |
} |
| 736 |
|
| 737 |
static void |
| 738 |
schedule_loopback_context_creation (GUPnPConnmanManager *manager) |
| 739 |
{ |
| 740 |
GUPnPConnmanManagerPrivate *priv; |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 746 |
priv->idle_context_creation_src = g_idle_source_new (); |
| 747 |
|
| 748 |
g_source_attach (priv->idle_context_creation_src, |
| 749 |
g_main_context_get_thread_default ()); |
| 750 |
|
| 751 |
g_source_set_callback (priv->idle_context_creation_src, |
| 752 |
loopback_context_create, |
| 753 |
manager, |
| 754 |
NULL); |
| 755 |
|
| 756 |
g_source_unref (priv->idle_context_creation_src); |
| 757 |
} |
| 758 |
|
| 759 |
static void |
| 760 |
init_connman_manager (GUPnPConnmanManager *manager) |
| 761 |
{ |
| 762 |
GUPnPConnmanManagerPrivate *priv; |
| 763 |
GError *error = NULL; |
| 764 |
GDBusConnection *connection; |
| 765 |
|
| 766 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 767 |
|
| 768 |
priv->manager_proxy = g_dbus_proxy_new_for_bus_sync ( |
| 769 |
G_BUS_TYPE_SYSTEM, |
| 770 |
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 771 |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 772 |
NULL, |
| 773 |
CM_DBUS_CONNMAN_NAME, |
| 774 |
CM_DBUS_MANAGER_PATH, |
| 775 |
CM_DBUS_MANAGER_INTERFACE, |
| 776 |
NULL, |
| 777 |
&error); |
| 778 |
|
| 779 |
if (error != NULL) { |
| 780 |
g_warning ("Failed to connect to Connman: %s", error->message); |
| 781 |
g_error_free (error); |
| 782 |
|
| 783 |
return; |
| 784 |
} |
| 785 |
|
| 786 |
connection = g_dbus_proxy_get_connection (priv->manager_proxy); |
| 787 |
|
| 788 |
if (connection == NULL) { |
| 789 |
g_warning ("Failed to get DBus Connection"); |
| 790 |
g_object_unref (priv->manager_proxy); |
| 791 |
priv->manager_proxy = NULL; |
| 792 |
|
| 793 |
return; |
| 794 |
} |
| 795 |
|
| 796 |
g_dbus_proxy_call (priv->manager_proxy, |
| 797 |
"GetServices", |
| 798 |
NULL, |
| 799 |
G_DBUS_CALL_FLAGS_NONE, |
| 800 |
-1, |
| 801 |
priv->cancellable, |
| 802 |
get_services_cb, |
| 803 |
manager); |
| 804 |
|
| 805 |
priv->sig_change_id = g_dbus_connection_signal_subscribe ( |
| 806 |
connection, |
| 807 |
CM_DBUS_CONNMAN_NAME, |
| 808 |
CM_DBUS_MANAGER_INTERFACE, |
| 809 |
"ServicesChanged", |
| 810 |
CM_DBUS_MANAGER_PATH, |
| 811 |
NULL, |
| 812 |
G_DBUS_SIGNAL_FLAGS_NONE, |
| 813 |
on_manager_svc_changed_signal, |
| 814 |
manager, |
| 815 |
NULL); |
| 816 |
} |
| 817 |
|
| 818 |
static void |
| 819 |
gupnp_connman_manager_dispose (GObject *object) |
| 820 |
{ |
| 821 |
GUPnPConnmanManager *manager; |
| 822 |
GUPnPConnmanManagerPrivate *priv; |
| 823 |
GObjectClass *object_class; |
| 824 |
GDBusConnection *cnx; |
| 825 |
|
| 826 |
manager = GUPNP_CONNMAN_MANAGER (object); |
| 827 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 828 |
cnx = g_dbus_proxy_get_connection (priv->manager_proxy); |
| 829 |
|
| 830 |
if (priv->sig_change_id) { |
| 831 |
g_dbus_connection_signal_unsubscribe (cnx, priv->sig_change_id); |
| 832 |
priv->sig_change_id = 0; |
| 833 |
} |
| 834 |
|
| 835 |
if (priv->idle_context_creation_src) { |
| 836 |
g_source_destroy (priv->idle_context_creation_src); |
| 837 |
priv->idle_context_creation_src = NULL; |
| 838 |
} |
| 839 |
|
| 840 |
if (priv->manager_proxy != NULL) { |
| 841 |
g_object_unref (priv->manager_proxy); |
| 842 |
priv->manager_proxy = NULL; |
| 843 |
} |
| 844 |
|
| 845 |
if (priv->cm_services) { |
| 846 |
g_hash_table_destroy (priv->cm_services); |
| 847 |
priv->cm_services = NULL; |
| 848 |
} |
| 849 |
|
| 850 |
if (priv->cancellable != NULL) { |
| 851 |
g_cancellable_cancel (priv->cancellable); |
| 852 |
g_object_unref (priv->cancellable); |
| 853 |
priv->cancellable = NULL; |
| 854 |
} |
| 855 |
|
| 856 |
g_clear_object (&(priv->system_bus)); |
| 857 |
|
| 858 |
|
| 859 |
object_class = G_OBJECT_CLASS (gupnp_connman_manager_parent_class); |
| 860 |
object_class->dispose (object); |
| 861 |
} |
| 862 |
|
| 863 |
static void |
| 864 |
gupnp_connman_manager_constructed (GObject *object) |
| 865 |
{ |
| 866 |
GUPnPConnmanManager *manager; |
| 867 |
GObjectClass *object_class; |
| 868 |
GUPnPConnmanManagerPrivate *priv; |
| 869 |
|
| 870 |
manager = GUPNP_CONNMAN_MANAGER (object); |
| 871 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 872 |
|
| 873 |
priv->cancellable = g_cancellable_new (); |
| 874 |
priv->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, |
| 875 |
NULL, |
| 876 |
NULL); |
| 877 |
|
| 878 |
init_connman_manager (manager); |
| 879 |
|
| 880 |
schedule_loopback_context_creation (manager); |
| 881 |
|
| 882 |
|
| 883 |
object_class = G_OBJECT_CLASS (gupnp_connman_manager_parent_class); |
| 884 |
|
| 885 |
if (object_class->constructed != NULL) { |
| 886 |
object_class->constructed (object); |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
static void |
| 891 |
gupnp_connman_manager_init (GUPnPConnmanManager *manager) |
| 892 |
{ |
| 893 |
GUPnPConnmanManagerPrivate *priv; |
| 894 |
|
| 895 |
priv = gupnp_connman_manager_get_instance_private (manager); |
| 896 |
|
| 897 |
priv->cm_services = g_hash_table_new_full ( |
| 898 |
g_str_hash, |
| 899 |
g_str_equal, |
| 900 |
(GDestroyNotify) g_free, |
| 901 |
(GDestroyNotify) cm_service_free); |
| 902 |
} |
| 903 |
|
| 904 |
static void |
| 905 |
gupnp_connman_manager_class_init (GUPnPConnmanManagerClass *klass) |
| 906 |
{ |
| 907 |
GObjectClass *object_class; |
| 908 |
|
| 909 |
object_class = G_OBJECT_CLASS (klass); |
| 910 |
|
| 911 |
object_class->constructed = gupnp_connman_manager_constructed; |
| 912 |
object_class->dispose = gupnp_connman_manager_dispose; |
| 913 |
} |
| 914 |
|
| 915 |
gboolean |
| 916 |
gupnp_connman_manager_is_available (void) |
| 917 |
{ |
| 918 |
GDBusProxy *dbus_proxy; |
| 919 |
GVariant *ret_values; |
| 920 |
GError *error = NULL; |
| 921 |
gboolean ret = FALSE; |
| 922 |
|
| 923 |
dbus_proxy = g_dbus_proxy_new_for_bus_sync ( |
| 924 |
G_BUS_TYPE_SYSTEM, |
| 925 |
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, |
| 926 |
NULL, |
| 927 |
DBUS_SERVICE_DBUS, |
| 928 |
DBUS_PATH_DBUS, |
| 929 |
DBUS_INTERFACE_DBUS, |
| 930 |
NULL, |
| 931 |
&error); |
| 932 |
|
| 933 |
|
| 934 |
if (error != NULL) { |
| 935 |
g_warning ("Failed to connect to Connman: %s", error->message); |
| 936 |
g_error_free (error); |
| 937 |
|
| 938 |
return ret; |
| 939 |
} |
| 940 |
|
| 941 |
ret_values = g_dbus_proxy_call_sync ( |
| 942 |
dbus_proxy, |
| 943 |
"NameHasOwner", |
| 944 |
g_variant_new ("(s)", CM_DBUS_CONNMAN_NAME), |
| 945 |
G_DBUS_CALL_FLAGS_NONE, |
| 946 |
-1, |
| 947 |
NULL, |
| 948 |
&error); |
| 949 |
|
| 950 |
if (error != NULL) { |
| 951 |
g_warning ("%s.NameHasOwner() failed: %s", |
| 952 |
DBUS_INTERFACE_DBUS, |
| 953 |
error->message); |
| 954 |
|
| 955 |
g_error_free (error); |
| 956 |
|
| 957 |
} else { |
| 958 |
g_variant_get_child (ret_values, 0, "b", &ret); |
| 959 |
g_variant_unref (ret_values); |
| 960 |
} |
| 961 |
|
| 962 |
g_object_unref (dbus_proxy); |
| 963 |
|
| 964 |
return ret; |
| 965 |
} |