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