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