device: Refactor device scan function
[platform/upstream/connman.git] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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         int 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         char *name;
51         char *node;
52         char *address;
53         char *interface;
54         char *ident;
55         char *path;
56         char *devname;
57         int index;
58         guint pending_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 static void clear_pending_trigger(struct connman_device *device)
69 {
70         if (device->pending_timeout > 0) {
71                 g_source_remove(device->pending_timeout);
72                 device->pending_timeout = 0;
73         }
74 }
75
76 static const char *type2description(enum connman_device_type type)
77 {
78         switch (type) {
79         case CONNMAN_DEVICE_TYPE_UNKNOWN:
80         case CONNMAN_DEVICE_TYPE_VENDOR:
81                 break;
82         case CONNMAN_DEVICE_TYPE_ETHERNET:
83                 return "Ethernet";
84         case CONNMAN_DEVICE_TYPE_WIFI:
85                 return "Wireless";
86         case CONNMAN_DEVICE_TYPE_WIMAX:
87                 return "WiMAX";
88         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
89                 return "Bluetooth";
90         case CONNMAN_DEVICE_TYPE_GPS:
91                 return "GPS";
92         case CONNMAN_DEVICE_TYPE_CELLULAR:
93                 return "Cellular";
94         case CONNMAN_DEVICE_TYPE_GADGET:
95                 return "Gadget";
96
97         }
98
99         return NULL;
100 }
101
102 static const char *type2string(enum connman_device_type type)
103 {
104         switch (type) {
105         case CONNMAN_DEVICE_TYPE_UNKNOWN:
106         case CONNMAN_DEVICE_TYPE_VENDOR:
107                 break;
108         case CONNMAN_DEVICE_TYPE_ETHERNET:
109                 return "ethernet";
110         case CONNMAN_DEVICE_TYPE_WIFI:
111                 return "wifi";
112         case CONNMAN_DEVICE_TYPE_WIMAX:
113                 return "wimax";
114         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
115                 return "bluetooth";
116         case CONNMAN_DEVICE_TYPE_GPS:
117                 return "gps";
118         case CONNMAN_DEVICE_TYPE_CELLULAR:
119                 return "cellular";
120         case CONNMAN_DEVICE_TYPE_GADGET:
121                 return "gadget";
122
123         }
124
125         return NULL;
126 }
127
128 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
129 {
130         enum connman_device_type type = connman_device_get_type(device);
131
132         switch (type) {
133         case CONNMAN_DEVICE_TYPE_UNKNOWN:
134         case CONNMAN_DEVICE_TYPE_VENDOR:
135         case CONNMAN_DEVICE_TYPE_GPS:
136                 break;
137         case CONNMAN_DEVICE_TYPE_ETHERNET:
138                 return CONNMAN_SERVICE_TYPE_ETHERNET;
139         case CONNMAN_DEVICE_TYPE_WIFI:
140                 return CONNMAN_SERVICE_TYPE_WIFI;
141         case CONNMAN_DEVICE_TYPE_WIMAX:
142                 return CONNMAN_SERVICE_TYPE_WIMAX;
143         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
144                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
145         case CONNMAN_DEVICE_TYPE_CELLULAR:
146                 return CONNMAN_SERVICE_TYPE_CELLULAR;
147         case CONNMAN_DEVICE_TYPE_GADGET:
148                 return CONNMAN_SERVICE_TYPE_GADGET;
149
150         }
151
152         return CONNMAN_SERVICE_TYPE_UNKNOWN;
153 }
154
155 static gboolean device_pending_reset(gpointer user_data)
156 {
157         struct connman_device *device = user_data;
158
159         DBG("device %p", device);
160
161         /* Power request timedout, reset power pending state. */
162         device->pending_timeout = 0;
163         device->powered_pending = PENDING_NONE;
164
165         return FALSE;
166 }
167
168 int __connman_device_enable(struct connman_device *device)
169 {
170         int err;
171
172         DBG("device %p", device);
173
174         if (!device->driver || !device->driver->enable)
175                 return -EOPNOTSUPP;
176
177         /* There is an ongoing power disable request. */
178         if (device->powered_pending == PENDING_DISABLE)
179                 return -EBUSY;
180
181         if (device->powered_pending == PENDING_ENABLE)
182                 return -EALREADY;
183
184         if (device->powered_pending == PENDING_NONE && device->powered == TRUE)
185                 return -EALREADY;
186
187         device->powered_pending = PENDING_ENABLE;
188
189         err = device->driver->enable(device);
190         /*
191          * device gets enabled right away.
192          * Invoke the callback
193          */
194         if (err == 0) {
195                 connman_device_set_powered(device, TRUE);
196                 goto done;
197         }
198
199         if (err == -EALREADY) {
200                 /* If device is already powered, but connman is not updated */
201                 connman_device_set_powered(device, TRUE);
202                 goto done;
203         }
204         /*
205          * if err == -EINPROGRESS, then the DBus call to the respective daemon
206          * was successful. We set a 4 sec timeout so if the daemon never
207          * returns a reply, we would reset the pending request.
208          */
209         if (err == -EINPROGRESS)
210                 device->pending_timeout = g_timeout_add_seconds(4,
211                                         device_pending_reset, device);
212 done:
213         return err;
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         /* Ongoing power enable request */
226         if (device->powered_pending == PENDING_ENABLE)
227                 return -EBUSY;
228
229         if (device->powered_pending == PENDING_DISABLE)
230                 return -EALREADY;
231
232         if (device->powered_pending == PENDING_NONE && device->powered == FALSE)
233                 return -EALREADY;
234
235         device->powered_pending = PENDING_DISABLE;
236         device->reconnect = FALSE;
237
238         if (device->network) {
239                 struct connman_service *service =
240                         connman_service_lookup_from_network(device->network);
241
242                 if (service != NULL)
243                         __connman_service_disconnect(service);
244                 else
245                         connman_network_set_connected(device->network, FALSE);
246         }
247
248         err = device->driver->disable(device);
249         if (err == 0 || err == -EALREADY) {
250                 connman_device_set_powered(device, FALSE);
251                 goto done;
252         }
253
254         if (err == -EINPROGRESS)
255                 device->pending_timeout = g_timeout_add_seconds(4,
256                                         device_pending_reset, device);
257 done:
258         return err;
259 }
260
261 static void probe_driver(struct connman_device_driver *driver)
262 {
263         GSList *list;
264
265         DBG("driver %p name %s", driver, driver->name);
266
267         for (list = device_list; list != NULL; list = list->next) {
268                 struct connman_device *device = list->data;
269
270                 if (device->driver != NULL)
271                         continue;
272
273                 if (driver->type != device->type)
274                         continue;
275
276                 if (driver->probe(device) < 0)
277                         continue;
278
279                 device->driver = driver;
280
281                 __connman_technology_add_device(device);
282         }
283 }
284
285 static void remove_device(struct connman_device *device)
286 {
287         DBG("device %p", device);
288
289         __connman_device_disable(device);
290
291         __connman_technology_remove_device(device);
292
293         if (device->driver->remove)
294                 device->driver->remove(device);
295
296         device->driver = NULL;
297 }
298
299 static void remove_driver(struct connman_device_driver *driver)
300 {
301         GSList *list;
302
303         DBG("driver %p name %s", driver, driver->name);
304
305         for (list = device_list; list != NULL; list = list->next) {
306                 struct connman_device *device = list->data;
307
308                 if (device->driver == driver)
309                         remove_device(device);
310         }
311 }
312
313 connman_bool_t __connman_device_has_driver(struct connman_device *device)
314 {
315         if (device == NULL || device->driver == NULL)
316                 return FALSE;
317
318         return TRUE;
319 }
320
321 static GSList *driver_list = NULL;
322
323 static gint compare_priority(gconstpointer a, gconstpointer b)
324 {
325         const struct connman_device_driver *driver1 = a;
326         const struct connman_device_driver *driver2 = b;
327
328         return driver2->priority - driver1->priority;
329 }
330
331 /**
332  * connman_device_driver_register:
333  * @driver: device driver definition
334  *
335  * Register a new device driver
336  *
337  * Returns: %0 on success
338  */
339 int connman_device_driver_register(struct connman_device_driver *driver)
340 {
341         DBG("driver %p name %s", driver, driver->name);
342
343         driver_list = g_slist_insert_sorted(driver_list, driver,
344                                                         compare_priority);
345         probe_driver(driver);
346
347         return 0;
348 }
349
350 /**
351  * connman_device_driver_unregister:
352  * @driver: device driver definition
353  *
354  * Remove a previously registered device driver
355  */
356 void connman_device_driver_unregister(struct connman_device_driver *driver)
357 {
358         DBG("driver %p name %s", driver, driver->name);
359
360         driver_list = g_slist_remove(driver_list, driver);
361
362         remove_driver(driver);
363 }
364
365 static void free_network(gpointer data)
366 {
367         struct connman_network *network = data;
368
369         DBG("network %p", network);
370
371         __connman_network_set_device(network, NULL);
372
373         connman_network_unref(network);
374 }
375
376 static void device_destruct(struct connman_device *device)
377 {
378         DBG("device %p name %s", device, device->name);
379
380         clear_pending_trigger(device);
381
382         g_free(device->ident);
383         g_free(device->node);
384         g_free(device->name);
385         g_free(device->address);
386         g_free(device->interface);
387         g_free(device->path);
388         g_free(device->devname);
389
390         g_free(device->last_network);
391
392         g_hash_table_destroy(device->networks);
393         device->networks = NULL;
394
395         g_free(device);
396 }
397
398 /**
399  * connman_device_create:
400  * @node: device node name (for example an address)
401  * @type: device type
402  *
403  * Allocate a new device of given #type and assign the #node name to it.
404  *
405  * Returns: a newly-allocated #connman_device structure
406  */
407 struct connman_device *connman_device_create(const char *node,
408                                                 enum connman_device_type type)
409 {
410         struct connman_device *device;
411
412         DBG("node %s type %d", node, type);
413
414         device = g_try_new0(struct connman_device, 1);
415         if (device == NULL)
416                 return NULL;
417
418         DBG("device %p", device);
419
420         device->refcount = 1;
421
422         device->type = type;
423         device->name = g_strdup(type2description(device->type));
424
425         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
426                                                 g_free, free_network);
427
428         device_list = g_slist_append(device_list, device);
429
430         return device;
431 }
432
433 /**
434  * connman_device_ref:
435  * @device: device structure
436  *
437  * Increase reference counter of device
438  */
439 struct connman_device *connman_device_ref_debug(struct connman_device *device,
440                                 const char *file, int line, const char *caller)
441 {
442         DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
443                 file, line, caller);
444
445         __sync_fetch_and_add(&device->refcount, 1);
446
447         return device;
448 }
449
450 /**
451  * connman_device_unref:
452  * @device: device structure
453  *
454  * Decrease reference counter of device
455  */
456 void connman_device_unref_debug(struct connman_device *device,
457                                 const char *file, int line, const char *caller)
458 {
459         DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
460                 file, line, caller);
461
462         if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
463                 return;
464
465         if (device->driver) {
466                 device->driver->remove(device);
467                 device->driver = NULL;
468         }
469
470         device_list = g_slist_remove(device_list, device);
471
472         device_destruct(device);
473 }
474
475 const char *__connman_device_get_type(struct connman_device *device)
476 {
477         return type2string(device->type);
478 }
479
480 /**
481  * connman_device_get_type:
482  * @device: device structure
483  *
484  * Get type of device
485  */
486 enum connman_device_type connman_device_get_type(struct connman_device *device)
487 {
488         return device->type;
489 }
490
491 /**
492  * connman_device_set_index:
493  * @device: device structure
494  * @index: index number
495  *
496  * Set index number of device
497  */
498 void connman_device_set_index(struct connman_device *device, int index)
499 {
500         device->index = index;
501 }
502
503 /**
504  * connman_device_get_index:
505  * @device: device structure
506  *
507  * Get index number of device
508  */
509 int connman_device_get_index(struct connman_device *device)
510 {
511         return device->index;
512 }
513
514 /**
515  * connman_device_set_interface:
516  * @device: device structure
517  * @interface: interface name
518  *
519  * Set interface name of device
520  */
521 void connman_device_set_interface(struct connman_device *device,
522                                                 const char *interface)
523 {
524         g_free(device->devname);
525         device->devname = g_strdup(interface);
526
527         g_free(device->interface);
528         device->interface = g_strdup(interface);
529
530         if (device->name == NULL) {
531                 const char *str = type2description(device->type);
532                 if (str != NULL && device->interface != NULL)
533                         device->name = g_strdup_printf("%s (%s)", str,
534                                                         device->interface);
535         }
536 }
537
538 /**
539  * connman_device_set_ident:
540  * @device: device structure
541  * @ident: unique identifier
542  *
543  * Set unique identifier of device
544  */
545 void connman_device_set_ident(struct connman_device *device,
546                                                         const char *ident)
547 {
548         g_free(device->ident);
549         device->ident = g_strdup(ident);
550 }
551
552 const char *connman_device_get_ident(struct connman_device *device)
553 {
554         return device->ident;
555 }
556
557 /**
558  * connman_device_set_powered:
559  * @device: device structure
560  * @powered: powered state
561  *
562  * Change power state of device
563  */
564 int connman_device_set_powered(struct connman_device *device,
565                                                 connman_bool_t powered)
566 {
567         enum connman_service_type type;
568
569         DBG("driver %p powered %d", device, powered);
570
571         if (device->powered == powered)
572                 return -EALREADY;
573
574         clear_pending_trigger(device);
575
576         device->powered_pending = PENDING_NONE;
577
578         device->powered = powered;
579
580         type = __connman_device_get_service_type(device);
581
582         if (device->powered == FALSE) {
583                 __connman_technology_disabled(type);
584                 return 0;
585         }
586
587         __connman_technology_enabled(type);
588
589         connman_device_set_disconnected(device, FALSE);
590         device->scanning = FALSE;
591
592         if (device->driver && device->driver->scan)
593                 device->driver->scan(device, NULL, 0, NULL, NULL, NULL);
594
595         return 0;
596 }
597
598 static int device_scan(struct connman_device *device)
599 {
600         if (!device->driver || !device->driver->scan)
601                 return -EOPNOTSUPP;
602
603         if (device->powered == FALSE)
604                 return -ENOLINK;
605
606         return device->driver->scan(device, NULL, 0, NULL, NULL, NULL);
607 }
608
609 int __connman_device_disconnect(struct connman_device *device)
610 {
611         GHashTableIter iter;
612         gpointer key, value;
613
614         DBG("device %p", device);
615
616         connman_device_set_disconnected(device, TRUE);
617
618         g_hash_table_iter_init(&iter, device->networks);
619
620         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
621                 struct connman_network *network = value;
622
623                 if (connman_network_get_connecting(network) == TRUE) {
624                         /*
625                          * Skip network in the process of connecting.
626                          * This is a workaround for WiFi networks serviced
627                          * by the supplicant plugin that hold a reference
628                          * to the network.  If we disconnect the network
629                          * here then the referenced object will not be
630                          * registered and usage (like launching DHCP client)
631                          * will fail.  There is nothing to be gained by
632                          * removing the network here anyway.
633                          */
634                         connman_warn("Skipping disconnect of %s, network is connecting.",
635                                 connman_network_get_identifier(network));
636                         continue;
637                 }
638
639                 __connman_network_disconnect(network);
640         }
641
642         return 0;
643 }
644
645 static void mark_network_available(gpointer key, gpointer value,
646                                                         gpointer user_data)
647 {
648         struct connman_network *network = value;
649
650         connman_network_set_available(network, TRUE);
651 }
652
653 static void mark_network_unavailable(gpointer key, gpointer value,
654                                                         gpointer user_data)
655 {
656         struct connman_network *network = value;
657
658         if (connman_network_get_connected(network) == TRUE)
659                 return;
660
661         connman_network_set_available(network, FALSE);
662 }
663
664 static gboolean remove_unavailable_network(gpointer key, gpointer value,
665                                                         gpointer user_data)
666 {
667         struct connman_network *network = value;
668
669         if (connman_network_get_connected(network) == TRUE)
670                 return FALSE;
671
672         if (connman_network_get_available(network) == TRUE)
673                 return FALSE;
674
675         return TRUE;
676 }
677
678 void __connman_device_cleanup_networks(struct connman_device *device)
679 {
680         g_hash_table_foreach_remove(device->networks,
681                                         remove_unavailable_network, NULL);
682 }
683
684 connman_bool_t connman_device_get_scanning(struct connman_device *device)
685 {
686         return device->scanning;
687 }
688
689 void connman_device_reset_scanning(struct connman_device *device)
690 {
691         g_hash_table_foreach(device->networks,
692                                 mark_network_available, NULL);
693 }
694
695 /**
696  * connman_device_set_scanning:
697  * @device: device structure
698  * @scanning: scanning state
699  *
700  * Change scanning state of device
701  */
702 int connman_device_set_scanning(struct connman_device *device,
703                                                 connman_bool_t scanning)
704 {
705         DBG("device %p scanning %d", device, scanning);
706
707         if (!device->driver || !device->driver->scan)
708                 return -EINVAL;
709
710         if (device->scanning == scanning)
711                 return -EALREADY;
712
713         device->scanning = scanning;
714
715         if (scanning == TRUE) {
716                 __connman_technology_scan_started(device);
717
718                 g_hash_table_foreach(device->networks,
719                                         mark_network_unavailable, NULL);
720
721                 return 0;
722         }
723
724         __connman_device_cleanup_networks(device);
725
726         __connman_technology_scan_stopped(device);
727
728         __connman_service_auto_connect();
729
730         return 0;
731 }
732
733 /**
734  * connman_device_set_disconnected:
735  * @device: device structure
736  * @disconnected: disconnected state
737  *
738  * Change disconnected state of device (only for device with networks)
739  */
740 int connman_device_set_disconnected(struct connman_device *device,
741                                                 connman_bool_t disconnected)
742 {
743         DBG("device %p disconnected %d", device, disconnected);
744
745         if (device->disconnected == disconnected)
746                 return -EALREADY;
747
748         device->disconnected = disconnected;
749
750         return 0;
751 }
752
753 /**
754  * connman_device_get_disconnected:
755  * @device: device structure
756  *
757  * Get device disconnected state
758  */
759 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
760 {
761         return device->disconnected;
762 }
763
764 /**
765  * connman_device_set_string:
766  * @device: device structure
767  * @key: unique identifier
768  * @value: string value
769  *
770  * Set string value for specific key
771  */
772 int connman_device_set_string(struct connman_device *device,
773                                         const char *key, const char *value)
774 {
775         DBG("device %p key %s value %s", device, key, value);
776
777         if (g_str_equal(key, "Address") == TRUE) {
778                 g_free(device->address);
779                 device->address = g_strdup(value);
780         } else if (g_str_equal(key, "Name") == TRUE) {
781                 g_free(device->name);
782                 device->name = g_strdup(value);
783         } else if (g_str_equal(key, "Node") == TRUE) {
784                 g_free(device->node);
785                 device->node = g_strdup(value);
786         } else if (g_str_equal(key, "Path") == TRUE) {
787                 g_free(device->path);
788                 device->path = g_strdup(value);
789         } else {
790                 return -EINVAL;
791         }
792
793         return 0;
794 }
795
796 /**
797  * connman_device_get_string:
798  * @device: device structure
799  * @key: unique identifier
800  *
801  * Get string value for specific key
802  */
803 const char *connman_device_get_string(struct connman_device *device,
804                                                         const char *key)
805 {
806         DBG("device %p key %s", device, key);
807
808         if (g_str_equal(key, "Address") == TRUE)
809                 return device->address;
810         else if (g_str_equal(key, "Name") == TRUE)
811                 return device->name;
812         else if (g_str_equal(key, "Node") == TRUE)
813                 return device->node;
814         else if (g_str_equal(key, "Interface") == TRUE)
815                 return device->interface;
816         else if (g_str_equal(key, "Path") == TRUE)
817                 return device->path;
818
819         return NULL;
820 }
821
822 /**
823  * connman_device_add_network:
824  * @device: device structure
825  * @network: network structure
826  *
827  * Add new network to the device
828  */
829 int connman_device_add_network(struct connman_device *device,
830                                         struct connman_network *network)
831 {
832         const char *identifier = connman_network_get_identifier(network);
833
834         DBG("device %p network %p", device, network);
835
836         if (identifier == NULL)
837                 return -EINVAL;
838
839         connman_network_ref(network);
840
841         __connman_network_set_device(network, device);
842
843         g_hash_table_replace(device->networks, g_strdup(identifier),
844                                                                 network);
845
846         return 0;
847 }
848
849 /**
850  * connman_device_get_network:
851  * @device: device structure
852  * @identifier: network identifier
853  *
854  * Get network for given identifier
855  */
856 struct connman_network *connman_device_get_network(struct connman_device *device,
857                                                         const char *identifier)
858 {
859         DBG("device %p identifier %s", device, identifier);
860
861         return g_hash_table_lookup(device->networks, identifier);
862 }
863
864 /**
865  * connman_device_remove_network:
866  * @device: device structure
867  * @identifier: network identifier
868  *
869  * Remove network for given identifier
870  */
871 int connman_device_remove_network(struct connman_device *device,
872                                                 struct connman_network *network)
873 {
874         const char *identifier;
875
876         DBG("device %p network %p", device, network);
877
878         if (network == NULL)
879                 return 0;
880
881         identifier = connman_network_get_identifier(network);
882         g_hash_table_remove(device->networks, identifier);
883
884         return 0;
885 }
886
887 void connman_device_remove_all_networks(struct connman_device *device)
888 {
889         g_hash_table_remove_all(device->networks);
890 }
891
892 void __connman_device_set_network(struct connman_device *device,
893                                         struct connman_network *network)
894 {
895         const char *name;
896
897         if (device == NULL)
898                 return;
899
900         if (device->network == network)
901                 return;
902
903         if (network != NULL) {
904                 name = connman_network_get_string(network, "Name");
905                 g_free(device->last_network);
906                 device->last_network = g_strdup(name);
907
908                 device->network = network;
909         } else {
910                 g_free(device->last_network);
911                 device->last_network = NULL;
912
913                 device->network = NULL;
914         }
915 }
916
917 void __connman_device_set_reconnect(struct connman_device *device,
918                                                 connman_bool_t reconnect)
919 {
920         device->reconnect = reconnect;
921 }
922
923 connman_bool_t  __connman_device_get_reconnect(
924                                 struct connman_device *device)
925 {
926         return device->reconnect;
927 }
928
929 static gboolean match_driver(struct connman_device *device,
930                                         struct connman_device_driver *driver)
931 {
932         if (device->type == driver->type ||
933                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
934                 return TRUE;
935
936         return FALSE;
937 }
938
939 /**
940  * connman_device_register:
941  * @device: device structure
942  *
943  * Register device with the system
944  */
945 int connman_device_register(struct connman_device *device)
946 {
947         GSList *list;
948
949         DBG("device %p name %s", device, device->name);
950
951         if (device->driver != NULL)
952                 return -EALREADY;
953
954         for (list = driver_list; list; list = list->next) {
955                 struct connman_device_driver *driver = list->data;
956
957                 if (match_driver(device, driver) == FALSE)
958                         continue;
959
960                 DBG("driver %p name %s", driver, driver->name);
961
962                 if (driver->probe(device) == 0) {
963                         device->driver = driver;
964                         break;
965                 }
966         }
967
968         if (device->driver == NULL)
969                 return 0;
970
971         return __connman_technology_add_device(device);
972 }
973
974 /**
975  * connman_device_unregister:
976  * @device: device structure
977  *
978  * Unregister device with the system
979  */
980 void connman_device_unregister(struct connman_device *device)
981 {
982         DBG("device %p name %s", device, device->name);
983
984         if (device->driver == NULL)
985                 return;
986
987         remove_device(device);
988 }
989
990 /**
991  * connman_device_get_data:
992  * @device: device structure
993  *
994  * Get private device data pointer
995  */
996 void *connman_device_get_data(struct connman_device *device)
997 {
998         return device->driver_data;
999 }
1000
1001 /**
1002  * connman_device_set_data:
1003  * @device: device structure
1004  * @data: data pointer
1005  *
1006  * Set private device data pointer
1007  */
1008 void connman_device_set_data(struct connman_device *device, void *data)
1009 {
1010         device->driver_data = data;
1011 }
1012
1013 struct connman_device *__connman_device_find_device(
1014                                 enum connman_service_type type)
1015 {
1016         GSList *list;
1017
1018         for (list = device_list; list != NULL; list = list->next) {
1019                 struct connman_device *device = list->data;
1020                 enum connman_service_type service_type =
1021                         __connman_device_get_service_type(device);
1022
1023                 if (service_type != type)
1024                         continue;
1025
1026                 return device;
1027         }
1028
1029         return NULL;
1030 }
1031
1032 /**
1033  * connman_device_set_regdom
1034  * @device: device structure
1035  * @alpha2: string representing regulatory domain
1036  *
1037  * Set regulatory domain on device basis
1038  */
1039 int connman_device_set_regdom(struct connman_device *device,
1040                                                 const char *alpha2)
1041 {
1042         if (device->driver == NULL || device->driver->set_regdom == NULL)
1043                 return -ENOTSUP;
1044
1045         if (device->powered == FALSE)
1046                 return -EINVAL;
1047
1048         return device->driver->set_regdom(device, alpha2);
1049 }
1050
1051 /**
1052  * connman_device_regdom_notify
1053  * @device: device structure
1054  * @alpha2: string representing regulatory domain
1055  *
1056  * Notify on setting regulatory domain on device basis
1057  */
1058 void connman_device_regdom_notify(struct connman_device *device,
1059                                         int result, const char *alpha2)
1060 {
1061         __connman_technology_notify_regdom_by_device(device, result, alpha2);
1062 }
1063
1064 int __connman_device_request_scan(enum connman_service_type type)
1065 {
1066         connman_bool_t success = FALSE;
1067         int last_err = -ENOSYS;
1068         GSList *list;
1069         int err;
1070
1071         switch (type) {
1072         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1073         case CONNMAN_SERVICE_TYPE_SYSTEM:
1074         case CONNMAN_SERVICE_TYPE_ETHERNET:
1075         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1076         case CONNMAN_SERVICE_TYPE_CELLULAR:
1077         case CONNMAN_SERVICE_TYPE_GPS:
1078         case CONNMAN_SERVICE_TYPE_VPN:
1079         case CONNMAN_SERVICE_TYPE_GADGET:
1080                 return -EOPNOTSUPP;
1081         case CONNMAN_SERVICE_TYPE_WIFI:
1082         case CONNMAN_SERVICE_TYPE_WIMAX:
1083                 break;
1084         }
1085
1086         for (list = device_list; list != NULL; list = list->next) {
1087                 struct connman_device *device = list->data;
1088                 enum connman_service_type service_type =
1089                         __connman_device_get_service_type(device);
1090
1091                 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1092                                 service_type != type) {
1093                         continue;
1094                 }
1095
1096                 err = device_scan(device);
1097                 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1098                         success = TRUE;
1099                 } else {
1100                         last_err = err;
1101                         DBG("device %p err %d", device, err);
1102                 }
1103         }
1104
1105         if (success == TRUE)
1106                 return 0;
1107
1108         return last_err;
1109 }
1110
1111 int __connman_device_request_hidden_scan(struct connman_device *device,
1112                                 const char *ssid, unsigned int ssid_len,
1113                                 const char *identity, const char *passphrase,
1114                                 void *user_data)
1115 {
1116         DBG("device %p", device);
1117
1118         if (device == NULL || device->driver == NULL ||
1119                         device->driver->scan == NULL)
1120                 return -EINVAL;
1121
1122         if (device->scanning == TRUE)
1123                 return -EALREADY;
1124
1125         return device->driver->scan(device, ssid, ssid_len,
1126                                         identity, passphrase, user_data);
1127 }
1128
1129 connman_bool_t __connman_device_isfiltered(const char *devname)
1130 {
1131         char **pattern;
1132         char **blacklisted_interfaces;
1133
1134         if (device_filter == NULL)
1135                 goto nodevice;
1136
1137         for (pattern = device_filter; *pattern; pattern++) {
1138                 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1139                         DBG("ignoring device %s (match)", devname);
1140                         return TRUE;
1141                 }
1142         }
1143
1144 nodevice:
1145         if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1146                 DBG("ignoring dummy networking devices");
1147                 return TRUE;
1148         }
1149
1150         if (nodevice_filter == NULL)
1151                 goto list;
1152
1153         for (pattern = nodevice_filter; *pattern; pattern++) {
1154                 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1155                         DBG("ignoring device %s (no match)", devname);
1156                         return TRUE;
1157                 }
1158         }
1159
1160 list:
1161         blacklisted_interfaces =
1162                 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1163         if (blacklisted_interfaces == NULL)
1164                 return FALSE;
1165
1166         for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1167                 if (g_str_has_prefix(devname, *pattern) == TRUE) {
1168                         DBG("ignoring device %s (blacklist)", devname);
1169                         return TRUE;
1170                 }
1171         }
1172
1173         return FALSE;
1174 }
1175
1176 static void cleanup_devices(void)
1177 {
1178         /*
1179          * Check what interfaces are currently up and if connman is
1180          * suppose to handle the interface, then cleanup the mess
1181          * related to that interface. There might be weird routes etc
1182          * that are related to that interface and that might confuse
1183          * connmand. So in this case we just turn the interface down
1184          * so that kernel removes routes/addresses automatically and
1185          * then proceed the startup.
1186          *
1187          * Note that this cleanup must be done before rtnl/detect code
1188          * has activated interface watches.
1189          */
1190
1191         char **interfaces;
1192         int i;
1193
1194         interfaces = __connman_inet_get_running_interfaces();
1195
1196         if (interfaces == NULL)
1197                 return;
1198
1199         for (i = 0; interfaces[i] != NULL; i++) {
1200                 connman_bool_t filtered;
1201                 int index;
1202
1203                 filtered = __connman_device_isfiltered(interfaces[i]);
1204                 if (filtered == TRUE)
1205                         continue;
1206
1207                 index = connman_inet_ifindex(interfaces[i]);
1208                 if (index < 0)
1209                         continue;
1210
1211                 DBG("cleaning up %s index %d", interfaces[i], index);
1212
1213                 connman_inet_ifdown(index);
1214
1215                 /*
1216                  * ConnMan will turn the interface UP automatically so
1217                  * no need to do it here.
1218                  */
1219         }
1220
1221         g_strfreev(interfaces);
1222 }
1223
1224 int __connman_device_init(const char *device, const char *nodevice)
1225 {
1226         DBG("");
1227
1228         if (device != NULL)
1229                 device_filter = g_strsplit(device, ",", -1);
1230
1231         if (nodevice != NULL)
1232                 nodevice_filter = g_strsplit(nodevice, ",", -1);
1233
1234         cleanup_devices();
1235
1236         return 0;
1237 }
1238
1239 void __connman_device_cleanup(void)
1240 {
1241         DBG("");
1242
1243         g_strfreev(nodevice_filter);
1244         g_strfreev(device_filter);
1245 }