device: No need to save the scan interval
[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
466         DBG("node %s type %d", node, type);
467
468         device = g_try_new0(struct connman_device, 1);
469         if (device == NULL)
470                 return NULL;
471
472         DBG("device %p", device);
473
474         __connman_element_initialize(&device->element);
475
476         device->element.name = g_strdup(node);
477         device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
478
479         device->element.device = device;
480         device->element.destruct = device_destruct;
481
482         str = type2string(type);
483         if (str != NULL)
484                 connman_element_set_string(&device->element,
485                                         CONNMAN_PROPERTY_ID_TYPE, str);
486
487         device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
488
489         device->type = type;
490         device->name = g_strdup(type2description(device->type));
491
492         device->powered_persistent = TRUE;
493
494         device->phyindex = -1;
495
496         service_type = __connman_device_get_service_type(device);
497         device->blocked = __connman_technology_get_blocked(service_type);
498         device->backoff_interval = SCAN_INITIAL_DELAY;
499
500         switch (type) {
501         case CONNMAN_DEVICE_TYPE_UNKNOWN:
502         case CONNMAN_DEVICE_TYPE_ETHERNET:
503         case CONNMAN_DEVICE_TYPE_WIMAX:
504         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
505         case CONNMAN_DEVICE_TYPE_CELLULAR:
506         case CONNMAN_DEVICE_TYPE_GPS:
507         case CONNMAN_DEVICE_TYPE_GADGET:
508         case CONNMAN_DEVICE_TYPE_VENDOR:
509                 device->scan_interval = 0;
510                 break;
511         case CONNMAN_DEVICE_TYPE_WIFI:
512                 device->scan_interval = 300;
513                 break;
514         }
515
516         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
517                                                 g_free, unregister_network);
518
519         return device;
520 }
521
522 /**
523  * connman_device_ref:
524  * @device: device structure
525  *
526  * Increase reference counter of device
527  */
528 struct connman_device *connman_device_ref(struct connman_device *device)
529 {
530         if (connman_element_ref(&device->element) == NULL)
531                 return NULL;
532
533         return device;
534 }
535
536 /**
537  * connman_device_unref:
538  * @device: device structure
539  *
540  * Decrease reference counter of device
541  */
542 void connman_device_unref(struct connman_device *device)
543 {
544         connman_element_unref(&device->element);
545 }
546
547 const char *__connman_device_get_type(struct connman_device *device)
548 {
549         return type2string(device->type);
550 }
551
552 /**
553  * connman_device_get_type:
554  * @device: device structure
555  *
556  * Get type of device
557  */
558 enum connman_device_type connman_device_get_type(struct connman_device *device)
559 {
560         return device->type;
561 }
562
563 /**
564  * connman_device_set_index:
565  * @device: device structure
566  * @index: index number
567  *
568  * Set index number of device
569  */
570 void connman_device_set_index(struct connman_device *device, int index)
571 {
572         device->element.index = index;
573 }
574
575 /**
576  * connman_device_get_index:
577  * @device: device structure
578  *
579  * Get index number of device
580  */
581 int connman_device_get_index(struct connman_device *device)
582 {
583         return device->element.index;
584 }
585
586 int __connman_device_get_phyindex(struct connman_device *device)
587 {
588         return device->phyindex;
589 }
590
591 void __connman_device_set_phyindex(struct connman_device *device,
592                                                         int phyindex)
593 {
594         device->phyindex = phyindex;
595 }
596
597 /**
598  * connman_device_set_interface:
599  * @device: device structure
600  * @interface: interface name
601  *
602  * Set interface name of device
603  */
604 void connman_device_set_interface(struct connman_device *device,
605                                                 const char *interface)
606 {
607         g_free(device->element.devname);
608         device->element.devname = g_strdup(interface);
609
610         g_free(device->interface);
611         device->interface = g_strdup(interface);
612
613         if (device->name == NULL) {
614                 const char *str = type2description(device->type);
615                 if (str != NULL && device->interface != NULL)
616                         device->name = g_strdup_printf("%s (%s)", str,
617                                                         device->interface);
618         }
619 }
620
621 /**
622  * connman_device_set_ident:
623  * @device: device structure
624  * @ident: unique identifier
625  *
626  * Set unique identifier of device
627  */
628 void connman_device_set_ident(struct connman_device *device,
629                                                         const char *ident)
630 {
631         g_free(device->ident);
632         device->ident = g_strdup(ident);
633 }
634
635 const char *connman_device_get_ident(struct connman_device *device)
636 {
637         return device->ident;
638 }
639
640 /**
641  * connman_device_set_powered:
642  * @device: device structure
643  * @powered: powered state
644  *
645  * Change power state of device
646  */
647 int connman_device_set_powered(struct connman_device *device,
648                                                 connman_bool_t powered)
649 {
650         int err;
651         enum connman_service_type type;
652
653         DBG("driver %p powered %d", device, powered);
654
655         if (device->powered == powered) {
656                 device->powered_pending = powered;
657                 return -EALREADY;
658         }
659
660         if (powered == TRUE)
661                 err = __connman_device_enable(device);
662         else
663                 err = __connman_device_disable(device);
664
665         if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
666                 return err;
667
668         device->powered = powered;
669         device->powered_pending = powered;
670
671         type = __connman_device_get_service_type(device);
672
673         if (device->powered == TRUE)
674                 __connman_technology_enable(type);
675         else
676                 __connman_technology_disable(type);
677
678         if (device->offlinemode == TRUE && powered == TRUE)
679                 return connman_device_set_powered(device, FALSE);
680
681         if (powered == FALSE)
682                 return 0;
683
684         reset_scan_trigger(device);
685
686         if (device->driver && device->driver->scan)
687                 device->driver->scan(device);
688
689         return 0;
690 }
691
692 int __connman_device_set_blocked(struct connman_device *device,
693                                                 connman_bool_t blocked)
694 {
695         connman_bool_t powered;
696
697         DBG("device %p blocked %d", device, blocked);
698
699         device->blocked = blocked;
700
701         if (device->offlinemode == TRUE)
702                 return 0;
703
704         connman_info("%s {rfkill} blocked %d", device->interface, blocked);
705
706         if (blocked == FALSE)
707                 powered = device->powered_persistent;
708         else
709                 powered = FALSE;
710
711         return set_powered(device, powered);
712 }
713
714 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
715 {
716         return device->blocked;
717 }
718
719 int __connman_device_scan(struct connman_device *device)
720 {
721         if (!device->driver || !device->driver->scan)
722                 return -EOPNOTSUPP;
723
724         if (device->powered == FALSE)
725                 return -ENOLINK;
726
727         reset_scan_trigger(device);
728
729         return device->driver->scan(device);
730 }
731
732 int __connman_device_enable_persistent(struct connman_device *device)
733 {
734         int err;
735
736         DBG("device %p", device);
737
738         device->powered_persistent = TRUE;
739
740         __connman_storage_save_device(device);
741
742         err = __connman_device_enable(device);
743         if (err == 0 || err == -EINPROGRESS) {
744                 device->offlinemode = FALSE;
745                 if (__connman_profile_get_offlinemode() == TRUE) {
746                         __connman_profile_set_offlinemode(FALSE, FALSE);
747
748                         __connman_profile_save_default();
749                 }
750         }
751
752         return err;
753 }
754
755 int __connman_device_disable_persistent(struct connman_device *device)
756 {
757         DBG("device %p", device);
758
759         device->powered_persistent = FALSE;
760
761         __connman_storage_save_device(device);
762
763         return __connman_device_disable(device);
764 }
765
766 int __connman_device_disconnect(struct connman_device *device)
767 {
768         GHashTableIter iter;
769         gpointer key, value;
770
771         DBG("device %p", device);
772
773         connman_device_set_disconnected(device, TRUE);
774
775         g_hash_table_iter_init(&iter, device->networks);
776
777         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
778                 struct connman_network *network = value;
779
780                 if (connman_network_get_connecting(network) == TRUE) {
781                         /*
782                          * Skip network in the process of connecting.
783                          * This is a workaround for WiFi networks serviced
784                          * by the supplicant plugin that hold a reference
785                          * to the network.  If we disconnect the network
786                          * here then the referenced object will not be
787                          * registered and usage (like launching DHCP client)
788                          * will fail.  There is nothing to be gained by
789                          * removing the network here anyway.
790                          */
791                         connman_warn("Skipping disconnect of %s",
792                                 connman_network_get_identifier(network));
793                         continue;
794                 }
795
796                 __connman_network_disconnect(network);
797         }
798
799         return 0;
800 }
801
802 static void mark_network_available(gpointer key, gpointer value,
803                                                         gpointer user_data)
804 {
805         struct connman_network *network = value;
806
807         connman_network_set_available(network, TRUE);
808 }
809
810 static void mark_network_unavailable(gpointer key, gpointer value,
811                                                         gpointer user_data)
812 {
813         struct connman_network *network = value;
814
815         if (connman_network_get_connected(network) == TRUE)
816                 return;
817
818         connman_network_set_available(network, FALSE);
819 }
820
821 static gboolean remove_unavailable_network(gpointer key, gpointer value,
822                                                         gpointer user_data)
823 {
824         struct connman_network *network = value;
825
826         if (connman_network_get_connected(network) == TRUE)
827                 return FALSE;
828
829         if (connman_network_get_available(network) == TRUE)
830                 return FALSE;
831
832         return TRUE;
833 }
834
835 void __connman_device_cleanup_networks(struct connman_device *device)
836 {
837         g_hash_table_foreach_remove(device->networks,
838                                         remove_unavailable_network, NULL);
839 }
840
841 connman_bool_t __connman_device_scanning(struct connman_device *device)
842 {
843         return device->scanning;
844 }
845
846 void connman_device_reset_scanning(struct connman_device *device)
847 {
848         device->scanning = FALSE;
849
850         g_hash_table_foreach(device->networks,
851                                 mark_network_available, NULL);
852
853 }
854
855 /**
856  * connman_device_set_scanning:
857  * @device: device structure
858  * @scanning: scanning state
859  *
860  * Change scanning state of device
861  */
862 int connman_device_set_scanning(struct connman_device *device,
863                                                 connman_bool_t scanning)
864 {
865         DBG("device %p scanning %d", device, scanning);
866
867         if (!device->driver || !device->driver->scan)
868                 return -EINVAL;
869
870         if (device->scanning == scanning)
871                 return -EALREADY;
872
873         device->scanning = scanning;
874
875         if (scanning == TRUE) {
876                 reset_scan_trigger(device);
877
878                 g_hash_table_foreach(device->networks,
879                                         mark_network_unavailable, NULL);
880
881                 return 0;
882         }
883
884         __connman_device_cleanup_networks(device);
885
886         if (device->connections > 0)
887                 return 0;
888
889         __connman_service_auto_connect();
890
891         return 0;
892 }
893
894 /**
895  * connman_device_set_disconnected:
896  * @device: device structure
897  * @disconnected: disconnected state
898  *
899  * Change disconnected state of device (only for device with networks)
900  */
901 int connman_device_set_disconnected(struct connman_device *device,
902                                                 connman_bool_t disconnected)
903 {
904         DBG("device %p disconnected %d", device, disconnected);
905
906         if (device->disconnected == disconnected)
907                 return -EALREADY;
908
909         device->disconnected = disconnected;
910
911         if (disconnected == TRUE)
912                 force_scan_trigger(device);
913
914         return 0;
915 }
916
917 /**
918  * connman_device_get_disconnected:
919  * @device: device structure
920  *
921  * Get device disconnected state
922  */
923 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
924 {
925         return device->disconnected;
926 }
927
928 /**
929  * connman_device_set_string:
930  * @device: device structure
931  * @key: unique identifier
932  * @value: string value
933  *
934  * Set string value for specific key
935  */
936 int connman_device_set_string(struct connman_device *device,
937                                         const char *key, const char *value)
938 {
939         DBG("device %p key %s value %s", device, key, value);
940
941         if (g_str_equal(key, "Address") == TRUE) {
942                 g_free(device->address);
943                 device->address = g_strdup(value);
944         } else if (g_str_equal(key, "Name") == TRUE) {
945                 g_free(device->name);
946                 device->name = g_strdup(value);
947         } else if (g_str_equal(key, "Node") == TRUE) {
948                 g_free(device->node);
949                 device->node = g_strdup(value);
950         }
951
952         return connman_element_set_string(&device->element, key, value);
953 }
954
955 /**
956  * connman_device_get_string:
957  * @device: device structure
958  * @key: unique identifier
959  *
960  * Get string value for specific key
961  */
962 const char *connman_device_get_string(struct connman_device *device,
963                                                         const char *key)
964 {
965         DBG("device %p key %s", device, key);
966
967         if (g_str_equal(key, "Address") == TRUE)
968                 return device->address;
969         else if (g_str_equal(key, "Name") == TRUE)
970                 return device->name;
971         else if (g_str_equal(key, "Node") == TRUE)
972                 return device->node;
973         else if (g_str_equal(key, "Interface") == TRUE)
974                 return device->interface;
975
976         return connman_element_get_string(&device->element, key);
977 }
978
979 static void set_offlinemode(struct connman_element *element, gpointer user_data)
980 {
981         struct connman_device *device = element->device;
982         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
983         connman_bool_t powered;
984
985         DBG("element %p name %s", element, element->name);
986
987         if (device == NULL)
988                 return;
989
990         device->offlinemode = offlinemode;
991
992         if (device->blocked == TRUE)
993                 return;
994
995         powered = (offlinemode == TRUE) ? FALSE : TRUE;
996
997         if (device->powered == powered)
998                 return;
999
1000         if (device->powered_persistent == FALSE)
1001                 powered = FALSE;
1002
1003         set_powered(device, powered);
1004 }
1005
1006 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1007 {
1008         DBG("offlinmode %d", offlinemode);
1009
1010         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1011                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
1012
1013         __connman_notifier_offlinemode(offlinemode);
1014
1015         return 0;
1016 }
1017
1018 void __connman_device_increase_connections(struct connman_device *device)
1019 {
1020         if (device == NULL)
1021                 return;
1022
1023         device->connections++;
1024 }
1025
1026 void __connman_device_decrease_connections(struct connman_device *device)
1027 {
1028         if (device == NULL)
1029                 return;
1030
1031         device->connections--;
1032
1033         if (device->connections == 0)
1034                 device->backoff_interval = SCAN_INITIAL_DELAY;
1035 }
1036
1037 /**
1038  * connman_device_add_network:
1039  * @device: device structure
1040  * @network: network structure
1041  *
1042  * Add new network to the device
1043  */
1044 int connman_device_add_network(struct connman_device *device,
1045                                         struct connman_network *network)
1046 {
1047         const char *identifier = connman_network_get_identifier(network);
1048         int err;
1049
1050         DBG("device %p network %p", device, network);
1051
1052         if (identifier == NULL)
1053                 return -EINVAL;
1054
1055         __connman_network_set_device(network, device);
1056
1057         err = connman_element_register((struct connman_element *) network,
1058                                                         &device->element);
1059         if (err < 0) {
1060                 __connman_network_set_device(network, NULL);
1061                 return err;
1062         }
1063
1064         g_hash_table_insert(device->networks, g_strdup(identifier),
1065                                                                 network);
1066
1067         return 0;
1068 }
1069
1070 /**
1071  * connman_device_get_network:
1072  * @device: device structure
1073  * @identifier: network identifier
1074  *
1075  * Get network for given identifier
1076  */
1077 struct connman_network *connman_device_get_network(struct connman_device *device,
1078                                                         const char *identifier)
1079 {
1080         DBG("device %p identifier %s", device, identifier);
1081
1082         return g_hash_table_lookup(device->networks, identifier);
1083 }
1084
1085 /**
1086  * connman_device_remove_network:
1087  * @device: device structure
1088  * @identifier: network identifier
1089  *
1090  * Remove network for given identifier
1091  */
1092 int connman_device_remove_network(struct connman_device *device,
1093                                                         const char *identifier)
1094 {
1095         DBG("device %p identifier %s", device, identifier);
1096
1097         g_hash_table_remove(device->networks, identifier);
1098
1099         return 0;
1100 }
1101
1102 void connman_device_remove_all_networks(struct connman_device *device)
1103 {
1104         g_hash_table_remove_all(device->networks);
1105 }
1106
1107 void __connman_device_set_network(struct connman_device *device,
1108                                         struct connman_network *network)
1109 {
1110         const char *name;
1111
1112         if (device == NULL)
1113                 return;
1114
1115         if (device->network == network)
1116                 return;
1117
1118         if (device->network != NULL)
1119                 connman_network_unref(device->network);
1120
1121         if (network != NULL) {
1122                 name = connman_network_get_string(network,
1123                                                 CONNMAN_PROPERTY_ID_NAME);
1124                 g_free(device->last_network);
1125                 device->last_network = g_strdup(name);
1126
1127                 device->network = connman_network_ref(network);
1128         } else {
1129                 g_free(device->last_network);
1130                 device->last_network = NULL;
1131
1132                 device->network = NULL;
1133         }
1134 }
1135
1136 void __connman_device_set_reconnect(struct connman_device *device,
1137                                                 connman_bool_t reconnect)
1138 {
1139         device->reconnect = reconnect;
1140 }
1141
1142 connman_bool_t  __connman_device_get_reconnect(
1143                                 struct connman_device *device)
1144 {
1145         return device->reconnect;
1146 }
1147
1148 /**
1149  * connman_device_register:
1150  * @device: device structure
1151  *
1152  * Register device with the system
1153  */
1154 int connman_device_register(struct connman_device *device)
1155 {
1156         __connman_storage_load_device(device);
1157
1158         device->offlinemode = __connman_profile_get_offlinemode();
1159
1160         return connman_element_register(&device->element, NULL);
1161 }
1162
1163 /**
1164  * connman_device_unregister:
1165  * @device: device structure
1166  *
1167  * Unregister device with the system
1168  */
1169 void connman_device_unregister(struct connman_device *device)
1170 {
1171         __connman_storage_save_device(device);
1172
1173         connman_element_unregister(&device->element);
1174 }
1175
1176 /**
1177  * connman_device_get_data:
1178  * @device: device structure
1179  *
1180  * Get private device data pointer
1181  */
1182 void *connman_device_get_data(struct connman_device *device)
1183 {
1184         return device->driver_data;
1185 }
1186
1187 /**
1188  * connman_device_set_data:
1189  * @device: device structure
1190  * @data: data pointer
1191  *
1192  * Set private device data pointer
1193  */
1194 void connman_device_set_data(struct connman_device *device, void *data)
1195 {
1196         device->driver_data = data;
1197 }
1198
1199 static gboolean match_driver(struct connman_device *device,
1200                                         struct connman_device_driver *driver)
1201 {
1202         if (device->type == driver->type ||
1203                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1204                 return TRUE;
1205
1206         return FALSE;
1207 }
1208
1209 static int device_probe(struct connman_element *element)
1210 {
1211         struct connman_device *device = element->device;
1212         GSList *list;
1213
1214         DBG("element %p name %s", element, element->name);
1215
1216         if (device == NULL)
1217                 return -ENODEV;
1218
1219         if (device->driver != NULL)
1220                 return -EALREADY;
1221
1222         for (list = driver_list; list; list = list->next) {
1223                 struct connman_device_driver *driver = list->data;
1224
1225                 if (match_driver(device, driver) == FALSE)
1226                         continue;
1227
1228                 DBG("driver %p name %s", driver, driver->name);
1229
1230                 if (driver->probe(device) == 0) {
1231                         device->driver = driver;
1232                         break;
1233                 }
1234         }
1235
1236         if (device->driver == NULL)
1237                 return -ENODEV;
1238
1239         return setup_device(device);
1240 }
1241
1242 static void device_remove(struct connman_element *element)
1243 {
1244         struct connman_device *device = element->device;
1245
1246         DBG("element %p name %s", element, element->name);
1247
1248         if (device == NULL)
1249                 return;
1250
1251         if (device->driver == NULL)
1252                 return;
1253
1254         remove_device(device);
1255 }
1256
1257 static struct connman_driver device_driver = {
1258         .name           = "device",
1259         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1260         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1261         .probe          = device_probe,
1262         .remove         = device_remove,
1263 };
1264
1265 static int device_load(struct connman_device *device)
1266 {
1267         const char *ident = __connman_profile_active_ident();
1268         GKeyFile *keyfile;
1269         GError *error = NULL;
1270         gchar *identifier;
1271         connman_bool_t powered;
1272
1273         DBG("device %p", device);
1274
1275         keyfile = __connman_storage_open_profile(ident);
1276         if (keyfile == NULL)
1277                 return 0;
1278
1279         identifier = g_strdup_printf("device_%s", device->element.name);
1280         if (identifier == NULL)
1281                 goto done;
1282
1283         powered = g_key_file_get_boolean(keyfile, identifier,
1284                                                 "Powered", &error);
1285         if (error == NULL)
1286                 device->powered_persistent = powered;
1287         g_clear_error(&error);
1288
1289 done:
1290         g_free(identifier);
1291
1292         __connman_storage_close_profile(ident, keyfile, FALSE);
1293
1294         return 0;
1295 }
1296
1297 static int device_save(struct connman_device *device)
1298 {
1299         const char *ident = __connman_profile_active_ident();
1300         GKeyFile *keyfile;
1301         gchar *identifier;
1302
1303         DBG("device %p", device);
1304
1305         keyfile = __connman_storage_open_profile(ident);
1306         if (keyfile == NULL)
1307                 return 0;
1308
1309         identifier = g_strdup_printf("device_%s", device->element.name);
1310         if (identifier == NULL)
1311                 goto done;
1312
1313         g_key_file_set_boolean(keyfile, identifier,
1314                                         "Powered", device->powered_persistent);
1315
1316 done:
1317         g_free(identifier);
1318
1319         __connman_storage_close_profile(ident, keyfile, TRUE);
1320
1321         return 0;
1322 }
1323
1324 static struct connman_storage device_storage = {
1325         .name           = "device",
1326         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1327         .device_load    = device_load,
1328         .device_save    = device_save,
1329 };
1330
1331 int __connman_device_init(void)
1332 {
1333         DBG("");
1334
1335         if (connman_storage_register(&device_storage) < 0)
1336                 connman_error("Failed to register device storage");
1337
1338         return connman_driver_register(&device_driver);
1339 }
1340
1341 void __connman_device_cleanup(void)
1342 {
1343         DBG("");
1344
1345         connman_driver_unregister(&device_driver);
1346
1347         connman_storage_unregister(&device_storage);
1348 }