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