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