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