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