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