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