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