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