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