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