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