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