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