All WiFi properties are now part of network interface
[connman] / 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
41 static gboolean started = FALSE;
42
43 static struct {
44         enum connman_property_id id;
45         int type;
46         const char *name;
47         const void *value;
48 } propid_table[] = {
49         { CONNMAN_PROPERTY_ID_IPV4_METHOD,
50                 DBUS_TYPE_STRING, "IPv4.Method", "dhcp" },
51         { CONNMAN_PROPERTY_ID_IPV4_ADDRESS,
52                 DBUS_TYPE_STRING, "IPv4.Address" },
53         { CONNMAN_PROPERTY_ID_IPV4_NETMASK,
54                 DBUS_TYPE_STRING, "IPv4.Netmask" },
55         { CONNMAN_PROPERTY_ID_IPV4_GATEWAY,
56                 DBUS_TYPE_STRING, "IPv4.Gateway" },
57         { CONNMAN_PROPERTY_ID_IPV4_BROADCAST,
58                 DBUS_TYPE_STRING, "IPv4.Broadcast" },
59         { CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
60                 DBUS_TYPE_STRING, "IPv4.Nameserver" },
61         { }
62 };
63
64 static int propid2type(enum connman_property_id id)
65 {
66         int i;
67
68         for (i = 0; propid_table[i].name; i++) {
69                 if (propid_table[i].id == id)
70                         return propid_table[i].type;
71         }
72
73         return DBUS_TYPE_INVALID;
74 }
75
76 static const char *propid2name(enum connman_property_id id)
77 {
78         int i;
79
80         for (i = 0; propid_table[i].name; i++) {
81                 if (propid_table[i].id == id)
82                         return propid_table[i].name;
83         }
84
85         return NULL;
86 }
87
88 static const char *type2string(enum connman_element_type type)
89 {
90         switch (type) {
91         case CONNMAN_ELEMENT_TYPE_UNKNOWN:
92                 return "unknown";
93         case CONNMAN_ELEMENT_TYPE_ROOT:
94                 return "root";
95         case CONNMAN_ELEMENT_TYPE_PROFILE:
96                 return "profile";
97         case CONNMAN_ELEMENT_TYPE_DEVICE:
98                 return "device";
99         case CONNMAN_ELEMENT_TYPE_NETWORK:
100                 return "network";
101         case CONNMAN_ELEMENT_TYPE_SERVICE:
102                 return "service";
103         case CONNMAN_ELEMENT_TYPE_PPP:
104                 return "ppp";
105         case CONNMAN_ELEMENT_TYPE_IPV4:
106                 return "ipv4";
107         case CONNMAN_ELEMENT_TYPE_IPV6:
108                 return "ipv6";
109         case CONNMAN_ELEMENT_TYPE_DHCP:
110                 return "dhcp";
111         case CONNMAN_ELEMENT_TYPE_BOOTP:
112                 return "bootp";
113         case CONNMAN_ELEMENT_TYPE_ZEROCONF:
114                 return "zeroconf";
115         case CONNMAN_ELEMENT_TYPE_CONNECTION:
116                 return "connection";
117         case CONNMAN_ELEMENT_TYPE_VENDOR:
118                 return "vendor";
119         }
120
121         return NULL;
122 }
123
124 const char *__connman_ipv4_method2string(enum connman_ipv4_method method)
125 {
126         switch (method) {
127         case CONNMAN_IPV4_METHOD_UNKNOWN:
128                 return "unknown";
129         case CONNMAN_IPV4_METHOD_OFF:
130                 return "off";
131         case CONNMAN_IPV4_METHOD_STATIC:
132                 return "static";
133         case CONNMAN_IPV4_METHOD_DHCP:
134                 return "dhcp";
135         }
136
137         return "unknown";
138 }
139
140 enum connman_ipv4_method __connman_ipv4_string2method(const char *method)
141 {
142         if (strcasecmp(method, "off") == 0)
143                 return CONNMAN_IPV4_METHOD_OFF;
144         else if (strcasecmp(method, "static") == 0)
145                 return CONNMAN_IPV4_METHOD_STATIC;
146         else if (strcasecmp(method, "dhcp") == 0)
147                 return CONNMAN_IPV4_METHOD_DHCP;
148         else
149                 return CONNMAN_IPV4_METHOD_UNKNOWN;
150 }
151
152 #if 0
153 static void append_property(DBusMessageIter *dict,
154                                 struct connman_property *property)
155 {
156         if (property->value == NULL)
157                 return;
158
159         switch (property->type) {
160         case DBUS_TYPE_ARRAY:
161                 connman_dbus_dict_append_array(dict, property->name,
162                         property->subtype, &property->value, property->size);
163                 break;
164         case DBUS_TYPE_STRING:
165                 connman_dbus_dict_append_variant(dict, property->name,
166                                         property->type, &property->value);
167                 break;
168         default:
169                 connman_dbus_dict_append_variant(dict, property->name,
170                                         property->type, property->value);
171                 break;
172         }
173 }
174
175 static void add_common_properties(struct connman_element *element,
176                                                 DBusMessageIter *dict)
177 {
178         const char *address = NULL, *netmask = NULL, *gateway = NULL;
179         GSList *list;
180
181         connman_element_get_value(element,
182                                 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
183         connman_element_get_value(element,
184                                 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
185         connman_element_get_value(element,
186                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
187
188         if (element->priority > 0)
189                 connman_dbus_dict_append_variant(dict, "Priority",
190                                         DBUS_TYPE_UINT16, &element->priority);
191
192         if (address != NULL)
193                 connman_dbus_dict_append_variant(dict, "IPv4.Address",
194                                                 DBUS_TYPE_STRING, &address);
195         if (netmask != NULL)
196                 connman_dbus_dict_append_variant(dict, "IPv4.Netmask",
197                                                 DBUS_TYPE_STRING, &netmask);
198         if (gateway != NULL)
199                 connman_dbus_dict_append_variant(dict, "IPv4.Gateway",
200                                                 DBUS_TYPE_STRING, &gateway);
201
202         if (element->wifi.security != NULL) {
203                 const char *passphrase = "";
204
205                 connman_dbus_dict_append_variant(dict, "WiFi.Security",
206                                 DBUS_TYPE_STRING, &element->wifi.security);
207
208                 if (element->wifi.passphrase != NULL)
209                         passphrase = element->wifi.passphrase;
210
211                 connman_dbus_dict_append_variant(dict, "WiFi.Passphrase",
212                                 DBUS_TYPE_STRING, &passphrase);
213         }
214
215         __connman_element_lock(element);
216
217         for (list = element->properties; list; list = list->next) {
218                 struct connman_property *property = list->data;
219
220                 append_property(dict, property);
221         }
222
223         __connman_element_unlock(element);
224 }
225
226 static void set_common_property(struct connman_element *element,
227                                 const char *name, DBusMessageIter *value)
228 {
229         GSList *list;
230
231         if (g_str_equal(name, "Priority") == TRUE) {
232                 dbus_message_iter_get_basic(value, &element->priority);
233                 return;
234         }
235
236         __connman_element_lock(element);
237
238         for (list = element->properties; list; list = list->next) {
239                 struct connman_property *property = list->data;
240                 const char *str;
241
242                 if (g_str_equal(property->name, name) == FALSE)
243                         continue;
244
245                 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
246                         continue;
247
248                 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
249
250                 if (property->type == DBUS_TYPE_STRING) {
251                         dbus_message_iter_get_basic(value, &str);
252                         g_free(property->value);
253                         property->value = g_strdup(str);
254                 } else
255                         property->value = NULL;
256         }
257
258         __connman_element_unlock(element);
259 }
260 #endif
261
262 static void emit_element_signal(DBusConnection *conn, const char *member,
263                                         struct connman_element *element)
264 {
265         DBusMessage *signal;
266
267         if (__connman_debug_enabled() == FALSE)
268                 return;
269
270         DBG("conn %p member %s", conn, member);
271
272         if (element == NULL)
273                 return;
274
275         signal = dbus_message_new_signal(element->path,
276                                         CONNMAN_DEBUG_INTERFACE, member);
277         if (signal == NULL)
278                 return;
279
280         g_dbus_send_message(conn, signal);
281 }
282
283 struct foreach_data {
284         enum connman_element_type type;
285         element_cb_t callback;
286         gpointer user_data;
287 };
288
289 static gboolean foreach_callback(GNode *node, gpointer user_data)
290 {
291         struct connman_element *element = node->data;
292         struct foreach_data *data = user_data;
293
294         DBG("element %p name %s", element, element->name);
295
296         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
297                 return FALSE;
298
299         if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
300                                         data->type != element->type)
301                 return FALSE;
302
303         if (data->callback)
304                 data->callback(element, data->user_data);
305
306         return FALSE;
307 }
308
309 void __connman_element_foreach(struct connman_element *element,
310                                 enum connman_element_type type,
311                                 element_cb_t callback, gpointer user_data)
312 {
313         struct foreach_data data = { type, callback, user_data };
314         GNode *node;
315
316         DBG("");
317
318         if (element != NULL) {
319                 node = g_node_find(element_root, G_PRE_ORDER,
320                                                 G_TRAVERSE_ALL, element);
321                 if (node == NULL)
322                         return;
323         } else
324                 node = element_root;
325
326         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
327                                                 foreach_callback, &data);
328 }
329
330 struct append_filter {
331         enum connman_element_type type;
332         DBusMessageIter *iter;
333 };
334
335 static gboolean append_path(GNode *node, gpointer user_data)
336 {
337         struct connman_element *element = node->data;
338         struct append_filter *filter = user_data;
339
340         DBG("element %p name %s", element, element->name);
341
342         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
343                 return FALSE;
344
345         if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
346                                         filter->type != element->type)
347                 return FALSE;
348
349         if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
350                         __connman_device_has_driver(element->device) == FALSE)
351                 return FALSE;
352
353         if (filter->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
354                         __connman_network_has_driver(element->network) == FALSE)
355                 return FALSE;
356
357         dbus_message_iter_append_basic(filter->iter,
358                                 DBUS_TYPE_OBJECT_PATH, &element->path);
359
360         return FALSE;
361 }
362
363 void __connman_element_list(struct connman_element *element,
364                                         enum connman_element_type type,
365                                                         DBusMessageIter *iter)
366 {
367         struct append_filter filter = { type, iter };
368         GNode *node;
369
370         DBG("");
371
372         if (element != NULL) {
373                 node = g_node_find(element_root, G_PRE_ORDER,
374                                                 G_TRAVERSE_ALL, element);
375                 if (node == NULL)
376                         return;
377         } else
378                 node = element_root;
379
380         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
381                                                 append_path, &filter);
382 }
383
384 struct count_data {
385         enum connman_element_type type;
386         int count;
387 };
388
389 static gboolean count_element(GNode *node, gpointer user_data)
390 {
391         struct connman_element *element = node->data;
392         struct count_data *data = user_data;
393
394         DBG("element %p name %s", element, element->name);
395
396         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
397                 return FALSE;
398
399         if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
400                                         data->type != element->type)
401                 return FALSE;
402
403         data->count++;
404
405         return FALSE;
406 }
407
408 int __connman_element_count(struct connman_element *element,
409                                         enum connman_element_type type)
410 {
411         struct count_data data = { type, 0 };
412         GNode *node;
413
414         DBG("");
415
416         if (element != NULL) {
417                 node = g_node_find(element_root, G_PRE_ORDER,
418                                                 G_TRAVERSE_ALL, element);
419                 if (node == NULL)
420                         return 0;
421         } else
422                 node = element_root;
423
424         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
425                                                 count_element, &data);
426
427         return data.count;
428 }
429
430 static gint compare_priority(gconstpointer a, gconstpointer b)
431 {
432         const struct connman_driver *driver1 = a;
433         const struct connman_driver *driver2 = b;
434
435         return driver2->priority - driver1->priority;
436 }
437
438 static gboolean match_driver(struct connman_element *element,
439                                         struct connman_driver *driver)
440 {
441         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
442                 return FALSE;
443
444         if (element->type == driver->type ||
445                         driver->type == CONNMAN_ELEMENT_TYPE_UNKNOWN)
446                 return TRUE;
447
448         return FALSE;
449 }
450
451 static gboolean probe_driver(GNode *node, gpointer data)
452 {
453         struct connman_element *element = node->data;
454         struct connman_driver *driver = data;
455
456         DBG("element %p name %s", element, element->name);
457
458         if (!element->driver && match_driver(element, driver) == TRUE) {
459                 if (driver->probe(element) < 0)
460                         return FALSE;
461
462                 __connman_element_lock(element);
463                 element->driver = driver;
464                 __connman_element_unlock(element);
465         }
466
467         return FALSE;
468 }
469
470 void __connman_driver_rescan(struct connman_driver *driver)
471 {
472         DBG("driver %p name %s", driver, driver->name);
473
474         if (!driver->probe)
475                 return;
476
477         if (element_root != NULL)
478                 g_node_traverse(element_root, G_PRE_ORDER,
479                                 G_TRAVERSE_ALL, -1, probe_driver, driver);
480 }
481
482 /**
483  * connman_driver_register:
484  * @driver: driver definition
485  *
486  * Register a new driver
487  *
488  * Returns: %0 on success
489  */
490 int connman_driver_register(struct connman_driver *driver)
491 {
492         DBG("driver %p name %s", driver, driver->name);
493
494         if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
495                 return -EINVAL;
496
497         if (!driver->probe)
498                 return -EINVAL;
499
500         driver_list = g_slist_insert_sorted(driver_list, driver,
501                                                         compare_priority);
502
503         if (started == FALSE)
504                 return 0;
505
506         if (element_root != NULL)
507                 g_node_traverse(element_root, G_PRE_ORDER,
508                                 G_TRAVERSE_ALL, -1, probe_driver, driver);
509
510         return 0;
511 }
512
513 static gboolean remove_driver(GNode *node, gpointer data)
514 {
515         struct connman_element *element = node->data;
516         struct connman_driver *driver = data;
517
518         DBG("element %p name %s", element, element->name);
519
520         if (element->driver == driver) {
521                 if (driver->remove)
522                         driver->remove(element);
523
524                 __connman_element_lock(element);
525                 element->driver = NULL;
526                 __connman_element_unlock(element);
527         }
528
529         return FALSE;
530 }
531
532 /**
533  * connman_driver_unregister:
534  * @driver: driver definition
535  *
536  * Remove a previously registered driver
537  */
538 void connman_driver_unregister(struct connman_driver *driver)
539 {
540         DBG("driver %p name %s", driver, driver->name);
541
542         driver_list = g_slist_remove(driver_list, driver);
543
544         if (element_root != NULL)
545                 g_node_traverse(element_root, G_POST_ORDER,
546                                 G_TRAVERSE_ALL, -1, remove_driver, driver);
547 }
548
549 /**
550  * connman_element_create:
551  * @name: element name
552  *
553  * Allocate a new element and assign the given #name to it. If the name
554  * is #NULL, it will be later on created based on the element type.
555  *
556  * Returns: a newly-allocated #connman_element structure
557  */
558 struct connman_element *connman_element_create(const char *name)
559 {
560         struct connman_element *element;
561
562         element = g_try_new0(struct connman_element, 1);
563         if (element == NULL)
564                 return NULL;
565
566         DBG("element %p", element);
567
568         element->refcount = 1;
569
570         element->name    = g_strdup(name);
571         element->type    = CONNMAN_ELEMENT_TYPE_UNKNOWN;
572         element->index   = -1;
573         element->enabled = FALSE;
574
575         return element;
576 }
577
578 struct connman_element *connman_element_ref(struct connman_element *element)
579 {
580         DBG("element %p name %s refcount %d", element, element->name,
581                                 g_atomic_int_get(&element->refcount) + 1);
582
583         g_atomic_int_inc(&element->refcount);
584
585         return element;
586 }
587
588 static void free_properties(struct connman_element *element)
589 {
590         GSList *list;
591
592         DBG("element %p name %s", element, element->name);
593
594         __connman_element_lock(element);
595
596         for (list = element->properties; list; list = list->next) {
597                 struct connman_property *property = list->data;
598
599                 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
600                         g_free(property->value);
601
602                 g_free(property->name);
603                 g_free(property);
604         }
605
606         g_slist_free(element->properties);
607
608         element->properties = NULL;
609
610         __connman_element_unlock(element);
611 }
612
613 void connman_element_unref(struct connman_element *element)
614 {
615         DBG("element %p name %s refcount %d", element, element->name,
616                                 g_atomic_int_get(&element->refcount) - 1);
617
618         if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
619                 if (element->destruct)
620                         element->destruct(element);
621                 free_properties(element);
622                 g_free(element->ipv4.address);
623                 g_free(element->ipv4.netmask);
624                 g_free(element->ipv4.gateway);
625                 g_free(element->ipv4.network);
626                 g_free(element->ipv4.broadcast);
627                 g_free(element->ipv4.nameserver);
628                 g_free(element->devname);
629                 g_free(element->devpath);
630                 g_free(element->path);
631                 g_free(element->name);
632                 g_free(element);
633         }
634 }
635
636 int connman_element_add_static_property(struct connman_element *element,
637                                 const char *name, int type, const void *value)
638 {
639         struct connman_property *property;
640
641         DBG("element %p name %s", element, element->name);
642
643         if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
644                 return -EINVAL;
645
646         property = g_try_new0(struct connman_property, 1);
647         if (property == NULL)
648                 return -ENOMEM;
649
650         property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
651         property->id    = CONNMAN_PROPERTY_ID_INVALID;
652         property->name  = g_strdup(name);
653         property->type  = type;
654
655         DBG("name %s type %d value %p", name, type, value);
656
657         switch (type) {
658         case DBUS_TYPE_STRING:
659                 property->value = g_strdup(*((const char **) value));
660                 break;
661         case DBUS_TYPE_BYTE:
662                 property->value = g_try_malloc(1);
663                 if (property->value != NULL)
664                         memcpy(property->value, value, 1);
665                 break;
666         }
667
668         __connman_element_lock(element);
669         element->properties = g_slist_append(element->properties, property);
670         __connman_element_unlock(element);
671
672         return 0;
673 }
674
675 int connman_element_set_static_property(struct connman_element *element,
676                                 const char *name, int type, const void *value)
677 {
678         GSList *list;
679
680         DBG("element %p name %s", element, element->name);
681
682         if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
683                 return -EINVAL;
684
685         __connman_element_lock(element);
686
687         for (list = element->properties; list; list = list->next) {
688                 struct connman_property *property = list->data;
689
690                 if (g_str_equal(property->name, name) == FALSE)
691                         continue;
692
693                 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
694                         continue;
695
696                 property->type = type;
697                 g_free(property->value);
698
699                 switch (type) {
700                 case DBUS_TYPE_STRING:
701                         property->value = g_strdup(*((const char **) value));
702                         break;
703                 case DBUS_TYPE_BYTE:
704                         property->value = g_try_malloc(1);
705                         if (property->value != NULL)
706                                 memcpy(property->value, value, 1);
707                         break;
708                 }
709         }
710
711         __connman_element_unlock(element);
712
713         return 0;
714 }
715
716 int connman_element_add_static_array_property(struct connman_element *element,
717                         const char *name, int type, const void *value, int len)
718 {
719         struct connman_property *property;
720
721         DBG("element %p name %s", element, element->name);
722
723         if (type != DBUS_TYPE_BYTE)
724                 return -EINVAL;
725
726         property = g_try_new0(struct connman_property, 1);
727         if (property == NULL)
728                 return -ENOMEM;
729
730         property->flags   = CONNMAN_PROPERTY_FLAG_STATIC;
731         property->id      = CONNMAN_PROPERTY_ID_INVALID;
732         property->name    = g_strdup(name);
733         property->type    = DBUS_TYPE_ARRAY;
734         property->subtype = type;
735
736         DBG("name %s type %d value %p", name, type, value);
737
738         switch (type) {
739         case DBUS_TYPE_BYTE:
740                 property->value = g_try_malloc(len);
741                 if (property->value != NULL) {
742                         memcpy(property->value,
743                                 *((const unsigned char **) value), len);
744                         property->size = len;
745                 }
746                 break;
747         }
748
749         __connman_element_lock(element);
750         element->properties = g_slist_append(element->properties, property);
751         __connman_element_unlock(element);
752
753         return 0;
754 }
755
756 static void *get_reference_value(struct connman_element *element,
757                                                 enum connman_property_id id)
758 {
759         GSList *list;
760
761         DBG("element %p name %s", element, element->name);
762
763         for (list = element->properties; list; list = list->next) {
764                 struct connman_property *property = list->data;
765
766                 if (property->id != id)
767                         continue;
768
769                 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
770                         return property->value;
771         }
772
773         if (element->parent == NULL)
774                 return NULL;
775
776         return get_reference_value(element->parent, id);
777 }
778
779 static void set_reference_properties(struct connman_element *element)
780 {
781         GSList *list;
782
783         DBG("element %p name %s", element, element->name);
784
785         for (list = element->properties; list; list = list->next) {
786                 struct connman_property *property = list->data;
787
788                 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
789                         continue;
790
791                 property->value = get_reference_value(element->parent,
792                                                                 property->id);
793         }
794 }
795
796 static struct connman_property *create_property(struct connman_element *element,
797                                                 enum connman_property_id id)
798 {
799         struct connman_property *property;
800         GSList *list;
801
802         DBG("element %p name %s", element, element->name);
803
804         __connman_element_lock(element);
805
806         for (list = element->properties; list; list = list->next) {
807                 property = list->data;
808
809                 if (property->id == id)
810                         goto unlock;
811         }
812
813         property = g_try_new0(struct connman_property, 1);
814         if (property == NULL)
815                 goto unlock;
816
817         property->flags = CONNMAN_PROPERTY_FLAG_REFERENCE;
818         property->id    = id;
819         property->name  = g_strdup(propid2name(id));
820         property->type  = propid2type(id);
821
822         if (property->name == NULL) {
823                 g_free(property);
824                 property = NULL;
825                 goto unlock;
826         }
827
828         element->properties = g_slist_append(element->properties, property);
829
830 unlock:
831         __connman_element_unlock(element);
832
833         return property;
834 }
835
836 static void create_default_properties(struct connman_element *element)
837 {
838         struct connman_property *property;
839         int i;
840
841         DBG("element %p name %s", element, element->name);
842
843         for (i = 0; propid_table[i].name; i++) {
844                 DBG("property %s", propid_table[i].name);
845
846                 property = create_property(element, propid_table[i].id);
847
848                 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
849
850                 if (propid_table[i].type != DBUS_TYPE_STRING)
851                         continue;
852
853                 if (propid_table[i].value)
854                         property->value = g_strdup(propid_table[i].value);
855                 else
856                         property->value = g_strdup("");
857         }
858 }
859
860 static int define_properties_valist(struct connman_element *element,
861                                                                 va_list args)
862 {
863         enum connman_property_id id;
864
865         DBG("element %p name %s", element, element->name);
866
867         id = va_arg(args, enum connman_property_id);
868
869         while (id != CONNMAN_PROPERTY_ID_INVALID) {
870
871                 DBG("property %d", id);
872
873                 create_property(element, id);
874
875                 id = va_arg(args, enum connman_property_id);
876         }
877
878         return 0;
879 }
880
881 /**
882  * connman_element_define_properties:
883  * @element: an element
884  * @varargs: list of property identifiers
885  *
886  * Define the valid properties for an element.
887  *
888  * Returns: %0 on success
889  */
890 int connman_element_define_properties(struct connman_element *element, ...)
891 {
892         va_list args;
893         int err;
894
895         DBG("element %p name %s", element, element->name);
896
897         va_start(args, element);
898
899         err = define_properties_valist(element, args);
900
901         va_end(args);
902
903         return err;
904 }
905
906 int connman_element_create_property(struct connman_element *element,
907                                                 const char *name, int type)
908 {
909         return -EIO;
910 }
911
912 int connman_element_set_property(struct connman_element *element,
913                                 enum connman_property_id id, const void *value)
914 {
915         switch (id) {
916         case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
917                 __connman_element_lock(element);
918                 g_free(element->ipv4.address);
919                 element->ipv4.address = g_strdup(*((const char **) value));
920                 __connman_element_unlock(element);
921                 break;
922         case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
923                 __connman_element_lock(element);
924                 g_free(element->ipv4.netmask);
925                 element->ipv4.netmask = g_strdup(*((const char **) value));
926                 __connman_element_unlock(element);
927                 break;
928         case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
929                 __connman_element_lock(element);
930                 g_free(element->ipv4.gateway);
931                 element->ipv4.gateway = g_strdup(*((const char **) value));
932                 __connman_element_unlock(element);
933                 break;
934         case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
935                 __connman_element_lock(element);
936                 g_free(element->ipv4.broadcast);
937                 element->ipv4.broadcast = g_strdup(*((const char **) value));
938                 __connman_element_unlock(element);
939                 break;
940         case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
941                 __connman_element_lock(element);
942                 g_free(element->ipv4.nameserver);
943                 element->ipv4.nameserver = g_strdup(*((const char **) value));
944                 __connman_element_unlock(element);
945                 break;
946         default:
947                 return -EINVAL;
948         }
949
950         return 0;
951 }
952
953 int connman_element_get_value(struct connman_element *element,
954                                 enum connman_property_id id, void *value)
955 {
956         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
957                 return -EINVAL;
958
959         switch (id) {
960         case CONNMAN_PROPERTY_ID_IPV4_METHOD:
961                 if (element->ipv4.method == CONNMAN_IPV4_METHOD_UNKNOWN)
962                         return connman_element_get_value(element->parent,
963                                                                 id, value);
964                 __connman_element_lock(element);
965                 *((const char **) value) = __connman_ipv4_method2string(element->ipv4.method);
966                 __connman_element_unlock(element);
967                 break;
968         case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
969                 if (element->ipv4.address == NULL)
970                         return connman_element_get_value(element->parent,
971                                                                 id, value);
972                 __connman_element_lock(element);
973                 *((char **) value) = element->ipv4.address;
974                 __connman_element_unlock(element);
975                 break;
976         case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
977                 if (element->ipv4.netmask == NULL)
978                         return connman_element_get_value(element->parent,
979                                                                 id, value);
980                 __connman_element_lock(element);
981                 *((char **) value) = element->ipv4.netmask;
982                 __connman_element_unlock(element);
983                 break;
984         case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
985                 if (element->ipv4.gateway == NULL)
986                         return connman_element_get_value(element->parent,
987                                                                 id, value);
988                 __connman_element_lock(element);
989                 *((char **) value) = element->ipv4.gateway;
990                 __connman_element_unlock(element);
991                 break;
992         case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
993                 if (element->ipv4.broadcast == NULL)
994                         return connman_element_get_value(element->parent,
995                                                                 id, value);
996                 __connman_element_lock(element);
997                 *((char **) value) = element->ipv4.broadcast;
998                 __connman_element_unlock(element);
999                 break;
1000         case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
1001                 if (element->ipv4.nameserver == NULL)
1002                         return connman_element_get_value(element->parent,
1003                                                                 id, value);
1004                 __connman_element_lock(element);
1005                 *((char **) value) = element->ipv4.nameserver;
1006                 __connman_element_unlock(element);
1007                 break;
1008         default:
1009                 return -EINVAL;
1010         }
1011
1012         return 0;
1013 }
1014
1015 gboolean connman_element_get_static_property(struct connman_element *element,
1016                                                 const char *name, void *value)
1017 {
1018         GSList *list;
1019         gboolean found = FALSE;
1020
1021         DBG("element %p name %s", element, element->name);
1022
1023         __connman_element_lock(element);
1024
1025         for (list = element->properties; list; list = list->next) {
1026                 struct connman_property *property = list->data;
1027
1028                 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1029                         continue;
1030
1031                 if (g_str_equal(property->name, name) == TRUE) {
1032                         switch (property->type) {
1033                         case DBUS_TYPE_STRING:
1034                                 *((char **) value) = property->value;
1035                                 found = TRUE;
1036                                 break;
1037                         }
1038                         break;
1039                 }
1040         }
1041
1042         __connman_element_unlock(element);
1043
1044         return found;
1045 }
1046
1047 gboolean connman_element_get_static_array_property(struct connman_element *element,
1048                                         const char *name, void *value, int *len)
1049 {
1050         GSList *list;
1051         gboolean found = FALSE;
1052
1053         DBG("element %p name %s", element, element->name);
1054
1055         __connman_element_lock(element);
1056
1057         for (list = element->properties; list; list = list->next) {
1058                 struct connman_property *property = list->data;
1059
1060                 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1061                         continue;
1062
1063                 if (g_str_equal(property->name, name) == TRUE) {
1064                         *((char **) value) = property->value;
1065                         *len = property->size;
1066                         found = TRUE;
1067                         break;
1068                 }
1069         }
1070
1071         __connman_element_unlock(element);
1072
1073         return found;
1074 }
1075
1076 gboolean connman_element_match_static_property(struct connman_element *element,
1077                                         const char *name, const void *value)
1078 {
1079         GSList *list;
1080         gboolean result = FALSE;
1081
1082         DBG("element %p name %s", element, element->name);
1083
1084         __connman_element_lock(element);
1085
1086         for (list = element->properties; list; list = list->next) {
1087                 struct connman_property *property = list->data;
1088
1089                 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1090                         continue;
1091
1092                 if (g_str_equal(property->name, name) == FALSE)
1093                         continue;
1094
1095                 if (property->type == DBUS_TYPE_STRING)
1096                         result = g_str_equal(property->value,
1097                                                 *((const char **) value));
1098
1099                 if (result == TRUE)
1100                         break;
1101         }
1102
1103         __connman_element_unlock(element);
1104
1105         return result;
1106 }
1107
1108 static void append_connections(DBusMessageIter *entry)
1109 {
1110         DBusMessageIter value, iter;
1111         const char *key = "Connections";
1112
1113         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1114
1115         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1116                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
1117                                                                 &value);
1118
1119         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
1120                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
1121         __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION, &iter);
1122         dbus_message_iter_close_container(&value, &iter);
1123
1124         dbus_message_iter_close_container(entry, &value);
1125 }
1126
1127 static void emit_connections_signal(DBusConnection *conn)
1128 {
1129         DBusMessage *signal;
1130         DBusMessageIter entry;
1131
1132         DBG("conn %p", conn);
1133
1134         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1135                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1136         if (signal == NULL)
1137                 return;
1138
1139         dbus_message_iter_init_append(signal, &entry);
1140
1141         append_connections(&entry);
1142
1143         g_dbus_send_message(conn, signal);
1144 }
1145
1146 static void append_state(DBusMessageIter *entry, const char *state)
1147 {
1148         DBusMessageIter value;
1149         const char *key = "State";
1150
1151         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1152
1153         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1154                                         DBUS_TYPE_STRING_AS_STRING, &value);
1155         dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
1156         dbus_message_iter_close_container(entry, &value);
1157 }
1158
1159 static void emit_state_change(DBusConnection *conn, const char *state)
1160 {
1161         DBusMessage *signal;
1162         DBusMessageIter entry;
1163
1164         DBG("conn %p", conn);
1165
1166         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1167                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1168         if (signal == NULL)
1169                 return;
1170
1171         dbus_message_iter_init_append(signal, &entry);
1172
1173         append_state(&entry, state);
1174
1175         g_dbus_send_message(conn, signal);
1176 }
1177
1178 static void set_signal_strength(struct connman_element *connection)
1179 {
1180         struct connman_element *element = connection;
1181
1182         while (element != NULL) {
1183                 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1184                         connection->strength = element->strength;
1185                         break;
1186                 }
1187
1188                 element = element->parent;
1189         }
1190 }
1191
1192 static void probe_element(struct connman_element *element)
1193 {
1194         GSList *list;
1195
1196         DBG("element %p name %s", element, element->name);
1197
1198         for (list = driver_list; list; list = list->next) {
1199                 struct connman_driver *driver = list->data;
1200
1201                 if (match_driver(element, driver) == FALSE)
1202                         continue;
1203
1204                 DBG("driver %p name %s", driver, driver->name);
1205
1206                 if (driver->probe(element) == 0) {
1207                         __connman_element_lock(element);
1208                         element->driver = driver;
1209                         __connman_element_unlock(element);
1210                         break;
1211                 }
1212         }
1213 }
1214
1215 static void register_element(gpointer data, gpointer user_data)
1216 {
1217         struct connman_element *element = data;
1218         const gchar *basepath;
1219         GNode *node;
1220
1221         __connman_element_lock(element);
1222
1223         if (element->parent) {
1224                 node = g_node_find(element_root, G_PRE_ORDER,
1225                                         G_TRAVERSE_ALL, element->parent);
1226                 basepath = element->parent->path;
1227         } else {
1228                 element->parent = element_root->data;
1229
1230                 node = element_root;
1231                 basepath = "";
1232         }
1233
1234         element->path = g_strdup_printf("%s/%s", basepath, element->name);
1235
1236         set_reference_properties(element);
1237
1238         __connman_element_unlock(element);
1239
1240         DBG("element %p path %s", element, element->path);
1241
1242         g_node_append_data(node, element);
1243
1244         if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1245                 set_signal_strength(element);
1246                 emit_connections_signal(connection);
1247                 emit_state_change(connection, "online");
1248         }
1249
1250         emit_element_signal(connection, "ElementAdded", element);
1251
1252         if (started == FALSE)
1253                 return;
1254
1255         probe_element(element);
1256 }
1257
1258 /**
1259  * connman_element_register:
1260  * @element: the element to register
1261  * @parent: the parent to register the element with
1262  *
1263  * Register an element with the core. It will be register under the given
1264  * parent of if %NULL is provided under the root element.
1265  *
1266  * Returns: %0 on success
1267  */
1268 int connman_element_register(struct connman_element *element,
1269                                         struct connman_element *parent)
1270 {
1271         DBG("element %p name %s parent %p", element, element->name, parent);
1272
1273         if (element->devname == NULL)
1274                 element->devname = g_strdup(element->name);
1275
1276         if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE) {
1277                 if (g_pattern_match_simple(device_filter,
1278                                                 element->devname) == FALSE) {
1279                         DBG("ignoring %s [%s] device", element->name,
1280                                                         element->devname);
1281                         return -EPERM;
1282                 }
1283         }
1284
1285         if (connman_element_ref(element) == NULL)
1286                 return -EINVAL;
1287
1288         __connman_element_lock(element);
1289
1290         if (element->name == NULL) {
1291                 element->name = g_strdup(type2string(element->type));
1292                 if (element->name == NULL) {
1293                         __connman_element_unlock(element);
1294                         return -EINVAL;
1295                 }
1296         }
1297
1298         if (element->type == CONNMAN_ELEMENT_TYPE_DHCP)
1299                 element->ipv4.method = CONNMAN_IPV4_METHOD_DHCP;
1300
1301         element->parent = parent;
1302
1303         __connman_element_unlock(element);
1304
1305         register_element(element, NULL);
1306
1307         return 0;
1308 }
1309
1310 static gboolean remove_element(GNode *node, gpointer user_data)
1311 {
1312         struct connman_element *element = node->data;
1313         struct connman_element *root = user_data;
1314
1315         DBG("element %p name %s", element, element->name);
1316
1317         if (element == root)
1318                 return FALSE;
1319
1320         if (node != NULL)
1321                 g_node_unlink(node);
1322
1323         if (element->driver) {
1324                 if (element->driver->remove)
1325                         element->driver->remove(element);
1326
1327                 __connman_element_lock(element);
1328                 element->driver = NULL;
1329                 __connman_element_unlock(element);
1330         }
1331
1332         if (node != NULL)
1333                 g_node_destroy(node);
1334
1335         if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1336                 if (__connman_element_count(NULL,
1337                                         CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1338                         emit_state_change(connection, "offline");
1339                 emit_connections_signal(connection);
1340         }
1341
1342         emit_element_signal(connection, "ElementRemoved", element);
1343
1344         connman_element_unref(element);
1345
1346         return FALSE;
1347 }
1348
1349 void connman_element_unregister(struct connman_element *element)
1350 {
1351         GNode *node;
1352
1353         DBG("element %p name %s", element, element->name);
1354
1355         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1356
1357         if (node != NULL)
1358                 g_node_traverse(node, G_POST_ORDER,
1359                                 G_TRAVERSE_ALL, -1, remove_element, NULL);
1360 }
1361
1362 void connman_element_unregister_children(struct connman_element *element)
1363 {
1364         GNode *node;
1365
1366         DBG("element %p name %s", element, element->name);
1367
1368         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1369
1370         if (node != NULL)
1371                 g_node_traverse(node, G_POST_ORDER,
1372                                 G_TRAVERSE_ALL, -1, remove_element, element);
1373 }
1374
1375 static gboolean update_element(GNode *node, gpointer user_data)
1376 {
1377         struct connman_element *element = node->data;
1378         struct connman_element *root = user_data;
1379
1380         DBG("element %p name %s", element, element->name);
1381
1382         if (element->driver && element->driver->update)
1383                 element->driver->update(element);
1384
1385         if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION &&
1386                                 root->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1387                 if (element->strength != root->strength)
1388                         element->strength = root->strength;
1389         }
1390
1391         emit_element_signal(connection, "ElementUpdated", element);
1392
1393         return FALSE;
1394 }
1395
1396 void connman_element_update(struct connman_element *element)
1397 {
1398         GNode *node;
1399
1400         DBG("element %p name %s", element, element->name);
1401
1402         node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1403
1404         if (node != NULL)
1405                 g_node_traverse(node, G_PRE_ORDER,
1406                                 G_TRAVERSE_ALL, -1, update_element, element);
1407 }
1408
1409 int connman_element_set_enabled(struct connman_element *element,
1410                                                         gboolean enabled)
1411 {
1412         if (element->enabled == enabled)
1413                 return 0;
1414
1415         element->enabled = enabled;
1416
1417         return 0;
1418 }
1419
1420 int __connman_element_init(DBusConnection *conn, const char *device)
1421 {
1422         struct connman_element *element;
1423
1424         DBG("conn %p", conn);
1425
1426         connection = dbus_connection_ref(conn);
1427         if (connection == NULL)
1428                 return -EIO;
1429
1430         device_filter = g_strdup(device);
1431
1432         element = connman_element_create("root");
1433
1434         element->path = g_strdup("/");
1435         element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1436
1437         create_default_properties(element);
1438
1439         element_root = g_node_new(element);
1440
1441         __connman_network_init();
1442         __connman_device_init();
1443
1444         return 0;
1445 }
1446
1447 static gboolean probe_node(GNode *node, gpointer data)
1448 {
1449         struct connman_element *element = node->data;
1450
1451         DBG("element %p name %s", element, element->name);
1452
1453         if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1454                 return FALSE;
1455
1456         if (element->driver)
1457                 return FALSE;
1458
1459         probe_element(element);
1460
1461         return FALSE;
1462 }
1463
1464 void __connman_element_start(void)
1465 {
1466         DBG("");
1467
1468         g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1469                                                         probe_node, NULL);
1470
1471         started = TRUE;
1472
1473         __connman_connection_init();
1474         __connman_ipv4_init();
1475         __connman_detect_init();
1476 }
1477
1478 void __connman_element_stop(void)
1479 {
1480         DBG("");
1481
1482         __connman_detect_cleanup();
1483         __connman_ipv4_cleanup();
1484         __connman_connection_cleanup();
1485 }
1486
1487 static gboolean free_driver(GNode *node, gpointer data)
1488 {
1489         struct connman_element *element = node->data;
1490
1491         DBG("element %p name %s", element, element->name);
1492
1493         if (element->driver) {
1494                 if (element->driver->remove)
1495                         element->driver->remove(element);
1496
1497                 __connman_element_lock(element);
1498                 element->driver = NULL;
1499                 __connman_element_unlock(element);
1500         }
1501
1502         return FALSE;
1503 }
1504
1505 static gboolean free_node(GNode *node, gpointer data)
1506 {
1507         struct connman_element *element = node->data;
1508
1509         DBG("element %p name %s", element, element->name);
1510
1511         if (g_node_depth(node) > 1)
1512                 connman_element_unregister(element);
1513
1514         return FALSE;
1515 }
1516
1517 void __connman_element_cleanup(void)
1518 {
1519         DBG("");
1520
1521         __connman_device_cleanup();
1522         __connman_network_cleanup();
1523
1524         g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1525                                                         free_driver, NULL);
1526
1527         g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1528                                                         free_node, NULL);
1529
1530         g_node_destroy(element_root);
1531         element_root = NULL;
1532
1533         g_free(device_filter);
1534
1535         dbus_connection_unref(connection);
1536 }