device: remove stale pointer from network
[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_set_device(network, NULL);
700
701         connman_network_unref(network);
702 }
703
704 static void device_destruct(struct connman_element *element)
705 {
706         struct connman_device *device = element->device;
707
708         DBG("element %p name %s", element, element->name);
709
710         if (device->timeout > 0) {
711                 g_source_remove(device->timeout);
712                 device->timeout = 0;
713         }
714
715         clear_scan_trigger(device);
716
717         if (device->pending != NULL) {
718                 dbus_message_unref(device->pending);
719                 device->pending = NULL;
720         }
721
722         g_free(device->ident);
723         g_free(device->node);
724         g_free(device->name);
725         g_free(device->address);
726         g_free(device->control);
727         g_free(device->interface);
728
729         g_free(device->last_network);
730
731         g_hash_table_destroy(device->networks);
732         device->networks = NULL;
733 }
734
735 /**
736  * connman_device_create:
737  * @node: device node name (for example an address)
738  * @type: device type
739  *
740  * Allocate a new device of given #type and assign the #node name to it.
741  *
742  * Returns: a newly-allocated #connman_device structure
743  */
744 struct connman_device *connman_device_create(const char *node,
745                                                 enum connman_device_type type)
746 {
747         struct connman_device *device;
748         const char *str;
749
750         DBG("node %s type %d", node, type);
751
752         device = g_try_new0(struct connman_device, 1);
753         if (device == NULL)
754                 return NULL;
755
756         DBG("device %p", device);
757
758         __connman_element_initialize(&device->element);
759
760         device->element.name = g_strdup(node);
761         device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
762
763         device->element.device = device;
764         device->element.destruct = device_destruct;
765
766         str = type2string(type);
767         if (str != NULL)
768                 connman_element_set_string(&device->element,
769                                         CONNMAN_PROPERTY_ID_TYPE, str);
770
771         device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
772
773         device->type = type;
774         device->name = g_strdup(type2description(device->type));
775         device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
776
777         device->powered_persistent = TRUE;
778
779         device->phyindex = -1;
780
781         switch (type) {
782         case CONNMAN_DEVICE_TYPE_UNKNOWN:
783         case CONNMAN_DEVICE_TYPE_VENDOR:
784                 device->scan_interval = 0;
785                 break;
786         case CONNMAN_DEVICE_TYPE_ETHERNET:
787         case CONNMAN_DEVICE_TYPE_WIFI:
788                 device->scan_interval = 300;
789                 break;
790         case CONNMAN_DEVICE_TYPE_WIMAX:
791                 device->scan_interval = 0;
792                 break;
793         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
794                 device->scan_interval = 0;
795                 break;
796         case CONNMAN_DEVICE_TYPE_GPS:
797                 device->scan_interval = 0;
798                 break;
799         case CONNMAN_DEVICE_TYPE_CELLULAR:
800                 device->scan_interval = 0;
801                 break;
802         }
803
804         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
805                                                 g_free, unregister_network);
806
807         return device;
808 }
809
810 /**
811  * connman_device_ref:
812  * @device: device structure
813  *
814  * Increase reference counter of device
815  */
816 struct connman_device *connman_device_ref(struct connman_device *device)
817 {
818         if (connman_element_ref(&device->element) == NULL)
819                 return NULL;
820
821         return device;
822 }
823
824 /**
825  * connman_device_unref:
826  * @device: device structure
827  *
828  * Decrease reference counter of device
829  */
830 void connman_device_unref(struct connman_device *device)
831 {
832         connman_element_unref(&device->element);
833 }
834
835 const char *__connman_device_get_type(struct connman_device *device)
836 {
837         return type2string(device->type);
838 }
839
840 /**
841  * connman_device_get_type:
842  * @device: device structure
843  *
844  * Get type of device
845  */
846 enum connman_device_type connman_device_get_type(struct connman_device *device)
847 {
848         return device->type;
849 }
850
851 /**
852  * connman_device_get_name:
853  * @device: device structure
854  *
855  * Get unique name of device
856  */
857 const char *connman_device_get_name(struct connman_device *device)
858 {
859         return device->element.name;
860 }
861
862 /**
863  * connman_device_get_path:
864  * @device: device structure
865  *
866  * Get path name of device
867  */
868 const char *connman_device_get_path(struct connman_device *device)
869 {
870         return device->element.path;
871 }
872
873 /**
874  * connman_device_set_index:
875  * @device: device structure
876  * @index: index number
877  *
878  * Set index number of device
879  */
880 void connman_device_set_index(struct connman_device *device, int index)
881 {
882         device->element.index = index;
883 }
884
885 /**
886  * connman_device_get_index:
887  * @device: device structure
888  *
889  * Get index number of device
890  */
891 int connman_device_get_index(struct connman_device *device)
892 {
893         return device->element.index;
894 }
895
896 int __connman_device_get_phyindex(struct connman_device *device)
897 {
898         return device->phyindex;
899 }
900
901 void __connman_device_set_phyindex(struct connman_device *device,
902                                                         int phyindex)
903 {
904         device->phyindex = phyindex;
905 }
906
907 /**
908  * connman_device_set_interface:
909  * @device: device structure
910  * @interface: interface name
911  * @control: control interface
912  *
913  * Set interface name of device
914  */
915 void connman_device_set_interface(struct connman_device *device,
916                                 const char *interface, const char *control)
917 {
918         g_free(device->element.devname);
919         device->element.devname = g_strdup(interface);
920
921         g_free(device->interface);
922         device->interface = g_strdup(interface);
923
924         g_free(device->control);
925         device->control = g_strdup(control);
926
927         if (device->name == NULL) {
928                 const char *str = type2description(device->type);
929                 if (str != NULL && device->interface != NULL)
930                         device->name = g_strdup_printf("%s (%s)", str,
931                                                         device->interface);
932         }
933 }
934
935 const char *connman_device_get_control(struct connman_device *device)
936 {
937         return device->control;
938 }
939
940 /**
941  * connman_device_set_ident:
942  * @device: device structure
943  * @ident: unique identifier
944  *
945  * Set unique identifier of device
946  */
947 void connman_device_set_ident(struct connman_device *device,
948                                                         const char *ident)
949 {
950         g_free(device->ident);
951         device->ident = g_strdup(ident);
952 }
953
954 const char *__connman_device_get_ident(struct connman_device *device)
955 {
956         return device->ident;
957 }
958
959 /**
960  * connman_device_set_mode:
961  * @device: device structure
962  * @mode: network mode
963  *
964  * Change network mode of device
965  */
966 void connman_device_set_mode(struct connman_device *device,
967                                                 enum connman_device_mode mode)
968 {
969         device->mode = mode;
970 }
971
972 /**
973  * connman_device_get_mode:
974  * @device: device structure
975  *
976  * Get network mode of device
977  */
978 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
979 {
980         return device->mode;
981 }
982
983 /**
984  * connman_device_set_powered:
985  * @device: device structure
986  * @powered: powered state
987  *
988  * Change power state of device
989  */
990 int connman_device_set_powered(struct connman_device *device,
991                                                 connman_bool_t powered)
992 {
993         DBG("driver %p powered %d", device, powered);
994
995         if (device->timeout > 0) {
996                 g_source_remove(device->timeout);
997                 device->timeout = 0;
998         }
999
1000         if (device->pending != NULL) {
1001                 g_dbus_send_reply(connection, device->pending,
1002                                                         DBUS_TYPE_INVALID);
1003
1004                 dbus_message_unref(device->pending);
1005                 device->pending = NULL;
1006         }
1007
1008         if (device->powered == powered) {
1009                 device->powered_pending = powered;
1010                 return -EALREADY;
1011         }
1012
1013         if (powered == TRUE)
1014                 __connman_device_enable(device);
1015         else
1016                 __connman_device_disable(device);
1017
1018         device->powered = powered;
1019         device->powered_pending = powered;
1020
1021         if (device->powered == TRUE)
1022                 __connman_technology_enable_device(device);
1023         else
1024                 __connman_technology_disable_device(device);
1025
1026         if (device->offlinemode == TRUE && powered == TRUE) {
1027                 powered_changed(device);
1028                 return connman_device_set_powered(device, FALSE);
1029         }
1030
1031         if (device->registered == FALSE)
1032                 return 0;
1033
1034         powered_changed(device);
1035
1036         if (powered == FALSE)
1037                 return 0;
1038
1039         reset_scan_trigger(device);
1040
1041         if (device->driver->scan)
1042                 device->driver->scan(device);
1043
1044         return 0;
1045 }
1046
1047 int __connman_device_set_blocked(struct connman_device *device,
1048                                                 connman_bool_t blocked)
1049 {
1050         connman_bool_t powered;
1051
1052         DBG("device %p blocked %d", device, blocked);
1053
1054         device->blocked = blocked;
1055
1056         blocked_changed(device);
1057
1058         if (device->offlinemode == TRUE)
1059                 return 0;
1060
1061         connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1062
1063         if (blocked == FALSE)
1064                 powered = device->powered_persistent;
1065         else
1066                 powered = FALSE;
1067
1068         return set_powered(device, powered);
1069 }
1070
1071 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1072 {
1073         return device->blocked;
1074 }
1075
1076 int __connman_device_scan(struct connman_device *device)
1077 {
1078         if (!device->driver || !device->driver->scan)
1079                 return -EOPNOTSUPP;
1080
1081         if (device->powered == FALSE)
1082                 return -ENOLINK;
1083
1084         reset_scan_trigger(device);
1085
1086         return device->driver->scan(device);
1087 }
1088
1089 int __connman_device_enable_persistent(struct connman_device *device)
1090 {
1091         int err;
1092
1093         DBG("device %p", device);
1094
1095         device->powered_persistent = TRUE;
1096
1097         __connman_storage_save_device(device);
1098
1099         err = __connman_device_enable(device);
1100         if (err == 0 || err == -EINPROGRESS) {
1101                 device->offlinemode = FALSE;
1102                 if (__connman_profile_get_offlinemode() == TRUE) {
1103                         __connman_profile_set_offlinemode(FALSE, FALSE);
1104
1105                         __connman_profile_save_default();
1106                 }
1107         }
1108
1109         return err;
1110 }
1111
1112 int __connman_device_disable_persistent(struct connman_device *device)
1113 {
1114         DBG("device %p", device);
1115
1116         device->powered_persistent = FALSE;
1117
1118         __connman_storage_save_device(device);
1119
1120         return __connman_device_disable(device);
1121 }
1122
1123 int __connman_device_disconnect(struct connman_device *device)
1124 {
1125         GHashTableIter iter;
1126         gpointer key, value;
1127
1128         DBG("device %p", device);
1129
1130         connman_device_set_disconnected(device, TRUE);
1131
1132         g_hash_table_iter_init(&iter, device->networks);
1133
1134         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1135                 struct connman_network *network = value;
1136
1137                 if (__connman_network_get_connecting(network) == TRUE) {
1138                         /*
1139                          * Skip network in the process of connecting.
1140                          * This is a workaround for WiFi networks serviced
1141                          * by the supplicant plugin that hold a reference
1142                          * to the network.  If we disconnect the network
1143                          * here then the referenced object will not be
1144                          * registered and usage (like launching DHCP client)
1145                          * will fail.  There is nothing to be gained by
1146                          * removing the network here anyway.
1147                          */
1148                         connman_warn("Skipping disconnect of %s",
1149                                 connman_network_get_identifier(network));
1150                         continue;
1151                 }
1152
1153                 __connman_network_disconnect(network);
1154         }
1155
1156         return 0;
1157 }
1158
1159 static void mark_network_unavailable(gpointer key, gpointer value,
1160                                                         gpointer user_data)
1161 {
1162         struct connman_network *network = value;
1163
1164         if (connman_network_get_connected(network) == TRUE)
1165                 return;
1166
1167         connman_network_set_available(network, FALSE);
1168 }
1169
1170 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1171                                                         gpointer user_data)
1172 {
1173         struct connman_network *network = value;
1174
1175         if (connman_network_get_connected(network) == TRUE)
1176                 return FALSE;
1177
1178         if (connman_network_get_available(network) == TRUE)
1179                 return FALSE;
1180
1181         return TRUE;
1182 }
1183
1184 void __connman_device_cleanup_networks(struct connman_device *device)
1185 {
1186         g_hash_table_foreach_remove(device->networks,
1187                                         remove_unavailable_network, NULL);
1188 }
1189
1190 static void scanning_changed(struct connman_device *device)
1191 {
1192         connman_dbus_property_changed_basic(device->element.path,
1193                                 CONNMAN_DEVICE_INTERFACE, "Scanning",
1194                                         DBUS_TYPE_BOOLEAN, &device->scanning);
1195 }
1196
1197 /**
1198  * connman_device_set_scanning:
1199  * @device: device structure
1200  * @scanning: scanning state
1201  *
1202  * Change scanning state of device
1203  */
1204 int connman_device_set_scanning(struct connman_device *device,
1205                                                 connman_bool_t scanning)
1206 {
1207         DBG("device %p scanning %d", device, scanning);
1208
1209         if (!device->driver || !device->driver->scan)
1210                 return -EINVAL;
1211
1212         if (device->scanning == scanning)
1213                 return -EALREADY;
1214
1215         device->scanning = scanning;
1216
1217         scanning_changed(device);
1218
1219         if (scanning == TRUE) {
1220                 reset_scan_trigger(device);
1221
1222                 g_hash_table_foreach(device->networks,
1223                                         mark_network_unavailable, NULL);
1224
1225                 return 0;
1226         }
1227
1228         __connman_device_cleanup_networks(device);
1229
1230         if (device->connections > 0)
1231                 return 0;
1232
1233         if (device->disconnected == TRUE)
1234                 return 0;
1235
1236         __connman_service_auto_connect();
1237
1238         return 0;
1239 }
1240
1241 /**
1242  * connman_device_set_disconnected:
1243  * @device: device structure
1244  * @disconnected: disconnected state
1245  *
1246  * Change disconnected state of device (only for device with networks)
1247  */
1248 int connman_device_set_disconnected(struct connman_device *device,
1249                                                 connman_bool_t disconnected)
1250 {
1251         DBG("device %p disconnected %d", device, disconnected);
1252
1253         switch (device->mode) {
1254         case CONNMAN_DEVICE_MODE_UNKNOWN:
1255                 return -EINVAL;
1256         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1257         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1258                 break;
1259         }
1260
1261         if (device->disconnected == disconnected)
1262                 return -EALREADY;
1263
1264         device->disconnected = disconnected;
1265
1266         if (disconnected == TRUE)
1267                 force_scan_trigger(device);
1268
1269         return 0;
1270 }
1271
1272 /**
1273  * connman_device_get_disconnected:
1274  * @device: device structure
1275  *
1276  * Get device disconnected state
1277  */
1278 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1279 {
1280         return device->disconnected;
1281 }
1282
1283 /**
1284  * connman_device_set_string:
1285  * @device: device structure
1286  * @key: unique identifier
1287  * @value: string value
1288  *
1289  * Set string value for specific key
1290  */
1291 int connman_device_set_string(struct connman_device *device,
1292                                         const char *key, const char *value)
1293 {
1294         DBG("device %p key %s value %s", device, key, value);
1295
1296         if (g_str_equal(key, "Address") == TRUE) {
1297                 g_free(device->address);
1298                 device->address = g_strdup(value);
1299         } else if (g_str_equal(key, "Name") == TRUE) {
1300                 g_free(device->name);
1301                 device->name = g_strdup(value);
1302         } else if (g_str_equal(key, "Node") == TRUE) {
1303                 g_free(device->node);
1304                 device->node = g_strdup(value);
1305         }
1306
1307         return connman_element_set_string(&device->element, key, value);
1308 }
1309
1310 /**
1311  * connman_device_get_string:
1312  * @device: device structure
1313  * @key: unique identifier
1314  *
1315  * Get string value for specific key
1316  */
1317 const char *connman_device_get_string(struct connman_device *device,
1318                                                         const char *key)
1319 {
1320         DBG("device %p key %s", device, key);
1321
1322         if (g_str_equal(key, "Address") == TRUE)
1323                 return device->address;
1324         else if (g_str_equal(key, "Name") == TRUE)
1325                 return device->name;
1326         else if (g_str_equal(key, "Node") == TRUE)
1327                 return device->node;
1328
1329         return connman_element_get_string(&device->element, key);
1330 }
1331
1332 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1333 {
1334         struct connman_device *device = element->device;
1335         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1336         connman_bool_t powered;
1337
1338         DBG("element %p name %s", element, element->name);
1339
1340         if (device == NULL)
1341                 return;
1342
1343         device->offlinemode = offlinemode;
1344
1345         if (device->blocked == TRUE)
1346                 return;
1347
1348         powered = (offlinemode == TRUE) ? FALSE : TRUE;
1349
1350         if (device->powered == powered)
1351                 return;
1352
1353         if (device->powered_persistent == FALSE)
1354                 powered = FALSE;
1355
1356         set_powered(device, powered);
1357 }
1358
1359 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1360 {
1361         DBG("offlinmode %d", offlinemode);
1362
1363         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1364                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
1365
1366         __connman_notifier_offlinemode(offlinemode);
1367
1368         return 0;
1369 }
1370
1371 void __connman_device_increase_connections(struct connman_device *device)
1372 {
1373         device->connections++;
1374 }
1375
1376 void __connman_device_decrease_connections(struct connman_device *device)
1377 {
1378         device->connections--;
1379 }
1380
1381 /**
1382  * connman_device_add_network:
1383  * @device: device structure
1384  * @network: network structure
1385  *
1386  * Add new network to the device
1387  */
1388 int connman_device_add_network(struct connman_device *device,
1389                                         struct connman_network *network)
1390 {
1391         const char *identifier = connman_network_get_identifier(network);
1392         int err;
1393
1394         DBG("device %p network %p", device, network);
1395
1396         if (identifier == NULL)
1397                 return -EINVAL;
1398
1399         switch (device->mode) {
1400         case CONNMAN_DEVICE_MODE_UNKNOWN:
1401                 return -EINVAL;
1402         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1403         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1404                 break;
1405         }
1406
1407         __connman_network_set_device(network, device);
1408
1409         err = connman_element_register((struct connman_element *) network,
1410                                                         &device->element);
1411         if (err < 0) {
1412                 __connman_network_set_device(network, NULL);
1413                 return err;
1414         }
1415
1416         g_hash_table_insert(device->networks, g_strdup(identifier),
1417                                                                 network);
1418
1419         return 0;
1420 }
1421
1422 /**
1423  * connman_device_get_network:
1424  * @device: device structure
1425  * @identifier: network identifier
1426  *
1427  * Get network for given identifier
1428  */
1429 struct connman_network *connman_device_get_network(struct connman_device *device,
1430                                                         const char *identifier)
1431 {
1432         DBG("device %p identifier %s", device, identifier);
1433
1434         return g_hash_table_lookup(device->networks, identifier);
1435 }
1436
1437 /**
1438  * connman_device_remove_network:
1439  * @device: device structure
1440  * @identifier: network identifier
1441  *
1442  * Remove network for given identifier
1443  */
1444 int connman_device_remove_network(struct connman_device *device,
1445                                                         const char *identifier)
1446 {
1447         DBG("device %p identifier %s", device, identifier);
1448
1449         g_hash_table_remove(device->networks, identifier);
1450
1451         return 0;
1452 }
1453
1454 void connman_device_remove_all_networks(struct connman_device *device)
1455 {
1456         g_hash_table_remove_all(device->networks);
1457 }
1458
1459 void __connman_device_set_network(struct connman_device *device,
1460                                         struct connman_network *network)
1461 {
1462         const char *name;
1463
1464         if (device == NULL)
1465                 return;
1466
1467         if (device->network == network)
1468                 return;
1469
1470         if (device->network != NULL)
1471                 connman_network_unref(device->network);
1472
1473         if (network != NULL) {
1474                 name = connman_network_get_string(network,
1475                                                 CONNMAN_PROPERTY_ID_NAME);
1476                 g_free(device->last_network);
1477                 device->last_network = g_strdup(name);
1478
1479                 device->network = connman_network_ref(network);
1480         } else {
1481                 g_free(device->last_network);
1482                 device->last_network = NULL;
1483
1484                 device->network = NULL;
1485         }
1486 }
1487
1488 void __connman_device_set_reconnect(struct connman_device *device,
1489                                                 connman_bool_t reconnect)
1490 {
1491         device->reconnect = reconnect;
1492 }
1493
1494 connman_bool_t  __connman_device_get_reconnect(
1495                                 struct connman_device *device)
1496 {
1497         return device->reconnect;
1498 }
1499
1500 /**
1501  * connman_device_register:
1502  * @device: device structure
1503  *
1504  * Register device with the system
1505  */
1506 int connman_device_register(struct connman_device *device)
1507 {
1508         __connman_storage_load_device(device);
1509
1510         device->offlinemode = __connman_profile_get_offlinemode();
1511
1512         return connman_element_register(&device->element, NULL);
1513 }
1514
1515 /**
1516  * connman_device_unregister:
1517  * @device: device structure
1518  *
1519  * Unregister device with the system
1520  */
1521 void connman_device_unregister(struct connman_device *device)
1522 {
1523         __connman_storage_save_device(device);
1524
1525         connman_element_unregister(&device->element);
1526 }
1527
1528 /**
1529  * connman_device_get_data:
1530  * @device: device structure
1531  *
1532  * Get private device data pointer
1533  */
1534 void *connman_device_get_data(struct connman_device *device)
1535 {
1536         return device->driver_data;
1537 }
1538
1539 /**
1540  * connman_device_set_data:
1541  * @device: device structure
1542  * @data: data pointer
1543  *
1544  * Set private device data pointer
1545  */
1546 void connman_device_set_data(struct connman_device *device, void *data)
1547 {
1548         device->driver_data = data;
1549 }
1550
1551 static gboolean match_driver(struct connman_device *device,
1552                                         struct connman_device_driver *driver)
1553 {
1554         if (device->type == driver->type ||
1555                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1556                 return TRUE;
1557
1558         return FALSE;
1559 }
1560
1561 static int device_probe(struct connman_element *element)
1562 {
1563         struct connman_device *device = element->device;
1564         GSList *list;
1565
1566         DBG("element %p name %s", element, element->name);
1567
1568         if (device == NULL)
1569                 return -ENODEV;
1570
1571         if (device->driver != NULL)
1572                 return -EALREADY;
1573
1574         for (list = driver_list; list; list = list->next) {
1575                 struct connman_device_driver *driver = list->data;
1576
1577                 if (match_driver(device, driver) == FALSE)
1578                         continue;
1579
1580                 DBG("driver %p name %s", driver, driver->name);
1581
1582                 if (driver->probe(device) == 0) {
1583                         device->driver = driver;
1584                         break;
1585                 }
1586         }
1587
1588         if (device->driver == NULL)
1589                 return -ENODEV;
1590
1591         return setup_device(device);
1592 }
1593
1594 static void device_remove(struct connman_element *element)
1595 {
1596         struct connman_device *device = element->device;
1597
1598         DBG("element %p name %s", element, element->name);
1599
1600         if (device == NULL)
1601                 return;
1602
1603         if (device->driver == NULL)
1604                 return;
1605
1606         remove_device(device);
1607 }
1608
1609 static struct connman_driver device_driver = {
1610         .name           = "device",
1611         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1612         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1613         .probe          = device_probe,
1614         .remove         = device_remove,
1615 };
1616
1617 static int device_load(struct connman_device *device)
1618 {
1619         const char *ident = __connman_profile_active_ident();
1620         GKeyFile *keyfile;
1621         GError *error = NULL;
1622         gchar *identifier;
1623         connman_bool_t powered;
1624         int val;
1625
1626         DBG("device %p", device);
1627
1628         keyfile = __connman_storage_open_profile(ident);
1629         if (keyfile == NULL)
1630                 return 0;
1631
1632         identifier = g_strdup_printf("device_%s", device->element.name);
1633         if (identifier == NULL)
1634                 goto done;
1635
1636         powered = g_key_file_get_boolean(keyfile, identifier,
1637                                                 "Powered", &error);
1638         if (error == NULL)
1639                 device->powered_persistent = powered;
1640         g_clear_error(&error);
1641
1642         switch (device->mode) {
1643         case CONNMAN_DEVICE_MODE_UNKNOWN:
1644                 break;
1645         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1646         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1647                 val = g_key_file_get_integer(keyfile, identifier,
1648                                                 "ScanInterval", &error);
1649                 if (error == NULL && val > 0)
1650                         device->scan_interval = val;
1651                 g_clear_error(&error);
1652                 break;
1653         }
1654
1655 done:
1656         g_free(identifier);
1657
1658         __connman_storage_close_profile(ident, keyfile, FALSE);
1659
1660         return 0;
1661 }
1662
1663 static int device_save(struct connman_device *device)
1664 {
1665         const char *ident = __connman_profile_active_ident();
1666         GKeyFile *keyfile;
1667         gchar *identifier;
1668
1669         DBG("device %p", device);
1670
1671         keyfile = __connman_storage_open_profile(ident);
1672         if (keyfile == NULL)
1673                 return 0;
1674
1675         identifier = g_strdup_printf("device_%s", device->element.name);
1676         if (identifier == NULL)
1677                 goto done;
1678
1679         g_key_file_set_boolean(keyfile, identifier,
1680                                         "Powered", device->powered_persistent);
1681
1682         switch (device->mode) {
1683         case CONNMAN_DEVICE_MODE_UNKNOWN:
1684                 break;
1685         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1686         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1687                 if (device->scan_interval > 0)
1688                         g_key_file_set_integer(keyfile, identifier,
1689                                         "ScanInterval", device->scan_interval);
1690                 break;
1691         }
1692
1693 done:
1694         g_free(identifier);
1695
1696         __connman_storage_close_profile(ident, keyfile, TRUE);
1697
1698         return 0;
1699 }
1700
1701 static struct connman_storage device_storage = {
1702         .name           = "device",
1703         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1704         .device_load    = device_load,
1705         .device_save    = device_save,
1706 };
1707
1708 int __connman_device_init(void)
1709 {
1710         DBG("");
1711
1712         connection = connman_dbus_get_connection();
1713
1714         if (connman_storage_register(&device_storage) < 0)
1715                 connman_error("Failed to register device storage");
1716
1717         return connman_driver_register(&device_driver);
1718 }
1719
1720 void __connman_device_cleanup(void)
1721 {
1722         DBG("");
1723
1724         connman_driver_unregister(&device_driver);
1725
1726         connman_storage_unregister(&device_storage);
1727
1728         dbus_connection_unref(connection);
1729 }