device: Reset device->connections and device->scanning on device enable
[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 "connman.h"
30
31 struct connman_device {
32         struct connman_element element;
33         enum connman_device_type type;
34         connman_bool_t offlinemode;
35         connman_bool_t blocked;
36         connman_bool_t powered;
37         connman_bool_t powered_pending;
38         connman_bool_t powered_persistent;
39         connman_bool_t scanning;
40         connman_bool_t disconnected;
41         connman_bool_t reconnect;
42         connman_uint16_t scan_interval;
43         char *name;
44         char *node;
45         char *address;
46         char *interface;
47         char *ident;
48         int phyindex;
49         unsigned int connections;
50         guint scan_timeout;
51
52         struct connman_device_driver *driver;
53         void *driver_data;
54
55         char *last_network;
56         struct connman_network *network;
57         GHashTable *networks;
58 };
59
60 static gboolean device_scan_trigger(gpointer user_data)
61 {
62         struct connman_device *device = user_data;
63
64         DBG("device %p", device);
65
66         if (device->driver == NULL) {
67                 device->scan_timeout = 0;
68                 return FALSE;
69         }
70
71         if (device->driver->scan)
72                 device->driver->scan(device);
73
74         return TRUE;
75 }
76
77 static void clear_scan_trigger(struct connman_device *device)
78 {
79         if (device->scan_timeout > 0) {
80                 g_source_remove(device->scan_timeout);
81                 device->scan_timeout = 0;
82         }
83 }
84
85 static void reset_scan_trigger(struct connman_device *device)
86 {
87         clear_scan_trigger(device);
88
89         if (device->scan_interval > 0) {
90                 guint interval = device->scan_interval;
91                 device->scan_timeout = g_timeout_add_seconds(interval,
92                                         device_scan_trigger, device);
93         }
94 }
95
96 static void force_scan_trigger(struct connman_device *device)
97 {
98         clear_scan_trigger(device);
99
100         device->scan_timeout = g_timeout_add_seconds(5,
101                                         device_scan_trigger, device);
102 }
103
104 void connman_device_schedule_scan(struct connman_device *device)
105 {
106         reset_scan_trigger(device);
107 }
108
109 static const char *type2description(enum connman_device_type type)
110 {
111         switch (type) {
112         case CONNMAN_DEVICE_TYPE_UNKNOWN:
113         case CONNMAN_DEVICE_TYPE_VENDOR:
114                 break;
115         case CONNMAN_DEVICE_TYPE_ETHERNET:
116                 return "Ethernet";
117         case CONNMAN_DEVICE_TYPE_WIFI:
118                 return "Wireless";
119         case CONNMAN_DEVICE_TYPE_WIMAX:
120                 return "WiMAX";
121         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
122                 return "Bluetooth";
123         case CONNMAN_DEVICE_TYPE_GPS:
124                 return "GPS";
125         case CONNMAN_DEVICE_TYPE_CELLULAR:
126                 return "Cellular";
127         case CONNMAN_DEVICE_TYPE_GADGET:
128                 return "Gadget";
129
130         }
131
132         return NULL;
133 }
134
135 static const char *type2string(enum connman_device_type type)
136 {
137         switch (type) {
138         case CONNMAN_DEVICE_TYPE_UNKNOWN:
139         case CONNMAN_DEVICE_TYPE_VENDOR:
140                 break;
141         case CONNMAN_DEVICE_TYPE_ETHERNET:
142                 return "ethernet";
143         case CONNMAN_DEVICE_TYPE_WIFI:
144                 return "wifi";
145         case CONNMAN_DEVICE_TYPE_WIMAX:
146                 return "wimax";
147         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
148                 return "bluetooth";
149         case CONNMAN_DEVICE_TYPE_GPS:
150                 return "gps";
151         case CONNMAN_DEVICE_TYPE_CELLULAR:
152                 return "cellular";
153         case CONNMAN_DEVICE_TYPE_GADGET:
154                 return "gadget";
155
156         }
157
158         return NULL;
159 }
160
161 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
162 {
163         enum connman_device_type type = connman_device_get_type(device);
164
165         switch (type) {
166         case CONNMAN_DEVICE_TYPE_UNKNOWN:
167         case CONNMAN_DEVICE_TYPE_VENDOR:
168         case CONNMAN_DEVICE_TYPE_GPS:
169                 break;
170         case CONNMAN_DEVICE_TYPE_ETHERNET:
171                 return CONNMAN_SERVICE_TYPE_ETHERNET;
172         case CONNMAN_DEVICE_TYPE_WIFI:
173                 return CONNMAN_SERVICE_TYPE_WIFI;
174         case CONNMAN_DEVICE_TYPE_WIMAX:
175                 return CONNMAN_SERVICE_TYPE_WIMAX;
176         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
177                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
178         case CONNMAN_DEVICE_TYPE_CELLULAR:
179                 return CONNMAN_SERVICE_TYPE_CELLULAR;
180         case CONNMAN_DEVICE_TYPE_GADGET:
181                 return CONNMAN_SERVICE_TYPE_GADGET;
182
183         }
184
185         return CONNMAN_SERVICE_TYPE_UNKNOWN;
186 }
187
188 int __connman_device_enable(struct connman_device *device)
189 {
190         int err;
191
192         DBG("device %p %d", device, device->blocked);
193
194         if (!device->driver || !device->driver->enable)
195                 return -EOPNOTSUPP;
196
197         if (device->powered_pending == TRUE)
198                 return -EALREADY;
199
200         if (device->blocked == TRUE)
201                 return -ENOLINK;
202
203         connman_device_set_disconnected(device, FALSE);
204         device->scanning = FALSE;
205
206         err = device->driver->enable(device);
207         if (err < 0 && err != -EALREADY) {
208                 if (err == -EINPROGRESS) {
209                         device->powered_pending = TRUE;
210                         device->offlinemode = FALSE;
211                         if (__connman_profile_get_offlinemode() == TRUE)
212                                 __connman_profile_set_offlinemode(FALSE, FALSE);
213                 }
214                 return err;
215         }
216
217         device->powered_pending = TRUE;
218         device->powered = TRUE;
219         device->offlinemode = FALSE;
220         if (__connman_profile_get_offlinemode() == TRUE)
221                 __connman_profile_set_offlinemode(FALSE, FALSE);
222
223         __connman_technology_enable_device(device);
224
225         return 0;
226 }
227
228 int __connman_device_disable(struct connman_device *device)
229 {
230         int err;
231
232         DBG("device %p", device);
233
234         if (!device->driver || !device->driver->disable)
235                 return -EOPNOTSUPP;
236
237         if (device->powered == FALSE)
238                 return -ENOLINK;
239
240         if (device->powered_pending == FALSE)
241                 return -EALREADY;
242
243         device->reconnect = FALSE;
244
245         clear_scan_trigger(device);
246
247         g_hash_table_remove_all(device->networks);
248
249         err = device->driver->disable(device);
250         if (err < 0 && err != -EALREADY) {
251                 if (err == -EINPROGRESS)
252                         device->powered_pending = FALSE;
253                 return err;
254         }
255
256         device->connections = 0;
257
258         device->powered_pending = FALSE;
259         device->powered = FALSE;
260
261         __connman_technology_disable_device(device);
262
263         return 0;
264 }
265
266 static int set_powered(struct connman_device *device, connman_bool_t powered)
267 {
268         DBG("device %p powered %d", device, powered);
269
270         if (powered == TRUE)
271                 return __connman_device_enable(device);
272         else
273                 return __connman_device_disable(device);
274 }
275
276 static int setup_device(struct connman_device *device)
277 {
278         DBG("device %p", device);
279
280         __connman_technology_add_device(device);
281
282         if (device->offlinemode == FALSE &&
283                                 device->powered_persistent == TRUE)
284                 __connman_device_enable(device);
285
286         return 0;
287 }
288
289 static void probe_driver(struct connman_element *element, gpointer user_data)
290 {
291         struct connman_device_driver *driver = user_data;
292
293         DBG("element %p name %s", element, element->name);
294
295         if (element->device == NULL)
296                 return;
297
298         if (element->device->driver != NULL)
299                 return;
300
301         if (driver->type != element->device->type)
302                 return;
303
304         if (driver->probe(element->device) < 0)
305                 return;
306
307         element->device->driver = driver;
308
309         __connman_element_set_driver(element);
310
311         setup_device(element->device);
312 }
313
314 static void remove_device(struct connman_device *device)
315 {
316         DBG("device %p", device);
317
318         __connman_device_disable(device);
319
320         __connman_technology_remove_device(device);
321
322         if (device->driver->remove)
323                 device->driver->remove(device);
324
325         device->driver = NULL;
326 }
327
328 static void remove_driver(struct connman_element *element, gpointer user_data)
329 {
330         struct connman_device_driver *driver = user_data;
331
332         DBG("element %p name %s", element, element->name);
333
334         if (element->device == NULL)
335                 return;
336
337         if (element->device->driver == driver)
338                 remove_device(element->device);
339 }
340
341 connman_bool_t __connman_device_has_driver(struct connman_device *device)
342 {
343         if (device == NULL || device->driver == NULL)
344                 return FALSE;
345
346         return TRUE;
347 }
348
349 static GSList *driver_list = NULL;
350
351 static gint compare_priority(gconstpointer a, gconstpointer b)
352 {
353         const struct connman_device_driver *driver1 = a;
354         const struct connman_device_driver *driver2 = b;
355
356         return driver2->priority - driver1->priority;
357 }
358
359 /**
360  * connman_device_driver_register:
361  * @driver: device driver definition
362  *
363  * Register a new device driver
364  *
365  * Returns: %0 on success
366  */
367 int connman_device_driver_register(struct connman_device_driver *driver)
368 {
369         DBG("driver %p name %s", driver, driver->name);
370
371         driver_list = g_slist_insert_sorted(driver_list, driver,
372                                                         compare_priority);
373
374         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
375                                                 probe_driver, driver);
376
377         return 0;
378 }
379
380 /**
381  * connman_device_driver_unregister:
382  * @driver: device driver definition
383  *
384  * Remove a previously registered device driver
385  */
386 void connman_device_driver_unregister(struct connman_device_driver *driver)
387 {
388         DBG("driver %p name %s", driver, driver->name);
389
390         driver_list = g_slist_remove(driver_list, driver);
391
392         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
393                                                 remove_driver, driver);
394 }
395
396 static void unregister_network(gpointer data)
397 {
398         struct connman_network *network = data;
399
400         DBG("network %p", network);
401
402         connman_element_unregister((struct connman_element *) network);
403
404         __connman_network_set_device(network, NULL);
405
406         connman_network_unref(network);
407 }
408
409 static void device_destruct(struct connman_element *element)
410 {
411         struct connman_device *device = element->device;
412
413         DBG("element %p name %s", element, element->name);
414
415         clear_scan_trigger(device);
416
417         g_free(device->ident);
418         g_free(device->node);
419         g_free(device->name);
420         g_free(device->address);
421         g_free(device->interface);
422
423         g_free(device->last_network);
424
425         g_hash_table_destroy(device->networks);
426         device->networks = NULL;
427 }
428
429 /**
430  * connman_device_create:
431  * @node: device node name (for example an address)
432  * @type: device type
433  *
434  * Allocate a new device of given #type and assign the #node name to it.
435  *
436  * Returns: a newly-allocated #connman_device structure
437  */
438 struct connman_device *connman_device_create(const char *node,
439                                                 enum connman_device_type type)
440 {
441         struct connman_device *device;
442         const char *str;
443         enum connman_service_type service_type;
444
445         DBG("node %s type %d", node, type);
446
447         device = g_try_new0(struct connman_device, 1);
448         if (device == NULL)
449                 return NULL;
450
451         DBG("device %p", device);
452
453         __connman_element_initialize(&device->element);
454
455         device->element.name = g_strdup(node);
456         device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
457
458         device->element.device = device;
459         device->element.destruct = device_destruct;
460
461         str = type2string(type);
462         if (str != NULL)
463                 connman_element_set_string(&device->element,
464                                         CONNMAN_PROPERTY_ID_TYPE, str);
465
466         device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
467
468         device->type = type;
469         device->name = g_strdup(type2description(device->type));
470
471         device->powered_persistent = TRUE;
472
473         device->phyindex = -1;
474
475         service_type = __connman_device_get_service_type(device);
476         device->blocked = __connman_technology_get_blocked(service_type);
477
478         switch (type) {
479         case CONNMAN_DEVICE_TYPE_UNKNOWN:
480         case CONNMAN_DEVICE_TYPE_VENDOR:
481                 device->scan_interval = 0;
482                 break;
483         case CONNMAN_DEVICE_TYPE_ETHERNET:
484         case CONNMAN_DEVICE_TYPE_WIFI:
485                 device->scan_interval = 300;
486                 break;
487         case CONNMAN_DEVICE_TYPE_WIMAX:
488                 device->scan_interval = 0;
489                 break;
490         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
491                 device->scan_interval = 0;
492                 break;
493         case CONNMAN_DEVICE_TYPE_GPS:
494                 device->scan_interval = 0;
495                 break;
496         case CONNMAN_DEVICE_TYPE_CELLULAR:
497                 device->scan_interval = 0;
498                 break;
499         case CONNMAN_DEVICE_TYPE_GADGET:
500                 device->scan_interval = 0;
501                 break;
502         }
503
504         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
505                                                 g_free, unregister_network);
506
507         return device;
508 }
509
510 /**
511  * connman_device_ref:
512  * @device: device structure
513  *
514  * Increase reference counter of device
515  */
516 struct connman_device *connman_device_ref(struct connman_device *device)
517 {
518         if (connman_element_ref(&device->element) == NULL)
519                 return NULL;
520
521         return device;
522 }
523
524 /**
525  * connman_device_unref:
526  * @device: device structure
527  *
528  * Decrease reference counter of device
529  */
530 void connman_device_unref(struct connman_device *device)
531 {
532         connman_element_unref(&device->element);
533 }
534
535 const char *__connman_device_get_type(struct connman_device *device)
536 {
537         return type2string(device->type);
538 }
539
540 /**
541  * connman_device_get_type:
542  * @device: device structure
543  *
544  * Get type of device
545  */
546 enum connman_device_type connman_device_get_type(struct connman_device *device)
547 {
548         return device->type;
549 }
550
551 /**
552  * connman_device_set_index:
553  * @device: device structure
554  * @index: index number
555  *
556  * Set index number of device
557  */
558 void connman_device_set_index(struct connman_device *device, int index)
559 {
560         device->element.index = index;
561 }
562
563 /**
564  * connman_device_get_index:
565  * @device: device structure
566  *
567  * Get index number of device
568  */
569 int connman_device_get_index(struct connman_device *device)
570 {
571         return device->element.index;
572 }
573
574 int __connman_device_get_phyindex(struct connman_device *device)
575 {
576         return device->phyindex;
577 }
578
579 void __connman_device_set_phyindex(struct connman_device *device,
580                                                         int phyindex)
581 {
582         device->phyindex = phyindex;
583 }
584
585 /**
586  * connman_device_set_interface:
587  * @device: device structure
588  * @interface: interface name
589  *
590  * Set interface name of device
591  */
592 void connman_device_set_interface(struct connman_device *device,
593                                                 const char *interface)
594 {
595         g_free(device->element.devname);
596         device->element.devname = g_strdup(interface);
597
598         g_free(device->interface);
599         device->interface = g_strdup(interface);
600
601         if (device->name == NULL) {
602                 const char *str = type2description(device->type);
603                 if (str != NULL && device->interface != NULL)
604                         device->name = g_strdup_printf("%s (%s)", str,
605                                                         device->interface);
606         }
607 }
608
609 /**
610  * connman_device_set_ident:
611  * @device: device structure
612  * @ident: unique identifier
613  *
614  * Set unique identifier of device
615  */
616 void connman_device_set_ident(struct connman_device *device,
617                                                         const char *ident)
618 {
619         g_free(device->ident);
620         device->ident = g_strdup(ident);
621 }
622
623 const char *connman_device_get_ident(struct connman_device *device)
624 {
625         return device->ident;
626 }
627
628 /**
629  * connman_device_set_powered:
630  * @device: device structure
631  * @powered: powered state
632  *
633  * Change power state of device
634  */
635 int connman_device_set_powered(struct connman_device *device,
636                                                 connman_bool_t powered)
637 {
638         int err;
639
640         DBG("driver %p powered %d", device, powered);
641
642         if (device->powered == powered) {
643                 device->powered_pending = powered;
644                 return -EALREADY;
645         }
646
647         if (powered == TRUE)
648                 err = __connman_device_enable(device);
649         else
650                 err = __connman_device_disable(device);
651
652         if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
653                 return err;
654
655         device->powered = powered;
656         device->powered_pending = powered;
657
658         if (device->powered == TRUE)
659                 __connman_technology_enable_device(device);
660         else
661                 __connman_technology_disable_device(device);
662
663         if (device->offlinemode == TRUE && powered == TRUE)
664                 return connman_device_set_powered(device, FALSE);
665
666         if (powered == FALSE)
667                 return 0;
668
669         reset_scan_trigger(device);
670
671         if (device->driver && device->driver->scan)
672                 device->driver->scan(device);
673
674         return 0;
675 }
676
677 int __connman_device_set_blocked(struct connman_device *device,
678                                                 connman_bool_t blocked)
679 {
680         connman_bool_t powered;
681
682         DBG("device %p blocked %d", device, blocked);
683
684         device->blocked = blocked;
685
686         if (device->offlinemode == TRUE)
687                 return 0;
688
689         connman_info("%s {rfkill} blocked %d", device->interface, blocked);
690
691         if (blocked == FALSE)
692                 powered = device->powered_persistent;
693         else
694                 powered = FALSE;
695
696         return set_powered(device, powered);
697 }
698
699 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
700 {
701         return device->blocked;
702 }
703
704 int __connman_device_scan(struct connman_device *device)
705 {
706         if (!device->driver || !device->driver->scan)
707                 return -EOPNOTSUPP;
708
709         if (device->powered == FALSE)
710                 return -ENOLINK;
711
712         reset_scan_trigger(device);
713
714         return device->driver->scan(device);
715 }
716
717 int __connman_device_enable_persistent(struct connman_device *device)
718 {
719         int err;
720
721         DBG("device %p", device);
722
723         device->powered_persistent = TRUE;
724
725         __connman_storage_save_device(device);
726
727         err = __connman_device_enable(device);
728         if (err == 0 || err == -EINPROGRESS) {
729                 device->offlinemode = FALSE;
730                 if (__connman_profile_get_offlinemode() == TRUE) {
731                         __connman_profile_set_offlinemode(FALSE, FALSE);
732
733                         __connman_profile_save_default();
734                 }
735         }
736
737         return err;
738 }
739
740 int __connman_device_disable_persistent(struct connman_device *device)
741 {
742         DBG("device %p", device);
743
744         device->powered_persistent = FALSE;
745
746         __connman_storage_save_device(device);
747
748         return __connman_device_disable(device);
749 }
750
751 int __connman_device_disconnect(struct connman_device *device)
752 {
753         GHashTableIter iter;
754         gpointer key, value;
755
756         DBG("device %p", device);
757
758         connman_device_set_disconnected(device, TRUE);
759
760         g_hash_table_iter_init(&iter, device->networks);
761
762         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
763                 struct connman_network *network = value;
764
765                 if (__connman_network_get_connecting(network) == TRUE) {
766                         /*
767                          * Skip network in the process of connecting.
768                          * This is a workaround for WiFi networks serviced
769                          * by the supplicant plugin that hold a reference
770                          * to the network.  If we disconnect the network
771                          * here then the referenced object will not be
772                          * registered and usage (like launching DHCP client)
773                          * will fail.  There is nothing to be gained by
774                          * removing the network here anyway.
775                          */
776                         connman_warn("Skipping disconnect of %s",
777                                 connman_network_get_identifier(network));
778                         continue;
779                 }
780
781                 __connman_network_disconnect(network);
782         }
783
784         return 0;
785 }
786
787 static void mark_network_unavailable(gpointer key, gpointer value,
788                                                         gpointer user_data)
789 {
790         struct connman_network *network = value;
791
792         if (connman_network_get_connected(network) == TRUE)
793                 return;
794
795         connman_network_set_available(network, FALSE);
796 }
797
798 static gboolean remove_unavailable_network(gpointer key, gpointer value,
799                                                         gpointer user_data)
800 {
801         struct connman_network *network = value;
802
803         if (connman_network_get_connected(network) == TRUE)
804                 return FALSE;
805
806         if (connman_network_get_available(network) == TRUE)
807                 return FALSE;
808
809         return TRUE;
810 }
811
812 void __connman_device_cleanup_networks(struct connman_device *device)
813 {
814         g_hash_table_foreach_remove(device->networks,
815                                         remove_unavailable_network, NULL);
816 }
817
818 /**
819  * connman_device_set_scanning:
820  * @device: device structure
821  * @scanning: scanning state
822  *
823  * Change scanning state of device
824  */
825 int connman_device_set_scanning(struct connman_device *device,
826                                                 connman_bool_t scanning)
827 {
828         DBG("device %p scanning %d", device, scanning);
829
830         if (!device->driver || !device->driver->scan)
831                 return -EINVAL;
832
833         if (device->scanning == scanning)
834                 return -EALREADY;
835
836         device->scanning = scanning;
837
838         if (scanning == TRUE) {
839                 reset_scan_trigger(device);
840
841                 g_hash_table_foreach(device->networks,
842                                         mark_network_unavailable, NULL);
843
844                 return 0;
845         }
846
847         __connman_device_cleanup_networks(device);
848
849         if (device->connections > 0)
850                 return 0;
851
852         if (device->disconnected == TRUE)
853                 return 0;
854
855         __connman_service_auto_connect();
856
857         return 0;
858 }
859
860 /**
861  * connman_device_set_disconnected:
862  * @device: device structure
863  * @disconnected: disconnected state
864  *
865  * Change disconnected state of device (only for device with networks)
866  */
867 int connman_device_set_disconnected(struct connman_device *device,
868                                                 connman_bool_t disconnected)
869 {
870         DBG("device %p disconnected %d", device, disconnected);
871
872         if (device->disconnected == disconnected)
873                 return -EALREADY;
874
875         device->disconnected = disconnected;
876
877         if (disconnected == TRUE)
878                 force_scan_trigger(device);
879
880         return 0;
881 }
882
883 /**
884  * connman_device_get_disconnected:
885  * @device: device structure
886  *
887  * Get device disconnected state
888  */
889 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
890 {
891         return device->disconnected;
892 }
893
894 /**
895  * connman_device_set_string:
896  * @device: device structure
897  * @key: unique identifier
898  * @value: string value
899  *
900  * Set string value for specific key
901  */
902 int connman_device_set_string(struct connman_device *device,
903                                         const char *key, const char *value)
904 {
905         DBG("device %p key %s value %s", device, key, value);
906
907         if (g_str_equal(key, "Address") == TRUE) {
908                 g_free(device->address);
909                 device->address = g_strdup(value);
910         } else if (g_str_equal(key, "Name") == TRUE) {
911                 g_free(device->name);
912                 device->name = g_strdup(value);
913         } else if (g_str_equal(key, "Node") == TRUE) {
914                 g_free(device->node);
915                 device->node = g_strdup(value);
916         }
917
918         return connman_element_set_string(&device->element, key, value);
919 }
920
921 /**
922  * connman_device_get_string:
923  * @device: device structure
924  * @key: unique identifier
925  *
926  * Get string value for specific key
927  */
928 const char *connman_device_get_string(struct connman_device *device,
929                                                         const char *key)
930 {
931         DBG("device %p key %s", device, key);
932
933         if (g_str_equal(key, "Address") == TRUE)
934                 return device->address;
935         else if (g_str_equal(key, "Name") == TRUE)
936                 return device->name;
937         else if (g_str_equal(key, "Node") == TRUE)
938                 return device->node;
939         else if (g_str_equal(key, "Interface") == TRUE)
940                 return device->interface;
941
942         return connman_element_get_string(&device->element, key);
943 }
944
945 static void set_offlinemode(struct connman_element *element, gpointer user_data)
946 {
947         struct connman_device *device = element->device;
948         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
949         connman_bool_t powered;
950
951         DBG("element %p name %s", element, element->name);
952
953         if (device == NULL)
954                 return;
955
956         device->offlinemode = offlinemode;
957
958         if (device->blocked == TRUE)
959                 return;
960
961         powered = (offlinemode == TRUE) ? FALSE : TRUE;
962
963         if (device->powered == powered)
964                 return;
965
966         if (device->powered_persistent == FALSE)
967                 powered = FALSE;
968
969         set_powered(device, powered);
970 }
971
972 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
973 {
974         DBG("offlinmode %d", offlinemode);
975
976         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
977                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
978
979         __connman_notifier_offlinemode(offlinemode);
980
981         return 0;
982 }
983
984 void __connman_device_increase_connections(struct connman_device *device)
985 {
986         if (device == NULL)
987                 return;
988
989         device->connections++;
990 }
991
992 void __connman_device_decrease_connections(struct connman_device *device)
993 {
994         if (device == NULL)
995                 return;
996
997         device->connections--;
998 }
999
1000 /**
1001  * connman_device_add_network:
1002  * @device: device structure
1003  * @network: network structure
1004  *
1005  * Add new network to the device
1006  */
1007 int connman_device_add_network(struct connman_device *device,
1008                                         struct connman_network *network)
1009 {
1010         const char *identifier = connman_network_get_identifier(network);
1011         int err;
1012
1013         DBG("device %p network %p", device, network);
1014
1015         if (identifier == NULL)
1016                 return -EINVAL;
1017
1018         __connman_network_set_device(network, device);
1019
1020         err = connman_element_register((struct connman_element *) network,
1021                                                         &device->element);
1022         if (err < 0) {
1023                 __connman_network_set_device(network, NULL);
1024                 return err;
1025         }
1026
1027         g_hash_table_insert(device->networks, g_strdup(identifier),
1028                                                                 network);
1029
1030         return 0;
1031 }
1032
1033 /**
1034  * connman_device_get_network:
1035  * @device: device structure
1036  * @identifier: network identifier
1037  *
1038  * Get network for given identifier
1039  */
1040 struct connman_network *connman_device_get_network(struct connman_device *device,
1041                                                         const char *identifier)
1042 {
1043         DBG("device %p identifier %s", device, identifier);
1044
1045         return g_hash_table_lookup(device->networks, identifier);
1046 }
1047
1048 /**
1049  * connman_device_remove_network:
1050  * @device: device structure
1051  * @identifier: network identifier
1052  *
1053  * Remove network for given identifier
1054  */
1055 int connman_device_remove_network(struct connman_device *device,
1056                                                         const char *identifier)
1057 {
1058         DBG("device %p identifier %s", device, identifier);
1059
1060         g_hash_table_remove(device->networks, identifier);
1061
1062         return 0;
1063 }
1064
1065 void connman_device_remove_all_networks(struct connman_device *device)
1066 {
1067         g_hash_table_remove_all(device->networks);
1068 }
1069
1070 void __connman_device_set_network(struct connman_device *device,
1071                                         struct connman_network *network)
1072 {
1073         const char *name;
1074
1075         if (device == NULL)
1076                 return;
1077
1078         if (device->network == network)
1079                 return;
1080
1081         if (device->network != NULL)
1082                 connman_network_unref(device->network);
1083
1084         if (network != NULL) {
1085                 name = connman_network_get_string(network,
1086                                                 CONNMAN_PROPERTY_ID_NAME);
1087                 g_free(device->last_network);
1088                 device->last_network = g_strdup(name);
1089
1090                 device->network = connman_network_ref(network);
1091         } else {
1092                 g_free(device->last_network);
1093                 device->last_network = NULL;
1094
1095                 device->network = NULL;
1096         }
1097 }
1098
1099 void __connman_device_set_reconnect(struct connman_device *device,
1100                                                 connman_bool_t reconnect)
1101 {
1102         device->reconnect = reconnect;
1103 }
1104
1105 connman_bool_t  __connman_device_get_reconnect(
1106                                 struct connman_device *device)
1107 {
1108         return device->reconnect;
1109 }
1110
1111 /**
1112  * connman_device_register:
1113  * @device: device structure
1114  *
1115  * Register device with the system
1116  */
1117 int connman_device_register(struct connman_device *device)
1118 {
1119         __connman_storage_load_device(device);
1120
1121         device->offlinemode = __connman_profile_get_offlinemode();
1122
1123         return connman_element_register(&device->element, NULL);
1124 }
1125
1126 /**
1127  * connman_device_unregister:
1128  * @device: device structure
1129  *
1130  * Unregister device with the system
1131  */
1132 void connman_device_unregister(struct connman_device *device)
1133 {
1134         __connman_storage_save_device(device);
1135
1136         connman_element_unregister(&device->element);
1137 }
1138
1139 /**
1140  * connman_device_get_data:
1141  * @device: device structure
1142  *
1143  * Get private device data pointer
1144  */
1145 void *connman_device_get_data(struct connman_device *device)
1146 {
1147         return device->driver_data;
1148 }
1149
1150 /**
1151  * connman_device_set_data:
1152  * @device: device structure
1153  * @data: data pointer
1154  *
1155  * Set private device data pointer
1156  */
1157 void connman_device_set_data(struct connman_device *device, void *data)
1158 {
1159         device->driver_data = data;
1160 }
1161
1162 static gboolean match_driver(struct connman_device *device,
1163                                         struct connman_device_driver *driver)
1164 {
1165         if (device->type == driver->type ||
1166                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1167                 return TRUE;
1168
1169         return FALSE;
1170 }
1171
1172 static int device_probe(struct connman_element *element)
1173 {
1174         struct connman_device *device = element->device;
1175         GSList *list;
1176
1177         DBG("element %p name %s", element, element->name);
1178
1179         if (device == NULL)
1180                 return -ENODEV;
1181
1182         if (device->driver != NULL)
1183                 return -EALREADY;
1184
1185         for (list = driver_list; list; list = list->next) {
1186                 struct connman_device_driver *driver = list->data;
1187
1188                 if (match_driver(device, driver) == FALSE)
1189                         continue;
1190
1191                 DBG("driver %p name %s", driver, driver->name);
1192
1193                 if (driver->probe(device) == 0) {
1194                         device->driver = driver;
1195                         break;
1196                 }
1197         }
1198
1199         if (device->driver == NULL)
1200                 return -ENODEV;
1201
1202         return setup_device(device);
1203 }
1204
1205 static void device_remove(struct connman_element *element)
1206 {
1207         struct connman_device *device = element->device;
1208
1209         DBG("element %p name %s", element, element->name);
1210
1211         if (device == NULL)
1212                 return;
1213
1214         if (device->driver == NULL)
1215                 return;
1216
1217         remove_device(device);
1218 }
1219
1220 static struct connman_driver device_driver = {
1221         .name           = "device",
1222         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1223         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1224         .probe          = device_probe,
1225         .remove         = device_remove,
1226 };
1227
1228 static int device_load(struct connman_device *device)
1229 {
1230         const char *ident = __connman_profile_active_ident();
1231         GKeyFile *keyfile;
1232         GError *error = NULL;
1233         gchar *identifier;
1234         connman_bool_t powered;
1235         int val;
1236
1237         DBG("device %p", device);
1238
1239         keyfile = __connman_storage_open_profile(ident);
1240         if (keyfile == NULL)
1241                 return 0;
1242
1243         identifier = g_strdup_printf("device_%s", device->element.name);
1244         if (identifier == NULL)
1245                 goto done;
1246
1247         powered = g_key_file_get_boolean(keyfile, identifier,
1248                                                 "Powered", &error);
1249         if (error == NULL)
1250                 device->powered_persistent = powered;
1251         g_clear_error(&error);
1252
1253         val = g_key_file_get_integer(keyfile, identifier,
1254                                                 "ScanInterval", &error);
1255         if (error == NULL)
1256                 device->scan_interval = val;
1257         g_clear_error(&error);
1258
1259 done:
1260         g_free(identifier);
1261
1262         __connman_storage_close_profile(ident, keyfile, FALSE);
1263
1264         return 0;
1265 }
1266
1267 static int device_save(struct connman_device *device)
1268 {
1269         const char *ident = __connman_profile_active_ident();
1270         GKeyFile *keyfile;
1271         gchar *identifier;
1272
1273         DBG("device %p", device);
1274
1275         keyfile = __connman_storage_open_profile(ident);
1276         if (keyfile == NULL)
1277                 return 0;
1278
1279         identifier = g_strdup_printf("device_%s", device->element.name);
1280         if (identifier == NULL)
1281                 goto done;
1282
1283         g_key_file_set_boolean(keyfile, identifier,
1284                                         "Powered", device->powered_persistent);
1285
1286         g_key_file_set_integer(keyfile, identifier,
1287                                         "ScanInterval", device->scan_interval);
1288
1289 done:
1290         g_free(identifier);
1291
1292         __connman_storage_close_profile(ident, keyfile, TRUE);
1293
1294         return 0;
1295 }
1296
1297 static struct connman_storage device_storage = {
1298         .name           = "device",
1299         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1300         .device_load    = device_load,
1301         .device_save    = device_save,
1302 };
1303
1304 int __connman_device_init(void)
1305 {
1306         DBG("");
1307
1308         if (connman_storage_register(&device_storage) < 0)
1309                 connman_error("Failed to register device storage");
1310
1311         return connman_driver_register(&device_driver);
1312 }
1313
1314 void __connman_device_cleanup(void)
1315 {
1316         DBG("");
1317
1318         connman_driver_unregister(&device_driver);
1319
1320         connman_storage_unregister(&device_storage);
1321 }