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