Store PAC URL configuration in DHCP and provider elements
[platform/upstream/connman.git] / src / element.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <stdarg.h>
28 #include <string.h>
29
30 #include <glib.h>
31 #include <gdbus.h>
32
33 #include "connman.h"
34
35 static DBusConnection *connection;
36
37 static GNode *element_root = NULL;
38 static GSList *driver_list = NULL;
39 static gchar *device_filter = NULL;
40 static gchar *nodevice_filter = NULL;
41
42 static gboolean started = FALSE;
43
44 static const char *type2string(enum connman_element_type type)
45 {
46         switch (type) {
47         case CONNMAN_ELEMENT_TYPE_UNKNOWN:
48                 return "unknown";
49         case CONNMAN_ELEMENT_TYPE_ROOT:
50                 return "root";
51         case CONNMAN_ELEMENT_TYPE_PROFILE:
52                 return "profile";
53         case CONNMAN_ELEMENT_TYPE_DEVICE:
54                 return "device";
55         case CONNMAN_ELEMENT_TYPE_NETWORK:
56                 return "network";
57         case CONNMAN_ELEMENT_TYPE_SERVICE:
58                 return "service";
59         case CONNMAN_ELEMENT_TYPE_IPV4:
60                 return "ipv4";
61         case CONNMAN_ELEMENT_TYPE_IPV6:
62                 return "ipv6";
63         case CONNMAN_ELEMENT_TYPE_DHCP:
64                 return "dhcp";
65         case CONNMAN_ELEMENT_TYPE_BOOTP:
66                 return "bootp";
67         case CONNMAN_ELEMENT_TYPE_ZEROCONF:
68                 return "zeroconf";
69         case CONNMAN_ELEMENT_TYPE_CONNECTION:
70                 return "connection";
71         case CONNMAN_ELEMENT_TYPE_VENDOR:
72                 return "vendor";
73         }
74
75         return NULL;
76 }
77
78 struct foreach_data {
79         enum connman_element_type type;
80         element_cb_t callback;
81         gpointer user_data;
82 };
83
84 static gboolean foreach_callback(GNode *node, gpointer user_data)
85 {
86         struct connman_element *element = node->data;
87         struct foreach_data *data = user_data;
88
89         DBG("element %p name %s", element, element->name);
90
91         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
92                 return FALSE;
93
94         if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
95                                         data->type != element->type)
96                 return FALSE;
97
98         if (data->callback)
99                 data->callback(element, data->user_data);
100
101         return FALSE;
102 }
103
104 void __connman_element_foreach(struct connman_element *element,
105                                 enum connman_element_type type,
106                                 element_cb_t callback, gpointer user_data)
107 {
108         struct foreach_data data = { type, callback, user_data };
109         GNode *node;
110
111         DBG("");
112
113         if (element != NULL) {
114                 node = g_node_find(element_root, G_PRE_ORDER,
115                                                 G_TRAVERSE_ALL, element);
116                 if (node == NULL)
117                         return;
118         } else
119                 node = element_root;
120
121         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
122                                                 foreach_callback, &data);
123 }
124
125 struct append_filter {
126         enum connman_element_type type;
127         DBusMessageIter *iter;
128 };
129
130 static gboolean append_path(GNode *node, gpointer user_data)
131 {
132         struct connman_element *element = node->data;
133         struct append_filter *filter = user_data;
134
135         DBG("element %p name %s", element, element->name);
136
137         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
138                 return FALSE;
139
140         if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
141                                         filter->type != element->type)
142                 return FALSE;
143
144         if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
145                         __connman_device_has_driver(element->device) == FALSE)
146                 return FALSE;
147
148         if (filter->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
149                         __connman_network_has_driver(element->network) == FALSE)
150                 return FALSE;
151
152         dbus_message_iter_append_basic(filter->iter,
153                                 DBUS_TYPE_OBJECT_PATH, &element->path);
154
155         return FALSE;
156 }
157
158 void __connman_element_list(struct connman_element *element,
159                                         enum connman_element_type type,
160                                                         DBusMessageIter *iter)
161 {
162         struct append_filter filter = { type, iter };
163         GNode *node;
164
165         DBG("");
166
167         if (element != NULL) {
168                 node = g_node_find(element_root, G_PRE_ORDER,
169                                                 G_TRAVERSE_ALL, element);
170                 if (node == NULL)
171                         return;
172         } else
173                 node = element_root;
174
175         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
176                                                 append_path, &filter);
177 }
178
179 struct count_data {
180         enum connman_element_type type;
181         int count;
182 };
183
184 static gboolean count_element(GNode *node, gpointer user_data)
185 {
186         struct connman_element *element = node->data;
187         struct count_data *data = user_data;
188
189         DBG("element %p name %s", element, element->name);
190
191         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
192                 return FALSE;
193
194         if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
195                                         data->type != element->type)
196                 return FALSE;
197
198         data->count++;
199
200         return FALSE;
201 }
202
203 int __connman_element_count(struct connman_element *element,
204                                         enum connman_element_type type)
205 {
206         struct count_data data = { type, 0 };
207         GNode *node;
208
209         DBG("");
210
211         if (element != NULL) {
212                 node = g_node_find(element_root, G_PRE_ORDER,
213                                                 G_TRAVERSE_ALL, element);
214                 if (node == NULL)
215                         return 0;
216         } else
217                 node = element_root;
218
219         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
220                                                 count_element, &data);
221
222         return data.count;
223 }
224
225 static struct connman_network *__connman_element_get_network(struct connman_element *element)
226 {
227         if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
228                                                 element->network != NULL)
229                 return element->network;
230
231         if (element->parent == NULL)
232                 return NULL;
233
234         return __connman_element_get_network(element->parent);
235 }
236
237 struct connman_service *__connman_element_get_service(struct connman_element *element)
238 {
239         struct connman_service *service = NULL;
240         struct connman_network *network;
241         struct connman_device *device;
242         enum connman_device_type type;
243
244         device = __connman_element_get_device(element);
245         if (device == NULL)
246                 return NULL;
247
248         type = connman_device_get_type(device);
249
250         switch (type) {
251         case CONNMAN_DEVICE_TYPE_UNKNOWN:
252         case CONNMAN_DEVICE_TYPE_VENDOR:
253         case CONNMAN_DEVICE_TYPE_GPS:
254                 break;
255         case CONNMAN_DEVICE_TYPE_ETHERNET:
256         case CONNMAN_DEVICE_TYPE_WIFI:
257         case CONNMAN_DEVICE_TYPE_WIMAX:
258         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
259         case CONNMAN_DEVICE_TYPE_CELLULAR:
260         case CONNMAN_DEVICE_TYPE_MBM:
261         case CONNMAN_DEVICE_TYPE_HSO:
262                 network = __connman_element_get_network(element);
263                 if (network == NULL)
264                         return NULL;
265                 service = __connman_service_lookup_from_network(network);
266                 break;
267         }
268
269         return service;
270 }
271
272 struct connman_device *__connman_element_get_device(struct connman_element *element)
273 {
274         if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
275                                                 element->device != NULL)
276                 return element->device;
277
278         if (element->parent == NULL)
279                 return NULL;
280
281         return __connman_element_get_device(element->parent);
282 }
283
284 const char *__connman_element_get_device_path(struct connman_element *element)
285 {
286         struct connman_device *device;
287
288         device = __connman_element_get_device(element);
289         if (device == NULL)
290                 return NULL;
291
292         return connman_device_get_path(device);
293 }
294
295 const char *__connman_element_get_network_path(struct connman_element *element)
296 {
297         if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
298                                                 element->network != NULL)
299                 return element->path;
300
301         if (element->parent == NULL)
302                 return NULL;
303
304         return __connman_element_get_network_path(element->parent);
305 }
306
307 struct find_data {
308         enum connman_service_type type;
309         struct connman_device *device;
310 };
311
312 static gboolean find_device(GNode *node, gpointer user_data)
313 {
314         struct connman_element *element = node->data;
315         struct find_data *data = user_data;
316
317         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
318                 return FALSE;
319
320         if (element->device == NULL)
321                 return FALSE;
322
323         if (data->type != connman_device_get_type(element->device))
324                 return FALSE;
325
326         data->device = element->device;
327
328         return TRUE;
329 }
330
331 struct connman_device *__connman_element_find_device(enum connman_service_type type)
332 {
333         struct find_data data = { .type = type, .device = NULL };
334
335         g_node_traverse(element_root, G_PRE_ORDER,
336                                 G_TRAVERSE_ALL, -1, find_device, &data);
337
338         return data.device;
339 }
340
341 static gboolean request_scan(GNode *node, gpointer user_data)
342 {
343         struct connman_element *element = node->data;
344         struct find_data *data = user_data;
345         enum connman_service_type type;
346
347         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
348                 return FALSE;
349
350         if (element->device == NULL)
351                 return FALSE;
352
353         type = __connman_device_get_service_type(element->device);
354
355         switch (type) {
356         case CONNMAN_SERVICE_TYPE_UNKNOWN:
357         case CONNMAN_SERVICE_TYPE_SYSTEM:
358         case CONNMAN_SERVICE_TYPE_ETHERNET:
359         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
360         case CONNMAN_SERVICE_TYPE_CELLULAR:
361         case CONNMAN_SERVICE_TYPE_VPN:
362                 return FALSE;
363         case CONNMAN_SERVICE_TYPE_WIFI:
364         case CONNMAN_SERVICE_TYPE_WIMAX:
365                 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
366                                                         data->type != type)
367                         return FALSE;
368                 break;
369         }
370
371         __connman_device_scan(element->device);
372
373         return FALSE;
374 }
375
376 int __connman_element_request_scan(enum connman_service_type type)
377 {
378         struct find_data data = { .type = type, .device = NULL };
379
380         g_node_traverse(element_root, G_PRE_ORDER,
381                                 G_TRAVERSE_ALL, -1, request_scan, &data);
382
383         return 0;
384 }
385
386 static gboolean enable_technology(GNode *node, gpointer user_data)
387 {
388         struct connman_element *element = node->data;
389         struct find_data *data = user_data;
390         enum connman_service_type type;
391
392         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
393                 return FALSE;
394
395         if (element->device == NULL)
396                 return FALSE;
397
398         type = __connman_device_get_service_type(element->device);
399
400         switch (type) {
401         case CONNMAN_SERVICE_TYPE_UNKNOWN:
402         case CONNMAN_SERVICE_TYPE_SYSTEM:
403         case CONNMAN_SERVICE_TYPE_VPN:
404                 return FALSE;
405         case CONNMAN_SERVICE_TYPE_ETHERNET:
406         case CONNMAN_SERVICE_TYPE_WIFI:
407         case CONNMAN_SERVICE_TYPE_WIMAX:
408         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
409         case CONNMAN_SERVICE_TYPE_CELLULAR:
410                 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
411                                                         data->type != type)
412                         return FALSE;
413                 break;
414         }
415
416         __connman_device_enable_persistent(element->device);
417
418         return FALSE;
419 }
420
421 int __connman_element_enable_technology(enum connman_service_type type)
422 {
423         struct find_data data = { .type = type, .device = NULL };
424
425         g_node_traverse(element_root, G_PRE_ORDER,
426                                 G_TRAVERSE_ALL, -1, enable_technology, &data);
427
428         return 0;
429 }
430
431 static gboolean disable_technology(GNode *node, gpointer user_data)
432 {
433         struct connman_element *element = node->data;
434         struct find_data *data = user_data;
435         enum connman_service_type type;
436
437         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
438                 return FALSE;
439
440         if (element->device == NULL)
441                 return FALSE;
442
443         type = __connman_device_get_service_type(element->device);
444
445         switch (type) {
446         case CONNMAN_SERVICE_TYPE_UNKNOWN:
447         case CONNMAN_SERVICE_TYPE_SYSTEM:
448         case CONNMAN_SERVICE_TYPE_VPN:
449                 return FALSE;
450         case CONNMAN_SERVICE_TYPE_ETHERNET:
451         case CONNMAN_SERVICE_TYPE_WIFI:
452         case CONNMAN_SERVICE_TYPE_WIMAX:
453         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
454         case CONNMAN_SERVICE_TYPE_CELLULAR:
455                 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
456                                                         data->type != type)
457                         return FALSE;
458                 break;
459         }
460
461         __connman_device_disable_persistent(element->device);
462
463         return FALSE;
464 }
465
466 int __connman_element_disable_technology(enum connman_service_type type)
467 {
468         struct find_data data = { .type = type, .device = NULL };
469
470         g_node_traverse(element_root, G_PRE_ORDER,
471                                 G_TRAVERSE_ALL, -1, disable_technology, &data);
472
473         return 0;
474 }
475
476 static gint compare_priority(gconstpointer a, gconstpointer b)
477 {
478         const struct connman_driver *driver1 = a;
479         const struct connman_driver *driver2 = b;
480
481         return driver2->priority - driver1->priority;
482 }
483
484 static gboolean match_driver(struct connman_element *element,
485                                         struct connman_driver *driver)
486 {
487         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
488                 return FALSE;
489
490         if (element->type == driver->type ||
491                         driver->type == CONNMAN_ELEMENT_TYPE_UNKNOWN)
492                 return TRUE;
493
494         return FALSE;
495 }
496
497 static gboolean probe_driver(GNode *node, gpointer data)
498 {
499         struct connman_element *element = node->data;
500         struct connman_driver *driver = data;
501
502         DBG("element %p name %s", element, element->name);
503
504         if (!element->driver && match_driver(element, driver) == TRUE) {
505                 if (driver->probe(element) < 0)
506                         return FALSE;
507
508                 __connman_element_lock(element);
509                 element->driver = driver;
510                 __connman_element_unlock(element);
511         }
512
513         return FALSE;
514 }
515
516 void __connman_driver_rescan(struct connman_driver *driver)
517 {
518         DBG("driver %p name %s", driver, driver->name);
519
520         if (!driver->probe)
521                 return;
522
523         if (element_root != NULL)
524                 g_node_traverse(element_root, G_PRE_ORDER,
525                                 G_TRAVERSE_ALL, -1, probe_driver, driver);
526 }
527
528 /**
529  * connman_driver_register:
530  * @driver: driver definition
531  *
532  * Register a new driver
533  *
534  * Returns: %0 on success
535  */
536 int connman_driver_register(struct connman_driver *driver)
537 {
538         DBG("driver %p name %s", driver, driver->name);
539
540         if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
541                 return -EINVAL;
542
543         if (!driver->probe)
544                 return -EINVAL;
545
546         driver_list = g_slist_insert_sorted(driver_list, driver,
547                                                         compare_priority);
548
549         if (started == FALSE)
550                 return 0;
551
552         if (element_root != NULL)
553                 g_node_traverse(element_root, G_PRE_ORDER,
554                                 G_TRAVERSE_ALL, -1, probe_driver, driver);
555
556         return 0;
557 }
558
559 static gboolean remove_driver(GNode *node, gpointer data)
560 {
561         struct connman_element *element = node->data;
562         struct connman_driver *driver = data;
563
564         DBG("element %p name %s", element, element->name);
565
566         if (element->driver == driver) {
567                 if (driver->remove)
568                         driver->remove(element);
569
570                 __connman_element_lock(element);
571                 element->driver = NULL;
572                 __connman_element_unlock(element);
573         }
574
575         return FALSE;
576 }
577
578 /**
579  * connman_driver_unregister:
580  * @driver: driver definition
581  *
582  * Remove a previously registered driver
583  */
584 void connman_driver_unregister(struct connman_driver *driver)
585 {
586         DBG("driver %p name %s", driver, driver->name);
587
588         driver_list = g_slist_remove(driver_list, driver);
589
590         if (element_root != NULL)
591                 g_node_traverse(element_root, G_POST_ORDER,
592                                 G_TRAVERSE_ALL, -1, remove_driver, driver);
593 }
594
595 static void unregister_property(gpointer data)
596 {
597         struct connman_property *property = data;
598
599         DBG("property %p", property);
600
601         g_free(property->value);
602         g_free(property);
603 }
604
605 void __connman_element_initialize(struct connman_element *element)
606 {
607         DBG("element %p", element);
608
609         element->refcount = 1;
610
611         element->name    = NULL;
612         element->type    = CONNMAN_ELEMENT_TYPE_UNKNOWN;
613         element->state   = CONNMAN_ELEMENT_STATE_UNKNOWN;
614         element->error   = CONNMAN_ELEMENT_ERROR_UNKNOWN;
615         element->index   = -1;
616         element->enabled = FALSE;
617
618         element->configuring = FALSE;
619
620         element->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
621                                                 g_free, unregister_property);
622 }
623
624 /**
625  * connman_element_create:
626  * @name: element name
627  *
628  * Allocate a new element and assign the given #name to it. If the name
629  * is #NULL, it will be later on created based on the element type.
630  *
631  * Returns: a newly-allocated #connman_element structure
632  */
633 struct connman_element *connman_element_create(const char *name)
634 {
635         struct connman_element *element;
636
637         element = g_try_new0(struct connman_element, 1);
638         if (element == NULL)
639                 return NULL;
640
641         DBG("element %p", element);
642
643         __connman_element_initialize(element);
644
645         return element;
646 }
647
648 struct connman_element *connman_element_ref(struct connman_element *element)
649 {
650         DBG("element %p name %s refcount %d", element, element->name,
651                                 g_atomic_int_get(&element->refcount) + 1);
652
653         g_atomic_int_inc(&element->refcount);
654
655         return element;
656 }
657
658 static void free_properties(struct connman_element *element)
659 {
660         DBG("element %p name %s", element, element->name);
661
662         __connman_element_lock(element);
663
664         g_hash_table_destroy(element->properties);
665         element->properties = NULL;
666
667         __connman_element_unlock(element);
668 }
669
670 void connman_element_unref(struct connman_element *element)
671 {
672         DBG("element %p name %s refcount %d", element, element->name,
673                                 g_atomic_int_get(&element->refcount) - 1);
674
675         if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
676                 if (element->destruct)
677                         element->destruct(element);
678                 free_properties(element);
679                 g_free(element->ipv4.address);
680                 g_free(element->ipv4.netmask);
681                 g_free(element->ipv4.gateway);
682                 g_free(element->ipv4.network);
683                 g_free(element->ipv4.broadcast);
684                 g_free(element->ipv4.nameserver);
685                 g_free(element->ipv4.timeserver);
686                 g_free(element->ipv4.pac);
687                 g_free(element->devname);
688                 g_free(element->path);
689                 g_free(element->name);
690                 g_free(element);
691         }
692 }
693
694 static int set_static_property(struct connman_element *element,
695                                 const char *name, int type, const void *value)
696 {
697         struct connman_property *property;
698
699         DBG("element %p name %s", element, element->name);
700
701         if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
702                 return -EINVAL;
703
704         property = g_try_new0(struct connman_property, 1);
705         if (property == NULL)
706                 return -ENOMEM;
707
708         property->id   = CONNMAN_PROPERTY_ID_INVALID;
709         property->type = type;
710
711         DBG("name %s type %d value %p", name, type, value);
712
713         switch (type) {
714         case DBUS_TYPE_STRING:
715                 property->value = g_strdup(*((const char **) value));
716                 break;
717         case DBUS_TYPE_BOOLEAN:
718         case DBUS_TYPE_BYTE:
719                 property->value = g_try_malloc(1);
720                 if (property->value != NULL)
721                         memcpy(property->value, value, 1);
722                 break;
723         }
724
725         __connman_element_lock(element);
726
727         g_hash_table_replace(element->properties, g_strdup(name), property);
728
729         __connman_element_unlock(element);
730
731         return 0;
732 }
733
734 static int set_static_array_property(struct connman_element *element,
735                         const char *name, int type, const void *value, int len)
736 {
737         struct connman_property *property;
738
739         DBG("element %p name %s", element, element->name);
740
741         if (type != DBUS_TYPE_BYTE)
742                 return -EINVAL;
743
744         property = g_try_new0(struct connman_property, 1);
745         if (property == NULL)
746                 return -ENOMEM;
747
748         property->id      = CONNMAN_PROPERTY_ID_INVALID;
749         property->type    = DBUS_TYPE_ARRAY;
750         property->subtype = type;
751
752         DBG("name %s type %d value %p", name, type, value);
753
754         switch (type) {
755         case DBUS_TYPE_BYTE:
756                 property->value = g_try_malloc(len);
757                 if (property->value != NULL) {
758                         memcpy(property->value,
759                                 *((const unsigned char **) value), len);
760                         property->size = len;
761                 }
762                 break;
763         }
764
765         __connman_element_lock(element);
766
767         g_hash_table_replace(element->properties, g_strdup(name), property);
768
769         __connman_element_unlock(element);
770
771         return 0;
772 }
773
774 #if 0
775 static int set_property(struct connman_element *element,
776                                 enum connman_property_id id, const void *value)
777 {
778         switch (id) {
779         case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
780                 __connman_element_lock(element);
781                 g_free(element->ipv4.address);
782                 element->ipv4.address = g_strdup(*((const char **) value));
783                 __connman_element_unlock(element);
784                 break;
785         case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
786                 __connman_element_lock(element);
787                 g_free(element->ipv4.netmask);
788                 element->ipv4.netmask = g_strdup(*((const char **) value));
789                 __connman_element_unlock(element);
790                 break;
791         case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
792                 __connman_element_lock(element);
793                 g_free(element->ipv4.gateway);
794                 element->ipv4.gateway = g_strdup(*((const char **) value));
795                 __connman_element_unlock(element);
796                 break;
797         case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
798                 __connman_element_lock(element);
799                 g_free(element->ipv4.broadcast);
800                 element->ipv4.broadcast = g_strdup(*((const char **) value));
801                 __connman_element_unlock(element);
802                 break;
803         case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
804                 __connman_element_lock(element);
805                 g_free(element->ipv4.nameserver);
806                 element->ipv4.nameserver = g_strdup(*((const char **) value));
807                 __connman_element_unlock(element);
808                 break;
809         default:
810                 return -EINVAL;
811         }
812
813         return 0;
814 }
815 #endif
816
817 int connman_element_get_value(struct connman_element *element,
818                                 enum connman_property_id id, void *value)
819 {
820         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
821                 return -EINVAL;
822
823         switch (id) {
824         case CONNMAN_PROPERTY_ID_IPV4_METHOD:
825                 if (element->ipv4.method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
826                         return connman_element_get_value(element->parent,
827                                                                 id, value);
828                 __connman_element_lock(element);
829                 *((const char **) value) = __connman_ipconfig_method2string(element->ipv4.method);
830                 __connman_element_unlock(element);
831                 break;
832         case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
833                 if (element->ipv4.address == NULL)
834                         return connman_element_get_value(element->parent,
835                                                                 id, value);
836                 __connman_element_lock(element);
837                 *((char **) value) = element->ipv4.address;
838                 __connman_element_unlock(element);
839                 break;
840         case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
841                 if (element->ipv4.netmask == NULL)
842                         return connman_element_get_value(element->parent,
843                                                                 id, value);
844                 __connman_element_lock(element);
845                 *((char **) value) = element->ipv4.netmask;
846                 __connman_element_unlock(element);
847                 break;
848         case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
849                 if (element->ipv4.gateway == NULL)
850                         return connman_element_get_value(element->parent,
851                                                                 id, value);
852                 __connman_element_lock(element);
853                 *((char **) value) = element->ipv4.gateway;
854                 __connman_element_unlock(element);
855                 break;
856         case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
857                 if (element->ipv4.broadcast == NULL)
858                         return connman_element_get_value(element->parent,
859                                                                 id, value);
860                 __connman_element_lock(element);
861                 *((char **) value) = element->ipv4.broadcast;
862                 __connman_element_unlock(element);
863                 break;
864         case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
865                 if (element->ipv4.nameserver == NULL)
866                         return connman_element_get_value(element->parent,
867                                                                 id, value);
868                 __connman_element_lock(element);
869                 *((char **) value) = element->ipv4.nameserver;
870                 __connman_element_unlock(element);
871                 break;
872         case CONNMAN_PROPERTY_ID_IPV4_TIMESERVER:
873                 if (element->ipv4.timeserver == NULL)
874                         return connman_element_get_value(element->parent,
875                                                                 id, value);
876                 __connman_element_lock(element);
877                 *((char **) value) = element->ipv4.timeserver;
878                 __connman_element_unlock(element);
879                 break;
880         case CONNMAN_PROPERTY_ID_IPV4_PAC:
881                 if (element->ipv4.pac == NULL)
882                         return connman_element_get_value(element->parent,
883                                                                 id, value);
884                 __connman_element_lock(element);
885                 *((char **) value) = element->ipv4.pac;
886                 __connman_element_unlock(element);
887                 break;
888         default:
889                 return -EINVAL;
890         }
891
892         return 0;
893 }
894
895 static gboolean get_static_property(struct connman_element *element,
896                                                 const char *name, void *value)
897 {
898         struct connman_property *property;
899         gboolean found = FALSE;
900
901         DBG("element %p name %s", element, element->name);
902
903         __connman_element_lock(element);
904
905         property = g_hash_table_lookup(element->properties, name);
906         if (property != NULL) {
907                 switch (property->type) {
908                 case DBUS_TYPE_STRING:
909                         *((char **) value) = property->value;
910                         found = TRUE;
911                         break;
912                 case DBUS_TYPE_BOOLEAN:
913                 case DBUS_TYPE_BYTE:
914                         memcpy(value, property->value, 1);
915                         found = TRUE;
916                         break;
917                 }
918         }
919
920         __connman_element_unlock(element);
921
922         if (found == FALSE && element->parent != NULL)
923                 return get_static_property(element->parent, name, value);
924
925         return found;
926 }
927
928 static gboolean get_static_array_property(struct connman_element *element,
929                         const char *name, void *value, unsigned int *len)
930 {
931         struct connman_property *property;
932         gboolean found = FALSE;
933
934         DBG("element %p name %s", element, element->name);
935
936         __connman_element_lock(element);
937
938         property = g_hash_table_lookup(element->properties, name);
939         if (property != NULL) {
940                 *((void **) value) = property->value;
941                 *len = property->size;
942                 found = TRUE;
943         }
944
945         __connman_element_unlock(element);
946
947         return found;
948 }
949
950 #if 0
951 static gboolean match_static_property(struct connman_element *element,
952                                         const char *name, const void *value)
953 {
954         struct connman_property *property;
955         gboolean result = FALSE;
956
957         DBG("element %p name %s", element, element->name);
958
959         __connman_element_lock(element);
960
961         property = g_hash_table_lookup(element->properties, name);
962         if (property != NULL) {
963                 if (property->type == DBUS_TYPE_STRING)
964                         result = g_str_equal(property->value,
965                                                 *((const char **) value));
966         }
967
968         __connman_element_unlock(element);
969
970         return result;
971 }
972 #endif
973
974 /**
975  * connman_element_set_string:
976  * @element: element structure
977  * @key: unique identifier
978  * @value: string value
979  *
980  * Set string value for specific key
981  */
982 int connman_element_set_string(struct connman_element *element,
983                                         const char *key, const char *value)
984 {
985         return set_static_property(element, key, DBUS_TYPE_STRING, &value);
986 }
987
988 /**
989  * connman_element_get_string:
990  * @element: element structure
991  * @key: unique identifier
992  *
993  * Get string value for specific key
994  */
995 const char *connman_element_get_string(struct connman_element *element,
996                                                         const char *key)
997 {
998         const char *value;
999
1000         if (get_static_property(element, key, &value) == FALSE)
1001                 return NULL;
1002
1003         return value;
1004 }
1005
1006 /**
1007  * connman_element_set_bool:
1008  * @element: element structure
1009  * @key: unique identifier
1010  * @value: boolean value
1011  *
1012  * Set boolean value for specific key
1013  */
1014 int connman_element_set_bool(struct connman_element *element,
1015                                         const char *key, connman_bool_t value)
1016 {
1017         return set_static_property(element, key, DBUS_TYPE_BOOLEAN, &value);
1018 }
1019
1020 /**
1021  * connman_element_get_bool:
1022  * @element: element structure
1023  * @key: unique identifier
1024  *
1025  * Get boolean value for specific key
1026  */
1027 connman_bool_t connman_element_get_bool(struct connman_element *element,
1028                                                         const char *key)
1029 {
1030         connman_bool_t value;
1031
1032         if (get_static_property(element, key, &value) == FALSE)
1033                 return FALSE;
1034
1035         return value;
1036 }
1037
1038 /**
1039  * connman_element_set_uint8:
1040  * @element: element structure
1041  * @key: unique identifier
1042  * @value: integer value
1043  *
1044  * Set integer value for specific key
1045  */
1046 int connman_element_set_uint8(struct connman_element *element,
1047                                         const char *key, connman_uint8_t value)
1048 {
1049         return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
1050 }
1051
1052 /**
1053  * connman_element_get_uint8:
1054  * @element: element structure
1055  * @key: unique identifier
1056  *
1057  * Get integer value for specific key
1058  */
1059 connman_uint8_t connman_element_get_uint8(struct connman_element *element,
1060                                                         const char *key)
1061 {
1062         connman_uint8_t value;
1063
1064         if (get_static_property(element, key, &value) == FALSE)
1065                 return 0;
1066
1067         return value;
1068 }
1069
1070 /**
1071  * connman_element_set_blob:
1072  * @element: element structure
1073  * @key: unique identifier
1074  * @data: blob data
1075  * @size: blob size
1076  *
1077  * Set binary blob value for specific key
1078  */
1079 int connman_element_set_blob(struct connman_element *element,
1080                         const char *key, const void *data, unsigned int size)
1081 {
1082         return set_static_array_property(element, key,
1083                                                 DBUS_TYPE_BYTE, &data, size);
1084 }
1085
1086 /**
1087  * connman_element_get_blob:
1088  * @element: element structure
1089  * @key: unique identifier
1090  * @size: pointer to blob size
1091  *
1092  * Get binary blob value for specific key
1093  */
1094 const void *connman_element_get_blob(struct connman_element *element,
1095                                         const char *key, unsigned int *size)
1096 {
1097         void *value;
1098
1099         if (get_static_array_property(element, key, &value, size) == FALSE)
1100                 return NULL;
1101
1102         return value;
1103 }
1104
1105 int __connman_element_append_ipv4(struct connman_element *element,
1106                                                 DBusMessageIter *dict)
1107 {
1108         const char *method = NULL;
1109         const char *address = NULL, *netmask = NULL, *gateway = NULL;
1110         const char *broadcast = NULL, *nameserver = NULL;
1111         const char *timeserver = NULL, *pac = NULL;
1112
1113         connman_element_get_value(element,
1114                                 CONNMAN_PROPERTY_ID_IPV4_METHOD, &method);
1115
1116         connman_element_get_value(element,
1117                                 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
1118         connman_element_get_value(element,
1119                                 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
1120         connman_element_get_value(element,
1121                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
1122         connman_element_get_value(element,
1123                         CONNMAN_PROPERTY_ID_IPV4_BROADCAST, &broadcast);
1124         connman_element_get_value(element,
1125                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
1126         connman_element_get_value(element,
1127                         CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, &timeserver);
1128         connman_element_get_value(element,
1129                         CONNMAN_PROPERTY_ID_IPV4_PAC, &pac);
1130
1131         if (method != NULL)
1132                 connman_dbus_dict_append_basic(dict, "IPv4.Method",
1133                                                 DBUS_TYPE_STRING, &method);
1134
1135         if (address != NULL)
1136                 connman_dbus_dict_append_basic(dict, "IPv4.Address",
1137                                                 DBUS_TYPE_STRING, &address);
1138
1139         if (netmask != NULL)
1140                 connman_dbus_dict_append_basic(dict, "IPv4.Netmask",
1141                                                 DBUS_TYPE_STRING, &netmask);
1142
1143         if (gateway != NULL)
1144                 connman_dbus_dict_append_basic(dict, "IPv4.Gateway",
1145                                                 DBUS_TYPE_STRING, &gateway);
1146
1147         if (broadcast != NULL)
1148                 connman_dbus_dict_append_basic(dict, "IPv4.Broadcast",
1149                                                 DBUS_TYPE_STRING, &broadcast);
1150
1151         if (nameserver != NULL)
1152                 connman_dbus_dict_append_basic(dict, "IPv4.Nameserver",
1153                                                 DBUS_TYPE_STRING, &nameserver);
1154
1155         if (timeserver != NULL)
1156                 connman_dbus_dict_append_basic(dict, "IPv4.Timeserver",
1157                                                 DBUS_TYPE_STRING, &timeserver);
1158
1159         if (pac != NULL)
1160                 connman_dbus_dict_append_basic(dict, "IPv4.PAC",
1161                                                 DBUS_TYPE_STRING, &pac);
1162
1163         return 0;
1164 }
1165
1166 int __connman_element_set_ipv4(struct connman_element *element,
1167                                 const char *name, DBusMessageIter *value)
1168 {
1169         int type;
1170
1171         type = dbus_message_iter_get_arg_type(value);
1172
1173         if (g_str_equal(name, "IPv4.Method") == TRUE) {
1174                 enum connman_ipconfig_method method;
1175                 const char *str;
1176
1177                 if (type != DBUS_TYPE_STRING)
1178                         return -EINVAL;
1179
1180                 dbus_message_iter_get_basic(value, &str);
1181                 method = __connman_ipconfig_string2method(str);
1182                 if (method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
1183                         return -EINVAL;
1184
1185                 if (method == element->ipv4.method)
1186                         return -EALREADY;
1187
1188                 element->ipv4.method = method;
1189
1190                 connman_element_update(element);
1191         } else if (g_str_equal(name, "IPv4.Address") == TRUE) {
1192                 const char *address;
1193
1194                 if (type != DBUS_TYPE_STRING)
1195                         return -EINVAL;
1196
1197                 dbus_message_iter_get_basic(value, &address);
1198
1199                 g_free(element->ipv4.address);
1200                 element->ipv4.address = g_strdup(address);
1201
1202                 connman_element_update(element);
1203         } else if (g_str_equal(name, "IPv4.Netmask") == TRUE) {
1204                 const char *netmask;
1205
1206                 if (type != DBUS_TYPE_STRING)
1207                         return -EINVAL;
1208
1209                 dbus_message_iter_get_basic(value, &netmask);
1210
1211                 g_free(element->ipv4.netmask);
1212                 element->ipv4.netmask = g_strdup(netmask);
1213
1214                 connman_element_update(element);
1215         } else if (g_str_equal(name, "IPv4.Gateway") == TRUE) {
1216                 const char *gateway;
1217
1218                 if (type != DBUS_TYPE_STRING)
1219                         return -EINVAL;
1220
1221                 dbus_message_iter_get_basic(value, &gateway);
1222
1223                 g_free(element->ipv4.gateway);
1224                 element->ipv4.gateway = g_strdup(gateway);
1225
1226                 connman_element_update(element);
1227         } else if (g_str_equal(name, "IPv4.Broadcast") == TRUE) {
1228                 const char *broadcast;
1229
1230                 if (type != DBUS_TYPE_STRING)
1231                         return -EINVAL;
1232
1233                 dbus_message_iter_get_basic(value, &broadcast);
1234
1235                 g_free(element->ipv4.broadcast);
1236                 element->ipv4.broadcast = g_strdup(broadcast);
1237
1238                 connman_element_update(element);
1239         } else if (g_str_equal(name, "IPv4.Nameserver") == TRUE) {
1240                 const char *nameserver;
1241
1242                 if (type != DBUS_TYPE_STRING)
1243                         return -EINVAL;
1244
1245                 dbus_message_iter_get_basic(value, &nameserver);
1246
1247                 g_free(element->ipv4.nameserver);
1248                 element->ipv4.nameserver = g_strdup(nameserver);
1249
1250                 connman_element_update(element);
1251         } else if (g_str_equal(name, "IPv4.Timeserver") == TRUE) {
1252                 const char *timeserver;
1253
1254                 if (type != DBUS_TYPE_STRING)
1255                         return -EINVAL;
1256
1257                 dbus_message_iter_get_basic(value, &timeserver);
1258
1259                 g_free(element->ipv4.timeserver);
1260                 element->ipv4.timeserver = g_strdup(timeserver);
1261
1262                 connman_element_update(element);
1263         } else if (g_str_equal(name, "IPv4.PAC") == TRUE) {
1264                 const char *pac;
1265
1266                 if (type != DBUS_TYPE_STRING)
1267                         return -EINVAL;
1268
1269                 dbus_message_iter_get_basic(value, &pac);
1270
1271                 g_free(element->ipv4.pac);
1272                 element->ipv4.pac = g_strdup(pac);
1273
1274                 connman_element_update(element);
1275         }
1276
1277         return 0;
1278 }
1279
1280 static void emit_state_change(DBusConnection *conn, const char *state)
1281 {
1282         DBusMessage *signal;
1283         DBusMessageIter iter;
1284
1285         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1286                                 CONNMAN_MANAGER_INTERFACE, "State",
1287                                                 DBUS_TYPE_STRING, &state);
1288
1289         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1290                                 CONNMAN_MANAGER_INTERFACE, "StateChanged");
1291         if (signal == NULL)
1292                 return;
1293
1294         dbus_message_iter_init_append(signal, &iter);
1295         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &state);
1296
1297         g_dbus_send_message(conn, signal);
1298 }
1299
1300 static void probe_element(struct connman_element *element)
1301 {
1302         GSList *list;
1303
1304         DBG("element %p name %s", element, element->name);
1305
1306         for (list = driver_list; list; list = list->next) {
1307                 struct connman_driver *driver = list->data;
1308
1309                 if (match_driver(element, driver) == FALSE)
1310                         continue;
1311
1312                 DBG("driver %p name %s", driver, driver->name);
1313
1314                 if (driver->probe(element) == 0) {
1315                         __connman_element_lock(element);
1316                         element->driver = driver;
1317                         __connman_element_unlock(element);
1318                         break;
1319                 }
1320         }
1321 }
1322
1323 static void register_element(gpointer data, gpointer user_data)
1324 {
1325         struct connman_element *element = data;
1326         const gchar *basepath;
1327         GNode *node;
1328
1329         __connman_element_lock(element);
1330
1331         if (element->parent) {
1332                 node = g_node_find(element_root, G_PRE_ORDER,
1333                                         G_TRAVERSE_ALL, element->parent);
1334                 basepath = element->parent->path;
1335         } else {
1336                 element->parent = element_root->data;
1337
1338                 node = element_root;
1339                 basepath = "/device";
1340         }
1341
1342         element->path = g_strdup_printf("%s/%s", basepath, element->name);
1343
1344         __connman_element_unlock(element);
1345
1346         if (node == NULL) {
1347                 connman_error("Element registration for %s failed",
1348                                                         element->path);
1349                 return;
1350         }
1351
1352         DBG("element %p path %s", element, element->path);
1353
1354         g_node_append_data(node, element);
1355
1356         if (element->type == CONNMAN_ELEMENT_TYPE_DHCP) {
1357                 element->parent->configuring = TRUE;
1358
1359 #if 0
1360                 if (__connman_element_count(NULL,
1361                                         CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1362                         emit_state_change(connection, "connecting");
1363 #endif
1364         }
1365
1366         if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1367                 struct connman_element *parent = element->parent;
1368
1369                 while (parent) {
1370                         parent->configuring = FALSE;
1371                         parent = parent->parent;
1372                 }
1373
1374                 if (__connman_element_count(NULL,
1375                                         CONNMAN_ELEMENT_TYPE_CONNECTION) == 1)
1376                         emit_state_change(connection, "online");
1377         }
1378
1379         if (started == FALSE)
1380                 return;
1381
1382         probe_element(element);
1383 }
1384
1385 gboolean __connman_element_device_isfiltered(const char *devname)
1386 {
1387         if (device_filter == NULL)
1388                 goto nodevice;
1389
1390         if (g_pattern_match_simple(device_filter, devname) == FALSE) {
1391                 DBG("ignoring device %s (match)", devname);
1392                 return TRUE;
1393         }
1394
1395 nodevice:
1396         if (nodevice_filter == NULL)
1397                 return FALSE;
1398
1399         if (g_pattern_match_simple(nodevice_filter, devname) == TRUE) {
1400                 DBG("ignoring device %s (no match)", devname);
1401                 return TRUE;
1402         }
1403
1404         return FALSE;
1405 }
1406
1407 /**
1408  * connman_element_register:
1409  * @element: the element to register
1410  * @parent: the parent to register the element with
1411  *
1412  * Register an element with the core. It will be register under the given
1413  * parent of if %NULL is provided under the root element.
1414  *
1415  * Returns: %0 on success
1416  */
1417 int connman_element_register(struct connman_element *element,
1418                                         struct connman_element *parent)
1419 {
1420         DBG("element %p name %s parent %p", element, element->name, parent);
1421
1422         if (element->devname == NULL)
1423                 element->devname = g_strdup(element->name);
1424
1425         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
1426                 goto setup;
1427
1428         if (__connman_element_device_isfiltered(element->devname) == TRUE)
1429                 return -EPERM;
1430
1431 setup:
1432         if (connman_element_ref(element) == NULL)
1433                 return -EINVAL;
1434
1435         __connman_element_lock(element);
1436
1437         if (element->name == NULL) {
1438                 element->name = g_strdup(type2string(element->type));
1439                 if (element->name == NULL) {
1440                         __connman_element_unlock(element);
1441                         return -EINVAL;
1442                 }
1443         }
1444
1445         if (element->type == CONNMAN_ELEMENT_TYPE_DHCP)
1446                 element->ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
1447
1448         element->parent = parent;
1449
1450         __connman_element_unlock(element);
1451
1452         register_element(element, NULL);
1453
1454         return 0;
1455 }
1456
1457 static gboolean remove_element(GNode *node, gpointer user_data)
1458 {
1459         struct connman_element *element = node->data;
1460         struct connman_element *root = user_data;
1461
1462         DBG("element %p name %s", element, element->name);
1463
1464         if (element == root)
1465                 return FALSE;
1466
1467         if (node != NULL)
1468                 g_node_unlink(node);
1469
1470         if (element->driver) {
1471                 if (element->driver->remove)
1472                         element->driver->remove(element);
1473
1474                 __connman_element_lock(element);
1475                 element->driver = NULL;
1476                 __connman_element_unlock(element);
1477         }
1478
1479         if (node != NULL)
1480                 g_node_destroy(node);
1481
1482         if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1483                 if (__connman_element_count(NULL,
1484                                         CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1485                         emit_state_change(connection, "offline");
1486         }
1487
1488         connman_element_unref(element);
1489
1490         return FALSE;
1491 }
1492
1493 void connman_element_unregister(struct connman_element *element)
1494 {
1495         GNode *node;
1496
1497         DBG("element %p name %s", element, element->name);
1498
1499         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1500
1501         if (node != NULL)
1502                 g_node_traverse(node, G_POST_ORDER,
1503                                 G_TRAVERSE_ALL, -1, remove_element, NULL);
1504 }
1505
1506 void connman_element_unregister_children(struct connman_element *element)
1507 {
1508         GNode *node;
1509
1510         DBG("element %p name %s", element, element->name);
1511
1512         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1513
1514         if (node != NULL)
1515                 g_node_traverse(node, G_POST_ORDER,
1516                                 G_TRAVERSE_ALL, -1, remove_element, element);
1517 }
1518
1519 static gboolean update_element(GNode *node, gpointer user_data)
1520 {
1521         struct connman_element *element = node->data;
1522
1523         DBG("element %p name %s", element, element->name);
1524
1525         if (element->driver && element->driver->update)
1526                 element->driver->update(element);
1527
1528         return FALSE;
1529 }
1530
1531 void connman_element_update(struct connman_element *element)
1532 {
1533         GNode *node;
1534
1535         DBG("element %p name %s", element, element->name);
1536
1537         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1538
1539         if (node != NULL)
1540                 g_node_traverse(node, G_PRE_ORDER,
1541                                 G_TRAVERSE_ALL, -1, update_element, element);
1542 }
1543
1544 int connman_element_set_enabled(struct connman_element *element,
1545                                                         gboolean enabled)
1546 {
1547         if (element->enabled == enabled)
1548                 return 0;
1549
1550         element->enabled = enabled;
1551
1552         connman_element_update(element);
1553
1554         return 0;
1555 }
1556
1557 static enum connman_service_error convert_error(enum connman_element_error error)
1558 {
1559         switch (error) {
1560         case CONNMAN_ELEMENT_ERROR_UNKNOWN:
1561         case CONNMAN_ELEMENT_ERROR_FAILED:
1562                 break;
1563         case CONNMAN_ELEMENT_ERROR_DHCP_FAILED:
1564                 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
1565         case CONNMAN_ELEMENT_ERROR_CONNECT_FAILED:
1566                 return CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
1567         }
1568
1569         return CONNMAN_SERVICE_ERROR_UNKNOWN;
1570 }
1571
1572 /**
1573  * connman_element_set_error:
1574  * @element: element structure
1575  * @error: error identifier
1576  *
1577  * Set error state and specific error identifier
1578  */
1579 void connman_element_set_error(struct connman_element *element,
1580                                         enum connman_element_error error)
1581 {
1582         struct connman_service *service;
1583
1584         DBG("element %p error %d", element, error);
1585
1586         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1587                 return;
1588
1589         element->state = CONNMAN_ELEMENT_STATE_ERROR;
1590         element->error = error;
1591
1592         if (element->driver && element->driver->change)
1593                 element->driver->change(element);
1594
1595         service = __connman_element_get_service(element);
1596         __connman_service_indicate_error(service, convert_error(error));
1597 }
1598
1599 int __connman_element_init(const char *device, const char *nodevice)
1600 {
1601         struct connman_element *element;
1602
1603         DBG("");
1604
1605         connection = connman_dbus_get_connection();
1606         if (connection == NULL)
1607                 return -EIO;
1608
1609         device_filter = g_strdup(device);
1610         nodevice_filter = g_strdup(nodevice);
1611
1612         element = connman_element_create("root");
1613
1614         element->path = g_strdup("/");
1615         element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1616
1617         element_root = g_node_new(element);
1618
1619         __connman_technology_init();
1620         __connman_notifier_init();
1621         __connman_service_init();
1622         __connman_provider_init();
1623         __connman_network_init();
1624         __connman_device_init();
1625
1626         return 0;
1627 }
1628
1629 static gboolean probe_node(GNode *node, gpointer data)
1630 {
1631         struct connman_element *element = node->data;
1632
1633         DBG("element %p name %s", element, element->name);
1634
1635         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1636                 return FALSE;
1637
1638         if (element->driver)
1639                 return FALSE;
1640
1641         probe_element(element);
1642
1643         return FALSE;
1644 }
1645
1646 void __connman_element_start(void)
1647 {
1648         DBG("");
1649
1650         __connman_storage_init_profile();
1651
1652         g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1653                                                         probe_node, NULL);
1654
1655         started = TRUE;
1656
1657         __connman_rtnl_start();
1658         __connman_udev_start();
1659
1660         __connman_connection_init();
1661         __connman_ipv4_init();
1662         __connman_dhcp_init();
1663
1664         if (__connman_rfkill_init() < 0)
1665                 __connman_udev_enable_rfkill_processing();
1666 }
1667
1668 void __connman_element_stop(void)
1669 {
1670         DBG("");
1671
1672         __connman_rfkill_cleanup();
1673
1674         __connman_dhcp_cleanup();
1675         __connman_ipv4_cleanup();
1676         __connman_provider_cleanup();
1677         __connman_connection_cleanup();
1678 }
1679
1680 static gboolean free_driver(GNode *node, gpointer data)
1681 {
1682         struct connman_element *element = node->data;
1683
1684         DBG("element %p name %s", element, element->name);
1685
1686         if (element->driver) {
1687                 if (element->driver->remove)
1688                         element->driver->remove(element);
1689
1690                 __connman_element_lock(element);
1691                 element->driver = NULL;
1692                 __connman_element_unlock(element);
1693         }
1694
1695         return FALSE;
1696 }
1697
1698 static gboolean free_node(GNode *node, gpointer data)
1699 {
1700         struct connman_element *element = node->data;
1701
1702         DBG("element %p name %s", element, element->name);
1703
1704         if (g_node_depth(node) > 1)
1705                 connman_element_unregister(element);
1706
1707         return FALSE;
1708 }
1709
1710 void __connman_element_cleanup(void)
1711 {
1712         DBG("");
1713
1714         __connman_device_cleanup();
1715         __connman_network_cleanup();
1716         __connman_service_cleanup();
1717         __connman_notifier_cleanup();
1718         __connman_technology_cleanup();
1719
1720         g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1721                                                         free_driver, NULL);
1722
1723         g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1724                                                         free_node, NULL);
1725
1726         g_node_destroy(element_root);
1727         element_root = NULL;
1728
1729         g_free(nodevice_filter);
1730         g_free(device_filter);
1731
1732         if (connection == NULL)
1733                 return;
1734
1735         dbus_connection_unref(connection);
1736 }