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