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