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_BROADCAST,
56 DBUS_TYPE_STRING, "IPv4.Broadcast" },
57 { CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
58 DBUS_TYPE_STRING, "IPv4.Nameserver" },
60 { CONNMAN_PROPERTY_ID_WIFI_SECURITY,
61 DBUS_TYPE_STRING, "WiFi.Security" },
62 { CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE,
63 DBUS_TYPE_STRING, "WiFi.Passphrase" },
68 static int propid2type(enum connman_property_id id)
72 for (i = 0; propid_table[i].name; i++) {
73 if (propid_table[i].id == id)
74 return propid_table[i].type;
77 return DBUS_TYPE_INVALID;
80 static const char *propid2name(enum connman_property_id id)
84 for (i = 0; propid_table[i].name; i++) {
85 if (propid_table[i].id == id)
86 return propid_table[i].name;
92 static const char *type2string(enum connman_element_type type)
95 case CONNMAN_ELEMENT_TYPE_UNKNOWN:
97 case CONNMAN_ELEMENT_TYPE_ROOT:
99 case CONNMAN_ELEMENT_TYPE_PROFILE:
101 case CONNMAN_ELEMENT_TYPE_DEVICE:
103 case CONNMAN_ELEMENT_TYPE_NETWORK:
105 case CONNMAN_ELEMENT_TYPE_SERVICE:
107 case CONNMAN_ELEMENT_TYPE_IPV4:
109 case CONNMAN_ELEMENT_TYPE_IPV6:
111 case CONNMAN_ELEMENT_TYPE_DHCP:
113 case CONNMAN_ELEMENT_TYPE_BOOTP:
115 case CONNMAN_ELEMENT_TYPE_ZEROCONF:
117 case CONNMAN_ELEMENT_TYPE_CONNECTION:
124 static const char *subtype2string(enum connman_element_subtype type)
127 case CONNMAN_ELEMENT_SUBTYPE_UNKNOWN:
129 case CONNMAN_ELEMENT_SUBTYPE_FAKE:
131 case CONNMAN_ELEMENT_SUBTYPE_NETWORK:
133 case CONNMAN_ELEMENT_SUBTYPE_ETHERNET:
135 case CONNMAN_ELEMENT_SUBTYPE_WIFI:
137 case CONNMAN_ELEMENT_SUBTYPE_WIMAX:
139 case CONNMAN_ELEMENT_SUBTYPE_MODEM:
141 case CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH:
148 static const char *subtype2description(enum connman_element_subtype type)
151 case CONNMAN_ELEMENT_SUBTYPE_UNKNOWN:
152 case CONNMAN_ELEMENT_SUBTYPE_FAKE:
153 case CONNMAN_ELEMENT_SUBTYPE_NETWORK:
155 case CONNMAN_ELEMENT_SUBTYPE_ETHERNET:
157 case CONNMAN_ELEMENT_SUBTYPE_WIFI:
159 case CONNMAN_ELEMENT_SUBTYPE_WIMAX:
161 case CONNMAN_ELEMENT_SUBTYPE_MODEM:
163 case CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH:
170 const char *__connman_element_policy2string(enum connman_element_policy policy)
173 case CONNMAN_ELEMENT_POLICY_UNKNOWN:
175 case CONNMAN_ELEMENT_POLICY_IGNORE:
177 case CONNMAN_ELEMENT_POLICY_AUTO:
179 case CONNMAN_ELEMENT_POLICY_ASK:
186 enum connman_element_policy __connman_element_string2policy(const char *policy)
188 if (strcasecmp(policy, "ignore") == 0)
189 return CONNMAN_ELEMENT_POLICY_IGNORE;
190 else if (strcasecmp(policy, "auto") == 0)
191 return CONNMAN_ELEMENT_POLICY_AUTO;
192 else if (strcasecmp(policy, "ask") == 0)
193 return CONNMAN_ELEMENT_POLICY_ASK;
195 return CONNMAN_ELEMENT_POLICY_UNKNOWN;
198 const char *__connman_ipv4_method2string(enum connman_ipv4_method method)
201 case CONNMAN_IPV4_METHOD_UNKNOWN:
203 case CONNMAN_IPV4_METHOD_OFF:
205 case CONNMAN_IPV4_METHOD_STATIC:
207 case CONNMAN_IPV4_METHOD_DHCP:
214 enum connman_ipv4_method __connman_ipv4_string2method(const char *method)
216 if (strcasecmp(method, "off") == 0)
217 return CONNMAN_IPV4_METHOD_OFF;
218 else if (strcasecmp(method, "static") == 0)
219 return CONNMAN_IPV4_METHOD_STATIC;
220 else if (strcasecmp(method, "dhcp") == 0)
221 return CONNMAN_IPV4_METHOD_DHCP;
223 return CONNMAN_IPV4_METHOD_UNKNOWN;
226 static void append_property(DBusMessageIter *dict,
227 struct connman_property *property)
229 if (property->value == NULL)
232 switch (property->type) {
233 case DBUS_TYPE_ARRAY:
234 connman_dbus_dict_append_array(dict, property->name,
235 property->subtype, &property->value, property->size);
237 case DBUS_TYPE_STRING:
238 connman_dbus_dict_append_variant(dict, property->name,
239 property->type, &property->value);
242 connman_dbus_dict_append_variant(dict, property->name,
243 property->type, property->value);
248 static void add_common_properties(struct connman_element *element,
249 DBusMessageIter *dict)
251 const char *address = NULL, *netmask = NULL, *gateway = NULL;
254 connman_element_get_value(element,
255 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
256 connman_element_get_value(element,
257 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
258 connman_element_get_value(element,
259 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
261 if (element->priority > 0)
262 connman_dbus_dict_append_variant(dict, "Priority",
263 DBUS_TYPE_UINT16, &element->priority);
266 connman_dbus_dict_append_variant(dict, "IPv4.Address",
267 DBUS_TYPE_STRING, &address);
269 connman_dbus_dict_append_variant(dict, "IPv4.Netmask",
270 DBUS_TYPE_STRING, &netmask);
272 connman_dbus_dict_append_variant(dict, "IPv4.Gateway",
273 DBUS_TYPE_STRING, &gateway);
275 if (element->wifi.security != NULL) {
276 const char *passphrase = "";
278 connman_dbus_dict_append_variant(dict, "WiFi.Security",
279 DBUS_TYPE_STRING, &element->wifi.security);
281 if (element->wifi.passphrase != NULL)
282 passphrase = element->wifi.passphrase;
284 connman_dbus_dict_append_variant(dict, "WiFi.Passphrase",
285 DBUS_TYPE_STRING, &passphrase);
288 __connman_element_lock(element);
290 for (list = element->properties; list; list = list->next) {
291 struct connman_property *property = list->data;
293 append_property(dict, property);
296 __connman_element_unlock(element);
299 static void set_common_property(struct connman_element *element,
300 const char *name, DBusMessageIter *value)
304 if (g_str_equal(name, "Priority") == TRUE) {
305 dbus_message_iter_get_basic(value, &element->priority);
309 __connman_element_lock(element);
311 for (list = element->properties; list; list = list->next) {
312 struct connman_property *property = list->data;
315 if (g_str_equal(property->name, name) == FALSE)
318 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
321 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
323 if (property->type == DBUS_TYPE_STRING) {
324 dbus_message_iter_get_basic(value, &str);
325 g_free(property->value);
326 property->value = g_strdup(str);
328 property->value = NULL;
331 __connman_element_unlock(element);
334 static void emit_enabled_signal(DBusConnection *conn,
335 struct connman_element *element)
338 DBusMessageIter entry, value;
339 const char *iface, *key;
341 DBG("conn %p", conn);
346 switch (element->type) {
347 case CONNMAN_ELEMENT_TYPE_DEVICE:
348 iface = CONNMAN_DEVICE_INTERFACE;
351 case CONNMAN_ELEMENT_TYPE_NETWORK:
352 iface = CONNMAN_NETWORK_INTERFACE;
355 case CONNMAN_ELEMENT_TYPE_CONNECTION:
356 iface = CONNMAN_CONNECTION_INTERFACE;
363 signal = dbus_message_new_signal(element->path,
364 iface, "PropertyChanged");
368 dbus_message_iter_init_append(signal, &entry);
370 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
372 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
373 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
374 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
376 dbus_message_iter_close_container(&entry, &value);
378 g_dbus_send_message(conn, signal);
381 static void emit_scanning_signal(DBusConnection *conn,
382 struct connman_element *element)
385 DBusMessageIter entry, value;
386 const char *key = "Scanning";
388 DBG("conn %p", conn);
393 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
396 signal = dbus_message_new_signal(element->path,
397 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
401 dbus_message_iter_init_append(signal, &entry);
403 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
405 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
406 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
407 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
409 dbus_message_iter_close_container(&entry, &value);
411 g_dbus_send_message(conn, signal);
414 static DBusMessage *do_update(DBusConnection *conn,
415 DBusMessage *msg, void *data)
417 struct connman_element *element = data;
419 DBG("conn %p", conn);
421 if (element->enabled == FALSE)
422 return __connman_error_failed(msg);
424 if (element->driver && element->driver->update) {
425 DBG("Calling update callback");
426 if (element->driver->update(element) < 0)
427 return __connman_error_failed(msg);
431 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
434 static DBusMessage *do_enable(DBusConnection *conn,
435 DBusMessage *msg, void *data)
437 struct connman_element *element = data;
439 DBG("conn %p", conn);
441 if (element->enabled == TRUE)
442 return __connman_error_failed(msg);
444 if (element->driver && element->driver->enable) {
445 DBG("Calling enable callback");
446 if (element->driver->enable(element) < 0)
447 return __connman_error_failed(msg);
450 element->enabled = TRUE;
452 emit_enabled_signal(connection, element);
454 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
457 static DBusMessage *do_disable(DBusConnection *conn,
458 DBusMessage *msg, void *data)
460 struct connman_element *element = data;
462 DBG("conn %p", conn);
464 if (element->enabled == FALSE)
465 return __connman_error_failed(msg);
467 if (element->driver && element->driver->disable) {
468 DBG("Calling disable callback");
469 if (element->driver->disable(element) < 0)
470 return __connman_error_failed(msg);
473 element->enabled = FALSE;
475 emit_enabled_signal(connection, element);
477 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
480 static void append_networks(struct connman_element *element,
481 DBusMessageIter *entry)
483 DBusMessageIter value, iter;
484 const char *key = "Networks";
486 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
488 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
489 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
492 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
493 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
495 __connman_element_list(element, CONNMAN_ELEMENT_TYPE_NETWORK, &iter);
497 dbus_message_iter_close_container(&value, &iter);
499 dbus_message_iter_close_container(entry, &value);
502 static DBusMessage *device_get_properties(DBusConnection *conn,
503 DBusMessage *msg, void *data)
505 struct connman_element *element = data;
507 DBusMessageIter array, dict, entry;
510 DBG("conn %p", conn);
512 reply = dbus_message_new_method_return(msg);
516 dbus_message_iter_init_append(reply, &array);
518 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
519 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
520 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
521 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
523 str = subtype2description(element->subtype);
524 if (str != NULL && element->devname != NULL) {
525 char *name = g_strdup_printf("%s (%s)", str, element->devname);
527 connman_dbus_dict_append_variant(&dict, "Name",
528 DBUS_TYPE_STRING, &name);
532 str = subtype2string(element->subtype);
534 connman_dbus_dict_append_variant(&dict, "Type",
535 DBUS_TYPE_STRING, &str);
537 str = __connman_element_policy2string(element->policy);
539 connman_dbus_dict_append_variant(&dict, "Policy",
540 DBUS_TYPE_STRING, &str);
542 connman_dbus_dict_append_variant(&dict, "Powered",
543 DBUS_TYPE_BOOLEAN, &element->enabled);
545 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI ||
546 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIMAX ||
547 element->subtype == CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH) {
548 connman_dbus_dict_append_variant(&dict, "Scanning",
549 DBUS_TYPE_BOOLEAN, &element->scanning);
551 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
553 append_networks(element, &entry);
554 dbus_message_iter_close_container(&dict, &entry);
557 add_common_properties(element, &dict);
559 dbus_message_iter_close_container(&array, &dict);
564 static DBusMessage *device_set_property(DBusConnection *conn,
565 DBusMessage *msg, void *data)
567 struct connman_element *element = data;
568 DBusMessageIter iter, value;
571 DBG("conn %p", conn);
573 if (dbus_message_iter_init(msg, &iter) == FALSE)
574 return __connman_error_invalid_arguments(msg);
576 dbus_message_iter_get_basic(&iter, &name);
577 dbus_message_iter_next(&iter);
578 dbus_message_iter_recurse(&iter, &value);
580 if (__connman_security_check_privileges(msg) < 0)
581 return __connman_error_permission_denied(msg);
583 if (g_str_equal(name, "Powered") == TRUE) {
586 dbus_message_iter_get_basic(&value, &powered);
589 do_enable(conn, msg, element);
591 do_disable(conn, msg, element);
593 set_common_property(element, name, &value);
595 __connman_element_store(element);
597 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
600 static int parse_network_dict(DBusMessageIter *iter, const char **ssid,
601 const char **security, const char **passphrase)
603 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_DICT_ENTRY) {
604 DBusMessageIter entry, value;
607 dbus_message_iter_recurse(iter, &entry);
608 dbus_message_iter_get_basic(&entry, &key);
610 dbus_message_iter_next(&entry);
611 dbus_message_iter_recurse(&entry, &value);
613 switch (dbus_message_iter_get_arg_type(&value)) {
614 case DBUS_TYPE_STRING:
615 if (g_str_equal(key, "WiFi.SSID") == TRUE)
616 dbus_message_iter_get_basic(&value, ssid);
617 else if (g_str_equal(key, "WiFi.Security") == TRUE)
618 dbus_message_iter_get_basic(&value, security);
619 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
620 dbus_message_iter_get_basic(&value, passphrase);
624 dbus_message_iter_next(iter);
630 static DBusMessage *device_create_network(DBusConnection *conn,
631 DBusMessage *msg, void *data)
633 struct connman_element *element = data;
634 struct connman_element *network;
635 DBusMessageIter iter, array;
636 const char *ssid = NULL, *security = NULL, *passphrase = NULL;
638 DBG("conn %p", conn);
640 if (dbus_message_iter_init(msg, &iter) == FALSE)
641 return __connman_error_invalid_arguments(msg);
643 dbus_message_iter_recurse(&iter, &array);
644 parse_network_dict(&array, &ssid, &security, &passphrase);
646 return __connman_error_invalid_arguments(msg);
648 DBG("ssid %s security %s passphrase %s", ssid, security, passphrase);
650 network = connman_element_create(ssid);
652 network->type = CONNMAN_ELEMENT_TYPE_NETWORK;
653 network->index = element->index;
655 network->remember = TRUE;
657 connman_element_add_static_property(network, "Name",
658 DBUS_TYPE_STRING, &ssid);
660 connman_element_add_static_array_property(element, "WiFi.SSID",
661 DBUS_TYPE_BYTE, &ssid, strlen(ssid));
663 network->wifi.security = g_strdup(security);
664 network->wifi.passphrase = g_strdup(passphrase);
666 connman_element_register(network, element);
668 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &network->path,
672 static DBusMessage *device_remove_network(DBusConnection *conn,
673 DBusMessage *msg, void *data)
675 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
678 static DBusMessage *network_get_properties(DBusConnection *conn,
679 DBusMessage *msg, void *data)
681 struct connman_element *element = data;
683 DBusMessageIter array, dict;
686 DBG("conn %p", conn);
688 reply = dbus_message_new_method_return(msg);
692 dbus_message_iter_init_append(reply, &array);
694 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
695 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
696 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
697 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
699 str = __connman_element_policy2string(element->policy);
701 connman_dbus_dict_append_variant(&dict, "Policy",
702 DBUS_TYPE_STRING, &str);
704 connman_dbus_dict_append_variant(&dict, "Available",
705 DBUS_TYPE_BOOLEAN, &element->available);
707 connman_dbus_dict_append_variant(&dict, "Connected",
708 DBUS_TYPE_BOOLEAN, &element->enabled);
710 connman_dbus_dict_append_variant(&dict, "Remember",
711 DBUS_TYPE_BOOLEAN, &element->remember);
713 add_common_properties(element, &dict);
715 dbus_message_iter_close_container(&array, &dict);
720 static DBusMessage *network_set_property(DBusConnection *conn,
721 DBusMessage *msg, void *data)
723 struct connman_element *element = data;
724 DBusMessageIter iter;
725 DBusMessageIter value;
728 DBG("conn %p", conn);
730 if (dbus_message_iter_init(msg, &iter) == FALSE)
731 return __connman_error_invalid_arguments(msg);
733 dbus_message_iter_get_basic(&iter, &name);
734 dbus_message_iter_next(&iter);
735 dbus_message_iter_recurse(&iter, &value);
737 if (__connman_security_check_privileges(msg) < 0)
738 return __connman_error_permission_denied(msg);
740 if (g_str_equal(name, "Remember") == TRUE) {
741 dbus_message_iter_get_basic(&value, &element->remember);
742 } else if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
745 dbus_message_iter_get_basic(&value, &str);
746 g_free(element->wifi.passphrase);
747 element->wifi.passphrase = g_strdup(str);
749 set_common_property(element, name, &value);
751 __connman_element_store(element);
753 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
756 static DBusMessage *connection_get_properties(DBusConnection *conn,
757 DBusMessage *msg, void *data)
759 struct connman_element *element = data;
761 DBusMessageIter array, dict;
764 DBG("conn %p", conn);
766 reply = dbus_message_new_method_return(msg);
770 dbus_message_iter_init_append(reply, &array);
772 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
773 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
774 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
775 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
777 str = subtype2string(element->subtype);
779 connman_dbus_dict_append_variant(&dict, "Type",
780 DBUS_TYPE_STRING, &str);
782 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI ||
783 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIMAX)
784 connman_dbus_dict_append_variant(&dict, "Strength",
785 DBUS_TYPE_BYTE, &element->strength);
787 connman_dbus_dict_append_variant(&dict, "Default",
788 DBUS_TYPE_BOOLEAN, &element->enabled);
790 add_common_properties(element, &dict);
792 dbus_message_iter_close_container(&array, &dict);
797 static DBusMessage *connection_set_property(DBusConnection *conn,
798 DBusMessage *msg, void *data)
800 struct connman_element *element = data;
801 DBusMessageIter iter, value;
804 DBG("conn %p", conn);
806 if (dbus_message_iter_init(msg, &iter) == FALSE)
807 return __connman_error_invalid_arguments(msg);
809 dbus_message_iter_get_basic(&iter, &name);
810 dbus_message_iter_next(&iter);
811 dbus_message_iter_recurse(&iter, &value);
813 if (__connman_security_check_privileges(msg) < 0)
814 return __connman_error_permission_denied(msg);
816 if (g_str_equal(name, "Default") == TRUE) {
819 dbus_message_iter_get_basic(&value, &enabled);
822 return do_enable(conn, msg, element);
824 return do_disable(conn, msg, element);
827 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
830 static GDBusMethodTable device_methods[] = {
831 { "GetProperties", "", "a{sv}", device_get_properties },
832 { "SetProperty", "sv", "", device_set_property },
833 { "CreateNetwork", "a{sv}", "o", device_create_network },
834 { "RemoveNetwork", "o", "", device_remove_network },
835 { "ProposeScan", "", "", do_update },
839 static GDBusMethodTable network_methods[] = {
840 { "GetProperties", "", "a{sv}", network_get_properties },
841 { "SetProperty", "sv", "", network_set_property },
842 { "Connect", "", "", do_enable },
843 { "Disconnect", "", "", do_disable },
847 static GDBusMethodTable connection_methods[] = {
848 { "GetProperties", "", "a{sv}", connection_get_properties },
849 { "SetProperty", "sv", "", connection_set_property },
853 static GDBusSignalTable element_signals[] = {
854 { "PropertyChanged", "sv" },
858 struct append_filter {
859 enum connman_element_type type;
860 DBusMessageIter *iter;
863 static gboolean append_path(GNode *node, gpointer user_data)
865 struct connman_element *element = node->data;
866 struct append_filter *filter = user_data;
868 DBG("element %p name %s", element, element->name);
870 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
873 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
874 filter->type != element->type)
877 if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
878 element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
881 dbus_message_iter_append_basic(filter->iter,
882 DBUS_TYPE_OBJECT_PATH, &element->path);
887 void __connman_element_list(struct connman_element *element,
888 enum connman_element_type type,
889 DBusMessageIter *iter)
891 struct append_filter filter = { type, iter };
896 if (element != NULL) {
897 node = g_node_find(element_root, G_PRE_ORDER,
898 G_TRAVERSE_ALL, element);
904 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
905 append_path, &filter);
909 enum connman_element_type type;
913 static gboolean count_element(GNode *node, gpointer user_data)
915 struct connman_element *element = node->data;
916 struct count_data *data = user_data;
918 DBG("element %p name %s", element, element->name);
920 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
923 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
924 data->type != element->type)
932 int __connman_element_count(struct connman_element *element,
933 enum connman_element_type type)
935 struct count_data data = { type, 0 };
940 if (element != NULL) {
941 node = g_node_find(element_root, G_PRE_ORDER,
942 G_TRAVERSE_ALL, element);
948 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
949 count_element, &data);
954 static gint compare_priority(gconstpointer a, gconstpointer b)
956 const struct connman_driver *driver1 = a;
957 const struct connman_driver *driver2 = b;
959 return driver2->priority - driver1->priority;
962 static gboolean match_driver(struct connman_element *element,
963 struct connman_driver *driver)
965 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
968 if (element->type != driver->type &&
969 driver->type != CONNMAN_ELEMENT_TYPE_UNKNOWN)
972 if (element->subtype == driver->subtype ||
973 driver->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
979 static void enable_element(struct connman_element *element)
981 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
984 if (element->policy != CONNMAN_ELEMENT_POLICY_AUTO)
987 if (element->driver && element->driver->enable) {
988 if (element->driver->enable(element) == 0) {
989 element->enabled = TRUE;
990 emit_enabled_signal(connection, element);
995 static gboolean probe_driver(GNode *node, gpointer data)
997 struct connman_element *element = node->data;
998 struct connman_driver *driver = data;
1000 DBG("element %p name %s", element, element->name);
1002 if (!element->driver && match_driver(element, driver) == TRUE) {
1003 if (driver->probe(element) < 0)
1006 __connman_element_lock(element);
1007 element->driver = driver;
1008 __connman_element_unlock(element);
1010 enable_element(element);
1016 void __connman_driver_rescan(struct connman_driver *driver)
1018 DBG("driver %p name %s", driver, driver->name);
1023 if (element_root != NULL)
1024 g_node_traverse(element_root, G_PRE_ORDER,
1025 G_TRAVERSE_ALL, -1, probe_driver, driver);
1029 * connman_driver_register:
1030 * @driver: driver definition
1032 * Register a new driver
1034 * Returns: %0 on success
1036 int connman_driver_register(struct connman_driver *driver)
1038 DBG("driver %p name %s", driver, driver->name);
1040 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
1046 driver_list = g_slist_insert_sorted(driver_list, driver,
1049 if (element_root != NULL)
1050 g_node_traverse(element_root, G_PRE_ORDER,
1051 G_TRAVERSE_ALL, -1, probe_driver, driver);
1056 static void disable_element(struct connman_element *element)
1058 if (element->policy != CONNMAN_ELEMENT_POLICY_AUTO)
1061 if (element->enabled == FALSE)
1064 if (element->driver && element->driver->disable) {
1065 if (element->driver->disable(element) == 0) {
1066 element->enabled = FALSE;
1067 emit_enabled_signal(connection, element);
1072 static gboolean remove_driver(GNode *node, gpointer data)
1074 struct connman_element *element = node->data;
1075 struct connman_driver *driver = data;
1077 DBG("element %p name %s", element, element->name);
1079 if (element->driver == driver) {
1080 disable_element(element);
1083 driver->remove(element);
1085 __connman_element_lock(element);
1086 element->driver = NULL;
1087 __connman_element_unlock(element);
1094 * connman_driver_unregister:
1095 * @driver: driver definition
1097 * Remove a previously registered driver
1099 void connman_driver_unregister(struct connman_driver *driver)
1101 DBG("driver %p name %s", driver, driver->name);
1103 driver_list = g_slist_remove(driver_list, driver);
1105 if (element_root != NULL)
1106 g_node_traverse(element_root, G_POST_ORDER,
1107 G_TRAVERSE_ALL, -1, remove_driver, driver);
1111 * connman_element_create:
1112 * @name: element name
1114 * Allocate a new element and assign the given #name to it. If the name
1115 * is #NULL, it will be later on created based on the element type.
1117 * Returns: a newly-allocated #connman_element structure
1119 struct connman_element *connman_element_create(const char *name)
1121 struct connman_element *element;
1123 element = g_try_new0(struct connman_element, 1);
1124 if (element == NULL)
1127 DBG("element %p", element);
1129 element->refcount = 1;
1131 element->name = g_strdup(name);
1132 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
1133 element->subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
1134 element->state = CONNMAN_ELEMENT_STATE_CLOSED;
1135 element->policy = CONNMAN_ELEMENT_POLICY_AUTO;
1136 element->index = -1;
1137 element->enabled = FALSE;
1142 struct connman_element *connman_element_ref(struct connman_element *element)
1144 DBG("element %p name %s refcount %d", element, element->name,
1145 g_atomic_int_get(&element->refcount) + 1);
1147 g_atomic_int_inc(&element->refcount);
1152 static void free_properties(struct connman_element *element)
1156 DBG("element %p name %s", element, element->name);
1158 __connman_element_lock(element);
1160 for (list = element->properties; list; list = list->next) {
1161 struct connman_property *property = list->data;
1163 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1164 g_free(property->value);
1166 g_free(property->name);
1170 g_slist_free(element->properties);
1172 element->properties = NULL;
1174 __connman_element_unlock(element);
1177 void connman_element_unref(struct connman_element *element)
1179 DBG("element %p name %s refcount %d", element, element->name,
1180 g_atomic_int_get(&element->refcount) - 1);
1182 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
1183 free_properties(element);
1184 g_free(element->ipv4.address);
1185 g_free(element->ipv4.netmask);
1186 g_free(element->ipv4.gateway);
1187 g_free(element->ipv4.network);
1188 g_free(element->ipv4.broadcast);
1189 g_free(element->ipv4.nameserver);
1190 g_free(element->devname);
1191 g_free(element->path);
1192 g_free(element->name);
1197 int connman_element_add_static_property(struct connman_element *element,
1198 const char *name, int type, const void *value)
1200 struct connman_property *property;
1202 DBG("element %p name %s", element, element->name);
1204 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
1207 property = g_try_new0(struct connman_property, 1);
1208 if (property == NULL)
1211 property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
1212 property->id = CONNMAN_PROPERTY_ID_INVALID;
1213 property->name = g_strdup(name);
1214 property->type = type;
1216 DBG("name %s type %d value %p", name, type, value);
1219 case DBUS_TYPE_STRING:
1220 property->value = g_strdup(*((const char **) value));
1222 case DBUS_TYPE_BYTE:
1223 property->value = g_try_malloc(1);
1224 if (property->value != NULL)
1225 memcpy(property->value, value, 1);
1229 __connman_element_lock(element);
1230 element->properties = g_slist_append(element->properties, property);
1231 __connman_element_unlock(element);
1236 static void emit_property_changed(DBusConnection *conn,
1237 struct connman_element *element,
1238 const char *name, int type, const void *data)
1240 DBusMessage *signal;
1241 DBusMessageIter entry, value;
1242 const char *iface, *sig;
1244 DBG("conn %p", conn);
1246 switch (element->type) {
1247 case CONNMAN_ELEMENT_TYPE_DEVICE:
1248 iface = CONNMAN_DEVICE_INTERFACE;
1250 case CONNMAN_ELEMENT_TYPE_NETWORK:
1251 iface = CONNMAN_NETWORK_INTERFACE;
1253 case CONNMAN_ELEMENT_TYPE_CONNECTION:
1254 iface = CONNMAN_CONNECTION_INTERFACE;
1260 signal = dbus_message_new_signal(element->path,
1261 iface, "PropertyChanged");
1265 dbus_message_iter_init_append(signal, &entry);
1267 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &name);
1270 case DBUS_TYPE_STRING:
1271 sig = DBUS_TYPE_STRING_AS_STRING;
1273 case DBUS_TYPE_BYTE:
1274 sig = DBUS_TYPE_BYTE_AS_STRING;
1277 sig = DBUS_TYPE_VARIANT_AS_STRING;
1281 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1283 dbus_message_iter_append_basic(&value, type, data);
1284 dbus_message_iter_close_container(&entry, &value);
1286 g_dbus_send_message(conn, signal);
1289 int connman_element_set_static_property(struct connman_element *element,
1290 const char *name, int type, const void *value)
1294 DBG("element %p name %s", element, element->name);
1296 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
1299 __connman_element_lock(element);
1301 for (list = element->properties; list; list = list->next) {
1302 struct connman_property *property = list->data;
1304 if (g_str_equal(property->name, name) == FALSE)
1307 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1310 property->type = type;
1311 g_free(property->value);
1314 case DBUS_TYPE_STRING:
1315 property->value = g_strdup(*((const char **) value));
1317 case DBUS_TYPE_BYTE:
1318 property->value = g_try_malloc(1);
1319 if (property->value != NULL)
1320 memcpy(property->value, value, 1);
1325 __connman_element_unlock(element);
1327 emit_property_changed(connection, element, name, type, value);
1332 int connman_element_add_static_array_property(struct connman_element *element,
1333 const char *name, int type, const void *value, int len)
1335 struct connman_property *property;
1337 DBG("element %p name %s", element, element->name);
1339 if (type != DBUS_TYPE_BYTE)
1342 property = g_try_new0(struct connman_property, 1);
1343 if (property == NULL)
1346 property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
1347 property->id = CONNMAN_PROPERTY_ID_INVALID;
1348 property->name = g_strdup(name);
1349 property->type = DBUS_TYPE_ARRAY;
1350 property->subtype = type;
1352 DBG("name %s type %d value %p", name, type, value);
1355 case DBUS_TYPE_BYTE:
1356 property->value = g_try_malloc(len);
1357 if (property->value != NULL) {
1358 memcpy(property->value,
1359 *((const unsigned char **) value), len);
1360 property->size = len;
1365 __connman_element_lock(element);
1366 element->properties = g_slist_append(element->properties, property);
1367 __connman_element_unlock(element);
1372 static void *get_reference_value(struct connman_element *element,
1373 enum connman_property_id id)
1377 DBG("element %p name %s", element, element->name);
1379 for (list = element->properties; list; list = list->next) {
1380 struct connman_property *property = list->data;
1382 if (property->id != id)
1385 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1386 return property->value;
1389 if (element->parent == NULL)
1392 return get_reference_value(element->parent, id);
1395 static void set_reference_properties(struct connman_element *element)
1399 DBG("element %p name %s", element, element->name);
1401 for (list = element->properties; list; list = list->next) {
1402 struct connman_property *property = list->data;
1404 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1407 property->value = get_reference_value(element->parent,
1412 static struct connman_property *create_property(struct connman_element *element,
1413 enum connman_property_id id)
1415 struct connman_property *property;
1418 DBG("element %p name %s", element, element->name);
1420 __connman_element_lock(element);
1422 for (list = element->properties; list; list = list->next) {
1423 property = list->data;
1425 if (property->id == id)
1429 property = g_try_new0(struct connman_property, 1);
1430 if (property == NULL)
1433 property->flags = CONNMAN_PROPERTY_FLAG_REFERENCE;
1435 property->name = g_strdup(propid2name(id));
1436 property->type = propid2type(id);
1438 if (property->name == NULL) {
1444 element->properties = g_slist_append(element->properties, property);
1447 __connman_element_unlock(element);
1452 static void create_default_properties(struct connman_element *element)
1454 struct connman_property *property;
1457 DBG("element %p name %s", element, element->name);
1459 for (i = 0; propid_table[i].name; i++) {
1460 DBG("property %s", propid_table[i].name);
1462 property = create_property(element, propid_table[i].id);
1464 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
1466 if (propid_table[i].type != DBUS_TYPE_STRING)
1469 if (propid_table[i].value)
1470 property->value = g_strdup(propid_table[i].value);
1472 property->value = g_strdup("");
1476 static int define_properties_valist(struct connman_element *element,
1479 enum connman_property_id id;
1481 DBG("element %p name %s", element, element->name);
1483 id = va_arg(args, enum connman_property_id);
1485 while (id != CONNMAN_PROPERTY_ID_INVALID) {
1487 DBG("property %d", id);
1489 create_property(element, id);
1491 id = va_arg(args, enum connman_property_id);
1498 * connman_element_define_properties:
1499 * @element: an element
1500 * @varargs: list of property identifiers
1502 * Define the valid properties for an element.
1504 * Returns: %0 on success
1506 int connman_element_define_properties(struct connman_element *element, ...)
1511 DBG("element %p name %s", element, element->name);
1513 va_start(args, element);
1515 err = define_properties_valist(element, args);
1522 int connman_element_create_property(struct connman_element *element,
1523 const char *name, int type)
1528 int connman_element_set_property(struct connman_element *element,
1529 enum connman_property_id id, const void *value)
1532 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
1533 __connman_element_lock(element);
1534 g_free(element->ipv4.address);
1535 element->ipv4.address = g_strdup(*((const char **) value));
1536 __connman_element_unlock(element);
1538 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
1539 __connman_element_lock(element);
1540 g_free(element->ipv4.netmask);
1541 element->ipv4.netmask = g_strdup(*((const char **) value));
1542 __connman_element_unlock(element);
1544 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
1545 __connman_element_lock(element);
1546 g_free(element->ipv4.gateway);
1547 element->ipv4.gateway = g_strdup(*((const char **) value));
1548 __connman_element_unlock(element);
1550 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
1551 __connman_element_lock(element);
1552 g_free(element->ipv4.broadcast);
1553 element->ipv4.broadcast = g_strdup(*((const char **) value));
1554 __connman_element_unlock(element);
1556 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
1557 __connman_element_lock(element);
1558 g_free(element->ipv4.nameserver);
1559 element->ipv4.nameserver = g_strdup(*((const char **) value));
1560 __connman_element_unlock(element);
1562 case CONNMAN_PROPERTY_ID_WIFI_SECURITY:
1563 __connman_element_lock(element);
1564 g_free(element->wifi.security);
1565 element->wifi.security = g_strdup(*((const char **) value));
1566 __connman_element_unlock(element);
1568 case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE:
1569 __connman_element_lock(element);
1570 g_free(element->wifi.passphrase);
1571 element->wifi.passphrase = g_strdup(*((const char **) value));
1572 __connman_element_unlock(element);
1581 int connman_element_get_value(struct connman_element *element,
1582 enum connman_property_id id, void *value)
1584 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1588 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
1589 if (element->ipv4.address == NULL)
1590 return connman_element_get_value(element->parent,
1592 __connman_element_lock(element);
1593 *((char **) value) = element->ipv4.address;
1594 __connman_element_unlock(element);
1596 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
1597 if (element->ipv4.netmask == NULL)
1598 return connman_element_get_value(element->parent,
1600 __connman_element_lock(element);
1601 *((char **) value) = element->ipv4.netmask;
1602 __connman_element_unlock(element);
1604 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
1605 if (element->ipv4.gateway == NULL)
1606 return connman_element_get_value(element->parent,
1608 __connman_element_lock(element);
1609 *((char **) value) = element->ipv4.gateway;
1610 __connman_element_unlock(element);
1612 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
1613 if (element->ipv4.broadcast == NULL)
1614 return connman_element_get_value(element->parent,
1616 __connman_element_lock(element);
1617 *((char **) value) = element->ipv4.broadcast;
1618 __connman_element_unlock(element);
1620 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
1621 if (element->ipv4.nameserver == NULL)
1622 return connman_element_get_value(element->parent,
1624 __connman_element_lock(element);
1625 *((char **) value) = element->ipv4.nameserver;
1626 __connman_element_unlock(element);
1628 case CONNMAN_PROPERTY_ID_WIFI_SECURITY:
1629 if (element->wifi.security == NULL)
1630 return connman_element_get_value(element->parent,
1632 __connman_element_lock(element);
1633 *((char **) value) = element->wifi.security;
1634 __connman_element_unlock(element);
1636 case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE:
1637 if (element->wifi.passphrase == NULL)
1638 return connman_element_get_value(element->parent,
1640 __connman_element_lock(element);
1641 *((char **) value) = element->wifi.passphrase;
1642 __connman_element_unlock(element);
1651 gboolean connman_element_get_static_property(struct connman_element *element,
1652 const char *name, void *value)
1655 gboolean found = FALSE;
1657 DBG("element %p name %s", element, element->name);
1659 __connman_element_lock(element);
1661 for (list = element->properties; list; list = list->next) {
1662 struct connman_property *property = list->data;
1664 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1667 if (g_str_equal(property->name, name) == TRUE) {
1668 switch (property->type) {
1669 case DBUS_TYPE_STRING:
1670 *((char **) value) = property->value;
1678 __connman_element_unlock(element);
1683 gboolean connman_element_get_static_array_property(struct connman_element *element,
1684 const char *name, void *value, int *len)
1687 gboolean found = FALSE;
1689 DBG("element %p name %s", element, element->name);
1691 __connman_element_lock(element);
1693 for (list = element->properties; list; list = list->next) {
1694 struct connman_property *property = list->data;
1696 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1699 if (g_str_equal(property->name, name) == TRUE) {
1700 *((char **) value) = property->value;
1701 *len = property->size;
1707 __connman_element_unlock(element);
1712 gboolean connman_element_match_static_property(struct connman_element *element,
1713 const char *name, const void *value)
1716 gboolean result = FALSE;
1718 DBG("element %p name %s", element, element->name);
1720 __connman_element_lock(element);
1722 for (list = element->properties; list; list = list->next) {
1723 struct connman_property *property = list->data;
1725 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1728 if (g_str_equal(property->name, name) == FALSE)
1731 if (property->type == DBUS_TYPE_STRING)
1732 result = g_str_equal(property->value,
1733 *((const char **) value));
1739 __connman_element_unlock(element);
1744 static void append_devices(DBusMessageIter *entry)
1746 DBusMessageIter value, iter;
1747 const char *key = "Devices";
1749 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1751 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1752 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
1755 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
1756 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
1757 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
1758 dbus_message_iter_close_container(&value, &iter);
1760 dbus_message_iter_close_container(entry, &value);
1763 static void emit_devices_signal(DBusConnection *conn)
1765 DBusMessage *signal;
1766 DBusMessageIter entry;
1768 DBG("conn %p", conn);
1770 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1771 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1775 dbus_message_iter_init_append(signal, &entry);
1777 append_devices(&entry);
1779 g_dbus_send_message(conn, signal);
1782 static void emit_networks_signal(DBusConnection *conn,
1783 struct connman_element *device)
1785 DBusMessage *signal;
1786 DBusMessageIter entry;
1788 DBG("conn %p", conn);
1793 signal = dbus_message_new_signal(device->path,
1794 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1798 dbus_message_iter_init_append(signal, &entry);
1800 append_networks(device, &entry);
1802 g_dbus_send_message(conn, signal);
1805 static void append_connections(DBusMessageIter *entry)
1807 DBusMessageIter value, iter;
1808 const char *key = "Connections";
1810 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1812 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1813 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
1816 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
1817 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
1818 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION, &iter);
1819 dbus_message_iter_close_container(&value, &iter);
1821 dbus_message_iter_close_container(entry, &value);
1824 static void emit_connections_signal(DBusConnection *conn)
1826 DBusMessage *signal;
1827 DBusMessageIter entry;
1829 DBG("conn %p", conn);
1831 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1832 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1836 dbus_message_iter_init_append(signal, &entry);
1838 append_connections(&entry);
1840 g_dbus_send_message(conn, signal);
1843 static void append_state(DBusMessageIter *entry, const char *state)
1845 DBusMessageIter value;
1846 const char *key = "State";
1848 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1850 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1851 DBUS_TYPE_STRING_AS_STRING, &value);
1852 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
1853 dbus_message_iter_close_container(entry, &value);
1856 static void emit_state_change(DBusConnection *conn, const char *state)
1858 DBusMessage *signal;
1859 DBusMessageIter entry;
1861 DBG("conn %p", conn);
1863 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1864 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1868 dbus_message_iter_init_append(signal, &entry);
1870 append_state(&entry, state);
1872 g_dbus_send_message(conn, signal);
1875 static void set_signal_strength(struct connman_element *connection)
1877 struct connman_element *element = connection;
1879 while (element != NULL) {
1880 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1881 connection->strength = element->strength;
1885 element = element->parent;
1889 static void register_element(gpointer data, gpointer user_data)
1891 struct connman_element *element = data;
1892 const gchar *basepath;
1896 __connman_element_lock(element);
1898 if (element->parent) {
1899 node = g_node_find(element_root, G_PRE_ORDER,
1900 G_TRAVERSE_ALL, element->parent);
1901 basepath = element->parent->path;
1903 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
1904 element->subtype = element->parent->subtype;
1906 element->parent = element_root->data;
1908 node = element_root;
1912 element->path = g_strdup_printf("%s/%s", basepath, element->name);
1914 set_reference_properties(element);
1916 __connman_element_unlock(element);
1918 DBG("element %p path %s", element, element->path);
1920 __connman_element_load(element);
1922 g_node_append_data(node, element);
1924 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
1925 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
1926 if (g_dbus_register_interface(connection, element->path,
1927 CONNMAN_DEVICE_INTERFACE,
1928 device_methods, element_signals,
1929 NULL, element, NULL) == FALSE)
1930 connman_error("Failed to register %s device",
1933 emit_devices_signal(connection);
1936 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1937 if (g_dbus_register_interface(connection, element->path,
1938 CONNMAN_NETWORK_INTERFACE,
1939 network_methods, element_signals,
1940 NULL, element, NULL) == FALSE)
1941 connman_error("Failed to register %s network",
1944 emit_networks_signal(connection, element->parent);
1947 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1948 if (g_dbus_register_interface(connection, element->path,
1949 CONNMAN_CONNECTION_INTERFACE,
1950 connection_methods, element_signals,
1951 NULL, element, NULL) == FALSE)
1952 connman_error("Failed to register %s connection",
1955 set_signal_strength(element);
1956 emit_connections_signal(connection);
1957 emit_state_change(connection, "online");
1961 __connman_element_store(element);
1963 for (list = driver_list; list; list = list->next) {
1964 struct connman_driver *driver = list->data;
1966 if (match_driver(element, driver) == FALSE)
1969 DBG("driver %p name %s", driver, driver->name);
1971 if (driver->probe(element) == 0) {
1972 __connman_element_lock(element);
1973 element->driver = driver;
1974 __connman_element_unlock(element);
1976 enable_element(element);
1983 * connman_element_register:
1984 * @element: the element to register
1985 * @parent: the parent to register the element with
1987 * Register an element with the core. It will be register under the given
1988 * parent of if %NULL is provided under the root element.
1990 * Returns: %0 on success
1992 int connman_element_register(struct connman_element *element,
1993 struct connman_element *parent)
1995 DBG("element %p name %s parent %p", element, element->name, parent);
1997 if (element->devname == NULL)
1998 element->devname = g_strdup(element->name);
2000 if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
2001 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
2002 if (g_pattern_match_simple(device_filter,
2003 element->devname) == FALSE) {
2004 DBG("ignoring %s [%s] device", element->name,
2010 if (connman_element_ref(element) == NULL)
2013 __connman_element_lock(element);
2015 if (element->name == NULL) {
2016 element->name = g_strdup(type2string(element->type));
2017 if (element->name == NULL) {
2018 __connman_element_unlock(element);
2023 element->parent = parent;
2025 __connman_element_unlock(element);
2027 register_element(element, NULL);
2032 static gboolean remove_element(GNode *node, gpointer user_data)
2034 struct connman_element *element = node->data;
2035 struct connman_element *root = user_data;
2037 DBG("element %p name %s", element, element->name);
2039 if (element == root)
2042 if (element->driver) {
2043 disable_element(element);
2045 if (element->driver->remove)
2046 element->driver->remove(element);
2048 __connman_element_lock(element);
2049 element->driver = NULL;
2050 __connman_element_unlock(element);
2054 g_node_unlink(node);
2055 g_node_destroy(node);
2058 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
2059 if (__connman_element_count(NULL,
2060 CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
2061 emit_state_change(connection, "offline");
2062 emit_connections_signal(connection);
2064 g_dbus_unregister_interface(connection, element->path,
2065 CONNMAN_CONNECTION_INTERFACE);
2068 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
2069 emit_networks_signal(connection, element->parent);
2071 g_dbus_unregister_interface(connection, element->path,
2072 CONNMAN_NETWORK_INTERFACE);
2075 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
2076 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
2077 emit_devices_signal(connection);
2079 g_dbus_unregister_interface(connection, element->path,
2080 CONNMAN_DEVICE_INTERFACE);
2083 connman_element_unref(element);
2088 void connman_element_unregister(struct connman_element *element)
2092 DBG("element %p name %s", element, element->name);
2094 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
2097 g_node_traverse(node, G_POST_ORDER,
2098 G_TRAVERSE_ALL, -1, remove_element, NULL);
2101 void connman_element_unregister_children(struct connman_element *element)
2105 DBG("element %p name %s", element, element->name);
2107 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
2110 g_node_traverse(node, G_POST_ORDER,
2111 G_TRAVERSE_ALL, -1, remove_element, element);
2114 static gboolean update_element(GNode *node, gpointer user_data)
2116 struct connman_element *element = node->data;
2117 struct connman_element *root = user_data;
2119 DBG("element %p name %s", element, element->name);
2121 if (element->driver && element->driver->update)
2122 element->driver->update(element);
2124 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION &&
2125 root->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
2126 if (element->strength != root->strength) {
2127 element->strength = root->strength;
2128 emit_property_changed(connection, element, "Strength",
2129 DBUS_TYPE_BYTE, &element->strength);
2136 void connman_element_update(struct connman_element *element)
2140 DBG("element %p name %s", element, element->name);
2142 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
2145 g_node_traverse(node, G_PRE_ORDER,
2146 G_TRAVERSE_ALL, -1, update_element, element);
2149 int connman_element_set_enabled(struct connman_element *element,
2152 if (element->enabled == enabled)
2155 element->enabled = enabled;
2157 emit_enabled_signal(connection, element);
2162 int connman_element_set_scanning(struct connman_element *element,
2165 if (element->scanning == scanning)
2168 element->scanning = scanning;
2170 emit_scanning_signal(connection, element);
2175 int __connman_element_init(DBusConnection *conn, const char *device)
2177 struct connman_element *element;
2179 DBG("conn %p", conn);
2181 connection = dbus_connection_ref(conn);
2182 if (connection == NULL)
2185 device_filter = g_strdup(device);
2187 element = connman_element_create("root");
2189 element->path = g_strdup("/");
2190 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
2192 create_default_properties(element);
2194 element_root = g_node_new(element);
2196 __connman_device_init();
2201 static gboolean free_driver(GNode *node, gpointer data)
2203 struct connman_element *element = node->data;
2205 DBG("element %p name %s", element, element->name);
2207 if (element->driver) {
2208 disable_element(element);
2210 if (element->driver->remove)
2211 element->driver->remove(element);
2213 __connman_element_lock(element);
2214 element->driver = NULL;
2215 __connman_element_unlock(element);
2221 static gboolean free_node(GNode *node, gpointer data)
2223 struct connman_element *element = node->data;
2225 DBG("element %p name %s", element, element->name);
2227 if (g_node_depth(node) > 1)
2228 connman_element_unregister(element);
2233 void __connman_element_cleanup(void)
2237 __connman_device_cleanup();
2239 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2242 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2245 g_node_destroy(element_root);
2246 element_root = NULL;
2248 g_free(device_filter);
2250 dbus_connection_unref(connection);