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"
static Eina_Bool
_timer1_cb(void *data EINA_UNUSED)
{
printf("\nFishing...\n");
}
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;
}
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;
int size_of_array = 5;
const char *array[5] = { "aaaa", "bbbb", "cccc", "dddd", "eeee" };
int i;
int plus_one = 24;
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");
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");
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");
variant = eldbus_message_iter_container_new(iter, 'v', "s");
eldbus_message_iter_basic_append(variant, 's', "test");
pending = eldbus_proxy_send(test2_proxy, msg, on_send_variant, NULL, -1);
msg = eldbus_proxy_method_call_new(test2_proxy, "DoubleContainner");
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_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_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);
return 0;
}
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
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
#define ECORE_CALLBACK_CANCEL
Return value to remove a callback.
Definition: Ecore_Common.h:152
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
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
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
void eldbus_connection_unref(Eldbus_Connection *conn)
Decrement connection reference count.
Definition: eldbus_core.c:1306
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
struct _Eldbus_Message_Iter Eldbus_Message_Iter
Represents an iterator over a complex message type (array, dict, struct, or variant).
Definition: Eldbus.h:181
struct _Eldbus_Message Eldbus_Message
Represents the way data is sent and received in DBus.
Definition: Eldbus.h:173
EAPI int eldbus_shutdown(void)
Shutdown eldbus.
Definition: eldbus_core.c:246
struct _Eldbus_Pending Eldbus_Pending
Represents a message that has been sent but has not yet reached its destination.
Definition: Eldbus.h:188
EAPI int eldbus_init(void)
Initialize eldbus.
Definition: eldbus_core.c:128
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_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
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
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 * 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
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 * eldbus_message_iter_get(const Eldbus_Message *msg)
Get the main Eldbus_Message_Iter from the Eldbus_Message.
Definition: eldbus_message.c:264
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
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
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
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
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 * 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_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 * 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_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_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
struct _Eldbus_Connection Eldbus_Connection
Represents a connection of one the type of connection with the DBus daemon.
Definition: Eldbus.h:227
struct _Eldbus_Proxy Eldbus_Proxy
Represents a client object bound to an interface.
Definition: Eldbus.h:221
struct _Eldbus_Object Eldbus_Object
Represents an object path already attached with bus name or unique id.
Definition: Eldbus.h:233