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