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