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