5 * Copyright (C) 2007-2009 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;
40 static gchar *nodevice_filter = NULL;
42 static gboolean started = FALSE;
44 static const char *type2string(enum connman_element_type type)
47 case CONNMAN_ELEMENT_TYPE_UNKNOWN:
49 case CONNMAN_ELEMENT_TYPE_ROOT:
51 case CONNMAN_ELEMENT_TYPE_PROFILE:
53 case CONNMAN_ELEMENT_TYPE_DEVICE:
55 case CONNMAN_ELEMENT_TYPE_NETWORK:
57 case CONNMAN_ELEMENT_TYPE_SERVICE:
59 case CONNMAN_ELEMENT_TYPE_IPV4:
61 case CONNMAN_ELEMENT_TYPE_IPV6:
63 case CONNMAN_ELEMENT_TYPE_DHCP:
65 case CONNMAN_ELEMENT_TYPE_BOOTP:
67 case CONNMAN_ELEMENT_TYPE_ZEROCONF:
69 case CONNMAN_ELEMENT_TYPE_CONNECTION:
71 case CONNMAN_ELEMENT_TYPE_VENDOR:
79 enum connman_element_type type;
80 element_cb_t callback;
84 static gboolean foreach_callback(GNode *node, gpointer user_data)
86 struct connman_element *element = node->data;
87 struct foreach_data *data = user_data;
89 DBG("element %p name %s", element, element->name);
91 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
94 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
95 data->type != element->type)
99 data->callback(element, data->user_data);
104 void __connman_element_foreach(struct connman_element *element,
105 enum connman_element_type type,
106 element_cb_t callback, gpointer user_data)
108 struct foreach_data data = { type, callback, user_data };
113 if (element != NULL) {
114 node = g_node_find(element_root, G_PRE_ORDER,
115 G_TRAVERSE_ALL, element);
121 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
122 foreach_callback, &data);
125 struct append_filter {
126 enum connman_element_type type;
127 DBusMessageIter *iter;
130 static gboolean append_path(GNode *node, gpointer user_data)
132 struct connman_element *element = node->data;
133 struct append_filter *filter = user_data;
135 DBG("element %p name %s", element, element->name);
137 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
140 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
141 filter->type != element->type)
144 if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
145 __connman_device_has_driver(element->device) == FALSE)
148 if (filter->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
149 __connman_network_has_driver(element->network) == FALSE)
152 dbus_message_iter_append_basic(filter->iter,
153 DBUS_TYPE_OBJECT_PATH, &element->path);
158 void __connman_element_list(struct connman_element *element,
159 enum connman_element_type type,
160 DBusMessageIter *iter)
162 struct append_filter filter = { type, iter };
167 if (element != NULL) {
168 node = g_node_find(element_root, G_PRE_ORDER,
169 G_TRAVERSE_ALL, element);
175 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
176 append_path, &filter);
180 enum connman_element_type type;
184 static gboolean count_element(GNode *node, gpointer user_data)
186 struct connman_element *element = node->data;
187 struct count_data *data = user_data;
189 DBG("element %p name %s", element, element->name);
191 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
194 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
195 data->type != element->type)
203 int __connman_element_count(struct connman_element *element,
204 enum connman_element_type type)
206 struct count_data data = { type, 0 };
211 if (element != NULL) {
212 node = g_node_find(element_root, G_PRE_ORDER,
213 G_TRAVERSE_ALL, element);
219 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
220 count_element, &data);
225 static struct connman_network *__connman_element_get_network(struct connman_element *element)
227 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
228 element->network != NULL)
229 return element->network;
231 if (element->parent == NULL)
234 return __connman_element_get_network(element->parent);
237 struct connman_service *__connman_element_get_service(struct connman_element *element)
239 struct connman_service *service = NULL;
240 struct connman_network *network;
241 struct connman_device *device;
242 enum connman_device_type type;
244 device = __connman_element_get_device(element);
248 type = connman_device_get_type(device);
251 case CONNMAN_DEVICE_TYPE_UNKNOWN:
252 case CONNMAN_DEVICE_TYPE_VENDOR:
253 case CONNMAN_DEVICE_TYPE_GPS:
255 case CONNMAN_DEVICE_TYPE_ETHERNET:
256 case CONNMAN_DEVICE_TYPE_WIFI:
257 case CONNMAN_DEVICE_TYPE_WIMAX:
258 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
259 case CONNMAN_DEVICE_TYPE_CELLULAR:
260 case CONNMAN_DEVICE_TYPE_MBM:
261 case CONNMAN_DEVICE_TYPE_HSO:
262 network = __connman_element_get_network(element);
265 service = __connman_service_lookup_from_network(network);
272 struct connman_device *__connman_element_get_device(struct connman_element *element)
274 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
275 element->device != NULL)
276 return element->device;
278 if (element->parent == NULL)
281 return __connman_element_get_device(element->parent);
284 const char *__connman_element_get_device_path(struct connman_element *element)
286 struct connman_device *device;
288 device = __connman_element_get_device(element);
292 return connman_device_get_path(device);
295 const char *__connman_element_get_network_path(struct connman_element *element)
297 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
298 element->network != NULL)
299 return element->path;
301 if (element->parent == NULL)
304 return __connman_element_get_network_path(element->parent);
308 enum connman_service_type type;
309 struct connman_device *device;
312 static gboolean find_device(GNode *node, gpointer user_data)
314 struct connman_element *element = node->data;
315 struct find_data *data = user_data;
317 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
320 if (element->device == NULL)
323 if (data->type != connman_device_get_type(element->device))
326 data->device = element->device;
331 struct connman_device *__connman_element_find_device(enum connman_service_type type)
333 struct find_data data = { .type = type, .device = NULL };
335 g_node_traverse(element_root, G_PRE_ORDER,
336 G_TRAVERSE_ALL, -1, find_device, &data);
341 static gboolean request_scan(GNode *node, gpointer user_data)
343 struct connman_element *element = node->data;
344 struct find_data *data = user_data;
345 enum connman_service_type type;
347 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
350 if (element->device == NULL)
353 type = __connman_device_get_service_type(element->device);
356 case CONNMAN_SERVICE_TYPE_UNKNOWN:
357 case CONNMAN_SERVICE_TYPE_ETHERNET:
358 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
359 case CONNMAN_SERVICE_TYPE_CELLULAR:
361 case CONNMAN_SERVICE_TYPE_WIFI:
362 case CONNMAN_SERVICE_TYPE_WIMAX:
363 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
369 __connman_device_scan(element->device);
374 int __connman_element_request_scan(enum connman_service_type type)
376 struct find_data data = { .type = type, .device = NULL };
378 g_node_traverse(element_root, G_PRE_ORDER,
379 G_TRAVERSE_ALL, -1, request_scan, &data);
384 static gboolean enable_technology(GNode *node, gpointer user_data)
386 struct connman_element *element = node->data;
387 struct find_data *data = user_data;
388 enum connman_service_type type;
390 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
393 if (element->device == NULL)
396 type = __connman_device_get_service_type(element->device);
399 case CONNMAN_SERVICE_TYPE_UNKNOWN:
401 case CONNMAN_SERVICE_TYPE_ETHERNET:
402 case CONNMAN_SERVICE_TYPE_WIFI:
403 case CONNMAN_SERVICE_TYPE_WIMAX:
404 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
405 case CONNMAN_SERVICE_TYPE_CELLULAR:
406 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
412 __connman_device_enable_persistent(element->device);
417 int __connman_element_enable_technology(enum connman_service_type type)
419 struct find_data data = { .type = type, .device = NULL };
421 g_node_traverse(element_root, G_PRE_ORDER,
422 G_TRAVERSE_ALL, -1, enable_technology, &data);
427 static gboolean disable_technology(GNode *node, gpointer user_data)
429 struct connman_element *element = node->data;
430 struct find_data *data = user_data;
431 enum connman_service_type type;
433 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
436 if (element->device == NULL)
439 type = __connman_device_get_service_type(element->device);
442 case CONNMAN_SERVICE_TYPE_UNKNOWN:
444 case CONNMAN_SERVICE_TYPE_ETHERNET:
445 case CONNMAN_SERVICE_TYPE_WIFI:
446 case CONNMAN_SERVICE_TYPE_WIMAX:
447 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
448 case CONNMAN_SERVICE_TYPE_CELLULAR:
449 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
455 __connman_device_disable_persistent(element->device);
460 int __connman_element_disable_technology(enum connman_service_type type)
462 struct find_data data = { .type = type, .device = NULL };
464 g_node_traverse(element_root, G_PRE_ORDER,
465 G_TRAVERSE_ALL, -1, disable_technology, &data);
470 static gint compare_priority(gconstpointer a, gconstpointer b)
472 const struct connman_driver *driver1 = a;
473 const struct connman_driver *driver2 = b;
475 return driver2->priority - driver1->priority;
478 static gboolean match_driver(struct connman_element *element,
479 struct connman_driver *driver)
481 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
484 if (element->type == driver->type ||
485 driver->type == CONNMAN_ELEMENT_TYPE_UNKNOWN)
491 static gboolean probe_driver(GNode *node, gpointer data)
493 struct connman_element *element = node->data;
494 struct connman_driver *driver = data;
496 DBG("element %p name %s", element, element->name);
498 if (!element->driver && match_driver(element, driver) == TRUE) {
499 if (driver->probe(element) < 0)
502 __connman_element_lock(element);
503 element->driver = driver;
504 __connman_element_unlock(element);
510 void __connman_driver_rescan(struct connman_driver *driver)
512 DBG("driver %p name %s", driver, driver->name);
517 if (element_root != NULL)
518 g_node_traverse(element_root, G_PRE_ORDER,
519 G_TRAVERSE_ALL, -1, probe_driver, driver);
523 * connman_driver_register:
524 * @driver: driver definition
526 * Register a new driver
528 * Returns: %0 on success
530 int connman_driver_register(struct connman_driver *driver)
532 DBG("driver %p name %s", driver, driver->name);
534 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
540 driver_list = g_slist_insert_sorted(driver_list, driver,
543 if (started == FALSE)
546 if (element_root != NULL)
547 g_node_traverse(element_root, G_PRE_ORDER,
548 G_TRAVERSE_ALL, -1, probe_driver, driver);
553 static gboolean remove_driver(GNode *node, gpointer data)
555 struct connman_element *element = node->data;
556 struct connman_driver *driver = data;
558 DBG("element %p name %s", element, element->name);
560 if (element->driver == driver) {
562 driver->remove(element);
564 __connman_element_lock(element);
565 element->driver = NULL;
566 __connman_element_unlock(element);
573 * connman_driver_unregister:
574 * @driver: driver definition
576 * Remove a previously registered driver
578 void connman_driver_unregister(struct connman_driver *driver)
580 DBG("driver %p name %s", driver, driver->name);
582 driver_list = g_slist_remove(driver_list, driver);
584 if (element_root != NULL)
585 g_node_traverse(element_root, G_POST_ORDER,
586 G_TRAVERSE_ALL, -1, remove_driver, driver);
589 static void unregister_property(gpointer data)
591 struct connman_property *property = data;
593 DBG("property %p", property);
595 g_free(property->value);
599 void __connman_element_initialize(struct connman_element *element)
601 DBG("element %p", element);
603 element->refcount = 1;
605 element->name = NULL;
606 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
607 element->state = CONNMAN_ELEMENT_STATE_UNKNOWN;
608 element->error = CONNMAN_ELEMENT_ERROR_UNKNOWN;
610 element->enabled = FALSE;
612 element->configuring = FALSE;
614 element->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
615 g_free, unregister_property);
619 * connman_element_create:
620 * @name: element name
622 * Allocate a new element and assign the given #name to it. If the name
623 * is #NULL, it will be later on created based on the element type.
625 * Returns: a newly-allocated #connman_element structure
627 struct connman_element *connman_element_create(const char *name)
629 struct connman_element *element;
631 element = g_try_new0(struct connman_element, 1);
635 DBG("element %p", element);
637 __connman_element_initialize(element);
642 struct connman_element *connman_element_ref(struct connman_element *element)
644 DBG("element %p name %s refcount %d", element, element->name,
645 g_atomic_int_get(&element->refcount) + 1);
647 g_atomic_int_inc(&element->refcount);
652 static void free_properties(struct connman_element *element)
654 DBG("element %p name %s", element, element->name);
656 __connman_element_lock(element);
658 g_hash_table_destroy(element->properties);
659 element->properties = NULL;
661 __connman_element_unlock(element);
664 void connman_element_unref(struct connman_element *element)
666 DBG("element %p name %s refcount %d", element, element->name,
667 g_atomic_int_get(&element->refcount) - 1);
669 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
670 if (element->destruct)
671 element->destruct(element);
672 free_properties(element);
673 g_free(element->ipv4.address);
674 g_free(element->ipv4.netmask);
675 g_free(element->ipv4.gateway);
676 g_free(element->ipv4.network);
677 g_free(element->ipv4.broadcast);
678 g_free(element->ipv4.nameserver);
679 g_free(element->devname);
680 g_free(element->path);
681 g_free(element->name);
686 static int set_static_property(struct connman_element *element,
687 const char *name, int type, const void *value)
689 struct connman_property *property;
691 DBG("element %p name %s", element, element->name);
693 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
696 property = g_try_new0(struct connman_property, 1);
697 if (property == NULL)
700 property->id = CONNMAN_PROPERTY_ID_INVALID;
701 property->type = type;
703 DBG("name %s type %d value %p", name, type, value);
706 case DBUS_TYPE_STRING:
707 property->value = g_strdup(*((const char **) value));
709 case DBUS_TYPE_BOOLEAN:
711 property->value = g_try_malloc(1);
712 if (property->value != NULL)
713 memcpy(property->value, value, 1);
717 __connman_element_lock(element);
719 g_hash_table_replace(element->properties, g_strdup(name), property);
721 __connman_element_unlock(element);
726 static int set_static_array_property(struct connman_element *element,
727 const char *name, int type, const void *value, int len)
729 struct connman_property *property;
731 DBG("element %p name %s", element, element->name);
733 if (type != DBUS_TYPE_BYTE)
736 property = g_try_new0(struct connman_property, 1);
737 if (property == NULL)
740 property->id = CONNMAN_PROPERTY_ID_INVALID;
741 property->type = DBUS_TYPE_ARRAY;
742 property->subtype = type;
744 DBG("name %s type %d value %p", name, type, value);
748 property->value = g_try_malloc(len);
749 if (property->value != NULL) {
750 memcpy(property->value,
751 *((const unsigned char **) value), len);
752 property->size = len;
757 __connman_element_lock(element);
759 g_hash_table_replace(element->properties, g_strdup(name), property);
761 __connman_element_unlock(element);
767 static int set_property(struct connman_element *element,
768 enum connman_property_id id, const void *value)
771 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
772 __connman_element_lock(element);
773 g_free(element->ipv4.address);
774 element->ipv4.address = g_strdup(*((const char **) value));
775 __connman_element_unlock(element);
777 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
778 __connman_element_lock(element);
779 g_free(element->ipv4.netmask);
780 element->ipv4.netmask = g_strdup(*((const char **) value));
781 __connman_element_unlock(element);
783 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
784 __connman_element_lock(element);
785 g_free(element->ipv4.gateway);
786 element->ipv4.gateway = g_strdup(*((const char **) value));
787 __connman_element_unlock(element);
789 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
790 __connman_element_lock(element);
791 g_free(element->ipv4.broadcast);
792 element->ipv4.broadcast = g_strdup(*((const char **) value));
793 __connman_element_unlock(element);
795 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
796 __connman_element_lock(element);
797 g_free(element->ipv4.nameserver);
798 element->ipv4.nameserver = g_strdup(*((const char **) value));
799 __connman_element_unlock(element);
809 int connman_element_get_value(struct connman_element *element,
810 enum connman_property_id id, void *value)
812 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
816 case CONNMAN_PROPERTY_ID_IPV4_METHOD:
817 if (element->ipv4.method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
818 return connman_element_get_value(element->parent,
820 __connman_element_lock(element);
821 *((const char **) value) = __connman_ipconfig_method2string(element->ipv4.method);
822 __connman_element_unlock(element);
824 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
825 if (element->ipv4.address == NULL)
826 return connman_element_get_value(element->parent,
828 __connman_element_lock(element);
829 *((char **) value) = element->ipv4.address;
830 __connman_element_unlock(element);
832 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
833 if (element->ipv4.netmask == NULL)
834 return connman_element_get_value(element->parent,
836 __connman_element_lock(element);
837 *((char **) value) = element->ipv4.netmask;
838 __connman_element_unlock(element);
840 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
841 if (element->ipv4.gateway == NULL)
842 return connman_element_get_value(element->parent,
844 __connman_element_lock(element);
845 *((char **) value) = element->ipv4.gateway;
846 __connman_element_unlock(element);
848 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
849 if (element->ipv4.broadcast == NULL)
850 return connman_element_get_value(element->parent,
852 __connman_element_lock(element);
853 *((char **) value) = element->ipv4.broadcast;
854 __connman_element_unlock(element);
856 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
857 if (element->ipv4.nameserver == NULL)
858 return connman_element_get_value(element->parent,
860 __connman_element_lock(element);
861 *((char **) value) = element->ipv4.nameserver;
862 __connman_element_unlock(element);
871 static gboolean get_static_property(struct connman_element *element,
872 const char *name, void *value)
874 struct connman_property *property;
875 gboolean found = FALSE;
877 DBG("element %p name %s", element, element->name);
879 __connman_element_lock(element);
881 property = g_hash_table_lookup(element->properties, name);
882 if (property != NULL) {
883 switch (property->type) {
884 case DBUS_TYPE_STRING:
885 *((char **) value) = property->value;
888 case DBUS_TYPE_BOOLEAN:
890 memcpy(value, property->value, 1);
896 __connman_element_unlock(element);
898 if (found == FALSE && element->parent != NULL)
899 return get_static_property(element->parent, name, value);
904 static gboolean get_static_array_property(struct connman_element *element,
905 const char *name, void *value, unsigned int *len)
907 struct connman_property *property;
908 gboolean found = FALSE;
910 DBG("element %p name %s", element, element->name);
912 __connman_element_lock(element);
914 property = g_hash_table_lookup(element->properties, name);
915 if (property != NULL) {
916 *((void **) value) = property->value;
917 *len = property->size;
921 __connman_element_unlock(element);
927 static gboolean match_static_property(struct connman_element *element,
928 const char *name, const void *value)
930 struct connman_property *property;
931 gboolean result = FALSE;
933 DBG("element %p name %s", element, element->name);
935 __connman_element_lock(element);
937 property = g_hash_table_lookup(element->properties, name);
938 if (property != NULL) {
939 if (property->type == DBUS_TYPE_STRING)
940 result = g_str_equal(property->value,
941 *((const char **) value));
944 __connman_element_unlock(element);
951 * connman_element_set_string:
952 * @element: element structure
953 * @key: unique identifier
954 * @value: string value
956 * Set string value for specific key
958 int connman_element_set_string(struct connman_element *element,
959 const char *key, const char *value)
961 return set_static_property(element, key, DBUS_TYPE_STRING, &value);
965 * connman_element_get_string:
966 * @element: element structure
967 * @key: unique identifier
969 * Get string value for specific key
971 const char *connman_element_get_string(struct connman_element *element,
976 if (get_static_property(element, key, &value) == FALSE)
983 * connman_element_set_bool:
984 * @element: element structure
985 * @key: unique identifier
986 * @value: boolean value
988 * Set boolean value for specific key
990 int connman_element_set_bool(struct connman_element *element,
991 const char *key, connman_bool_t value)
993 return set_static_property(element, key, DBUS_TYPE_BOOLEAN, &value);
997 * connman_element_get_bool:
998 * @element: element structure
999 * @key: unique identifier
1001 * Get boolean value for specific key
1003 connman_bool_t connman_element_get_bool(struct connman_element *element,
1006 connman_bool_t value;
1008 if (get_static_property(element, key, &value) == FALSE)
1015 * connman_element_set_uint8:
1016 * @element: element structure
1017 * @key: unique identifier
1018 * @value: integer value
1020 * Set integer value for specific key
1022 int connman_element_set_uint8(struct connman_element *element,
1023 const char *key, connman_uint8_t value)
1025 return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
1029 * connman_element_get_uint8:
1030 * @element: element structure
1031 * @key: unique identifier
1033 * Get integer value for specific key
1035 connman_uint8_t connman_element_get_uint8(struct connman_element *element,
1038 connman_uint8_t value;
1040 if (get_static_property(element, key, &value) == FALSE)
1047 * connman_element_set_blob:
1048 * @element: element structure
1049 * @key: unique identifier
1053 * Set binary blob value for specific key
1055 int connman_element_set_blob(struct connman_element *element,
1056 const char *key, const void *data, unsigned int size)
1058 return set_static_array_property(element, key,
1059 DBUS_TYPE_BYTE, &data, size);
1063 * connman_element_get_blob:
1064 * @element: element structure
1065 * @key: unique identifier
1066 * @size: pointer to blob size
1068 * Get binary blob value for specific key
1070 const void *connman_element_get_blob(struct connman_element *element,
1071 const char *key, unsigned int *size)
1075 if (get_static_array_property(element, key, &value, size) == FALSE)
1081 int __connman_element_append_ipv4(struct connman_element *element,
1082 DBusMessageIter *dict)
1084 const char *method = NULL;
1085 const char *address = NULL, *netmask = NULL, *gateway = NULL;
1086 const char *broadcast = NULL, *nameserver = NULL;
1088 connman_element_get_value(element,
1089 CONNMAN_PROPERTY_ID_IPV4_METHOD, &method);
1091 connman_element_get_value(element,
1092 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
1093 connman_element_get_value(element,
1094 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
1095 connman_element_get_value(element,
1096 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
1097 connman_element_get_value(element,
1098 CONNMAN_PROPERTY_ID_IPV4_BROADCAST, &broadcast);
1099 connman_element_get_value(element,
1100 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
1103 connman_dbus_dict_append_variant(dict, "IPv4.Method",
1104 DBUS_TYPE_STRING, &method);
1106 if (address != NULL)
1107 connman_dbus_dict_append_variant(dict, "IPv4.Address",
1108 DBUS_TYPE_STRING, &address);
1110 if (netmask != NULL)
1111 connman_dbus_dict_append_variant(dict, "IPv4.Netmask",
1112 DBUS_TYPE_STRING, &netmask);
1114 if (gateway != NULL)
1115 connman_dbus_dict_append_variant(dict, "IPv4.Gateway",
1116 DBUS_TYPE_STRING, &gateway);
1118 if (broadcast != NULL)
1119 connman_dbus_dict_append_variant(dict, "IPv4.Broadcast",
1120 DBUS_TYPE_STRING, &broadcast);
1122 if (nameserver != NULL)
1123 connman_dbus_dict_append_variant(dict, "IPv4.Nameserver",
1124 DBUS_TYPE_STRING, &nameserver);
1129 int __connman_element_set_ipv4(struct connman_element *element,
1130 const char *name, DBusMessageIter *value)
1134 type = dbus_message_iter_get_arg_type(value);
1136 if (g_str_equal(name, "IPv4.Method") == TRUE) {
1137 enum connman_ipconfig_method method;
1140 if (type != DBUS_TYPE_STRING)
1143 dbus_message_iter_get_basic(value, &str);
1144 method = __connman_ipconfig_string2method(str);
1145 if (method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
1148 if (method == element->ipv4.method)
1151 element->ipv4.method = method;
1153 connman_element_update(element);
1154 } else if (g_str_equal(name, "IPv4.Address") == TRUE) {
1155 const char *address;
1157 if (type != DBUS_TYPE_STRING)
1160 dbus_message_iter_get_basic(value, &address);
1162 g_free(element->ipv4.address);
1163 element->ipv4.address = g_strdup(address);
1165 connman_element_update(element);
1166 } else if (g_str_equal(name, "IPv4.Netmask") == TRUE) {
1167 const char *netmask;
1169 if (type != DBUS_TYPE_STRING)
1172 dbus_message_iter_get_basic(value, &netmask);
1174 g_free(element->ipv4.netmask);
1175 element->ipv4.netmask = g_strdup(netmask);
1177 connman_element_update(element);
1178 } else if (g_str_equal(name, "IPv4.Gateway") == TRUE) {
1179 const char *gateway;
1181 if (type != DBUS_TYPE_STRING)
1184 dbus_message_iter_get_basic(value, &gateway);
1186 g_free(element->ipv4.gateway);
1187 element->ipv4.gateway = g_strdup(gateway);
1189 connman_element_update(element);
1190 } else if (g_str_equal(name, "IPv4.Broadcast") == TRUE) {
1191 const char *broadcast;
1193 if (type != DBUS_TYPE_STRING)
1196 dbus_message_iter_get_basic(value, &broadcast);
1198 g_free(element->ipv4.broadcast);
1199 element->ipv4.broadcast = g_strdup(broadcast);
1201 connman_element_update(element);
1202 } else if (g_str_equal(name, "IPv4.Nameserver") == TRUE) {
1203 const char *nameserver;
1205 if (type != DBUS_TYPE_STRING)
1208 dbus_message_iter_get_basic(value, &nameserver);
1210 g_free(element->ipv4.nameserver);
1211 element->ipv4.nameserver = g_strdup(nameserver);
1213 connman_element_update(element);
1219 static void append_state(DBusMessageIter *entry, const char *state)
1221 DBusMessageIter value;
1222 const char *key = "State";
1224 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1226 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1227 DBUS_TYPE_STRING_AS_STRING, &value);
1228 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
1229 dbus_message_iter_close_container(entry, &value);
1232 static void emit_state_change(DBusConnection *conn, const char *state)
1234 DBusMessage *signal;
1235 DBusMessageIter entry;
1237 DBG("conn %p", conn);
1239 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1240 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1244 dbus_message_iter_init_append(signal, &entry);
1246 append_state(&entry, state);
1248 g_dbus_send_message(conn, signal);
1250 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1251 CONNMAN_MANAGER_INTERFACE, "StateChanged");
1255 dbus_message_iter_init_append(signal, &entry);
1256 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &state);
1258 g_dbus_send_message(conn, signal);
1261 static void probe_element(struct connman_element *element)
1265 DBG("element %p name %s", element, element->name);
1267 for (list = driver_list; list; list = list->next) {
1268 struct connman_driver *driver = list->data;
1270 if (match_driver(element, driver) == FALSE)
1273 DBG("driver %p name %s", driver, driver->name);
1275 if (driver->probe(element) == 0) {
1276 __connman_element_lock(element);
1277 element->driver = driver;
1278 __connman_element_unlock(element);
1284 static void register_element(gpointer data, gpointer user_data)
1286 struct connman_element *element = data;
1287 const gchar *basepath;
1290 __connman_element_lock(element);
1292 if (element->parent) {
1293 node = g_node_find(element_root, G_PRE_ORDER,
1294 G_TRAVERSE_ALL, element->parent);
1295 basepath = element->parent->path;
1297 element->parent = element_root->data;
1299 node = element_root;
1300 basepath = "/device";
1303 element->path = g_strdup_printf("%s/%s", basepath, element->name);
1305 __connman_element_unlock(element);
1308 connman_error("Element registration for %s failed",
1313 DBG("element %p path %s", element, element->path);
1315 g_node_append_data(node, element);
1317 if (element->type == CONNMAN_ELEMENT_TYPE_DHCP) {
1318 element->parent->configuring = TRUE;
1321 if (__connman_element_count(NULL,
1322 CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1323 emit_state_change(connection, "connecting");
1327 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1328 struct connman_element *parent = element->parent;
1331 parent->configuring = FALSE;
1332 parent = parent->parent;
1335 if (__connman_element_count(NULL,
1336 CONNMAN_ELEMENT_TYPE_CONNECTION) == 1)
1337 emit_state_change(connection, "online");
1340 if (started == FALSE)
1343 probe_element(element);
1346 gboolean __connman_element_device_isfiltered(const char *devname)
1348 if (device_filter == NULL)
1351 if (g_pattern_match_simple(device_filter, devname) == FALSE) {
1352 DBG("ignoring device %s (match)", devname);
1357 if (nodevice_filter == NULL)
1360 if (g_pattern_match_simple(nodevice_filter, devname) == TRUE) {
1361 DBG("ignoring device %s (no match)", devname);
1369 * connman_element_register:
1370 * @element: the element to register
1371 * @parent: the parent to register the element with
1373 * Register an element with the core. It will be register under the given
1374 * parent of if %NULL is provided under the root element.
1376 * Returns: %0 on success
1378 int connman_element_register(struct connman_element *element,
1379 struct connman_element *parent)
1381 DBG("element %p name %s parent %p", element, element->name, parent);
1383 if (element->devname == NULL)
1384 element->devname = g_strdup(element->name);
1386 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
1389 if (__connman_element_device_isfiltered(element->devname) == TRUE)
1393 if (connman_element_ref(element) == NULL)
1396 __connman_element_lock(element);
1398 if (element->name == NULL) {
1399 element->name = g_strdup(type2string(element->type));
1400 if (element->name == NULL) {
1401 __connman_element_unlock(element);
1406 if (element->type == CONNMAN_ELEMENT_TYPE_DHCP)
1407 element->ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
1409 element->parent = parent;
1411 __connman_element_unlock(element);
1413 register_element(element, NULL);
1418 static gboolean remove_element(GNode *node, gpointer user_data)
1420 struct connman_element *element = node->data;
1421 struct connman_element *root = user_data;
1423 DBG("element %p name %s", element, element->name);
1425 if (element == root)
1429 g_node_unlink(node);
1431 if (element->driver) {
1432 if (element->driver->remove)
1433 element->driver->remove(element);
1435 __connman_element_lock(element);
1436 element->driver = NULL;
1437 __connman_element_unlock(element);
1441 g_node_destroy(node);
1443 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1444 if (__connman_element_count(NULL,
1445 CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1446 emit_state_change(connection, "offline");
1449 connman_element_unref(element);
1454 void connman_element_unregister(struct connman_element *element)
1458 DBG("element %p name %s", element, element->name);
1460 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1463 g_node_traverse(node, G_POST_ORDER,
1464 G_TRAVERSE_ALL, -1, remove_element, NULL);
1467 void connman_element_unregister_children(struct connman_element *element)
1471 DBG("element %p name %s", element, element->name);
1473 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1476 g_node_traverse(node, G_POST_ORDER,
1477 G_TRAVERSE_ALL, -1, remove_element, element);
1480 static gboolean update_element(GNode *node, gpointer user_data)
1482 struct connman_element *element = node->data;
1484 DBG("element %p name %s", element, element->name);
1486 if (element->driver && element->driver->update)
1487 element->driver->update(element);
1492 void connman_element_update(struct connman_element *element)
1496 DBG("element %p name %s", element, element->name);
1498 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1501 g_node_traverse(node, G_PRE_ORDER,
1502 G_TRAVERSE_ALL, -1, update_element, element);
1505 int connman_element_set_enabled(struct connman_element *element,
1508 if (element->enabled == enabled)
1511 element->enabled = enabled;
1513 connman_element_update(element);
1518 static enum connman_service_error convert_error(enum connman_element_error error)
1521 case CONNMAN_ELEMENT_ERROR_UNKNOWN:
1522 case CONNMAN_ELEMENT_ERROR_FAILED:
1524 case CONNMAN_ELEMENT_ERROR_DHCP_FAILED:
1525 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
1526 case CONNMAN_ELEMENT_ERROR_CONNECT_FAILED:
1527 return CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
1530 return CONNMAN_SERVICE_ERROR_UNKNOWN;
1534 * connman_element_set_error:
1535 * @element: element structure
1536 * @error: error identifier
1538 * Set error state and specific error identifier
1540 void connman_element_set_error(struct connman_element *element,
1541 enum connman_element_error error)
1543 struct connman_service *service;
1545 DBG("element %p error %d", element, error);
1547 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1550 element->state = CONNMAN_ELEMENT_STATE_ERROR;
1551 element->error = error;
1553 if (element->driver && element->driver->change)
1554 element->driver->change(element);
1556 service = __connman_element_get_service(element);
1557 __connman_service_indicate_error(service, convert_error(error));
1560 int __connman_element_init(const char *device, const char *nodevice)
1562 struct connman_element *element;
1566 connection = connman_dbus_get_connection();
1567 if (connection == NULL)
1570 device_filter = g_strdup(device);
1571 nodevice_filter = g_strdup(nodevice);
1573 element = connman_element_create("root");
1575 element->path = g_strdup("/");
1576 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1578 element_root = g_node_new(element);
1580 __connman_notifier_init();
1581 __connman_service_init();
1582 __connman_provider_init();
1583 __connman_network_init();
1584 __connman_device_init();
1589 static gboolean probe_node(GNode *node, gpointer data)
1591 struct connman_element *element = node->data;
1593 DBG("element %p name %s", element, element->name);
1595 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1598 if (element->driver)
1601 probe_element(element);
1606 void __connman_element_start(void)
1610 __connman_storage_init_profile();
1612 g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1617 __connman_rtnl_start();
1618 __connman_udev_start();
1620 __connman_connection_init();
1621 __connman_ipv4_init();
1622 __connman_dhcp_init();
1624 if (__connman_rfkill_init() < 0)
1625 __connman_udev_enable_rfkill_processing();
1628 void __connman_element_stop(void)
1632 __connman_rfkill_cleanup();
1634 __connman_dhcp_cleanup();
1635 __connman_ipv4_cleanup();
1636 __connman_provider_cleanup();
1637 __connman_connection_cleanup();
1640 static gboolean free_driver(GNode *node, gpointer data)
1642 struct connman_element *element = node->data;
1644 DBG("element %p name %s", element, element->name);
1646 if (element->driver) {
1647 if (element->driver->remove)
1648 element->driver->remove(element);
1650 __connman_element_lock(element);
1651 element->driver = NULL;
1652 __connman_element_unlock(element);
1658 static gboolean free_node(GNode *node, gpointer data)
1660 struct connman_element *element = node->data;
1662 DBG("element %p name %s", element, element->name);
1664 if (g_node_depth(node) > 1)
1665 connman_element_unregister(element);
1670 void __connman_element_cleanup(void)
1674 __connman_device_cleanup();
1675 __connman_network_cleanup();
1676 __connman_service_cleanup();
1677 __connman_notifier_cleanup();
1679 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1682 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1685 g_node_destroy(element_root);
1686 element_root = NULL;
1688 g_free(nodevice_filter);
1689 g_free(device_filter);
1691 if (connection == NULL)
1694 dbus_connection_unref(connection);