device: Do not use the disconnected flag for auto connection
[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 connman_bool_t __connman_device_scanning(struct connman_device *device)
819 {
820         return device->scanning;
821 }
822
823 /**
824  * connman_device_set_scanning:
825  * @device: device structure
826  * @scanning: scanning state
827  *
828  * Change scanning state of device
829  */
830 int connman_device_set_scanning(struct connman_device *device,
831                                                 connman_bool_t scanning)
832 {
833         DBG("device %p scanning %d", device, scanning);
834
835         if (!device->driver || !device->driver->scan)
836                 return -EINVAL;
837
838         if (device->scanning == scanning)
839                 return -EALREADY;
840
841         device->scanning = scanning;
842
843         if (scanning == TRUE) {
844                 reset_scan_trigger(device);
845
846                 g_hash_table_foreach(device->networks,
847                                         mark_network_unavailable, NULL);
848
849                 return 0;
850         }
851
852         __connman_device_cleanup_networks(device);
853
854         if (device->connections > 0)
855                 return 0;
856
857         __connman_service_auto_connect();
858
859         return 0;
860 }
861
862 /**
863  * connman_device_set_disconnected:
864  * @device: device structure
865  * @disconnected: disconnected state
866  *
867  * Change disconnected state of device (only for device with networks)
868  */
869 int connman_device_set_disconnected(struct connman_device *device,
870                                                 connman_bool_t disconnected)
871 {
872         DBG("device %p disconnected %d", device, disconnected);
873
874         if (device->disconnected == disconnected)
875                 return -EALREADY;
876
877         device->disconnected = disconnected;
878
879         if (disconnected == TRUE)
880                 force_scan_trigger(device);
881
882         return 0;
883 }
884
885 /**
886  * connman_device_get_disconnected:
887  * @device: device structure
888  *
889  * Get device disconnected state
890  */
891 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
892 {
893         return device->disconnected;
894 }
895
896 /**
897  * connman_device_set_string:
898  * @device: device structure
899  * @key: unique identifier
900  * @value: string value
901  *
902  * Set string value for specific key
903  */
904 int connman_device_set_string(struct connman_device *device,
905                                         const char *key, const char *value)
906 {
907         DBG("device %p key %s value %s", device, key, value);
908
909         if (g_str_equal(key, "Address") == TRUE) {
910                 g_free(device->address);
911                 device->address = g_strdup(value);
912         } else if (g_str_equal(key, "Name") == TRUE) {
913                 g_free(device->name);
914                 device->name = g_strdup(value);
915         } else if (g_str_equal(key, "Node") == TRUE) {
916                 g_free(device->node);
917                 device->node = g_strdup(value);
918         }
919
920         return connman_element_set_string(&device->element, key, value);
921 }
922
923 /**
924  * connman_device_get_string:
925  * @device: device structure
926  * @key: unique identifier
927  *
928  * Get string value for specific key
929  */
930 const char *connman_device_get_string(struct connman_device *device,
931                                                         const char *key)
932 {
933         DBG("device %p key %s", device, key);
934
935         if (g_str_equal(key, "Address") == TRUE)
936                 return device->address;
937         else if (g_str_equal(key, "Name") == TRUE)
938                 return device->name;
939         else if (g_str_equal(key, "Node") == TRUE)
940                 return device->node;
941         else if (g_str_equal(key, "Interface") == TRUE)
942                 return device->interface;
943
944         return connman_element_get_string(&device->element, key);
945 }
946
947 static void set_offlinemode(struct connman_element *element, gpointer user_data)
948 {
949         struct connman_device *device = element->device;
950         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
951         connman_bool_t powered;
952
953         DBG("element %p name %s", element, element->name);
954
955         if (device == NULL)
956                 return;
957
958         device->offlinemode = offlinemode;
959
960         if (device->blocked == TRUE)
961                 return;
962
963         powered = (offlinemode == TRUE) ? FALSE : TRUE;
964
965         if (device->powered == powered)
966                 return;
967
968         if (device->powered_persistent == FALSE)
969                 powered = FALSE;
970
971         set_powered(device, powered);
972 }
973
974 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
975 {
976         DBG("offlinmode %d", offlinemode);
977
978         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
979                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
980
981         __connman_notifier_offlinemode(offlinemode);
982
983         return 0;
984 }
985
986 void __connman_device_increase_connections(struct connman_device *device)
987 {
988         if (device == NULL)
989                 return;
990
991         device->connections++;
992 }
993
994 void __connman_device_decrease_connections(struct connman_device *device)
995 {
996         if (device == NULL)
997                 return;
998
999         device->connections--;
1000 }
1001
1002 /**
1003  * connman_device_add_network:
1004  * @device: device structure
1005  * @network: network structure
1006  *
1007  * Add new network to the device
1008  */
1009 int connman_device_add_network(struct connman_device *device,
1010                                         struct connman_network *network)
1011 {
1012         const char *identifier = connman_network_get_identifier(network);
1013         int err;
1014
1015         DBG("device %p network %p", device, network);
1016
1017         if (identifier == NULL)
1018                 return -EINVAL;
1019
1020         __connman_network_set_device(network, device);
1021
1022         err = connman_element_register((struct connman_element *) network,
1023                                                         &device->element);
1024         if (err < 0) {
1025                 __connman_network_set_device(network, NULL);
1026                 return err;
1027         }
1028
1029         g_hash_table_insert(device->networks, g_strdup(identifier),
1030                                                                 network);
1031
1032         return 0;
1033 }
1034
1035 /**
1036  * connman_device_get_network:
1037  * @device: device structure
1038  * @identifier: network identifier
1039  *
1040  * Get network for given identifier
1041  */
1042 struct connman_network *connman_device_get_network(struct connman_device *device,
1043                                                         const char *identifier)
1044 {
1045         DBG("device %p identifier %s", device, identifier);
1046
1047         return g_hash_table_lookup(device->networks, identifier);
1048 }
1049
1050 /**
1051  * connman_device_remove_network:
1052  * @device: device structure
1053  * @identifier: network identifier
1054  *
1055  * Remove network for given identifier
1056  */
1057 int connman_device_remove_network(struct connman_device *device,
1058                                                         const char *identifier)
1059 {
1060         DBG("device %p identifier %s", device, identifier);
1061
1062         g_hash_table_remove(device->networks, identifier);
1063
1064         return 0;
1065 }
1066
1067 void connman_device_remove_all_networks(struct connman_device *device)
1068 {
1069         g_hash_table_remove_all(device->networks);
1070 }
1071
1072 void __connman_device_set_network(struct connman_device *device,
1073                                         struct connman_network *network)
1074 {
1075         const char *name;
1076
1077         if (device == NULL)
1078                 return;
1079
1080         if (device->network == network)
1081                 return;
1082
1083         if (device->network != NULL)
1084                 connman_network_unref(device->network);
1085
1086         if (network != NULL) {
1087                 name = connman_network_get_string(network,
1088                                                 CONNMAN_PROPERTY_ID_NAME);
1089                 g_free(device->last_network);
1090                 device->last_network = g_strdup(name);
1091
1092                 device->network = connman_network_ref(network);
1093         } else {
1094                 g_free(device->last_network);
1095                 device->last_network = NULL;
1096
1097                 device->network = NULL;
1098         }
1099 }
1100
1101 void __connman_device_set_reconnect(struct connman_device *device,
1102                                                 connman_bool_t reconnect)
1103 {
1104         device->reconnect = reconnect;
1105 }
1106
1107 connman_bool_t  __connman_device_get_reconnect(
1108                                 struct connman_device *device)
1109 {
1110         return device->reconnect;
1111 }
1112
1113 /**
1114  * connman_device_register:
1115  * @device: device structure
1116  *
1117  * Register device with the system
1118  */
1119 int connman_device_register(struct connman_device *device)
1120 {
1121         __connman_storage_load_device(device);
1122
1123         device->offlinemode = __connman_profile_get_offlinemode();
1124
1125         return connman_element_register(&device->element, NULL);
1126 }
1127
1128 /**
1129  * connman_device_unregister:
1130  * @device: device structure
1131  *
1132  * Unregister device with the system
1133  */
1134 void connman_device_unregister(struct connman_device *device)
1135 {
1136         __connman_storage_save_device(device);
1137
1138         connman_element_unregister(&device->element);
1139 }
1140
1141 /**
1142  * connman_device_get_data:
1143  * @device: device structure
1144  *
1145  * Get private device data pointer
1146  */
1147 void *connman_device_get_data(struct connman_device *device)
1148 {
1149         return device->driver_data;
1150 }
1151
1152 /**
1153  * connman_device_set_data:
1154  * @device: device structure
1155  * @data: data pointer
1156  *
1157  * Set private device data pointer
1158  */
1159 void connman_device_set_data(struct connman_device *device, void *data)
1160 {
1161         device->driver_data = data;
1162 }
1163
1164 static gboolean match_driver(struct connman_device *device,
1165                                         struct connman_device_driver *driver)
1166 {
1167         if (device->type == driver->type ||
1168                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1169                 return TRUE;
1170
1171         return FALSE;
1172 }
1173
1174 static int device_probe(struct connman_element *element)
1175 {
1176         struct connman_device *device = element->device;
1177         GSList *list;
1178
1179         DBG("element %p name %s", element, element->name);
1180
1181         if (device == NULL)
1182                 return -ENODEV;
1183
1184         if (device->driver != NULL)
1185                 return -EALREADY;
1186
1187         for (list = driver_list; list; list = list->next) {
1188                 struct connman_device_driver *driver = list->data;
1189
1190                 if (match_driver(device, driver) == FALSE)
1191                         continue;
1192
1193                 DBG("driver %p name %s", driver, driver->name);
1194
1195                 if (driver->probe(device) == 0) {
1196                         device->driver = driver;
1197                         break;
1198                 }
1199         }
1200
1201         if (device->driver == NULL)
1202                 return -ENODEV;
1203
1204         return setup_device(device);
1205 }
1206
1207 static void device_remove(struct connman_element *element)
1208 {
1209         struct connman_device *device = element->device;
1210
1211         DBG("element %p name %s", element, element->name);
1212
1213         if (device == NULL)
1214                 return;
1215
1216         if (device->driver == NULL)
1217                 return;
1218
1219         remove_device(device);
1220 }
1221
1222 static struct connman_driver device_driver = {
1223         .name           = "device",
1224         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1225         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1226         .probe          = device_probe,
1227         .remove         = device_remove,
1228 };
1229
1230 static int device_load(struct connman_device *device)
1231 {
1232         const char *ident = __connman_profile_active_ident();
1233         GKeyFile *keyfile;
1234         GError *error = NULL;
1235         gchar *identifier;
1236         connman_bool_t powered;
1237         int val;
1238
1239         DBG("device %p", device);
1240
1241         keyfile = __connman_storage_open_profile(ident);
1242         if (keyfile == NULL)
1243                 return 0;
1244
1245         identifier = g_strdup_printf("device_%s", device->element.name);
1246         if (identifier == NULL)
1247                 goto done;
1248
1249         powered = g_key_file_get_boolean(keyfile, identifier,
1250                                                 "Powered", &error);
1251         if (error == NULL)
1252                 device->powered_persistent = powered;
1253         g_clear_error(&error);
1254
1255         val = g_key_file_get_integer(keyfile, identifier,
1256                                                 "ScanInterval", &error);
1257         if (error == NULL)
1258                 device->scan_interval = val;
1259         g_clear_error(&error);
1260
1261 done:
1262         g_free(identifier);
1263
1264         __connman_storage_close_profile(ident, keyfile, FALSE);
1265
1266         return 0;
1267 }
1268
1269 static int device_save(struct connman_device *device)
1270 {
1271         const char *ident = __connman_profile_active_ident();
1272         GKeyFile *keyfile;
1273         gchar *identifier;
1274
1275         DBG("device %p", device);
1276
1277         keyfile = __connman_storage_open_profile(ident);
1278         if (keyfile == NULL)
1279                 return 0;
1280
1281         identifier = g_strdup_printf("device_%s", device->element.name);
1282         if (identifier == NULL)
1283                 goto done;
1284
1285         g_key_file_set_boolean(keyfile, identifier,
1286                                         "Powered", device->powered_persistent);
1287
1288         g_key_file_set_integer(keyfile, identifier,
1289                                         "ScanInterval", device->scan_interval);
1290
1291 done:
1292         g_free(identifier);
1293
1294         __connman_storage_close_profile(ident, keyfile, TRUE);
1295
1296         return 0;
1297 }
1298
1299 static struct connman_storage device_storage = {
1300         .name           = "device",
1301         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1302         .device_load    = device_load,
1303         .device_save    = device_save,
1304 };
1305
1306 int __connman_device_init(void)
1307 {
1308         DBG("");
1309
1310         if (connman_storage_register(&device_storage) < 0)
1311                 connman_error("Failed to register device storage");
1312
1313         return connman_driver_register(&device_driver);
1314 }
1315
1316 void __connman_device_cleanup(void)
1317 {
1318         DBG("");
1319
1320         connman_driver_unregister(&device_driver);
1321
1322         connman_storage_unregister(&device_storage);
1323 }