Add service types for system and VPN connections
[framework/connectivity/connman.git] / src / element.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 <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->devname);
686                 g_free(element->path);
687                 g_free(element->name);
688                 g_free(element);
689         }
690 }
691
692 static int set_static_property(struct connman_element *element,
693                                 const char *name, int type, const void *value)
694 {
695         struct connman_property *property;
696
697         DBG("element %p name %s", element, element->name);
698
699         if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
700                 return -EINVAL;
701
702         property = g_try_new0(struct connman_property, 1);
703         if (property == NULL)
704                 return -ENOMEM;
705
706         property->id   = CONNMAN_PROPERTY_ID_INVALID;
707         property->type = type;
708
709         DBG("name %s type %d value %p", name, type, value);
710
711         switch (type) {
712         case DBUS_TYPE_STRING:
713                 property->value = g_strdup(*((const char **) value));
714                 break;
715         case DBUS_TYPE_BOOLEAN:
716         case DBUS_TYPE_BYTE:
717                 property->value = g_try_malloc(1);
718                 if (property->value != NULL)
719                         memcpy(property->value, value, 1);
720                 break;
721         }
722
723         __connman_element_lock(element);
724
725         g_hash_table_replace(element->properties, g_strdup(name), property);
726
727         __connman_element_unlock(element);
728
729         return 0;
730 }
731
732 static int set_static_array_property(struct connman_element *element,
733                         const char *name, int type, const void *value, int len)
734 {
735         struct connman_property *property;
736
737         DBG("element %p name %s", element, element->name);
738
739         if (type != DBUS_TYPE_BYTE)
740                 return -EINVAL;
741
742         property = g_try_new0(struct connman_property, 1);
743         if (property == NULL)
744                 return -ENOMEM;
745
746         property->id      = CONNMAN_PROPERTY_ID_INVALID;
747         property->type    = DBUS_TYPE_ARRAY;
748         property->subtype = type;
749
750         DBG("name %s type %d value %p", name, type, value);
751
752         switch (type) {
753         case DBUS_TYPE_BYTE:
754                 property->value = g_try_malloc(len);
755                 if (property->value != NULL) {
756                         memcpy(property->value,
757                                 *((const unsigned char **) value), len);
758                         property->size = len;
759                 }
760                 break;
761         }
762
763         __connman_element_lock(element);
764
765         g_hash_table_replace(element->properties, g_strdup(name), property);
766
767         __connman_element_unlock(element);
768
769         return 0;
770 }
771
772 #if 0
773 static int set_property(struct connman_element *element,
774                                 enum connman_property_id id, const void *value)
775 {
776         switch (id) {
777         case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
778                 __connman_element_lock(element);
779                 g_free(element->ipv4.address);
780                 element->ipv4.address = g_strdup(*((const char **) value));
781                 __connman_element_unlock(element);
782                 break;
783         case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
784                 __connman_element_lock(element);
785                 g_free(element->ipv4.netmask);
786                 element->ipv4.netmask = g_strdup(*((const char **) value));
787                 __connman_element_unlock(element);
788                 break;
789         case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
790                 __connman_element_lock(element);
791                 g_free(element->ipv4.gateway);
792                 element->ipv4.gateway = g_strdup(*((const char **) value));
793                 __connman_element_unlock(element);
794                 break;
795         case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
796                 __connman_element_lock(element);
797                 g_free(element->ipv4.broadcast);
798                 element->ipv4.broadcast = g_strdup(*((const char **) value));
799                 __connman_element_unlock(element);
800                 break;
801         case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
802                 __connman_element_lock(element);
803                 g_free(element->ipv4.nameserver);
804                 element->ipv4.nameserver = g_strdup(*((const char **) value));
805                 __connman_element_unlock(element);
806                 break;
807         default:
808                 return -EINVAL;
809         }
810
811         return 0;
812 }
813 #endif
814
815 int connman_element_get_value(struct connman_element *element,
816                                 enum connman_property_id id, void *value)
817 {
818         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
819                 return -EINVAL;
820
821         switch (id) {
822         case CONNMAN_PROPERTY_ID_IPV4_METHOD:
823                 if (element->ipv4.method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
824                         return connman_element_get_value(element->parent,
825                                                                 id, value);
826                 __connman_element_lock(element);
827                 *((const char **) value) = __connman_ipconfig_method2string(element->ipv4.method);
828                 __connman_element_unlock(element);
829                 break;
830         case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
831                 if (element->ipv4.address == NULL)
832                         return connman_element_get_value(element->parent,
833                                                                 id, value);
834                 __connman_element_lock(element);
835                 *((char **) value) = element->ipv4.address;
836                 __connman_element_unlock(element);
837                 break;
838         case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
839                 if (element->ipv4.netmask == NULL)
840                         return connman_element_get_value(element->parent,
841                                                                 id, value);
842                 __connman_element_lock(element);
843                 *((char **) value) = element->ipv4.netmask;
844                 __connman_element_unlock(element);
845                 break;
846         case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
847                 if (element->ipv4.gateway == NULL)
848                         return connman_element_get_value(element->parent,
849                                                                 id, value);
850                 __connman_element_lock(element);
851                 *((char **) value) = element->ipv4.gateway;
852                 __connman_element_unlock(element);
853                 break;
854         case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
855                 if (element->ipv4.broadcast == NULL)
856                         return connman_element_get_value(element->parent,
857                                                                 id, value);
858                 __connman_element_lock(element);
859                 *((char **) value) = element->ipv4.broadcast;
860                 __connman_element_unlock(element);
861                 break;
862         case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
863                 if (element->ipv4.nameserver == NULL)
864                         return connman_element_get_value(element->parent,
865                                                                 id, value);
866                 __connman_element_lock(element);
867                 *((char **) value) = element->ipv4.nameserver;
868                 __connman_element_unlock(element);
869                 break;
870         default:
871                 return -EINVAL;
872         }
873
874         return 0;
875 }
876
877 static gboolean get_static_property(struct connman_element *element,
878                                                 const char *name, void *value)
879 {
880         struct connman_property *property;
881         gboolean found = FALSE;
882
883         DBG("element %p name %s", element, element->name);
884
885         __connman_element_lock(element);
886
887         property = g_hash_table_lookup(element->properties, name);
888         if (property != NULL) {
889                 switch (property->type) {
890                 case DBUS_TYPE_STRING:
891                         *((char **) value) = property->value;
892                         found = TRUE;
893                         break;
894                 case DBUS_TYPE_BOOLEAN:
895                 case DBUS_TYPE_BYTE:
896                         memcpy(value, property->value, 1);
897                         found = TRUE;
898                         break;
899                 }
900         }
901
902         __connman_element_unlock(element);
903
904         if (found == FALSE && element->parent != NULL)
905                 return get_static_property(element->parent, name, value);
906
907         return found;
908 }
909
910 static gboolean get_static_array_property(struct connman_element *element,
911                         const char *name, void *value, unsigned int *len)
912 {
913         struct connman_property *property;
914         gboolean found = FALSE;
915
916         DBG("element %p name %s", element, element->name);
917
918         __connman_element_lock(element);
919
920         property = g_hash_table_lookup(element->properties, name);
921         if (property != NULL) {
922                 *((void **) value) = property->value;
923                 *len = property->size;
924                 found = TRUE;
925         }
926
927         __connman_element_unlock(element);
928
929         return found;
930 }
931
932 #if 0
933 static gboolean match_static_property(struct connman_element *element,
934                                         const char *name, const void *value)
935 {
936         struct connman_property *property;
937         gboolean result = FALSE;
938
939         DBG("element %p name %s", element, element->name);
940
941         __connman_element_lock(element);
942
943         property = g_hash_table_lookup(element->properties, name);
944         if (property != NULL) {
945                 if (property->type == DBUS_TYPE_STRING)
946                         result = g_str_equal(property->value,
947                                                 *((const char **) value));
948         }
949
950         __connman_element_unlock(element);
951
952         return result;
953 }
954 #endif
955
956 /**
957  * connman_element_set_string:
958  * @element: element structure
959  * @key: unique identifier
960  * @value: string value
961  *
962  * Set string value for specific key
963  */
964 int connman_element_set_string(struct connman_element *element,
965                                         const char *key, const char *value)
966 {
967         return set_static_property(element, key, DBUS_TYPE_STRING, &value);
968 }
969
970 /**
971  * connman_element_get_string:
972  * @element: element structure
973  * @key: unique identifier
974  *
975  * Get string value for specific key
976  */
977 const char *connman_element_get_string(struct connman_element *element,
978                                                         const char *key)
979 {
980         const char *value;
981
982         if (get_static_property(element, key, &value) == FALSE)
983                 return NULL;
984
985         return value;
986 }
987
988 /**
989  * connman_element_set_bool:
990  * @element: element structure
991  * @key: unique identifier
992  * @value: boolean value
993  *
994  * Set boolean value for specific key
995  */
996 int connman_element_set_bool(struct connman_element *element,
997                                         const char *key, connman_bool_t value)
998 {
999         return set_static_property(element, key, DBUS_TYPE_BOOLEAN, &value);
1000 }
1001
1002 /**
1003  * connman_element_get_bool:
1004  * @element: element structure
1005  * @key: unique identifier
1006  *
1007  * Get boolean value for specific key
1008  */
1009 connman_bool_t connman_element_get_bool(struct connman_element *element,
1010                                                         const char *key)
1011 {
1012         connman_bool_t value;
1013
1014         if (get_static_property(element, key, &value) == FALSE)
1015                 return FALSE;
1016
1017         return value;
1018 }
1019
1020 /**
1021  * connman_element_set_uint8:
1022  * @element: element structure
1023  * @key: unique identifier
1024  * @value: integer value
1025  *
1026  * Set integer value for specific key
1027  */
1028 int connman_element_set_uint8(struct connman_element *element,
1029                                         const char *key, connman_uint8_t value)
1030 {
1031         return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
1032 }
1033
1034 /**
1035  * connman_element_get_uint8:
1036  * @element: element structure
1037  * @key: unique identifier
1038  *
1039  * Get integer value for specific key
1040  */
1041 connman_uint8_t connman_element_get_uint8(struct connman_element *element,
1042                                                         const char *key)
1043 {
1044         connman_uint8_t value;
1045
1046         if (get_static_property(element, key, &value) == FALSE)
1047                 return 0;
1048
1049         return value;
1050 }
1051
1052 /**
1053  * connman_element_set_blob:
1054  * @element: element structure
1055  * @key: unique identifier
1056  * @data: blob data
1057  * @size: blob size
1058  *
1059  * Set binary blob value for specific key
1060  */
1061 int connman_element_set_blob(struct connman_element *element,
1062                         const char *key, const void *data, unsigned int size)
1063 {
1064         return set_static_array_property(element, key,
1065                                                 DBUS_TYPE_BYTE, &data, size);
1066 }
1067
1068 /**
1069  * connman_element_get_blob:
1070  * @element: element structure
1071  * @key: unique identifier
1072  * @size: pointer to blob size
1073  *
1074  * Get binary blob value for specific key
1075  */
1076 const void *connman_element_get_blob(struct connman_element *element,
1077                                         const char *key, unsigned int *size)
1078 {
1079         void *value;
1080
1081         if (get_static_array_property(element, key, &value, size) == FALSE)
1082                 return NULL;
1083
1084         return value;
1085 }
1086
1087 int __connman_element_append_ipv4(struct connman_element *element,
1088                                                 DBusMessageIter *dict)
1089 {
1090         const char *method = NULL;
1091         const char *address = NULL, *netmask = NULL, *gateway = NULL;
1092         const char *broadcast = NULL, *nameserver = NULL;
1093
1094         connman_element_get_value(element,
1095                                 CONNMAN_PROPERTY_ID_IPV4_METHOD, &method);
1096
1097         connman_element_get_value(element,
1098                                 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
1099         connman_element_get_value(element,
1100                                 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
1101         connman_element_get_value(element,
1102                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
1103         connman_element_get_value(element,
1104                         CONNMAN_PROPERTY_ID_IPV4_BROADCAST, &broadcast);
1105         connman_element_get_value(element,
1106                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
1107
1108         if (method != NULL)
1109                 connman_dbus_dict_append_variant(dict, "IPv4.Method",
1110                                                 DBUS_TYPE_STRING, &method);
1111
1112         if (address != NULL)
1113                 connman_dbus_dict_append_variant(dict, "IPv4.Address",
1114                                                 DBUS_TYPE_STRING, &address);
1115
1116         if (netmask != NULL)
1117                 connman_dbus_dict_append_variant(dict, "IPv4.Netmask",
1118                                                 DBUS_TYPE_STRING, &netmask);
1119
1120         if (gateway != NULL)
1121                 connman_dbus_dict_append_variant(dict, "IPv4.Gateway",
1122                                                 DBUS_TYPE_STRING, &gateway);
1123
1124         if (broadcast != NULL)
1125                 connman_dbus_dict_append_variant(dict, "IPv4.Broadcast",
1126                                                 DBUS_TYPE_STRING, &broadcast);
1127
1128         if (nameserver != NULL)
1129                 connman_dbus_dict_append_variant(dict, "IPv4.Nameserver",
1130                                                 DBUS_TYPE_STRING, &nameserver);
1131
1132         return 0;
1133 }
1134
1135 int __connman_element_set_ipv4(struct connman_element *element,
1136                                 const char *name, DBusMessageIter *value)
1137 {
1138         int type;
1139
1140         type = dbus_message_iter_get_arg_type(value);
1141
1142         if (g_str_equal(name, "IPv4.Method") == TRUE) {
1143                 enum connman_ipconfig_method method;
1144                 const char *str;
1145
1146                 if (type != DBUS_TYPE_STRING)
1147                         return -EINVAL;
1148
1149                 dbus_message_iter_get_basic(value, &str);
1150                 method = __connman_ipconfig_string2method(str);
1151                 if (method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
1152                         return -EINVAL;
1153
1154                 if (method == element->ipv4.method)
1155                         return -EALREADY;
1156
1157                 element->ipv4.method = method;
1158
1159                 connman_element_update(element);
1160         } else if (g_str_equal(name, "IPv4.Address") == TRUE) {
1161                 const char *address;
1162
1163                 if (type != DBUS_TYPE_STRING)
1164                         return -EINVAL;
1165
1166                 dbus_message_iter_get_basic(value, &address);
1167
1168                 g_free(element->ipv4.address);
1169                 element->ipv4.address = g_strdup(address);
1170
1171                 connman_element_update(element);
1172         } else if (g_str_equal(name, "IPv4.Netmask") == TRUE) {
1173                 const char *netmask;
1174
1175                 if (type != DBUS_TYPE_STRING)
1176                         return -EINVAL;
1177
1178                 dbus_message_iter_get_basic(value, &netmask);
1179
1180                 g_free(element->ipv4.netmask);
1181                 element->ipv4.netmask = g_strdup(netmask);
1182
1183                 connman_element_update(element);
1184         } else if (g_str_equal(name, "IPv4.Gateway") == TRUE) {
1185                 const char *gateway;
1186
1187                 if (type != DBUS_TYPE_STRING)
1188                         return -EINVAL;
1189
1190                 dbus_message_iter_get_basic(value, &gateway);
1191
1192                 g_free(element->ipv4.gateway);
1193                 element->ipv4.gateway = g_strdup(gateway);
1194
1195                 connman_element_update(element);
1196         } else if (g_str_equal(name, "IPv4.Broadcast") == TRUE) {
1197                 const char *broadcast;
1198
1199                 if (type != DBUS_TYPE_STRING)
1200                         return -EINVAL;
1201
1202                 dbus_message_iter_get_basic(value, &broadcast);
1203
1204                 g_free(element->ipv4.broadcast);
1205                 element->ipv4.broadcast = g_strdup(broadcast);
1206
1207                 connman_element_update(element);
1208         } else if (g_str_equal(name, "IPv4.Nameserver") == TRUE) {
1209                 const char *nameserver;
1210
1211                 if (type != DBUS_TYPE_STRING)
1212                         return -EINVAL;
1213
1214                 dbus_message_iter_get_basic(value, &nameserver);
1215
1216                 g_free(element->ipv4.nameserver);
1217                 element->ipv4.nameserver = g_strdup(nameserver);
1218
1219                 connman_element_update(element);
1220         }
1221
1222         return 0;
1223 }
1224
1225 static void append_state(DBusMessageIter *entry, const char *state)
1226 {
1227         DBusMessageIter value;
1228         const char *key = "State";
1229
1230         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1231
1232         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1233                                         DBUS_TYPE_STRING_AS_STRING, &value);
1234         dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
1235         dbus_message_iter_close_container(entry, &value);
1236 }
1237
1238 static void emit_state_change(DBusConnection *conn, const char *state)
1239 {
1240         DBusMessage *signal;
1241         DBusMessageIter entry;
1242
1243         DBG("conn %p", conn);
1244
1245         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1246                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1247         if (signal == NULL)
1248                 return;
1249
1250         dbus_message_iter_init_append(signal, &entry);
1251
1252         append_state(&entry, state);
1253
1254         g_dbus_send_message(conn, signal);
1255
1256         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1257                                 CONNMAN_MANAGER_INTERFACE, "StateChanged");
1258         if (signal == NULL)
1259                 return;
1260
1261         dbus_message_iter_init_append(signal, &entry);
1262         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &state);
1263
1264         g_dbus_send_message(conn, signal);
1265 }
1266
1267 static void probe_element(struct connman_element *element)
1268 {
1269         GSList *list;
1270
1271         DBG("element %p name %s", element, element->name);
1272
1273         for (list = driver_list; list; list = list->next) {
1274                 struct connman_driver *driver = list->data;
1275
1276                 if (match_driver(element, driver) == FALSE)
1277                         continue;
1278
1279                 DBG("driver %p name %s", driver, driver->name);
1280
1281                 if (driver->probe(element) == 0) {
1282                         __connman_element_lock(element);
1283                         element->driver = driver;
1284                         __connman_element_unlock(element);
1285                         break;
1286                 }
1287         }
1288 }
1289
1290 static void register_element(gpointer data, gpointer user_data)
1291 {
1292         struct connman_element *element = data;
1293         const gchar *basepath;
1294         GNode *node;
1295
1296         __connman_element_lock(element);
1297
1298         if (element->parent) {
1299                 node = g_node_find(element_root, G_PRE_ORDER,
1300                                         G_TRAVERSE_ALL, element->parent);
1301                 basepath = element->parent->path;
1302         } else {
1303                 element->parent = element_root->data;
1304
1305                 node = element_root;
1306                 basepath = "/device";
1307         }
1308
1309         element->path = g_strdup_printf("%s/%s", basepath, element->name);
1310
1311         __connman_element_unlock(element);
1312
1313         if (node == NULL) {
1314                 connman_error("Element registration for %s failed",
1315                                                         element->path);
1316                 return;
1317         }
1318
1319         DBG("element %p path %s", element, element->path);
1320
1321         g_node_append_data(node, element);
1322
1323         if (element->type == CONNMAN_ELEMENT_TYPE_DHCP) {
1324                 element->parent->configuring = TRUE;
1325
1326 #if 0
1327                 if (__connman_element_count(NULL,
1328                                         CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1329                         emit_state_change(connection, "connecting");
1330 #endif
1331         }
1332
1333         if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1334                 struct connman_element *parent = element->parent;
1335
1336                 while (parent) {
1337                         parent->configuring = FALSE;
1338                         parent = parent->parent;
1339                 }
1340
1341                 if (__connman_element_count(NULL,
1342                                         CONNMAN_ELEMENT_TYPE_CONNECTION) == 1)
1343                         emit_state_change(connection, "online");
1344         }
1345
1346         if (started == FALSE)
1347                 return;
1348
1349         probe_element(element);
1350 }
1351
1352 gboolean __connman_element_device_isfiltered(const char *devname)
1353 {
1354         if (device_filter == NULL)
1355                 goto nodevice;
1356
1357         if (g_pattern_match_simple(device_filter, devname) == FALSE) {
1358                 DBG("ignoring device %s (match)", devname);
1359                 return TRUE;
1360         }
1361
1362 nodevice:
1363         if (nodevice_filter == NULL)
1364                 return FALSE;
1365
1366         if (g_pattern_match_simple(nodevice_filter, devname) == TRUE) {
1367                 DBG("ignoring device %s (no match)", devname);
1368                 return TRUE;
1369         }
1370
1371         return FALSE;
1372 }
1373
1374 /**
1375  * connman_element_register:
1376  * @element: the element to register
1377  * @parent: the parent to register the element with
1378  *
1379  * Register an element with the core. It will be register under the given
1380  * parent of if %NULL is provided under the root element.
1381  *
1382  * Returns: %0 on success
1383  */
1384 int connman_element_register(struct connman_element *element,
1385                                         struct connman_element *parent)
1386 {
1387         DBG("element %p name %s parent %p", element, element->name, parent);
1388
1389         if (element->devname == NULL)
1390                 element->devname = g_strdup(element->name);
1391
1392         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
1393                 goto setup;
1394
1395         if (__connman_element_device_isfiltered(element->devname) == TRUE)
1396                 return -EPERM;
1397
1398 setup:
1399         if (connman_element_ref(element) == NULL)
1400                 return -EINVAL;
1401
1402         __connman_element_lock(element);
1403
1404         if (element->name == NULL) {
1405                 element->name = g_strdup(type2string(element->type));
1406                 if (element->name == NULL) {
1407                         __connman_element_unlock(element);
1408                         return -EINVAL;
1409                 }
1410         }
1411
1412         if (element->type == CONNMAN_ELEMENT_TYPE_DHCP)
1413                 element->ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
1414
1415         element->parent = parent;
1416
1417         __connman_element_unlock(element);
1418
1419         register_element(element, NULL);
1420
1421         return 0;
1422 }
1423
1424 static gboolean remove_element(GNode *node, gpointer user_data)
1425 {
1426         struct connman_element *element = node->data;
1427         struct connman_element *root = user_data;
1428
1429         DBG("element %p name %s", element, element->name);
1430
1431         if (element == root)
1432                 return FALSE;
1433
1434         if (node != NULL)
1435                 g_node_unlink(node);
1436
1437         if (element->driver) {
1438                 if (element->driver->remove)
1439                         element->driver->remove(element);
1440
1441                 __connman_element_lock(element);
1442                 element->driver = NULL;
1443                 __connman_element_unlock(element);
1444         }
1445
1446         if (node != NULL)
1447                 g_node_destroy(node);
1448
1449         if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1450                 if (__connman_element_count(NULL,
1451                                         CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1452                         emit_state_change(connection, "offline");
1453         }
1454
1455         connman_element_unref(element);
1456
1457         return FALSE;
1458 }
1459
1460 void connman_element_unregister(struct connman_element *element)
1461 {
1462         GNode *node;
1463
1464         DBG("element %p name %s", element, element->name);
1465
1466         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1467
1468         if (node != NULL)
1469                 g_node_traverse(node, G_POST_ORDER,
1470                                 G_TRAVERSE_ALL, -1, remove_element, NULL);
1471 }
1472
1473 void connman_element_unregister_children(struct connman_element *element)
1474 {
1475         GNode *node;
1476
1477         DBG("element %p name %s", element, element->name);
1478
1479         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1480
1481         if (node != NULL)
1482                 g_node_traverse(node, G_POST_ORDER,
1483                                 G_TRAVERSE_ALL, -1, remove_element, element);
1484 }
1485
1486 static gboolean update_element(GNode *node, gpointer user_data)
1487 {
1488         struct connman_element *element = node->data;
1489
1490         DBG("element %p name %s", element, element->name);
1491
1492         if (element->driver && element->driver->update)
1493                 element->driver->update(element);
1494
1495         return FALSE;
1496 }
1497
1498 void connman_element_update(struct connman_element *element)
1499 {
1500         GNode *node;
1501
1502         DBG("element %p name %s", element, element->name);
1503
1504         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1505
1506         if (node != NULL)
1507                 g_node_traverse(node, G_PRE_ORDER,
1508                                 G_TRAVERSE_ALL, -1, update_element, element);
1509 }
1510
1511 int connman_element_set_enabled(struct connman_element *element,
1512                                                         gboolean enabled)
1513 {
1514         if (element->enabled == enabled)
1515                 return 0;
1516
1517         element->enabled = enabled;
1518
1519         connman_element_update(element);
1520
1521         return 0;
1522 }
1523
1524 static enum connman_service_error convert_error(enum connman_element_error error)
1525 {
1526         switch (error) {
1527         case CONNMAN_ELEMENT_ERROR_UNKNOWN:
1528         case CONNMAN_ELEMENT_ERROR_FAILED:
1529                 break;
1530         case CONNMAN_ELEMENT_ERROR_DHCP_FAILED:
1531                 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
1532         case CONNMAN_ELEMENT_ERROR_CONNECT_FAILED:
1533                 return CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
1534         }
1535
1536         return CONNMAN_SERVICE_ERROR_UNKNOWN;
1537 }
1538
1539 /**
1540  * connman_element_set_error:
1541  * @element: element structure
1542  * @error: error identifier
1543  *
1544  * Set error state and specific error identifier
1545  */
1546 void connman_element_set_error(struct connman_element *element,
1547                                         enum connman_element_error error)
1548 {
1549         struct connman_service *service;
1550
1551         DBG("element %p error %d", element, error);
1552
1553         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1554                 return;
1555
1556         element->state = CONNMAN_ELEMENT_STATE_ERROR;
1557         element->error = error;
1558
1559         if (element->driver && element->driver->change)
1560                 element->driver->change(element);
1561
1562         service = __connman_element_get_service(element);
1563         __connman_service_indicate_error(service, convert_error(error));
1564 }
1565
1566 int __connman_element_init(const char *device, const char *nodevice)
1567 {
1568         struct connman_element *element;
1569
1570         DBG("");
1571
1572         connection = connman_dbus_get_connection();
1573         if (connection == NULL)
1574                 return -EIO;
1575
1576         device_filter = g_strdup(device);
1577         nodevice_filter = g_strdup(nodevice);
1578
1579         element = connman_element_create("root");
1580
1581         element->path = g_strdup("/");
1582         element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1583
1584         element_root = g_node_new(element);
1585
1586         __connman_notifier_init();
1587         __connman_service_init();
1588         __connman_provider_init();
1589         __connman_network_init();
1590         __connman_device_init();
1591
1592         return 0;
1593 }
1594
1595 static gboolean probe_node(GNode *node, gpointer data)
1596 {
1597         struct connman_element *element = node->data;
1598
1599         DBG("element %p name %s", element, element->name);
1600
1601         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1602                 return FALSE;
1603
1604         if (element->driver)
1605                 return FALSE;
1606
1607         probe_element(element);
1608
1609         return FALSE;
1610 }
1611
1612 void __connman_element_start(void)
1613 {
1614         DBG("");
1615
1616         __connman_storage_init_profile();
1617
1618         g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1619                                                         probe_node, NULL);
1620
1621         started = TRUE;
1622
1623         __connman_rtnl_start();
1624         __connman_udev_start();
1625
1626         __connman_connection_init();
1627         __connman_ipv4_init();
1628         __connman_dhcp_init();
1629
1630         if (__connman_rfkill_init() < 0)
1631                 __connman_udev_enable_rfkill_processing();
1632 }
1633
1634 void __connman_element_stop(void)
1635 {
1636         DBG("");
1637
1638         __connman_rfkill_cleanup();
1639
1640         __connman_dhcp_cleanup();
1641         __connman_ipv4_cleanup();
1642         __connman_provider_cleanup();
1643         __connman_connection_cleanup();
1644 }
1645
1646 static gboolean free_driver(GNode *node, gpointer data)
1647 {
1648         struct connman_element *element = node->data;
1649
1650         DBG("element %p name %s", element, element->name);
1651
1652         if (element->driver) {
1653                 if (element->driver->remove)
1654                         element->driver->remove(element);
1655
1656                 __connman_element_lock(element);
1657                 element->driver = NULL;
1658                 __connman_element_unlock(element);
1659         }
1660
1661         return FALSE;
1662 }
1663
1664 static gboolean free_node(GNode *node, gpointer data)
1665 {
1666         struct connman_element *element = node->data;
1667
1668         DBG("element %p name %s", element, element->name);
1669
1670         if (g_node_depth(node) > 1)
1671                 connman_element_unregister(element);
1672
1673         return FALSE;
1674 }
1675
1676 void __connman_element_cleanup(void)
1677 {
1678         DBG("");
1679
1680         __connman_device_cleanup();
1681         __connman_network_cleanup();
1682         __connman_service_cleanup();
1683         __connman_notifier_cleanup();
1684
1685         g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1686                                                         free_driver, NULL);
1687
1688         g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1689                                                         free_node, NULL);
1690
1691         g_node_destroy(element_root);
1692         element_root = NULL;
1693
1694         g_free(nodevice_filter);
1695         g_free(device_filter);
1696
1697         if (connection == NULL)
1698                 return;
1699
1700         dbus_connection_unref(connection);
1701 }