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