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