device: Remove device persistent code
[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 enum connman_pending_type {
36         PENDING_NONE    = 0,
37         PENDING_ENABLE  = 1,
38         PENDING_DISABLE = 2,
39 };
40
41 struct connman_device {
42         gint refcount;
43         enum connman_device_type type;
44         enum connman_pending_type powered_pending;      /* Indicates a pending
45                                                         enable/disable request */
46         connman_bool_t powered;
47         connman_bool_t scanning;
48         connman_bool_t disconnected;
49         connman_bool_t reconnect;
50         connman_uint16_t scan_interval;
51         connman_uint16_t backoff_interval;
52         char *name;
53         char *node;
54         char *address;
55         char *interface;
56         char *ident;
57         char *path;
58         char *devname;
59         int phyindex;
60         int index;
61         unsigned int connections;
62         guint scan_timeout;
63         guint pending_timeout;
64
65         struct connman_device_driver *driver;
66         void *driver_data;
67
68         char *last_network;
69         struct connman_network *network;
70         GHashTable *networks;
71 };
72
73 #define SCAN_INITIAL_DELAY 10
74
75 static gboolean device_scan_trigger(gpointer user_data)
76 {
77         struct connman_device *device = user_data;
78
79         DBG("device %p", device);
80
81         if (device->driver == NULL) {
82                 device->scan_timeout = 0;
83                 return FALSE;
84         }
85
86         if (device->driver->scan)
87                 device->driver->scan(device);
88
89         return TRUE;
90 }
91
92 static void clear_scan_trigger(struct connman_device *device)
93 {
94         if (device->scan_timeout > 0) {
95                 g_source_remove(device->scan_timeout);
96                 device->scan_timeout = 0;
97         }
98 }
99
100 static void reset_scan_trigger(struct connman_device *device)
101 {
102         clear_scan_trigger(device);
103
104         if (device->scan_interval > 0) {
105                 guint interval;
106
107                 if (g_hash_table_size(device->networks) == 0) {
108                         if (device->backoff_interval >= device->scan_interval)
109                                 device->backoff_interval = SCAN_INITIAL_DELAY;
110                         interval = device->backoff_interval;
111                 } else
112                         interval = device->scan_interval;
113
114                 DBG("interval %d", interval);
115
116                 device->scan_timeout = g_timeout_add_seconds(interval,
117                                         device_scan_trigger, device);
118
119                 device->backoff_interval *= 2;
120                 if (device->backoff_interval > device->scan_interval)
121                         device->backoff_interval = device->scan_interval;
122         }
123 }
124
125 static void force_scan_trigger(struct connman_device *device)
126 {
127         clear_scan_trigger(device);
128
129         device->scan_timeout = g_timeout_add_seconds(5,
130                                         device_scan_trigger, device);
131 }
132
133 void connman_device_schedule_scan(struct connman_device *device)
134 {
135         reset_scan_trigger(device);
136 }
137
138 static const char *type2description(enum connman_device_type type)
139 {
140         switch (type) {
141         case CONNMAN_DEVICE_TYPE_UNKNOWN:
142         case CONNMAN_DEVICE_TYPE_VENDOR:
143                 break;
144         case CONNMAN_DEVICE_TYPE_ETHERNET:
145                 return "Ethernet";
146         case CONNMAN_DEVICE_TYPE_WIFI:
147                 return "Wireless";
148         case CONNMAN_DEVICE_TYPE_WIMAX:
149                 return "WiMAX";
150         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
151                 return "Bluetooth";
152         case CONNMAN_DEVICE_TYPE_GPS:
153                 return "GPS";
154         case CONNMAN_DEVICE_TYPE_CELLULAR:
155                 return "Cellular";
156         case CONNMAN_DEVICE_TYPE_GADGET:
157                 return "Gadget";
158
159         }
160
161         return NULL;
162 }
163
164 static const char *type2string(enum connman_device_type type)
165 {
166         switch (type) {
167         case CONNMAN_DEVICE_TYPE_UNKNOWN:
168         case CONNMAN_DEVICE_TYPE_VENDOR:
169                 break;
170         case CONNMAN_DEVICE_TYPE_ETHERNET:
171                 return "ethernet";
172         case CONNMAN_DEVICE_TYPE_WIFI:
173                 return "wifi";
174         case CONNMAN_DEVICE_TYPE_WIMAX:
175                 return "wimax";
176         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
177                 return "bluetooth";
178         case CONNMAN_DEVICE_TYPE_GPS:
179                 return "gps";
180         case CONNMAN_DEVICE_TYPE_CELLULAR:
181                 return "cellular";
182         case CONNMAN_DEVICE_TYPE_GADGET:
183                 return "gadget";
184
185         }
186
187         return NULL;
188 }
189
190 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
191 {
192         enum connman_device_type type = connman_device_get_type(device);
193
194         switch (type) {
195         case CONNMAN_DEVICE_TYPE_UNKNOWN:
196         case CONNMAN_DEVICE_TYPE_VENDOR:
197         case CONNMAN_DEVICE_TYPE_GPS:
198                 break;
199         case CONNMAN_DEVICE_TYPE_ETHERNET:
200                 return CONNMAN_SERVICE_TYPE_ETHERNET;
201         case CONNMAN_DEVICE_TYPE_WIFI:
202                 return CONNMAN_SERVICE_TYPE_WIFI;
203         case CONNMAN_DEVICE_TYPE_WIMAX:
204                 return CONNMAN_SERVICE_TYPE_WIMAX;
205         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
206                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
207         case CONNMAN_DEVICE_TYPE_CELLULAR:
208                 return CONNMAN_SERVICE_TYPE_CELLULAR;
209         case CONNMAN_DEVICE_TYPE_GADGET:
210                 return CONNMAN_SERVICE_TYPE_GADGET;
211
212         }
213
214         return CONNMAN_SERVICE_TYPE_UNKNOWN;
215 }
216
217 static gboolean device_pending_reset(gpointer user_data)
218 {
219         struct connman_device *device = user_data;
220
221         DBG("device %p", device);
222
223         /* Power request timedout, reset power pending state. */
224         if (device->pending_timeout > 0) {
225                 g_source_remove(device->pending_timeout);
226                 device->pending_timeout = 0;
227                 device->powered_pending = PENDING_NONE;
228         }
229
230         return FALSE;
231 }
232
233 int __connman_device_enable(struct connman_device *device)
234 {
235         int err;
236
237         DBG("device %p", device);
238
239         if (!device->driver || !device->driver->enable)
240                 return -EOPNOTSUPP;
241
242         /* There is an ongoing power disable request. */
243         if (device->powered_pending == PENDING_DISABLE)
244                 return -EBUSY;
245
246         if (device->powered_pending == PENDING_ENABLE)
247                 return -EALREADY;
248
249         if (device->powered_pending == PENDING_NONE && device->powered == TRUE)
250                 return -EALREADY;
251
252         device->powered_pending = PENDING_ENABLE;
253
254         err = device->driver->enable(device);
255         /*
256          * device gets enabled right away.
257          * Invoke the callback
258          */
259         if (err == 0) {
260                 connman_device_set_powered(device, TRUE);
261                 goto done;
262         }
263
264         if (err == -EALREADY) {
265                 /* If device is already powered, but connman is not updated */
266                 connman_device_set_powered(device, TRUE);
267                 goto done;
268         }
269         /*
270          * if err == -EINPROGRESS, then the DBus call to the respective daemon
271          * was successful. We set a 4 sec timeout so if the daemon never
272          * returns a reply, we would reset the pending request.
273          */
274         if (err == -EINPROGRESS)
275                 device->pending_timeout = g_timeout_add_seconds(4,
276                                         device_pending_reset, device);
277 done:
278         return err;
279 }
280
281 int __connman_device_disable(struct connman_device *device)
282 {
283         int err;
284
285         DBG("device %p", device);
286
287         if (!device->driver || !device->driver->disable)
288                 return -EOPNOTSUPP;
289
290         /* Ongoing power enable request */
291         if (device->powered_pending == PENDING_ENABLE)
292                 return -EBUSY;
293
294         if (device->powered_pending == PENDING_DISABLE)
295                 return -EALREADY;
296
297         if (device->powered_pending == PENDING_NONE && device->powered == FALSE)
298                 return -EALREADY;
299
300         device->reconnect = FALSE;
301
302         clear_scan_trigger(device);
303
304         err = device->driver->disable(device);
305         if (err == 0) {
306                 connman_device_set_powered(device, FALSE);
307                 goto done;
308         }
309
310         if (err == -EALREADY) {
311                 connman_device_set_powered(device, FALSE);
312                 goto done;
313         }
314
315         if (err == -EINPROGRESS)
316                 device->pending_timeout = g_timeout_add_seconds(4,
317                                         device_pending_reset, device);
318 done:
319         return err;
320 }
321
322 static void probe_driver(struct connman_device_driver *driver)
323 {
324         GSList *list;
325
326         DBG("driver %p name %s", driver, driver->name);
327
328         for (list = device_list; list != NULL; list = list->next) {
329                 struct connman_device *device = list->data;
330
331                 if (device->driver != NULL)
332                         continue;
333
334                 if (driver->type != device->type)
335                         continue;
336
337                 if (driver->probe(device) < 0)
338                         continue;
339
340                 device->driver = driver;
341
342                 __connman_technology_add_device(device);
343         }
344 }
345
346 static void remove_device(struct connman_device *device)
347 {
348         DBG("device %p", device);
349
350         __connman_device_disable(device);
351
352         __connman_technology_remove_device(device);
353
354         if (device->driver->remove)
355                 device->driver->remove(device);
356
357         device->driver = NULL;
358 }
359
360 static void remove_driver(struct connman_device_driver *driver)
361 {
362         GSList *list;
363
364         DBG("driver %p name %s", driver, driver->name);
365
366         for (list = device_list; list != NULL; list = list->next) {
367                 struct connman_device *device = list->data;
368
369                 if (device->driver == driver)
370                         remove_device(device);
371         }
372 }
373
374 connman_bool_t __connman_device_has_driver(struct connman_device *device)
375 {
376         if (device == NULL || device->driver == NULL)
377                 return FALSE;
378
379         return TRUE;
380 }
381
382 static GSList *driver_list = NULL;
383
384 static gint compare_priority(gconstpointer a, gconstpointer b)
385 {
386         const struct connman_device_driver *driver1 = a;
387         const struct connman_device_driver *driver2 = b;
388
389         return driver2->priority - driver1->priority;
390 }
391
392 /**
393  * connman_device_driver_register:
394  * @driver: device driver definition
395  *
396  * Register a new device driver
397  *
398  * Returns: %0 on success
399  */
400 int connman_device_driver_register(struct connman_device_driver *driver)
401 {
402         DBG("driver %p name %s", driver, driver->name);
403
404         driver_list = g_slist_insert_sorted(driver_list, driver,
405                                                         compare_priority);
406         probe_driver(driver);
407
408         return 0;
409 }
410
411 /**
412  * connman_device_driver_unregister:
413  * @driver: device driver definition
414  *
415  * Remove a previously registered device driver
416  */
417 void connman_device_driver_unregister(struct connman_device_driver *driver)
418 {
419         DBG("driver %p name %s", driver, driver->name);
420
421         driver_list = g_slist_remove(driver_list, driver);
422
423         remove_driver(driver);
424 }
425
426 static void free_network(gpointer data)
427 {
428         struct connman_network *network = data;
429
430         DBG("network %p", network);
431
432         __connman_network_set_device(network, NULL);
433
434         connman_network_unref(network);
435 }
436
437 static void device_destruct(struct connman_device *device)
438 {
439         DBG("device %p name %s", device, device->name);
440
441         clear_scan_trigger(device);
442
443         g_free(device->ident);
444         g_free(device->node);
445         g_free(device->name);
446         g_free(device->address);
447         g_free(device->interface);
448         g_free(device->path);
449         g_free(device->devname);
450
451         g_free(device->last_network);
452
453         g_hash_table_destroy(device->networks);
454         device->networks = NULL;
455
456         g_free(device);
457 }
458
459 /**
460  * connman_device_create:
461  * @node: device node name (for example an address)
462  * @type: device type
463  *
464  * Allocate a new device of given #type and assign the #node name to it.
465  *
466  * Returns: a newly-allocated #connman_device structure
467  */
468 struct connman_device *connman_device_create(const char *node,
469                                                 enum connman_device_type type)
470 {
471         struct connman_device *device;
472         connman_bool_t bg_scan;
473
474         DBG("node %s type %d", node, type);
475
476         device = g_try_new0(struct connman_device, 1);
477         if (device == NULL)
478                 return NULL;
479
480         DBG("device %p", device);
481
482         device->refcount = 1;
483
484         bg_scan = connman_setting_get_bool("BackgroundScanning");
485
486         device->type = type;
487         device->name = g_strdup(type2description(device->type));
488
489         device->phyindex = -1;
490
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         enum connman_service_type type;
660
661         DBG("driver %p powered %d", device, powered);
662
663         if (device->powered == powered)
664                 return -EALREADY;
665
666         /* Reset pending request */
667         g_source_remove(device->pending_timeout);
668         device->pending_timeout = 0;
669         device->powered_pending = PENDING_NONE;
670
671         device->powered = powered;
672
673         type = __connman_device_get_service_type(device);
674
675         if (device->powered == TRUE)
676                 __connman_technology_enabled(type);
677         else
678                 __connman_technology_disabled(type);
679
680         if (powered == FALSE) {
681                 device->connections = 0;
682                 return 0;
683         }
684
685         connman_device_set_disconnected(device, FALSE);
686         device->scanning = FALSE;
687
688         reset_scan_trigger(device);
689
690         if (device->driver && device->driver->scan)
691                 device->driver->scan(device);
692
693         return 0;
694 }
695
696 static int device_scan(struct connman_device *device)
697 {
698         if (!device->driver || !device->driver->scan)
699                 return -EOPNOTSUPP;
700
701         if (device->powered == FALSE)
702                 return -ENOLINK;
703
704         reset_scan_trigger(device);
705
706         return device->driver->scan(device);
707 }
708
709 int __connman_device_disconnect(struct connman_device *device)
710 {
711         GHashTableIter iter;
712         gpointer key, value;
713
714         DBG("device %p", device);
715
716         connman_device_set_disconnected(device, TRUE);
717
718         g_hash_table_iter_init(&iter, device->networks);
719
720         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
721                 struct connman_network *network = value;
722
723                 if (connman_network_get_connecting(network) == TRUE) {
724                         /*
725                          * Skip network in the process of connecting.
726                          * This is a workaround for WiFi networks serviced
727                          * by the supplicant plugin that hold a reference
728                          * to the network.  If we disconnect the network
729                          * here then the referenced object will not be
730                          * registered and usage (like launching DHCP client)
731                          * will fail.  There is nothing to be gained by
732                          * removing the network here anyway.
733                          */
734                         connman_warn("Skipping disconnect of %s, network is connecting.",
735                                 connman_network_get_identifier(network));
736                         continue;
737                 }
738
739                 __connman_network_disconnect(network);
740         }
741
742         return 0;
743 }
744
745 static void mark_network_available(gpointer key, gpointer value,
746                                                         gpointer user_data)
747 {
748         struct connman_network *network = value;
749
750         connman_network_set_available(network, TRUE);
751 }
752
753 static void mark_network_unavailable(gpointer key, gpointer value,
754                                                         gpointer user_data)
755 {
756         struct connman_network *network = value;
757
758         if (connman_network_get_connected(network) == TRUE)
759                 return;
760
761         connman_network_set_available(network, FALSE);
762 }
763
764 static gboolean remove_unavailable_network(gpointer key, gpointer value,
765                                                         gpointer user_data)
766 {
767         struct connman_network *network = value;
768
769         if (connman_network_get_connected(network) == TRUE)
770                 return FALSE;
771
772         if (connman_network_get_available(network) == TRUE)
773                 return FALSE;
774
775         return TRUE;
776 }
777
778 void __connman_device_cleanup_networks(struct connman_device *device)
779 {
780         g_hash_table_foreach_remove(device->networks,
781                                         remove_unavailable_network, NULL);
782 }
783
784 connman_bool_t __connman_device_scanning(struct connman_device *device)
785 {
786         return device->scanning;
787 }
788
789 void connman_device_reset_scanning(struct connman_device *device)
790 {
791         device->scanning = FALSE;
792
793         g_hash_table_foreach(device->networks,
794                                 mark_network_available, NULL);
795
796 }
797
798 /**
799  * connman_device_set_scanning:
800  * @device: device structure
801  * @scanning: scanning state
802  *
803  * Change scanning state of device
804  */
805 int connman_device_set_scanning(struct connman_device *device,
806                                                 connman_bool_t scanning)
807 {
808         DBG("device %p scanning %d", device, scanning);
809
810         if (!device->driver || !device->driver->scan)
811                 return -EINVAL;
812
813         if (device->scanning == scanning)
814                 return -EALREADY;
815
816         device->scanning = scanning;
817
818         if (scanning == TRUE) {
819                 reset_scan_trigger(device);
820
821                 g_hash_table_foreach(device->networks,
822                                         mark_network_unavailable, NULL);
823
824                 return 0;
825         }
826
827         __connman_device_cleanup_networks(device);
828
829         if (device->connections > 0)
830                 return 0;
831
832         __connman_service_auto_connect();
833
834         return 0;
835 }
836
837 /**
838  * connman_device_set_disconnected:
839  * @device: device structure
840  * @disconnected: disconnected state
841  *
842  * Change disconnected state of device (only for device with networks)
843  */
844 int connman_device_set_disconnected(struct connman_device *device,
845                                                 connman_bool_t disconnected)
846 {
847         DBG("device %p disconnected %d", device, disconnected);
848
849         if (device->disconnected == disconnected)
850                 return -EALREADY;
851
852         device->disconnected = disconnected;
853
854         if (disconnected == TRUE)
855                 force_scan_trigger(device);
856
857         return 0;
858 }
859
860 /**
861  * connman_device_get_disconnected:
862  * @device: device structure
863  *
864  * Get device disconnected state
865  */
866 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
867 {
868         return device->disconnected;
869 }
870
871 /**
872  * connman_device_set_string:
873  * @device: device structure
874  * @key: unique identifier
875  * @value: string value
876  *
877  * Set string value for specific key
878  */
879 int connman_device_set_string(struct connman_device *device,
880                                         const char *key, const char *value)
881 {
882         DBG("device %p key %s value %s", device, key, value);
883
884         if (g_str_equal(key, "Address") == TRUE) {
885                 g_free(device->address);
886                 device->address = g_strdup(value);
887         } else if (g_str_equal(key, "Name") == TRUE) {
888                 g_free(device->name);
889                 device->name = g_strdup(value);
890         } else if (g_str_equal(key, "Node") == TRUE) {
891                 g_free(device->node);
892                 device->node = g_strdup(value);
893         } else if (g_str_equal(key, "Path") == TRUE) {
894                 g_free(device->path);
895                 device->path = g_strdup(value);
896         } else {
897                 return -EINVAL;
898         }
899
900         return 0;
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         else if (g_str_equal(key, "Path") == TRUE)
924                 return device->path;
925
926         return NULL;
927 }
928
929 void __connman_device_increase_connections(struct connman_device *device)
930 {
931         if (device == NULL)
932                 return;
933
934         device->connections++;
935 }
936
937 void __connman_device_decrease_connections(struct connman_device *device)
938 {
939         if (device == NULL)
940                 return;
941
942         device->connections--;
943
944         if (device->connections == 0)
945                 device->backoff_interval = SCAN_INITIAL_DELAY;
946 }
947
948 /**
949  * connman_device_add_network:
950  * @device: device structure
951  * @network: network structure
952  *
953  * Add new network to the device
954  */
955 int connman_device_add_network(struct connman_device *device,
956                                         struct connman_network *network)
957 {
958         const char *identifier = connman_network_get_identifier(network);
959
960         DBG("device %p network %p", device, network);
961
962         if (identifier == NULL)
963                 return -EINVAL;
964
965         connman_network_ref(network);
966
967         __connman_network_set_device(network, device);
968
969         g_hash_table_insert(device->networks, g_strdup(identifier),
970                                                                 network);
971
972         return 0;
973 }
974
975 /**
976  * connman_device_get_network:
977  * @device: device structure
978  * @identifier: network identifier
979  *
980  * Get network for given identifier
981  */
982 struct connman_network *connman_device_get_network(struct connman_device *device,
983                                                         const char *identifier)
984 {
985         DBG("device %p identifier %s", device, identifier);
986
987         return g_hash_table_lookup(device->networks, identifier);
988 }
989
990 /**
991  * connman_device_remove_network:
992  * @device: device structure
993  * @identifier: network identifier
994  *
995  * Remove network for given identifier
996  */
997 int connman_device_remove_network(struct connman_device *device,
998                                                 struct connman_network *network)
999 {
1000         const char *identifier;
1001
1002         DBG("device %p network %p", device, network);
1003
1004         if (network == NULL)
1005                 return 0;
1006
1007         identifier = connman_network_get_identifier(network);
1008         g_hash_table_remove(device->networks, identifier);
1009
1010         return 0;
1011 }
1012
1013 void connman_device_remove_all_networks(struct connman_device *device)
1014 {
1015         g_hash_table_remove_all(device->networks);
1016 }
1017
1018 void __connman_device_set_network(struct connman_device *device,
1019                                         struct connman_network *network)
1020 {
1021         const char *name;
1022
1023         if (device == NULL)
1024                 return;
1025
1026         if (device->network == network)
1027                 return;
1028
1029         if (network != NULL) {
1030                 name = connman_network_get_string(network, "Name");
1031                 g_free(device->last_network);
1032                 device->last_network = g_strdup(name);
1033
1034                 device->network = network;
1035         } else {
1036                 g_free(device->last_network);
1037                 device->last_network = NULL;
1038
1039                 device->network = NULL;
1040         }
1041 }
1042
1043 void __connman_device_set_reconnect(struct connman_device *device,
1044                                                 connman_bool_t reconnect)
1045 {
1046         device->reconnect = reconnect;
1047 }
1048
1049 connman_bool_t  __connman_device_get_reconnect(
1050                                 struct connman_device *device)
1051 {
1052         return device->reconnect;
1053 }
1054
1055 static gboolean match_driver(struct connman_device *device,
1056                                         struct connman_device_driver *driver)
1057 {
1058         if (device->type == driver->type ||
1059                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1060                 return TRUE;
1061
1062         return FALSE;
1063 }
1064
1065 static int device_probe(struct connman_device *device)
1066 {
1067         GSList *list;
1068
1069         DBG("device %p name %s", device, device->name);
1070
1071         if (device->driver != NULL)
1072                 return -EALREADY;
1073
1074         for (list = driver_list; list; list = list->next) {
1075                 struct connman_device_driver *driver = list->data;
1076
1077                 if (match_driver(device, driver) == FALSE)
1078                         continue;
1079
1080                 DBG("driver %p name %s", driver, driver->name);
1081
1082                 if (driver->probe(device) == 0) {
1083                         device->driver = driver;
1084                         break;
1085                 }
1086         }
1087
1088         if (device->driver == NULL)
1089                 return 0;
1090
1091         return __connman_technology_add_device(device);
1092 }
1093
1094 static void device_remove(struct connman_device *device)
1095 {
1096         DBG("device %p name %s", device, device->name);
1097
1098         if (device->driver == NULL)
1099                 return;
1100
1101         remove_device(device);
1102 }
1103
1104 /**
1105  * connman_device_register:
1106  * @device: device structure
1107  *
1108  * Register device with the system
1109  */
1110 int connman_device_register(struct connman_device *device)
1111 {
1112         return device_probe(device);
1113 }
1114
1115 /**
1116  * connman_device_unregister:
1117  * @device: device structure
1118  *
1119  * Unregister device with the system
1120  */
1121 void connman_device_unregister(struct connman_device *device)
1122 {
1123         device_remove(device);
1124 }
1125
1126 /**
1127  * connman_device_get_data:
1128  * @device: device structure
1129  *
1130  * Get private device data pointer
1131  */
1132 void *connman_device_get_data(struct connman_device *device)
1133 {
1134         return device->driver_data;
1135 }
1136
1137 /**
1138  * connman_device_set_data:
1139  * @device: device structure
1140  * @data: data pointer
1141  *
1142  * Set private device data pointer
1143  */
1144 void connman_device_set_data(struct connman_device *device, void *data)
1145 {
1146         device->driver_data = data;
1147 }
1148
1149 struct connman_device *__connman_device_find_device(
1150                                 enum connman_service_type type)
1151 {
1152         GSList *list;
1153
1154         for (list = device_list; list != NULL; list = list->next) {
1155                 struct connman_device *device = list->data;
1156                 enum connman_service_type service_type =
1157                         __connman_device_get_service_type(device);
1158
1159                 if (service_type != type)
1160                         continue;
1161
1162                 return device;
1163         }
1164
1165         return NULL;
1166 }
1167
1168 int __connman_device_request_scan(enum connman_service_type type)
1169 {
1170         GSList *list;
1171         int err;
1172
1173         switch (type) {
1174         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1175         case CONNMAN_SERVICE_TYPE_SYSTEM:
1176         case CONNMAN_SERVICE_TYPE_ETHERNET:
1177         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1178         case CONNMAN_SERVICE_TYPE_CELLULAR:
1179         case CONNMAN_SERVICE_TYPE_GPS:
1180         case CONNMAN_SERVICE_TYPE_VPN:
1181         case CONNMAN_SERVICE_TYPE_GADGET:
1182                 return 0;
1183         case CONNMAN_SERVICE_TYPE_WIFI:
1184         case CONNMAN_SERVICE_TYPE_WIMAX:
1185                 break;
1186         }
1187
1188         for (list = device_list; list != NULL; list = list->next) {
1189                 struct connman_device *device = list->data;
1190                 enum connman_service_type service_type =
1191                         __connman_device_get_service_type(device);
1192
1193                 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1194                                 service_type != type) {
1195                         continue;
1196                 }
1197
1198                 err = device_scan(device);
1199                 if (err < 0 && err != -EINPROGRESS) {
1200                         DBG("err %d", err);
1201                         /* XXX maybe only a continue? */
1202                         return err;
1203                 }
1204         }
1205
1206         return 0;
1207 }
1208
1209 connman_bool_t __connman_device_isfiltered(const char *devname)
1210 {
1211         char **pattern;
1212
1213         if (device_filter == NULL)
1214                 goto nodevice;
1215
1216         for (pattern = device_filter; *pattern; pattern++) {
1217                 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1218                         DBG("ignoring device %s (match)", devname);
1219                         return TRUE;
1220                 }
1221         }
1222
1223 nodevice:
1224         if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1225                 DBG("ignoring dummy networking devices");
1226                 return TRUE;
1227         }
1228
1229         if (nodevice_filter == NULL)
1230                 return FALSE;
1231
1232         for (pattern = nodevice_filter; *pattern; pattern++) {
1233                 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1234                         DBG("ignoring device %s (no match)", devname);
1235                         return TRUE;
1236                 }
1237         }
1238
1239         return FALSE;
1240 }
1241
1242 int __connman_device_init(const char *device, const char *nodevice)
1243 {
1244         DBG("");
1245
1246         if (device != NULL)
1247                 device_filter = g_strsplit(device, ",", -1);
1248
1249         if (nodevice != NULL)
1250                 nodevice_filter = g_strsplit(nodevice, ",", -1);
1251
1252         return 0;
1253 }
1254
1255 void __connman_device_cleanup(void)
1256 {
1257         DBG("");
1258
1259         g_strfreev(nodevice_filter);
1260         g_strfreev(device_filter);
1261 }