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