complex-types-client-eina-value.c

Client to test complex types (arrays, structs, dicts) against a provided server (complex-types-server.c) returning them as Eina_Value to/from Message.

//Compile with:
// gcc -o complex-types-client-eina-value complex-types-client-eina-value.c `pkg-config --cflags --libs eldbus ecore eina`
#include "Eldbus.h"
#include <Ecore.h>
#define BUS "com.profusion"
#define PATH "/com/profusion/Test"
#define IFACE "com.profusion.Test"
#define size_of_array 5
static const char *array_string[] = {
"aaaa", "bbbb", "cccc", "dddd", "eeee"
};
typedef struct _sub_struct
{
char *txt;
int num;
} sub_struct;
typedef struct _main_struct
{
int size;
sub_struct array[];
} main_struct;
static unsigned int
_type_offset(unsigned base, unsigned size)
{
unsigned padding;
if (!(base % size))
return base;
padding = base - size;
return base + padding;
}
static void
_fill_receive_array_of_string_int_with_size(Eldbus_Message *msg, int size, const char *array[])
{
Eina_Value *value_struct, *value_array;
int i;
unsigned offset;
Eina_Value_Struct_Member main_members[2];
Eina_Value_Struct_Member sub_members[] = {
EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_STRING, sub_struct, txt),
EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT, sub_struct, num)
};
Eina_Value_Struct_Desc desc_sub_struct = {
EINA_VALUE_STRUCT_DESC_VERSION,
NULL, // no special operations
sub_members,
2,
sizeof(sub_struct)
};
Eina_Value_Struct_Desc desc_struct = {
EINA_VALUE_STRUCT_DESC_VERSION,
NULL, // no special operations
main_members,
2,
0//will be set below
};
offset = _type_offset(sizeof(int), sizeof(Eina_Value_Array));
main_members[0].name = "size";
main_members[0].type = EINA_VALUE_TYPE_INT;
main_members[0].offset = 0;
main_members[1].name = "array";
main_members[1].type = EINA_VALUE_TYPE_ARRAY;
main_members[1].offset = offset;
desc_struct. size = offset + sizeof(Eina_Value_Array);
value_struct = eina_value_struct_new(&desc_struct);
eina_value_struct_set(value_struct, "size", size);
value_array = eina_value_array_new(EINA_VALUE_TYPE_STRUCT, size);
for (i = 0; i < size; i++)
{
Eina_Value *value_sub_struct = eina_value_struct_new(&desc_sub_struct);
Eina_Value_Struct st;
eina_value_struct_set(value_sub_struct, "txt", array[i]);
eina_value_struct_set(value_sub_struct, "num", i);
eina_value_get(value_sub_struct, &st);
eina_value_array_append(value_array, st);
eina_value_free(value_sub_struct);
}
eina_value_struct_value_set(value_struct, "array", value_array);
eldbus_message_from_eina_value("ia(si)", msg, value_struct);
eina_value_free(value_struct);
eina_value_free(value_array);
}
static void
on_send_array(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eina_Value *v, array;
const char *txt;
unsigned i;
printf("2 - on_send_array()\n");
if (eldbus_message_error_get(msg, NULL, NULL))
{
printf("Message error\n\n");
return;
}
v = eldbus_message_to_eina_value(msg);
eina_value_struct_value_get(v, "arg0", &array);
for (i = 0; i < eina_value_array_count(&array); i++)
{
eina_value_array_get(&array, i, &txt);
printf("\t%s\n", txt);
}
eina_value_free(v);
eina_value_flush(&array);
}
static void
on_receive_array_with_size(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname;
const char *errmsg;
printf("1 - on_receive_array_with_size()\n");
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
fprintf(stderr, "Error: %s %s\n", errname, errmsg);
}
}
static void
on_plus_one(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eina_Value *v;
int num2;
if (eldbus_message_error_get(msg, NULL, NULL))
{
printf("Message error\n\n");
return;
}
v = eldbus_message_to_eina_value(msg);
eina_value_struct_get(v, "arg0", &num2);
printf("3 - on_plus_one() %d\n", num2);
eina_value_free(v);
}
static void
receive_variant_cb(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eina_Value *v, variant, array;
unsigned i;
printf("4 - receive a variant with an array of strings\n");
if (eldbus_message_error_get(msg, NULL, NULL))
{
printf("Message error\n\n");
return;
}
v = eldbus_message_to_eina_value(msg);
eina_value_struct_value_get(v, "arg0", &variant);
eina_value_struct_value_get(&variant, "arg0", &array);
for (i = 0; i < eina_value_array_count(&array); i++)
{
const char *txt;
eina_value_array_get(&array, i, &txt);
printf("\t%s\n", txt);
}
eina_value_flush(&array);
eina_value_flush(&variant);
eina_value_free(v);
}
static void
_property_removed(void *data EINA_UNUSED, Eldbus_Proxy *proxy EINA_UNUSED, void *event_info)
{
Eldbus_Proxy_Event_Property_Removed *event = event_info;
printf("\nproperty removed: %s", event->name);
}
static void
_property_changed(void *data EINA_UNUSED, Eldbus_Proxy *proxy EINA_UNUSED, void *event_info)
{
Eldbus_Proxy_Event_Property_Changed *event = event_info;
const char *name;
const Eina_Value *value;
printf("\nproperty changed\n");
name = event->name;
value = event->value;
if (!strcmp(name, "text") || !strcmp(name, "Resp2"))
{
const char *txt;
eina_value_get(value, &txt);
printf("[%s] = %s\n", name, txt);
}
else if (!strcmp(name, "int32"))
{
int num;
eina_value_get(value, &num);
printf("[%s] = %d\n", name, num);
}
else if (!strcmp(name, "st"))
{
const char *txt;
eina_value_struct_get(value, "arg0", &txt);
printf("[%s] %s | ", name, txt);
eina_value_struct_get(value, "arg1", &txt);
printf("%s\n", txt);
}
}
static Eina_Bool
_read_cache(void *data)
{
Eldbus_Proxy *proxy = data;
const char *txt;
int num;
Eina_Value *v;
v = eldbus_proxy_property_local_get(proxy, "text");
if (v)
{
eina_value_get(v, &txt);
printf("Read cache: [txt] = %s\n", txt);
}
v = eldbus_proxy_property_local_get(proxy, "int32");
if (v)
{
eina_value_get(v, &num);
printf("Read cache: [int32] = %d\n", num);
}
v = eldbus_proxy_property_local_get(proxy, "st");
if (v)
{
eina_value_struct_get(v, "arg0", &txt);
printf("Read cache: [st] %s | ", txt);
eina_value_struct_get(v, "arg1", &txt);
printf("%s\n", txt);
}
return EINA_FALSE;
}
static void
_fill_plus_one(Eldbus_Message *msg, int num)
{
Eina_Value *v;
Eina_Value_Struct_Member main_members[] = {
{"num", EINA_VALUE_TYPE_INT, 0}
};
Eina_Value_Struct_Desc desc_struct = {
EINA_VALUE_STRUCT_DESC_VERSION,
NULL, // no special operations
main_members,
1,
sizeof(int)
};
v = eina_value_struct_new(&desc_struct);
eina_value_struct_set(v, "num", num);
eldbus_message_from_eina_value("i", msg, v);
eina_value_free(v);
}
int
main(void)
{
Eldbus_Connection *conn;
Eldbus_Object *obj;
Eldbus_Proxy *proxy;
Eldbus_Message *msg;
ecore_init();
eldbus_init();
conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
obj = eldbus_object_get(conn, BUS, PATH);
proxy = eldbus_proxy_get(obj, IFACE);
msg = eldbus_proxy_method_call_new(proxy, "ReceiveArrayOfStringIntWithSize");
_fill_receive_array_of_string_int_with_size(msg, size_of_array, array_string);
eldbus_proxy_send(proxy, msg, on_receive_array_with_size, NULL, -1);
eldbus_proxy_call(proxy, "SendArray", on_send_array, NULL, -1 , "");
msg = eldbus_proxy_method_call_new(proxy, "PlusOne");
_fill_plus_one(msg, 14);
eldbus_proxy_send(proxy, msg, on_plus_one, NULL, -1);
eldbus_proxy_event_callback_add(proxy,
ELDBUS_PROXY_EVENT_PROPERTY_CHANGED,
_property_changed, NULL);
eldbus_proxy_event_callback_add(proxy, ELDBUS_PROXY_EVENT_PROPERTY_REMOVED,
_property_removed, NULL);
eldbus_proxy_properties_monitor(proxy, EINA_TRUE);
ecore_timer_add(10, _read_cache, proxy);
eldbus_proxy_call(proxy, "ReceiveVariantData", receive_variant_cb, NULL, -1, "");
ecore_main_loop_begin();
eldbus_proxy_event_callback_del(proxy, ELDBUS_PROXY_EVENT_PROPERTY_CHANGED,
_property_changed, NULL);
eldbus_connection_unref(conn);
eldbus_shutdown();
ecore_shutdown();
return 0;
}
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
ecore_timer_add
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:189
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
eina_value_array_count
static unsigned int eina_value_array_count(const Eina_Value *value)
Queries number of elements in value of array type.
eina_value_array_append
static Eina_Bool eina_value_array_append(Eina_Value *value,...)
Appends a generic value in an array.
eina_value_array_get
static Eina_Bool eina_value_array_get(const Eina_Value *value, unsigned int position,...)
Gets the generic value from an array member.
eina_value_array_new
EINA_API Eina_Value * eina_value_array_new(const Eina_Value_Type *subtype, unsigned int step)
Creates generic value storage of type array.
Definition: eina_value.c:5763
Eina_Value_Array
struct _Eina_Value_Array Eina_Value_Array
Value type for EINA_VALUE_TYPE_ARRAY.
Definition: eina_value.h:1175
EINA_VALUE_TYPE_INT
EINA_API const Eina_Value_Type * EINA_VALUE_TYPE_INT
manages int type.
Definition: eina_value.c:5602
EINA_VALUE_TYPE_STRUCT
EINA_API const Eina_Value_Type * EINA_VALUE_TYPE_STRUCT
manages struct type.
Definition: eina_value.c:5614
EINA_VALUE_TYPE_ARRAY
EINA_API const Eina_Value_Type * EINA_VALUE_TYPE_ARRAY
manages array type.
Definition: eina_value.c:5609
EINA_VALUE_TYPE_STRING
EINA_API const Eina_Value_Type * EINA_VALUE_TYPE_STRING
manages string type.
Definition: eina_value.c:5608
eina_value_struct_get
static Eina_Bool eina_value_struct_get(const Eina_Value *value, const char *name,...)
Gets the generic value from a struct member.
EINA_VALUE_STRUCT_MEMBER
#define EINA_VALUE_STRUCT_MEMBER(eina_value_type, type, member)
Helper to define Eina_Value_Struct_Member fields, uses offsetof() with type and member.
Definition: eina_value.h:3160
eina_value_struct_set
static Eina_Bool eina_value_struct_set(Eina_Value *value, const char *name,...)
Sets the generic value in a struct member.
eina_value_struct_new
EINA_API Eina_Value * eina_value_struct_new(const Eina_Value_Struct_Desc *desc)
Creates generic value storage of type struct.
Definition: eina_value.c:5823
eina_value_struct_value_set
static Eina_Bool eina_value_struct_value_set(Eina_Value *dst, const char *name, const Eina_Value *src)
Sets the member from Eina_Value source.
eina_value_struct_value_get
static Eina_Bool eina_value_struct_value_get(const Eina_Value *src, const char *name, Eina_Value *dst)
Gets the member as Eina_Value copy.
EINA_VALUE_STRUCT_DESC_VERSION
#define EINA_VALUE_STRUCT_DESC_VERSION
Current API version, used to validate _Eina_Value_Struct_Desc.
Definition: eina_value.h:3131
eina_value_free
EINA_API void eina_value_free(Eina_Value *value)
Frees value and its data.
Definition: eina_value.c:5645
eina_value_get
static Eina_Bool eina_value_get(const Eina_Value *value,...)
Gets the generic value.
eina_value_flush
static void eina_value_flush(Eina_Value *value)
Empties a generic value storage.
eldbus_message_from_eina_value
Eina_Bool eldbus_message_from_eina_value(const char *signature, Eldbus_Message *msg, const Eina_Value *value)
Convert Eina_Value to Eldbus_Message.
Definition: eldbus_message_from_eina_value.c:375
eldbus_message_to_eina_value
Eina_Value * eldbus_message_to_eina_value(const Eldbus_Message *msg)
Convert Eldbus_Message to Eina_Value.
Definition: eldbus_message_to_eina_value.c:453
eldbus_connection_unref
void eldbus_connection_unref(Eldbus_Connection *conn)
Decrement connection reference count.
Definition: eldbus_core.c:1306
eldbus_connection_get
Eldbus_Connection * eldbus_connection_get(Eldbus_Connection_Type type)
Establish a connection to bus and integrate it with the ecore main loop.
Definition: eldbus_core.c:1102
Eldbus_Message
struct _Eldbus_Message Eldbus_Message
Represents the way data is sent and received in DBus.
Definition: Eldbus.h:173
eldbus_shutdown
EAPI int eldbus_shutdown(void)
Shutdown eldbus.
Definition: eldbus_core.c:246
Eldbus_Pending
struct _Eldbus_Pending Eldbus_Pending
Represents a message that has been sent but has not yet reached its destination.
Definition: Eldbus.h:188
eldbus_init
EAPI int eldbus_init(void)
Initialize eldbus.
Definition: eldbus_core.c:128
eldbus_proxy_property_local_get
Eina_Value * eldbus_proxy_property_local_get(Eldbus_Proxy *proxy, const char *name)
Return the cached value of property.
Definition: eldbus_proxy.c:887
eldbus_proxy_properties_monitor
Eina_Bool eldbus_proxy_properties_monitor(Eldbus_Proxy *proxy, Eina_Bool enable)
Enable or disable local cache of properties.
Definition: eldbus_proxy.c:843
eldbus_message_error_get
Eina_Bool eldbus_message_error_get(const Eldbus_Message *msg, const char **name, const char **text)
Get the error text and name from a Eldbus_Message.
Definition: eldbus_message.c:233
eldbus_object_get
Eldbus_Object * eldbus_object_get(Eldbus_Connection *conn, const char *bus, const char *path)
Get an object of the given bus and path.
Definition: eldbus_object.c:188
eldbus_proxy_call
Eldbus_Pending * eldbus_proxy_call(Eldbus_Proxy *proxy, const char *member, Eldbus_Message_Cb cb, const void *cb_data, double timeout, const char *signature,...)
Call a method in proxy.
Definition: eldbus_proxy.c:621
eldbus_proxy_method_call_new
Eldbus_Message * eldbus_proxy_method_call_new(Eldbus_Proxy *proxy, const char *member)
Constructs a new message to invoke a method on a remote interface.
Definition: eldbus_proxy.c:592
eldbus_proxy_event_callback_add
void eldbus_proxy_event_callback_add(Eldbus_Proxy *proxy, Eldbus_Proxy_Event_Type type, Eldbus_Proxy_Event_Cb cb, const void *cb_data)
Add a callback function to be called when occurs a event of the type passed.
Definition: eldbus_proxy.c:389
eldbus_proxy_get
Eldbus_Proxy * eldbus_proxy_get(Eldbus_Object *obj, const char *interface)
Get a proxy of the following interface name in a Eldbus_Object.
Definition: eldbus_proxy.c:208
eldbus_proxy_send
Eldbus_Pending * eldbus_proxy_send(Eldbus_Proxy *proxy, Eldbus_Message *msg, Eldbus_Message_Cb cb, const void *cb_data, double timeout)
Send a message.
Definition: eldbus_proxy.c:574
eldbus_proxy_event_callback_del
void eldbus_proxy_event_callback_del(Eldbus_Proxy *proxy, Eldbus_Proxy_Event_Type type, Eldbus_Proxy_Event_Cb cb, const void *cb_data)
Remove callback registered in eldbus_proxy_event_callback_add().
Definition: eldbus_proxy.c:435
ELDBUS_PROXY_EVENT_PROPERTY_REMOVED
@ ELDBUS_PROXY_EVENT_PROPERTY_REMOVED
a property was removed
Definition: eldbus_proxy.h:191
ELDBUS_PROXY_EVENT_PROPERTY_CHANGED
@ ELDBUS_PROXY_EVENT_PROPERTY_CHANGED
a property has changed
Definition: eldbus_proxy.h:190
Eldbus_Connection
struct _Eldbus_Connection Eldbus_Connection
Represents a connection of one the type of connection with the DBus daemon.
Definition: Eldbus.h:227
Eldbus_Proxy
struct _Eldbus_Proxy Eldbus_Proxy
Represents a client object bound to an interface.
Definition: Eldbus.h:221
Eldbus_Object
struct _Eldbus_Object Eldbus_Object
Represents an object path already attached with bus name or unique id.
Definition: Eldbus.h:233
_Eina_Value_Array
Used to store the array and its subtype.
Definition: eina_value.h:1183
_Eina_Value_Struct_Desc
Describes the struct by listing its size, members and operations.
Definition: eina_value.h:3144
_Eina_Value_Struct_Desc::size
unsigned int size
byte size to allocate, may be bigger than sum of members
Definition: eina_value.h:3149
_Eina_Value_Struct_Member
Describes a single member of struct.
Definition: eina_value.h:3121
_Eina_Value_Struct_Member::offset
unsigned int offset
where this member is located within the structure memory
Definition: eina_value.h:3124
_Eina_Value_Struct_Member::type
const Eina_Value_Type * type
how to use this member
Definition: eina_value.h:3123
_Eina_Value_Struct_Member::name
const char * name
member name, used in lookups such as eina_value_struct_get()
Definition: eina_value.h:3122
_Eina_Value_Struct
Used to store the memory and its description.
Definition: eina_value.h:3180
_Eina_Value
defines the contents of a value
Definition: eina_value.h:662
_Eldbus_Proxy_Event_Property_Changed
Definition: eldbus_proxy.h:203
_Eldbus_Proxy_Event_Property_Removed
Definition: eldbus_proxy.h:225