Save profile when enabling device while in offline mode
[framework/connectivity/connman.git] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection = NULL;
34
35 struct connman_device {
36         struct connman_element element;
37         enum connman_device_type type;
38         enum connman_device_mode mode;
39         connman_bool_t offlinemode;
40         connman_bool_t blocked;
41         connman_bool_t powered;
42         connman_bool_t powered_pending;
43         connman_bool_t powered_persistent;
44         connman_bool_t scanning;
45         connman_bool_t disconnected;
46         connman_bool_t reconnect;
47         connman_uint16_t scan_interval;
48         char *name;
49         char *node;
50         char *address;
51         char *interface;
52         char *control;
53         char *ident;
54         int phyindex;
55         unsigned int connections;
56         guint scan_timeout;
57
58         struct connman_device_driver *driver;
59         void *driver_data;
60
61         connman_bool_t registered;
62
63         char *last_network;
64         struct connman_network *network;
65         GHashTable *networks;
66
67         DBusMessage *pending;
68         guint timeout;
69 };
70
71 static gboolean device_scan_trigger(gpointer user_data)
72 {
73         struct connman_device *device = user_data;
74
75         DBG("device %p", device);
76
77         if (device->driver == NULL) {
78                 device->scan_timeout = 0;
79                 return FALSE;
80         }
81
82         if (device->driver->scan)
83                 device->driver->scan(device);
84
85         return TRUE;
86 }
87
88 static void clear_scan_trigger(struct connman_device *device)
89 {
90         if (device->scan_timeout > 0) {
91                 g_source_remove(device->scan_timeout);
92                 device->scan_timeout = 0;
93         }
94 }
95
96 static void reset_scan_trigger(struct connman_device *device)
97 {
98         clear_scan_trigger(device);
99
100         if (device->scan_interval > 0) {
101                 guint interval = device->scan_interval;
102                 device->scan_timeout = g_timeout_add_seconds(interval,
103                                         device_scan_trigger, device);
104         }
105 }
106
107 static void force_scan_trigger(struct connman_device *device)
108 {
109         clear_scan_trigger(device);
110
111         device->scan_timeout = g_timeout_add_seconds(5,
112                                         device_scan_trigger, device);
113 }
114
115 void connman_device_schedule_scan(struct connman_device *device)
116 {
117         reset_scan_trigger(device);
118 }
119
120 static const char *type2description(enum connman_device_type type)
121 {
122         switch (type) {
123         case CONNMAN_DEVICE_TYPE_UNKNOWN:
124         case CONNMAN_DEVICE_TYPE_VENDOR:
125                 break;
126         case CONNMAN_DEVICE_TYPE_ETHERNET:
127                 return "Ethernet";
128         case CONNMAN_DEVICE_TYPE_WIFI:
129                 return "Wireless";
130         case CONNMAN_DEVICE_TYPE_WIMAX:
131                 return "WiMAX";
132         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
133                 return "Bluetooth";
134         case CONNMAN_DEVICE_TYPE_GPS:
135                 return "GPS";
136         case CONNMAN_DEVICE_TYPE_CELLULAR:
137                 return "Cellular";
138         }
139
140         return NULL;
141 }
142
143 static const char *type2string(enum connman_device_type type)
144 {
145         switch (type) {
146         case CONNMAN_DEVICE_TYPE_UNKNOWN:
147         case CONNMAN_DEVICE_TYPE_VENDOR:
148                 break;
149         case CONNMAN_DEVICE_TYPE_ETHERNET:
150                 return "ethernet";
151         case CONNMAN_DEVICE_TYPE_WIFI:
152                 return "wifi";
153         case CONNMAN_DEVICE_TYPE_WIMAX:
154                 return "wimax";
155         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
156                 return "bluetooth";
157         case CONNMAN_DEVICE_TYPE_GPS:
158                 return "gps";
159         case CONNMAN_DEVICE_TYPE_CELLULAR:
160                 return "cellular";
161         }
162
163         return NULL;
164 }
165
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
167 {
168         enum connman_device_type type = connman_device_get_type(device);
169
170         switch (type) {
171         case CONNMAN_DEVICE_TYPE_UNKNOWN:
172         case CONNMAN_DEVICE_TYPE_VENDOR:
173         case CONNMAN_DEVICE_TYPE_GPS:
174                 break;
175         case CONNMAN_DEVICE_TYPE_ETHERNET:
176                 return CONNMAN_SERVICE_TYPE_ETHERNET;
177         case CONNMAN_DEVICE_TYPE_WIFI:
178                 return CONNMAN_SERVICE_TYPE_WIFI;
179         case CONNMAN_DEVICE_TYPE_WIMAX:
180                 return CONNMAN_SERVICE_TYPE_WIMAX;
181         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183         case CONNMAN_DEVICE_TYPE_CELLULAR:
184                 return CONNMAN_SERVICE_TYPE_CELLULAR;
185         }
186
187         return CONNMAN_SERVICE_TYPE_UNKNOWN;
188 }
189
190 static void powered_changed(struct connman_device *device)
191 {
192         connman_dbus_property_changed_basic(device->element.path,
193                                 CONNMAN_DEVICE_INTERFACE, "Powered",
194                                         DBUS_TYPE_BOOLEAN, &device->powered);
195 }
196
197 static void blocked_changed(struct connman_device *device)
198 {
199         connman_dbus_property_changed_basic(device->element.path,
200                                 CONNMAN_DEVICE_INTERFACE, "Blocked",
201                                         DBUS_TYPE_BOOLEAN, &device->blocked);
202 }
203
204 int __connman_device_enable(struct connman_device *device)
205 {
206         int err;
207
208         DBG("device %p", device);
209
210         if (!device->driver || !device->driver->enable)
211                 return -EOPNOTSUPP;
212
213         if (device->powered_pending == TRUE)
214                 return -EALREADY;
215
216         if (device->blocked == TRUE)
217                 return -ENOLINK;
218
219         err = device->driver->enable(device);
220         if (err < 0) {
221                 if (err == -EINPROGRESS)
222                         device->powered_pending = TRUE;
223                 return err;
224         }
225
226         device->powered_pending = TRUE;
227         device->powered = TRUE;
228
229         __connman_technology_enable_device(device);
230
231         return 0;
232 }
233
234 int __connman_device_disable(struct connman_device *device)
235 {
236         int err;
237
238         DBG("device %p", device);
239
240         if (!device->driver || !device->driver->disable)
241                 return -EOPNOTSUPP;
242
243         if (device->powered == FALSE)
244                 return -ENOLINK;
245
246         if (device->powered_pending == FALSE)
247                 return -EALREADY;
248
249         device->reconnect = FALSE;
250
251         clear_scan_trigger(device);
252
253         g_hash_table_remove_all(device->networks);
254
255         err = device->driver->disable(device);
256         if (err < 0) {
257                 if (err == -EINPROGRESS)
258                         device->powered_pending = FALSE;
259                 return err;
260         }
261
262         device->powered_pending = FALSE;
263         device->powered = FALSE;
264
265         __connman_technology_disable_device(device);
266
267         return 0;
268 }
269
270 static int set_powered(struct connman_device *device, connman_bool_t powered)
271 {
272         DBG("device %p powered %d", device, powered);
273
274         if (powered == TRUE)
275                 return __connman_device_enable(device);
276         else
277                 return __connman_device_disable(device);
278 }
279
280 void __connman_device_list(DBusMessageIter *iter, void *user_data)
281 {
282         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
283 }
284
285 static void append_path(gpointer key, gpointer value, gpointer user_data)
286 {
287         struct connman_element *element = value;
288         DBusMessageIter *iter = user_data;
289
290         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
291                                                         &element->path);
292 }
293
294 static void append_networks(DBusMessageIter *iter, void *user_data)
295 {
296         struct connman_device *device = user_data;
297
298         g_hash_table_foreach(device->networks, append_path, iter);
299 }
300
301 static DBusMessage *get_properties(DBusConnection *conn,
302                                         DBusMessage *msg, void *data)
303 {
304         struct connman_device *device = data;
305         DBusMessage *reply;
306         DBusMessageIter array, dict;
307         const char *str;
308
309         DBG("conn %p", conn);
310
311         if (__connman_security_check_privilege(msg,
312                                         CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
313                 return __connman_error_permission_denied(msg);
314
315         reply = dbus_message_new_method_return(msg);
316         if (reply == NULL)
317                 return NULL;
318
319         dbus_message_iter_init_append(reply, &array);
320
321         connman_dbus_dict_open(&array, &dict);
322
323         if (device->name != NULL)
324                 connman_dbus_dict_append_basic(&dict, "Name",
325                                         DBUS_TYPE_STRING, &device->name);
326
327         str = type2string(device->type);
328         if (str != NULL)
329                 connman_dbus_dict_append_basic(&dict, "Type",
330                                                 DBUS_TYPE_STRING, &str);
331
332         if (device->address != NULL)
333                 connman_dbus_dict_append_basic(&dict, "Address",
334                                         DBUS_TYPE_STRING, &device->address);
335
336         if (device->interface != NULL)
337                 connman_dbus_dict_append_basic(&dict, "Interface",
338                                         DBUS_TYPE_STRING, &device->interface);
339
340         connman_dbus_dict_append_basic(&dict, "Powered",
341                                         DBUS_TYPE_BOOLEAN, &device->powered);
342
343         connman_dbus_dict_append_basic(&dict, "Blocked",
344                                         DBUS_TYPE_BOOLEAN, &device->blocked);
345
346         if (device->driver && device->driver->scan)
347                 connman_dbus_dict_append_basic(&dict, "Scanning",
348                                         DBUS_TYPE_BOOLEAN, &device->scanning);
349
350         switch (device->mode) {
351         case CONNMAN_DEVICE_MODE_UNKNOWN:
352                 break;
353         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
354         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
355                 if (device->scan_interval > 0)
356                         connman_dbus_dict_append_basic(&dict, "ScanInterval",
357                                 DBUS_TYPE_UINT16, &device->scan_interval);
358
359                 connman_dbus_dict_append_array(&dict, "Networks",
360                                 DBUS_TYPE_OBJECT_PATH, append_networks, device);
361                 break;
362         }
363
364         connman_dbus_dict_close(&array, &dict);
365
366         return reply;
367 }
368
369 static gboolean powered_timeout(gpointer user_data)
370 {
371         struct connman_device *device = user_data;
372
373         DBG("device %p", device);
374
375         device->timeout = 0;
376
377         if (device->pending != NULL) {
378                 DBusMessage *reply;
379
380                 reply = __connman_error_operation_timeout(device->pending);
381                 if (reply != NULL)
382                         g_dbus_send_message(connection, reply);
383
384                 dbus_message_unref(device->pending);
385                 device->pending = NULL;
386         }
387
388         return FALSE;
389 }
390
391 static DBusMessage *set_property(DBusConnection *conn,
392                                         DBusMessage *msg, void *data)
393 {
394         struct connman_device *device = data;
395         DBusMessageIter iter, value;
396         const char *name;
397         int type;
398
399         DBG("conn %p", conn);
400
401         if (dbus_message_iter_init(msg, &iter) == FALSE)
402                 return __connman_error_invalid_arguments(msg);
403
404         dbus_message_iter_get_basic(&iter, &name);
405         dbus_message_iter_next(&iter);
406         dbus_message_iter_recurse(&iter, &value);
407
408         if (__connman_security_check_privilege(msg,
409                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
410                 return __connman_error_permission_denied(msg);
411
412         type = dbus_message_iter_get_arg_type(&value);
413
414         if (g_str_equal(name, "Powered") == TRUE) {
415                 connman_bool_t powered;
416                 int err;
417
418                 if (type != DBUS_TYPE_BOOLEAN)
419                         return __connman_error_invalid_arguments(msg);
420
421                 dbus_message_iter_get_basic(&value, &powered);
422
423                 device->powered_persistent = powered;
424
425                 __connman_storage_save_device(device);
426
427                 if (device->powered == powered)
428                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
429
430                 if (device->pending != NULL)
431                         return __connman_error_in_progress(msg);
432
433                 err = set_powered(device, powered);
434                 if (err < 0) {
435                         if (err != -EINPROGRESS)
436                                 return __connman_error_failed(msg, -err);
437
438                         device->pending = dbus_message_ref(msg);
439
440                         device->timeout = g_timeout_add_seconds(15,
441                                                 powered_timeout, device);
442
443                         return NULL;
444                 }
445         } else if (g_str_equal(name, "ScanInterval") == TRUE) {
446                 connman_uint16_t interval;
447
448                 switch (device->mode) {
449                 case CONNMAN_DEVICE_MODE_UNKNOWN:
450                 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
451                 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
452                         break;
453                 }
454
455                 if (type != DBUS_TYPE_UINT16)
456                         return __connman_error_invalid_arguments(msg);
457
458                 dbus_message_iter_get_basic(&value, &interval);
459
460                 if (device->scan_interval != interval) {
461                         device->scan_interval = interval;
462
463                         __connman_storage_save_device(device);
464
465                         reset_scan_trigger(device);
466                 }
467         } else
468                 return __connman_error_invalid_property(msg);
469
470         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
471 }
472
473 static DBusMessage *propose_scan(DBusConnection *conn,
474                                         DBusMessage *msg, void *data)
475 {
476         struct connman_device *device = data;
477         int err;
478
479         DBG("conn %p", conn);
480
481         switch (device->mode) {
482         case CONNMAN_DEVICE_MODE_UNKNOWN:
483                 return __connman_error_not_supported(msg);
484         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
485         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
486                 break;
487         }
488
489         err = __connman_device_scan(device);
490         if (err < 0)
491                 return __connman_error_failed(msg, -err);
492
493         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
494 }
495
496 static GDBusMethodTable device_methods[] = {
497         { "GetProperties", "",      "a{sv}", get_properties },
498         { "SetProperty",   "sv",    "",      set_property,
499                                                 G_DBUS_METHOD_FLAG_ASYNC },
500         { "ProposeScan",   "",      "",      propose_scan   },
501         { },
502 };
503
504 static GDBusSignalTable device_signals[] = {
505         { "PropertyChanged", "sv" },
506         { },
507 };
508
509 static int register_interface(struct connman_element *element)
510 {
511         struct connman_device *device = element->device;
512
513         DBG("element %p name %s", element, element->name);
514
515         if (g_dbus_register_interface(connection, element->path,
516                                         CONNMAN_DEVICE_INTERFACE,
517                                         device_methods, device_signals,
518                                         NULL, device, NULL) == FALSE) {
519                 connman_error("Failed to register %s device", element->path);
520                 return -EIO;
521         }
522
523         device->registered = TRUE;
524
525         return 0;
526 }
527
528 static void unregister_interface(struct connman_element *element)
529 {
530         struct connman_device *device = element->device;
531
532         DBG("element %p name %s", element, element->name);
533
534         device->registered = FALSE;
535
536         g_dbus_unregister_interface(connection, element->path,
537                                                 CONNMAN_DEVICE_INTERFACE);
538 }
539
540 static int setup_device(struct connman_device *device)
541 {
542         int err;
543
544         DBG("device %p", device);
545
546         err = register_interface(&device->element);
547         if (err < 0) {
548                 if (device->driver->remove)
549                         device->driver->remove(device);
550                 device->driver = NULL;
551                 return err;
552         }
553
554         __connman_technology_add_device(device);
555
556         switch (device->mode) {
557         case CONNMAN_DEVICE_MODE_UNKNOWN:
558         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
559         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
560                 break;
561         }
562
563         if (__connman_udev_get_blocked(device->phyindex) == TRUE)
564                 return 0;
565
566         if (device->offlinemode == FALSE &&
567                                 device->powered_persistent == TRUE)
568                 __connman_device_enable(device);
569
570         return 0;
571 }
572
573 static void probe_driver(struct connman_element *element, gpointer user_data)
574 {
575         struct connman_device_driver *driver = user_data;
576
577         DBG("element %p name %s", element, element->name);
578
579         if (element->device == NULL)
580                 return;
581
582         if (element->device->driver != NULL)
583                 return;
584
585         if (driver->type != element->device->type)
586                 return;
587
588         if (driver->probe(element->device) < 0)
589                 return;
590
591         element->device->driver = driver;
592
593         setup_device(element->device);
594 }
595
596 static void remove_device(struct connman_device *device)
597 {
598         int err;
599
600         DBG("device %p", device);
601
602         err = __connman_device_disable(device);
603         if (err < 0 && err == -EINPROGRESS)
604                 __connman_technology_disable_device(device);
605
606         switch (device->mode) {
607         case CONNMAN_DEVICE_MODE_UNKNOWN:
608         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
609         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
610                 break;
611         }
612
613         __connman_technology_remove_device(device);
614
615         unregister_interface(&device->element);
616
617         if (device->driver->remove)
618                 device->driver->remove(device);
619
620         device->driver = NULL;
621 }
622
623 static void remove_driver(struct connman_element *element, gpointer user_data)
624 {
625         struct connman_device_driver *driver = user_data;
626
627         DBG("element %p name %s", element, element->name);
628
629         if (element->device == NULL)
630                 return;
631
632         if (element->device->driver == driver)
633                 remove_device(element->device);
634 }
635
636 connman_bool_t __connman_device_has_driver(struct connman_device *device)
637 {
638         if (device == NULL || device->driver == NULL)
639                 return FALSE;
640
641         return device->registered;
642 }
643
644 static GSList *driver_list = NULL;
645
646 static gint compare_priority(gconstpointer a, gconstpointer b)
647 {
648         const struct connman_device_driver *driver1 = a;
649         const struct connman_device_driver *driver2 = b;
650
651         return driver2->priority - driver1->priority;
652 }
653
654 /**
655  * connman_device_driver_register:
656  * @driver: device driver definition
657  *
658  * Register a new device driver
659  *
660  * Returns: %0 on success
661  */
662 int connman_device_driver_register(struct connman_device_driver *driver)
663 {
664         DBG("driver %p name %s", driver, driver->name);
665
666         driver_list = g_slist_insert_sorted(driver_list, driver,
667                                                         compare_priority);
668
669         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
670                                                 probe_driver, driver);
671
672         return 0;
673 }
674
675 /**
676  * connman_device_driver_unregister:
677  * @driver: device driver definition
678  *
679  * Remove a previously registered device driver
680  */
681 void connman_device_driver_unregister(struct connman_device_driver *driver)
682 {
683         DBG("driver %p name %s", driver, driver->name);
684
685         driver_list = g_slist_remove(driver_list, driver);
686
687         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
688                                                 remove_driver, driver);
689 }
690
691 static void unregister_network(gpointer data)
692 {
693         struct connman_network *network = data;
694
695         DBG("network %p", network);
696
697         connman_element_unregister((struct connman_element *) network);
698
699         connman_network_unref(network);
700 }
701
702 static void device_destruct(struct connman_element *element)
703 {
704         struct connman_device *device = element->device;
705
706         DBG("element %p name %s", element, element->name);
707
708         if (device->timeout > 0) {
709                 g_source_remove(device->timeout);
710                 device->timeout = 0;
711         }
712
713         clear_scan_trigger(device);
714
715         if (device->pending != NULL) {
716                 dbus_message_unref(device->pending);
717                 device->pending = NULL;
718         }
719
720         g_free(device->ident);
721         g_free(device->node);
722         g_free(device->name);
723         g_free(device->address);
724         g_free(device->control);
725         g_free(device->interface);
726
727         g_free(device->last_network);
728
729         g_hash_table_destroy(device->networks);
730         device->networks = NULL;
731 }
732
733 /**
734  * connman_device_create:
735  * @node: device node name (for example an address)
736  * @type: device type
737  *
738  * Allocate a new device of given #type and assign the #node name to it.
739  *
740  * Returns: a newly-allocated #connman_device structure
741  */
742 struct connman_device *connman_device_create(const char *node,
743                                                 enum connman_device_type type)
744 {
745         struct connman_device *device;
746         const char *str;
747
748         DBG("node %s type %d", node, type);
749
750         device = g_try_new0(struct connman_device, 1);
751         if (device == NULL)
752                 return NULL;
753
754         DBG("device %p", device);
755
756         __connman_element_initialize(&device->element);
757
758         device->element.name = g_strdup(node);
759         device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
760
761         device->element.device = device;
762         device->element.destruct = device_destruct;
763
764         str = type2string(type);
765         if (str != NULL)
766                 connman_element_set_string(&device->element,
767                                         CONNMAN_PROPERTY_ID_TYPE, str);
768
769         device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
770
771         device->type = type;
772         device->name = g_strdup(type2description(device->type));
773         device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
774
775         device->powered_persistent = TRUE;
776
777         device->phyindex = -1;
778
779         switch (type) {
780         case CONNMAN_DEVICE_TYPE_UNKNOWN:
781         case CONNMAN_DEVICE_TYPE_VENDOR:
782                 device->scan_interval = 0;
783                 break;
784         case CONNMAN_DEVICE_TYPE_ETHERNET:
785         case CONNMAN_DEVICE_TYPE_WIFI:
786                 device->scan_interval = 300;
787                 break;
788         case CONNMAN_DEVICE_TYPE_WIMAX:
789                 device->scan_interval = 0;
790                 break;
791         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
792                 device->scan_interval = 0;
793                 break;
794         case CONNMAN_DEVICE_TYPE_GPS:
795                 device->scan_interval = 0;
796                 break;
797         case CONNMAN_DEVICE_TYPE_CELLULAR:
798                 device->scan_interval = 0;
799                 break;
800         }
801
802         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
803                                                 g_free, unregister_network);
804
805         return device;
806 }
807
808 /**
809  * connman_device_ref:
810  * @device: device structure
811  *
812  * Increase reference counter of device
813  */
814 struct connman_device *connman_device_ref(struct connman_device *device)
815 {
816         if (connman_element_ref(&device->element) == NULL)
817                 return NULL;
818
819         return device;
820 }
821
822 /**
823  * connman_device_unref:
824  * @device: device structure
825  *
826  * Decrease reference counter of device
827  */
828 void connman_device_unref(struct connman_device *device)
829 {
830         connman_element_unref(&device->element);
831 }
832
833 const char *__connman_device_get_type(struct connman_device *device)
834 {
835         return type2string(device->type);
836 }
837
838 /**
839  * connman_device_get_type:
840  * @device: device structure
841  *
842  * Get type of device
843  */
844 enum connman_device_type connman_device_get_type(struct connman_device *device)
845 {
846         return device->type;
847 }
848
849 /**
850  * connman_device_get_name:
851  * @device: device structure
852  *
853  * Get unique name of device
854  */
855 const char *connman_device_get_name(struct connman_device *device)
856 {
857         return device->element.name;
858 }
859
860 /**
861  * connman_device_get_path:
862  * @device: device structure
863  *
864  * Get path name of device
865  */
866 const char *connman_device_get_path(struct connman_device *device)
867 {
868         return device->element.path;
869 }
870
871 /**
872  * connman_device_set_index:
873  * @device: device structure
874  * @index: index number
875  *
876  * Set index number of device
877  */
878 void connman_device_set_index(struct connman_device *device, int index)
879 {
880         device->element.index = index;
881 }
882
883 /**
884  * connman_device_get_index:
885  * @device: device structure
886  *
887  * Get index number of device
888  */
889 int connman_device_get_index(struct connman_device *device)
890 {
891         return device->element.index;
892 }
893
894 int __connman_device_get_phyindex(struct connman_device *device)
895 {
896         return device->phyindex;
897 }
898
899 void __connman_device_set_phyindex(struct connman_device *device,
900                                                         int phyindex)
901 {
902         device->phyindex = phyindex;
903 }
904
905 /**
906  * connman_device_set_interface:
907  * @device: device structure
908  * @interface: interface name
909  * @control: control interface
910  *
911  * Set interface name of device
912  */
913 void connman_device_set_interface(struct connman_device *device,
914                                 const char *interface, const char *control)
915 {
916         g_free(device->element.devname);
917         device->element.devname = g_strdup(interface);
918
919         g_free(device->interface);
920         device->interface = g_strdup(interface);
921
922         g_free(device->control);
923         device->control = g_strdup(control);
924
925         if (device->name == NULL) {
926                 const char *str = type2description(device->type);
927                 if (str != NULL && device->interface != NULL)
928                         device->name = g_strdup_printf("%s (%s)", str,
929                                                         device->interface);
930         }
931 }
932
933 const char *connman_device_get_control(struct connman_device *device)
934 {
935         return device->control;
936 }
937
938 /**
939  * connman_device_set_ident:
940  * @device: device structure
941  * @ident: unique identifier
942  *
943  * Set unique identifier of device
944  */
945 void connman_device_set_ident(struct connman_device *device,
946                                                         const char *ident)
947 {
948         g_free(device->ident);
949         device->ident = g_strdup(ident);
950 }
951
952 const char *__connman_device_get_ident(struct connman_device *device)
953 {
954         return device->ident;
955 }
956
957 /**
958  * connman_device_set_mode:
959  * @device: device structure
960  * @mode: network mode
961  *
962  * Change network mode of device
963  */
964 void connman_device_set_mode(struct connman_device *device,
965                                                 enum connman_device_mode mode)
966 {
967         device->mode = mode;
968 }
969
970 /**
971  * connman_device_get_mode:
972  * @device: device structure
973  *
974  * Get network mode of device
975  */
976 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
977 {
978         return device->mode;
979 }
980
981 /**
982  * connman_device_set_powered:
983  * @device: device structure
984  * @powered: powered state
985  *
986  * Change power state of device
987  */
988 int connman_device_set_powered(struct connman_device *device,
989                                                 connman_bool_t powered)
990 {
991         DBG("driver %p powered %d", device, powered);
992
993         if (device->timeout > 0) {
994                 g_source_remove(device->timeout);
995                 device->timeout = 0;
996         }
997
998         if (device->pending != NULL) {
999                 g_dbus_send_reply(connection, device->pending,
1000                                                         DBUS_TYPE_INVALID);
1001
1002                 dbus_message_unref(device->pending);
1003                 device->pending = NULL;
1004         }
1005
1006         if (device->powered == powered) {
1007                 device->powered_pending = powered;
1008                 return -EALREADY;
1009         }
1010
1011         if (powered == TRUE)
1012                 __connman_device_enable(device);
1013         else
1014                 __connman_device_disable(device);
1015
1016         device->powered = powered;
1017         device->powered_pending = powered;
1018
1019         if (device->powered == TRUE)
1020                 __connman_technology_enable_device(device);
1021         else
1022                 __connman_technology_disable_device(device);
1023
1024         if (device->offlinemode == TRUE && powered == TRUE) {
1025                 powered_changed(device);
1026                 return connman_device_set_powered(device, FALSE);
1027         }
1028
1029         if (device->registered == FALSE)
1030                 return 0;
1031
1032         powered_changed(device);
1033
1034         if (powered == FALSE)
1035                 return 0;
1036
1037         reset_scan_trigger(device);
1038
1039         if (device->driver->scan)
1040                 device->driver->scan(device);
1041
1042         return 0;
1043 }
1044
1045 int __connman_device_set_blocked(struct connman_device *device,
1046                                                 connman_bool_t blocked)
1047 {
1048         connman_bool_t powered;
1049
1050         DBG("device %p blocked %d", device, blocked);
1051
1052         device->blocked = blocked;
1053
1054         blocked_changed(device);
1055
1056         if (device->offlinemode == TRUE)
1057                 return 0;
1058
1059         connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1060
1061         if (blocked == FALSE)
1062                 powered = device->powered_persistent;
1063         else
1064                 powered = FALSE;
1065
1066         return set_powered(device, powered);
1067 }
1068
1069 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1070 {
1071         return device->blocked;
1072 }
1073
1074 int __connman_device_scan(struct connman_device *device)
1075 {
1076         if (!device->driver || !device->driver->scan)
1077                 return -EOPNOTSUPP;
1078
1079         if (device->powered == FALSE)
1080                 return -ENOLINK;
1081
1082         reset_scan_trigger(device);
1083
1084         return device->driver->scan(device);
1085 }
1086
1087 int __connman_device_enable_persistent(struct connman_device *device)
1088 {
1089         int err;
1090
1091         DBG("device %p", device);
1092
1093         device->powered_persistent = TRUE;
1094
1095         __connman_storage_save_device(device);
1096
1097         err = __connman_device_enable(device);
1098         if (err == 0 || err == -EINPROGRESS) {
1099                 device->offlinemode = FALSE;
1100                 if (__connman_profile_get_offlinemode() == TRUE) {
1101                         __connman_profile_set_offlinemode(FALSE, FALSE);
1102
1103                         __connman_profile_save_default();
1104                 }
1105         }
1106
1107         return err;
1108 }
1109
1110 int __connman_device_disable_persistent(struct connman_device *device)
1111 {
1112         DBG("device %p", device);
1113
1114         device->powered_persistent = FALSE;
1115
1116         __connman_storage_save_device(device);
1117
1118         return __connman_device_disable(device);
1119 }
1120
1121 int __connman_device_disconnect(struct connman_device *device)
1122 {
1123         GHashTableIter iter;
1124         gpointer key, value;
1125
1126         DBG("device %p", device);
1127
1128         connman_device_set_disconnected(device, TRUE);
1129
1130         g_hash_table_iter_init(&iter, device->networks);
1131
1132         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1133                 struct connman_network *network = value;
1134
1135                 if (__connman_network_get_connecting(network) == TRUE) {
1136                         /*
1137                          * Skip network in the process of connecting.
1138                          * This is a workaround for WiFi networks serviced
1139                          * by the supplicant plugin that hold a reference
1140                          * to the network.  If we disconnect the network
1141                          * here then the referenced object will not be
1142                          * registered and usage (like launching DHCP client)
1143                          * will fail.  There is nothing to be gained by
1144                          * removing the network here anyway.
1145                          */
1146                         connman_warn("Skipping disconnect of %s",
1147                                 connman_network_get_identifier(network));
1148                         continue;
1149                 }
1150
1151                 __connman_network_disconnect(network);
1152         }
1153
1154         return 0;
1155 }
1156
1157 static void mark_network_unavailable(gpointer key, gpointer value,
1158                                                         gpointer user_data)
1159 {
1160         struct connman_network *network = value;
1161
1162         if (connman_network_get_connected(network) == TRUE)
1163                 return;
1164
1165         connman_network_set_available(network, FALSE);
1166 }
1167
1168 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1169                                                         gpointer user_data)
1170 {
1171         struct connman_network *network = value;
1172
1173         if (connman_network_get_connected(network) == TRUE)
1174                 return FALSE;
1175
1176         if (connman_network_get_available(network) == TRUE)
1177                 return FALSE;
1178
1179         return TRUE;
1180 }
1181
1182 void __connman_device_cleanup_networks(struct connman_device *device)
1183 {
1184         g_hash_table_foreach_remove(device->networks,
1185                                         remove_unavailable_network, NULL);
1186 }
1187
1188 static void scanning_changed(struct connman_device *device)
1189 {
1190         connman_dbus_property_changed_basic(device->element.path,
1191                                 CONNMAN_DEVICE_INTERFACE, "Scanning",
1192                                         DBUS_TYPE_BOOLEAN, &device->scanning);
1193 }
1194
1195 /**
1196  * connman_device_set_scanning:
1197  * @device: device structure
1198  * @scanning: scanning state
1199  *
1200  * Change scanning state of device
1201  */
1202 int connman_device_set_scanning(struct connman_device *device,
1203                                                 connman_bool_t scanning)
1204 {
1205         DBG("device %p scanning %d", device, scanning);
1206
1207         if (!device->driver || !device->driver->scan)
1208                 return -EINVAL;
1209
1210         if (device->scanning == scanning)
1211                 return -EALREADY;
1212
1213         device->scanning = scanning;
1214
1215         scanning_changed(device);
1216
1217         if (scanning == TRUE) {
1218                 reset_scan_trigger(device);
1219
1220                 g_hash_table_foreach(device->networks,
1221                                         mark_network_unavailable, NULL);
1222
1223                 return 0;
1224         }
1225
1226         __connman_device_cleanup_networks(device);
1227
1228         if (device->connections > 0)
1229                 return 0;
1230
1231         if (device->disconnected == TRUE)
1232                 return 0;
1233
1234         __connman_service_auto_connect();
1235
1236         return 0;
1237 }
1238
1239 /**
1240  * connman_device_set_disconnected:
1241  * @device: device structure
1242  * @disconnected: disconnected state
1243  *
1244  * Change disconnected state of device (only for device with networks)
1245  */
1246 int connman_device_set_disconnected(struct connman_device *device,
1247                                                 connman_bool_t disconnected)
1248 {
1249         DBG("device %p disconnected %d", device, disconnected);
1250
1251         switch (device->mode) {
1252         case CONNMAN_DEVICE_MODE_UNKNOWN:
1253                 return -EINVAL;
1254         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1255         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1256                 break;
1257         }
1258
1259         if (device->disconnected == disconnected)
1260                 return -EALREADY;
1261
1262         device->disconnected = disconnected;
1263
1264         if (disconnected == TRUE)
1265                 force_scan_trigger(device);
1266
1267         return 0;
1268 }
1269
1270 /**
1271  * connman_device_get_disconnected:
1272  * @device: device structure
1273  *
1274  * Get device disconnected state
1275  */
1276 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1277 {
1278         return device->disconnected;
1279 }
1280
1281 /**
1282  * connman_device_set_string:
1283  * @device: device structure
1284  * @key: unique identifier
1285  * @value: string value
1286  *
1287  * Set string value for specific key
1288  */
1289 int connman_device_set_string(struct connman_device *device,
1290                                         const char *key, const char *value)
1291 {
1292         DBG("device %p key %s value %s", device, key, value);
1293
1294         if (g_str_equal(key, "Address") == TRUE) {
1295                 g_free(device->address);
1296                 device->address = g_strdup(value);
1297         } else if (g_str_equal(key, "Name") == TRUE) {
1298                 g_free(device->name);
1299                 device->name = g_strdup(value);
1300         } else if (g_str_equal(key, "Node") == TRUE) {
1301                 g_free(device->node);
1302                 device->node = g_strdup(value);
1303         }
1304
1305         return connman_element_set_string(&device->element, key, value);
1306 }
1307
1308 /**
1309  * connman_device_get_string:
1310  * @device: device structure
1311  * @key: unique identifier
1312  *
1313  * Get string value for specific key
1314  */
1315 const char *connman_device_get_string(struct connman_device *device,
1316                                                         const char *key)
1317 {
1318         DBG("device %p key %s", device, key);
1319
1320         if (g_str_equal(key, "Address") == TRUE)
1321                 return device->address;
1322         else if (g_str_equal(key, "Name") == TRUE)
1323                 return device->name;
1324         else if (g_str_equal(key, "Node") == TRUE)
1325                 return device->node;
1326
1327         return connman_element_get_string(&device->element, key);
1328 }
1329
1330 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1331 {
1332         struct connman_device *device = element->device;
1333         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1334         connman_bool_t powered;
1335
1336         DBG("element %p name %s", element, element->name);
1337
1338         if (device == NULL)
1339                 return;
1340
1341         device->offlinemode = offlinemode;
1342
1343         powered = (offlinemode == TRUE) ? FALSE : TRUE;
1344
1345         if (device->powered == powered)
1346                 return;
1347
1348         if (device->powered_persistent == FALSE)
1349                 powered = FALSE;
1350
1351         set_powered(device, powered);
1352 }
1353
1354 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1355 {
1356         DBG("offlinmode %d", offlinemode);
1357
1358         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1359                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
1360
1361         __connman_notifier_offlinemode(offlinemode);
1362
1363         return 0;
1364 }
1365
1366 void __connman_device_increase_connections(struct connman_device *device)
1367 {
1368         device->connections++;
1369 }
1370
1371 void __connman_device_decrease_connections(struct connman_device *device)
1372 {
1373         device->connections--;
1374 }
1375
1376 /**
1377  * connman_device_add_network:
1378  * @device: device structure
1379  * @network: network structure
1380  *
1381  * Add new network to the device
1382  */
1383 int connman_device_add_network(struct connman_device *device,
1384                                         struct connman_network *network)
1385 {
1386         const char *identifier = connman_network_get_identifier(network);
1387         int err;
1388
1389         DBG("device %p network %p", device, network);
1390
1391         if (identifier == NULL)
1392                 return -EINVAL;
1393
1394         switch (device->mode) {
1395         case CONNMAN_DEVICE_MODE_UNKNOWN:
1396                 return -EINVAL;
1397         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1398         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1399                 break;
1400         }
1401
1402         __connman_network_set_device(network, device);
1403
1404         err = connman_element_register((struct connman_element *) network,
1405                                                         &device->element);
1406         if (err < 0) {
1407                 __connman_network_set_device(network, NULL);
1408                 return err;
1409         }
1410
1411         g_hash_table_insert(device->networks, g_strdup(identifier),
1412                                                                 network);
1413
1414         return 0;
1415 }
1416
1417 /**
1418  * connman_device_get_network:
1419  * @device: device structure
1420  * @identifier: network identifier
1421  *
1422  * Get network for given identifier
1423  */
1424 struct connman_network *connman_device_get_network(struct connman_device *device,
1425                                                         const char *identifier)
1426 {
1427         DBG("device %p identifier %s", device, identifier);
1428
1429         return g_hash_table_lookup(device->networks, identifier);
1430 }
1431
1432 /**
1433  * connman_device_remove_network:
1434  * @device: device structure
1435  * @identifier: network identifier
1436  *
1437  * Remove network for given identifier
1438  */
1439 int connman_device_remove_network(struct connman_device *device,
1440                                                         const char *identifier)
1441 {
1442         DBG("device %p identifier %s", device, identifier);
1443
1444         g_hash_table_remove(device->networks, identifier);
1445
1446         return 0;
1447 }
1448
1449 void connman_device_remove_all_networks(struct connman_device *device)
1450 {
1451         g_hash_table_remove_all(device->networks);
1452 }
1453
1454 void __connman_device_set_network(struct connman_device *device,
1455                                         struct connman_network *network)
1456 {
1457         const char *name;
1458
1459         if (device == NULL)
1460                 return;
1461
1462         if (device->network == network)
1463                 return;
1464
1465         if (device->network != NULL)
1466                 connman_network_unref(device->network);
1467
1468         if (network != NULL) {
1469                 name = connman_network_get_string(network,
1470                                                 CONNMAN_PROPERTY_ID_NAME);
1471                 g_free(device->last_network);
1472                 device->last_network = g_strdup(name);
1473
1474                 device->network = connman_network_ref(network);
1475         } else {
1476                 g_free(device->last_network);
1477                 device->last_network = NULL;
1478
1479                 device->network = NULL;
1480         }
1481 }
1482
1483 void __connman_device_set_reconnect(struct connman_device *device,
1484                                                 connman_bool_t reconnect)
1485 {
1486         device->reconnect = reconnect;
1487 }
1488
1489 connman_bool_t  __connman_device_get_reconnect(
1490                                 struct connman_device *device)
1491 {
1492         return device->reconnect;
1493 }
1494
1495 /**
1496  * connman_device_register:
1497  * @device: device structure
1498  *
1499  * Register device with the system
1500  */
1501 int connman_device_register(struct connman_device *device)
1502 {
1503         __connman_storage_load_device(device);
1504
1505         device->offlinemode = __connman_profile_get_offlinemode();
1506
1507         return connman_element_register(&device->element, NULL);
1508 }
1509
1510 /**
1511  * connman_device_unregister:
1512  * @device: device structure
1513  *
1514  * Unregister device with the system
1515  */
1516 void connman_device_unregister(struct connman_device *device)
1517 {
1518         __connman_storage_save_device(device);
1519
1520         connman_element_unregister(&device->element);
1521 }
1522
1523 /**
1524  * connman_device_get_data:
1525  * @device: device structure
1526  *
1527  * Get private device data pointer
1528  */
1529 void *connman_device_get_data(struct connman_device *device)
1530 {
1531         return device->driver_data;
1532 }
1533
1534 /**
1535  * connman_device_set_data:
1536  * @device: device structure
1537  * @data: data pointer
1538  *
1539  * Set private device data pointer
1540  */
1541 void connman_device_set_data(struct connman_device *device, void *data)
1542 {
1543         device->driver_data = data;
1544 }
1545
1546 static gboolean match_driver(struct connman_device *device,
1547                                         struct connman_device_driver *driver)
1548 {
1549         if (device->type == driver->type ||
1550                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1551                 return TRUE;
1552
1553         return FALSE;
1554 }
1555
1556 static int device_probe(struct connman_element *element)
1557 {
1558         struct connman_device *device = element->device;
1559         GSList *list;
1560
1561         DBG("element %p name %s", element, element->name);
1562
1563         if (device == NULL)
1564                 return -ENODEV;
1565
1566         if (device->driver != NULL)
1567                 return -EALREADY;
1568
1569         for (list = driver_list; list; list = list->next) {
1570                 struct connman_device_driver *driver = list->data;
1571
1572                 if (match_driver(device, driver) == FALSE)
1573                         continue;
1574
1575                 DBG("driver %p name %s", driver, driver->name);
1576
1577                 if (driver->probe(device) == 0) {
1578                         device->driver = driver;
1579                         break;
1580                 }
1581         }
1582
1583         if (device->driver == NULL)
1584                 return -ENODEV;
1585
1586         return setup_device(device);
1587 }
1588
1589 static void device_remove(struct connman_element *element)
1590 {
1591         struct connman_device *device = element->device;
1592
1593         DBG("element %p name %s", element, element->name);
1594
1595         if (device == NULL)
1596                 return;
1597
1598         if (device->driver == NULL)
1599                 return;
1600
1601         remove_device(device);
1602 }
1603
1604 static struct connman_driver device_driver = {
1605         .name           = "device",
1606         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1607         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1608         .probe          = device_probe,
1609         .remove         = device_remove,
1610 };
1611
1612 static int device_load(struct connman_device *device)
1613 {
1614         const char *ident = __connman_profile_active_ident();
1615         GKeyFile *keyfile;
1616         GError *error = NULL;
1617         gchar *identifier;
1618         connman_bool_t powered;
1619         int val;
1620
1621         DBG("device %p", device);
1622
1623         keyfile = __connman_storage_open_profile(ident);
1624         if (keyfile == NULL)
1625                 return 0;
1626
1627         identifier = g_strdup_printf("device_%s", device->element.name);
1628         if (identifier == NULL)
1629                 goto done;
1630
1631         powered = g_key_file_get_boolean(keyfile, identifier,
1632                                                 "Powered", &error);
1633         if (error == NULL)
1634                 device->powered_persistent = powered;
1635         g_clear_error(&error);
1636
1637         switch (device->mode) {
1638         case CONNMAN_DEVICE_MODE_UNKNOWN:
1639                 break;
1640         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1641         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1642                 val = g_key_file_get_integer(keyfile, identifier,
1643                                                 "ScanInterval", &error);
1644                 if (error == NULL && val > 0)
1645                         device->scan_interval = val;
1646                 g_clear_error(&error);
1647                 break;
1648         }
1649
1650 done:
1651         g_free(identifier);
1652
1653         __connman_storage_close_profile(ident, keyfile, FALSE);
1654
1655         return 0;
1656 }
1657
1658 static int device_save(struct connman_device *device)
1659 {
1660         const char *ident = __connman_profile_active_ident();
1661         GKeyFile *keyfile;
1662         gchar *identifier;
1663
1664         DBG("device %p", device);
1665
1666         keyfile = __connman_storage_open_profile(ident);
1667         if (keyfile == NULL)
1668                 return 0;
1669
1670         identifier = g_strdup_printf("device_%s", device->element.name);
1671         if (identifier == NULL)
1672                 goto done;
1673
1674         g_key_file_set_boolean(keyfile, identifier,
1675                                         "Powered", device->powered_persistent);
1676
1677         switch (device->mode) {
1678         case CONNMAN_DEVICE_MODE_UNKNOWN:
1679                 break;
1680         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1681         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1682                 if (device->scan_interval > 0)
1683                         g_key_file_set_integer(keyfile, identifier,
1684                                         "ScanInterval", device->scan_interval);
1685                 break;
1686         }
1687
1688 done:
1689         g_free(identifier);
1690
1691         __connman_storage_close_profile(ident, keyfile, TRUE);
1692
1693         return 0;
1694 }
1695
1696 static struct connman_storage device_storage = {
1697         .name           = "device",
1698         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1699         .device_load    = device_load,
1700         .device_save    = device_save,
1701 };
1702
1703 int __connman_device_init(void)
1704 {
1705         DBG("");
1706
1707         connection = connman_dbus_get_connection();
1708
1709         if (connman_storage_register(&device_storage) < 0)
1710                 connman_error("Failed to register device storage");
1711
1712         return connman_driver_register(&device_driver);
1713 }
1714
1715 void __connman_device_cleanup(void)
1716 {
1717         DBG("");
1718
1719         connman_driver_unregister(&device_driver);
1720
1721         connman_storage_unregister(&device_storage);
1722
1723         dbus_connection_unref(connection);
1724 }