Add BLOCKED technology state
[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                 if (__connman_profile_get_offlinemode() == TRUE) {
1096                         device->offlinemode = FALSE;
1097                         __connman_profile_set_offlinemode(FALSE, FALSE);
1098                 }
1099
1100         }
1101
1102         return err;
1103 }
1104
1105 int __connman_device_disable_persistent(struct connman_device *device)
1106 {
1107         DBG("device %p", device);
1108
1109         device->powered_persistent = FALSE;
1110
1111         __connman_storage_save_device(device);
1112
1113         return __connman_device_disable(device);
1114 }
1115
1116 int __connman_device_disconnect(struct connman_device *device)
1117 {
1118         GHashTableIter iter;
1119         gpointer key, value;
1120
1121         DBG("device %p", device);
1122
1123         connman_device_set_disconnected(device, TRUE);
1124
1125         g_hash_table_iter_init(&iter, device->networks);
1126
1127         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1128                 struct connman_network *network = value;
1129
1130                 if (__connman_network_get_connecting(network) == TRUE) {
1131                         /*
1132                          * Skip network in the process of connecting.
1133                          * This is a workaround for WiFi networks serviced
1134                          * by the supplicant plugin that hold a reference
1135                          * to the network.  If we disconnect the network
1136                          * here then the referenced object will not be
1137                          * registered and usage (like launching DHCP client)
1138                          * will fail.  There is nothing to be gained by
1139                          * removing the network here anyway.
1140                          */
1141                         connman_warn("Skipping disconnect of %s",
1142                                 connman_network_get_identifier(network));
1143                         continue;
1144                 }
1145
1146                 __connman_network_disconnect(network);
1147         }
1148
1149         return 0;
1150 }
1151
1152 static void mark_network_unavailable(gpointer key, gpointer value,
1153                                                         gpointer user_data)
1154 {
1155         struct connman_network *network = value;
1156
1157         if (connman_network_get_connected(network) == TRUE)
1158                 return;
1159
1160         connman_network_set_available(network, FALSE);
1161 }
1162
1163 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1164                                                         gpointer user_data)
1165 {
1166         struct connman_network *network = value;
1167
1168         if (connman_network_get_connected(network) == TRUE)
1169                 return FALSE;
1170
1171         if (connman_network_get_available(network) == TRUE)
1172                 return FALSE;
1173
1174         return TRUE;
1175 }
1176
1177 void __connman_device_cleanup_networks(struct connman_device *device)
1178 {
1179         g_hash_table_foreach_remove(device->networks,
1180                                         remove_unavailable_network, NULL);
1181 }
1182
1183 static void scanning_changed(struct connman_device *device)
1184 {
1185         connman_dbus_property_changed_basic(device->element.path,
1186                                 CONNMAN_DEVICE_INTERFACE, "Scanning",
1187                                         DBUS_TYPE_BOOLEAN, &device->scanning);
1188 }
1189
1190 /**
1191  * connman_device_set_scanning:
1192  * @device: device structure
1193  * @scanning: scanning state
1194  *
1195  * Change scanning state of device
1196  */
1197 int connman_device_set_scanning(struct connman_device *device,
1198                                                 connman_bool_t scanning)
1199 {
1200         DBG("device %p scanning %d", device, scanning);
1201
1202         if (!device->driver || !device->driver->scan)
1203                 return -EINVAL;
1204
1205         if (device->scanning == scanning)
1206                 return -EALREADY;
1207
1208         device->scanning = scanning;
1209
1210         scanning_changed(device);
1211
1212         if (scanning == TRUE) {
1213                 reset_scan_trigger(device);
1214
1215                 g_hash_table_foreach(device->networks,
1216                                         mark_network_unavailable, NULL);
1217
1218                 return 0;
1219         }
1220
1221         __connman_device_cleanup_networks(device);
1222
1223         if (device->connections > 0)
1224                 return 0;
1225
1226         if (device->disconnected == TRUE)
1227                 return 0;
1228
1229         __connman_service_auto_connect();
1230
1231         return 0;
1232 }
1233
1234 /**
1235  * connman_device_set_disconnected:
1236  * @device: device structure
1237  * @disconnected: disconnected state
1238  *
1239  * Change disconnected state of device (only for device with networks)
1240  */
1241 int connman_device_set_disconnected(struct connman_device *device,
1242                                                 connman_bool_t disconnected)
1243 {
1244         DBG("device %p disconnected %d", device, disconnected);
1245
1246         switch (device->mode) {
1247         case CONNMAN_DEVICE_MODE_UNKNOWN:
1248                 return -EINVAL;
1249         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1250         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1251                 break;
1252         }
1253
1254         if (device->disconnected == disconnected)
1255                 return -EALREADY;
1256
1257         device->disconnected = disconnected;
1258
1259         if (disconnected == TRUE)
1260                 force_scan_trigger(device);
1261
1262         return 0;
1263 }
1264
1265 /**
1266  * connman_device_get_disconnected:
1267  * @device: device structure
1268  *
1269  * Get device disconnected state
1270  */
1271 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1272 {
1273         return device->disconnected;
1274 }
1275
1276 /**
1277  * connman_device_set_string:
1278  * @device: device structure
1279  * @key: unique identifier
1280  * @value: string value
1281  *
1282  * Set string value for specific key
1283  */
1284 int connman_device_set_string(struct connman_device *device,
1285                                         const char *key, const char *value)
1286 {
1287         DBG("device %p key %s value %s", device, key, value);
1288
1289         if (g_str_equal(key, "Address") == TRUE) {
1290                 g_free(device->address);
1291                 device->address = g_strdup(value);
1292         } else if (g_str_equal(key, "Name") == TRUE) {
1293                 g_free(device->name);
1294                 device->name = g_strdup(value);
1295         } else if (g_str_equal(key, "Node") == TRUE) {
1296                 g_free(device->node);
1297                 device->node = g_strdup(value);
1298         }
1299
1300         return connman_element_set_string(&device->element, key, value);
1301 }
1302
1303 /**
1304  * connman_device_get_string:
1305  * @device: device structure
1306  * @key: unique identifier
1307  *
1308  * Get string value for specific key
1309  */
1310 const char *connman_device_get_string(struct connman_device *device,
1311                                                         const char *key)
1312 {
1313         DBG("device %p key %s", device, key);
1314
1315         if (g_str_equal(key, "Address") == TRUE)
1316                 return device->address;
1317         else if (g_str_equal(key, "Name") == TRUE)
1318                 return device->name;
1319         else if (g_str_equal(key, "Node") == TRUE)
1320                 return device->node;
1321
1322         return connman_element_get_string(&device->element, key);
1323 }
1324
1325 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1326 {
1327         struct connman_device *device = element->device;
1328         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1329         connman_bool_t powered;
1330
1331         DBG("element %p name %s", element, element->name);
1332
1333         if (device == NULL)
1334                 return;
1335
1336         device->offlinemode = offlinemode;
1337
1338         powered = (offlinemode == TRUE) ? FALSE : TRUE;
1339
1340         if (device->powered == powered)
1341                 return;
1342
1343         if (device->powered_persistent == FALSE)
1344                 powered = FALSE;
1345
1346         set_powered(device, powered);
1347 }
1348
1349 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1350 {
1351         DBG("offlinmode %d", offlinemode);
1352
1353         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1354                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
1355
1356         __connman_notifier_offlinemode(offlinemode);
1357
1358         return 0;
1359 }
1360
1361 void __connman_device_increase_connections(struct connman_device *device)
1362 {
1363         device->connections++;
1364 }
1365
1366 void __connman_device_decrease_connections(struct connman_device *device)
1367 {
1368         device->connections--;
1369 }
1370
1371 /**
1372  * connman_device_add_network:
1373  * @device: device structure
1374  * @network: network structure
1375  *
1376  * Add new network to the device
1377  */
1378 int connman_device_add_network(struct connman_device *device,
1379                                         struct connman_network *network)
1380 {
1381         const char *identifier = connman_network_get_identifier(network);
1382         int err;
1383
1384         DBG("device %p network %p", device, network);
1385
1386         if (identifier == NULL)
1387                 return -EINVAL;
1388
1389         switch (device->mode) {
1390         case CONNMAN_DEVICE_MODE_UNKNOWN:
1391                 return -EINVAL;
1392         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1393         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1394                 break;
1395         }
1396
1397         __connman_network_set_device(network, device);
1398
1399         err = connman_element_register((struct connman_element *) network,
1400                                                         &device->element);
1401         if (err < 0) {
1402                 __connman_network_set_device(network, NULL);
1403                 return err;
1404         }
1405
1406         g_hash_table_insert(device->networks, g_strdup(identifier),
1407                                                                 network);
1408
1409         return 0;
1410 }
1411
1412 /**
1413  * connman_device_get_network:
1414  * @device: device structure
1415  * @identifier: network identifier
1416  *
1417  * Get network for given identifier
1418  */
1419 struct connman_network *connman_device_get_network(struct connman_device *device,
1420                                                         const char *identifier)
1421 {
1422         DBG("device %p identifier %s", device, identifier);
1423
1424         return g_hash_table_lookup(device->networks, identifier);
1425 }
1426
1427 /**
1428  * connman_device_remove_network:
1429  * @device: device structure
1430  * @identifier: network identifier
1431  *
1432  * Remove network for given identifier
1433  */
1434 int connman_device_remove_network(struct connman_device *device,
1435                                                         const char *identifier)
1436 {
1437         DBG("device %p identifier %s", device, identifier);
1438
1439         g_hash_table_remove(device->networks, identifier);
1440
1441         return 0;
1442 }
1443
1444 void connman_device_remove_all_networks(struct connman_device *device)
1445 {
1446         g_hash_table_remove_all(device->networks);
1447 }
1448
1449 void __connman_device_set_network(struct connman_device *device,
1450                                         struct connman_network *network)
1451 {
1452         const char *name;
1453
1454         if (device == NULL)
1455                 return;
1456
1457         if (device->network == network)
1458                 return;
1459
1460         if (device->network != NULL)
1461                 connman_network_unref(device->network);
1462
1463         if (network != NULL) {
1464                 name = connman_network_get_string(network,
1465                                                 CONNMAN_PROPERTY_ID_NAME);
1466                 g_free(device->last_network);
1467                 device->last_network = g_strdup(name);
1468
1469                 device->network = connman_network_ref(network);
1470         } else {
1471                 g_free(device->last_network);
1472                 device->last_network = NULL;
1473
1474                 device->network = NULL;
1475         }
1476 }
1477
1478 void __connman_device_set_reconnect(struct connman_device *device,
1479                                                 connman_bool_t reconnect)
1480 {
1481         device->reconnect = reconnect;
1482 }
1483
1484 connman_bool_t  __connman_device_get_reconnect(
1485                                 struct connman_device *device)
1486 {
1487         return device->reconnect;
1488 }
1489
1490 /**
1491  * connman_device_register:
1492  * @device: device structure
1493  *
1494  * Register device with the system
1495  */
1496 int connman_device_register(struct connman_device *device)
1497 {
1498         __connman_storage_load_device(device);
1499
1500         device->offlinemode = __connman_profile_get_offlinemode();
1501
1502         return connman_element_register(&device->element, NULL);
1503 }
1504
1505 /**
1506  * connman_device_unregister:
1507  * @device: device structure
1508  *
1509  * Unregister device with the system
1510  */
1511 void connman_device_unregister(struct connman_device *device)
1512 {
1513         __connman_storage_save_device(device);
1514
1515         connman_element_unregister(&device->element);
1516 }
1517
1518 /**
1519  * connman_device_get_data:
1520  * @device: device structure
1521  *
1522  * Get private device data pointer
1523  */
1524 void *connman_device_get_data(struct connman_device *device)
1525 {
1526         return device->driver_data;
1527 }
1528
1529 /**
1530  * connman_device_set_data:
1531  * @device: device structure
1532  * @data: data pointer
1533  *
1534  * Set private device data pointer
1535  */
1536 void connman_device_set_data(struct connman_device *device, void *data)
1537 {
1538         device->driver_data = data;
1539 }
1540
1541 static gboolean match_driver(struct connman_device *device,
1542                                         struct connman_device_driver *driver)
1543 {
1544         if (device->type == driver->type ||
1545                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1546                 return TRUE;
1547
1548         return FALSE;
1549 }
1550
1551 static int device_probe(struct connman_element *element)
1552 {
1553         struct connman_device *device = element->device;
1554         GSList *list;
1555
1556         DBG("element %p name %s", element, element->name);
1557
1558         if (device == NULL)
1559                 return -ENODEV;
1560
1561         if (device->driver != NULL)
1562                 return -EALREADY;
1563
1564         for (list = driver_list; list; list = list->next) {
1565                 struct connman_device_driver *driver = list->data;
1566
1567                 if (match_driver(device, driver) == FALSE)
1568                         continue;
1569
1570                 DBG("driver %p name %s", driver, driver->name);
1571
1572                 if (driver->probe(device) == 0) {
1573                         device->driver = driver;
1574                         break;
1575                 }
1576         }
1577
1578         if (device->driver == NULL)
1579                 return -ENODEV;
1580
1581         return setup_device(device);
1582 }
1583
1584 static void device_remove(struct connman_element *element)
1585 {
1586         struct connman_device *device = element->device;
1587
1588         DBG("element %p name %s", element, element->name);
1589
1590         if (device == NULL)
1591                 return;
1592
1593         if (device->driver == NULL)
1594                 return;
1595
1596         remove_device(device);
1597 }
1598
1599 static struct connman_driver device_driver = {
1600         .name           = "device",
1601         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1602         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1603         .probe          = device_probe,
1604         .remove         = device_remove,
1605 };
1606
1607 static int device_load(struct connman_device *device)
1608 {
1609         const char *ident = __connman_profile_active_ident();
1610         GKeyFile *keyfile;
1611         GError *error = NULL;
1612         gchar *identifier;
1613         connman_bool_t powered;
1614         int val;
1615
1616         DBG("device %p", device);
1617
1618         keyfile = __connman_storage_open_profile(ident);
1619         if (keyfile == NULL)
1620                 return 0;
1621
1622         identifier = g_strdup_printf("device_%s", device->element.name);
1623         if (identifier == NULL)
1624                 goto done;
1625
1626         powered = g_key_file_get_boolean(keyfile, identifier,
1627                                                 "Powered", &error);
1628         if (error == NULL)
1629                 device->powered_persistent = powered;
1630         g_clear_error(&error);
1631
1632         switch (device->mode) {
1633         case CONNMAN_DEVICE_MODE_UNKNOWN:
1634                 break;
1635         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1636         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1637                 val = g_key_file_get_integer(keyfile, identifier,
1638                                                 "ScanInterval", &error);
1639                 if (error == NULL && val > 0)
1640                         device->scan_interval = val;
1641                 g_clear_error(&error);
1642                 break;
1643         }
1644
1645 done:
1646         g_free(identifier);
1647
1648         __connman_storage_close_profile(ident, keyfile, FALSE);
1649
1650         return 0;
1651 }
1652
1653 static int device_save(struct connman_device *device)
1654 {
1655         const char *ident = __connman_profile_active_ident();
1656         GKeyFile *keyfile;
1657         gchar *identifier;
1658
1659         DBG("device %p", device);
1660
1661         keyfile = __connman_storage_open_profile(ident);
1662         if (keyfile == NULL)
1663                 return 0;
1664
1665         identifier = g_strdup_printf("device_%s", device->element.name);
1666         if (identifier == NULL)
1667                 goto done;
1668
1669         g_key_file_set_boolean(keyfile, identifier,
1670                                         "Powered", device->powered_persistent);
1671
1672         switch (device->mode) {
1673         case CONNMAN_DEVICE_MODE_UNKNOWN:
1674                 break;
1675         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1676         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1677                 if (device->scan_interval > 0)
1678                         g_key_file_set_integer(keyfile, identifier,
1679                                         "ScanInterval", device->scan_interval);
1680                 break;
1681         }
1682
1683 done:
1684         g_free(identifier);
1685
1686         __connman_storage_close_profile(ident, keyfile, TRUE);
1687
1688         return 0;
1689 }
1690
1691 static struct connman_storage device_storage = {
1692         .name           = "device",
1693         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1694         .device_load    = device_load,
1695         .device_save    = device_save,
1696 };
1697
1698 int __connman_device_init(void)
1699 {
1700         DBG("");
1701
1702         connection = connman_dbus_get_connection();
1703
1704         if (connman_storage_register(&device_storage) < 0)
1705                 connman_error("Failed to register device storage");
1706
1707         return connman_driver_register(&device_driver);
1708 }
1709
1710 void __connman_device_cleanup(void)
1711 {
1712         DBG("");
1713
1714         connman_driver_unregister(&device_driver);
1715
1716         connman_storage_unregister(&device_storage);
1717
1718         dbus_connection_unref(connection);
1719 }