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);
700 connman_dbus_dict_append_variant(&dict, "Device",
701 DBUS_TYPE_OBJECT_PATH, &element->parent->path);
703 str = __connman_element_policy2string(element->policy);
705 connman_dbus_dict_append_variant(&dict, "Policy",
706 DBUS_TYPE_STRING, &str);
708 connman_dbus_dict_append_variant(&dict, "Available",
709 DBUS_TYPE_BOOLEAN, &element->available);
711 connman_dbus_dict_append_variant(&dict, "Connected",
712 DBUS_TYPE_BOOLEAN, &element->enabled);
714 connman_dbus_dict_append_variant(&dict, "Remember",
715 DBUS_TYPE_BOOLEAN, &element->remember);
717 add_common_properties(element, &dict);
719 dbus_message_iter_close_container(&array, &dict);
724 static DBusMessage *network_set_property(DBusConnection *conn,
725 DBusMessage *msg, void *data)
727 struct connman_element *element = data;
728 DBusMessageIter iter;
729 DBusMessageIter value;
732 DBG("conn %p", conn);
734 if (dbus_message_iter_init(msg, &iter) == FALSE)
735 return __connman_error_invalid_arguments(msg);
737 dbus_message_iter_get_basic(&iter, &name);
738 dbus_message_iter_next(&iter);
739 dbus_message_iter_recurse(&iter, &value);
741 if (__connman_security_check_privileges(msg) < 0)
742 return __connman_error_permission_denied(msg);
744 if (g_str_equal(name, "Remember") == TRUE) {
745 dbus_message_iter_get_basic(&value, &element->remember);
746 } else if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
749 dbus_message_iter_get_basic(&value, &str);
750 g_free(element->wifi.passphrase);
751 element->wifi.passphrase = g_strdup(str);
753 set_common_property(element, name, &value);
755 __connman_element_store(element);
757 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
760 static DBusMessage *connection_get_properties(DBusConnection *conn,
761 DBusMessage *msg, void *data)
763 struct connman_element *element = data;
765 DBusMessageIter array, dict;
768 DBG("conn %p", conn);
770 reply = dbus_message_new_method_return(msg);
774 dbus_message_iter_init_append(reply, &array);
776 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
777 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
778 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
779 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
781 str = subtype2string(element->subtype);
783 connman_dbus_dict_append_variant(&dict, "Type",
784 DBUS_TYPE_STRING, &str);
786 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI ||
787 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIMAX)
788 connman_dbus_dict_append_variant(&dict, "Strength",
789 DBUS_TYPE_BYTE, &element->strength);
791 connman_dbus_dict_append_variant(&dict, "Default",
792 DBUS_TYPE_BOOLEAN, &element->enabled);
794 add_common_properties(element, &dict);
796 dbus_message_iter_close_container(&array, &dict);
801 static DBusMessage *connection_set_property(DBusConnection *conn,
802 DBusMessage *msg, void *data)
804 struct connman_element *element = data;
805 DBusMessageIter iter, value;
808 DBG("conn %p", conn);
810 if (dbus_message_iter_init(msg, &iter) == FALSE)
811 return __connman_error_invalid_arguments(msg);
813 dbus_message_iter_get_basic(&iter, &name);
814 dbus_message_iter_next(&iter);
815 dbus_message_iter_recurse(&iter, &value);
817 if (__connman_security_check_privileges(msg) < 0)
818 return __connman_error_permission_denied(msg);
820 if (g_str_equal(name, "Default") == TRUE) {
823 dbus_message_iter_get_basic(&value, &enabled);
826 return do_enable(conn, msg, element);
828 return do_disable(conn, msg, element);
831 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
834 static GDBusMethodTable device_methods[] = {
835 { "GetProperties", "", "a{sv}", device_get_properties },
836 { "SetProperty", "sv", "", device_set_property },
837 { "CreateNetwork", "a{sv}", "o", device_create_network },
838 { "RemoveNetwork", "o", "", device_remove_network },
839 { "ProposeScan", "", "", do_update },
843 static GDBusMethodTable network_methods[] = {
844 { "GetProperties", "", "a{sv}", network_get_properties },
845 { "SetProperty", "sv", "", network_set_property },
846 { "Connect", "", "", do_enable },
847 { "Disconnect", "", "", do_disable },
851 static GDBusMethodTable connection_methods[] = {
852 { "GetProperties", "", "a{sv}", connection_get_properties },
853 { "SetProperty", "sv", "", connection_set_property },
857 static GDBusSignalTable element_signals[] = {
858 { "PropertyChanged", "sv" },
862 struct append_filter {
863 enum connman_element_type type;
864 DBusMessageIter *iter;
867 static gboolean append_path(GNode *node, gpointer user_data)
869 struct connman_element *element = node->data;
870 struct append_filter *filter = user_data;
872 DBG("element %p name %s", element, element->name);
874 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
877 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
878 filter->type != element->type)
881 if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
882 element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
885 dbus_message_iter_append_basic(filter->iter,
886 DBUS_TYPE_OBJECT_PATH, &element->path);
891 void __connman_element_list(struct connman_element *element,
892 enum connman_element_type type,
893 DBusMessageIter *iter)
895 struct append_filter filter = { type, iter };
900 if (element != NULL) {
901 node = g_node_find(element_root, G_PRE_ORDER,
902 G_TRAVERSE_ALL, element);
908 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
909 append_path, &filter);
913 enum connman_element_type type;
917 static gboolean count_element(GNode *node, gpointer user_data)
919 struct connman_element *element = node->data;
920 struct count_data *data = user_data;
922 DBG("element %p name %s", element, element->name);
924 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
927 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
928 data->type != element->type)
936 int __connman_element_count(struct connman_element *element,
937 enum connman_element_type type)
939 struct count_data data = { type, 0 };
944 if (element != NULL) {
945 node = g_node_find(element_root, G_PRE_ORDER,
946 G_TRAVERSE_ALL, element);
952 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
953 count_element, &data);
958 static gint compare_priority(gconstpointer a, gconstpointer b)
960 const struct connman_driver *driver1 = a;
961 const struct connman_driver *driver2 = b;
963 return driver2->priority - driver1->priority;
966 static gboolean match_driver(struct connman_element *element,
967 struct connman_driver *driver)
969 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
972 if (element->type != driver->type &&
973 driver->type != CONNMAN_ELEMENT_TYPE_UNKNOWN)
976 if (element->subtype == driver->subtype ||
977 driver->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
983 static void enable_element(struct connman_element *element)
985 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
988 if (element->policy != CONNMAN_ELEMENT_POLICY_AUTO)
991 if (element->driver && element->driver->enable) {
992 if (element->driver->enable(element) == 0) {
993 element->enabled = TRUE;
994 emit_enabled_signal(connection, element);
999 static gboolean probe_driver(GNode *node, gpointer data)
1001 struct connman_element *element = node->data;
1002 struct connman_driver *driver = data;
1004 DBG("element %p name %s", element, element->name);
1006 if (!element->driver && match_driver(element, driver) == TRUE) {
1007 if (driver->probe(element) < 0)
1010 __connman_element_lock(element);
1011 element->driver = driver;
1012 __connman_element_unlock(element);
1014 enable_element(element);
1020 void __connman_driver_rescan(struct connman_driver *driver)
1022 DBG("driver %p name %s", driver, driver->name);
1027 if (element_root != NULL)
1028 g_node_traverse(element_root, G_PRE_ORDER,
1029 G_TRAVERSE_ALL, -1, probe_driver, driver);
1033 * connman_driver_register:
1034 * @driver: driver definition
1036 * Register a new driver
1038 * Returns: %0 on success
1040 int connman_driver_register(struct connman_driver *driver)
1042 DBG("driver %p name %s", driver, driver->name);
1044 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
1050 driver_list = g_slist_insert_sorted(driver_list, driver,
1053 if (element_root != NULL)
1054 g_node_traverse(element_root, G_PRE_ORDER,
1055 G_TRAVERSE_ALL, -1, probe_driver, driver);
1060 static void disable_element(struct connman_element *element)
1062 if (element->policy != CONNMAN_ELEMENT_POLICY_AUTO)
1065 if (element->enabled == FALSE)
1068 if (element->driver && element->driver->disable) {
1069 if (element->driver->disable(element) == 0) {
1070 element->enabled = FALSE;
1071 emit_enabled_signal(connection, element);
1076 static gboolean remove_driver(GNode *node, gpointer data)
1078 struct connman_element *element = node->data;
1079 struct connman_driver *driver = data;
1081 DBG("element %p name %s", element, element->name);
1083 if (element->driver == driver) {
1084 disable_element(element);
1087 driver->remove(element);
1089 __connman_element_lock(element);
1090 element->driver = NULL;
1091 __connman_element_unlock(element);
1098 * connman_driver_unregister:
1099 * @driver: driver definition
1101 * Remove a previously registered driver
1103 void connman_driver_unregister(struct connman_driver *driver)
1105 DBG("driver %p name %s", driver, driver->name);
1107 driver_list = g_slist_remove(driver_list, driver);
1109 if (element_root != NULL)
1110 g_node_traverse(element_root, G_POST_ORDER,
1111 G_TRAVERSE_ALL, -1, remove_driver, driver);
1115 * connman_element_create:
1116 * @name: element name
1118 * Allocate a new element and assign the given #name to it. If the name
1119 * is #NULL, it will be later on created based on the element type.
1121 * Returns: a newly-allocated #connman_element structure
1123 struct connman_element *connman_element_create(const char *name)
1125 struct connman_element *element;
1127 element = g_try_new0(struct connman_element, 1);
1128 if (element == NULL)
1131 DBG("element %p", element);
1133 element->refcount = 1;
1135 element->name = g_strdup(name);
1136 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
1137 element->subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
1138 element->state = CONNMAN_ELEMENT_STATE_CLOSED;
1139 element->policy = CONNMAN_ELEMENT_POLICY_AUTO;
1140 element->index = -1;
1141 element->enabled = FALSE;
1146 struct connman_element *connman_element_ref(struct connman_element *element)
1148 DBG("element %p name %s refcount %d", element, element->name,
1149 g_atomic_int_get(&element->refcount) + 1);
1151 g_atomic_int_inc(&element->refcount);
1156 static void free_properties(struct connman_element *element)
1160 DBG("element %p name %s", element, element->name);
1162 __connman_element_lock(element);
1164 for (list = element->properties; list; list = list->next) {
1165 struct connman_property *property = list->data;
1167 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1168 g_free(property->value);
1170 g_free(property->name);
1174 g_slist_free(element->properties);
1176 element->properties = NULL;
1178 __connman_element_unlock(element);
1181 void connman_element_unref(struct connman_element *element)
1183 DBG("element %p name %s refcount %d", element, element->name,
1184 g_atomic_int_get(&element->refcount) - 1);
1186 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
1187 free_properties(element);
1188 g_free(element->ipv4.address);
1189 g_free(element->ipv4.netmask);
1190 g_free(element->ipv4.gateway);
1191 g_free(element->ipv4.network);
1192 g_free(element->ipv4.broadcast);
1193 g_free(element->ipv4.nameserver);
1194 g_free(element->devname);
1195 g_free(element->path);
1196 g_free(element->name);
1201 int connman_element_add_static_property(struct connman_element *element,
1202 const char *name, int type, const void *value)
1204 struct connman_property *property;
1206 DBG("element %p name %s", element, element->name);
1208 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
1211 property = g_try_new0(struct connman_property, 1);
1212 if (property == NULL)
1215 property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
1216 property->id = CONNMAN_PROPERTY_ID_INVALID;
1217 property->name = g_strdup(name);
1218 property->type = type;
1220 DBG("name %s type %d value %p", name, type, value);
1223 case DBUS_TYPE_STRING:
1224 property->value = g_strdup(*((const char **) value));
1226 case DBUS_TYPE_BYTE:
1227 property->value = g_try_malloc(1);
1228 if (property->value != NULL)
1229 memcpy(property->value, value, 1);
1233 __connman_element_lock(element);
1234 element->properties = g_slist_append(element->properties, property);
1235 __connman_element_unlock(element);
1240 static void emit_property_changed(DBusConnection *conn,
1241 struct connman_element *element,
1242 const char *name, int type, const void *data)
1244 DBusMessage *signal;
1245 DBusMessageIter entry, value;
1246 const char *iface, *sig;
1248 DBG("conn %p", conn);
1250 switch (element->type) {
1251 case CONNMAN_ELEMENT_TYPE_DEVICE:
1252 iface = CONNMAN_DEVICE_INTERFACE;
1254 case CONNMAN_ELEMENT_TYPE_NETWORK:
1255 iface = CONNMAN_NETWORK_INTERFACE;
1257 case CONNMAN_ELEMENT_TYPE_CONNECTION:
1258 iface = CONNMAN_CONNECTION_INTERFACE;
1264 signal = dbus_message_new_signal(element->path,
1265 iface, "PropertyChanged");
1269 dbus_message_iter_init_append(signal, &entry);
1271 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &name);
1274 case DBUS_TYPE_STRING:
1275 sig = DBUS_TYPE_STRING_AS_STRING;
1277 case DBUS_TYPE_BYTE:
1278 sig = DBUS_TYPE_BYTE_AS_STRING;
1281 sig = DBUS_TYPE_VARIANT_AS_STRING;
1285 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1287 dbus_message_iter_append_basic(&value, type, data);
1288 dbus_message_iter_close_container(&entry, &value);
1290 g_dbus_send_message(conn, signal);
1293 int connman_element_set_static_property(struct connman_element *element,
1294 const char *name, int type, const void *value)
1298 DBG("element %p name %s", element, element->name);
1300 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
1303 __connman_element_lock(element);
1305 for (list = element->properties; list; list = list->next) {
1306 struct connman_property *property = list->data;
1308 if (g_str_equal(property->name, name) == FALSE)
1311 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1314 property->type = type;
1315 g_free(property->value);
1318 case DBUS_TYPE_STRING:
1319 property->value = g_strdup(*((const char **) value));
1321 case DBUS_TYPE_BYTE:
1322 property->value = g_try_malloc(1);
1323 if (property->value != NULL)
1324 memcpy(property->value, value, 1);
1329 __connman_element_unlock(element);
1331 emit_property_changed(connection, element, name, type, value);
1336 int connman_element_add_static_array_property(struct connman_element *element,
1337 const char *name, int type, const void *value, int len)
1339 struct connman_property *property;
1341 DBG("element %p name %s", element, element->name);
1343 if (type != DBUS_TYPE_BYTE)
1346 property = g_try_new0(struct connman_property, 1);
1347 if (property == NULL)
1350 property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
1351 property->id = CONNMAN_PROPERTY_ID_INVALID;
1352 property->name = g_strdup(name);
1353 property->type = DBUS_TYPE_ARRAY;
1354 property->subtype = type;
1356 DBG("name %s type %d value %p", name, type, value);
1359 case DBUS_TYPE_BYTE:
1360 property->value = g_try_malloc(len);
1361 if (property->value != NULL) {
1362 memcpy(property->value,
1363 *((const unsigned char **) value), len);
1364 property->size = len;
1369 __connman_element_lock(element);
1370 element->properties = g_slist_append(element->properties, property);
1371 __connman_element_unlock(element);
1376 static void *get_reference_value(struct connman_element *element,
1377 enum connman_property_id id)
1381 DBG("element %p name %s", element, element->name);
1383 for (list = element->properties; list; list = list->next) {
1384 struct connman_property *property = list->data;
1386 if (property->id != id)
1389 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1390 return property->value;
1393 if (element->parent == NULL)
1396 return get_reference_value(element->parent, id);
1399 static void set_reference_properties(struct connman_element *element)
1403 DBG("element %p name %s", element, element->name);
1405 for (list = element->properties; list; list = list->next) {
1406 struct connman_property *property = list->data;
1408 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
1411 property->value = get_reference_value(element->parent,
1416 static struct connman_property *create_property(struct connman_element *element,
1417 enum connman_property_id id)
1419 struct connman_property *property;
1422 DBG("element %p name %s", element, element->name);
1424 __connman_element_lock(element);
1426 for (list = element->properties; list; list = list->next) {
1427 property = list->data;
1429 if (property->id == id)
1433 property = g_try_new0(struct connman_property, 1);
1434 if (property == NULL)
1437 property->flags = CONNMAN_PROPERTY_FLAG_REFERENCE;
1439 property->name = g_strdup(propid2name(id));
1440 property->type = propid2type(id);
1442 if (property->name == NULL) {
1448 element->properties = g_slist_append(element->properties, property);
1451 __connman_element_unlock(element);
1456 static void create_default_properties(struct connman_element *element)
1458 struct connman_property *property;
1461 DBG("element %p name %s", element, element->name);
1463 for (i = 0; propid_table[i].name; i++) {
1464 DBG("property %s", propid_table[i].name);
1466 property = create_property(element, propid_table[i].id);
1468 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
1470 if (propid_table[i].type != DBUS_TYPE_STRING)
1473 if (propid_table[i].value)
1474 property->value = g_strdup(propid_table[i].value);
1476 property->value = g_strdup("");
1480 static int define_properties_valist(struct connman_element *element,
1483 enum connman_property_id id;
1485 DBG("element %p name %s", element, element->name);
1487 id = va_arg(args, enum connman_property_id);
1489 while (id != CONNMAN_PROPERTY_ID_INVALID) {
1491 DBG("property %d", id);
1493 create_property(element, id);
1495 id = va_arg(args, enum connman_property_id);
1502 * connman_element_define_properties:
1503 * @element: an element
1504 * @varargs: list of property identifiers
1506 * Define the valid properties for an element.
1508 * Returns: %0 on success
1510 int connman_element_define_properties(struct connman_element *element, ...)
1515 DBG("element %p name %s", element, element->name);
1517 va_start(args, element);
1519 err = define_properties_valist(element, args);
1526 int connman_element_create_property(struct connman_element *element,
1527 const char *name, int type)
1532 int connman_element_set_property(struct connman_element *element,
1533 enum connman_property_id id, const void *value)
1536 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
1537 __connman_element_lock(element);
1538 g_free(element->ipv4.address);
1539 element->ipv4.address = g_strdup(*((const char **) value));
1540 __connman_element_unlock(element);
1542 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
1543 __connman_element_lock(element);
1544 g_free(element->ipv4.netmask);
1545 element->ipv4.netmask = g_strdup(*((const char **) value));
1546 __connman_element_unlock(element);
1548 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
1549 __connman_element_lock(element);
1550 g_free(element->ipv4.gateway);
1551 element->ipv4.gateway = g_strdup(*((const char **) value));
1552 __connman_element_unlock(element);
1554 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
1555 __connman_element_lock(element);
1556 g_free(element->ipv4.broadcast);
1557 element->ipv4.broadcast = g_strdup(*((const char **) value));
1558 __connman_element_unlock(element);
1560 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
1561 __connman_element_lock(element);
1562 g_free(element->ipv4.nameserver);
1563 element->ipv4.nameserver = g_strdup(*((const char **) value));
1564 __connman_element_unlock(element);
1566 case CONNMAN_PROPERTY_ID_WIFI_SECURITY:
1567 __connman_element_lock(element);
1568 g_free(element->wifi.security);
1569 element->wifi.security = g_strdup(*((const char **) value));
1570 __connman_element_unlock(element);
1572 case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE:
1573 __connman_element_lock(element);
1574 g_free(element->wifi.passphrase);
1575 element->wifi.passphrase = g_strdup(*((const char **) value));
1576 __connman_element_unlock(element);
1585 int connman_element_get_value(struct connman_element *element,
1586 enum connman_property_id id, void *value)
1588 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1592 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
1593 if (element->ipv4.address == NULL)
1594 return connman_element_get_value(element->parent,
1596 __connman_element_lock(element);
1597 *((char **) value) = element->ipv4.address;
1598 __connman_element_unlock(element);
1600 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
1601 if (element->ipv4.netmask == NULL)
1602 return connman_element_get_value(element->parent,
1604 __connman_element_lock(element);
1605 *((char **) value) = element->ipv4.netmask;
1606 __connman_element_unlock(element);
1608 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
1609 if (element->ipv4.gateway == NULL)
1610 return connman_element_get_value(element->parent,
1612 __connman_element_lock(element);
1613 *((char **) value) = element->ipv4.gateway;
1614 __connman_element_unlock(element);
1616 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
1617 if (element->ipv4.broadcast == NULL)
1618 return connman_element_get_value(element->parent,
1620 __connman_element_lock(element);
1621 *((char **) value) = element->ipv4.broadcast;
1622 __connman_element_unlock(element);
1624 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
1625 if (element->ipv4.nameserver == NULL)
1626 return connman_element_get_value(element->parent,
1628 __connman_element_lock(element);
1629 *((char **) value) = element->ipv4.nameserver;
1630 __connman_element_unlock(element);
1632 case CONNMAN_PROPERTY_ID_WIFI_SECURITY:
1633 if (element->wifi.security == NULL)
1634 return connman_element_get_value(element->parent,
1636 __connman_element_lock(element);
1637 *((char **) value) = element->wifi.security;
1638 __connman_element_unlock(element);
1640 case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE:
1641 if (element->wifi.passphrase == NULL)
1642 return connman_element_get_value(element->parent,
1644 __connman_element_lock(element);
1645 *((char **) value) = element->wifi.passphrase;
1646 __connman_element_unlock(element);
1655 gboolean connman_element_get_static_property(struct connman_element *element,
1656 const char *name, void *value)
1659 gboolean found = FALSE;
1661 DBG("element %p name %s", element, element->name);
1663 __connman_element_lock(element);
1665 for (list = element->properties; list; list = list->next) {
1666 struct connman_property *property = list->data;
1668 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1671 if (g_str_equal(property->name, name) == TRUE) {
1672 switch (property->type) {
1673 case DBUS_TYPE_STRING:
1674 *((char **) value) = property->value;
1682 __connman_element_unlock(element);
1687 gboolean connman_element_get_static_array_property(struct connman_element *element,
1688 const char *name, void *value, int *len)
1691 gboolean found = FALSE;
1693 DBG("element %p name %s", element, element->name);
1695 __connman_element_lock(element);
1697 for (list = element->properties; list; list = list->next) {
1698 struct connman_property *property = list->data;
1700 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1703 if (g_str_equal(property->name, name) == TRUE) {
1704 *((char **) value) = property->value;
1705 *len = property->size;
1711 __connman_element_unlock(element);
1716 gboolean connman_element_match_static_property(struct connman_element *element,
1717 const char *name, const void *value)
1720 gboolean result = FALSE;
1722 DBG("element %p name %s", element, element->name);
1724 __connman_element_lock(element);
1726 for (list = element->properties; list; list = list->next) {
1727 struct connman_property *property = list->data;
1729 if (!(property->flags & CONNMAN_PROPERTY_FLAG_STATIC))
1732 if (g_str_equal(property->name, name) == FALSE)
1735 if (property->type == DBUS_TYPE_STRING)
1736 result = g_str_equal(property->value,
1737 *((const char **) value));
1743 __connman_element_unlock(element);
1748 static void append_devices(DBusMessageIter *entry)
1750 DBusMessageIter value, iter;
1751 const char *key = "Devices";
1753 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1755 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1756 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
1759 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
1760 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
1761 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
1762 dbus_message_iter_close_container(&value, &iter);
1764 dbus_message_iter_close_container(entry, &value);
1767 static void emit_devices_signal(DBusConnection *conn)
1769 DBusMessage *signal;
1770 DBusMessageIter entry;
1772 DBG("conn %p", conn);
1774 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1775 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1779 dbus_message_iter_init_append(signal, &entry);
1781 append_devices(&entry);
1783 g_dbus_send_message(conn, signal);
1786 static void emit_networks_signal(DBusConnection *conn,
1787 struct connman_element *device)
1789 DBusMessage *signal;
1790 DBusMessageIter entry;
1792 DBG("conn %p", conn);
1797 signal = dbus_message_new_signal(device->path,
1798 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1802 dbus_message_iter_init_append(signal, &entry);
1804 append_networks(device, &entry);
1806 g_dbus_send_message(conn, signal);
1809 static void append_connections(DBusMessageIter *entry)
1811 DBusMessageIter value, iter;
1812 const char *key = "Connections";
1814 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1816 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1817 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
1820 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
1821 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
1822 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION, &iter);
1823 dbus_message_iter_close_container(&value, &iter);
1825 dbus_message_iter_close_container(entry, &value);
1828 static void emit_connections_signal(DBusConnection *conn)
1830 DBusMessage *signal;
1831 DBusMessageIter entry;
1833 DBG("conn %p", conn);
1835 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1836 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1840 dbus_message_iter_init_append(signal, &entry);
1842 append_connections(&entry);
1844 g_dbus_send_message(conn, signal);
1847 static void append_state(DBusMessageIter *entry, const char *state)
1849 DBusMessageIter value;
1850 const char *key = "State";
1852 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1854 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1855 DBUS_TYPE_STRING_AS_STRING, &value);
1856 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
1857 dbus_message_iter_close_container(entry, &value);
1860 static void emit_state_change(DBusConnection *conn, const char *state)
1862 DBusMessage *signal;
1863 DBusMessageIter entry;
1865 DBG("conn %p", conn);
1867 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1868 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1872 dbus_message_iter_init_append(signal, &entry);
1874 append_state(&entry, state);
1876 g_dbus_send_message(conn, signal);
1879 static void set_signal_strength(struct connman_element *connection)
1881 struct connman_element *element = connection;
1883 while (element != NULL) {
1884 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1885 connection->strength = element->strength;
1889 element = element->parent;
1893 static void register_element(gpointer data, gpointer user_data)
1895 struct connman_element *element = data;
1896 const gchar *basepath;
1900 __connman_element_lock(element);
1902 if (element->parent) {
1903 node = g_node_find(element_root, G_PRE_ORDER,
1904 G_TRAVERSE_ALL, element->parent);
1905 basepath = element->parent->path;
1907 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
1908 element->subtype = element->parent->subtype;
1910 element->parent = element_root->data;
1912 node = element_root;
1916 element->path = g_strdup_printf("%s/%s", basepath, element->name);
1918 set_reference_properties(element);
1920 __connman_element_unlock(element);
1922 DBG("element %p path %s", element, element->path);
1924 __connman_element_load(element);
1926 g_node_append_data(node, element);
1928 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
1929 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
1930 if (g_dbus_register_interface(connection, element->path,
1931 CONNMAN_DEVICE_INTERFACE,
1932 device_methods, element_signals,
1933 NULL, element, NULL) == FALSE)
1934 connman_error("Failed to register %s device",
1937 emit_devices_signal(connection);
1940 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
1941 if (g_dbus_register_interface(connection, element->path,
1942 CONNMAN_NETWORK_INTERFACE,
1943 network_methods, element_signals,
1944 NULL, element, NULL) == FALSE)
1945 connman_error("Failed to register %s network",
1948 emit_networks_signal(connection, element->parent);
1951 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1952 if (g_dbus_register_interface(connection, element->path,
1953 CONNMAN_CONNECTION_INTERFACE,
1954 connection_methods, element_signals,
1955 NULL, element, NULL) == FALSE)
1956 connman_error("Failed to register %s connection",
1959 set_signal_strength(element);
1960 emit_connections_signal(connection);
1961 emit_state_change(connection, "online");
1965 __connman_element_store(element);
1967 for (list = driver_list; list; list = list->next) {
1968 struct connman_driver *driver = list->data;
1970 if (match_driver(element, driver) == FALSE)
1973 DBG("driver %p name %s", driver, driver->name);
1975 if (driver->probe(element) == 0) {
1976 __connman_element_lock(element);
1977 element->driver = driver;
1978 __connman_element_unlock(element);
1980 enable_element(element);
1987 * connman_element_register:
1988 * @element: the element to register
1989 * @parent: the parent to register the element with
1991 * Register an element with the core. It will be register under the given
1992 * parent of if %NULL is provided under the root element.
1994 * Returns: %0 on success
1996 int connman_element_register(struct connman_element *element,
1997 struct connman_element *parent)
1999 DBG("element %p name %s parent %p", element, element->name, parent);
2001 if (element->devname == NULL)
2002 element->devname = g_strdup(element->name);
2004 if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
2005 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
2006 if (g_pattern_match_simple(device_filter,
2007 element->devname) == FALSE) {
2008 DBG("ignoring %s [%s] device", element->name,
2014 if (connman_element_ref(element) == NULL)
2017 __connman_element_lock(element);
2019 if (element->name == NULL) {
2020 element->name = g_strdup(type2string(element->type));
2021 if (element->name == NULL) {
2022 __connman_element_unlock(element);
2027 element->parent = parent;
2029 __connman_element_unlock(element);
2031 register_element(element, NULL);
2036 static gboolean remove_element(GNode *node, gpointer user_data)
2038 struct connman_element *element = node->data;
2039 struct connman_element *root = user_data;
2041 DBG("element %p name %s", element, element->name);
2043 if (element == root)
2046 if (element->driver) {
2047 disable_element(element);
2049 if (element->driver->remove)
2050 element->driver->remove(element);
2052 __connman_element_lock(element);
2053 element->driver = NULL;
2054 __connman_element_unlock(element);
2058 g_node_unlink(node);
2059 g_node_destroy(node);
2062 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
2063 if (__connman_element_count(NULL,
2064 CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
2065 emit_state_change(connection, "offline");
2066 emit_connections_signal(connection);
2068 g_dbus_unregister_interface(connection, element->path,
2069 CONNMAN_CONNECTION_INTERFACE);
2072 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
2073 emit_networks_signal(connection, element->parent);
2075 g_dbus_unregister_interface(connection, element->path,
2076 CONNMAN_NETWORK_INTERFACE);
2079 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
2080 element->subtype != CONNMAN_ELEMENT_SUBTYPE_NETWORK) {
2081 emit_devices_signal(connection);
2083 g_dbus_unregister_interface(connection, element->path,
2084 CONNMAN_DEVICE_INTERFACE);
2087 connman_element_unref(element);
2092 void connman_element_unregister(struct connman_element *element)
2096 DBG("element %p name %s", element, element->name);
2098 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
2101 g_node_traverse(node, G_POST_ORDER,
2102 G_TRAVERSE_ALL, -1, remove_element, NULL);
2105 void connman_element_unregister_children(struct connman_element *element)
2109 DBG("element %p name %s", element, element->name);
2111 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
2114 g_node_traverse(node, G_POST_ORDER,
2115 G_TRAVERSE_ALL, -1, remove_element, element);
2118 static gboolean update_element(GNode *node, gpointer user_data)
2120 struct connman_element *element = node->data;
2121 struct connman_element *root = user_data;
2123 DBG("element %p name %s", element, element->name);
2125 if (element->driver && element->driver->update)
2126 element->driver->update(element);
2128 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION &&
2129 root->type == CONNMAN_ELEMENT_TYPE_NETWORK) {
2130 if (element->strength != root->strength) {
2131 element->strength = root->strength;
2132 emit_property_changed(connection, element, "Strength",
2133 DBUS_TYPE_BYTE, &element->strength);
2140 void connman_element_update(struct connman_element *element)
2144 DBG("element %p name %s", element, element->name);
2146 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
2149 g_node_traverse(node, G_PRE_ORDER,
2150 G_TRAVERSE_ALL, -1, update_element, element);
2153 int connman_element_set_enabled(struct connman_element *element,
2156 if (element->enabled == enabled)
2159 element->enabled = enabled;
2161 emit_enabled_signal(connection, element);
2166 int connman_element_set_scanning(struct connman_element *element,
2169 if (element->scanning == scanning)
2172 element->scanning = scanning;
2174 emit_scanning_signal(connection, element);
2179 int __connman_element_init(DBusConnection *conn, const char *device)
2181 struct connman_element *element;
2183 DBG("conn %p", conn);
2185 connection = dbus_connection_ref(conn);
2186 if (connection == NULL)
2189 device_filter = g_strdup(device);
2191 element = connman_element_create("root");
2193 element->path = g_strdup("/");
2194 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
2196 create_default_properties(element);
2198 element_root = g_node_new(element);
2200 __connman_device_init();
2205 static gboolean free_driver(GNode *node, gpointer data)
2207 struct connman_element *element = node->data;
2209 DBG("element %p name %s", element, element->name);
2211 if (element->driver) {
2212 disable_element(element);
2214 if (element->driver->remove)
2215 element->driver->remove(element);
2217 __connman_element_lock(element);
2218 element->driver = NULL;
2219 __connman_element_unlock(element);
2225 static gboolean free_node(GNode *node, gpointer data)
2227 struct connman_element *element = node->data;
2229 DBG("element %p name %s", element, element->name);
2231 if (g_node_depth(node) > 1)
2232 connman_element_unregister(element);
2237 void __connman_element_cleanup(void)
2241 __connman_device_cleanup();
2243 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2246 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
2249 g_node_destroy(element_root);
2250 element_root = NULL;
2252 g_free(device_filter);
2254 dbus_connection_unref(connection);