Track IP configuration based on RTNL interface list
[platform/upstream/connman.git] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 secondary;
40         connman_bool_t offlinemode;
41         connman_bool_t blocked;
42         connman_bool_t powered;
43         connman_bool_t powered_pending;
44         connman_bool_t powered_persistent;
45         connman_bool_t carrier;
46         connman_bool_t scanning;
47         connman_bool_t disconnected;
48         connman_uint16_t scan_interval;
49         char *name;
50         char *node;
51         char *address;
52         char *interface;
53         char *control;
54         char *ident;
55         int phyindex;
56         unsigned int connections;
57         guint scan_timeout;
58         struct connman_ipconfig *ipconfig;
59
60         struct connman_device_driver *driver;
61         void *driver_data;
62
63         connman_bool_t registered;
64
65         char *last_network;
66         struct connman_network *network;
67         GHashTable *networks;
68
69         DBusMessage *pending;
70         guint timeout;
71 };
72
73 static gboolean device_scan_trigger(gpointer user_data)
74 {
75         struct connman_device *device = user_data;
76
77         DBG("device %p", device);
78
79         if (device->driver == NULL) {
80                 device->scan_timeout = 0;
81                 return FALSE;
82         }
83
84         if (device->driver->scan)
85                 device->driver->scan(device);
86
87         return TRUE;
88 }
89
90 static void clear_scan_trigger(struct connman_device *device)
91 {
92         if (device->scan_timeout > 0) {
93                 g_source_remove(device->scan_timeout);
94                 device->scan_timeout = 0;
95         }
96 }
97
98 static void reset_scan_trigger(struct connman_device *device)
99 {
100         clear_scan_trigger(device);
101
102         if (device->scan_interval > 0) {
103                 guint interval = device->scan_interval;
104                 device->scan_timeout = g_timeout_add_seconds(interval,
105                                         device_scan_trigger, device);
106         }
107 }
108
109 static const char *type2description(enum connman_device_type type)
110 {
111         switch (type) {
112         case CONNMAN_DEVICE_TYPE_UNKNOWN:
113         case CONNMAN_DEVICE_TYPE_VENDOR:
114                 break;
115         case CONNMAN_DEVICE_TYPE_ETHERNET:
116                 return "Ethernet";
117         case CONNMAN_DEVICE_TYPE_WIFI:
118                 return "Wireless";
119         case CONNMAN_DEVICE_TYPE_WIMAX:
120                 return "WiMAX";
121         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
122                 return "Bluetooth";
123         case CONNMAN_DEVICE_TYPE_GPS:
124                 return "GPS";
125         case CONNMAN_DEVICE_TYPE_MBM:
126         case CONNMAN_DEVICE_TYPE_HSO:
127         case CONNMAN_DEVICE_TYPE_NOZOMI:
128         case CONNMAN_DEVICE_TYPE_HUAWEI:
129         case CONNMAN_DEVICE_TYPE_NOVATEL:
130                 return "Cellular";
131         }
132
133         return NULL;
134 }
135
136 static const char *type2string(enum connman_device_type type)
137 {
138         switch (type) {
139         case CONNMAN_DEVICE_TYPE_UNKNOWN:
140         case CONNMAN_DEVICE_TYPE_VENDOR:
141                 break;
142         case CONNMAN_DEVICE_TYPE_ETHERNET:
143                 return "ethernet";
144         case CONNMAN_DEVICE_TYPE_WIFI:
145                 return "wifi";
146         case CONNMAN_DEVICE_TYPE_WIMAX:
147                 return "wimax";
148         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
149                 return "bluetooth";
150         case CONNMAN_DEVICE_TYPE_GPS:
151                 return "gps";
152         case CONNMAN_DEVICE_TYPE_MBM:
153         case CONNMAN_DEVICE_TYPE_HSO:
154         case CONNMAN_DEVICE_TYPE_HUAWEI:
155         case CONNMAN_DEVICE_TYPE_NOZOMI:
156         case CONNMAN_DEVICE_TYPE_NOVATEL:
157                 return "cellular";
158         }
159
160         return NULL;
161 }
162
163 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
164 {
165         enum connman_device_type type = connman_device_get_type(device);
166
167         switch (type) {
168         case CONNMAN_DEVICE_TYPE_UNKNOWN:
169         case CONNMAN_DEVICE_TYPE_VENDOR:
170         case CONNMAN_DEVICE_TYPE_GPS:
171         case CONNMAN_DEVICE_TYPE_NOZOMI:
172         case CONNMAN_DEVICE_TYPE_HUAWEI:
173         case CONNMAN_DEVICE_TYPE_NOVATEL:
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_MBM:
184         case CONNMAN_DEVICE_TYPE_HSO:
185                 return CONNMAN_SERVICE_TYPE_CELLULAR;
186         }
187
188         return CONNMAN_SERVICE_TYPE_UNKNOWN;
189 }
190
191 static int set_connected(struct connman_device *device,
192                                                 connman_bool_t connected)
193 {
194         if (connected == TRUE) {
195                 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
196                 struct connman_element *element;
197
198                 device->disconnected = TRUE;
199
200                 switch (device->element.ipv4.method) {
201                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
202                 case CONNMAN_IPCONFIG_METHOD_OFF:
203                         return 0;
204                 case CONNMAN_IPCONFIG_METHOD_STATIC:
205                         type = CONNMAN_ELEMENT_TYPE_IPV4;
206                         break;
207                 case CONNMAN_IPCONFIG_METHOD_DHCP:
208                         type = CONNMAN_ELEMENT_TYPE_DHCP;
209                         break;
210                 }
211
212                 element = connman_element_create(NULL);
213                 if (element != NULL) {
214                         struct connman_service *service;
215
216                         element->type  = type;
217                         element->index = device->element.index;
218
219                         if (connman_element_register(element,
220                                                         &device->element) < 0)
221                                 connman_element_unref(element);
222
223                         device->disconnected = FALSE;
224
225                         service = __connman_service_lookup_from_device(device);
226                         __connman_service_indicate_state(service,
227                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
228                 }
229         } else {
230                 struct connman_service *service;
231
232                 connman_element_unregister_children(&device->element);
233
234                 device->disconnected = TRUE;
235
236                 service = __connman_service_lookup_from_device(device);
237                 __connman_service_indicate_state(service,
238                                         CONNMAN_SERVICE_STATE_IDLE);
239         }
240
241         if (connected == TRUE) {
242                 enum connman_service_type type;
243
244                 type = __connman_device_get_service_type(device);
245                 __connman_notifier_connect(type);
246         } else {
247                 enum connman_service_type type;
248
249                 type = __connman_device_get_service_type(device);
250                 __connman_notifier_disconnect(type);
251         }
252
253         return 0;
254 }
255
256 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
257 {
258         if (carrier == TRUE)
259                 __connman_profile_add_device(device);
260         else
261                 __connman_profile_remove_device(device);
262
263         return set_connected(device, carrier);
264 }
265
266 static int powered_changed(struct connman_device *device)
267 {
268         DBusMessage *signal;
269         DBusMessageIter entry, value;
270         const char *key = "Powered";
271
272         signal = dbus_message_new_signal(device->element.path,
273                                 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
274         if (signal == NULL)
275                 return -ENOMEM;
276
277         dbus_message_iter_init_append(signal, &entry);
278
279         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
280
281         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
282                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
283         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
284                                                         &device->powered);
285         dbus_message_iter_close_container(&entry, &value);
286
287         g_dbus_send_message(connection, signal);
288
289         return 0;
290 }
291
292 static int set_powered(struct connman_device *device, connman_bool_t powered)
293 {
294         struct connman_device_driver *driver = device->driver;
295         enum connman_service_type type;
296         int err;
297
298         DBG("device %p powered %d", device, powered);
299
300         if (device->powered_pending == powered)
301                 return -EALREADY;
302
303         device->powered_pending = powered;
304
305         if (!driver)
306                 return -EINVAL;
307
308         type = __connman_device_get_service_type(device);
309
310         if (powered == TRUE) {
311                 if (driver->enable) {
312                         err = driver->enable(device);
313                         if (err == 0)
314                                 __connman_notifier_enable(type);
315                 } else
316                         err = -EINVAL;
317         } else {
318                 clear_scan_trigger(device);
319
320                 g_hash_table_remove_all(device->networks);
321
322                 set_carrier(device, FALSE);
323
324                 if (driver->disable) {
325                         err = driver->disable(device);
326                         if (err == 0)
327                                 __connman_notifier_disable(type);
328                 } else
329                         err = -EINVAL;
330         }
331
332         if (err == 0) {
333                 device->powered = powered;
334
335                 if (device->registered == TRUE)
336                         powered_changed(device);
337         }
338
339         return err;
340 }
341
342 static void append_path(gpointer key, gpointer value, gpointer user_data)
343 {
344         struct connman_element *element = value;
345         DBusMessageIter *iter = user_data;
346
347         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
348                                                         &element->path);
349 }
350
351 static void append_networks(struct connman_device *device,
352                                                 DBusMessageIter *entry)
353 {
354         DBusMessageIter value, iter;
355         const char *key = "Networks";
356
357         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
358
359         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
360                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
361                                                                 &value);
362
363         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
364                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
365         g_hash_table_foreach(device->networks, append_path, &iter);
366         dbus_message_iter_close_container(&value, &iter);
367
368         dbus_message_iter_close_container(entry, &value);
369 }
370
371 static DBusMessage *get_properties(DBusConnection *conn,
372                                         DBusMessage *msg, void *data)
373 {
374         struct connman_device *device = data;
375         DBusMessage *reply;
376         DBusMessageIter array, dict, entry;
377         const char *str;
378
379         DBG("conn %p", conn);
380
381         if (__connman_security_check_privilege(msg,
382                                         CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
383                 return __connman_error_permission_denied(msg);
384
385         reply = dbus_message_new_method_return(msg);
386         if (reply == NULL)
387                 return NULL;
388
389         dbus_message_iter_init_append(reply, &array);
390
391         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
392                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
393                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
394                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
395
396         if (device->name != NULL)
397                 connman_dbus_dict_append_variant(&dict, "Name",
398                                         DBUS_TYPE_STRING, &device->name);
399
400         str = type2string(device->type);
401         if (str != NULL)
402                 connman_dbus_dict_append_variant(&dict, "Type",
403                                                 DBUS_TYPE_STRING, &str);
404
405         if (device->address != NULL)
406                 connman_dbus_dict_append_variant(&dict, "Address",
407                                         DBUS_TYPE_STRING, &device->address);
408
409         if (device->interface != NULL)
410                 connman_dbus_dict_append_variant(&dict, "Interface",
411                                         DBUS_TYPE_STRING, &device->interface);
412
413         connman_dbus_dict_append_variant(&dict, "Powered",
414                                         DBUS_TYPE_BOOLEAN, &device->powered);
415
416         if (device->driver && device->driver->scan)
417                 connman_dbus_dict_append_variant(&dict, "Scanning",
418                                         DBUS_TYPE_BOOLEAN, &device->scanning);
419
420         switch (device->mode) {
421         case CONNMAN_DEVICE_MODE_UNKNOWN:
422                 break;
423         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
424                 __connman_element_append_ipv4(&device->element, &dict);
425                 break;
426         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
427         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
428                 if (device->scan_interval > 0)
429                         connman_dbus_dict_append_variant(&dict, "ScanInterval",
430                                 DBUS_TYPE_UINT16, &device->scan_interval);
431
432                 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
433                                                                 NULL, &entry);
434                 append_networks(device, &entry);
435                 dbus_message_iter_close_container(&dict, &entry);
436                 break;
437         }
438
439         dbus_message_iter_close_container(&array, &dict);
440
441         return reply;
442 }
443
444 static gboolean powered_timeout(gpointer user_data)
445 {
446         struct connman_device *device = user_data;
447
448         DBG("device %p", device);
449
450         device->timeout = 0;
451
452         if (device->pending != NULL) {
453                 DBusMessage *reply;
454
455                 reply = __connman_error_operation_timeout(device->pending);
456                 if (reply != NULL)
457                         g_dbus_send_message(connection, reply);
458
459                 dbus_message_unref(device->pending);
460                 device->pending = NULL;
461         }
462
463         return FALSE;
464 }
465
466 static DBusMessage *set_property(DBusConnection *conn,
467                                         DBusMessage *msg, void *data)
468 {
469         struct connman_device *device = data;
470         DBusMessageIter iter, value;
471         const char *name;
472         int type;
473
474         DBG("conn %p", conn);
475
476         if (dbus_message_iter_init(msg, &iter) == FALSE)
477                 return __connman_error_invalid_arguments(msg);
478
479         dbus_message_iter_get_basic(&iter, &name);
480         dbus_message_iter_next(&iter);
481         dbus_message_iter_recurse(&iter, &value);
482
483         if (__connman_security_check_privilege(msg,
484                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
485                 return __connman_error_permission_denied(msg);
486
487         type = dbus_message_iter_get_arg_type(&value);
488
489         if (g_str_equal(name, "Powered") == TRUE) {
490                 connman_bool_t powered;
491                 int err;
492
493                 if (type != DBUS_TYPE_BOOLEAN)
494                         return __connman_error_invalid_arguments(msg);
495
496                 dbus_message_iter_get_basic(&value, &powered);
497
498                 device->powered_persistent = powered;
499
500                 __connman_storage_save_device(device);
501
502                 if (device->powered == powered)
503                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
504
505                 if (device->pending != NULL)
506                         return __connman_error_in_progress(msg);
507
508                 err = set_powered(device, powered);
509                 if (err < 0) {
510                         if (err != -EINPROGRESS)
511                                 return __connman_error_failed(msg, -err);
512
513                         device->pending = dbus_message_ref(msg);
514
515                         device->timeout = g_timeout_add_seconds(15,
516                                                 powered_timeout, device);
517
518                         return NULL;
519                 }
520         } else if (g_str_equal(name, "ScanInterval") == TRUE) {
521                 connman_uint16_t interval;
522
523                 switch (device->mode) {
524                 case CONNMAN_DEVICE_MODE_UNKNOWN:
525                 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
526                         return __connman_error_invalid_arguments(msg);
527                 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
528                 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
529                         break;
530                 }
531
532                 if (type != DBUS_TYPE_UINT16)
533                         return __connman_error_invalid_arguments(msg);
534
535                 dbus_message_iter_get_basic(&value, &interval);
536
537                 if (device->scan_interval != interval) {
538                         device->scan_interval = interval;
539
540                         __connman_storage_save_device(device);
541
542                         reset_scan_trigger(device);
543                 }
544         } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
545                 int err;
546
547                 if (device->ipconfig == NULL)
548                         return __connman_error_invalid_property(msg);
549
550                 switch (device->mode) {
551                 case CONNMAN_DEVICE_MODE_UNKNOWN:
552                 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
553                 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
554                         return __connman_error_invalid_arguments(msg);
555                 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
556                         break;
557                 }
558
559                 err = __connman_ipconfig_set_ipv4(device->ipconfig,
560                                                         name + 5, &value);
561                 if (err < 0)
562                         return __connman_error_failed(msg, -err);
563         } else
564                 return __connman_error_invalid_property(msg);
565
566         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
567 }
568
569 static DBusMessage *propose_scan(DBusConnection *conn,
570                                         DBusMessage *msg, void *data)
571 {
572         struct connman_device *device = data;
573         int err;
574
575         DBG("conn %p", conn);
576
577         switch (device->mode) {
578         case CONNMAN_DEVICE_MODE_UNKNOWN:
579         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
580                 return __connman_error_not_supported(msg);
581         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
582         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
583                 break;
584         }
585
586         err = __connman_device_scan(device);
587         if (err < 0)
588                 return __connman_error_failed(msg, -err);
589
590         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
591 }
592
593 static GDBusMethodTable device_methods[] = {
594         { "GetProperties", "",      "a{sv}", get_properties },
595         { "SetProperty",   "sv",    "",      set_property,
596                                                 G_DBUS_METHOD_FLAG_ASYNC },
597         { "ProposeScan",   "",      "",      propose_scan   },
598         { },
599 };
600
601 static GDBusSignalTable device_signals[] = {
602         { "PropertyChanged", "sv" },
603         { },
604 };
605
606 static void append_devices(DBusMessageIter *entry)
607 {
608         DBusMessageIter value, iter;
609         const char *key = "Devices";
610
611         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
612
613         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
614                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
615                                                                 &value);
616
617         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
618                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
619         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
620         dbus_message_iter_close_container(&value, &iter);
621
622         dbus_message_iter_close_container(entry, &value);
623 }
624
625 static void emit_devices_signal(void)
626 {
627         DBusMessage *signal;
628         DBusMessageIter entry;
629
630         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
631                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
632         if (signal == NULL)
633                 return;
634
635         dbus_message_iter_init_append(signal, &entry);
636
637         append_devices(&entry);
638
639         g_dbus_send_message(connection, signal);
640 }
641
642 static int register_interface(struct connman_element *element)
643 {
644         struct connman_device *device = element->device;
645
646         DBG("element %p name %s", element, element->name);
647
648         if (g_dbus_register_interface(connection, element->path,
649                                         CONNMAN_DEVICE_INTERFACE,
650                                         device_methods, device_signals,
651                                         NULL, device, NULL) == FALSE) {
652                 connman_error("Failed to register %s device", element->path);
653                 return -EIO;
654         }
655
656         device->registered = TRUE;
657
658         emit_devices_signal();
659
660         return 0;
661 }
662
663 static void unregister_interface(struct connman_element *element)
664 {
665         struct connman_device *device = element->device;
666
667         DBG("element %p name %s", element, element->name);
668
669         device->registered = FALSE;
670
671         emit_devices_signal();
672
673         g_dbus_unregister_interface(connection, element->path,
674                                                 CONNMAN_DEVICE_INTERFACE);
675 }
676
677 static int setup_device(struct connman_device *device)
678 {
679         enum connman_service_type type;
680         int err;
681
682         DBG("device %p", device);
683
684         err = register_interface(&device->element);
685         if (err < 0) {
686                 if (device->driver->remove)
687                         device->driver->remove(device);
688                 device->driver = NULL;
689                 return err;
690         }
691
692         type = __connman_device_get_service_type(device);
693         __connman_notifier_register(type);
694
695         switch (device->mode) {
696         case CONNMAN_DEVICE_MODE_UNKNOWN:
697         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
698         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
699                 break;
700         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
701                 if (device->carrier == TRUE && device->secondary == FALSE)
702                         __connman_profile_add_device(device);
703                 break;
704         }
705
706         if (device->offlinemode == FALSE &&
707                                 device->powered_persistent == TRUE)
708                 __connman_device_enable(device);
709
710         return 0;
711 }
712
713 static void probe_driver(struct connman_element *element, gpointer user_data)
714 {
715         struct connman_device_driver *driver = user_data;
716
717         DBG("element %p name %s", element, element->name);
718
719         if (element->device == NULL)
720                 return;
721
722         if (element->device->driver != NULL)
723                 return;
724
725         if (driver->type != element->device->type)
726                 return;
727
728         if (driver->probe(element->device) < 0)
729                 return;
730
731         element->device->driver = driver;
732
733         setup_device(element->device);
734 }
735
736 static void remove_device(struct connman_device *device)
737 {
738         enum connman_service_type type;
739
740         DBG("device %p", device);
741
742         __connman_device_disable(device);
743
744         switch (device->mode) {
745         case CONNMAN_DEVICE_MODE_UNKNOWN:
746         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
747         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
748                 break;
749         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
750                 if (device->secondary == FALSE)
751                         __connman_profile_remove_device(device);
752                 break;
753         }
754
755         type = __connman_device_get_service_type(device);
756         __connman_notifier_unregister(type);
757
758         unregister_interface(&device->element);
759
760         if (device->driver->remove)
761                 device->driver->remove(device);
762
763         device->driver = NULL;
764 }
765
766 static void remove_driver(struct connman_element *element, gpointer user_data)
767 {
768         struct connman_device_driver *driver = user_data;
769
770         DBG("element %p name %s", element, element->name);
771
772         if (element->device == NULL)
773                 return;
774
775         if (element->device->driver == driver)
776                 remove_device(element->device);
777 }
778
779 connman_bool_t __connman_device_has_driver(struct connman_device *device)
780 {
781         if (device == NULL || device->driver == NULL)
782                 return FALSE;
783
784         return device->registered;
785 }
786
787 static GSList *driver_list = NULL;
788
789 static gint compare_priority(gconstpointer a, gconstpointer b)
790 {
791         const struct connman_device_driver *driver1 = a;
792         const struct connman_device_driver *driver2 = b;
793
794         return driver2->priority - driver1->priority;
795 }
796
797 /**
798  * connman_device_driver_register:
799  * @driver: device driver definition
800  *
801  * Register a new device driver
802  *
803  * Returns: %0 on success
804  */
805 int connman_device_driver_register(struct connman_device_driver *driver)
806 {
807         DBG("driver %p name %s", driver, driver->name);
808
809         driver_list = g_slist_insert_sorted(driver_list, driver,
810                                                         compare_priority);
811
812         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
813                                                 probe_driver, driver);
814
815         return 0;
816 }
817
818 /**
819  * connman_device_driver_unregister:
820  * @driver: device driver definition
821  *
822  * Remove a previously registered device driver
823  */
824 void connman_device_driver_unregister(struct connman_device_driver *driver)
825 {
826         DBG("driver %p name %s", driver, driver->name);
827
828         driver_list = g_slist_remove(driver_list, driver);
829
830         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
831                                                 remove_driver, driver);
832 }
833
834 static void unregister_network(gpointer data)
835 {
836         struct connman_network *network = data;
837
838         DBG("network %p", network);
839
840         connman_element_unregister((struct connman_element *) network);
841
842         connman_network_unref(network);
843 }
844
845 static void device_destruct(struct connman_element *element)
846 {
847         struct connman_device *device = element->device;
848
849         DBG("element %p name %s", element, element->name);
850
851         if (device->timeout > 0) {
852                 g_source_remove(device->timeout);
853                 device->timeout = 0;
854         }
855
856         if (device->pending != NULL) {
857                 dbus_message_unref(device->pending);
858                 device->pending = NULL;
859         }
860
861         g_free(device->ident);
862         g_free(device->node);
863         g_free(device->name);
864         g_free(device->address);
865         g_free(device->control);
866         g_free(device->interface);
867
868         if (device->ipconfig != NULL) {
869                 connman_ipconfig_unref(device->ipconfig);
870                 device->ipconfig = NULL;
871         }
872
873         g_free(device->last_network);
874
875         g_hash_table_destroy(device->networks);
876         device->networks = NULL;
877 }
878
879 /**
880  * connman_device_create:
881  * @node: device node name (for example an address)
882  * @type: device type
883  *
884  * Allocate a new device of given #type and assign the #node name to it.
885  *
886  * Returns: a newly-allocated #connman_device structure
887  */
888 struct connman_device *connman_device_create(const char *node,
889                                                 enum connman_device_type type)
890 {
891         struct connman_device *device;
892         const char *str;
893
894         DBG("node %s type %d", node, type);
895
896         device = g_try_new0(struct connman_device, 1);
897         if (device == NULL)
898                 return NULL;
899
900         DBG("device %p", device);
901
902         __connman_element_initialize(&device->element);
903
904         device->element.name = g_strdup(node);
905         device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
906
907         device->element.device = device;
908         device->element.destruct = device_destruct;
909
910         str = type2string(type);
911         if (str != NULL)
912                 connman_element_set_string(&device->element,
913                                         CONNMAN_PROPERTY_ID_TYPE, str);
914
915         device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
916
917         device->type      = type;
918         device->name      = g_strdup(type2description(device->type));
919         device->mode      = CONNMAN_DEVICE_MODE_UNKNOWN;
920         device->secondary = FALSE;
921
922         device->powered_persistent = TRUE;
923
924         device->phyindex = -1;
925
926         switch (type) {
927         case CONNMAN_DEVICE_TYPE_UNKNOWN:
928         case CONNMAN_DEVICE_TYPE_VENDOR:
929                 device->scan_interval = 0;
930                 break;
931         case CONNMAN_DEVICE_TYPE_ETHERNET:
932         case CONNMAN_DEVICE_TYPE_WIFI:
933                 device->scan_interval = 300;
934                 break;
935         case CONNMAN_DEVICE_TYPE_WIMAX:
936                 device->scan_interval = 0;
937                 break;
938         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
939                 device->scan_interval = 0;
940                 break;
941         case CONNMAN_DEVICE_TYPE_GPS:
942                 device->scan_interval = 0;
943                 break;
944         case CONNMAN_DEVICE_TYPE_MBM:
945         case CONNMAN_DEVICE_TYPE_HSO:
946         case CONNMAN_DEVICE_TYPE_NOZOMI:
947         case CONNMAN_DEVICE_TYPE_HUAWEI:
948         case CONNMAN_DEVICE_TYPE_NOVATEL:
949                 device->scan_interval = 0;
950                 break;
951         }
952
953         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
954                                                 g_free, unregister_network);
955
956         return device;
957 }
958
959 /**
960  * connman_device_ref:
961  * @device: device structure
962  *
963  * Increase reference counter of device
964  */
965 struct connman_device *connman_device_ref(struct connman_device *device)
966 {
967         if (connman_element_ref(&device->element) == NULL)
968                 return NULL;
969
970         return device;
971 }
972
973 /**
974  * connman_device_unref:
975  * @device: device structure
976  *
977  * Decrease reference counter of device
978  */
979 void connman_device_unref(struct connman_device *device)
980 {
981         connman_element_unref(&device->element);
982 }
983
984 const char *__connman_device_get_type(struct connman_device *device)
985 {
986         return type2string(device->type);
987 }
988
989 /**
990  * connman_device_get_type:
991  * @device: device structure
992  *
993  * Get type of device
994  */
995 enum connman_device_type connman_device_get_type(struct connman_device *device)
996 {
997         return device->type;
998 }
999
1000 /**
1001  * connman_device_get_name:
1002  * @device: device structure
1003  *
1004  * Get unique name of device
1005  */
1006 const char *connman_device_get_name(struct connman_device *device)
1007 {
1008         return device->element.name;
1009 }
1010
1011 /**
1012  * connman_device_get_path:
1013  * @device: device structure
1014  *
1015  * Get path name of device
1016  */
1017 const char *connman_device_get_path(struct connman_device *device)
1018 {
1019         return device->element.path;
1020 }
1021
1022 /**
1023  * connman_device_set_index:
1024  * @device: device structure
1025  * @index: index number
1026  *
1027  * Set index number of device
1028  */
1029 void connman_device_set_index(struct connman_device *device, int index)
1030 {
1031         device->element.index = index;
1032
1033         //if (device->ipconfig != NULL)
1034         //      connman_ipconfig_unref(device->ipconfig);
1035
1036         //device->ipconfig = connman_ipconfig_create(index);
1037 }
1038
1039 /**
1040  * connman_device_get_index:
1041  * @device: device structure
1042  *
1043  * Get index number of device
1044  */
1045 int connman_device_get_index(struct connman_device *device)
1046 {
1047         return device->element.index;
1048 }
1049
1050 int __connman_device_get_phyindex(struct connman_device *device)
1051 {
1052         return device->phyindex;
1053 }
1054
1055 void __connman_device_set_phyindex(struct connman_device *device,
1056                                                         int phyindex)
1057 {
1058         device->phyindex = phyindex;
1059 }
1060
1061 /**
1062  * connman_device_set_interface:
1063  * @device: device structure
1064  * @interface: interface name
1065  * @control: control interface
1066  *
1067  * Set interface name of device
1068  */
1069 void connman_device_set_interface(struct connman_device *device,
1070                                 const char *interface, const char *control)
1071 {
1072         g_free(device->element.devname);
1073         device->element.devname = g_strdup(interface);
1074
1075         g_free(device->interface);
1076         device->interface = g_strdup(interface);
1077
1078         g_free(device->control);
1079         device->control = g_strdup(control);
1080
1081         if (device->name == NULL) {
1082                 const char *str = type2description(device->type);
1083                 if (str != NULL && device->interface != NULL)
1084                         device->name = g_strdup_printf("%s (%s)", str,
1085                                                         device->interface);
1086         }
1087 }
1088
1089 const char *connman_device_get_control(struct connman_device *device)
1090 {
1091         return device->control;
1092 }
1093
1094 /**
1095  * connman_device_set_ident:
1096  * @device: device structure
1097  * @ident: unique identifier
1098  *
1099  * Set unique identifier of device
1100  */
1101 void connman_device_set_ident(struct connman_device *device,
1102                                                         const char *ident)
1103 {
1104         g_free(device->ident);
1105         device->ident = g_strdup(ident);
1106 }
1107
1108 const char *__connman_device_get_ident(struct connman_device *device)
1109 {
1110         return device->ident;
1111 }
1112
1113 /**
1114  * connman_device_set_mode:
1115  * @device: device structure
1116  * @mode: network mode
1117  *
1118  * Change network mode of device
1119  */
1120 void connman_device_set_mode(struct connman_device *device,
1121                                                 enum connman_device_mode mode)
1122 {
1123         device->mode = mode;
1124 }
1125
1126 /**
1127  * connman_device_get_mode:
1128  * @device: device structure
1129  *
1130  * Get network mode of device
1131  */
1132 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1133 {
1134         return device->mode;
1135 }
1136
1137 /**
1138  * connman_device_set_secondary:
1139  * @device: device structure
1140  * @secondary: secondary value
1141  *
1142  * Change secondary value of device
1143  */
1144 void connman_device_set_secondary(struct connman_device *device,
1145                                                 connman_bool_t secondary)
1146 {
1147         device->secondary = secondary;
1148 }
1149
1150 /**
1151  * connman_device_get_secondary:
1152  * @device: device structure
1153  *
1154  * Get secondary value of device
1155  */
1156 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1157 {
1158         return device->secondary;
1159 }
1160
1161 /**
1162  * connman_device_set_powered:
1163  * @device: device structure
1164  * @powered: powered state
1165  *
1166  * Change power state of device
1167  */
1168 int connman_device_set_powered(struct connman_device *device,
1169                                                 connman_bool_t powered)
1170 {
1171         enum connman_service_type type;
1172
1173         DBG("driver %p powered %d", device, powered);
1174
1175         if (device->timeout > 0) {
1176                 g_source_remove(device->timeout);
1177                 device->timeout = 0;
1178         }
1179
1180         if (device->pending != NULL) {
1181                 g_dbus_send_reply(connection, device->pending,
1182                                                         DBUS_TYPE_INVALID);
1183
1184                 dbus_message_unref(device->pending);
1185                 device->pending = NULL;
1186         }
1187
1188         if (device->powered == powered)
1189                 return -EALREADY;
1190
1191         device->powered = powered;
1192         device->powered_pending = powered;
1193
1194         type = __connman_device_get_service_type(device);
1195
1196         if (device->powered == TRUE)
1197                 __connman_notifier_enable(type);
1198         else
1199                 __connman_notifier_disable(type);
1200
1201         if (device->registered == FALSE)
1202                 return 0;
1203
1204         powered_changed(device);
1205
1206         if (powered == FALSE)
1207                 return 0;
1208
1209         reset_scan_trigger(device);
1210
1211         if (device->driver->scan)
1212                 device->driver->scan(device);
1213
1214         return 0;
1215 }
1216
1217 int __connman_device_set_blocked(struct connman_device *device,
1218                                                 connman_bool_t blocked)
1219 {
1220         connman_bool_t powered;
1221
1222         DBG("device %p blocked %d", device, blocked);
1223
1224         device->blocked = blocked;
1225
1226         if (device->offlinemode == TRUE)
1227                 return 0;
1228
1229         if (blocked == FALSE)
1230                 powered = device->powered_persistent;
1231         else
1232                 powered = FALSE;
1233
1234         return set_powered(device, powered);
1235 }
1236
1237 /**
1238  * connman_device_set_carrier:
1239  * @device: device structure
1240  * @carrier: carrier state
1241  *
1242  * Change carrier state of device (only for device without scanning)
1243  */
1244 int connman_device_set_carrier(struct connman_device *device,
1245                                                 connman_bool_t carrier)
1246 {
1247         DBG("device %p carrier %d", device, carrier);
1248
1249         switch (device->mode) {
1250         case CONNMAN_DEVICE_MODE_UNKNOWN:
1251         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1252         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1253                 return -EINVAL;
1254         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1255                 break;
1256         }
1257
1258         if (device->carrier == carrier)
1259                 return -EALREADY;
1260
1261         device->carrier = carrier;
1262
1263         return set_carrier(device, device->carrier);
1264 }
1265
1266 int __connman_device_scan(struct connman_device *device)
1267 {
1268         if (!device->driver || !device->driver->scan)
1269                 return -EOPNOTSUPP;
1270
1271         if (device->powered == FALSE)
1272                 return -ENOLINK;
1273
1274         return device->driver->scan(device);
1275 }
1276
1277 int __connman_device_enable(struct connman_device *device)
1278 {
1279         enum connman_service_type type;
1280         int err;
1281
1282         DBG("device %p", device);
1283
1284         if (!device->driver || !device->driver->enable)
1285                 return -EOPNOTSUPP;
1286
1287         if (device->powered_pending == TRUE)
1288                 return -EALREADY;
1289
1290         device->powered_pending = TRUE;
1291
1292         err = device->driver->enable(device);
1293         if (err < 0)
1294                 return err;
1295
1296         device->powered = TRUE;
1297
1298         type = __connman_device_get_service_type(device);
1299         __connman_notifier_enable(type);
1300
1301         return 0;
1302 }
1303
1304 int __connman_device_enable_persistent(struct connman_device *device)
1305 {
1306         DBG("device %p", device);
1307
1308         device->powered_persistent = TRUE;
1309
1310         __connman_storage_save_device(device);
1311
1312         return __connman_device_enable(device);
1313 }
1314
1315 int __connman_device_disable(struct connman_device *device)
1316 {
1317         enum connman_service_type type;
1318         int err;
1319
1320         DBG("device %p", device);
1321
1322         if (!device->driver || !device->driver->disable)
1323                 return -EOPNOTSUPP;
1324
1325         if (device->powered == FALSE)
1326                 return -ENOLINK;
1327
1328         if (device->powered_pending == FALSE)
1329                 return -EALREADY;
1330
1331         device->powered_pending = FALSE;
1332
1333         clear_scan_trigger(device);
1334
1335         g_hash_table_remove_all(device->networks);
1336
1337         err = device->driver->disable(device);
1338         if (err < 0)
1339                 return err;
1340
1341         device->powered = FALSE;
1342
1343         type = __connman_device_get_service_type(device);
1344         __connman_notifier_disable(type);
1345
1346         return 0;
1347 }
1348
1349 int __connman_device_disable_persistent(struct connman_device *device)
1350 {
1351         DBG("device %p", device);
1352
1353         device->powered_persistent = FALSE;
1354
1355         __connman_storage_save_device(device);
1356
1357         return __connman_device_disable(device);
1358 }
1359
1360 int __connman_device_connect(struct connman_device *device)
1361 {
1362         DBG("device %p", device);
1363
1364         if (device->disconnected == FALSE)
1365                 return -EINVAL;
1366
1367         if (device->driver && device->driver->connect)
1368                 device->driver->connect(device);
1369
1370         return 0;
1371 }
1372
1373 int __connman_device_disconnect(struct connman_device *device)
1374 {
1375         GHashTableIter iter;
1376         gpointer key, value;
1377
1378         DBG("device %p", device);
1379
1380         connman_device_set_disconnected(device, TRUE);
1381
1382         g_hash_table_iter_init(&iter, device->networks);
1383
1384         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1385                 struct connman_network *network = value;
1386
1387                 __connman_network_disconnect(network);
1388         }
1389
1390         if (device->driver && device->driver->disconnect)
1391                 device->driver->disconnect(device);
1392
1393         return 0;
1394 }
1395
1396 static void mark_network_unavailable(gpointer key, gpointer value,
1397                                                         gpointer user_data)
1398 {
1399         struct connman_network *network = value;
1400
1401         if (connman_network_get_connected(network) == TRUE)
1402                 return;
1403
1404         connman_network_set_available(network, FALSE);
1405 }
1406
1407 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1408                                                         gpointer user_data)
1409 {
1410         struct connman_network *network = value;
1411
1412         if (connman_network_get_connected(network) == TRUE)
1413                 return FALSE;
1414
1415         if (connman_network_get_available(network) == TRUE)
1416                 return FALSE;
1417
1418         return TRUE;
1419 }
1420
1421 void __connman_device_cleanup_networks(struct connman_device *device)
1422 {
1423         g_hash_table_foreach_remove(device->networks,
1424                                         remove_unavailable_network, NULL);
1425 }
1426
1427 /**
1428  * connman_device_set_scanning:
1429  * @device: device structure
1430  * @scanning: scanning state
1431  *
1432  * Change scanning state of device
1433  */
1434 int connman_device_set_scanning(struct connman_device *device,
1435                                                 connman_bool_t scanning)
1436 {
1437         DBusMessage *signal;
1438         DBusMessageIter entry, value;
1439         const char *key = "Scanning";
1440
1441         DBG("device %p scanning %d", device, scanning);
1442
1443         if (!device->driver || !device->driver->scan)
1444                 return -EINVAL;
1445
1446         if (device->scanning == scanning)
1447                 return -EALREADY;
1448
1449         device->scanning = scanning;
1450
1451         signal = dbus_message_new_signal(device->element.path,
1452                                 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1453         if (signal == NULL)
1454                 return 0;
1455
1456         dbus_message_iter_init_append(signal, &entry);
1457
1458         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1459
1460         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1461                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1462         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1463         dbus_message_iter_close_container(&entry, &value);
1464
1465         g_dbus_send_message(connection, signal);
1466
1467         if (scanning == TRUE) {
1468                 reset_scan_trigger(device);
1469
1470                 g_hash_table_foreach(device->networks,
1471                                         mark_network_unavailable, NULL);
1472
1473                 return 0;
1474         }
1475
1476         __connman_device_cleanup_networks(device);
1477
1478         if (device->connections > 0)
1479                 return 0;
1480
1481         if (device->disconnected == TRUE)
1482                 return 0;
1483
1484         __connman_service_auto_connect();
1485
1486         return 0;
1487 }
1488
1489 /**
1490  * connman_device_set_disconnected:
1491  * @device: device structure
1492  * @disconnected: disconnected state
1493  *
1494  * Change disconnected state of device (only for device with networks)
1495  */
1496 int connman_device_set_disconnected(struct connman_device *device,
1497                                                 connman_bool_t disconnected)
1498 {
1499         DBG("device %p disconnected %d", device, disconnected);
1500
1501         switch (device->mode) {
1502         case CONNMAN_DEVICE_MODE_UNKNOWN:
1503         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1504                 return -EINVAL;
1505         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1506         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1507                 break;
1508         }
1509
1510         if (device->disconnected == disconnected)
1511                 return -EALREADY;
1512
1513         device->disconnected = disconnected;
1514
1515         return 0;
1516 }
1517
1518 /**
1519  * connman_device_get_disconnected:
1520  * @device: device structure
1521  *
1522  * Get device disconnected state
1523  */
1524 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1525 {
1526         return device->disconnected;
1527 }
1528
1529 /**
1530  * connman_device_set_connected:
1531  * @device: device structure
1532  * @connected: connected state
1533  *
1534  * Change connected state of device (for Ethernet like devices)
1535  */
1536 int connman_device_set_connected(struct connman_device *device,
1537                                                 connman_bool_t connected)
1538 {
1539         DBG("device %p connected %d", device, connected);
1540
1541         switch (device->mode) {
1542         case CONNMAN_DEVICE_MODE_UNKNOWN:
1543         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1544         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1545                 return -EINVAL;
1546         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1547                 break;
1548         }
1549
1550         if (device->carrier == FALSE)
1551                 return -ENOTCONN;
1552
1553         return set_connected(device, connected);
1554 }
1555
1556 /**
1557  * connman_device_set_string:
1558  * @device: device structure
1559  * @key: unique identifier
1560  * @value: string value
1561  *
1562  * Set string value for specific key
1563  */
1564 int connman_device_set_string(struct connman_device *device,
1565                                         const char *key, const char *value)
1566 {
1567         DBG("device %p key %s value %s", device, key, value);
1568
1569         if (g_str_equal(key, "Address") == TRUE) {
1570                 g_free(device->address);
1571                 device->address = g_strdup(value);
1572         } else if (g_str_equal(key, "Name") == TRUE) {
1573                 g_free(device->name);
1574                 device->name = g_strdup(value);
1575         } else if (g_str_equal(key, "Node") == TRUE) {
1576                 g_free(device->node);
1577                 device->node = g_strdup(value);
1578         }
1579
1580         return connman_element_set_string(&device->element, key, value);
1581 }
1582
1583 /**
1584  * connman_device_get_string:
1585  * @device: device structure
1586  * @key: unique identifier
1587  *
1588  * Get string value for specific key
1589  */
1590 const char *connman_device_get_string(struct connman_device *device,
1591                                                         const char *key)
1592 {
1593         DBG("device %p key %s", device, key);
1594
1595         if (g_str_equal(key, "Address") == TRUE)
1596                 return device->address;
1597         else if (g_str_equal(key, "Name") == TRUE)
1598                 return device->name;
1599         else if (g_str_equal(key, "Node") == TRUE)
1600                 return device->node;
1601
1602         return connman_element_get_string(&device->element, key);
1603 }
1604
1605 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1606 {
1607         struct connman_device *device = element->device;
1608         connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1609         connman_bool_t powered;
1610
1611         DBG("element %p name %s", element, element->name);
1612
1613         if (device == NULL)
1614                 return;
1615
1616         device->offlinemode = offlinemode;
1617
1618         powered = (offlinemode == TRUE) ? FALSE : TRUE;
1619
1620         if (device->powered == powered)
1621                 return;
1622
1623         if (device->powered_persistent == FALSE)
1624                 powered = FALSE;
1625
1626         set_powered(device, powered);
1627 }
1628
1629 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1630 {
1631         DBG("offlinmode %d", offlinemode);
1632
1633         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1634                         set_offlinemode, GUINT_TO_POINTER(offlinemode));
1635
1636         __connman_notifier_offlinemode(offlinemode);
1637
1638         return 0;
1639 }
1640
1641 void __connman_device_increase_connections(struct connman_device *device)
1642 {
1643         device->connections++;
1644 }
1645
1646 void __connman_device_decrease_connections(struct connman_device *device)
1647 {
1648         device->connections--;
1649 }
1650
1651 /**
1652  * connman_device_add_network:
1653  * @device: device structure
1654  * @network: network structure
1655  *
1656  * Add new network to the device
1657  */
1658 int connman_device_add_network(struct connman_device *device,
1659                                         struct connman_network *network)
1660 {
1661         const char *identifier = connman_network_get_identifier(network);
1662         int err;
1663
1664         DBG("device %p network %p", device, network);
1665
1666         switch (device->mode) {
1667         case CONNMAN_DEVICE_MODE_UNKNOWN:
1668         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1669                 return -EINVAL;
1670         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1671         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1672                 break;
1673         }
1674
1675         __connman_network_set_device(network, device);
1676
1677         err = connman_element_register((struct connman_element *) network,
1678                                                         &device->element);
1679         if (err < 0) {
1680                 __connman_network_set_device(network, NULL);
1681                 return err;
1682         }
1683
1684         g_hash_table_insert(device->networks, g_strdup(identifier),
1685                                                                 network);
1686
1687         return 0;
1688 }
1689
1690 /**
1691  * connman_device_get_network:
1692  * @device: device structure
1693  * @identifier: network identifier
1694  *
1695  * Get network for given identifier
1696  */
1697 struct connman_network *connman_device_get_network(struct connman_device *device,
1698                                                         const char *identifier)
1699 {
1700         DBG("device %p identifier %s", device, identifier);
1701
1702         return g_hash_table_lookup(device->networks, identifier);
1703 }
1704
1705 /**
1706  * connman_device_remove_network:
1707  * @device: device structure
1708  * @identifier: network identifier
1709  *
1710  * Remove network for given identifier
1711  */
1712 int connman_device_remove_network(struct connman_device *device,
1713                                                         const char *identifier)
1714 {
1715         DBG("device %p identifier %s", device, identifier);
1716
1717         g_hash_table_remove(device->networks, identifier);
1718
1719         return 0;
1720 }
1721
1722 void __connman_device_set_network(struct connman_device *device,
1723                                         struct connman_network *network)
1724 {
1725         const char *name;
1726
1727         if (device->network == network)
1728                 return;
1729
1730         if (device->network != NULL)
1731                 connman_network_unref(device->network);
1732
1733         if (network != NULL) {
1734                 name = connman_network_get_string(network,
1735                                                 CONNMAN_PROPERTY_ID_NAME);
1736                 g_free(device->last_network);
1737                 device->last_network = g_strdup(name);
1738
1739                 device->network = connman_network_ref(network);
1740         } else {
1741                 g_free(device->last_network);
1742                 device->last_network = NULL;
1743
1744                 device->network = NULL;
1745         }
1746 }
1747
1748 /**
1749  * connman_device_register:
1750  * @device: device structure
1751  *
1752  * Register device with the system
1753  */
1754 int connman_device_register(struct connman_device *device)
1755 {
1756         __connman_storage_load_device(device);
1757
1758         device->offlinemode = __connman_profile_get_offlinemode();
1759
1760         return connman_element_register(&device->element, NULL);
1761 }
1762
1763 /**
1764  * connman_device_unregister:
1765  * @device: device structure
1766  *
1767  * Unregister device with the system
1768  */
1769 void connman_device_unregister(struct connman_device *device)
1770 {
1771         __connman_storage_save_device(device);
1772
1773         connman_element_unregister(&device->element);
1774 }
1775
1776 /**
1777  * connman_device_get_data:
1778  * @device: device structure
1779  *
1780  * Get private device data pointer
1781  */
1782 void *connman_device_get_data(struct connman_device *device)
1783 {
1784         return device->driver_data;
1785 }
1786
1787 /**
1788  * connman_device_set_data:
1789  * @device: device structure
1790  * @data: data pointer
1791  *
1792  * Set private device data pointer
1793  */
1794 void connman_device_set_data(struct connman_device *device, void *data)
1795 {
1796         device->driver_data = data;
1797 }
1798
1799 static gboolean match_driver(struct connman_device *device,
1800                                         struct connman_device_driver *driver)
1801 {
1802         if (device->type == driver->type ||
1803                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1804                 return TRUE;
1805
1806         return FALSE;
1807 }
1808
1809 static int device_probe(struct connman_element *element)
1810 {
1811         struct connman_device *device = element->device;
1812         GSList *list;
1813
1814         DBG("element %p name %s", element, element->name);
1815
1816         if (device == NULL)
1817                 return -ENODEV;
1818
1819         if (device->driver != NULL)
1820                 return -EALREADY;
1821
1822         for (list = driver_list; list; list = list->next) {
1823                 struct connman_device_driver *driver = list->data;
1824
1825                 if (match_driver(device, driver) == FALSE)
1826                         continue;
1827
1828                 DBG("driver %p name %s", driver, driver->name);
1829
1830                 if (driver->probe(device) == 0) {
1831                         device->driver = driver;
1832                         break;
1833                 }
1834         }
1835
1836         if (device->driver == NULL)
1837                 return -ENODEV;
1838
1839         return setup_device(device);
1840 }
1841
1842 static void device_remove(struct connman_element *element)
1843 {
1844         struct connman_device *device = element->device;
1845
1846         DBG("element %p name %s", element, element->name);
1847
1848         if (device == NULL)
1849                 return;
1850
1851         if (device->driver == NULL)
1852                 return;
1853
1854         remove_device(device);
1855 }
1856
1857 static struct connman_driver device_driver = {
1858         .name           = "device",
1859         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
1860         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1861         .probe          = device_probe,
1862         .remove         = device_remove,
1863 };
1864
1865 static int device_load(struct connman_device *device)
1866 {
1867         const char *ident = __connman_profile_active_ident();
1868         GKeyFile *keyfile;
1869         GError *error = NULL;
1870         gchar *identifier;
1871         connman_bool_t powered;
1872         int val;
1873
1874         DBG("device %p", device);
1875
1876         keyfile = __connman_storage_open(ident);
1877         if (keyfile == NULL)
1878                 return 0;
1879
1880         identifier = g_strdup_printf("device_%s", device->element.name);
1881         if (identifier == NULL)
1882                 goto done;
1883
1884         powered = g_key_file_get_boolean(keyfile, identifier,
1885                                                 "Powered", &error);
1886         if (error == NULL)
1887                 device->powered_persistent = powered;
1888         g_clear_error(&error);
1889
1890         switch (device->mode) {
1891         case CONNMAN_DEVICE_MODE_UNKNOWN:
1892         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1893                 break;
1894         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1895         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1896                 val = g_key_file_get_integer(keyfile, identifier,
1897                                                 "ScanInterval", &error);
1898                 if (error == NULL && val > 0)
1899                         device->scan_interval = val;
1900                 g_clear_error(&error);
1901                 break;
1902         }
1903
1904 done:
1905         g_free(identifier);
1906
1907         __connman_storage_close(ident, keyfile, FALSE);
1908
1909         return 0;
1910 }
1911
1912 static int device_save(struct connman_device *device)
1913 {
1914         const char *ident = __connman_profile_active_ident();
1915         GKeyFile *keyfile;
1916         gchar *identifier;
1917
1918         DBG("device %p", device);
1919
1920         keyfile = __connman_storage_open(ident);
1921         if (keyfile == NULL)
1922                 return 0;
1923
1924         identifier = g_strdup_printf("device_%s", device->element.name);
1925         if (identifier == NULL)
1926                 goto done;
1927
1928         g_key_file_set_boolean(keyfile, identifier,
1929                                         "Powered", device->powered_persistent);
1930
1931         switch (device->mode) {
1932         case CONNMAN_DEVICE_MODE_UNKNOWN:
1933         case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1934                 break;
1935         case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1936         case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1937                 if (device->scan_interval > 0)
1938                         g_key_file_set_integer(keyfile, identifier,
1939                                         "ScanInterval", device->scan_interval);
1940                 break;
1941         }
1942
1943 done:
1944         g_free(identifier);
1945
1946         __connman_storage_close(ident, keyfile, TRUE);
1947
1948         return 0;
1949 }
1950
1951 static struct connman_storage device_storage = {
1952         .name           = "device",
1953         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
1954         .device_load    = device_load,
1955         .device_save    = device_save,
1956 };
1957
1958 int __connman_device_init(void)
1959 {
1960         DBG("");
1961
1962         connection = connman_dbus_get_connection();
1963
1964         if (connman_storage_register(&device_storage) < 0)
1965                 connman_error("Failed to register device storage");
1966
1967         return connman_driver_register(&device_driver);
1968 }
1969
1970 void __connman_device_cleanup(void)
1971 {
1972         DBG("");
1973
1974         connman_driver_unregister(&device_driver);
1975
1976         connman_storage_unregister(&device_storage);
1977
1978         dbus_connection_unref(connection);
1979 }