5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
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.
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.
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
35 static DBusConnection *connection;
37 static GNode *element_root = NULL;
38 static GSList *driver_list = NULL;
39 static gchar *device_filter = NULL;
42 enum connman_property_id id;
47 { CONNMAN_PROPERTY_ID_IPV4_METHOD,
48 DBUS_TYPE_STRING, "IPv4.Method", "dhcp" },
49 { CONNMAN_PROPERTY_ID_IPV4_ADDRESS,
50 DBUS_TYPE_STRING, "IPv4.Address" },
51 { CONNMAN_PROPERTY_ID_IPV4_NETMASK,
52 DBUS_TYPE_STRING, "IPv4.Netmask" },
53 { CONNMAN_PROPERTY_ID_IPV4_GATEWAY,
54 DBUS_TYPE_STRING, "IPv4.Gateway" },
55 { CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
56 DBUS_TYPE_STRING, "IPv4.Nameserver" },
58 { CONNMAN_PROPERTY_ID_WIFI_SECURITY,
59 DBUS_TYPE_STRING, "WiFi.Security" },
60 { CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE,
61 DBUS_TYPE_STRING, "WiFi.Passphrase" },
66 static int propid2type(enum connman_property_id id)
70 for (i = 0; propid_table[i].name; i++) {
71 if (propid_table[i].id == id)
72 return propid_table[i].type;
75 return DBUS_TYPE_INVALID;
78 static const char *propid2name(enum connman_property_id id)
82 for (i = 0; propid_table[i].name; i++) {
83 if (propid_table[i].id == id)
84 return propid_table[i].name;
90 static const char *type2string(enum connman_element_type type)
93 case CONNMAN_ELEMENT_TYPE_UNKNOWN:
95 case CONNMAN_ELEMENT_TYPE_ROOT:
97 case CONNMAN_ELEMENT_TYPE_PROFILE:
99 case CONNMAN_ELEMENT_TYPE_DEVICE:
101 case CONNMAN_ELEMENT_TYPE_NETWORK:
103 case CONNMAN_ELEMENT_TYPE_SERVICE:
105 case CONNMAN_ELEMENT_TYPE_IPV4:
107 case CONNMAN_ELEMENT_TYPE_IPV6:
109 case CONNMAN_ELEMENT_TYPE_DHCP:
111 case CONNMAN_ELEMENT_TYPE_BOOTP:
113 case CONNMAN_ELEMENT_TYPE_ZEROCONF:
115 case CONNMAN_ELEMENT_TYPE_CONNECTION:
122 static const char *subtype2string(enum connman_element_subtype type)
125 case CONNMAN_ELEMENT_SUBTYPE_UNKNOWN:
127 case CONNMAN_ELEMENT_SUBTYPE_FAKE:
129 case CONNMAN_ELEMENT_SUBTYPE_NETWORK:
131 case CONNMAN_ELEMENT_SUBTYPE_ETHERNET:
133 case CONNMAN_ELEMENT_SUBTYPE_WIFI:
135 case CONNMAN_ELEMENT_SUBTYPE_WIMAX:
137 case CONNMAN_ELEMENT_SUBTYPE_MODEM:
139 case CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH:
146 const char *__connman_element_policy2string(enum connman_element_policy policy)
149 case CONNMAN_ELEMENT_POLICY_UNKNOWN:
151 case CONNMAN_ELEMENT_POLICY_IGNORE:
153 case CONNMAN_ELEMENT_POLICY_AUTO:
155 case CONNMAN_ELEMENT_POLICY_ASK:
162 enum connman_element_policy __connman_element_string2policy(const char *policy)
164 if (strcasecmp(policy, "ignore") == 0)
165 return CONNMAN_ELEMENT_POLICY_IGNORE;
166 else if (strcasecmp(policy, "auto") == 0)
167 return CONNMAN_ELEMENT_POLICY_AUTO;
168 else if (strcasecmp(policy, "ask") == 0)
169 return CONNMAN_ELEMENT_POLICY_ASK;
171 return CONNMAN_ELEMENT_POLICY_UNKNOWN;
174 const char *__connman_ipv4_method2string(enum connman_ipv4_method method)
177 case CONNMAN_IPV4_METHOD_UNKNOWN:
179 case CONNMAN_IPV4_METHOD_OFF:
181 case CONNMAN_IPV4_METHOD_STATIC:
183 case CONNMAN_IPV4_METHOD_DHCP:
190 enum connman_ipv4_method __connman_ipv4_string2method(const char *method)
192 if (strcasecmp(method, "off") == 0)
193 return CONNMAN_IPV4_METHOD_OFF;
194 else if (strcasecmp(method, "static") == 0)
195 return CONNMAN_IPV4_METHOD_STATIC;
196 else if (strcasecmp(method, "dhcp") == 0)
197 return CONNMAN_IPV4_METHOD_DHCP;
199 return CONNMAN_IPV4_METHOD_UNKNOWN;
202 static void append_property(DBusMessageIter *dict,
203 struct connman_property *property)
205 if (property->value == NULL)
208 switch (property->type) {
209 case DBUS_TYPE_ARRAY:
210 connman_dbus_dict_append_array(dict, property->name,
211 property->subtype, &property->value, property->size);
213 case DBUS_TYPE_STRING:
214 connman_dbus_dict_append_variant(dict, property->name,
215 property->type, &property->value);
218 connman_dbus_dict_append_variant(dict, property->name,
219 property->type, property->value);
224 static void add_common_properties(struct connman_element *element,
225 DBusMessageIter *dict)
227 const char *address = NULL, *netmask = NULL, *gateway = NULL;
230 connman_element_get_value(element,
231 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
232 connman_element_get_value(element,
233 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
234 connman_element_get_value(element,
235 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
237 if (element->priority > 0)
238 connman_dbus_dict_append_variant(dict, "Priority",
239 DBUS_TYPE_UINT16, &element->priority);
242 connman_dbus_dict_append_variant(dict, "IPv4.Address",
243 DBUS_TYPE_STRING, &address);
245 connman_dbus_dict_append_variant(dict, "IPv4.Netmask",
246 DBUS_TYPE_STRING, &netmask);
248 connman_dbus_dict_append_variant(dict, "IPv4.Gateway",
249 DBUS_TYPE_STRING, &gateway);
251 if (element->wifi.security != NULL) {
252 const char *passphrase = "";
254 connman_dbus_dict_append_variant(dict, "WiFi.Security",
255 DBUS_TYPE_STRING, &element->wifi.security);
257 if (element->wifi.passphrase != NULL)
258 passphrase = element->wifi.passphrase;
260 connman_dbus_dict_append_variant(dict, "WiFi.Passphrase",
261 DBUS_TYPE_STRING, &passphrase);
264 __connman_element_lock(element);
266 for (list = element->properties; list; list = list->next) {
267 struct connman_property *property = list->data;
269 append_property(dict, property);
272 __connman_element_unlock(element);
275 static void set_common_property(struct connman_element *element,
276 const char *name, DBusMessageIter *value)
280 if (g_str_equal(name, "Priority") == TRUE) {
281 dbus_message_iter_get_basic(value, &element->priority);
285 __connman_element_lock(element);
287 for (list = element->properties; list; list = list->next) {
288 struct connman_property *property = list->data;
291 if (g_str_equal(property->name, name) == FALSE)
294 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
297 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
299 if (property->type == DBUS_TYPE_STRING) {
300 dbus_message_iter_get_basic(value, &str);
301 g_free(property->value);
302 property->value = g_strdup(str);
304 property->value = NULL;
307 __connman_element_unlock(element);
310 static void emit_enabled_signal(DBusConnection *conn,
311 struct connman_element *element)
314 DBusMessageIter entry, value;
315 const char *iface, *key;
317 DBG("conn %p", conn);
322 switch (element->type) {
323 case CONNMAN_ELEMENT_TYPE_DEVICE:
324 iface = CONNMAN_DEVICE_INTERFACE;
327 case CONNMAN_ELEMENT_TYPE_NETWORK:
328 iface = CONNMAN_NETWORK_INTERFACE;
335 signal = dbus_message_new_signal(element->path,
336 iface, "PropertyChanged");
340 dbus_message_iter_init_append(signal, &entry);
342 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
344 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
345 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
346 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
348 dbus_message_iter_close_container(&entry, &value);
350 g_dbus_send_message(conn, signal);
353 static DBusMessage *do_update(DBusConnection *conn,
354 DBusMessage *msg, void *data)
356 struct connman_element *element = data;
358 DBG("conn %p", conn);
360 if (element->enabled == FALSE)
361 return __connman_error_failed(msg);
363 if (element->driver && element->driver->update) {
364 DBG("Calling update callback");
365 if (element->driver->update(element) < 0)
366 return __connman_error_failed(msg);
370 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
373 static DBusMessage *do_enable(DBusConnection *conn,
374 DBusMessage *msg, void *data)
376 struct connman_element *element = data;
378 DBG("conn %p", conn);
380 if (element->enabled == TRUE)
381 return __connman_error_failed(msg);
383 if (element->driver && element->driver->enable) {
384 DBG("Calling enable callback");
385 if (element->driver->enable(element) < 0)
386 return __connman_error_failed(msg);
389 element->enabled = TRUE;
391 emit_enabled_signal(connection, element);
393 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
396 static DBusMessage *do_disable(DBusConnection *conn,
397 DBusMessage *msg, void *data)
399 struct connman_element *element = data;
401 DBG("conn %p", conn);
403 if (element->enabled == FALSE)
404 return __connman_error_failed(msg);
406 if (element->driver && element->driver->disable) {
407 DBG("Calling disable callback");
408 if (element->driver->disable(element) < 0)
409 return __connman_error_failed(msg);
412 element->enabled = FALSE;
414 emit_enabled_signal(connection, element);
416 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
419 static void append_networks(struct connman_element *element,
420 DBusMessageIter *entry)
422 DBusMessageIter value, iter;
423 const char *key = "Networks";
425 if (element->subtype != CONNMAN_ELEMENT_SUBTYPE_WIFI &&
426 element->subtype != CONNMAN_ELEMENT_SUBTYPE_WIMAX)
429 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
431 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
432 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
435 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
436 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
438 __connman_element_list(element, CONNMAN_ELEMENT_TYPE_NETWORK, &iter);
440 dbus_message_iter_close_container(&value, &iter);
442 dbus_message_iter_close_container(entry, &value);
445 static DBusMessage *device_get_properties(DBusConnection *conn,
446 DBusMessage *msg, void *data)
448 struct connman_element *element = data;
450 DBusMessageIter array, dict, entry;
453 DBG("conn %p", conn);
455 reply = dbus_message_new_method_return(msg);
459 dbus_message_iter_init_append(reply, &array);
461 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
462 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
463 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
464 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
466 str = subtype2string(element->subtype);
468 connman_dbus_dict_append_variant(&dict, "Type",
469 DBUS_TYPE_STRING, &str);
471 str = __connman_element_policy2string(element->policy);
473 connman_dbus_dict_append_variant(&dict, "Policy",
474 DBUS_TYPE_STRING, &str);
476 connman_dbus_dict_append_variant(&dict, "Powered",
477 DBUS_TYPE_BOOLEAN, &element->enabled);
479 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
482 append_networks(element, &entry);
484 dbus_message_iter_close_container(&dict, &entry);
486 add_common_properties(element, &dict);
488 dbus_message_iter_close_container(&array, &dict);
493 static DBusMessage *device_set_property(DBusConnection *conn,
494 DBusMessage *msg, void *data)
496 struct connman_element *element = data;
497 DBusMessageIter iter, value;
500 DBG("conn %p", conn);
502 if (dbus_message_iter_init(msg, &iter) == FALSE)
503 return __connman_error_invalid_arguments(msg);
505 dbus_message_iter_get_basic(&iter, &name);
506 dbus_message_iter_next(&iter);
507 dbus_message_iter_recurse(&iter, &value);
509 if (__connman_security_check_privileges(msg) < 0)
510 return __connman_error_permission_denied(msg);
512 if (g_str_equal(name, "Powered") == TRUE) {
515 dbus_message_iter_get_basic(&value, &powered);
518 do_enable(conn, msg, data);
520 do_disable(conn, msg, data);
522 set_common_property(element, name, &value);
524 __connman_element_store(element);
526 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
529 static int parse_network_dict(DBusMessageIter *iter, const char **ssid,
530 const char **security, const char **passphrase)
532 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_DICT_ENTRY) {
533 DBusMessageIter entry, value;
536 dbus_message_iter_recurse(iter, &entry);
537 dbus_message_iter_get_basic(&entry, &key);
539 dbus_message_iter_next(&entry);
540 dbus_message_iter_recurse(&entry, &value);
542 switch (dbus_message_iter_get_arg_type(&value)) {
543 case DBUS_TYPE_STRING:
544 if (g_str_equal(key, "WiFi.SSID") == TRUE)
545 dbus_message_iter_get_basic(&value, ssid);
546 else if (g_str_equal(key, "WiFi.Security") == TRUE)
547 dbus_message_iter_get_basic(&value, security);
548 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
549 dbus_message_iter_get_basic(&value, passphrase);
553 dbus_message_iter_next(iter);
559 static DBusMessage *device_create_network(DBusConnection *conn,
560 DBusMessage *msg, void *data)
562 struct connman_element *element = data;
563 struct connman_element *network;
564 DBusMessageIter iter, array;
565 const char *ssid = NULL, *security = NULL, *passphrase = NULL;
567 DBG("conn %p", conn);
569 if (dbus_message_iter_init(msg, &iter) == FALSE)
570 return __connman_error_invalid_arguments(msg);
572 dbus_message_iter_recurse(&iter, &array);
573 parse_network_dict(&array, &ssid, &security, &passphrase);
575 return __connman_error_invalid_arguments(msg);
577 DBG("ssid %s security %s passphrase %s", ssid, security, passphrase);
579 network = connman_element_create(ssid);
581 network->type = CONNMAN_ELEMENT_TYPE_NETWORK;
582 network->index = element->index;
584 network->remember = TRUE;
586 connman_element_add_static_property(network, "Name",
587 DBUS_TYPE_STRING, &ssid);
589 connman_element_add_static_array_property(element, "WiFi.SSID",
590 DBUS_TYPE_BYTE, &ssid, strlen(ssid));
592 network->wifi.security = g_strdup(security);
593 network->wifi.passphrase = g_strdup(passphrase);
595 connman_element_register(network, element);
597 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &network->path,
601 static DBusMessage *device_remove_network(DBusConnection *conn,
602 DBusMessage *msg, void *data)
604 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
607 static DBusMessage *network_get_properties(DBusConnection *conn,
608 DBusMessage *msg, void *data)
610 struct connman_element *element = data;
612 DBusMessageIter array, dict;
615 DBG("conn %p", conn);
617 reply = dbus_message_new_method_return(msg);
621 dbus_message_iter_init_append(reply, &array);
623 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
624 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
625 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
626 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
628 str = __connman_element_policy2string(element->policy);
630 connman_dbus_dict_append_variant(&dict, "Policy",
631 DBUS_TYPE_STRING, &str);
633 connman_dbus_dict_append_variant(&dict, "Available",
634 DBUS_TYPE_BOOLEAN, &element->available);
636 connman_dbus_dict_append_variant(&dict, "Connected",
637 DBUS_TYPE_BOOLEAN, &element->enabled);
639 connman_dbus_dict_append_variant(&dict, "Remember",
640 DBUS_TYPE_BOOLEAN, &element->remember);
642 add_common_properties(element, &dict);
644 dbus_message_iter_close_container(&array, &dict);
649 static DBusMessage *network_set_property(DBusConnection *conn,
650 DBusMessage *msg, void *data)
652 struct connman_element *element = data;
653 DBusMessageIter iter;
654 DBusMessageIter value;
657 DBG("conn %p", conn);
659 if (dbus_message_iter_init(msg, &iter) == FALSE)
660 return __connman_error_invalid_arguments(msg);
662 dbus_message_iter_get_basic(&iter, &name);
663 dbus_message_iter_next(&iter);
664 dbus_message_iter_recurse(&iter, &value);
666 if (__connman_security_check_privileges(msg) < 0)
667 return __connman_error_permission_denied(msg);
669 if (g_str_equal(name, "Remember") == TRUE) {
670 dbus_message_iter_get_basic(&value, &element->remember);
671 } else if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
674 dbus_message_iter_get_basic(&value, &str);
675 g_free(element->wifi.passphrase);
676 element->wifi.passphrase = g_strdup(str);
678 set_common_property(element, name, &value);
680 __connman_element_store(element);
682 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
685 static DBusMessage *get_connection_properties(DBusConnection *conn,
686 DBusMessage *msg, void *data)
688 struct connman_element *element = data;
690 DBusMessageIter array, dict;
692 DBG("conn %p", conn);
694 reply = dbus_message_new_method_return(msg);
698 dbus_message_iter_init_append(reply, &array);
700 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
701 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
702 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
703 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
705 add_common_properties(element, &dict);
707 dbus_message_iter_close_container(&array, &dict);
712 static GDBusMethodTable device_methods[] = {
713 { "GetProperties", "", "a{sv}", device_get_properties },
714 { "SetProperty", "sv", "", device_set_property },
715 { "CreateNetwork", "a{sv}", "o", device_create_network },
716 { "RemoveNetwork", "o", "", device_remove_network },
717 { "ProposeScan", "", "", do_update },
721 static GDBusMethodTable network_methods[] = {
722 { "GetProperties", "", "a{sv}", network_get_properties },
723 { "SetProperty", "sv", "", network_set_property },
724 { "Connect", "", "", do_enable },
725 { "Disconnect", "", "", do_disable },
729 static GDBusMethodTable connection_methods[] = {
730 { "GetProperties", "", "a{sv}", get_connection_properties },
734 static GDBusSignalTable element_signals[] = {
735 { "PropertyChanged", "sv" },
739 struct append_filter {
740 enum connman_element_type type;
741 DBusMessageIter *iter;
744 static gboolean append_path(GNode *node, gpointer user_data)
746 struct connman_element *element = node->data;
747 struct append_filter *filter = user_data;
749 DBG("element %p name %s", element, element->name);
751 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
754 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
755 filter->type != element->type)
758 if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
759 element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
762 dbus_message_iter_append_basic(filter->iter,
763 DBUS_TYPE_OBJECT_PATH, &element->path);
768 void __connman_element_list(struct connman_element *element,
769 enum connman_element_type type,
770 DBusMessageIter *iter)
772 struct append_filter filter = { type, iter };
777 if (element != NULL) {
778 node = g_node_find(element_root, G_PRE_ORDER,
779 G_TRAVERSE_ALL, element);
785 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
786 append_path, &filter);
790 enum connman_element_type type;
794 static gboolean count_element(GNode *node, gpointer user_data)
796 struct connman_element *element = node->data;
797 struct count_data *data = user_data;
799 DBG("element %p name %s", element, element->name);
801 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
804 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
805 data->type != element->type)
813 int __connman_element_count(struct connman_element *element,
814 enum connman_element_type type)
816 struct count_data data = { type, 0 };
821 if (element != NULL) {
822 node = g_node_find(element_root, G_PRE_ORDER,
823 G_TRAVERSE_ALL, element);
829 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
830 count_element, &data);
835 static gint compare_priority(gconstpointer a, gconstpointer b)
837 const struct connman_driver *driver1 = a;
838 const struct connman_driver *driver2 = b;
840 return driver2->priority - driver1->priority;
843 static gboolean match_driver(struct connman_element *element,
844 struct connman_driver *driver)
846 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
849 if (element->type != driver->type &&
850 driver->type != CONNMAN_ELEMENT_TYPE_UNKNOWN)
853 if (element->subtype == driver->subtype ||
854 driver->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
860 static void enable_element(struct connman_element *element)
862 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
865 if (element->policy != CONNMAN_ELEMENT_POLICY_AUTO)
868 if (element->driver && element->driver->enable) {
869 if (element->driver->enable(element) == 0) {
870 element->enabled = TRUE;
871 emit_enabled_signal(connection, element);
876 static gboolean probe_driver(GNode *node, gpointer data)
878 struct connman_element *element = node->data;
879 struct connman_driver *driver = data;
881 DBG("element %p name %s", element, element->name);
883 if (!element->driver && match_driver(element, driver) == TRUE) {
884 if (driver->probe(element) < 0)
887 __connman_element_lock(element);
888 element->driver = driver;
889 __connman_element_unlock(element);
891 enable_element(element);
897 void __connman_driver_rescan(struct connman_driver *driver)
899 DBG("driver %p name %s", driver, driver->name);
904 if (element_root != NULL)
905 g_node_traverse(element_root, G_PRE_ORDER,
906 G_TRAVERSE_ALL, -1, probe_driver, driver);
910 * connman_driver_register:
911 * @driver: driver definition
913 * Register a new driver
915 * Returns: %0 on success
917 int connman_driver_register(struct connman_driver *driver)
919 DBG("driver %p name %s", driver, driver->name);
921 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
927 driver_list = g_slist_insert_sorted(driver_list, driver,
930 if (element_root != NULL)
931 g_node_traverse(element_root, G_PRE_ORDER,
932 G_TRAVERSE_ALL, -1, probe_driver, driver);
937 static void disable_element(struct connman_element *element)
939 if (element->policy != CONNMAN_ELEMENT_POLICY_AUTO)
942 if (element->enabled == FALSE)
945 if (element->driver && element->driver->disable) {
946 if (element->driver->disable(element) == 0) {
947 element->enabled = FALSE;
948 emit_enabled_signal(connection, element);
953 static gboolean remove_driver(GNode *node, gpointer data)
955 struct connman_element *element = node->data;
956 struct connman_driver *driver = data;
958 DBG("element %p name %s", element, element->name);
960 if (element->driver == driver) {
961 disable_element(element);
964 driver->remove(element);
966 __connman_element_lock(element);
967 element->driver = NULL;
968 __connman_element_unlock(element);
975 * connman_driver_unregister:
976 * @driver: driver definition
978 * Remove a previously registered driver
980 void connman_driver_unregister(struct connman_driver *driver)
982 DBG("driver %p name %s", driver, driver->name);
984 driver_list = g_slist_remove(driver_list, driver);
986 if (element_root != NULL)
987 g_node_traverse(element_root, G_POST_ORDER,
988 G_TRAVERSE_ALL, -1, remove_driver, driver);
992 * connman_element_create:
993 * @name: element name
995 * Allocate a new element and assign the given #name to it. If the name
996 * is #NULL, it will be later on created based on the element type.
998 * Returns: a newly-allocated #connman_element structure
1000 struct connman_element *connman_element_create(const char *name)
1002 struct connman_element *element;
1004 element = g_try_new0(struct connman_element, 1);
1005 if (element == NULL)
1008 DBG("element %p", element);
1010 element->refcount = 1;
1012 element->name = g_strdup(name);
1013 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
1014 element->subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
1015 element->state = CONNMAN_ELEMENT_STATE_CLOSED;
1016 element->policy = CONNMAN_ELEMENT_POLICY_AUTO;
1017 element->index = -1;
1018 element->enabled = FALSE;
1023 struct connman_element *connman_element_ref(struct connman_element *element)
1025 DBG("element %p name %s refcount %d", element, element->name,
1026 g_atomic_int_get(&element->refcount) + 1);
1028 g_atomic_int_inc(&element->refcount);
1033 static void free_properties(struct connman_element *element)
1037 DBG("element %p name %s", element, element->name);
1039 __connman_element_lock(element);
1041 for (list = element->properties; list; list = list->next) {
1042 struct connman_property *property = list->data;
1044 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1045 g_free(property->value);
1047 g_free(property->name);
1051 g_slist_free(element->properties);
1053 element->properties = NULL;
1055 __connman_element_unlock(element);
1058 void connman_element_unref(struct connman_element *element)
1060 DBG("element %p name %s refcount %d", element, element->name,
1061 g_atomic_int_get(&element->refcount) - 1);
1063 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
1064 free_properties(element);
1065 g_free(element->ipv4.address);
1066 g_free(element->ipv4.netmask);
1067 g_free(element->ipv4.gateway);
1068 g_free(element->ipv4.network);
1069 g_free(element->ipv4.broadcast);
1070 g_free(element->ipv4.nameserver);
1071 g_free(element->devname);
1072 g_free(element->path);
1073 g_free(element->name);
1078 int connman_element_add_static_property(struct connman_element *element,
1079 const char *name, int type, const void *value)
1081 struct connman_property *property;
1083 DBG("element %p name %s", element, element->name);
1085 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
1088 property = g_try_new0(struct connman_property, 1);
1089 if (property == NULL)
1092 property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
1093 property->id = CONNMAN_PROPERTY_ID_INVALID;
1094 property->name = g_strdup(name);
1095 property->type = type;
1097 DBG("name %s type %d value %p", name, type, value);
1100 case DBUS_TYPE_STRING:
1101 property->value = g_strdup(*((const char **) value));
1103 case DBUS_TYPE_BYTE:
1104 property->value = g_try_malloc(1);
1105 if (property->value != NULL)
1106 memcpy(property->value, value, 1);
1110 __connman_element_lock(element);
1111 element->properties = g_slist_append(element->properties, property);
1112 __connman_element_unlock(element);
1117 static void emit_property_changed(DBusConnection *conn,
1118 struct connman_element *element,
1119 const char *name, int type, const void *data)
1121 DBusMessage *signal;
1122 DBusMessageIter entry, value;
1123 const char *iface, *sig;
1125 DBG("conn %p", conn);
1127 switch (element->type) {
1128 case CONNMAN_ELEMENT_TYPE_DEVICE:
1129 iface = CONNMAN_DEVICE_INTERFACE;
1131 case CONNMAN_ELEMENT_TYPE_NETWORK:
1132 iface = CONNMAN_NETWORK_INTERFACE;
1138 signal = dbus_message_new_signal(element->path,
1139 iface, "PropertyChanged");
1143 dbus_message_iter_init_append(signal, &entry);
1145 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &name);
1148 case DBUS_TYPE_STRING:
1149 sig = DBUS_TYPE_STRING_AS_STRING;
1151 case DBUS_TYPE_BYTE:
1152 sig = DBUS_TYPE_BYTE_AS_STRING;
1155 sig = DBUS_TYPE_VARIANT_AS_STRING;
1159 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1161 dbus_message_iter_append_basic(&value, type, data);
1162 dbus_message_iter_close_container(&entry, &value);
1164 g_dbus_send_message(conn, signal);
1167 int connman_element_set_static_property(struct connman_element *element,
1168 const char *name, int type, const void *value)
1172 DBG("element %p name %s", element, element->name);
1174 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
1177 __connman_element_lock(element);
1179 for (list = element->properties; list; list = list->next) {
1180 struct connman_property *property = list->data;
1182 if (g_str_equal(property->name, name) == FALSE)
1185 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1188 property->type = type;
1189 g_free(property->value);
1192 case DBUS_TYPE_STRING:
1193 property->value = g_strdup(*((const char **) value));
1195 case DBUS_TYPE_BYTE:
1196 property->value = g_try_malloc(1);
1197 if (property->value != NULL)
1198 memcpy(property->value, value, 1);
1203 __connman_element_unlock(element);
1205 emit_property_changed(connection, element, name, type, value);
1210 int connman_element_add_static_array_property(struct connman_element *element,
1211 const char *name, int type, const void *value, int len)
1213 struct connman_property *property;
1215 DBG("element %p name %s", element, element->name);
1217 if (type != DBUS_TYPE_BYTE)
1220 property = g_try_new0(struct connman_property, 1);
1221 if (property == NULL)
1224 property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
1225 property->id = CONNMAN_PROPERTY_ID_INVALID;
1226 property->name = g_strdup(name);
1227 property->type = DBUS_TYPE_ARRAY;
1228 property->subtype = type;
1230 DBG("name %s type %d value %p", name, type, value);
1233 case DBUS_TYPE_BYTE:
1234 property->value = g_try_malloc(len);
1235 if (property->value != NULL) {
1236 memcpy(property->value,
1237 *((const unsigned char **) value), len);
1238 property->size = len;
1243 __connman_element_lock(element);
1244 element->properties = g_slist_append(element->properties, property);
1245 __connman_element_unlock(element);
1250 static void *get_reference_value(struct connman_element *element,
1251 enum connman_property_id id)
1255 DBG("element %p name %s", element, element->name);
1257 for (list = element->properties; list; list = list->next) {
1258 struct connman_property *property = list->data;
1260 if (property->id != id)
1263 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1264 return property->value;
1267 if (element->parent == NULL)
1270 return get_reference_value(element->parent, id);
1273 static void set_reference_properties(struct connman_element *element)
1277 DBG("element %p name %s", element, element->name);
1279 for (list = element->properties; list; list = list->next) {
1280 struct connman_property *property = list->data;
1282 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1285 property->value = get_reference_value(element->parent,
1290 static struct connman_property *create_property(struct connman_element *element,
1291 enum connman_property_id id)
1293 struct connman_property *property;
1296 DBG("element %p name %s", element, element->name);
1298 __connman_element_lock(element);
1300 for (list = element->properties; list; list = list->next) {
1301 property = list->data;
1303 if (property->id == id)
1307 property = g_try_new0(struct connman_property, 1);
1308 if (property == NULL)
1311 property->flags = CONNMAN_PROPERTY_FLAG_REFERENCE;
1313 property->name = g_strdup(propid2name(id));
1314 property->type = propid2type(id);
1316 if (property->name == NULL) {
1322 element->properties = g_slist_append(element->properties, property);
1325 __connman_element_unlock(element);
1330 static void create_default_properties(struct connman_element *element)
1332 struct connman_property *property;
1335 DBG("element %p name %s", element, element->name);
1337 for (i = 0; propid_table[i].name; i++) {
1338 DBG("property %s", propid_table[i].name);
1340 property = create_property(element, propid_table[i].id);
1342 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
1344 if (propid_table[i].type != DBUS_TYPE_STRING)
1347 if (propid_table[i].value)
1348 property->value = g_strdup(propid_table[i].value);
1350 property->value = g_strdup("");
1354 static int define_properties_valist(struct connman_element *element,
1357 enum connman_property_id id;
1359 DBG("element %p name %s", element, element->name);
1361 id = va_arg(args, enum connman_property_id);
1363 while (id != CONNMAN_PROPERTY_ID_INVALID) {
1365 DBG("property %d", id);
1367 create_property(element, id);
1369 id = va_arg(args, enum connman_property_id);
1376 * connman_element_define_properties:
1377 * @element: an element
1378 * @varargs: list of property identifiers
1380 * Define the valid properties for an element.
1382 * Returns: %0 on success
1384 int connman_element_define_properties(struct connman_element *element, ...)
1389 DBG("element %p name %s", element, element->name);
1391 va_start(args, element);
1393 err = define_properties_valist(element, args);
1400 int connman_element_create_property(struct connman_element *element,
1401 const char *name, int type)
1406 int connman_element_set_property(struct connman_element *element,
1407 enum connman_property_id id, const void *value)
1410 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
1411 __connman_element_lock(element);
1412 g_free(element->ipv4.address);
1413 element->ipv4.address = g_strdup(*((const char **) value));
1414 __connman_element_unlock(element);
1416 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
1417 __connman_element_lock(element);
1418 g_free(element->ipv4.netmask);
1419 element->ipv4.netmask = g_strdup(*((const char **) value));
1420 __connman_element_unlock(element);
1422 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
1423 __connman_element_lock(element);
1424 g_free(element->ipv4.gateway);
1425 element->ipv4.gateway = g_strdup(*((const char **) value));
1426 __connman_element_unlock(element);
1428 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
1429 __connman_element_lock(element);
1430 g_free(element->ipv4.nameserver);
1431 element->ipv4.nameserver = g_strdup(*((const char **) value));
1432 __connman_element_unlock(element);
1434 case CONNMAN_PROPERTY_ID_WIFI_SECURITY:
1435 __connman_element_lock(element);
1436 g_free(element->wifi.security);
1437 element->wifi.security = g_strdup(*((const char **) value));
1438 __connman_element_unlock(element);
1440 case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE:
1441 __connman_element_lock(element);
1442 g_free(element->wifi.passphrase);
1443 element->wifi.passphrase = g_strdup(*((const char **) value));
1444 __connman_element_unlock(element);
1453 int connman_element_get_value(struct connman_element *element,
1454 enum connman_property_id id, void *value)
1456 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1460 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
1461 if (element->ipv4.address == NULL)
1462 return connman_element_get_value(element->parent,
1464 __connman_element_lock(element);
1465 *((char **) value) = element->ipv4.address;
1466 __connman_element_unlock(element);
1468 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
1469 if (element->ipv4.netmask == NULL)
1470 return connman_element_get_value(element->parent,
1472 __connman_element_lock(element);
1473 *((char **) value) = element->ipv4.netmask;
1474 __connman_element_unlock(element);
1476 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
1477 if (element->ipv4.gateway == NULL)
1478 return connman_element_get_value(element->parent,
1480 __connman_element_lock(element);
1481 *((char **) value) = element->ipv4.gateway;
1482 __connman_element_unlock(element);
1484 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
1485 if (element->ipv4.nameserver == NULL)
1486 return connman_element_get_value(element->parent,
1488 __connman_element_lock(element);
1489 *((char **) value) = element->ipv4.nameserver;
1490 __connman_element_unlock(element);
1492 case CONNMAN_PROPERTY_ID_WIFI_SECURITY:
1493 if (element->wifi.security == NULL)
1494 return connman_element_get_value(element->parent,
1496 __connman_element_lock(element);
1497 *((char **) value) = element->wifi.security;
1498 __connman_element_unlock(element);
1500 case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE:
1501 if (element->wifi.passphrase == NULL)
1502 return connman_element_get_value(element->parent,
1504 __connman_element_lock(element);
1505 *((char **) value) = element->wifi.passphrase;
1506 __connman_element_unlock(element);
1515 gboolean connman_element_get_static_property(struct connman_element *element,
1516 const char *name, void *value)
1519 gboolean found = FALSE;
1521 DBG("element %p name %s", element, element->name);
1523 __connman_element_lock(element);
1525 for (list = element->properties; list; list = list->next) {
1526 struct connman_property *property = list->data;
1528 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1531 if (g_str_equal(property->name, name) == TRUE) {
1532 switch (property->type) {
1533 case DBUS_TYPE_STRING:
1534 *((char **) value) = property->value;
1542 __connman_element_unlock(element);
1547 gboolean connman_element_get_static_array_property(struct connman_element *element,
1548 const char *name, void *value, int *len)
1551 gboolean found = FALSE;
1553 DBG("element %p name %s", element, element->name);
1555 __connman_element_lock(element);
1557 for (list = element->properties; list; list = list->next) {
1558 struct connman_property *property = list->data;
1560 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1563 if (g_str_equal(property->name, name) == TRUE) {
1564 *((char **) value) = property->value;
1565 *len = property->size;
1571 __connman_element_unlock(element);
1576 gboolean connman_element_match_static_property(struct connman_element *element,
1577 const char *name, const void *value)
1580 gboolean result = FALSE;
1582 DBG("element %p name %s", element, element->name);
1584 __connman_element_lock(element);
1586 for (list = element->properties; list; list = list->next) {
1587 struct connman_property *property = list->data;
1589 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1592 if (g_str_equal(property->name, name) == FALSE)
1595 if (property->type == DBUS_TYPE_STRING)
1596 result = g_str_equal(property->value,
1597 *((const char **) value));
1603 __connman_element_unlock(element);
1608 static void append_devices(DBusMessageIter *entry)
1610 DBusMessageIter value, iter;
1611 const char *key = "Devices";
1613 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1615 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1616 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
1619 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
1620 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
1621 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
1622 dbus_message_iter_close_container(&value, &iter);
1624 dbus_message_iter_close_container(entry, &value);
1627 static void emit_devices_signal(DBusConnection *conn)
1629 DBusMessage *signal;
1630 DBusMessageIter entry;
1632 DBG("conn %p", conn);
1634 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1635 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1639 dbus_message_iter_init_append(signal, &entry);
1641 append_devices(&entry);
1643 g_dbus_send_message(conn, signal);
1646 static void emit_networks_signal(DBusConnection *conn,
1647 struct connman_element *device)
1649 DBusMessage *signal;
1650 DBusMessageIter entry;
1652 DBG("conn %p", conn);
1657 signal = dbus_message_new_signal(device->path,
1658 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1662 dbus_message_iter_init_append(signal, &entry);
1664 append_networks(device, &entry);
1666 g_dbus_send_message(conn, signal);
1669 static void append_connections(DBusMessageIter *entry)
1671 DBusMessageIter value, iter;
1672 const char *key = "Connections";
1674 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1676 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1677 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
1680 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
1681 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
1682 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION, &iter);
1683 dbus_message_iter_close_container(&value, &iter);
1685 dbus_message_iter_close_container(entry, &value);
1688 static void emit_connections_signal(DBusConnection *conn)
1690 DBusMessage *signal;
1691 DBusMessageIter entry;
1693 DBG("conn %p", conn);
1695 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1696 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1700 dbus_message_iter_init_append(signal, &entry);
1702 append_connections(&entry);
1704 g_dbus_send_message(conn, signal);
1707 static void append_state(DBusMessageIter *entry, const char *state)
1709 DBusMessageIter value;
1710 const char *key = "State";
1712 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1714 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1715 DBUS_TYPE_STRING_AS_STRING, &value);
1716 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
1717 dbus_message_iter_close_container(entry, &value);
1720 static void emit_state_change(DBusConnection *conn, const char *state)
1722 DBusMessage *signal;
1723 DBusMessageIter entry;
1725 DBG("conn %p", conn);
1727 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1728 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1732 dbus_message_iter_init_append(signal, &entry);
1734 append_state(&entry, state);
1736 g_dbus_send_message(conn, signal);
1739 static void register_element(gpointer data, gpointer user_data)
1741 struct connman_element *element = data;
1742 const gchar *basepath;
1746 __connman_element_lock(element);
1748 if (element->parent) {
1749 node = g_node_find(element_root, G_PRE_ORDER,
1750 G_TRAVERSE_ALL, element->parent);
1751 basepath = element->parent->path;
1753 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
1754 element->subtype = element->parent->subtype;
1756 element->parent = element_root->data;
1758 node = element_root;
1762 element->path = g_strdup_printf("%s/%s", basepath, element->name);
1764 set_reference_properties(element);
1766 __connman_element_unlock(element);
1768 DBG("element %p path %s", element, element->path);
1770 __connman_element_load(element);
1772 g_node_append_data(node, element);
1774 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
1775 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
1776 if (g_dbus_register_interface(connection, element->path,
1777 CONNMAN_DEVICE_INTERFACE,
1778 device_methods, element_signals,
1779 NULL, element, NULL) == FALSE)
1780 connman_error("Failed to register %s device",
1783 emit_devices_signal(connection);
1786 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1787 if (g_dbus_register_interface(connection, element->path,
1788 CONNMAN_NETWORK_INTERFACE,
1789 network_methods, element_signals,
1790 NULL, element, NULL) == FALSE)
1791 connman_error("Failed to register %s network",
1794 emit_networks_signal(connection, element->parent);
1797 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1798 if (g_dbus_register_interface(connection, element->path,
1799 CONNMAN_CONNECTION_INTERFACE,
1800 connection_methods, element_signals,
1801 NULL, element, NULL) == FALSE)
1802 connman_error("Failed to register %s connection",
1805 emit_connections_signal(connection);
1806 emit_state_change(connection, "online");
1810 __connman_element_store(element);
1812 for (list = driver_list; list; list = list->next) {
1813 struct connman_driver *driver = list->data;
1815 if (match_driver(element, driver) == FALSE)
1818 DBG("driver %p name %s", driver, driver->name);
1820 if (driver->probe(element) == 0) {
1821 __connman_element_lock(element);
1822 element->driver = driver;
1823 __connman_element_unlock(element);
1825 enable_element(element);
1832 * connman_element_register:
1833 * @element: the element to register
1834 * @parent: the parent to register the element with
1836 * Register an element with the core. It will be register under the given
1837 * parent of if %NULL is provided under the root element.
1839 * Returns: %0 on success
1841 int connman_element_register(struct connman_element *element,
1842 struct connman_element *parent)
1844 DBG("element %p name %s parent %p", element, element->name, parent);
1846 if (element->devname == NULL)
1847 element->devname = g_strdup(element->name);
1849 if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
1850 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
1851 if (g_pattern_match_simple(device_filter,
1852 element->devname) == FALSE) {
1853 DBG("ignoring %s [%s] device", element->name,
1859 if (connman_element_ref(element) == NULL)
1862 __connman_element_lock(element);
1864 if (element->name == NULL) {
1865 element->name = g_strdup(type2string(element->type));
1866 if (element->name == NULL) {
1867 __connman_element_unlock(element);
1872 element->parent = parent;
1874 __connman_element_unlock(element);
1876 register_element(element, NULL);
1881 static gboolean remove_element(GNode *node, gpointer user_data)
1883 struct connman_element *element = node->data;
1884 struct connman_element *root = user_data;
1886 DBG("element %p name %s", element, element->name);
1888 if (element == root)
1891 if (element->driver) {
1892 disable_element(element);
1894 if (element->driver->remove)
1895 element->driver->remove(element);
1897 __connman_element_lock(element);
1898 element->driver = NULL;
1899 __connman_element_unlock(element);
1903 g_node_unlink(node);
1904 g_node_destroy(node);
1907 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1908 emit_state_change(connection, "offline");
1909 emit_connections_signal(connection);
1911 g_dbus_unregister_interface(connection, element->path,
1912 CONNMAN_CONNECTION_INTERFACE);
1915 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1916 emit_networks_signal(connection, element->parent);
1918 g_dbus_unregister_interface(connection, element->path,
1919 CONNMAN_NETWORK_INTERFACE);
1922 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
1923 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
1924 emit_devices_signal(connection);
1926 g_dbus_unregister_interface(connection, element->path,
1927 CONNMAN_DEVICE_INTERFACE);
1930 connman_element_unref(element);
1935 void connman_element_unregister(struct connman_element *element)
1939 DBG("element %p name %s", element, element->name);
1941 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1944 g_node_traverse(node, G_POST_ORDER,
1945 G_TRAVERSE_ALL, -1, remove_element, NULL);
1948 void connman_element_unregister_children(struct connman_element *element)
1952 DBG("element %p name %s", element, element->name);
1954 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1957 g_node_traverse(node, G_POST_ORDER,
1958 G_TRAVERSE_ALL, -1, remove_element, element);
1961 static gboolean update_element(GNode *node, gpointer user_data)
1963 struct connman_element *element = node->data;
1965 DBG("element %p name %s", element, element->name);
1967 if (element->driver && element->driver->update)
1968 element->driver->update(element);
1973 void connman_element_update(struct connman_element *element)
1977 DBG("element %p name %s", element, element->name);
1979 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1982 g_node_traverse(node, G_PRE_ORDER,
1983 G_TRAVERSE_ALL, -1, update_element, NULL);
1986 int connman_element_set_enabled(struct connman_element *element,
1989 if (element->enabled == enabled)
1992 element->enabled = enabled;
1994 emit_enabled_signal(connection, element);
1999 int __connman_element_init(DBusConnection *conn, const char *device)
2001 struct connman_element *element;
2003 DBG("conn %p", conn);
2005 connection = dbus_connection_ref(conn);
2006 if (connection == NULL)
2009 device_filter = g_strdup(device);
2011 element = connman_element_create("root");
2013 element->path = g_strdup("/");
2014 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
2016 create_default_properties(element);
2018 element_root = g_node_new(element);
2020 __connman_device_init();
2025 static gboolean free_driver(GNode *node, gpointer data)
2027 struct connman_element *element = node->data;
2029 DBG("element %p name %s", element, element->name);
2031 if (element->driver) {
2032 disable_element(element);
2034 if (element->driver->remove)
2035 element->driver->remove(element);
2037 __connman_element_lock(element);
2038 element->driver = NULL;
2039 __connman_element_unlock(element);
2045 static gboolean free_node(GNode *node, gpointer data)
2047 struct connman_element *element = node->data;
2049 DBG("element %p name %s", element, element->name);
2051 if (g_node_depth(node) > 1)
2052 connman_element_unregister(element);
2057 void __connman_element_cleanup(void)
2061 __connman_device_cleanup();
2063 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2066 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2069 g_node_destroy(element_root);
2070 element_root = NULL;
2072 g_free(device_filter);
2074 dbus_connection_unref(connection);