1 #include "e_connman_private.h"
5 static Eina_Hash *elements = NULL;
7 typedef struct _E_Connman_Element_Pending E_Connman_Element_Pending;
8 typedef struct _E_Connman_Element_Call_Data E_Connman_Element_Call_Data;
9 typedef struct _E_Connman_Element_Property E_Connman_Element_Property;
10 typedef struct _E_Connman_Element_Listener E_Connman_Element_Listener;
11 typedef struct _E_Connman_Element_Dict_Entry E_Connman_Element_Dict_Entry;
13 struct _E_Connman_Element_Pending
16 DBusPendingCall *pending;
18 E_DBus_Method_Return_Cb user_cb;
22 struct _E_Connman_Element_Call_Data
24 E_Connman_Element *element;
25 E_DBus_Method_Return_Cb cb;
26 E_Connman_Element_Pending *pending;
30 struct _E_Connman_Element_Property
43 E_Connman_Array *array;
47 struct _E_Connman_Element_Dict_Entry
58 E_Connman_Array *array;
62 struct _E_Connman_Element_Listener
65 void (*cb)(void *data, const E_Connman_Element *element);
67 void (*free_data)(void *data);
71 _e_connman_element_event_no_free(void *data __UNUSED__, void *ev)
73 E_Connman_Element *element = ev;
74 e_connman_element_unref(element);
78 e_connman_element_event_add(int event_type, E_Connman_Element *element)
80 e_connman_element_ref(element);
82 (event_type, element, _e_connman_element_event_no_free, element);
86 e_connman_element_call_dispatch_and_free(void *d, DBusMessage *msg, DBusError *err)
88 E_Connman_Element_Call_Data *data = d;
89 E_Connman_Element_Pending *pending;
91 pending = data->pending;
92 pending->pending = NULL;
95 data->cb(data->element, msg, err);
98 pending->user_cb(pending->user_data, msg, err);
100 pending->data = NULL;
101 *data->p_list = eina_inlist_remove(*data->p_list, EINA_INLIST_GET(pending));
107 e_connman_element_pending_cancel_and_free(Eina_Inlist **pending)
111 E_Connman_Element_Pending *p = (E_Connman_Element_Pending *)*pending;
114 dbus_pending_call_cancel(p->pending);
116 dbus_error_init(&err);
117 dbus_set_error(&err, "Canceled", "Pending method call was canceled.");
118 e_connman_element_call_dispatch_and_free(p->data, NULL, &err);
119 dbus_error_free(&err);
124 e_connman_element_listener_add(E_Connman_Element *element, void (*cb)(void *data, const E_Connman_Element *element), const void *data, void (*free_data)(void *data))
126 E_Connman_Element_Listener *l;
127 EINA_SAFETY_ON_FALSE_GOTO(element, error);
128 EINA_SAFETY_ON_FALSE_GOTO(cb, error);
130 l = malloc(sizeof(*l));
133 ERR("could not allocate E_Connman_Element_Listener");
138 l->data = (void *)data;
139 l->free_data = free_data;
141 element->_listeners = eina_inlist_append
142 (element->_listeners, EINA_INLIST_GET(l));
148 free_data((void *)data);
152 e_connman_element_listener_del(E_Connman_Element *element, void (*cb)(void *data, const E_Connman_Element *element), const void *data)
154 E_Connman_Element_Listener *l;
156 EINA_SAFETY_ON_NULL_RETURN(element);
157 EINA_SAFETY_ON_NULL_RETURN(cb);
159 EINA_INLIST_FOREACH(element->_listeners, l)
160 if ((l->cb == cb) && (l->data == data))
162 element->_listeners = eina_inlist_remove
163 (element->_listeners, EINA_INLIST_GET(l));
165 l->free_data(l->data);
173 _e_connman_element_listeners_call_do(E_Connman_Element *element)
175 E_Connman_Element_Listener *l;
178 /* NB: iterate on a copy in order to allow listeners to be deleted
179 * from callbacks. number of listeners should be small, so the
180 * following should do fine.
182 if (eina_inlist_count(element->_listeners) < 1) goto end;
184 EINA_INLIST_FOREACH_SAFE(element->_listeners, x, l)
185 l->cb(l->data, element);
188 e_connman_element_event_add(E_CONNMAN_EVENT_ELEMENT_UPDATED, element);
192 _e_connman_element_listeners_call_idler(void *data)
194 E_Connman_Element *element = data;
195 _e_connman_element_listeners_call_do(element);
196 element->_idler.changed = NULL;
197 return ECORE_CALLBACK_CANCEL;
201 _e_connman_element_listeners_call(E_Connman_Element *element)
203 if (element->_idler.changed)
206 element->_idler.changed = ecore_idler_add
207 (_e_connman_element_listeners_call_idler, element);
210 /***********************************************************************
212 ***********************************************************************/
215 _e_connman_element_dict_entry_free(E_Connman_Element_Dict_Entry *entry)
219 case DBUS_TYPE_BOOLEAN:
221 case DBUS_TYPE_UINT16:
222 case DBUS_TYPE_UINT32:
225 case DBUS_TYPE_OBJECT_PATH:
226 eina_stringshare_del(entry->value.path);
229 case DBUS_TYPE_STRING:
230 eina_stringshare_del(entry->value.str);
234 ERR("don't know how to free dict entry '%s' of type %c (%d)",
235 entry->name, entry->type, entry->type);
238 eina_stringshare_del(entry->name);
242 static E_Connman_Element_Dict_Entry *
243 _e_connman_element_dict_entry_new(DBusMessageIter *itr)
245 E_Connman_Element_Dict_Entry *entry;
246 DBusMessageIter e_itr, v_itr;
248 const char *key = NULL;
251 dbus_message_iter_recurse(itr, &e_itr);
253 t = dbus_message_iter_get_arg_type(&e_itr);
254 if (!_dbus_iter_type_check(t, DBUS_TYPE_STRING))
256 ERR("invalid format for dict entry. first type not a string: %c (%d)",
261 dbus_message_iter_get_basic(&e_itr, &key);
264 ERR("invalid format for dict entry. no key.");
268 dbus_message_iter_next(&e_itr);
269 t = dbus_message_iter_get_arg_type(&e_itr);
270 if (!_dbus_iter_type_check(t, DBUS_TYPE_VARIANT))
272 ERR("invalid format for dict entry '%s'. "
273 "second type not a variant: %c (%d)",
278 dbus_message_iter_recurse(&e_itr, &v_itr);
280 t = dbus_message_iter_get_arg_type(&v_itr);
281 if ((t == DBUS_TYPE_INVALID) || (t == DBUS_TYPE_ARRAY))
283 ERR("invalid type for dict value for entry '%s': %c (%d)",
288 entry = calloc(1, sizeof(*entry));
291 ERR("could not allocate memory for dict entry.");
295 dbus_message_iter_get_basic(&v_itr, &value);
298 case DBUS_TYPE_BOOLEAN:
299 entry->value.boolean = (Eina_Bool)(long)value;
303 entry->value.byte = (unsigned char)(long)value;
306 case DBUS_TYPE_UINT16:
307 entry->value.u16 = (unsigned short)(long)value;
310 case DBUS_TYPE_UINT32:
311 entry->value.u32 = (unsigned int)(long)value;
314 case DBUS_TYPE_STRING:
315 entry->value.str = eina_stringshare_add(value);
318 case DBUS_TYPE_OBJECT_PATH:
319 entry->value.path = eina_stringshare_add(value);
323 ERR("don't know how to create dict entry '%s' for of type %c (%d)",
329 entry->name = eina_stringshare_add(key);
334 static E_Connman_Element_Dict_Entry *
335 _e_connman_element_array_dict_find_stringshared(const E_Connman_Array *array, const char *key)
337 E_Connman_Element_Dict_Entry *entry;
338 Eina_Array_Iterator iterator;
341 EINA_ARRAY_ITER_NEXT(array->array, i, entry, iterator)
342 if (entry->name == key)
349 _e_connman_element_array_free(E_Connman_Array *array, E_Connman_Array *new __UNUSED__)
351 Eina_Array_Iterator iterator;
360 case DBUS_TYPE_BOOLEAN:
362 case DBUS_TYPE_UINT16:
363 case DBUS_TYPE_UINT32:
366 case DBUS_TYPE_STRING:
367 case DBUS_TYPE_OBJECT_PATH:
368 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
369 eina_stringshare_del(item);
372 case DBUS_TYPE_DICT_ENTRY:
373 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
374 _e_connman_element_dict_entry_free(item);
378 ERR("don't know how to free array of values of type %c (%d)",
379 array->type, array->type);
382 eina_array_free(array->array);
387 _e_connman_element_property_value_free(E_Connman_Element_Property *property)
389 switch (property->type)
394 case DBUS_TYPE_BOOLEAN:
396 case DBUS_TYPE_UINT16:
397 case DBUS_TYPE_UINT32:
400 case DBUS_TYPE_STRING:
401 eina_stringshare_del(property->value.str);
404 case DBUS_TYPE_OBJECT_PATH:
405 eina_stringshare_del(property->value.path);
408 case DBUS_TYPE_ARRAY:
409 _e_connman_element_array_free(property->value.array, NULL);
413 ERR("don't know how to free value of property type %c (%d)",
414 property->type, property->type);
419 _e_connman_element_get_interface(const char *key)
421 const char *interface = NULL, *tail;
430 if (strcmp(tail, "rofiles") == 0)
431 interface = e_connman_iface_profile;
436 if (strcmp(tail, "ervices") == 0)
437 interface = e_connman_iface_service;
442 if (strcmp(tail, "echnologies") == 0)
443 interface = e_connman_iface_technology;
452 ERR("failed to find interface for property \"%s\"", key);
458 _e_connman_element_item_register(const char *key, const char *item)
460 E_Connman_Element *element;
461 const char *interface;
463 interface = _e_connman_element_get_interface(key);
467 element = e_connman_element_register(item, interface);
468 if ((element) && (!e_connman_element_properties_sync(element)))
469 WRN("could not get properties of %s", element->path);
472 /* Match 2 arrays to find which are new and which are old elements
473 * For new elements, register them under prop_name property
474 * For old elements, unregister them, sending proper DEL event
477 _e_connman_element_array_match(E_Connman_Array *old, E_Connman_Array *new, const char *prop_name)
479 Eina_List *deleted = NULL;
480 Eina_Array_Iterator iter_old, iter_new;
481 unsigned int i_old = 0, i_new = 0;
482 void *item_old, *item_new;
489 if (old->type != DBUS_TYPE_OBJECT_PATH)
492 if ((!new) || (!new->array) || eina_array_count_get(new->array) == 0)
494 if ((!old) || (!old->array) || eina_array_count_get(old->array) == 0)
500 iter_old = old->array->data;
501 goto out_remove_remaining;
505 iter_new = new->array->data;
506 item_new = *iter_new;
507 EINA_ARRAY_ITER_NEXT(old->array, i_old, item_old, iter_old)
509 if (item_old == item_new)
512 if (i_new >= eina_array_count_get(new->array))
519 item_new = *iter_new;
523 deleted = eina_list_append(deleted, item_old);
527 for(; i_new < eina_array_count_get(new->array); iter_new++, i_new++)
529 Eina_Bool found = EINA_FALSE;
530 item_new = *iter_new;
534 EINA_LIST_FOREACH(deleted, l, data)
536 if (data == item_new)
538 deleted = eina_list_remove_list(deleted, l);
545 _e_connman_element_item_register(prop_name, item_new);
546 DBG("Add element %s\n", (const char *)item_new);
550 /* everybody after i_old on old->array + everybody from deleted list
553 EINA_LIST_FREE(deleted, data)
555 E_Connman_Element *e = e_connman_element_get(data);
557 e_connman_element_unregister(e);
559 DBG("Delete element %s\n", (const char *)data);
562 out_remove_remaining:
563 for(; i_old < eina_array_count_get(old->array); iter_old++, i_old++)
565 E_Connman_Element *e;
566 item_old = *iter_old;
570 e = e_connman_element_get(item_old);
572 e_connman_element_unregister(e);
574 DBG("Delete element %s\n", (const char *)item_old);
579 _e_connman_element_property_update(E_Connman_Element_Property *property, int type, void *data)
581 Eina_Bool changed = EINA_FALSE;
583 if ((type == DBUS_TYPE_STRING || type == DBUS_TYPE_OBJECT_PATH) && data)
584 data = (char *)eina_stringshare_add(data);
586 if (property->type != type)
589 DBG("property type changed from '%c' to '%c'",
590 property->type, type);
592 _e_connman_element_property_value_free(property);
593 memset(&property->value, 0, sizeof(property->value));
594 property->type = type;
600 case DBUS_TYPE_BOOLEAN:
601 if (changed || property->value.boolean != (Eina_Bool)(long)data)
603 property->value.boolean = (Eina_Bool)(long)data;
610 if (changed || property->value.byte != (unsigned char)(long)data)
612 property->value.byte = (unsigned char)(long)data;
618 case DBUS_TYPE_UINT16:
619 if (changed || property->value.u16 != (unsigned short)(long)data)
621 property->value.u16 = (unsigned short)(long)data;
627 case DBUS_TYPE_UINT32:
628 if (changed || property->value.u32 != (unsigned int)(long)data)
630 property->value.u32 = (unsigned int)(long)data;
636 case DBUS_TYPE_STRING:
639 property->value.str = data;
643 if (property->value.str)
644 eina_stringshare_del(property->value.str);
646 if (property->value.str != data)
648 property->value.str = data;
655 case DBUS_TYPE_OBJECT_PATH:
658 property->value.path = data;
662 if (property->value.path)
663 eina_stringshare_del(property->value.path);
665 if (property->value.path != data)
667 property->value.path = data;
674 case DBUS_TYPE_ARRAY:
676 if (property->value.array)
678 _e_connman_element_array_match(property->value.array, data, property->name);
679 _e_connman_element_array_free(property->value.array, data);
682 property->value.array = data;
687 ERR("don't know how to update property type %c (%d)", type, type);
693 static E_Connman_Element_Property *
694 _e_connman_element_property_new(const char *name, int type, void *data)
696 E_Connman_Element_Property *property;
698 property = calloc(1, sizeof(*property));
701 eina_stringshare_del(name);
702 ERR("could not allocate property: %s", strerror(errno));
706 property->name = name;
707 _e_connman_element_property_update(property, type, data);
712 _e_connman_element_property_free(E_Connman_Element_Property *property)
714 _e_connman_element_property_value_free(property);
715 eina_stringshare_del(property->name);
719 /***********************************************************************
721 ***********************************************************************/
723 e_connman_element_bytes_array_get_stringshared(const E_Connman_Element *element, const char *property, unsigned int *count)
725 Eina_Array_Iterator iterator;
726 E_Connman_Array *array;
727 unsigned char *ret, *p;
731 EINA_SAFETY_ON_NULL_RETURN_VAL(element, NULL);
732 EINA_SAFETY_ON_NULL_RETURN_VAL(property, NULL);
733 EINA_SAFETY_ON_NULL_RETURN_VAL(count, NULL);
737 if (!e_connman_element_property_get_stringshared
738 (element, property, NULL, &array))
741 if ((!array) || (!(array->array)))
744 *count = eina_array_count_get(array->array);
745 ret = malloc(*count * sizeof(unsigned char));
748 ERR("could not allocate return array of %d bytes: %s",
749 *count, strerror(errno));
755 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
757 *p = (unsigned char)(long)item;
764 e_connman_element_objects_array_get_stringshared(const E_Connman_Element *element, const char *property, unsigned int *count, E_Connman_Element ***p_elements)
766 E_Connman_Element **ret, **p;
767 Eina_Array_Iterator iterator;
768 E_Connman_Array *array;
773 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
774 EINA_SAFETY_ON_NULL_RETURN_VAL(property, EINA_FALSE);
775 EINA_SAFETY_ON_NULL_RETURN_VAL(count, EINA_FALSE);
776 EINA_SAFETY_ON_NULL_RETURN_VAL(p_elements, EINA_FALSE);
781 if (!e_connman_element_property_get_stringshared
782 (element, property, &type, &array))
785 if (type != DBUS_TYPE_ARRAY)
787 ERR("property %s is not an array!", property);
791 if ((!array) || (!array->array) || (array->type == DBUS_TYPE_INVALID))
794 if (array->type != DBUS_TYPE_OBJECT_PATH)
796 ERR("property %s is not an array of object paths!", property);
800 *count = eina_array_count_get(array->array);
801 ret = malloc(*count * sizeof(E_Connman_Element *));
804 ERR("could not allocate return array of %d elements: %s",
805 *count, strerror(errno));
812 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
814 E_Connman_Element *e = e_connman_element_get(item);
826 /* array and strings are just pointers (references),
827 * no malloc, strdup or stringshare_add/ref
830 e_connman_element_strings_array_get_stringshared(const E_Connman_Element *element, const char *property, unsigned int *count, const char ***strings)
832 E_Connman_Array *array;
835 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
836 EINA_SAFETY_ON_NULL_RETURN_VAL(property, EINA_FALSE);
837 EINA_SAFETY_ON_NULL_RETURN_VAL(count, EINA_FALSE);
838 EINA_SAFETY_ON_NULL_RETURN_VAL(strings, EINA_FALSE);
843 if (!e_connman_element_property_get_stringshared
844 (element, property, &type, &array))
847 if (type != DBUS_TYPE_ARRAY)
849 ERR("property %s is not an array!", property);
853 if ((!array) || (!array->array) || (array->type == DBUS_TYPE_INVALID))
856 if (array->type != DBUS_TYPE_STRING)
858 ERR("property %s is not an array of strings!", property);
862 *count = array->array->count;
863 *strings = (const char **)array->array->data;
868 _e_connman_element_array_print(FILE *fp, E_Connman_Array *array)
870 Eina_Array_Iterator iterator;
879 case DBUS_TYPE_OBJECT_PATH:
880 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
881 fprintf(fp, "\"%s\", ", (const char *)item);
884 case DBUS_TYPE_STRING:
885 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
886 fprintf(fp, "\"%s\", ", (const char *)item);
890 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
891 fprintf(fp, "%#02hhx (\"%c\"), ", (unsigned char)(long)item,
892 (unsigned char)(long)item);
895 case DBUS_TYPE_UINT16:
896 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
897 fprintf(fp, "%#04hx (%hu), ", (unsigned short)(long)item,
898 (unsigned short)(long)item);
901 case DBUS_TYPE_UINT32:
902 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
903 fprintf(fp, "%#08x (%u), ", (unsigned int)(long)item,
904 (unsigned int)(long)item);
907 case DBUS_TYPE_DICT_ENTRY:
909 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
911 E_Connman_Element_Dict_Entry *entry = item;
912 fprintf(fp, "%s: ", entry->name);
915 case DBUS_TYPE_OBJECT_PATH:
916 fprintf(fp, "\"%s\", ", entry->value.path);
919 case DBUS_TYPE_STRING:
920 fprintf(fp, "\"%s\", ", entry->value.str);
924 fprintf(fp, "%#02hhx (\"%c\"), ",
925 entry->value.byte, entry->value.byte);
928 case DBUS_TYPE_UINT16:
929 fprintf(fp, "%#04hx (%hu), ",
930 entry->value.u16, entry->value.u16);
933 case DBUS_TYPE_UINT32:
934 fprintf(fp, "%#08x (%u), ",
935 entry->value.u32, entry->value.u32);
939 fprintf(fp, "<UNKNOWN TYPE '%c'>", entry->type);
946 fprintf(fp, "<UNKNOWN ARRAY TYPE '%c'>", array->type);
951 * Print element to file descriptor.
954 e_connman_element_print(FILE *fp, const E_Connman_Element *element)
956 const E_Connman_Element_Property *p;
958 EINA_SAFETY_ON_NULL_RETURN(fp);
961 fputs("Error: no element to print\n", fp);
966 "Element %p: %s [%s]\n"
968 element, element->path, element->interface);
970 EINA_INLIST_FOREACH(element->props, p)
972 fprintf(fp, "\t\t%s (%c) = ", p->name, p->type);
976 case DBUS_TYPE_STRING:
977 fprintf(fp, "\"%s\"", p->value.str);
980 case DBUS_TYPE_OBJECT_PATH:
981 fprintf(fp, "\"%s\"", p->value.path);
984 case DBUS_TYPE_BOOLEAN:
985 fprintf(fp, "%hhu", p->value.boolean);
989 fprintf(fp, "%#02hhx (%d), ", p->value.byte, p->value.byte);
992 case DBUS_TYPE_UINT16:
993 fprintf(fp, "%hu", p->value.u16);
996 case DBUS_TYPE_UINT32:
997 fprintf(fp, "%u", p->value.u32);
1000 case DBUS_TYPE_ARRAY:
1001 _e_connman_element_array_print(fp, p->value.array);
1005 fputs("don't know how to print type", fp);
1012 static E_Connman_Element *
1013 e_connman_element_new(const char *path, const char *interface)
1015 E_Connman_Element *element;
1017 element = calloc(1, sizeof(*element));
1020 ERR("could not allocate element: %s", strerror(errno));
1024 element->path = eina_stringshare_add(path);
1025 element->interface = eina_stringshare_ref(interface);
1026 element->_references = 1;
1032 e_connman_element_extra_properties_free(E_Connman_Element *element)
1034 while (element->props)
1036 E_Connman_Element_Property *prop;
1037 prop = (E_Connman_Element_Property *)element->props;
1038 element->props = element->props->next;
1039 _e_connman_element_property_free(prop);
1044 e_connman_element_free(E_Connman_Element *element)
1046 if (element->_idler.changed)
1047 ecore_idler_del(element->_idler.changed);
1049 while (element->_listeners)
1051 E_Connman_Element_Listener *l = (void *)element->_listeners;
1052 element->_listeners = eina_inlist_remove
1053 (element->_listeners, element->_listeners);
1056 l->free_data(l->data);
1061 e_connman_element_pending_cancel_and_free(&element->_pending.properties_get);
1062 e_connman_element_pending_cancel_and_free(&element->_pending.property_set);
1063 e_connman_element_pending_cancel_and_free(&element->_pending.agent_register);
1064 e_connman_element_pending_cancel_and_free(&element->_pending.agent_unregister);
1065 e_connman_element_pending_cancel_and_free(&element->_pending.request_scan);
1066 e_connman_element_pending_cancel_and_free(&element->_pending.technology_enable);
1067 e_connman_element_pending_cancel_and_free(&element->_pending.technology_disable);
1068 e_connman_element_pending_cancel_and_free(&element->_pending.profile_remove);
1069 e_connman_element_pending_cancel_and_free(&element->_pending.service_connect);
1070 e_connman_element_pending_cancel_and_free(&element->_pending.service_disconnect);
1071 e_connman_element_pending_cancel_and_free(&element->_pending.service_remove);
1072 e_connman_element_pending_cancel_and_free(&element->_pending.service_move_before);
1073 e_connman_element_pending_cancel_and_free(&element->_pending.service_move_after);
1074 e_connman_element_pending_cancel_and_free(&element->_pending.service_clear_property);
1076 e_connman_element_extra_properties_free(element);
1077 eina_stringshare_del(element->interface);
1078 eina_stringshare_del(element->path);
1083 * Add reference to element.
1086 e_connman_element_ref(E_Connman_Element *element)
1088 EINA_SAFETY_ON_NULL_RETURN_VAL(element, 0);
1089 return ++element->_references;
1093 * Remove reference from element.
1095 * If reference count drops to 0 element will be freed.
1098 e_connman_element_unref(E_Connman_Element *element)
1101 EINA_SAFETY_ON_NULL_RETURN_VAL(element, 0);
1103 i = --element->_references;
1105 e_connman_element_free(element);
1107 ERR("element %p references %d < 0", element, i);
1113 * Send message with callbacks set to work with connman elements.
1115 * If this call fails (returns @c EINA_FALSE), pending callbacks will not be called,
1116 * not even with error messages.
1118 * @return @c EINA_TRUE on success, @c EINA_FALSE on failure.
1121 e_connman_element_message_send(E_Connman_Element *element, const char *method_name, E_DBus_Method_Return_Cb cb, DBusMessage *msg, Eina_Inlist **pending, E_DBus_Method_Return_Cb user_cb, const void *user_data)
1123 E_Connman_Element_Call_Data *data;
1124 E_Connman_Element_Pending *p;
1126 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1127 EINA_SAFETY_ON_NULL_RETURN_VAL(method_name, EINA_FALSE);
1128 EINA_SAFETY_ON_NULL_RETURN_VAL(pending, EINA_FALSE);
1129 EINA_SAFETY_ON_NULL_RETURN_VAL(msg, EINA_FALSE);
1131 data = malloc(sizeof(*data));
1134 ERR("could not alloc e_connman_element_call_data: %s",
1136 dbus_message_unref(msg);
1140 p = malloc(sizeof(*p));
1143 ERR("could not alloc E_Connman_Element_Pending: %s",
1146 dbus_message_unref(msg);
1150 data->element = element;
1153 data->p_list = pending;
1154 p->user_cb = user_cb;
1155 p->user_data = (void *)user_data;
1157 p->pending = e_dbus_message_send
1158 (e_connman_conn, msg, e_connman_element_call_dispatch_and_free, -1, data);
1159 dbus_message_unref(msg);
1163 *pending = eina_inlist_append(*pending, EINA_INLIST_GET(p));
1167 ERR("failed to call %s (obj=%s, path=%s, iface=%s)",
1168 method_name, e_connman_system_bus_name_get(),
1169 element->path, element->interface);
1176 e_connman_element_call_full(E_Connman_Element *element, const char *method_name, E_DBus_Method_Return_Cb cb, Eina_Inlist **pending, E_DBus_Method_Return_Cb user_cb, const void *user_data)
1180 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1181 EINA_SAFETY_ON_NULL_RETURN_VAL(method_name, EINA_FALSE);
1182 EINA_SAFETY_ON_NULL_RETURN_VAL(pending, EINA_FALSE);
1184 msg = dbus_message_new_method_call
1185 (e_connman_system_bus_name_get(), element->path, element->interface,
1188 return e_connman_element_message_send
1189 (element, method_name, cb, msg, pending, user_cb, user_data);
1193 _e_connman_element_property_value_add(E_Connman_Element *element, const char *name, int type, void *value)
1195 E_Connman_Element_Property *p;
1197 name = eina_stringshare_add(name);
1198 EINA_INLIST_FOREACH(element->props, p)
1200 if (p->name == name)
1202 eina_stringshare_del(name);
1203 return _e_connman_element_property_update(p, type, value);
1207 p = _e_connman_element_property_new(name, type, value);
1210 ERR("could not create property %s (%c)", name, type);
1214 element->props = eina_inlist_append(element->props, EINA_INLIST_GET(p));
1218 static E_Connman_Array *
1219 _e_connman_element_iter_get_array(DBusMessageIter *itr, const char *key)
1221 E_Connman_Array *array;
1222 DBusMessageIter e_itr;
1224 array = malloc(sizeof(E_Connman_Array));
1227 ERR("could not create new e_connman array.");
1231 array->array = eina_array_new(16);
1232 if (!(array->array))
1234 ERR("could not create new eina array.");
1239 dbus_message_iter_recurse(itr, &e_itr);
1240 array->type = dbus_message_iter_get_arg_type(&e_itr);
1241 if (array->type == DBUS_TYPE_INVALID)
1243 DBG("array %s is of type 'invalid' (empty?)", key);
1244 eina_array_free(array->array);
1251 switch (array->type)
1253 case DBUS_TYPE_OBJECT_PATH:
1257 dbus_message_iter_get_basic(&e_itr, &path);
1258 path = eina_stringshare_add(path);
1259 eina_array_push(array->array, path);
1260 _e_connman_element_item_register(key, path);
1264 case DBUS_TYPE_STRING:
1268 dbus_message_iter_get_basic(&e_itr, &str);
1269 str = eina_stringshare_add(str);
1270 eina_array_push(array->array, str);
1274 case DBUS_TYPE_BYTE:
1277 dbus_message_iter_get_basic(&e_itr, &byte);
1278 eina_array_push(array->array, (void *)(long)byte);
1282 case DBUS_TYPE_DICT_ENTRY:
1284 E_Connman_Element_Dict_Entry *entry;
1285 entry = _e_connman_element_dict_entry_new(&e_itr);
1287 eina_array_push(array->array, entry);
1292 ERR("don't know how to build array '%s' of type %c (%d)",
1293 key, array->type, array->type);
1294 eina_array_free(array->array);
1299 while (dbus_message_iter_next(&e_itr));
1304 _e_connman_element_get_properties_callback(void *user_data, DBusMessage *msg, DBusError *err)
1306 E_Connman_Element *element = user_data;
1307 DBusMessageIter itr, s_itr;
1310 DBG("get_properties msg=%p", msg);
1312 if (!_dbus_callback_check_and_init(msg, &itr, err))
1315 t = dbus_message_iter_get_arg_type(&itr);
1316 if (!_dbus_iter_type_check(t, DBUS_TYPE_ARRAY))
1320 dbus_message_iter_recurse(&itr, &s_itr);
1323 DBusMessageIter e_itr, v_itr;
1328 t = dbus_message_iter_get_arg_type(&s_itr);
1329 if (!_dbus_iter_type_check(t, DBUS_TYPE_DICT_ENTRY))
1332 dbus_message_iter_recurse(&s_itr, &e_itr);
1334 t = dbus_message_iter_get_arg_type(&e_itr);
1335 if (!_dbus_iter_type_check(t, DBUS_TYPE_STRING))
1338 dbus_message_iter_get_basic(&e_itr, &key);
1339 dbus_message_iter_next(&e_itr);
1340 t = dbus_message_iter_get_arg_type(&e_itr);
1341 if (!_dbus_iter_type_check(t, DBUS_TYPE_VARIANT))
1344 dbus_message_iter_recurse(&e_itr, &v_itr);
1345 t = dbus_message_iter_get_arg_type(&v_itr);
1346 if (t == DBUS_TYPE_ARRAY)
1348 value = _e_connman_element_iter_get_array(&v_itr, key);
1350 else if (t != DBUS_TYPE_INVALID)
1352 dbus_message_iter_get_basic(&v_itr, &value);
1356 ERR("property has invalid type %s", key);
1360 r = _e_connman_element_property_value_add(element, key, t, value);
1363 ERR("failed to add property value %s (%c)", key, t);
1367 INF("property value changed %s (%c)", key, t);
1371 while (dbus_message_iter_next(&s_itr));
1374 _e_connman_element_listeners_call(element);
1378 * Sync element properties with server.
1380 * Call method GetProperties() at the given element on server in order to sync
1383 * @param element to call method on server.
1384 * @param cb function to call when server replies or some error happens.
1385 * @param data data to give to cb when it is called.
1387 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1390 e_connman_element_sync_properties_full(E_Connman_Element *element, E_DBus_Method_Return_Cb cb, const void *data)
1392 const char name[] = "GetProperties";
1394 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1395 return e_connman_element_call_full
1396 (element, name, _e_connman_element_get_properties_callback,
1397 &element->_pending.properties_get, cb, data);
1401 * Sync element properties with server, simple version.
1403 * Call method GetProperties() at the given element on server in order to sync
1404 * them. This is the simple version and there is no check of server reply
1407 * @param element to call method on server.
1409 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1412 e_connman_element_properties_sync(E_Connman_Element *element)
1414 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1415 return e_connman_element_sync_properties_full(element, NULL, NULL);
1419 * Call method SetProperty(prop, {key: value}) at the given element on server.
1421 * This is a server call, not local, so it may fail and in that case
1422 * no property is updated locally. If the value was set the event
1423 * E_CONNMAN_EVENT_ELEMENT_UPDATED will be added to main loop.
1425 * @param element to call method on server.
1426 * @param prop property name.
1427 * @param key dict key name.
1428 * @param type DBus type to use for value.
1429 * @param value pointer to value, just like regular DBus, see
1430 * dbus_message_iter_append_basic().
1431 * @param cb function to call when server replies or some error happens.
1432 * @param data data to give to cb when it is called.
1434 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1437 e_connman_element_property_dict_set_full(E_Connman_Element *element, const char *prop, const char *key, int type, const void *value, E_DBus_Method_Return_Cb cb, const void *data)
1439 const char name[] = "SetProperty";
1441 DBusMessageIter itr, variant, dict, entry;
1444 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1445 EINA_SAFETY_ON_NULL_RETURN_VAL(prop, EINA_FALSE);
1447 msg = dbus_message_new_method_call
1448 (e_connman_system_bus_name_get(), element->path, element->interface, name);
1453 dbus_message_iter_init_append(msg, &itr);
1454 dbus_message_iter_append_basic(&itr, DBUS_TYPE_STRING, &prop);
1456 if ((size_t)snprintf(typestr, sizeof(typestr),
1457 (DBUS_TYPE_ARRAY_AS_STRING
1458 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1459 DBUS_TYPE_STRING_AS_STRING
1461 DBUS_DICT_ENTRY_END_CHAR_AS_STRING),
1462 type) >= sizeof(typestr))
1464 ERR("sizeof(typestr) is too small!");
1468 dbus_message_iter_open_container(&itr, DBUS_TYPE_VARIANT, typestr, &variant);
1470 snprintf(typestr, sizeof(typestr),
1471 (DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1472 DBUS_TYPE_STRING_AS_STRING
1474 DBUS_DICT_ENTRY_END_CHAR_AS_STRING),
1477 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY, typestr, &dict);
1478 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &entry);
1480 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1482 if ((type == DBUS_TYPE_STRING) || (type == DBUS_TYPE_OBJECT_PATH))
1483 dbus_message_iter_append_basic(&entry, type, &value);
1485 dbus_message_iter_append_basic(&entry, type, value);
1487 dbus_message_iter_close_container(&dict, &entry);
1488 dbus_message_iter_close_container(&variant, &dict);
1489 dbus_message_iter_close_container(&itr, &variant);
1491 return e_connman_element_message_send
1492 (element, name, NULL, msg, &element->_pending.property_set, cb, data);
1496 * Call method SetProperty(prop, value) at the given element on server.
1498 * This is a server call, not local, so it may fail and in that case
1499 * no property is updated locally. If the value was set the event
1500 * E_CONNMAN_EVENT_ELEMENT_UPDATED will be added to main loop.
1502 * @param element to call method on server.
1503 * @param prop property name.
1504 * @param type DBus type to use for value.
1505 * @param value pointer to value, just like regular DBus, see
1506 * dbus_message_iter_append_basic().
1507 * @param cb function to call when server replies or some error happens.
1508 * @param data data to give to cb when it is called.
1510 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1513 e_connman_element_property_set_full(E_Connman_Element *element, const char *prop, int type, const void *value, E_DBus_Method_Return_Cb cb, const void *data)
1515 const char name[] = "SetProperty";
1519 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1520 EINA_SAFETY_ON_NULL_RETURN_VAL(prop, EINA_FALSE);
1522 msg = dbus_message_new_method_call
1523 (e_connman_system_bus_name_get(), element->path, element->interface, name);
1528 DBusMessageIter itr, v;
1529 dbus_message_iter_init_append(msg, &itr);
1530 dbus_message_iter_append_basic(&itr, DBUS_TYPE_STRING, &prop);
1534 dbus_message_iter_open_container(&itr, DBUS_TYPE_VARIANT, typestr, &v);
1535 if ((type == DBUS_TYPE_STRING) || (type == DBUS_TYPE_OBJECT_PATH))
1537 dbus_message_iter_append_basic(&v, type, &value);
1539 else if (type == DBUS_TYPE_BOOLEAN)
1541 unsigned int b = *(char *)value;
1542 dbus_message_iter_append_basic(&v, type, &b);
1546 dbus_message_iter_append_basic(&v, type, value);
1549 dbus_message_iter_close_container(&itr, &v);
1551 return e_connman_element_message_send
1552 (element, name, NULL, msg, &element->_pending.property_set, cb, data);
1556 * Call method SetProperty(prop, value) at the given element on server, when
1557 * value is an array.
1559 * This is a server call, not local, so it may fail and in that case
1560 * no property is updated locally. If the value was set the event
1561 * E_CONNMAN_EVENT_ELEMENT_UPDATED will be added to main loop.
1563 * @param element to call method on server.
1564 * @param prop property name.
1565 * @param type DBus type to use for value.
1566 * @param value pointer to value, just like regular DBus, see
1567 * dbus_message_iter_append_basic().
1568 * @param cb function to call when server replies or some error happens.
1569 * @param data data to give to cb when it is called.
1571 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1574 e_connman_element_property_array_set_full(E_Connman_Element *element, const char *prop, int type, unsigned int count, const void * const *values, E_DBus_Method_Return_Cb cb, const void *data)
1576 const char name[] = "SetProperty";
1577 char type_sig[2] = { type, '\0'};
1578 char array_sig[3] = { DBUS_TYPE_ARRAY, type, '\0' };
1580 DBusMessageIter itr, variant, array;
1582 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1583 EINA_SAFETY_ON_NULL_RETURN_VAL(prop, EINA_FALSE);
1585 msg = dbus_message_new_method_call
1586 (e_connman_system_bus_name_get(), element->path, element->interface, name);
1591 dbus_message_iter_init_append(msg, &itr);
1592 dbus_message_iter_append_basic(&itr, DBUS_TYPE_STRING, &prop);
1594 dbus_message_iter_open_container(&itr, DBUS_TYPE_VARIANT, array_sig,
1596 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY, type_sig,
1599 if (type == DBUS_TYPE_STRING || type == DBUS_TYPE_OBJECT_PATH)
1602 for (i = 0; i < count; i++)
1604 const void *entry = values[i];
1605 dbus_message_iter_append_basic(&array, type, &entry);
1611 for (i = 0; i < count; i++)
1613 const void *entry = values[i];
1614 dbus_message_iter_append_basic(&array, type, entry);
1618 dbus_message_iter_close_container(&variant, &array);
1619 dbus_message_iter_close_container(&itr, &variant);
1621 return e_connman_element_message_send(element, name, NULL, msg,
1622 &element->_pending.property_set,
1627 * Call method SetProperty(prop, value) at the given element on server.
1629 * This is the simple version and there is no check of server reply
1632 * @param element to call method on server.
1633 * @param prop property name.
1634 * @param type DBus type to use for value.
1635 * @param value pointer to value, just like regular DBus, see
1636 * dbus_message_iter_append_basic().
1638 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1641 e_connman_element_property_set(E_Connman_Element *element, const char *prop, int type, const void *value)
1643 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1644 EINA_SAFETY_ON_NULL_RETURN_VAL(prop, EINA_FALSE);
1645 return e_connman_element_property_set_full
1646 (element, prop, type, value, NULL, NULL);
1650 e_connman_element_call_with_path(E_Connman_Element *element, const char *method_name, const char *string, E_DBus_Method_Return_Cb cb, Eina_Inlist **pending, E_DBus_Method_Return_Cb user_cb, const void *user_data)
1652 DBusMessageIter itr;
1655 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1656 EINA_SAFETY_ON_NULL_RETURN_VAL(method_name, EINA_FALSE);
1657 EINA_SAFETY_ON_NULL_RETURN_VAL(string, EINA_FALSE);
1658 EINA_SAFETY_ON_NULL_RETURN_VAL(pending, EINA_FALSE);
1660 msg = dbus_message_new_method_call
1661 (e_connman_system_bus_name_get(), element->path, element->interface,
1667 dbus_message_iter_init_append(msg, &itr);
1668 dbus_message_iter_append_basic(&itr, DBUS_TYPE_OBJECT_PATH, &string);
1670 return e_connman_element_message_send
1671 (element, method_name, cb, msg, pending, user_cb, user_data);
1675 e_connman_element_call_with_string(E_Connman_Element *element, const char *method_name, const char *string, E_DBus_Method_Return_Cb cb, Eina_Inlist **pending, E_DBus_Method_Return_Cb user_cb, const void *user_data)
1677 DBusMessageIter itr;
1680 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1681 EINA_SAFETY_ON_NULL_RETURN_VAL(method_name, EINA_FALSE);
1682 EINA_SAFETY_ON_NULL_RETURN_VAL(string, EINA_FALSE);
1683 EINA_SAFETY_ON_NULL_RETURN_VAL(pending, EINA_FALSE);
1685 msg = dbus_message_new_method_call
1686 (e_connman_system_bus_name_get(), element->path, element->interface,
1692 dbus_message_iter_init_append(msg, &itr);
1693 dbus_message_iter_append_basic(&itr, DBUS_TYPE_STRING, &string);
1695 return e_connman_element_message_send
1696 (element, method_name, cb, msg, pending, user_cb, user_data);
1700 * Get property type.
1702 * If @c EINA_FALSE is returned, then this call failed and parameter-returned
1703 * values shall be considered invalid.
1705 * @param element which element to get the property
1706 * @param name property name, must be previously stringshared
1707 * @param type will contain the value type.
1709 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1712 e_connman_element_property_type_get_stringshared(const E_Connman_Element *element, const char *name, int *type)
1714 const E_Connman_Element_Property *p;
1716 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1717 EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE);
1718 EINA_SAFETY_ON_NULL_RETURN_VAL(type, EINA_FALSE);
1720 EINA_INLIST_FOREACH(element->props, p)
1722 if (p->name == name)
1729 WRN("element %s (%p) has no property with name \"%s\".",
1730 element->path, element, name);
1735 * Get property type.
1737 * If @c EINA_FALSE is returned, then this call failed and parameter-returned
1738 * values shall be considered invalid.
1740 * @param element which element to get the property
1741 * @param name property name
1742 * @param type will contain the value type.
1744 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1747 e_connman_element_property_type_get(const E_Connman_Element *element, const char *name, int *type)
1750 name = eina_stringshare_add(name);
1751 ret = e_connman_element_property_type_get_stringshared(element, name, type);
1752 eina_stringshare_del(name);
1757 e_connman_element_list_properties(const E_Connman_Element *element, Eina_Bool (*cb)(void *data, const E_Connman_Element *element, const char *name, int type, const void *value), const void *data)
1759 const E_Connman_Element_Property *p;
1761 EINA_SAFETY_ON_NULL_RETURN(element);
1762 EINA_SAFETY_ON_NULL_RETURN(cb);
1764 EINA_INLIST_FOREACH(element->props, p)
1766 const void *value = NULL;
1770 case DBUS_TYPE_STRING:
1771 value = &p->value.str;
1774 case DBUS_TYPE_OBJECT_PATH:
1775 value = &p->value.path;
1778 case DBUS_TYPE_BOOLEAN:
1779 value = (void *)&p->value.boolean;
1782 case DBUS_TYPE_UINT16:
1783 value = &p->value.u16;
1786 case DBUS_TYPE_UINT32:
1787 value = &p->value.u32;
1791 ERR("unsupported type %c", p->type);
1794 if (!cb((void *)data, element, p->name, p->type, value))
1800 * Get dict value given its key inside a dict property.
1802 * This will look into properties for one of type dict that contains
1803 * the given key, to find the property. If no property is found then
1804 * @c EINA_FALSE is returned.
1806 * If @c EINA_FALSE is returned, then this call failed and parameter-returned
1807 * values shall be considered invalid.
1809 * @param element which element to get the property
1810 * @param dict_name property name, must be previously stringshared
1811 * @param key key inside dict, must be previously stringshared
1812 * @param type if provided it will contain the value type.
1813 * @param value where to store the property value, must be a pointer to the
1814 * exact type, (Eina_Bool *) for booleans, (char **) for strings, and so on.
1816 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1819 e_connman_element_property_dict_get_stringshared(const E_Connman_Element *element, const char *dict_name, const char *key, int *type, void *value)
1821 const E_Connman_Element_Property *p;
1823 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1824 EINA_SAFETY_ON_NULL_RETURN_VAL(dict_name, EINA_FALSE);
1825 EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
1826 EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE);
1828 EINA_INLIST_FOREACH(element->props, p)
1830 E_Connman_Element_Dict_Entry *entry;
1831 E_Connman_Array *array;
1833 if (p->name != dict_name)
1836 if (p->type != DBUS_TYPE_ARRAY)
1838 WRN("element %s (%p) has property \"%s\" is not an array: %c (%d)",
1839 element->path, element, dict_name, p->type, p->type);
1843 array = p->value.array;
1844 if ((!array) || (array->type != DBUS_TYPE_DICT_ENTRY))
1846 int t = array ? array->type : DBUS_TYPE_INVALID;
1847 WRN("element %s (%p) has property \"%s\" is not a dict: %c (%d)",
1848 element->path, element, dict_name, t, t);
1852 entry = _e_connman_element_array_dict_find_stringshared(array, key);
1855 WRN("element %s (%p) has no dict property with name \"%s\" with "
1857 element->path, element, dict_name, key);
1862 *type = entry->type;
1864 switch (entry->type)
1866 case DBUS_TYPE_BOOLEAN:
1867 *(Eina_Bool *)value = entry->value.boolean;
1870 case DBUS_TYPE_BYTE:
1871 *(unsigned char *)value = entry->value.byte;
1874 case DBUS_TYPE_UINT16:
1875 *(unsigned short *)value = entry->value.u16;
1878 case DBUS_TYPE_UINT32:
1879 *(unsigned int *)value = entry->value.u32;
1882 case DBUS_TYPE_STRING:
1883 *(const char **)value = entry->value.str;
1886 case DBUS_TYPE_OBJECT_PATH:
1887 *(const char **)value = entry->value.path;
1890 case DBUS_TYPE_ARRAY:
1891 *(E_Connman_Array **)value = entry->value.array;
1895 ERR("don't know how to get property %s, key %s type %c (%d)",
1896 dict_name, key, entry->type, entry->type);
1901 DBG("element %s (%p) has no property with name \"%s\".",
1902 element->path, element, dict_name);
1907 e_connman_element_property_dict_strings_array_get_stringshared(const E_Connman_Element *element, const char *dict_name, const char *key, unsigned int *count, const char ***strings)
1909 const char **ret, **p;
1910 Eina_Array_Iterator iterator;
1911 E_Connman_Array *array;
1916 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1917 EINA_SAFETY_ON_NULL_RETURN_VAL(dict_name, EINA_FALSE);
1918 EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
1919 EINA_SAFETY_ON_NULL_RETURN_VAL(count, EINA_FALSE);
1920 EINA_SAFETY_ON_NULL_RETURN_VAL(strings, EINA_FALSE);
1925 if (!e_connman_element_property_dict_get_stringshared(element, dict_name,
1926 key, &type, &array))
1929 if (type != DBUS_TYPE_ARRAY)
1931 ERR("property %s.%s is not an array!", dict_name, key);
1935 if ((!array) || (!array->array) || (array->type == DBUS_TYPE_INVALID))
1938 if (array->type != DBUS_TYPE_STRING)
1940 ERR("property %s.%s is not an array of strings!", dict_name, key);
1944 *count = eina_array_count_get(array->array);
1945 ret = malloc(*count * sizeof(char *));
1948 ERR("could not allocate return array of %d strings: %s",
1949 *count, strerror(errno));
1956 EINA_ARRAY_ITER_NEXT(array->array, i, item, iterator)
1970 * Get property value given its name.
1972 * This will look into properties, to find the property.
1973 * If no property is found then @c EINA_FALSE is returned.
1975 * If @c EINA_FALSE is returned, then this call failed and parameter-returned
1976 * values shall be considered invalid.
1978 * @param element which element to get the property
1979 * @param name property name, must be previously stringshared
1980 * @param type if provided it will contain the value type.
1981 * @param value where to store the property value, must be a pointer to the
1982 * exact type, (Eina_Bool *) for booleans, (char **) for strings, and so on.
1984 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
1987 e_connman_element_property_get_stringshared(const E_Connman_Element *element, const char *name, int *type, void *value)
1989 const E_Connman_Element_Property *p;
1991 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
1992 EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE);
1993 EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE);
1995 EINA_INLIST_FOREACH(element->props, p)
1997 if (p->name != name)
2005 case DBUS_TYPE_BOOLEAN:
2006 *(Eina_Bool *)value = p->value.boolean;
2009 case DBUS_TYPE_BYTE:
2010 *(unsigned char *)value = p->value.byte;
2013 case DBUS_TYPE_UINT16:
2014 *(unsigned short *)value = p->value.u16;
2017 case DBUS_TYPE_UINT32:
2018 *(unsigned int *)value = p->value.u32;
2021 case DBUS_TYPE_STRING:
2022 *(const char **)value = p->value.str;
2025 case DBUS_TYPE_OBJECT_PATH:
2026 *(const char **)value = p->value.path;
2029 case DBUS_TYPE_ARRAY:
2030 *(E_Connman_Array **)value = p->value.array;
2034 ERR("don't know how to get property type %c (%d)",
2040 WRN("element %s (%p) has no property with name \"%s\".",
2041 element->path, element, name);
2046 * Get property value given its name.
2048 * This will look into properties, to find the property.
2049 * If no property is found then @c EINA_FALSE is returned.
2051 * If @c EINA_FALSE is returned, then this call failed and parameter-returned
2052 * values shall be considered invalid.
2054 * @param element which element to get the property
2055 * @param name property name
2056 * @param type if provided it will contain the value type.
2057 * @param value where to store the property value, must be a pointer to the
2058 * exact type, (Eina_Bool *) for booleans, (char **) for strings, and so on.
2060 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
2063 e_connman_element_property_get(const E_Connman_Element *element, const char *name, int *type, void *value)
2066 name = eina_stringshare_add(name);
2067 ret = e_connman_element_property_get_stringshared
2068 (element, name, type, value);
2069 eina_stringshare_del(name);
2073 struct e_connman_elements_for_each_data
2075 Eina_Hash_Foreach cb;
2080 _e_connman_elements_for_each(Eina_Hash *hash __UNUSED__, const char *key, void *data, void *fdata)
2082 struct e_connman_elements_for_each_data *each_data = fdata;
2084 each_data->cb(elements, key, data, each_data->data);
2089 * Call the given function for each existing element.
2091 * @param cb function to call for each element. It will get as parameters,
2092 * in order: the element pointer and the given @a user_data.
2093 * @param user_data data to give to @a cb for each element.
2096 e_connman_elements_for_each(Eina_Hash_Foreach cb, const void *user_data)
2098 struct e_connman_elements_for_each_data data = {cb, (void *)user_data};
2100 EINA_SAFETY_ON_NULL_RETURN(cb);
2102 eina_hash_foreach(elements, (Eina_Hash_Foreach)_e_connman_elements_for_each,
2107 _e_connman_elements_get_allocate(unsigned int *count, E_Connman_Element ***p_elements)
2109 *count = eina_hash_population(elements);
2116 *p_elements = malloc(*count * sizeof(E_Connman_Element *));
2119 ERR("could not allocate return array of %d elements: %s",
2120 *count, strerror(errno));
2129 _e_connman_elements_get_all(Eina_Hash *hash __UNUSED__, const char *key __UNUSED__, void *data, void *fdata)
2131 E_Connman_Element *element = data;
2132 E_Connman_Element ***p_ret = fdata;
2140 * Get all known elements.
2142 * No reference is added to these elements, since there are no threads
2143 * in the system, you are free to add references yourself right after
2144 * the return of this call without race condition, elements by the
2145 * system (ie: elementRemoved signal)could only be touched on the next
2146 * main loop iteration.
2148 * @param count return the number of elements in array.
2149 * @param p_elements array with all elements, these are not referenced
2150 * and in no particular order, just set if return is @c EINA_TRUE.
2152 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
2155 e_connman_elements_get_all(unsigned int *count, E_Connman_Element ***p_elements)
2157 E_Connman_Element **p;
2159 EINA_SAFETY_ON_NULL_RETURN_VAL(count, EINA_FALSE);
2160 EINA_SAFETY_ON_NULL_RETURN_VAL(p_elements, EINA_FALSE);
2162 if (!_e_connman_elements_get_allocate(count, p_elements))
2166 eina_hash_foreach(elements, (Eina_Hash_Foreach)_e_connman_elements_get_all,
2171 struct e_connman_elements_get_all_str_data
2173 E_Connman_Element **elements;
2179 _e_connman_elements_get_all_type(Eina_Hash *hash __UNUSED__, const char *key __UNUSED__, void *e, void *user_data)
2181 struct e_connman_elements_get_all_str_data *data = user_data;
2182 E_Connman_Element *element = e;
2184 if ((data->str) && (element->interface != data->str))
2187 data->elements[data->count] = element;
2193 * Get all known elements of type.
2195 * No reference is added to these elements, since there are no threads
2196 * in the system, you are free to add references yourself right after
2197 * the return of this call without race condition, elements by the
2198 * system (ie: ElementRemoved signal) could only be touched on the next
2199 * main loop iteration.
2201 * @param type type to filter, or NULL to get all.
2202 * @param count return the number of elements in array.
2203 * @param p_elements array with all elements, these are not referenced
2204 * and in no particular order, just set if return is @c EINA_TRUE.
2206 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
2208 * @see e_connman_elements_get_all()
2211 e_connman_elements_get_all_type(const char *type, unsigned int *count, E_Connman_Element ***p_elements)
2213 struct e_connman_elements_get_all_str_data data;
2215 EINA_SAFETY_ON_NULL_RETURN_VAL(count, EINA_FALSE);
2216 EINA_SAFETY_ON_NULL_RETURN_VAL(p_elements, EINA_FALSE);
2218 if (!_e_connman_elements_get_allocate(count, p_elements))
2221 data.elements = *p_elements;
2223 data.str = eina_stringshare_add(type);
2224 eina_hash_foreach(elements,
2225 (Eina_Hash_Foreach)_e_connman_elements_get_all_type,
2228 eina_stringshare_del(data.str);
2229 *count = data.count;
2234 * Get the element registered at given path.
2236 * @param path the path to query for registered object.
2238 * @return element pointer if found, NULL otherwise. No references are added.
2241 e_connman_element_get(const char *path)
2243 E_Connman_Element *element;
2245 EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);
2246 element = eina_hash_find(elements, path);
2252 _e_connman_element_property_changed_callback(void *data, DBusMessage *msg)
2254 E_Connman_Element *element = (E_Connman_Element *)data;
2255 DBusMessageIter itr, v_itr;
2256 int t, r, changed = 0;
2257 const char *name = NULL;
2260 DBG("Property changed in element %s", element->path);
2262 if (!_dbus_callback_check_and_init(msg, &itr, NULL))
2265 t = dbus_message_iter_get_arg_type(&itr);
2266 if (!_dbus_iter_type_check(t, DBUS_TYPE_STRING))
2268 ERR("missing name in property changed signal");
2272 dbus_message_iter_get_basic(&itr, &name);
2274 dbus_message_iter_next(&itr);
2275 t = dbus_message_iter_get_arg_type(&itr);
2276 if (!_dbus_iter_type_check(t, DBUS_TYPE_VARIANT))
2278 ERR("missing value in property changed signal");
2282 dbus_message_iter_recurse(&itr, &v_itr);
2283 t = dbus_message_iter_get_arg_type(&v_itr);
2285 if (t == DBUS_TYPE_ARRAY)
2287 value = _e_connman_element_iter_get_array(&v_itr, name);
2289 else if (t != DBUS_TYPE_INVALID)
2291 dbus_message_iter_get_basic(&v_itr, &value);
2295 ERR("property has invalid type %s", name);
2299 r = _e_connman_element_property_value_add(element, name, t, value);
2302 ERR("failed to add property value %s (%c)", name, t);
2306 INF("property value changed %s (%c)", name, t);
2311 _e_connman_element_listeners_call(element);
2315 * Register the given path, possible creating and element and return it.
2317 * This will check if path is already registered, in that case the
2318 * exiting element is returned. If it was not registered yet, a new
2319 * element is created, registered and returned.
2321 * This call will not add extra references to the object.
2323 * @param path the path to register the element
2325 * @return the registered object, no references are added.
2328 e_connman_element_register(const char *path, const char *interface)
2330 E_Connman_Element *element;
2332 EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);
2333 EINA_SAFETY_ON_NULL_RETURN_VAL(interface, NULL);
2335 element = eina_hash_find(elements, path);
2339 element = e_connman_element_new(path, interface);
2343 if (!eina_hash_add(elements, element->path, element))
2345 ERR("could not add element %s to hash, delete it.", path);
2346 e_connman_element_free(element);
2350 element->signal_handler =
2351 e_dbus_signal_handler_add
2352 (e_connman_conn, e_connman_system_bus_name_get(),
2353 element->path, element->interface, "PropertyChanged",
2354 _e_connman_element_property_changed_callback, element);
2356 e_connman_element_event_add(E_CONNMAN_EVENT_ELEMENT_ADD, element);
2362 _e_connman_element_event_unregister_and_free(void *data __UNUSED__, void *ev)
2364 E_Connman_Element *element = ev;
2365 e_connman_element_unref(element);
2369 _e_connman_element_unregister_internal(E_Connman_Element *element)
2371 if (element->signal_handler)
2373 e_dbus_signal_handler_del(e_connman_conn, element->signal_handler);
2374 element->signal_handler = NULL;
2377 ecore_event_add(E_CONNMAN_EVENT_ELEMENT_DEL, element,
2378 _e_connman_element_event_unregister_and_free, NULL);
2382 * Forget about the given element.
2384 * This will remove the element from the pool of known objects, then
2385 * add an E_CONNMAN_EVENT_ELEMENT_DEL and after that will unreference it,
2386 * possible freeing it.
2388 * @param element element to forget about. Its reference will be removed.
2391 e_connman_element_unregister(E_Connman_Element *element)
2397 eina_hash_del_by_key(elements, element->path);
2401 * Remove all known elements.
2403 * This will remove all known elements but will NOT add any
2404 * E_CONNMAN_EVENT_ELEMENT_DEL to main loop.
2406 * This is just useful to make sure next e_connman_manager_sync_elements()
2407 * will not leave any stale elements. This is unlikely to happen, as
2408 * E_Connman is supposed to catch all required events to avoid stale elements.
2411 e_connman_manager_clear_elements(void)
2413 e_connman_elements_shutdown();
2414 e_connman_elements_init();
2418 * Creates elements hash.
2420 * This has no init counter since its already guarded by other code.
2424 e_connman_elements_init(void)
2426 EINA_SAFETY_ON_FALSE_RETURN(!elements);
2428 eina_hash_string_superfast_new(EINA_FREE_CB
2429 (_e_connman_element_unregister_internal));
2433 e_connman_elements_shutdown(void)
2435 EINA_SAFETY_ON_FALSE_RETURN(!!elements);
2436 eina_hash_free(elements);
2440 static inline Eina_Bool
2441 _e_connman_element_is(const E_Connman_Element *element, const char *interface)
2443 return element->interface == interface;
2447 e_connman_element_is_manager(const E_Connman_Element *element)
2449 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
2450 return _e_connman_element_is(element, e_connman_iface_manager);
2454 e_connman_element_is_profile(const E_Connman_Element *element)
2456 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
2457 return _e_connman_element_is(element, e_connman_iface_profile);
2461 e_connman_element_is_service(const E_Connman_Element *element)
2463 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
2464 return _e_connman_element_is(element, e_connman_iface_service);
2468 e_connman_element_is_technology(const E_Connman_Element *element)
2470 EINA_SAFETY_ON_NULL_RETURN_VAL(element, EINA_FALSE);
2471 return _e_connman_element_is(element, e_connman_iface_technology);