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