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