Export devices blocked status
[platform/upstream/connman.git] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection = NULL;
34
35 struct connman_device {
36         struct connman_element element;
37         enum connman_device_type type;
38         enum connman_device_mode mode;
39         connman_bool_t offlinemode;
40         connman_bool_t blocked;
41         connman_bool_t powered;
42         connman_bool_t powered_pending;
43         connman_bool_t powered_persistent;
44         connman_bool_t scanning;
45         connman_bool_t disconnected;
46         connman_bool_t reconnect;
47         connman_uint16_t scan_interval;
48         char *name;
49         char *node;
50         char *address;
51         char *interface;
52         char *control;
53         char *ident;
54         int phyindex;
55         unsigned int connections;
56         guint scan_timeout;
57
58         struct connman_device_driver *driver;
59         void *driver_data;
60
61         connman_bool_t registered;
62
63         char *last_network;
64         struct connman_network *network;
65         GHashTable *networks;
66
67         DBusMessage *pending;
68         guint timeout;
69 };
70
71 static gboolean device_scan_trigger(gpointer user_data)
72 {
73         struct connman_device *device = user_data;
74
75         DBG("device %p", device);
76
77         if (device->driver == NULL) {
78                 device->scan_timeout = 0;
79                 return FALSE;
80         }
81
82         if (device->driver->scan)
83                 device->driver->scan(device);
84
85         return TRUE;
86 }
87
88 static void clear_scan_trigger(struct connman_device *device)
89 {
90         if (device->scan_timeout > 0) {
91                 g_source_remove(device->scan_timeout);
92                 device->scan_timeout = 0;
93         }
94 }
95
96 static void reset_scan_trigger(struct connman_device *device)
97 {
98         clear_scan_trigger(device);
99
100         if (device->scan_interval > 0) {
101                 guint interval = device->scan_interval;
102                 device->scan_timeout = g_timeout_add_seconds(interval,
103                                         device_scan_trigger, device);
104         }
105 }
106
107 static void force_scan_trigger(struct connman_device *device)
108 {
109         clear_scan_trigger(device);
110
111         device->scan_timeout = g_timeout_add_seconds(5,
112                                         device_scan_trigger, device);
113 }
114
115 void connman_device_schedule_scan(struct connman_device *device)
116 {
117         reset_scan_trigger(device);
118 }
119
120 static const char *type2description(enum connman_device_type type)
121 {
122         switch (type) {
123         case CONNMAN_DEVICE_TYPE_UNKNOWN:
124         case CONNMAN_DEVICE_TYPE_VENDOR:
125                 break;
126         case CONNMAN_DEVICE_TYPE_ETHERNET:
127                 return "Ethernet";
128         case CONNMAN_DEVICE_TYPE_WIFI:
129                 return "Wireless";
130         case CONNMAN_DEVICE_TYPE_WIMAX:
131                 return "WiMAX";
132         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
133                 return "Bluetooth";
134         case CONNMAN_DEVICE_TYPE_GPS:
135                 return "GPS";
136         case CONNMAN_DEVICE_TYPE_CELLULAR:
137                 return "Cellular";
138         }
139
140         return NULL;
141 }
142
143 static const char *type2string(enum connman_device_type type)
144 {
145         switch (type) {
146         case CONNMAN_DEVICE_TYPE_UNKNOWN:
147         case CONNMAN_DEVICE_TYPE_VENDOR:
148                 break;
149         case CONNMAN_DEVICE_TYPE_ETHERNET:
150                 return "ethernet";
151         case CONNMAN_DEVICE_TYPE_WIFI:
152                 return "wifi";
153         case CONNMAN_DEVICE_TYPE_WIMAX:
154                 return "wimax";
155         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
156                 return "bluetooth";
157         case CONNMAN_DEVICE_TYPE_GPS:
158                 return "gps";
159         case CONNMAN_DEVICE_TYPE_CELLULAR:
160                 return "cellular";
161         }
162
163         return NULL;
164 }
165
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
167 {
168         enum connman_device_type type = connman_device_get_type(device);
169
170         switch (type) {
171         case CONNMAN_DEVICE_TYPE_UNKNOWN:
172         case CONNMAN_DEVICE_TYPE_VENDOR:
173         case CONNMAN_DEVICE_TYPE_GPS:
174                 break;
175         case CONNMAN_DEVICE_TYPE_ETHERNET:
176                 return CONNMAN_SERVICE_TYPE_ETHERNET;
177         case CONNMAN_DEVICE_TYPE_WIFI:
178                 return CONNMAN_SERVICE_TYPE_WIFI;
179         case CONNMAN_DEVICE_TYPE_WIMAX:
180                 return CONNMAN_SERVICE_TYPE_WIMAX;
181         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183         case CONNMAN_DEVICE_TYPE_CELLULAR:
184                 return CONNMAN_SERVICE_TYPE_CELLULAR;
185         }
186
187         return CONNMAN_SERVICE_TYPE_UNKNOWN;
188 }
189
190 static void powered_changed(struct connman_device *device)
191 {
192         connman_dbus_property_changed_basic(device->element.path,
193                                 CONNMAN_DEVICE_INTERFACE, "Powered",
194                                         DBUS_TYPE_BOOLEAN, &device->powered);
195 }
196
197 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 int __connman_device_scan(struct connman_device *device)
1066 {
1067         if (!device->driver || !device->driver->scan)
1068                 return -EOPNOTSUPP;
1069
1070         if (device->powered == FALSE)
1071                 return -ENOLINK;
1072
1073         reset_scan_trigger(device);
1074
1075         return device->driver->scan(device);
1076 }
1077
1078 int __connman_device_enable_persistent(struct connman_device *device)
1079 {
1080         int err;
1081
1082         DBG("device %p", device);
1083
1084         device->powered_persistent = TRUE;
1085
1086         __connman_storage_save_device(device);
1087
1088         err = __connman_device_enable(device);
1089         if (err == 0 || err == -EINPROGRESS) {
1090                 if (__connman_profile_get_offlinemode() == TRUE) {
1091                         device->offlinemode = FALSE;
1092                         __connman_profile_set_offlinemode(FALSE, FALSE);
1093                 }
1094
1095         }
1096
1097         return err;
1098 }
1099
1100 int __connman_device_disable_persistent(struct connman_device *device)
1101 {
1102         DBG("device %p", device);
1103
1104         device->powered_persistent = FALSE;
1105
1106         __connman_storage_save_device(device);
1107
1108         return __connman_device_disable(device);
1109 }
1110
1111 int __connman_device_disconnect(struct connman_device *device)
1112 {
1113         GHashTableIter iter;
1114         gpointer key, value;
1115
1116         DBG("device %p", device);
1117
1118         connman_device_set_disconnected(device, TRUE);
1119
1120         g_hash_table_iter_init(&iter, device->networks);
1121
1122         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1123                 struct connman_network *network = value;
1124
1125                 if (__connman_network_get_connecting(network) == TRUE) {
1126                         /*
1127                          * Skip network in the process of connecting.
1128                          * This is a workaround for WiFi networks serviced
1129                          * by the supplicant plugin that hold a reference
1130                          * to the network.  If we disconnect the network
1131                          * here then the referenced object will not be
1132                          * registered and usage (like launching DHCP client)
1133                          * will fail.  There is nothing to be gained by
1134                          * removing the network here anyway.
1135                          */
1136                         connman_warn("Skipping disconnect of %s",
1137                                 connman_network_get_identifier(network));
1138                         continue;
1139                 }
1140
1141                 __connman_network_disconnect(network);
1142         }
1143
1144         return 0;
1145 }
1146
1147 static void mark_network_unavailable(gpointer key, gpointer value,
1148                                                         gpointer user_data)
1149 {
1150         struct connman_network *network = value;
1151
1152         if (connman_network_get_connected(network) == TRUE)
1153                 return;
1154
1155         connman_network_set_available(network, FALSE);
1156 }
1157
1158 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1159                                                         gpointer user_data)
1160 {
1161         struct connman_network *network = value;
1162
1163         if (connman_network_get_connected(network) == TRUE)
1164                 return FALSE;
1165
1166         if (connman_network_get_available(network) == TRUE)
1167                 return FALSE;
1168
1169         return TRUE;
1170 }
1171
1172 void __connman_device_cleanup_networks(struct connman_device *device)
1173 {
1174         g_hash_table_foreach_remove(device->networks,
1175                                         remove_unavailable_network, NULL);
1176 }
1177
1178 static void scanning_changed(struct connman_device *device)
1179 {
1180         connman_dbus_property_changed_basic(device->element.path,
1181                                 CONNMAN_DEVICE_INTERFACE, "Scanning",
1182                                         DBUS_TYPE_BOOLEAN, &device->scanning);
1183 }
1184
1185 /**
1186  * connman_device_set_scanning:
1187  * @device: device structure
1188  * @scanning: scanning state
1189  *
1190  * Change scanning state of device
1191  */
1192 int connman_device_set_scanning(struct connman_device *device,
1193                                                 connman_bool_t scanning)
1194 {
1195         DBG("device %p scanning %d", device, scanning);
1196
1197         if (!device->driver || !device->driver->scan)
1198                 return -EINVAL;
1199
1200         if (device->scanning == scanning)
1201                 return -EALREADY;
1202
1203         device->scanning = scanning;
1204
1205         scanning_changed(device);
1206
1207         if (scanning == TRUE) {
1208                 reset_scan_trigger(device);
1209
1210                 g_hash_table_foreach(device->networks,
1211                                         mark_network_unavailable, NULL);
1212
1213                 return 0;
1214         }
1215
1216         __connman_device_cleanup_networks(device);
1217
1218         if (device->connections > 0)
1219                 return 0;
1220
1221         if (device->disconnected == TRUE)
1222                 return 0;
1223
1224         __connman_service_auto_connect();
1225
1226         return 0;
1227 }
1228
1229 /**
1230  * connman_device_set_disconnected:
1231  * @device: device structure
1232  * @disconnected: disconnected state
1233  *
1234  * Change disconnected state of device (only for device with networks)
1235  */
1236 int connman_device_set_disconnected(struct connman_device *device,
1237                                                 connman_bool_t disconnected)
1238 {
1239         DBG("device %p disconnected %d", device, disconnected);
1240
1241         switch (device->mode) {
1242         case CONNMAN_DEVICE_MODE_UNKNOWN:
1243                 return -EINVAL;
1244         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1245         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1246                 break;
1247         }
1248
1249         if (device->disconnected == disconnected)
1250                 return -EALREADY;
1251
1252         device->disconnected = disconnected;
1253
1254         if (disconnected == TRUE)
1255                 force_scan_trigger(device);
1256
1257         return 0;
1258 }
1259
1260 /**
1261  * connman_device_get_disconnected:
1262  * @device: device structure
1263  *
1264  * Get device disconnected state
1265  */
1266 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1267 {
1268         return device->disconnected;
1269 }
1270
1271 /**
1272  * connman_device_set_string:
1273  * @device: device structure
1274  * @key: unique identifier
1275  * @value: string value
1276  *
1277  * Set string value for specific key
1278  */
1279 int connman_device_set_string(struct connman_device *device,
1280                                         const char *key, const char *value)
1281 {
1282         DBG("device %p key %s value %s", device, key, value);
1283
1284         if (g_str_equal(key, "Address") == TRUE) {
1285                 g_free(device->address);
1286                 device->address = g_strdup(value);
1287         } else if (g_str_equal(key, "Name") == TRUE) {
1288                 g_free(device->name);
1289                 device->name = g_strdup(value);
1290         } else if (g_str_equal(key, "Node") == TRUE) {
1291                 g_free(device->node);
1292                 device->node = g_strdup(value);
1293         }
1294
1295         return connman_element_set_string(&device->element, key, value);
1296 }
1297
1298 /**
1299  * connman_device_get_string:
1300  * @device: device structure
1301  * @key: unique identifier
1302  *
1303  * Get string value for specific key
1304  */
1305 const char *connman_device_get_string(struct connman_device *device,
1306                                                         const char *key)
1307 {
1308         DBG("device %p key %s", device, key);
1309
1310         if (g_str_equal(key, "Address") == TRUE)
1311                 return device->address;
1312         else if (g_str_equal(key, "Name") == TRUE)
1313                 return device->name;
1314         else if (g_str_equal(key, "Node") == TRUE)
1315                 return device->node;
1316
1317         return connman_element_get_string(&device->element, key);
1318 }
1319
1320 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1321 {
1322         struct connman_device *device = element->device;
1323         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1324         connman_bool_t powered;
1325
1326         DBG("element %p name %s", element, element->name);
1327
1328         if (device == NULL)
1329                 return;
1330
1331         device->offlinemode = offlinemode;
1332
1333         powered = (offlinemode == TRUE) ? FALSE : TRUE;
1334
1335         if (device->powered == powered)
1336                 return;
1337
1338         if (device->powered_persistent == FALSE)
1339                 powered = FALSE;
1340
1341         set_powered(device, powered);
1342 }
1343
1344 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1345 {
1346         DBG("offlinmode %d", offlinemode);
1347
1348         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1349                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
1350
1351         __connman_notifier_offlinemode(offlinemode);
1352
1353         return 0;
1354 }
1355
1356 void __connman_device_increase_connections(struct connman_device *device)
1357 {
1358         device->connections++;
1359 }
1360
1361 void __connman_device_decrease_connections(struct connman_device *device)
1362 {
1363         device->connections--;
1364 }
1365
1366 /**
1367  * connman_device_add_network:
1368  * @device: device structure
1369  * @network: network structure
1370  *
1371  * Add new network to the device
1372  */
1373 int connman_device_add_network(struct connman_device *device,
1374                                         struct connman_network *network)
1375 {
1376         const char *identifier = connman_network_get_identifier(network);
1377         int err;
1378
1379         DBG("device %p network %p", device, network);
1380
1381         if (identifier == NULL)
1382                 return -EINVAL;
1383
1384         switch (device->mode) {
1385         case CONNMAN_DEVICE_MODE_UNKNOWN:
1386                 return -EINVAL;
1387         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1388         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1389                 break;
1390         }
1391
1392         __connman_network_set_device(network, device);
1393
1394         err = connman_element_register((struct connman_element *) network,
1395                                                         &device->element);
1396         if (err < 0) {
1397                 __connman_network_set_device(network, NULL);
1398                 return err;
1399         }
1400
1401         g_hash_table_insert(device->networks, g_strdup(identifier),
1402                                                                 network);
1403
1404         return 0;
1405 }
1406
1407 /**
1408  * connman_device_get_network:
1409  * @device: device structure
1410  * @identifier: network identifier
1411  *
1412  * Get network for given identifier
1413  */
1414 struct connman_network *connman_device_get_network(struct connman_device *device,
1415                                                         const char *identifier)
1416 {
1417         DBG("device %p identifier %s", device, identifier);
1418
1419         return g_hash_table_lookup(device->networks, identifier);
1420 }
1421
1422 /**
1423  * connman_device_remove_network:
1424  * @device: device structure
1425  * @identifier: network identifier
1426  *
1427  * Remove network for given identifier
1428  */
1429 int connman_device_remove_network(struct connman_device *device,
1430                                                         const char *identifier)
1431 {
1432         DBG("device %p identifier %s", device, identifier);
1433
1434         g_hash_table_remove(device->networks, identifier);
1435
1436         return 0;
1437 }
1438
1439 void connman_device_remove_all_networks(struct connman_device *device)
1440 {
1441         g_hash_table_remove_all(device->networks);
1442 }
1443
1444 void __connman_device_set_network(struct connman_device *device,
1445                                         struct connman_network *network)
1446 {
1447         const char *name;
1448
1449         if (device == NULL)
1450                 return;
1451
1452         if (device->network == network)
1453                 return;
1454
1455         if (device->network != NULL)
1456                 connman_network_unref(device->network);
1457
1458         if (network != NULL) {
1459                 name = connman_network_get_string(network,
1460                                                 CONNMAN_PROPERTY_ID_NAME);
1461                 g_free(device->last_network);
1462                 device->last_network = g_strdup(name);
1463
1464                 device->network = connman_network_ref(network);
1465         } else {
1466                 g_free(device->last_network);
1467                 device->last_network = NULL;
1468
1469                 device->network = NULL;
1470         }
1471 }
1472
1473 void __connman_device_set_reconnect(struct connman_device *device,
1474                                                 connman_bool_t reconnect)
1475 {
1476         device->reconnect = reconnect;
1477 }
1478
1479 connman_bool_t  __connman_device_get_reconnect(
1480                                 struct connman_device *device)
1481 {
1482         return device->reconnect;
1483 }
1484
1485 /**
1486  * connman_device_register:
1487  * @device: device structure
1488  *
1489  * Register device with the system
1490  */
1491 int connman_device_register(struct connman_device *device)
1492 {
1493         __connman_storage_load_device(device);
1494
1495         device->offlinemode = __connman_profile_get_offlinemode();
1496
1497         return connman_element_register(&device->element, NULL);
1498 }
1499
1500 /**
1501  * connman_device_unregister:
1502  * @device: device structure
1503  *
1504  * Unregister device with the system
1505  */
1506 void connman_device_unregister(struct connman_device *device)
1507 {
1508         __connman_storage_save_device(device);
1509
1510         connman_element_unregister(&device->element);
1511 }
1512
1513 /**
1514  * connman_device_get_data:
1515  * @device: device structure
1516  *
1517  * Get private device data pointer
1518  */
1519 void *connman_device_get_data(struct connman_device *device)
1520 {
1521         return device->driver_data;
1522 }
1523
1524 /**
1525  * connman_device_set_data:
1526  * @device: device structure
1527  * @data: data pointer
1528  *
1529  * Set private device data pointer
1530  */
1531 void connman_device_set_data(struct connman_device *device, void *data)
1532 {
1533         device->driver_data = data;
1534 }
1535
1536 static gboolean match_driver(struct connman_device *device,
1537                                         struct connman_device_driver *driver)
1538 {
1539         if (device->type == driver->type ||
1540                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1541                 return TRUE;
1542
1543         return FALSE;
1544 }
1545
1546 static int device_probe(struct connman_element *element)
1547 {
1548         struct connman_device *device = element->device;
1549         GSList *list;
1550
1551         DBG("element %p name %s", element, element->name);
1552
1553         if (device == NULL)
1554                 return -ENODEV;
1555
1556         if (device->driver != NULL)
1557                 return -EALREADY;
1558
1559         for (list = driver_list; list; list = list->next) {
1560                 struct connman_device_driver *driver = list->data;
1561
1562                 if (match_driver(device, driver) == FALSE)
1563                         continue;
1564
1565                 DBG("driver %p name %s", driver, driver->name);
1566
1567                 if (driver->probe(device) == 0) {
1568                         device->driver = driver;
1569                         break;
1570                 }
1571         }
1572
1573         if (device->driver == NULL)
1574                 return -ENODEV;
1575
1576         return setup_device(device);
1577 }
1578
1579 static void device_remove(struct connman_element *element)
1580 {
1581         struct connman_device *device = element->device;
1582
1583         DBG("element %p name %s", element, element->name);
1584
1585         if (device == NULL)
1586                 return;
1587
1588         if (device->driver == NULL)
1589                 return;
1590
1591         remove_device(device);
1592 }
1593
1594 static struct connman_driver device_driver = {
1595         .name           = "device",
1596         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1597         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1598         .probe          = device_probe,
1599         .remove         = device_remove,
1600 };
1601
1602 static int device_load(struct connman_device *device)
1603 {
1604         const char *ident = __connman_profile_active_ident();
1605         GKeyFile *keyfile;
1606         GError *error = NULL;
1607         gchar *identifier;
1608         connman_bool_t powered;
1609         int val;
1610
1611         DBG("device %p", device);
1612
1613         keyfile = __connman_storage_open_profile(ident);
1614         if (keyfile == NULL)
1615                 return 0;
1616
1617         identifier = g_strdup_printf("device_%s", device->element.name);
1618         if (identifier == NULL)
1619                 goto done;
1620
1621         powered = g_key_file_get_boolean(keyfile, identifier,
1622                                                 "Powered", &error);
1623         if (error == NULL)
1624                 device->powered_persistent = powered;
1625         g_clear_error(&error);
1626
1627         switch (device->mode) {
1628         case CONNMAN_DEVICE_MODE_UNKNOWN:
1629                 break;
1630         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1631         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1632                 val = g_key_file_get_integer(keyfile, identifier,
1633                                                 "ScanInterval", &error);
1634                 if (error == NULL && val > 0)
1635                         device->scan_interval = val;
1636                 g_clear_error(&error);
1637                 break;
1638         }
1639
1640 done:
1641         g_free(identifier);
1642
1643         __connman_storage_close_profile(ident, keyfile, FALSE);
1644
1645         return 0;
1646 }
1647
1648 static int device_save(struct connman_device *device)
1649 {
1650         const char *ident = __connman_profile_active_ident();
1651         GKeyFile *keyfile;
1652         gchar *identifier;
1653
1654         DBG("device %p", device);
1655
1656         keyfile = __connman_storage_open_profile(ident);
1657         if (keyfile == NULL)
1658                 return 0;
1659
1660         identifier = g_strdup_printf("device_%s", device->element.name);
1661         if (identifier == NULL)
1662                 goto done;
1663
1664         g_key_file_set_boolean(keyfile, identifier,
1665                                         "Powered", device->powered_persistent);
1666
1667         switch (device->mode) {
1668         case CONNMAN_DEVICE_MODE_UNKNOWN:
1669                 break;
1670         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1671         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1672                 if (device->scan_interval > 0)
1673                         g_key_file_set_integer(keyfile, identifier,
1674                                         "ScanInterval", device->scan_interval);
1675                 break;
1676         }
1677
1678 done:
1679         g_free(identifier);
1680
1681         __connman_storage_close_profile(ident, keyfile, TRUE);
1682
1683         return 0;
1684 }
1685
1686 static struct connman_storage device_storage = {
1687         .name           = "device",
1688         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1689         .device_load    = device_load,
1690         .device_save    = device_save,
1691 };
1692
1693 int __connman_device_init(void)
1694 {
1695         DBG("");
1696
1697         connection = connman_dbus_get_connection();
1698
1699         if (connman_storage_register(&device_storage) < 0)
1700                 connman_error("Failed to register device storage");
1701
1702         return connman_driver_register(&device_driver);
1703 }
1704
1705 void __connman_device_cleanup(void)
1706 {
1707         DBG("");
1708
1709         connman_driver_unregister(&device_driver);
1710
1711         connman_storage_unregister(&device_storage);
1712
1713         dbus_connection_unref(connection);
1714 }