complex-types.c

Client to test complex types (arrays, structs, dicts) against a provided server (complex-types-server.c).

Client to test complex types (arrays, structs, dicts) against a provided server (complex-types-server.c)

//Compile with:
// gcc -o complex-types complex-types.c `pkg-config --cflags --libs eldbus ecore`
#include "Eldbus.h"
#include <Ecore.h>
#define BUS "com.profusion"
#define PATH "/com/profusion/Test"
#define IFACE "com.profusion.Test"
Eldbus_Connection *conn;
static Eina_Bool
_timer1_cb(void *data EINA_UNUSED)
{
printf("\nFishing...\n");
ecore_main_loop_quit();
return ECORE_CALLBACK_CANCEL;
}
static void
on_plus_one(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
int num2 = 0;
if (eldbus_message_error_get(msg, NULL, NULL))
{
printf("Message error\n\n");
return;
}
if (!eldbus_message_arguments_get(msg, "i", &num2))
{
printf("Error getting arguments.");
return;
}
printf("on_plus_one() %d\n", num2);
}
static void
set_property_resp2(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname;
const char *errmsg;
printf("set_property_resp2()\n");
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
printf("Message error %s - %s\n\n", errname, errmsg);
return;
}
}
static void
get_property_resp2(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eldbus_Proxy *proxy = data;
Eldbus_Message_Iter *variant = NULL;
char *type;
char *resp2;
const char *errname;
const char *errmsg;
printf("get_property_resp2()\n");
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
printf("Message error %s - %s\n\n", errname, errmsg);
return;
}
if (!eldbus_message_arguments_get(msg, "v", &variant))
{
printf("Error getting arguments.");
return;
}
type = eldbus_message_iter_signature_get(variant);
if (type[1])
{
printf("It is a complex type, not handle yet.\n\n");
return;
}
if (type[0] != 's')
{
printf("Expected type is string.\n\n");
return;
}
if (!eldbus_message_iter_arguments_get(variant, "s", &resp2))
{
printf("error in eldbus_message_iter_arguments_get()\n\n");
return;
}
printf("resp2=%s\n", resp2);
free(type);
eldbus_proxy_property_set(proxy, "Resp2", "s", &"lalala", set_property_resp2, NULL);
eldbus_proxy_property_set(proxy, "int32", "i", (void*)(intptr_t)99, set_property_resp2, NULL);
}
static void
on_send_array_int(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eldbus_Message_Iter *array = NULL;
int num;
printf("on_send_array_int()\n");
if (eldbus_message_error_get(msg, NULL, NULL))
{
printf("Message error\n\n");
return;
}
if (!eldbus_message_arguments_get(msg, "ai", &array))
{
printf("Error getting arguments.");
return;
}
while (eldbus_message_iter_get_and_next(array, 'i', &num))
{
printf("%d\n", num);
}
}
static void
on_send_array(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eldbus_Message_Iter *array = NULL;
char *txt = NULL;
char *string[10];
int i = 0;
int z;
printf("on_send_array()\n");
if (eldbus_message_error_get(msg, NULL, NULL))
{
printf("Message error\n\n");
return;
}
if (!eldbus_message_arguments_get(msg, "as", &array))
{
printf("Error getting arguments.");
return;
}
while (eldbus_message_iter_get_and_next(array, 's', &txt))
{
string[i] = txt;
i++;
}
for (z = 0; z < i; z++)
printf("string = %s\n", string[z]);
}
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("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_send_variant(void *data EINA_UNUSED, const Eldbus_Message *msg EINA_UNUSED, Eldbus_Pending *pending EINA_UNUSED)
{
printf("on_send_variant()\n\n");
}
static void
on_receive_array(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *errname;
const char *errmsg;
printf("on_receive_array()\n");
if (eldbus_message_error_get(msg, &errname, &errmsg))
{
fprintf(stderr, "Error: %s %s\n", errname, errmsg);
}
}
int
main(void)
{
Eldbus_Object *test2_obj;
Eldbus_Proxy *test2_proxy;
Eldbus_Pending *pending;
Eldbus_Message_Iter *iter, *array_of_string, *variant;
Eldbus_Message_Iter *array_itr, *structure;
Eldbus_Message *msg;
int size_of_array = 5;
const char *array[5] = { "aaaa", "bbbb", "cccc", "dddd", "eeee" };
int i;
int plus_one = 24;
ecore_init();
eldbus_init();
conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
test2_obj = eldbus_object_get(conn, BUS, PATH);
test2_proxy = eldbus_proxy_get(test2_obj, IFACE);
msg = eldbus_proxy_method_call_new(test2_proxy, "ReceiveArray");
iter = eldbus_message_iter_get(msg);
array_of_string = eldbus_message_iter_container_new(iter, 'a',"s");
if (!array_of_string) printf("array_of_string == NULL\n\n");
for (i = 0; i < 5; i++)
eldbus_message_iter_basic_append(array_of_string, 's', array[i]);
eldbus_message_iter_container_close(iter, array_of_string);
pending = eldbus_proxy_send(test2_proxy, msg, on_receive_array, NULL, -1);
if (!pending) printf("Error in eldbus_proxy_send()\n\n");
msg = eldbus_proxy_method_call_new(test2_proxy, "ReceiveArrayOfStringIntWithSize");
iter = eldbus_message_iter_get(msg);
if (!eldbus_message_iter_arguments_append(iter, "ia(si)", size_of_array, &array_of_string))
printf("error on eldbus_massage_iterator_arguments_set()\n\n");
for (i = 0; i < size_of_array; i++)
{
Eldbus_Message_Iter *struct_of_si;
eldbus_message_iter_arguments_append(array_of_string, "(si)", &struct_of_si);
eldbus_message_iter_arguments_append(struct_of_si, "si", array[i], i);
eldbus_message_iter_container_close(array_of_string, struct_of_si);
}
eldbus_message_iter_container_close(iter, array_of_string);
pending = eldbus_proxy_send(test2_proxy, msg, on_receive_array_with_size, NULL, -1);
msg = eldbus_proxy_method_call_new(test2_proxy, "SendVariantData");
iter = eldbus_message_iter_get(msg);
variant = eldbus_message_iter_container_new(iter, 'v', "s");
eldbus_message_iter_basic_append(variant, 's', "test");
eldbus_message_iter_container_close(iter, variant);
pending = eldbus_proxy_send(test2_proxy, msg, on_send_variant, NULL, -1);
msg = eldbus_proxy_method_call_new(test2_proxy, "DoubleContainner");
iter = eldbus_message_iter_get(msg);
eldbus_message_iter_arguments_append(iter, "a(ii)", &array_itr);
for (i = 0; i < 5; i++)
{
eldbus_message_iter_arguments_append(array_itr, "(ii)", &structure);
eldbus_message_iter_arguments_append(structure, "ii", i, i*i);
eldbus_message_iter_container_close(array_itr, structure);
}
eldbus_message_iter_container_close(iter, array_itr);
eldbus_message_iter_arguments_append(iter, "a(ii)", &array_itr);
for (i = 0; i < 7; i++)
{
eldbus_message_iter_arguments_append(array_itr, "(ii)", &structure);
eldbus_message_iter_arguments_append(structure, "ii", i, i*i*i);
eldbus_message_iter_container_close(array_itr, structure);
}
eldbus_message_iter_container_close(iter, array_itr);
eldbus_proxy_send(test2_proxy, msg, NULL, NULL, -1);
pending = eldbus_proxy_call(test2_proxy, "SendArrayInt", on_send_array_int, NULL,
-1 , "");
pending = eldbus_proxy_call(test2_proxy, "SendArray", on_send_array, NULL,
-1 , "");
pending = eldbus_proxy_call(test2_proxy, "PlusOne", on_plus_one, NULL,
-1 , "i", plus_one);
pending = eldbus_proxy_property_get(test2_proxy, "Resp2", get_property_resp2, test2_proxy);
ecore_timer_add(10, _timer1_cb, NULL);
ecore_main_loop_begin();
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_quit
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition ecore_main.c:1321
ECORE_CALLBACK_CANCEL
#define ECORE_CALLBACK_CANCEL
Return value to remove a callback.
Definition Ecore_Common.h:152
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_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
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_shutdown
EAPI int eldbus_shutdown(void)
Shutdown eldbus.
Definition eldbus_core.c:246
Eldbus_Message
struct _Eldbus_Message Eldbus_Message
Represents the way data is sent and received in DBus.
Definition Eldbus.h:173
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_Message_Iter
struct _Eldbus_Message_Iter Eldbus_Message_Iter
Represents an iterator over a complex message type (array, dict, struct, or variant).
Definition Eldbus.h:181
eldbus_init
EAPI int eldbus_init(void)
Initialize eldbus.
Definition eldbus_core.c:128
eldbus_proxy_property_set
Eldbus_Pending * eldbus_proxy_property_set(Eldbus_Proxy *proxy, const char *name, const char *sig, const void *value, Eldbus_Message_Cb cb, const void *data)
Set a property.
Definition eldbus_proxy.c:704
eldbus_proxy_property_get
Eldbus_Pending * eldbus_proxy_property_get(Eldbus_Proxy *proxy, const char *name, Eldbus_Message_Cb cb, const void *data)
Get a property.
Definition eldbus_proxy.c:681
eldbus_message_iter_container_close
Eina_Bool eldbus_message_iter_container_close(Eldbus_Message_Iter *iter, Eldbus_Message_Iter *sub)
Closes a container-typed value appended to the message.
Definition eldbus_message.c:548
eldbus_message_iter_get_and_next
Eina_Bool eldbus_message_iter_get_and_next(Eldbus_Message_Iter *iter, char signature,...)
Useful when iterating over arrays.
Definition eldbus_message.c:736
eldbus_message_iter_container_new
Eldbus_Message_Iter * eldbus_message_iter_container_new(Eldbus_Message_Iter *iter, int type, const char *contained_signature)
Create and append a typed iterator to another iterator.
Definition eldbus_message.c:526
eldbus_message_iter_signature_get
char * eldbus_message_iter_signature_get(Eldbus_Message_Iter *iter)
Returns the current signature of a message iterator.
Definition eldbus_message.c:605
eldbus_message_iter_get
Eldbus_Message_Iter * eldbus_message_iter_get(const Eldbus_Message *msg)
Get the main Eldbus_Message_Iter from the Eldbus_Message.
Definition eldbus_message.c:264
eldbus_message_iter_arguments_append
Eina_Bool eldbus_message_iter_arguments_append(Eldbus_Message_Iter *iter, const char *signature,...)
Append an argument into an Eldbus_Message_Iter.
Definition eldbus_message.c:379
eldbus_message_iter_arguments_get
Eina_Bool eldbus_message_iter_arguments_get(Eldbus_Message_Iter *iter, const char *signature,...)
Get data from Eldbus_Message_Iter, for each complete type must have a pointer to store his value,...
Definition eldbus_message.c:842
eldbus_message_iter_basic_append
Eina_Bool eldbus_message_iter_basic_append(Eldbus_Message_Iter *iter, int type,...)
Append a basic type into an Eldbus_Iterator.
Definition eldbus_message.c:558
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_message_arguments_get
Eina_Bool eldbus_message_arguments_get(const Eldbus_Message *msg, const char *signature,...)
Get the arguments from an Eldbus_Message.
Definition eldbus_message.c:274
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_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_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_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_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