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