Fix issue with missing scanning after disconnect
[framework/connectivity/connman.git] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection = NULL;
34
35 struct connman_device {
36         struct connman_element element;
37         enum connman_device_type type;
38         enum connman_device_mode mode;
39         connman_bool_t secondary;
40         connman_bool_t offlinemode;
41         connman_bool_t blocked;
42         connman_bool_t powered;
43         connman_bool_t powered_pending;
44         connman_bool_t powered_persistent;
45         connman_bool_t carrier;
46         connman_bool_t scanning;
47         connman_bool_t disconnected;
48         connman_uint16_t scan_interval;
49         char *name;
50         char *node;
51         char *address;
52         char *interface;
53         char *control;
54         char *ident;
55         int phyindex;
56         unsigned int connections;
57         guint scan_timeout;
58         struct connman_ipconfig *ipconfig;
59
60         struct connman_device_driver *driver;
61         void *driver_data;
62
63         connman_bool_t registered;
64
65         char *last_network;
66         struct connman_network *network;
67         GHashTable *networks;
68
69         DBusMessage *pending;
70         guint timeout;
71 };
72
73 static gboolean device_scan_trigger(gpointer user_data)
74 {
75         struct connman_device *device = user_data;
76
77         DBG("device %p", device);
78
79         if (device->driver == NULL) {
80                 device->scan_timeout = 0;
81                 return FALSE;
82         }
83
84         if (device->driver->scan)
85                 device->driver->scan(device);
86
87         return TRUE;
88 }
89
90 static void clear_scan_trigger(struct connman_device *device)
91 {
92         if (device->scan_timeout > 0) {
93                 g_source_remove(device->scan_timeout);
94                 device->scan_timeout = 0;
95         }
96 }
97
98 static void reset_scan_trigger(struct connman_device *device)
99 {
100         clear_scan_trigger(device);
101
102         if (device->scan_interval > 0) {
103                 guint interval = device->scan_interval;
104                 device->scan_timeout = g_timeout_add_seconds(interval,
105                                         device_scan_trigger, device);
106         }
107 }
108
109 static void force_scan_trigger(struct connman_device *device)
110 {
111         clear_scan_trigger(device);
112
113         device->scan_timeout = g_timeout_add_seconds(5,
114                                         device_scan_trigger, device);
115 }
116
117 static const char *type2description(enum connman_device_type type)
118 {
119         switch (type) {
120         case CONNMAN_DEVICE_TYPE_UNKNOWN:
121         case CONNMAN_DEVICE_TYPE_VENDOR:
122                 break;
123         case CONNMAN_DEVICE_TYPE_ETHERNET:
124                 return "Ethernet";
125         case CONNMAN_DEVICE_TYPE_WIFI:
126                 return "Wireless";
127         case CONNMAN_DEVICE_TYPE_WIMAX:
128                 return "WiMAX";
129         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
130                 return "Bluetooth";
131         case CONNMAN_DEVICE_TYPE_GPS:
132                 return "GPS";
133         case CONNMAN_DEVICE_TYPE_MBM:
134         case CONNMAN_DEVICE_TYPE_HSO:
135         case CONNMAN_DEVICE_TYPE_NOZOMI:
136         case CONNMAN_DEVICE_TYPE_HUAWEI:
137         case CONNMAN_DEVICE_TYPE_NOVATEL:
138                 return "Cellular";
139         }
140
141         return NULL;
142 }
143
144 static const char *type2string(enum connman_device_type type)
145 {
146         switch (type) {
147         case CONNMAN_DEVICE_TYPE_UNKNOWN:
148         case CONNMAN_DEVICE_TYPE_VENDOR:
149                 break;
150         case CONNMAN_DEVICE_TYPE_ETHERNET:
151                 return "ethernet";
152         case CONNMAN_DEVICE_TYPE_WIFI:
153                 return "wifi";
154         case CONNMAN_DEVICE_TYPE_WIMAX:
155                 return "wimax";
156         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
157                 return "bluetooth";
158         case CONNMAN_DEVICE_TYPE_GPS:
159                 return "gps";
160         case CONNMAN_DEVICE_TYPE_MBM:
161         case CONNMAN_DEVICE_TYPE_HSO:
162         case CONNMAN_DEVICE_TYPE_HUAWEI:
163         case CONNMAN_DEVICE_TYPE_NOZOMI:
164         case CONNMAN_DEVICE_TYPE_NOVATEL:
165                 return "cellular";
166         }
167
168         return NULL;
169 }
170
171 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
172 {
173         enum connman_device_type type = connman_device_get_type(device);
174
175         switch (type) {
176         case CONNMAN_DEVICE_TYPE_UNKNOWN:
177         case CONNMAN_DEVICE_TYPE_VENDOR:
178         case CONNMAN_DEVICE_TYPE_GPS:
179         case CONNMAN_DEVICE_TYPE_NOZOMI:
180         case CONNMAN_DEVICE_TYPE_HUAWEI:
181         case CONNMAN_DEVICE_TYPE_NOVATEL:
182                 break;
183         case CONNMAN_DEVICE_TYPE_ETHERNET:
184                 return CONNMAN_SERVICE_TYPE_ETHERNET;
185         case CONNMAN_DEVICE_TYPE_WIFI:
186                 return CONNMAN_SERVICE_TYPE_WIFI;
187         case CONNMAN_DEVICE_TYPE_WIMAX:
188                 return CONNMAN_SERVICE_TYPE_WIMAX;
189         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
190                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
191         case CONNMAN_DEVICE_TYPE_MBM:
192         case CONNMAN_DEVICE_TYPE_HSO:
193                 return CONNMAN_SERVICE_TYPE_CELLULAR;
194         }
195
196         return CONNMAN_SERVICE_TYPE_UNKNOWN;
197 }
198
199 static int set_connected(struct connman_device *device,
200                                                 connman_bool_t connected)
201 {
202         if (connected == TRUE) {
203                 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
204                 struct connman_element *element;
205
206                 device->disconnected = TRUE;
207
208                 switch (device->element.ipv4.method) {
209                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
210                 case CONNMAN_IPCONFIG_METHOD_IGNORE:
211                         return 0;
212                 case CONNMAN_IPCONFIG_METHOD_STATIC:
213                         type = CONNMAN_ELEMENT_TYPE_IPV4;
214                         break;
215                 case CONNMAN_IPCONFIG_METHOD_DHCP:
216                         type = CONNMAN_ELEMENT_TYPE_DHCP;
217                         break;
218                 }
219
220                 element = connman_element_create(NULL);
221                 if (element != NULL) {
222                         struct connman_service *service;
223
224                         element->type  = type;
225                         element->index = device->element.index;
226
227                         if (connman_element_register(element,
228                                                         &device->element) < 0)
229                                 connman_element_unref(element);
230
231                         device->disconnected = FALSE;
232
233                         service = __connman_service_lookup_from_device(device);
234                         __connman_service_indicate_state(service,
235                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
236                 }
237         } else {
238                 struct connman_service *service;
239
240                 connman_element_unregister_children(&device->element);
241
242                 device->disconnected = TRUE;
243
244                 service = __connman_service_lookup_from_device(device);
245                 __connman_service_indicate_state(service,
246                                         CONNMAN_SERVICE_STATE_IDLE);
247         }
248
249         if (connected == TRUE) {
250                 enum connman_service_type type;
251
252                 type = __connman_device_get_service_type(device);
253                 __connman_notifier_connect(type);
254         } else {
255                 enum connman_service_type type;
256
257                 type = __connman_device_get_service_type(device);
258                 __connman_notifier_disconnect(type);
259         }
260
261         return 0;
262 }
263
264 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
265 {
266         if (carrier == TRUE)
267                 __connman_profile_add_device(device);
268         else
269                 __connman_profile_remove_device(device);
270
271         return set_connected(device, carrier);
272 }
273
274 static int powered_changed(struct connman_device *device)
275 {
276         DBusMessage *signal;
277         DBusMessageIter entry, value;
278         const char *key = "Powered";
279
280         signal = dbus_message_new_signal(device->element.path,
281                                 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
282         if (signal == NULL)
283                 return -ENOMEM;
284
285         dbus_message_iter_init_append(signal, &entry);
286
287         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
288
289         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
290                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
291         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
292                                                         &device->powered);
293         dbus_message_iter_close_container(&entry, &value);
294
295         g_dbus_send_message(connection, signal);
296
297         return 0;
298 }
299
300 static int set_powered(struct connman_device *device, connman_bool_t powered)
301 {
302         struct connman_device_driver *driver = device->driver;
303         enum connman_service_type type;
304         int err;
305
306         DBG("device %p powered %d", device, powered);
307
308         if (device->powered_pending == powered)
309                 return -EALREADY;
310
311         if (!driver)
312                 return -EINVAL;
313
314         type = __connman_device_get_service_type(device);
315
316         if (powered == TRUE) {
317                 if (driver->enable) {
318                         device->powered_pending = powered;
319
320                         err = driver->enable(device);
321                         if (err == 0)
322                                 __connman_notifier_enable(type);
323                 } else
324                         err = -EINVAL;
325         } else {
326                 device->powered_pending = powered;
327
328                 clear_scan_trigger(device);
329
330                 g_hash_table_remove_all(device->networks);
331
332                 set_carrier(device, FALSE);
333
334                 if (driver->disable) {
335                         err = driver->disable(device);
336                         if (err == 0)
337                                 __connman_notifier_disable(type);
338                 } else
339                         err = -EINVAL;
340         }
341
342         if (err == 0) {
343                 device->powered = powered;
344
345                 if (device->registered == TRUE)
346                         powered_changed(device);
347         }
348
349         return err;
350 }
351
352 static void append_path(gpointer key, gpointer value, gpointer user_data)
353 {
354         struct connman_element *element = value;
355         DBusMessageIter *iter = user_data;
356
357         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
358                                                         &element->path);
359 }
360
361 static void append_networks(struct connman_device *device,
362                                                 DBusMessageIter *entry)
363 {
364         DBusMessageIter value, iter;
365         const char *key = "Networks";
366
367         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
368
369         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
370                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
371                                                                 &value);
372
373         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
374                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
375         g_hash_table_foreach(device->networks, append_path, &iter);
376         dbus_message_iter_close_container(&value, &iter);
377
378         dbus_message_iter_close_container(entry, &value);
379 }
380
381 static DBusMessage *get_properties(DBusConnection *conn,
382                                         DBusMessage *msg, void *data)
383 {
384         struct connman_device *device = data;
385         DBusMessage *reply;
386         DBusMessageIter array, dict, entry;
387         const char *str;
388
389         DBG("conn %p", conn);
390
391         if (__connman_security_check_privilege(msg,
392                                         CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
393                 return __connman_error_permission_denied(msg);
394
395         reply = dbus_message_new_method_return(msg);
396         if (reply == NULL)
397                 return NULL;
398
399         dbus_message_iter_init_append(reply, &array);
400
401         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
402                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
403                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
404                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
405
406         if (device->name != NULL)
407                 connman_dbus_dict_append_variant(&dict, "Name",
408                                         DBUS_TYPE_STRING, &device->name);
409
410         str = type2string(device->type);
411         if (str != NULL)
412                 connman_dbus_dict_append_variant(&dict, "Type",
413                                                 DBUS_TYPE_STRING, &str);
414
415         if (device->address != NULL)
416                 connman_dbus_dict_append_variant(&dict, "Address",
417                                         DBUS_TYPE_STRING, &device->address);
418
419         if (device->interface != NULL)
420                 connman_dbus_dict_append_variant(&dict, "Interface",
421                                         DBUS_TYPE_STRING, &device->interface);
422
423         connman_dbus_dict_append_variant(&dict, "Powered",
424                                         DBUS_TYPE_BOOLEAN, &device->powered);
425
426         if (device->driver && device->driver->scan)
427                 connman_dbus_dict_append_variant(&dict, "Scanning",
428                                         DBUS_TYPE_BOOLEAN, &device->scanning);
429
430         switch (device->mode) {
431         case CONNMAN_DEVICE_MODE_UNKNOWN:
432                 break;
433         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
434                 __connman_element_append_ipv4(&device->element, &dict);
435                 break;
436         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
437         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
438                 if (device->scan_interval > 0)
439                         connman_dbus_dict_append_variant(&dict, "ScanInterval",
440                                 DBUS_TYPE_UINT16, &device->scan_interval);
441
442                 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
443                                                                 NULL, &entry);
444                 append_networks(device, &entry);
445                 dbus_message_iter_close_container(&dict, &entry);
446                 break;
447         }
448
449         dbus_message_iter_close_container(&array, &dict);
450
451         return reply;
452 }
453
454 static gboolean powered_timeout(gpointer user_data)
455 {
456         struct connman_device *device = user_data;
457
458         DBG("device %p", device);
459
460         device->timeout = 0;
461
462         if (device->pending != NULL) {
463                 DBusMessage *reply;
464
465                 reply = __connman_error_operation_timeout(device->pending);
466                 if (reply != NULL)
467                         g_dbus_send_message(connection, reply);
468
469                 dbus_message_unref(device->pending);
470                 device->pending = NULL;
471         }
472
473         return FALSE;
474 }
475
476 static DBusMessage *set_property(DBusConnection *conn,
477                                         DBusMessage *msg, void *data)
478 {
479         struct connman_device *device = data;
480         DBusMessageIter iter, value;
481         const char *name;
482         int type;
483
484         DBG("conn %p", conn);
485
486         if (dbus_message_iter_init(msg, &iter) == FALSE)
487                 return __connman_error_invalid_arguments(msg);
488
489         dbus_message_iter_get_basic(&iter, &name);
490         dbus_message_iter_next(&iter);
491         dbus_message_iter_recurse(&iter, &value);
492
493         if (__connman_security_check_privilege(msg,
494                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
495                 return __connman_error_permission_denied(msg);
496
497         type = dbus_message_iter_get_arg_type(&value);
498
499         if (g_str_equal(name, "Powered") == TRUE) {
500                 connman_bool_t powered;
501                 int err;
502
503                 if (type != DBUS_TYPE_BOOLEAN)
504                         return __connman_error_invalid_arguments(msg);
505
506                 dbus_message_iter_get_basic(&value, &powered);
507
508                 device->powered_persistent = powered;
509
510                 __connman_storage_save_device(device);
511
512                 if (device->powered == powered)
513                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
514
515                 if (device->pending != NULL)
516                         return __connman_error_in_progress(msg);
517
518                 err = set_powered(device, powered);
519                 if (err < 0) {
520                         if (err != -EINPROGRESS)
521                                 return __connman_error_failed(msg, -err);
522
523                         device->pending = dbus_message_ref(msg);
524
525                         device->timeout = g_timeout_add_seconds(15,
526                                                 powered_timeout, device);
527
528                         return NULL;
529                 }
530         } else if (g_str_equal(name, "ScanInterval") == TRUE) {
531                 connman_uint16_t interval;
532
533                 switch (device->mode) {
534                 case CONNMAN_DEVICE_MODE_UNKNOWN:
535                 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
536                         return __connman_error_invalid_arguments(msg);
537                 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
538                 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
539                         break;
540                 }
541
542                 if (type != DBUS_TYPE_UINT16)
543                         return __connman_error_invalid_arguments(msg);
544
545                 dbus_message_iter_get_basic(&value, &interval);
546
547                 if (device->scan_interval != interval) {
548                         device->scan_interval = interval;
549
550                         __connman_storage_save_device(device);
551
552                         reset_scan_trigger(device);
553                 }
554         } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
555                 int err;
556
557                 if (device->ipconfig == NULL)
558                         return __connman_error_invalid_property(msg);
559
560                 switch (device->mode) {
561                 case CONNMAN_DEVICE_MODE_UNKNOWN:
562                 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
563                 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
564                         return __connman_error_invalid_arguments(msg);
565                 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
566                         break;
567                 }
568
569                 err = __connman_ipconfig_set_ipv4(device->ipconfig,
570                                                         name + 5, &value);
571                 if (err < 0)
572                         return __connman_error_failed(msg, -err);
573         } else
574                 return __connman_error_invalid_property(msg);
575
576         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
577 }
578
579 static DBusMessage *propose_scan(DBusConnection *conn,
580                                         DBusMessage *msg, void *data)
581 {
582         struct connman_device *device = data;
583         int err;
584
585         DBG("conn %p", conn);
586
587         switch (device->mode) {
588         case CONNMAN_DEVICE_MODE_UNKNOWN:
589         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
590                 return __connman_error_not_supported(msg);
591         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
592         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
593                 break;
594         }
595
596         err = __connman_device_scan(device);
597         if (err < 0)
598                 return __connman_error_failed(msg, -err);
599
600         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
601 }
602
603 static GDBusMethodTable device_methods[] = {
604         { "GetProperties", "",      "a{sv}", get_properties },
605         { "SetProperty",   "sv",    "",      set_property,
606                                                 G_DBUS_METHOD_FLAG_ASYNC },
607         { "ProposeScan",   "",      "",      propose_scan   },
608         { },
609 };
610
611 static GDBusSignalTable device_signals[] = {
612         { "PropertyChanged", "sv" },
613         { },
614 };
615
616 static void append_devices(DBusMessageIter *entry)
617 {
618         DBusMessageIter value, iter;
619         const char *key = "Devices";
620
621         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
622
623         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
624                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
625                                                                 &value);
626
627         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
628                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
629         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
630         dbus_message_iter_close_container(&value, &iter);
631
632         dbus_message_iter_close_container(entry, &value);
633 }
634
635 static void emit_devices_signal(void)
636 {
637         DBusMessage *signal;
638         DBusMessageIter entry;
639
640         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
641                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
642         if (signal == NULL)
643                 return;
644
645         dbus_message_iter_init_append(signal, &entry);
646
647         append_devices(&entry);
648
649         g_dbus_send_message(connection, signal);
650 }
651
652 static int register_interface(struct connman_element *element)
653 {
654         struct connman_device *device = element->device;
655
656         DBG("element %p name %s", element, element->name);
657
658         if (g_dbus_register_interface(connection, element->path,
659                                         CONNMAN_DEVICE_INTERFACE,
660                                         device_methods, device_signals,
661                                         NULL, device, NULL) == FALSE) {
662                 connman_error("Failed to register %s device", element->path);
663                 return -EIO;
664         }
665
666         device->registered = TRUE;
667
668         emit_devices_signal();
669
670         return 0;
671 }
672
673 static void unregister_interface(struct connman_element *element)
674 {
675         struct connman_device *device = element->device;
676
677         DBG("element %p name %s", element, element->name);
678
679         device->registered = FALSE;
680
681         emit_devices_signal();
682
683         g_dbus_unregister_interface(connection, element->path,
684                                                 CONNMAN_DEVICE_INTERFACE);
685 }
686
687 static int setup_device(struct connman_device *device)
688 {
689         enum connman_service_type type;
690         int err;
691
692         DBG("device %p", device);
693
694         err = register_interface(&device->element);
695         if (err < 0) {
696                 if (device->driver->remove)
697                         device->driver->remove(device);
698                 device->driver = NULL;
699                 return err;
700         }
701
702         type = __connman_device_get_service_type(device);
703         __connman_notifier_register(type);
704
705         switch (device->mode) {
706         case CONNMAN_DEVICE_MODE_UNKNOWN:
707         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
708         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
709                 break;
710         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
711                 if (device->carrier == TRUE && device->secondary == FALSE)
712                         __connman_profile_add_device(device);
713                 break;
714         }
715
716         if (device->offlinemode == FALSE &&
717                                 device->powered_persistent == TRUE)
718                 __connman_device_enable(device);
719
720         return 0;
721 }
722
723 static void probe_driver(struct connman_element *element, gpointer user_data)
724 {
725         struct connman_device_driver *driver = user_data;
726
727         DBG("element %p name %s", element, element->name);
728
729         if (element->device == NULL)
730                 return;
731
732         if (element->device->driver != NULL)
733                 return;
734
735         if (driver->type != element->device->type)
736                 return;
737
738         if (driver->probe(element->device) < 0)
739                 return;
740
741         element->device->driver = driver;
742
743         setup_device(element->device);
744 }
745
746 static void remove_device(struct connman_device *device)
747 {
748         enum connman_service_type type;
749
750         DBG("device %p", device);
751
752         __connman_device_disable(device);
753
754         switch (device->mode) {
755         case CONNMAN_DEVICE_MODE_UNKNOWN:
756         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
757         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
758                 break;
759         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
760                 if (device->secondary == FALSE)
761                         __connman_profile_remove_device(device);
762                 break;
763         }
764
765         type = __connman_device_get_service_type(device);
766         __connman_notifier_unregister(type);
767
768         unregister_interface(&device->element);
769
770         if (device->driver->remove)
771                 device->driver->remove(device);
772
773         device->driver = NULL;
774 }
775
776 static void remove_driver(struct connman_element *element, gpointer user_data)
777 {
778         struct connman_device_driver *driver = user_data;
779
780         DBG("element %p name %s", element, element->name);
781
782         if (element->device == NULL)
783                 return;
784
785         if (element->device->driver == driver)
786                 remove_device(element->device);
787 }
788
789 connman_bool_t __connman_device_has_driver(struct connman_device *device)
790 {
791         if (device == NULL || device->driver == NULL)
792                 return FALSE;
793
794         return device->registered;
795 }
796
797 static GSList *driver_list = NULL;
798
799 static gint compare_priority(gconstpointer a, gconstpointer b)
800 {
801         const struct connman_device_driver *driver1 = a;
802         const struct connman_device_driver *driver2 = b;
803
804         return driver2->priority - driver1->priority;
805 }
806
807 /**
808  * connman_device_driver_register:
809  * @driver: device driver definition
810  *
811  * Register a new device driver
812  *
813  * Returns: %0 on success
814  */
815 int connman_device_driver_register(struct connman_device_driver *driver)
816 {
817         DBG("driver %p name %s", driver, driver->name);
818
819         driver_list = g_slist_insert_sorted(driver_list, driver,
820                                                         compare_priority);
821
822         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
823                                                 probe_driver, driver);
824
825         return 0;
826 }
827
828 /**
829  * connman_device_driver_unregister:
830  * @driver: device driver definition
831  *
832  * Remove a previously registered device driver
833  */
834 void connman_device_driver_unregister(struct connman_device_driver *driver)
835 {
836         DBG("driver %p name %s", driver, driver->name);
837
838         driver_list = g_slist_remove(driver_list, driver);
839
840         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
841                                                 remove_driver, driver);
842 }
843
844 static void unregister_network(gpointer data)
845 {
846         struct connman_network *network = data;
847
848         DBG("network %p", network);
849
850         connman_element_unregister((struct connman_element *) network);
851
852         connman_network_unref(network);
853 }
854
855 static void device_destruct(struct connman_element *element)
856 {
857         struct connman_device *device = element->device;
858
859         DBG("element %p name %s", element, element->name);
860
861         if (device->timeout > 0) {
862                 g_source_remove(device->timeout);
863                 device->timeout = 0;
864         }
865
866         if (device->pending != NULL) {
867                 dbus_message_unref(device->pending);
868                 device->pending = NULL;
869         }
870
871         g_free(device->ident);
872         g_free(device->node);
873         g_free(device->name);
874         g_free(device->address);
875         g_free(device->control);
876         g_free(device->interface);
877
878         if (device->ipconfig != NULL) {
879                 connman_ipconfig_unref(device->ipconfig);
880                 device->ipconfig = NULL;
881         }
882
883         g_free(device->last_network);
884
885         g_hash_table_destroy(device->networks);
886         device->networks = NULL;
887 }
888
889 /**
890  * connman_device_create:
891  * @node: device node name (for example an address)
892  * @type: device type
893  *
894  * Allocate a new device of given #type and assign the #node name to it.
895  *
896  * Returns: a newly-allocated #connman_device structure
897  */
898 struct connman_device *connman_device_create(const char *node,
899                                                 enum connman_device_type type)
900 {
901         struct connman_device *device;
902         const char *str;
903
904         DBG("node %s type %d", node, type);
905
906         device = g_try_new0(struct connman_device, 1);
907         if (device == NULL)
908                 return NULL;
909
910         DBG("device %p", device);
911
912         __connman_element_initialize(&device->element);
913
914         device->element.name = g_strdup(node);
915         device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
916
917         device->element.device = device;
918         device->element.destruct = device_destruct;
919
920         str = type2string(type);
921         if (str != NULL)
922                 connman_element_set_string(&device->element,
923                                         CONNMAN_PROPERTY_ID_TYPE, str);
924
925         device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
926
927         device->type      = type;
928         device->name      = g_strdup(type2description(device->type));
929         device->mode      = CONNMAN_DEVICE_MODE_UNKNOWN;
930         device->secondary = FALSE;
931
932         device->powered_persistent = TRUE;
933
934         device->phyindex = -1;
935
936         switch (type) {
937         case CONNMAN_DEVICE_TYPE_UNKNOWN:
938         case CONNMAN_DEVICE_TYPE_VENDOR:
939                 device->scan_interval = 0;
940                 break;
941         case CONNMAN_DEVICE_TYPE_ETHERNET:
942         case CONNMAN_DEVICE_TYPE_WIFI:
943                 device->scan_interval = 300;
944                 break;
945         case CONNMAN_DEVICE_TYPE_WIMAX:
946                 device->scan_interval = 0;
947                 break;
948         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
949                 device->scan_interval = 0;
950                 break;
951         case CONNMAN_DEVICE_TYPE_GPS:
952                 device->scan_interval = 0;
953                 break;
954         case CONNMAN_DEVICE_TYPE_MBM:
955         case CONNMAN_DEVICE_TYPE_HSO:
956         case CONNMAN_DEVICE_TYPE_NOZOMI:
957         case CONNMAN_DEVICE_TYPE_HUAWEI:
958         case CONNMAN_DEVICE_TYPE_NOVATEL:
959                 device->scan_interval = 0;
960                 break;
961         }
962
963         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
964                                                 g_free, unregister_network);
965
966         return device;
967 }
968
969 /**
970  * connman_device_ref:
971  * @device: device structure
972  *
973  * Increase reference counter of device
974  */
975 struct connman_device *connman_device_ref(struct connman_device *device)
976 {
977         if (connman_element_ref(&device->element) == NULL)
978                 return NULL;
979
980         return device;
981 }
982
983 /**
984  * connman_device_unref:
985  * @device: device structure
986  *
987  * Decrease reference counter of device
988  */
989 void connman_device_unref(struct connman_device *device)
990 {
991         connman_element_unref(&device->element);
992 }
993
994 const char *__connman_device_get_type(struct connman_device *device)
995 {
996         return type2string(device->type);
997 }
998
999 /**
1000  * connman_device_get_type:
1001  * @device: device structure
1002  *
1003  * Get type of device
1004  */
1005 enum connman_device_type connman_device_get_type(struct connman_device *device)
1006 {
1007         return device->type;
1008 }
1009
1010 /**
1011  * connman_device_get_name:
1012  * @device: device structure
1013  *
1014  * Get unique name of device
1015  */
1016 const char *connman_device_get_name(struct connman_device *device)
1017 {
1018         return device->element.name;
1019 }
1020
1021 /**
1022  * connman_device_get_path:
1023  * @device: device structure
1024  *
1025  * Get path name of device
1026  */
1027 const char *connman_device_get_path(struct connman_device *device)
1028 {
1029         return device->element.path;
1030 }
1031
1032 /**
1033  * connman_device_set_index:
1034  * @device: device structure
1035  * @index: index number
1036  *
1037  * Set index number of device
1038  */
1039 void connman_device_set_index(struct connman_device *device, int index)
1040 {
1041         device->element.index = index;
1042
1043         if (device->ipconfig != NULL) {
1044                 if (index == connman_ipconfig_get_index(device->ipconfig))
1045                         return;
1046
1047                 connman_ipconfig_unref(device->ipconfig);
1048         }
1049
1050         device->ipconfig = connman_ipconfig_create(index);
1051 }
1052
1053 /**
1054  * connman_device_get_index:
1055  * @device: device structure
1056  *
1057  * Get index number of device
1058  */
1059 int connman_device_get_index(struct connman_device *device)
1060 {
1061         return device->element.index;
1062 }
1063
1064 int __connman_device_get_phyindex(struct connman_device *device)
1065 {
1066         return device->phyindex;
1067 }
1068
1069 void __connman_device_set_phyindex(struct connman_device *device,
1070                                                         int phyindex)
1071 {
1072         device->phyindex = phyindex;
1073 }
1074
1075 /**
1076  * connman_device_set_interface:
1077  * @device: device structure
1078  * @interface: interface name
1079  * @control: control interface
1080  *
1081  * Set interface name of device
1082  */
1083 void connman_device_set_interface(struct connman_device *device,
1084                                 const char *interface, const char *control)
1085 {
1086         g_free(device->element.devname);
1087         device->element.devname = g_strdup(interface);
1088
1089         g_free(device->interface);
1090         device->interface = g_strdup(interface);
1091
1092         g_free(device->control);
1093         device->control = g_strdup(control);
1094
1095         if (device->name == NULL) {
1096                 const char *str = type2description(device->type);
1097                 if (str != NULL && device->interface != NULL)
1098                         device->name = g_strdup_printf("%s (%s)", str,
1099                                                         device->interface);
1100         }
1101 }
1102
1103 const char *connman_device_get_control(struct connman_device *device)
1104 {
1105         return device->control;
1106 }
1107
1108 /**
1109  * connman_device_set_ident:
1110  * @device: device structure
1111  * @ident: unique identifier
1112  *
1113  * Set unique identifier of device
1114  */
1115 void connman_device_set_ident(struct connman_device *device,
1116                                                         const char *ident)
1117 {
1118         g_free(device->ident);
1119         device->ident = g_strdup(ident);
1120 }
1121
1122 const char *__connman_device_get_ident(struct connman_device *device)
1123 {
1124         return device->ident;
1125 }
1126
1127 /**
1128  * connman_device_set_mode:
1129  * @device: device structure
1130  * @mode: network mode
1131  *
1132  * Change network mode of device
1133  */
1134 void connman_device_set_mode(struct connman_device *device,
1135                                                 enum connman_device_mode mode)
1136 {
1137         device->mode = mode;
1138 }
1139
1140 /**
1141  * connman_device_get_mode:
1142  * @device: device structure
1143  *
1144  * Get network mode of device
1145  */
1146 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1147 {
1148         return device->mode;
1149 }
1150
1151 /**
1152  * connman_device_set_secondary:
1153  * @device: device structure
1154  * @secondary: secondary value
1155  *
1156  * Change secondary value of device
1157  */
1158 void connman_device_set_secondary(struct connman_device *device,
1159                                                 connman_bool_t secondary)
1160 {
1161         device->secondary = secondary;
1162 }
1163
1164 /**
1165  * connman_device_get_secondary:
1166  * @device: device structure
1167  *
1168  * Get secondary value of device
1169  */
1170 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1171 {
1172         return device->secondary;
1173 }
1174
1175 /**
1176  * connman_device_set_powered:
1177  * @device: device structure
1178  * @powered: powered state
1179  *
1180  * Change power state of device
1181  */
1182 int connman_device_set_powered(struct connman_device *device,
1183                                                 connman_bool_t powered)
1184 {
1185         enum connman_service_type type;
1186
1187         DBG("driver %p powered %d", device, powered);
1188
1189         if (device->timeout > 0) {
1190                 g_source_remove(device->timeout);
1191                 device->timeout = 0;
1192         }
1193
1194         if (device->pending != NULL) {
1195                 g_dbus_send_reply(connection, device->pending,
1196                                                         DBUS_TYPE_INVALID);
1197
1198                 dbus_message_unref(device->pending);
1199                 device->pending = NULL;
1200         }
1201
1202         if (device->powered == powered)
1203                 return -EALREADY;
1204
1205         device->powered = powered;
1206         device->powered_pending = powered;
1207
1208         type = __connman_device_get_service_type(device);
1209
1210         if (device->powered == TRUE)
1211                 __connman_notifier_enable(type);
1212         else
1213                 __connman_notifier_disable(type);
1214
1215         if (device->registered == FALSE)
1216                 return 0;
1217
1218         powered_changed(device);
1219
1220         if (powered == FALSE)
1221                 return 0;
1222
1223         reset_scan_trigger(device);
1224
1225         if (device->driver->scan)
1226                 device->driver->scan(device);
1227
1228         return 0;
1229 }
1230
1231 int __connman_device_set_blocked(struct connman_device *device,
1232                                                 connman_bool_t blocked)
1233 {
1234         connman_bool_t powered;
1235
1236         DBG("device %p blocked %d", device, blocked);
1237
1238         device->blocked = blocked;
1239
1240         if (device->offlinemode == TRUE)
1241                 return 0;
1242
1243         if (blocked == FALSE)
1244                 powered = device->powered_persistent;
1245         else
1246                 powered = FALSE;
1247
1248         return set_powered(device, powered);
1249 }
1250
1251 /**
1252  * connman_device_set_carrier:
1253  * @device: device structure
1254  * @carrier: carrier state
1255  *
1256  * Change carrier state of device (only for device without scanning)
1257  */
1258 int connman_device_set_carrier(struct connman_device *device,
1259                                                 connman_bool_t carrier)
1260 {
1261         DBG("device %p carrier %d", device, carrier);
1262
1263         switch (device->mode) {
1264         case CONNMAN_DEVICE_MODE_UNKNOWN:
1265         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1266         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1267                 return -EINVAL;
1268         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1269                 break;
1270         }
1271
1272         if (device->carrier == carrier)
1273                 return -EALREADY;
1274
1275         device->carrier = carrier;
1276
1277         return set_carrier(device, device->carrier);
1278 }
1279
1280 int __connman_device_scan(struct connman_device *device)
1281 {
1282         if (!device->driver || !device->driver->scan)
1283                 return -EOPNOTSUPP;
1284
1285         if (device->powered == FALSE)
1286                 return -ENOLINK;
1287
1288         reset_scan_trigger(device);
1289
1290         return device->driver->scan(device);
1291 }
1292
1293 int __connman_device_enable(struct connman_device *device)
1294 {
1295         enum connman_service_type type;
1296         int err;
1297
1298         DBG("device %p", device);
1299
1300         if (!device->driver || !device->driver->enable)
1301                 return -EOPNOTSUPP;
1302
1303         if (device->powered_pending == TRUE)
1304                 return -EALREADY;
1305
1306         device->powered_pending = TRUE;
1307
1308         err = device->driver->enable(device);
1309         if (err < 0)
1310                 return err;
1311
1312         device->powered = TRUE;
1313
1314         type = __connman_device_get_service_type(device);
1315         __connman_notifier_enable(type);
1316
1317         return 0;
1318 }
1319
1320 int __connman_device_enable_persistent(struct connman_device *device)
1321 {
1322         DBG("device %p", device);
1323
1324         device->powered_persistent = TRUE;
1325
1326         __connman_storage_save_device(device);
1327
1328         return __connman_device_enable(device);
1329 }
1330
1331 int __connman_device_disable(struct connman_device *device)
1332 {
1333         enum connman_service_type type;
1334         int err;
1335
1336         DBG("device %p", device);
1337
1338         if (!device->driver || !device->driver->disable)
1339                 return -EOPNOTSUPP;
1340
1341         if (device->powered == FALSE)
1342                 return -ENOLINK;
1343
1344         if (device->powered_pending == FALSE)
1345                 return -EALREADY;
1346
1347         device->powered_pending = FALSE;
1348
1349         clear_scan_trigger(device);
1350
1351         g_hash_table_remove_all(device->networks);
1352
1353         err = device->driver->disable(device);
1354         if (err < 0)
1355                 return err;
1356
1357         device->powered = FALSE;
1358
1359         type = __connman_device_get_service_type(device);
1360         __connman_notifier_disable(type);
1361
1362         return 0;
1363 }
1364
1365 int __connman_device_disable_persistent(struct connman_device *device)
1366 {
1367         DBG("device %p", device);
1368
1369         device->powered_persistent = FALSE;
1370
1371         __connman_storage_save_device(device);
1372
1373         return __connman_device_disable(device);
1374 }
1375
1376 int __connman_device_connect(struct connman_device *device)
1377 {
1378         DBG("device %p", device);
1379
1380         if (device->disconnected == FALSE)
1381                 return -EINVAL;
1382
1383         if (device->driver && device->driver->connect)
1384                 device->driver->connect(device);
1385
1386         return 0;
1387 }
1388
1389 int __connman_device_disconnect(struct connman_device *device)
1390 {
1391         GHashTableIter iter;
1392         gpointer key, value;
1393
1394         DBG("device %p", device);
1395
1396         connman_device_set_disconnected(device, TRUE);
1397
1398         g_hash_table_iter_init(&iter, device->networks);
1399
1400         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1401                 struct connman_network *network = value;
1402
1403                 __connman_network_disconnect(network);
1404         }
1405
1406         if (device->driver && device->driver->disconnect)
1407                 device->driver->disconnect(device);
1408
1409         return 0;
1410 }
1411
1412 static void mark_network_unavailable(gpointer key, gpointer value,
1413                                                         gpointer user_data)
1414 {
1415         struct connman_network *network = value;
1416
1417         if (connman_network_get_connected(network) == TRUE)
1418                 return;
1419
1420         connman_network_set_available(network, FALSE);
1421 }
1422
1423 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1424                                                         gpointer user_data)
1425 {
1426         struct connman_network *network = value;
1427
1428         if (connman_network_get_connected(network) == TRUE)
1429                 return FALSE;
1430
1431         if (connman_network_get_available(network) == TRUE)
1432                 return FALSE;
1433
1434         return TRUE;
1435 }
1436
1437 void __connman_device_cleanup_networks(struct connman_device *device)
1438 {
1439         g_hash_table_foreach_remove(device->networks,
1440                                         remove_unavailable_network, NULL);
1441 }
1442
1443 /**
1444  * connman_device_set_scanning:
1445  * @device: device structure
1446  * @scanning: scanning state
1447  *
1448  * Change scanning state of device
1449  */
1450 int connman_device_set_scanning(struct connman_device *device,
1451                                                 connman_bool_t scanning)
1452 {
1453         DBusMessage *signal;
1454         DBusMessageIter entry, value;
1455         const char *key = "Scanning";
1456
1457         DBG("device %p scanning %d", device, scanning);
1458
1459         if (!device->driver || !device->driver->scan)
1460                 return -EINVAL;
1461
1462         if (device->scanning == scanning)
1463                 return -EALREADY;
1464
1465         device->scanning = scanning;
1466
1467         signal = dbus_message_new_signal(device->element.path,
1468                                 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1469         if (signal == NULL)
1470                 return 0;
1471
1472         dbus_message_iter_init_append(signal, &entry);
1473
1474         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1475
1476         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1477                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1478         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1479         dbus_message_iter_close_container(&entry, &value);
1480
1481         g_dbus_send_message(connection, signal);
1482
1483         if (scanning == TRUE) {
1484                 reset_scan_trigger(device);
1485
1486                 g_hash_table_foreach(device->networks,
1487                                         mark_network_unavailable, NULL);
1488
1489                 return 0;
1490         }
1491
1492         __connman_device_cleanup_networks(device);
1493
1494         if (device->connections > 0)
1495                 return 0;
1496
1497         if (device->disconnected == TRUE)
1498                 return 0;
1499
1500         __connman_service_auto_connect();
1501
1502         return 0;
1503 }
1504
1505 /**
1506  * connman_device_set_disconnected:
1507  * @device: device structure
1508  * @disconnected: disconnected state
1509  *
1510  * Change disconnected state of device (only for device with networks)
1511  */
1512 int connman_device_set_disconnected(struct connman_device *device,
1513                                                 connman_bool_t disconnected)
1514 {
1515         DBG("device %p disconnected %d", device, disconnected);
1516
1517         switch (device->mode) {
1518         case CONNMAN_DEVICE_MODE_UNKNOWN:
1519         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1520                 return -EINVAL;
1521         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1522         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1523                 break;
1524         }
1525
1526         if (device->disconnected == disconnected)
1527                 return -EALREADY;
1528
1529         device->disconnected = disconnected;
1530
1531         if (disconnected == TRUE)
1532                 force_scan_trigger(device);
1533
1534         return 0;
1535 }
1536
1537 /**
1538  * connman_device_get_disconnected:
1539  * @device: device structure
1540  *
1541  * Get device disconnected state
1542  */
1543 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1544 {
1545         return device->disconnected;
1546 }
1547
1548 /**
1549  * connman_device_set_connected:
1550  * @device: device structure
1551  * @connected: connected state
1552  *
1553  * Change connected state of device (for Ethernet like devices)
1554  */
1555 int connman_device_set_connected(struct connman_device *device,
1556                                                 connman_bool_t connected)
1557 {
1558         DBG("device %p connected %d", device, connected);
1559
1560         switch (device->mode) {
1561         case CONNMAN_DEVICE_MODE_UNKNOWN:
1562         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1563         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1564                 return -EINVAL;
1565         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1566                 break;
1567         }
1568
1569         if (device->carrier == FALSE)
1570                 return -ENOTCONN;
1571
1572         return set_connected(device, connected);
1573 }
1574
1575 /**
1576  * connman_device_set_string:
1577  * @device: device structure
1578  * @key: unique identifier
1579  * @value: string value
1580  *
1581  * Set string value for specific key
1582  */
1583 int connman_device_set_string(struct connman_device *device,
1584                                         const char *key, const char *value)
1585 {
1586         DBG("device %p key %s value %s", device, key, value);
1587
1588         if (g_str_equal(key, "Address") == TRUE) {
1589                 g_free(device->address);
1590                 device->address = g_strdup(value);
1591         } else if (g_str_equal(key, "Name") == TRUE) {
1592                 g_free(device->name);
1593                 device->name = g_strdup(value);
1594         } else if (g_str_equal(key, "Node") == TRUE) {
1595                 g_free(device->node);
1596                 device->node = g_strdup(value);
1597         }
1598
1599         return connman_element_set_string(&device->element, key, value);
1600 }
1601
1602 /**
1603  * connman_device_get_string:
1604  * @device: device structure
1605  * @key: unique identifier
1606  *
1607  * Get string value for specific key
1608  */
1609 const char *connman_device_get_string(struct connman_device *device,
1610                                                         const char *key)
1611 {
1612         DBG("device %p key %s", device, key);
1613
1614         if (g_str_equal(key, "Address") == TRUE)
1615                 return device->address;
1616         else if (g_str_equal(key, "Name") == TRUE)
1617                 return device->name;
1618         else if (g_str_equal(key, "Node") == TRUE)
1619                 return device->node;
1620
1621         return connman_element_get_string(&device->element, key);
1622 }
1623
1624 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1625 {
1626         struct connman_device *device = element->device;
1627         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1628         connman_bool_t powered;
1629
1630         DBG("element %p name %s", element, element->name);
1631
1632         if (device == NULL)
1633                 return;
1634
1635         device->offlinemode = offlinemode;
1636
1637         powered = (offlinemode == TRUE) ? FALSE : TRUE;
1638
1639         if (device->powered == powered)
1640                 return;
1641
1642         if (device->powered_persistent == FALSE)
1643                 powered = FALSE;
1644
1645         set_powered(device, powered);
1646 }
1647
1648 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1649 {
1650         DBG("offlinmode %d", offlinemode);
1651
1652         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1653                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
1654
1655         __connman_notifier_offlinemode(offlinemode);
1656
1657         return 0;
1658 }
1659
1660 void __connman_device_increase_connections(struct connman_device *device)
1661 {
1662         device->connections++;
1663 }
1664
1665 void __connman_device_decrease_connections(struct connman_device *device)
1666 {
1667         device->connections--;
1668 }
1669
1670 /**
1671  * connman_device_add_network:
1672  * @device: device structure
1673  * @network: network structure
1674  *
1675  * Add new network to the device
1676  */
1677 int connman_device_add_network(struct connman_device *device,
1678                                         struct connman_network *network)
1679 {
1680         const char *identifier = connman_network_get_identifier(network);
1681         int err;
1682
1683         DBG("device %p network %p", device, network);
1684
1685         switch (device->mode) {
1686         case CONNMAN_DEVICE_MODE_UNKNOWN:
1687         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1688                 return -EINVAL;
1689         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1690         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1691                 break;
1692         }
1693
1694         __connman_network_set_device(network, device);
1695
1696         err = connman_element_register((struct connman_element *) network,
1697                                                         &device->element);
1698         if (err < 0) {
1699                 __connman_network_set_device(network, NULL);
1700                 return err;
1701         }
1702
1703         g_hash_table_insert(device->networks, g_strdup(identifier),
1704                                                                 network);
1705
1706         return 0;
1707 }
1708
1709 /**
1710  * connman_device_get_network:
1711  * @device: device structure
1712  * @identifier: network identifier
1713  *
1714  * Get network for given identifier
1715  */
1716 struct connman_network *connman_device_get_network(struct connman_device *device,
1717                                                         const char *identifier)
1718 {
1719         DBG("device %p identifier %s", device, identifier);
1720
1721         return g_hash_table_lookup(device->networks, identifier);
1722 }
1723
1724 /**
1725  * connman_device_remove_network:
1726  * @device: device structure
1727  * @identifier: network identifier
1728  *
1729  * Remove network for given identifier
1730  */
1731 int connman_device_remove_network(struct connman_device *device,
1732                                                         const char *identifier)
1733 {
1734         DBG("device %p identifier %s", device, identifier);
1735
1736         g_hash_table_remove(device->networks, identifier);
1737
1738         return 0;
1739 }
1740
1741 void __connman_device_set_network(struct connman_device *device,
1742                                         struct connman_network *network)
1743 {
1744         const char *name;
1745
1746         if (device->network == network)
1747                 return;
1748
1749         if (device->network != NULL)
1750                 connman_network_unref(device->network);
1751
1752         if (network != NULL) {
1753                 name = connman_network_get_string(network,
1754                                                 CONNMAN_PROPERTY_ID_NAME);
1755                 g_free(device->last_network);
1756                 device->last_network = g_strdup(name);
1757
1758                 device->network = connman_network_ref(network);
1759         } else {
1760                 g_free(device->last_network);
1761                 device->last_network = NULL;
1762
1763                 device->network = NULL;
1764         }
1765 }
1766
1767 /**
1768  * connman_device_register:
1769  * @device: device structure
1770  *
1771  * Register device with the system
1772  */
1773 int connman_device_register(struct connman_device *device)
1774 {
1775         __connman_storage_load_device(device);
1776
1777         device->offlinemode = __connman_profile_get_offlinemode();
1778
1779         return connman_element_register(&device->element, NULL);
1780 }
1781
1782 /**
1783  * connman_device_unregister:
1784  * @device: device structure
1785  *
1786  * Unregister device with the system
1787  */
1788 void connman_device_unregister(struct connman_device *device)
1789 {
1790         __connman_storage_save_device(device);
1791
1792         connman_element_unregister(&device->element);
1793 }
1794
1795 /**
1796  * connman_device_get_data:
1797  * @device: device structure
1798  *
1799  * Get private device data pointer
1800  */
1801 void *connman_device_get_data(struct connman_device *device)
1802 {
1803         return device->driver_data;
1804 }
1805
1806 /**
1807  * connman_device_set_data:
1808  * @device: device structure
1809  * @data: data pointer
1810  *
1811  * Set private device data pointer
1812  */
1813 void connman_device_set_data(struct connman_device *device, void *data)
1814 {
1815         device->driver_data = data;
1816 }
1817
1818 static gboolean match_driver(struct connman_device *device,
1819                                         struct connman_device_driver *driver)
1820 {
1821         if (device->type == driver->type ||
1822                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1823                 return TRUE;
1824
1825         return FALSE;
1826 }
1827
1828 static int device_probe(struct connman_element *element)
1829 {
1830         struct connman_device *device = element->device;
1831         GSList *list;
1832
1833         DBG("element %p name %s", element, element->name);
1834
1835         if (device == NULL)
1836                 return -ENODEV;
1837
1838         if (device->driver != NULL)
1839                 return -EALREADY;
1840
1841         for (list = driver_list; list; list = list->next) {
1842                 struct connman_device_driver *driver = list->data;
1843
1844                 if (match_driver(device, driver) == FALSE)
1845                         continue;
1846
1847                 DBG("driver %p name %s", driver, driver->name);
1848
1849                 if (driver->probe(device) == 0) {
1850                         device->driver = driver;
1851                         break;
1852                 }
1853         }
1854
1855         if (device->driver == NULL)
1856                 return -ENODEV;
1857
1858         return setup_device(device);
1859 }
1860
1861 static void device_remove(struct connman_element *element)
1862 {
1863         struct connman_device *device = element->device;
1864
1865         DBG("element %p name %s", element, element->name);
1866
1867         if (device == NULL)
1868                 return;
1869
1870         if (device->driver == NULL)
1871                 return;
1872
1873         remove_device(device);
1874 }
1875
1876 static struct connman_driver device_driver = {
1877         .name           = "device",
1878         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1879         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1880         .probe          = device_probe,
1881         .remove         = device_remove,
1882 };
1883
1884 static int device_load(struct connman_device *device)
1885 {
1886         const char *ident = __connman_profile_active_ident();
1887         GKeyFile *keyfile;
1888         GError *error = NULL;
1889         gchar *identifier;
1890         connman_bool_t powered;
1891         int val;
1892
1893         DBG("device %p", device);
1894
1895         keyfile = __connman_storage_open(ident);
1896         if (keyfile == NULL)
1897                 return 0;
1898
1899         identifier = g_strdup_printf("device_%s", device->element.name);
1900         if (identifier == NULL)
1901                 goto done;
1902
1903         powered = g_key_file_get_boolean(keyfile, identifier,
1904                                                 "Powered", &error);
1905         if (error == NULL)
1906                 device->powered_persistent = powered;
1907         g_clear_error(&error);
1908
1909         switch (device->mode) {
1910         case CONNMAN_DEVICE_MODE_UNKNOWN:
1911         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1912                 break;
1913         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1914         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1915                 val = g_key_file_get_integer(keyfile, identifier,
1916                                                 "ScanInterval", &error);
1917                 if (error == NULL && val > 0)
1918                         device->scan_interval = val;
1919                 g_clear_error(&error);
1920                 break;
1921         }
1922
1923 done:
1924         g_free(identifier);
1925
1926         __connman_storage_close(ident, keyfile, FALSE);
1927
1928         return 0;
1929 }
1930
1931 static int device_save(struct connman_device *device)
1932 {
1933         const char *ident = __connman_profile_active_ident();
1934         GKeyFile *keyfile;
1935         gchar *identifier;
1936
1937         DBG("device %p", device);
1938
1939         keyfile = __connman_storage_open(ident);
1940         if (keyfile == NULL)
1941                 return 0;
1942
1943         identifier = g_strdup_printf("device_%s", device->element.name);
1944         if (identifier == NULL)
1945                 goto done;
1946
1947         g_key_file_set_boolean(keyfile, identifier,
1948                                         "Powered", device->powered_persistent);
1949
1950         switch (device->mode) {
1951         case CONNMAN_DEVICE_MODE_UNKNOWN:
1952         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1953                 break;
1954         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1955         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1956                 if (device->scan_interval > 0)
1957                         g_key_file_set_integer(keyfile, identifier,
1958                                         "ScanInterval", device->scan_interval);
1959                 break;
1960         }
1961
1962 done:
1963         g_free(identifier);
1964
1965         __connman_storage_close(ident, keyfile, TRUE);
1966
1967         return 0;
1968 }
1969
1970 static struct connman_storage device_storage = {
1971         .name           = "device",
1972         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1973         .device_load    = device_load,
1974         .device_save    = device_save,
1975 };
1976
1977 int __connman_device_init(void)
1978 {
1979         DBG("");
1980
1981         connection = connman_dbus_get_connection();
1982
1983         if (connman_storage_register(&device_storage) < 0)
1984                 connman_error("Failed to register device storage");
1985
1986         return connman_driver_register(&device_driver);
1987 }
1988
1989 void __connman_device_cleanup(void)
1990 {
1991         DBG("");
1992
1993         connman_driver_unregister(&device_driver);
1994
1995         connman_storage_unregister(&device_storage);
1996
1997         dbus_connection_unref(connection);
1998 }