Disable offline mode when enabling a technology from offline mode
[platform/upstream/connman.git] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static DBusConnection *connection = NULL;
34
35 struct connman_device {
36         struct connman_element element;
37         enum connman_device_type type;
38         enum connman_device_mode mode;
39         connman_bool_t offlinemode;
40         connman_bool_t blocked;
41         connman_bool_t powered;
42         connman_bool_t powered_pending;
43         connman_bool_t powered_persistent;
44         connman_bool_t scanning;
45         connman_bool_t disconnected;
46         connman_bool_t reconnect;
47         connman_uint16_t scan_interval;
48         char *name;
49         char *node;
50         char *address;
51         char *interface;
52         char *control;
53         char *ident;
54         int phyindex;
55         unsigned int connections;
56         guint scan_timeout;
57
58         struct connman_device_driver *driver;
59         void *driver_data;
60
61         connman_bool_t registered;
62
63         char *last_network;
64         struct connman_network *network;
65         GHashTable *networks;
66
67         DBusMessage *pending;
68         guint timeout;
69 };
70
71 static gboolean device_scan_trigger(gpointer user_data)
72 {
73         struct connman_device *device = user_data;
74
75         DBG("device %p", device);
76
77         if (device->driver == NULL) {
78                 device->scan_timeout = 0;
79                 return FALSE;
80         }
81
82         if (device->driver->scan)
83                 device->driver->scan(device);
84
85         return TRUE;
86 }
87
88 static void clear_scan_trigger(struct connman_device *device)
89 {
90         if (device->scan_timeout > 0) {
91                 g_source_remove(device->scan_timeout);
92                 device->scan_timeout = 0;
93         }
94 }
95
96 static void reset_scan_trigger(struct connman_device *device)
97 {
98         clear_scan_trigger(device);
99
100         if (device->scan_interval > 0) {
101                 guint interval = device->scan_interval;
102                 device->scan_timeout = g_timeout_add_seconds(interval,
103                                         device_scan_trigger, device);
104         }
105 }
106
107 static void force_scan_trigger(struct connman_device *device)
108 {
109         clear_scan_trigger(device);
110
111         device->scan_timeout = g_timeout_add_seconds(5,
112                                         device_scan_trigger, device);
113 }
114
115 void connman_device_schedule_scan(struct connman_device *device)
116 {
117         reset_scan_trigger(device);
118 }
119
120 static const char *type2description(enum connman_device_type type)
121 {
122         switch (type) {
123         case CONNMAN_DEVICE_TYPE_UNKNOWN:
124         case CONNMAN_DEVICE_TYPE_VENDOR:
125                 break;
126         case CONNMAN_DEVICE_TYPE_ETHERNET:
127                 return "Ethernet";
128         case CONNMAN_DEVICE_TYPE_WIFI:
129                 return "Wireless";
130         case CONNMAN_DEVICE_TYPE_WIMAX:
131                 return "WiMAX";
132         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
133                 return "Bluetooth";
134         case CONNMAN_DEVICE_TYPE_GPS:
135                 return "GPS";
136         case CONNMAN_DEVICE_TYPE_CELLULAR:
137                 return "Cellular";
138         }
139
140         return NULL;
141 }
142
143 static const char *type2string(enum connman_device_type type)
144 {
145         switch (type) {
146         case CONNMAN_DEVICE_TYPE_UNKNOWN:
147         case CONNMAN_DEVICE_TYPE_VENDOR:
148                 break;
149         case CONNMAN_DEVICE_TYPE_ETHERNET:
150                 return "ethernet";
151         case CONNMAN_DEVICE_TYPE_WIFI:
152                 return "wifi";
153         case CONNMAN_DEVICE_TYPE_WIMAX:
154                 return "wimax";
155         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
156                 return "bluetooth";
157         case CONNMAN_DEVICE_TYPE_GPS:
158                 return "gps";
159         case CONNMAN_DEVICE_TYPE_CELLULAR:
160                 return "cellular";
161         }
162
163         return NULL;
164 }
165
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
167 {
168         enum connman_device_type type = connman_device_get_type(device);
169
170         switch (type) {
171         case CONNMAN_DEVICE_TYPE_UNKNOWN:
172         case CONNMAN_DEVICE_TYPE_VENDOR:
173         case CONNMAN_DEVICE_TYPE_GPS:
174                 break;
175         case CONNMAN_DEVICE_TYPE_ETHERNET:
176                 return CONNMAN_SERVICE_TYPE_ETHERNET;
177         case CONNMAN_DEVICE_TYPE_WIFI:
178                 return CONNMAN_SERVICE_TYPE_WIFI;
179         case CONNMAN_DEVICE_TYPE_WIMAX:
180                 return CONNMAN_SERVICE_TYPE_WIMAX;
181         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183         case CONNMAN_DEVICE_TYPE_CELLULAR:
184                 return CONNMAN_SERVICE_TYPE_CELLULAR;
185         }
186
187         return CONNMAN_SERVICE_TYPE_UNKNOWN;
188 }
189
190 static void powered_changed(struct connman_device *device)
191 {
192         connman_dbus_property_changed_basic(device->element.path,
193                                 CONNMAN_DEVICE_INTERFACE, "Powered",
194                                         DBUS_TYPE_BOOLEAN, &device->powered);
195 }
196
197 static 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
661 static void device_destruct(struct connman_element *element)
662 {
663         struct connman_device *device = element->device;
664
665         DBG("element %p name %s", element, element->name);
666
667         if (device->timeout > 0) {
668                 g_source_remove(device->timeout);
669                 device->timeout = 0;
670         }
671
672         clear_scan_trigger(device);
673
674         if (device->pending != NULL) {
675                 dbus_message_unref(device->pending);
676                 device->pending = NULL;
677         }
678
679         g_free(device->ident);
680         g_free(device->node);
681         g_free(device->name);
682         g_free(device->address);
683         g_free(device->control);
684         g_free(device->interface);
685
686         g_free(device->last_network);
687
688         g_hash_table_destroy(device->networks);
689         device->networks = NULL;
690 }
691
692 /**
693  * connman_device_create:
694  * @node: device node name (for example an address)
695  * @type: device type
696  *
697  * Allocate a new device of given #type and assign the #node name to it.
698  *
699  * Returns: a newly-allocated #connman_device structure
700  */
701 struct connman_device *connman_device_create(const char *node,
702                                                 enum connman_device_type type)
703 {
704         struct connman_device *device;
705         const char *str;
706
707         DBG("node %s type %d", node, type);
708
709         device = g_try_new0(struct connman_device, 1);
710         if (device == NULL)
711                 return NULL;
712
713         DBG("device %p", device);
714
715         __connman_element_initialize(&device->element);
716
717         device->element.name = g_strdup(node);
718         device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
719
720         device->element.device = device;
721         device->element.destruct = device_destruct;
722
723         str = type2string(type);
724         if (str != NULL)
725                 connman_element_set_string(&device->element,
726                                         CONNMAN_PROPERTY_ID_TYPE, str);
727
728         device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
729
730         device->type = type;
731         device->name = g_strdup(type2description(device->type));
732         device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
733
734         device->powered_persistent = TRUE;
735
736         device->phyindex = -1;
737
738         switch (type) {
739         case CONNMAN_DEVICE_TYPE_UNKNOWN:
740         case CONNMAN_DEVICE_TYPE_VENDOR:
741                 device->scan_interval = 0;
742                 break;
743         case CONNMAN_DEVICE_TYPE_ETHERNET:
744         case CONNMAN_DEVICE_TYPE_WIFI:
745                 device->scan_interval = 300;
746                 break;
747         case CONNMAN_DEVICE_TYPE_WIMAX:
748                 device->scan_interval = 0;
749                 break;
750         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
751                 device->scan_interval = 0;
752                 break;
753         case CONNMAN_DEVICE_TYPE_GPS:
754                 device->scan_interval = 0;
755                 break;
756         case CONNMAN_DEVICE_TYPE_CELLULAR:
757                 device->scan_interval = 0;
758                 break;
759         }
760
761         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
762                                                 g_free, unregister_network);
763
764         return device;
765 }
766
767 /**
768  * connman_device_ref:
769  * @device: device structure
770  *
771  * Increase reference counter of device
772  */
773 struct connman_device *connman_device_ref(struct connman_device *device)
774 {
775         if (connman_element_ref(&device->element) == NULL)
776                 return NULL;
777
778         return device;
779 }
780
781 /**
782  * connman_device_unref:
783  * @device: device structure
784  *
785  * Decrease reference counter of device
786  */
787 void connman_device_unref(struct connman_device *device)
788 {
789         connman_element_unref(&device->element);
790 }
791
792 const char *__connman_device_get_type(struct connman_device *device)
793 {
794         return type2string(device->type);
795 }
796
797 /**
798  * connman_device_get_type:
799  * @device: device structure
800  *
801  * Get type of device
802  */
803 enum connman_device_type connman_device_get_type(struct connman_device *device)
804 {
805         return device->type;
806 }
807
808 /**
809  * connman_device_get_name:
810  * @device: device structure
811  *
812  * Get unique name of device
813  */
814 const char *connman_device_get_name(struct connman_device *device)
815 {
816         return device->element.name;
817 }
818
819 /**
820  * connman_device_get_path:
821  * @device: device structure
822  *
823  * Get path name of device
824  */
825 const char *connman_device_get_path(struct connman_device *device)
826 {
827         return device->element.path;
828 }
829
830 /**
831  * connman_device_set_index:
832  * @device: device structure
833  * @index: index number
834  *
835  * Set index number of device
836  */
837 void connman_device_set_index(struct connman_device *device, int index)
838 {
839         device->element.index = index;
840 }
841
842 /**
843  * connman_device_get_index:
844  * @device: device structure
845  *
846  * Get index number of device
847  */
848 int connman_device_get_index(struct connman_device *device)
849 {
850         return device->element.index;
851 }
852
853 int __connman_device_get_phyindex(struct connman_device *device)
854 {
855         return device->phyindex;
856 }
857
858 void __connman_device_set_phyindex(struct connman_device *device,
859                                                         int phyindex)
860 {
861         device->phyindex = phyindex;
862 }
863
864 /**
865  * connman_device_set_interface:
866  * @device: device structure
867  * @interface: interface name
868  * @control: control interface
869  *
870  * Set interface name of device
871  */
872 void connman_device_set_interface(struct connman_device *device,
873                                 const char *interface, const char *control)
874 {
875         g_free(device->element.devname);
876         device->element.devname = g_strdup(interface);
877
878         g_free(device->interface);
879         device->interface = g_strdup(interface);
880
881         g_free(device->control);
882         device->control = g_strdup(control);
883
884         if (device->name == NULL) {
885                 const char *str = type2description(device->type);
886                 if (str != NULL && device->interface != NULL)
887                         device->name = g_strdup_printf("%s (%s)", str,
888                                                         device->interface);
889         }
890 }
891
892 const char *connman_device_get_control(struct connman_device *device)
893 {
894         return device->control;
895 }
896
897 /**
898  * connman_device_set_ident:
899  * @device: device structure
900  * @ident: unique identifier
901  *
902  * Set unique identifier of device
903  */
904 void connman_device_set_ident(struct connman_device *device,
905                                                         const char *ident)
906 {
907         g_free(device->ident);
908         device->ident = g_strdup(ident);
909 }
910
911 const char *__connman_device_get_ident(struct connman_device *device)
912 {
913         return device->ident;
914 }
915
916 /**
917  * connman_device_set_mode:
918  * @device: device structure
919  * @mode: network mode
920  *
921  * Change network mode of device
922  */
923 void connman_device_set_mode(struct connman_device *device,
924                                                 enum connman_device_mode mode)
925 {
926         device->mode = mode;
927 }
928
929 /**
930  * connman_device_get_mode:
931  * @device: device structure
932  *
933  * Get network mode of device
934  */
935 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
936 {
937         return device->mode;
938 }
939
940 /**
941  * connman_device_set_powered:
942  * @device: device structure
943  * @powered: powered state
944  *
945  * Change power state of device
946  */
947 int connman_device_set_powered(struct connman_device *device,
948                                                 connman_bool_t powered)
949 {
950         DBG("driver %p powered %d", device, powered);
951
952         if (device->timeout > 0) {
953                 g_source_remove(device->timeout);
954                 device->timeout = 0;
955         }
956
957         if (device->pending != NULL) {
958                 g_dbus_send_reply(connection, device->pending,
959                                                         DBUS_TYPE_INVALID);
960
961                 dbus_message_unref(device->pending);
962                 device->pending = NULL;
963         }
964
965         if (device->powered == powered)
966                 return -EALREADY;
967
968         if (powered == TRUE)
969                 __connman_device_enable(device);
970         else
971                 __connman_device_disable(device);
972
973         device->powered = powered;
974         device->powered_pending = powered;
975
976         if (device->powered == TRUE)
977                 __connman_technology_enable_device(device);
978         else
979                 __connman_technology_disable_device(device);
980
981         if (device->registered == FALSE)
982                 return 0;
983
984         powered_changed(device);
985
986         if (powered == FALSE)
987                 return 0;
988
989         reset_scan_trigger(device);
990
991         if (device->driver->scan)
992                 device->driver->scan(device);
993
994         return 0;
995 }
996
997 int __connman_device_set_blocked(struct connman_device *device,
998                                                 connman_bool_t blocked)
999 {
1000         connman_bool_t powered;
1001
1002         DBG("device %p blocked %d", device, blocked);
1003
1004         device->blocked = blocked;
1005
1006         if (device->offlinemode == TRUE)
1007                 return 0;
1008
1009         connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1010
1011         if (blocked == FALSE)
1012                 powered = device->powered_persistent;
1013         else
1014                 powered = FALSE;
1015
1016         return set_powered(device, powered);
1017 }
1018
1019 int __connman_device_scan(struct connman_device *device)
1020 {
1021         if (!device->driver || !device->driver->scan)
1022                 return -EOPNOTSUPP;
1023
1024         if (device->powered == FALSE)
1025                 return -ENOLINK;
1026
1027         reset_scan_trigger(device);
1028
1029         return device->driver->scan(device);
1030 }
1031
1032 int __connman_device_enable(struct connman_device *device)
1033 {
1034         int err;
1035
1036         DBG("device %p", device);
1037
1038         if (!device->driver || !device->driver->enable)
1039                 return -EOPNOTSUPP;
1040
1041         if (device->powered_pending == TRUE)
1042                 return -EALREADY;
1043
1044         device->powered_pending = TRUE;
1045
1046         err = device->driver->enable(device);
1047         if (err < 0)
1048                 return err;
1049
1050         device->powered = TRUE;
1051
1052         __connman_technology_enable_device(device);
1053
1054         return 0;
1055 }
1056
1057 int __connman_device_enable_persistent(struct connman_device *device)
1058 {
1059         DBG("device %p", device);
1060
1061         device->powered_persistent = TRUE;
1062
1063         if (__connman_profile_get_offlinemode() == TRUE)
1064                 __connman_profile_set_offlinemode(FALSE, FALSE);
1065
1066         __connman_storage_save_device(device);
1067
1068         return __connman_device_enable(device);
1069 }
1070
1071 int __connman_device_disable(struct connman_device *device)
1072 {
1073         int err;
1074
1075         DBG("device %p", device);
1076
1077         if (!device->driver || !device->driver->disable)
1078                 return -EOPNOTSUPP;
1079
1080         if (device->powered == FALSE)
1081                 return -ENOLINK;
1082
1083         if (device->powered_pending == FALSE)
1084                 return -EALREADY;
1085
1086         device->powered_pending = FALSE;
1087
1088         device->reconnect = FALSE;
1089
1090         clear_scan_trigger(device);
1091
1092         g_hash_table_remove_all(device->networks);
1093
1094         err = device->driver->disable(device);
1095         if (err < 0)
1096                 return err;
1097
1098         device->powered = FALSE;
1099
1100         __connman_technology_disable_device(device);
1101
1102         return 0;
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 }