summaryrefslogtreecommitdiff
path: root/src/examples/edbus
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-28 17:53:25 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-28 17:53:25 +0000
commit331488d1ce3db2d07533ee276dabdc23f153fa61 (patch)
treea091fe83bfcf0c9d8f9f34ebfe44a5347537bff8 /src/examples/edbus
parent6800b178dc918633ede220c572e5b99fa0046e7e (diff)
efl: merge edbus (v2).
SVN revision: 81825
Diffstat (limited to 'src/examples/edbus')
-rw-r--r--src/examples/edbus/Makefile.am71
-rw-r--r--src/examples/edbus/banshee.c239
-rw-r--r--src/examples/edbus/client.c331
-rw-r--r--src/examples/edbus/complex-types-client-eina-value.c323
-rw-r--r--src/examples/edbus/complex-types-server.c390
-rw-r--r--src/examples/edbus/complex-types.c285
-rw-r--r--src/examples/edbus/connman-list-services.c107
-rw-r--r--src/examples/edbus/ofono-dial.c85
-rw-r--r--src/examples/edbus/server.c245
-rw-r--r--src/examples/edbus/simple-signal-emit.c83
10 files changed, 2159 insertions, 0 deletions
diff --git a/src/examples/edbus/Makefile.am b/src/examples/edbus/Makefile.am
new file mode 100644
index 0000000000..33e8e80a02
--- /dev/null
+++ b/src/examples/edbus/Makefile.am
@@ -0,0 +1,71 @@
1MAINTAINERCLEANFILES = Makefile.in
2
3AM_CPPFLAGS = \
4-I$(top_srcdir)/src/lib/eina \
5-I$(top_srcdir)/src/lib/eo \
6-I$(top_srcdir)/src/lib/ecore \
7-I$(top_srcdir)/src/lib/edbus \
8-I$(top_builddir)/src/lib/eina \
9-I$(top_builddir)/src/lib/eo \
10-I$(top_builddir)/src/lib/ecore \
11-I$(top_builddir)/src/lib/edbus \
12@EDBUS_CFLAGS@
13
14EXAMPLES_LIBS = \
15$(top_builddir)/src/lib/ecore/libecore.la \
16$(top_builddir)/src/lib/eina/libeina.la \
17$(top_builddir)/src/lib/edbus/libedbus2.la
18
19EXTRA_PROGRAMS = \
20connman-list-services \
21ofono-dial \
22banshee \
23complex-types \
24complex-types-server \
25server \
26client \
27simple-signal-emit \
28complex-types-client-eina-value
29
30connman_list_services_SOURCES = connman-list-services.c
31connman_list_services_LDADD = $(EXAMPLES_LIBS)
32
33ofono_dial_SOURCES = ofono-dial.c
34ofono_dial_LDADD = $(EXAMPLES_LIBS)
35
36banshee_SOURCES = banshee.c
37banshee_LDADD = $(EXAMPLES_LIBS)
38
39complex_types_SOURCES = complex-types.c
40complex_types_LDADD = $(EXAMPLES_LIBS)
41
42complex_types_server_SOURCES = complex-types-server.c
43complex_types_server_LDADD = $(EXAMPLES_LIBS)
44
45server_SOURCES = server.c
46server_LDADD = $(EXAMPLES_LIBS)
47
48client_SOURCES = client.c
49client_LDADD = $(EXAMPLES_LIBS)
50
51simple_signal_emit_SOURCES = simple-signal-emit.c
52simple_signal_emit_LDADD = $(EXAMPLES_LIBS)
53
54complex_types_client_eina_value_SOURCES = complex-types-client-eina-value.c
55complex_types_client_eina_value_LDADD = $(EXAMPLES_LIBS)
56
57SRCS = $(EXTRA_PROGRAMS)
58
59examples: $(EXTRA_PROGRAMS)
60
61clean-local:
62 rm -f $(EXTRA_PROGRAMS)
63
64install-examples:
65 mkdir -p $(datadir)/edbus/examples
66 $(install_sh_DATA) -c $(SRCS) $(datadir)/edbus/examples
67
68uninstall-local:
69 for f in $(SRCS) ; do \
70 rm -f $(datadir)/edbus/examples/$$f ; \
71 done
diff --git a/src/examples/edbus/banshee.c b/src/examples/edbus/banshee.c
new file mode 100644
index 0000000000..59c6a9a493
--- /dev/null
+++ b/src/examples/edbus/banshee.c
@@ -0,0 +1,239 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "org.bansheeproject.Banshee"
5#define ENGINE_PATH "/org/bansheeproject/Banshee/PlayerEngine"
6#define CONTROLLER_PATH "/org/bansheeproject/Banshee/PlaybackController"
7#define MPRIS_PATH "/org/mpris/MediaPlayer2"
8
9#define ENGINE_IFACE "org.bansheeproject.Banshee.PlayerEngine"
10#define CONTROLLER_IFACE "org.bansheeproject.Banshee.PlaybackController"
11#define MPRIS_IFACE "org.mpris.MediaPlayer2.Playlists"
12
13static EDBus_Signal_Handler *state_changed2;
14
15static Eina_Bool
16_timeout_application(void *data)
17{
18 printf("\n## ecore_main_loop_quit()\n");
19 ecore_main_loop_quit();
20 return EINA_TRUE;
21}
22
23static void
24on_get_playlists(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
25{
26 EDBus_Message_Iter *array, *struct_entry;
27 const char *path, *name, *image;
28 int i = 0;
29
30 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
31
32 if (!edbus_message_arguments_get(msg, "a(oss)", &array))
33 {
34 fprintf(stderr, "Error: could not get entry contents\n");
35 return;
36 }
37 printf("on_get_playlists() \n\n");
38 while (edbus_message_iter_get_and_next(array, 'r', &struct_entry))
39 {
40 if (!edbus_message_iter_arguments_get(struct_entry, "oss", &path, &name, &image))
41 {
42 printf("error on edbus_massage_iterator_arguments_get()");
43 return;
44 }
45 i++;
46 printf("%d - %s | %s | %s\n", i, path, name, image);
47 }
48 printf("end of on_get_playlists()\n\n");
49}
50
51static void
52iterate_dict(void *data, const void *key, EDBus_Message_Iter *var)
53{
54 const char *skey = key;
55
56 if (!strcmp(skey, "PlaylistCount"))
57 {
58 unsigned count;
59 if (!edbus_message_iter_arguments_get(var, "u", &count))
60 printf("error2\n");
61 printf("PlaylistCount=%d\n", count);
62 }
63 else if (!strcmp(skey, "Orderings"))
64 {
65 EDBus_Message_Iter *as;
66 const char *txt;
67 printf("- Orderings\n");
68 if (!edbus_message_iter_arguments_get(var, "as", &as))
69 printf("error1\n");
70 while (edbus_message_iter_get_and_next(as, 's', &txt))
71 printf("\t%s\n", txt);
72 }
73}
74
75static void
76playlist_get_all_cb(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
77{
78 EDBus_Message_Iter *array;
79 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
80
81 if (edbus_message_arguments_get(msg, "a{sv}", &array))
82 edbus_message_iter_dict_iterate(array, "sv", iterate_dict, NULL);
83}
84
85static void
86on_introspect(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
87{
88 const char *string;
89
90 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
91
92 if (!edbus_message_arguments_get(msg, "s", &string))
93 {
94 fprintf(stderr, "Error: could not get entry contents\n");
95 return;
96 }
97
98 printf("on_introspect() data=\n%s\n\n", string);
99}
100
101static void
102on_next_or_pause(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
103{
104 const char *status = data;
105
106 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
107
108 printf("%s\n", status);
109}
110
111static void
112on_state_changed(void *data, const EDBus_Message *msg)
113{
114 const char *status;
115 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
116
117 if (!edbus_message_arguments_get(msg, "s", &status))
118 {
119 fprintf(stderr, "Error: could not get entry contents\n");
120 return;
121 }
122
123 printf("on_state_changed = %s\n", status);
124}
125
126static void
127on_state_changed2(void *data, const EDBus_Message *msg)
128{
129 const char *status;
130 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
131
132 if (!edbus_message_arguments_get(msg, "s", &status))
133 {
134 fprintf(stderr, "Error: could not get entry contents\n");
135 return;
136 }
137
138 printf("on_state_changed2 = %s\n", status);
139 edbus_signal_handler_unref(state_changed2);
140 state_changed2 = NULL;
141}
142
143static void
144on_banshee_startup(void *data, const EDBus_Message *msg)
145{
146 const char *bus, *older_id, *new_id;
147
148 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
149 if (!edbus_message_arguments_get(msg, "sss", &bus, &older_id, &new_id))
150 {
151 printf("Error getting arguments from NameOwnerChanged");
152 return;
153 }
154
155 printf("banshee started on id=%s\n", new_id);
156}
157
158static void
159on_name_owner_changed(void *data, const EDBus_Message *msg)
160{
161 const char *bus, *older_id, *new_id;
162
163 EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
164 if (!edbus_message_arguments_get(msg, "sss", &bus, &older_id, &new_id))
165 {
166 printf("Error getting arguments from NameOwnerChanged");
167 return;
168 }
169
170 printf("bus = %s older=%s new=%s\n\n", bus, older_id, new_id);
171}
172
173int
174main(void)
175{
176 EDBus_Connection *conn;
177 EDBus_Object *engine_obj, *controller_obj, *mpris_obj;
178 EDBus_Proxy *engine, *controler, *playlists;
179 EDBus_Signal_Handler *sh;
180
181 ecore_init();
182 edbus_init();
183
184 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
185
186 engine_obj = edbus_object_get(conn, BUS, ENGINE_PATH);
187 controller_obj = edbus_object_get(conn, BUS, CONTROLLER_PATH);
188 mpris_obj = edbus_object_get(conn, BUS, MPRIS_PATH);
189
190 engine = edbus_proxy_get(engine_obj, ENGINE_IFACE);
191 EINA_SAFETY_ON_NULL_GOTO(engine, end);
192 controler = edbus_proxy_get(controller_obj, CONTROLLER_IFACE);
193 EINA_SAFETY_ON_NULL_GOTO(controler, end);
194 playlists = edbus_proxy_get(mpris_obj, MPRIS_IFACE);
195 EINA_SAFETY_ON_NULL_GOTO(playlists, end);
196
197 edbus_object_introspect(engine_obj, on_introspect, NULL);
198
199 edbus_proxy_signal_handler_add(engine, "StateChanged", on_state_changed, NULL);
200 edbus_proxy_call(engine, "Pause", on_next_or_pause, "Pause", -1, "");
201
202 edbus_proxy_call(controler, "Next", on_next_or_pause, "Next", -1, "b", EINA_TRUE);
203
204 edbus_proxy_property_get_all(playlists, playlist_get_all_cb, NULL);
205 edbus_proxy_call(playlists, "GetPlaylists", on_get_playlists, NULL, -1,
206 "uusb", (unsigned)0, (unsigned)30, "asc", EINA_FALSE);
207
208 edbus_signal_handler_add(conn, BUS, ENGINE_PATH, ENGINE_IFACE,
209 "StateChanged", on_state_changed, NULL);
210 state_changed2 = edbus_signal_handler_add(conn, BUS, ENGINE_PATH, ENGINE_IFACE,
211 "StateChanged", on_state_changed2, NULL);
212
213 sh = edbus_signal_handler_add(conn, EDBUS_FDO_BUS, EDBUS_FDO_PATH,
214 EDBUS_FDO_INTERFACE, "NameOwnerChanged",
215 on_name_owner_changed, NULL);
216 edbus_signal_handler_match_extra_set(sh, "arg0", BUS, NULL);
217
218 sh = edbus_signal_handler_add(conn, EDBUS_FDO_BUS, EDBUS_FDO_PATH,
219 EDBUS_FDO_INTERFACE, "NameOwnerChanged",
220 on_banshee_startup, NULL);
221 edbus_signal_handler_match_extra_set(sh, "arg0", BUS, "arg1", "", NULL);
222
223 ecore_timer_add(50, _timeout_application, NULL);
224
225 ecore_main_loop_begin();
226
227end:
228 /**
229 * It's not necessary unref all objecs, proxys and signal handlers
230 * When a parent have ref = 0, it will unref all your childrens
231 * before free it self.
232 **/
233 edbus_connection_unref(conn);
234
235 edbus_shutdown();
236 ecore_shutdown();
237 return 0;
238}
239
diff --git a/src/examples/edbus/client.c b/src/examples/edbus/client.c
new file mode 100644
index 0000000000..02da5a2e9d
--- /dev/null
+++ b/src/examples/edbus/client.c
@@ -0,0 +1,331 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "org.Enlightenment"
5#define PATH "/org/enlightenment"
6#define INTERFACE "org.enlightenment.Test"
7#define NTESTS 8
8
9static int _client_log_dom = -1;
10#define ERR(...) EINA_LOG_DOM_ERR(_client_log_dom, __VA_ARGS__)
11
12static void
13_on_alive(void *context, const EDBus_Message *msg)
14{
15 printf("Alive\n\n");
16}
17
18static void
19_on_hello(void *context, const EDBus_Message *msg)
20{
21 const char *txt;
22 if (edbus_message_arguments_get(msg, "s", &txt))
23 printf("%s\n", txt);
24}
25
26#include <inttypes.h>
27
28static struct expected
29{
30 Eina_Bool b;
31 uint8_t y;
32 uint32_t u;
33 int32_t i;
34 int16_t n;
35 double d;
36 const char *s;
37} expected = {
38 .b = EINA_TRUE,
39 .y = 0xAA,
40 .u = 0xFFFFFFFF,
41 .i = 0xFFFFFFFF,
42 .n = 0xFFFF,
43 .d = 3.1415926,
44 .s = "test",
45};
46
47static void
48test(void)
49{
50 static int n = 0;
51 n++;
52 if (n >= NTESTS)
53 printf("Passed in all tests\n");
54 else
55 printf("Passed in %d/%d tests\n", n, NTESTS);
56}
57
58static void
59_on_send_bool(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
60{
61 const char *errname, *errmsg;
62 Eina_Bool b;
63
64 if (edbus_message_error_get(msg, &errname, &errmsg))
65 {
66 ERR("%s %s", errname, errmsg);
67 return;
68 }
69
70 if (!edbus_message_arguments_get(msg, "b", &b))
71 {
72 ERR("Could not get entry contents");
73 return;
74 }
75
76 if (b != expected.b)
77 {
78 ERR("Bool value doesn't match");
79 return;
80 }
81
82 test();
83}
84
85static void
86_on_send_byte(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
87{
88 const char *errname, *errmsg;
89 uint8_t y;
90
91 if (edbus_message_error_get(msg, &errname, &errmsg))
92 {
93 ERR("%s %s", errname, errmsg);
94 return;
95 }
96
97 if (!edbus_message_arguments_get(msg, "y", &y))
98 {
99 ERR("Could not get entry contents");
100 return;
101 }
102
103 if (y != expected.y)
104 {
105 ERR("Byte value doesn't match expected value");
106 return;
107 }
108
109 test();
110}
111
112static void
113_on_send_uint32(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
114{
115 const char *errname, *errmsg;
116 unsigned int u;
117
118 if (edbus_message_error_get(msg, &errname, &errmsg))
119 {
120 ERR("%s %s", errname, errmsg);
121 return;
122 }
123
124 if (!edbus_message_arguments_get(msg, "u", &u))
125 {
126 ERR("Could not get entry contents");
127 return;
128 }
129
130 if (u != expected.u)
131 {
132 ERR("Uint32 value doesn't match expected value");
133 return;
134 }
135
136 test();
137}
138
139static void
140_on_send_int32(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
141{
142 const char *errname, *errmsg;
143 int32_t i;
144
145 if (edbus_message_error_get(msg, &errname, &errmsg))
146 {
147 ERR("%s %s", errname, errmsg);
148 return;
149 }
150
151 if (!edbus_message_arguments_get(msg, "i", &i))
152 {
153 ERR("Could not get entry contents");
154 return;
155 }
156
157 if (i != expected.i)
158 {
159 ERR("Int32 value doesn't match expected value");
160 return;
161 }
162
163 test();
164}
165
166static void
167_on_send_int16(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
168{
169 const char *errname, *errmsg;
170 int16_t n;
171
172 if (edbus_message_error_get(msg, &errname, &errmsg))
173 {
174 ERR("%s %s", errname, errmsg);
175 return;
176 }
177
178 if (!edbus_message_arguments_get(msg, "n", &n))
179 {
180 ERR("Could not get entry contents");
181 return;
182 }
183
184 if (n != expected.n)
185 {
186 ERR("Int16 value doesn't match expected value");
187 return;
188 }
189
190 test();
191}
192
193static void
194_on_send_double(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
195{
196 const char *errname, *errmsg;
197 double d;
198
199 if (edbus_message_error_get(msg, &errname, &errmsg))
200 {
201 ERR("%s %s", errname, errmsg);
202 return;
203 }
204
205 if (!edbus_message_arguments_get(msg, "d", &d))
206 {
207 ERR("Could not get entry contents");
208 return;
209 }
210
211 if (d != expected.d)
212 {
213 ERR("Double value doesn't match expected value");
214 return;
215 }
216
217 test();
218}
219
220static void
221_on_send_string(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
222{
223 const char *errname, *errmsg;
224 char *s;
225
226 if (edbus_message_error_get(msg, &errname, &errmsg))
227 {
228 ERR("%s %s", errname, errmsg);
229 return;
230 }
231
232 if (!edbus_message_arguments_get(msg, "s", &s))
233 {
234 ERR("Could not get entry contents");
235 return;
236 }
237
238 if (strcmp(s, expected.s) != 0)
239 {
240 ERR("Uint32 value doesn't match expected value");
241 return;
242 }
243
244 test();
245}
246
247static void
248_on_async_test(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
249{
250 const char *errname, *errmsg;
251
252 if (edbus_message_error_get(msg, &errname, &errmsg))
253 {
254 ERR("%s %s", errname, errmsg);
255 return;
256 }
257
258 test();
259}
260
261static void
262on_name_owner_changed(void *data, const char *bus, const char *old_id, const char *new_id)
263{
264 printf("Bus=%s | old=%s | new=%s\n", bus, old_id, new_id);
265}
266
267static Eina_Bool
268finish(void *data)
269{
270 ecore_main_loop_quit();
271 return ECORE_CALLBACK_CANCEL;
272}
273
274int
275main(void)
276{
277 EDBus_Connection *conn;
278 EDBus_Object *obj;
279 EDBus_Proxy *proxy;
280
281 eina_init();
282 _client_log_dom = eina_log_domain_register("client", EINA_COLOR_CYAN);
283 if (_client_log_dom < 0)
284 {
285 EINA_LOG_ERR("Unable to create 'client' log domain");
286 goto exit_eina;
287 }
288
289 ecore_init();
290 edbus_init();
291
292 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
293
294 obj = edbus_object_get(conn, BUS, PATH);
295 proxy = edbus_proxy_get(obj, INTERFACE);
296 edbus_proxy_signal_handler_add(proxy, "Alive", _on_alive, NULL);
297 edbus_proxy_signal_handler_add(proxy, "Hello", _on_hello, NULL);
298
299 edbus_proxy_call(proxy, "SendBool", _on_send_bool, NULL, -1, "b",
300 expected.b);
301 edbus_proxy_call(proxy, "SendByte", _on_send_byte, NULL, -1, "y",
302 expected.y);
303 edbus_proxy_call(proxy, "SendUint32", _on_send_uint32, NULL, -1, "u",
304 expected.u);
305 edbus_proxy_call(proxy, "SendInt32", _on_send_int32, NULL, -1, "i",
306 expected.i);
307 edbus_proxy_call(proxy, "SendInt16", _on_send_int16, NULL, -1, "n",
308 expected.n);
309 edbus_proxy_call(proxy, "SendDouble", _on_send_double, NULL, -1, "d",
310 expected.d);
311 edbus_proxy_call(proxy, "SendString", _on_send_string, NULL, -1, "s",
312 expected.s);
313 edbus_proxy_call(proxy, "AsyncTest", _on_async_test, NULL, -1, "");
314
315 edbus_name_owner_changed_callback_add(conn, BUS, on_name_owner_changed,
316 conn, EINA_TRUE);
317 ecore_timer_add(30, finish, NULL);
318
319 ecore_main_loop_begin();
320
321 edbus_connection_unref(conn);
322
323 edbus_shutdown();
324 ecore_shutdown();
325
326 eina_log_domain_unregister(_client_log_dom);
327exit_eina:
328 eina_shutdown();
329
330 return 0;
331}
diff --git a/src/examples/edbus/complex-types-client-eina-value.c b/src/examples/edbus/complex-types-client-eina-value.c
new file mode 100644
index 0000000000..5f7fa48313
--- /dev/null
+++ b/src/examples/edbus/complex-types-client-eina-value.c
@@ -0,0 +1,323 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "com.profusion"
5#define PATH "/com/profusion/Test"
6#define IFACE "com.profusion.Test"
7
8#define size_of_array 5
9static const char *array_string[] = {
10 "aaaa", "bbbb", "cccc", "dddd", "eeee"
11};
12
13typedef struct _sub_struct
14{
15 char *txt;
16 int num;
17} sub_struct;
18
19typedef struct _main_struct
20{
21 int size;
22 sub_struct array[];
23} main_struct;
24
25static unsigned int
26_type_offset(unsigned base, unsigned size)
27{
28 unsigned padding;
29 if (!(base % size))
30 return base;
31 padding = abs(base - size);
32 return base + padding;
33}
34
35static void
36_fill_receive_array_of_string_int_with_size(EDBus_Message *msg, int size, const char *array[])
37{
38 Eina_Value *value_struct, *value_array;
39 int i;
40 unsigned offset;
41 Eina_Value_Struct_Member main_members[2];
42 Eina_Value_Struct_Member sub_members[] = {
43 EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_STRING, sub_struct, txt),
44 EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT, sub_struct, num)
45 };
46 Eina_Value_Struct_Desc desc_sub_struct = {
47 EINA_VALUE_STRUCT_DESC_VERSION,
48 NULL, // no special operations
49 sub_members,
50 2,
51 sizeof(sub_struct)
52 };
53 Eina_Value_Struct_Desc desc_struct = {
54 EINA_VALUE_STRUCT_DESC_VERSION,
55 NULL, // no special operations
56 main_members,
57 2,
58 0//will be set below
59 };
60
61 offset = _type_offset(sizeof(int), sizeof(Eina_Value_Array));
62 main_members[0].name = "size";
63 main_members[0].type = EINA_VALUE_TYPE_INT, 0;
64 main_members[0].offset = 0;
65 main_members[1].name = "array";
66 main_members[1].type = EINA_VALUE_TYPE_ARRAY;
67 main_members[1].offset = offset;
68 desc_struct.size = offset + sizeof(Eina_Value_Array);
69
70 value_struct = eina_value_struct_new(&desc_struct);
71 eina_value_struct_set(value_struct, "size", size);
72
73 value_array = eina_value_array_new(EINA_VALUE_TYPE_STRUCT, size);
74 for (i = 0; i < size; i++)
75 {
76 Eina_Value *value_sub_struct = eina_value_struct_new(&desc_sub_struct);
77 Eina_Value_Struct st;
78 eina_value_struct_set(value_sub_struct, "txt", array[i]);
79 eina_value_struct_set(value_sub_struct, "num", i);
80 eina_value_get(value_sub_struct, &st);
81 eina_value_array_append(value_array, st);
82 eina_value_free(value_sub_struct);
83 }
84 eina_value_struct_value_set(value_struct, "array", value_array);
85
86 edbus_message_from_eina_value("ia(si)", msg, value_struct);
87
88 eina_value_free(value_struct);
89 eina_value_free(value_array);
90}
91
92static void
93on_send_array(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
94{
95 Eina_Value *v, array;
96 const char *txt;
97 unsigned i;
98 printf("2 - on_send_array()\n");
99
100 if (edbus_message_error_get(msg, NULL, NULL))
101 {
102 printf("Message error\n\n");
103 return;
104 }
105
106 v = edbus_message_to_eina_value(msg);
107 eina_value_struct_value_get(v, "arg0", &array);
108 for (i = 0; i < eina_value_array_count(&array); i++)
109 {
110 eina_value_array_get(&array, i, &txt);
111 printf("\t%s\n", txt);
112 }
113
114 eina_value_free(v);
115 eina_value_flush(&array);
116}
117
118static void
119on_receive_array_with_size(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
120{
121 const char *errname;
122 const char *errmsg;
123
124 printf("1 - on_receive_array_with_size()\n");
125 if (edbus_message_error_get(msg, &errname, &errmsg))
126 {
127 fprintf(stderr, "Error: %s %s\n", errname, errmsg);
128 }
129}
130
131static void
132on_plus_one(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
133{
134 Eina_Value *v;
135 int num2;
136
137 if (edbus_message_error_get(msg, NULL, NULL))
138 {
139 printf("Message error\n\n");
140 return;
141 }
142
143 v = edbus_message_to_eina_value(msg);
144 eina_value_struct_get(v, "arg0", &num2);
145
146 printf("3 - on_plus_one() %d\n", num2);
147 eina_value_free(v);
148}
149
150static void
151receive_variant_cb(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
152{
153 Eina_Value *v, variant, array;
154 unsigned i;
155
156 printf("4 - receive a variant with an array of strings\n");
157 if (edbus_message_error_get(msg, NULL, NULL))
158 {
159 printf("Message error\n\n");
160 return;
161 }
162
163 v = edbus_message_to_eina_value(msg);
164
165 eina_value_struct_value_get(v, "arg0", &variant);
166 eina_value_struct_value_get(&variant, "arg0", &array);
167 for (i = 0; i < eina_value_array_count(&array); i++)
168 {
169 const char *txt;
170 eina_value_array_get(&array, i, &txt);
171 printf("\t%s\n", txt);
172 }
173
174 eina_value_flush(&array);
175 eina_value_flush(&variant);
176 eina_value_free(v);
177}
178
179static void
180_property_removed(void *data, EDBus_Proxy *proxy, void *event_info)
181{
182 EDBus_Proxy_Event_Property_Removed *event = event_info;
183
184 printf("\nproperty removed: %s", event->name);
185}
186
187static void
188_property_changed(void *data, EDBus_Proxy *proxy, void *event_info)
189{
190 EDBus_Proxy_Event_Property_Changed *event = event_info;
191 const char *name;
192 const Eina_Value *value;
193 printf("\nproperty changed\n");
194
195 name = event->name;
196 value = event->value;
197
198 if (!strcmp(name, "text") || !strcmp(name, "Resp2"))
199 {
200 const char *txt;
201 eina_value_get(value, &txt);
202 printf("[%s] = %s\n", name, txt);
203 }
204 else if (!strcmp(name, "int32"))
205 {
206 int num;
207 eina_value_get(value, &num);
208 printf("[%s] = %d\n", name, num);
209 }
210 else if (!strcmp(name, "st"))
211 {
212 const char *txt;
213 eina_value_struct_get(value, "arg0", &txt);
214 printf("[%s] %s | ", name, txt);
215 eina_value_struct_get(value, "arg1", &txt);
216 printf("%s\n", txt);
217 }
218}
219
220static Eina_Bool
221_read_cache(void *data)
222{
223 EDBus_Proxy *proxy = data;
224 const char *txt;
225 int num;
226 Eina_Value *v;
227
228 v = edbus_proxy_property_local_get(proxy, "text");
229 if (v)
230 {
231 eina_value_get(v, &txt);
232 printf("Read cache: [txt] = %s\n", txt);
233 }
234
235 v = edbus_proxy_property_local_get(proxy, "int32");
236 if (v)
237 {
238 eina_value_get(v, &num);
239 printf("Read cache: [int32] = %d\n", num);
240 }
241
242 v = edbus_proxy_property_local_get(proxy, "st");
243 if (v)
244 {
245 eina_value_struct_get(v, "arg0", &txt);
246 printf("Read cache: [st] %s | ", txt);
247 eina_value_struct_get(v, "arg1", &txt);
248 printf("%s\n", txt);
249 }
250
251 return EINA_FALSE;
252}
253
254static void
255_fill_plus_one(EDBus_Message *msg, int num)
256{
257 Eina_Value *v;
258 Eina_Value_Struct_Member main_members[] = {
259 {"num", EINA_VALUE_TYPE_INT, 0}
260 };
261 Eina_Value_Struct_Desc desc_struct = {
262 EINA_VALUE_STRUCT_DESC_VERSION,
263 NULL, // no special operations
264 main_members,
265 1,
266 sizeof(int)
267 };
268 v = eina_value_struct_new(&desc_struct);
269 eina_value_struct_set(v, "num", num);
270
271 edbus_message_from_eina_value("i", msg, v);
272
273 eina_value_free(v);
274}
275
276int
277main(void)
278{
279 EDBus_Connection *conn;
280 EDBus_Object *obj;
281 EDBus_Proxy *proxy;
282 EDBus_Message *msg;
283
284 ecore_init();
285 edbus_init();
286
287 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
288 obj = edbus_object_get(conn, BUS, PATH);
289 proxy = edbus_proxy_get(obj, IFACE);
290
291 msg = edbus_proxy_method_call_new(proxy, "ReceiveArrayOfStringIntWithSize");
292 _fill_receive_array_of_string_int_with_size(msg, size_of_array, array_string);
293 edbus_proxy_send(proxy, msg, on_receive_array_with_size, NULL, -1);
294 edbus_message_unref(msg);
295
296 edbus_proxy_call(proxy, "SendArray", on_send_array, NULL, -1 , "");
297
298 msg = edbus_proxy_method_call_new(proxy, "PlusOne");
299 _fill_plus_one(msg, 14);
300 edbus_proxy_send(proxy, msg, on_plus_one, NULL, -1);
301 edbus_message_unref(msg);
302
303 edbus_proxy_event_callback_add(proxy,
304 EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
305 _property_changed, NULL);
306 edbus_proxy_event_callback_add(proxy, EDBUS_PROXY_EVENT_PROPERTY_REMOVED,
307 _property_removed, NULL);
308
309 edbus_proxy_properties_monitor(proxy, EINA_TRUE);
310 ecore_timer_add(10, _read_cache, proxy);
311
312 edbus_proxy_call(proxy, "ReceiveVariantData", receive_variant_cb, NULL, -1, "");
313
314 ecore_main_loop_begin();
315
316 edbus_proxy_event_callback_del(proxy, EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
317 _property_changed, NULL);
318 edbus_connection_unref(conn);
319
320 edbus_shutdown();
321 ecore_shutdown();
322 return 0;
323}
diff --git a/src/examples/edbus/complex-types-server.c b/src/examples/edbus/complex-types-server.c
new file mode 100644
index 0000000000..5e8742236c
--- /dev/null
+++ b/src/examples/edbus/complex-types-server.c
@@ -0,0 +1,390 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "com.profusion"
5#define PATH "/com/profusion/Test"
6#define IFACE "com.profusion.Test"
7
8static char *resp2;
9/* dummy, incremented each time DBus.Properties.Get() is called */
10static int int32 = 35;
11static Ecore_Timer *timer;
12
13static EDBus_Message *
14_receive_array(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
15{
16 EDBus_Message *reply = edbus_message_method_return_new(msg);
17 EDBus_Message_Iter *array;
18 const char *txt;
19
20 printf("- receiveArray\n");
21 if (!edbus_message_arguments_get(msg, "as", &array))
22 {
23 printf("Error on edbus_message_arguments_get()\n");
24 return reply;
25 }
26
27 while (edbus_message_iter_get_and_next(array, 's', &txt))
28 printf("%s\n", txt);
29 printf("}\n\n");
30
31 return reply;
32}
33
34static EDBus_Message *
35_receive_array_of_string_int_with_size(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
36{
37 EDBus_Message *reply = edbus_message_method_return_new(msg);
38 EDBus_Message_Iter *array, *struct_si;
39 int size, i = 0;
40
41 printf("- receiveArrayOfStringIntWithSize\n{\n");
42 if (!edbus_message_arguments_get(msg, "ia(si)", &size, &array))
43 {
44 printf("Error on edbus_message_arguments_get()\n");
45 return reply;
46 }
47
48 while (edbus_message_iter_get_and_next(array, 'r', &struct_si))
49 {
50 const char *txt;
51 int num;
52 if (!edbus_message_iter_arguments_get(struct_si, "si", &txt, &num))
53 {
54 printf("Error on edbus_message_arguments_get()\n");
55 return reply;
56 }
57 printf("%s | %d\n", txt, num);
58 i++;
59 }
60 printf("size in msg %d | size read %d\n", size, i);
61 printf("}\n\n");
62
63 return reply;
64}
65
66static EDBus_Message *
67_receive_variant(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
68{
69 EDBus_Message *reply = edbus_message_method_return_new(msg);
70 EDBus_Message_Iter *var, *array, *main_iter;
71
72 main_iter = edbus_message_iter_get(reply);
73 var = edbus_message_iter_container_new(main_iter, 'v', "as");
74 edbus_message_iter_arguments_append(var, "as", &array);
75
76 edbus_message_iter_arguments_append(array, "s", "item1");
77 edbus_message_iter_arguments_append(array, "s", "item2");
78 edbus_message_iter_arguments_append(array, "s", "item3");
79
80 edbus_message_iter_container_close(var, array);
81 edbus_message_iter_container_close(main_iter, var);
82
83 return reply;
84}
85
86static EDBus_Message *
87_send_variant(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
88{
89 EDBus_Message *reply = edbus_message_method_return_new(msg);
90 EDBus_Message_Iter *variant;
91 char *type;
92
93 printf("- sendVariantData\n{\n");
94 if (!edbus_message_arguments_get(msg, "v", &variant))
95 {
96 printf("Error on edbus_message_arguments_get()\n");
97 return reply;
98 }
99
100 type = edbus_message_iter_signature_get(variant);
101 if (type[1])
102 {
103 printf("It is a complex type, not handle yet.\n");
104 free(type);
105 return reply;
106 }
107
108 switch (type[0])
109 {
110 case 's':
111 case 'o':
112 {
113 char *txt;
114 edbus_message_iter_arguments_get(variant, type, &txt);
115 printf("type = %c value = %s\n", type[0], txt);
116 break;
117 }
118 case 'i':
119 {
120 int num;
121 edbus_message_iter_arguments_get(variant, type, &num);
122 printf("type = %c value = %d\n", type[0], num);
123 break;
124 }
125 default:
126 {
127 printf("Unhandled type\n");
128 }
129 }
130
131 printf("}\n\n");
132
133 free(type);
134 return reply;
135}
136
137static EDBus_Message *
138_send_array_int(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
139{
140 EDBus_Message *reply = edbus_message_method_return_new(msg);
141 EDBus_Message_Iter *iter, *array;
142 int i;
143
144 printf("- sendArrayInt\n\n");
145
146 iter = edbus_message_iter_get(reply);
147 array = edbus_message_iter_container_new(iter, 'a', "i");
148 for (i = 0; i < 5; i++)
149 edbus_message_iter_arguments_append(array, "i", i);
150 edbus_message_iter_container_close(iter, array);
151
152 return reply;
153}
154
155static EDBus_Message *
156_send_array(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
157{
158 EDBus_Message *reply = edbus_message_method_return_new(msg);
159 EDBus_Message_Iter *iter, *array;
160 const char *array_string[5] = {"qqqq", "wwwww", "eeeeee", "rrrrr", "ttttt"};
161 int i;
162
163 printf("sendArray\n\n");
164
165 iter = edbus_message_iter_get(reply);
166 array = edbus_message_iter_container_new(iter, 'a', "s");
167 for (i = 0; i < 5; i++)
168 edbus_message_iter_arguments_append(array, "s", array_string[i]);
169 edbus_message_iter_container_close(iter, array);
170
171 return reply;
172}
173
174static EDBus_Message *
175_plus_one(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
176{
177 EDBus_Message *reply = edbus_message_method_return_new(msg);
178 int num;
179
180 printf("- plusOne\n\n");
181 if (!edbus_message_arguments_get(msg, "i", &num))
182 {
183 printf("Error on edbus_message_arguments_get()\n");
184 return reply;
185 }
186 num++;
187 edbus_message_arguments_append(reply, "i", num);
188
189 return reply;
190}
191
192static EDBus_Message *
193_double_container(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
194{
195 EDBus_Message_Iter *array1, *array2, *structure;
196 int num1, num2;
197 EDBus_Message *reply = edbus_message_method_return_new(msg);
198
199 if (!edbus_message_arguments_get(msg, "a(ii)a(ii)", &array1, &array2))
200 {
201 printf("Error on edbus_message_arguments_get()\n");
202 return NULL;
203 }
204
205 printf("DoubleCountainer\n{\nArray1:\n");
206 while (edbus_message_iter_get_and_next(array1, 'r', &structure))
207 {
208 edbus_message_iter_arguments_get(structure, "ii", &num1, &num2);
209 printf("1 %d - 2 %d\n", num1, num2);
210 }
211
212 printf("Array2:\n");
213 while (edbus_message_iter_get_and_next(array2, 'r', &structure))
214 {
215 edbus_message_iter_arguments_get(structure, "ii", &num1, &num2);
216 printf("1 %d - 2 %d\n", num1, num2);
217 }
218 printf("}\n\n");
219 return reply;
220}
221
222static Eina_Bool
223_properties_get(const EDBus_Service_Interface *iface, const char *propname, EDBus_Message_Iter *iter, const EDBus_Message *request_msg, EDBus_Message **error)
224{
225 printf("Properties_get - %s\n", propname);
226 if (!strcmp(propname, "Resp2"))
227 edbus_message_iter_basic_append(iter, 's', resp2);
228 else if (!strcmp(propname, "text"))
229 edbus_message_iter_basic_append(iter, 's', "lalalala");
230 else if (!strcmp(propname, "int32"))
231 {
232 edbus_message_iter_arguments_append(iter, "i", int32);
233 int32++;
234 }
235 else if (!strcmp(propname, "st"))
236 {
237 EDBus_Message_Iter *st;
238 edbus_message_iter_arguments_append(iter, "(ss)", &st);
239 edbus_message_iter_arguments_append(st, "ss", "string1", "string2");
240 edbus_message_iter_container_close(iter, st);
241 }
242 return EINA_TRUE;
243}
244
245static EDBus_Message *
246_properties_set(const EDBus_Service_Interface *iface, const char *propname, EDBus_Message_Iter *iter, const EDBus_Message *msg)
247{
248 char *type;
249
250 type = edbus_message_iter_signature_get(iter);
251
252 if (!strcmp(propname, "int32"))
253 {
254 int num;
255 if (type[0] != 'i')
256 goto invalid_signature;
257 edbus_message_iter_arguments_get(iter, "i", &num);
258 printf("int32 was set to: %d, previously was: %d\n", num, int32);
259 int32 = num;
260 }
261 else if (!strcmp(propname, "Resp2"))
262 {
263 const char *txt;
264 if (type[0] != 's')
265 goto invalid_signature;
266 edbus_message_iter_arguments_get(iter, "s", &txt);
267 printf("Resp2 was set to: %s, previously was: %s\n", txt, resp2);
268 free(resp2);
269 resp2 = strdup(txt);
270 }
271 free(type);
272 return edbus_message_method_return_new(msg);
273
274invalid_signature:
275 free(type);
276 return edbus_message_error_new(msg, "org.freedesktop.DBus.Error.InvalidSignature",
277 "Invalid type.");
278}
279
280static const EDBus_Method methods[] = {
281 {
282 "ReceiveArray", EDBUS_ARGS({"as", "array_of_strings"}),
283 NULL, _receive_array
284 },
285 {
286 "ReceiveArrayOfStringIntWithSize",
287 EDBUS_ARGS({"i", "size_of_array"}, {"a(si)", "array"}),
288 NULL, _receive_array_of_string_int_with_size, 0
289 },
290 {
291 "SendVariantData", EDBUS_ARGS({"v", "variant_data"}),
292 NULL, _send_variant
293 },
294 {
295 "ReceiveVariantData", NULL, EDBUS_ARGS({"v", "variant_data"}),
296 _receive_variant
297 },
298 {
299 "SendArrayInt", NULL,
300 EDBUS_ARGS({"ai", "array_of_int"}), _send_array_int, 0
301 },
302 {
303 "SendArray", NULL, EDBUS_ARGS({"as", "array_string"}),
304 _send_array
305 },
306 {
307 "PlusOne", EDBUS_ARGS({"i", "integer"}),
308 EDBUS_ARGS({"i", "integer_plus_one"}), _plus_one
309 },
310 {
311 "DoubleContainner", EDBUS_ARGS({"a(ii)", "array1"}, {"a(ii)", "array2"}),
312 NULL, _double_container
313 },
314 { }
315};
316
317static const EDBus_Property properties[] = {
318 { "Resp2", "s", NULL, _properties_set },
319 { "text", "s" },
320 { "int32", "i", NULL, _properties_set },
321 { "st", "(ss)" },
322 { }
323};
324
325static const EDBus_Service_Interface_Desc iface_desc = {
326 IFACE, methods, NULL, properties, _properties_get
327};
328
329static Eina_Bool _emit_changed(void *data)
330{
331 EDBus_Service_Interface *iface = data;
332 edbus_service_property_changed(iface, "int32");
333 edbus_service_property_invalidate_set(iface, "Resp2", EINA_TRUE);
334 return ECORE_CALLBACK_RENEW;
335}
336
337static void
338on_name_request(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
339{
340 unsigned int reply;
341 EDBus_Service_Interface *iface = data;
342
343 if (edbus_message_error_get(msg, NULL, NULL))
344 {
345 printf("error on on_name_request\n");
346 return;
347 }
348
349 if (!edbus_message_arguments_get(msg, "u", &reply))
350 {
351 printf("error geting arguments on on_name_request\n");
352 return;
353 }
354
355 if (reply != EDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
356 {
357 printf("error name already in use\n");
358 return;
359 }
360
361 timer = ecore_timer_add(3, _emit_changed, iface);
362}
363
364int
365main(void)
366{
367 EDBus_Connection *conn;
368 EDBus_Service_Interface *iface;
369
370 ecore_init();
371 edbus_init();
372
373 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
374
375 resp2 = malloc(sizeof(char) * 5);
376 strcpy(resp2, "test");
377 iface = edbus_service_interface_register(conn, PATH, &iface_desc);
378 edbus_name_request(conn, BUS, EDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
379 on_name_request, iface);
380
381 ecore_main_loop_begin();
382
383 free(resp2);
384 ecore_timer_del(timer);
385 edbus_connection_unref(conn);
386
387 edbus_shutdown();
388 ecore_shutdown();
389 return 0;
390}
diff --git a/src/examples/edbus/complex-types.c b/src/examples/edbus/complex-types.c
new file mode 100644
index 0000000000..a0af846f5f
--- /dev/null
+++ b/src/examples/edbus/complex-types.c
@@ -0,0 +1,285 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "com.profusion"
5#define PATH "/com/profusion/Test"
6#define IFACE "com.profusion.Test"
7
8EDBus_Connection *conn;
9
10static Eina_Bool
11_timer1_cb(void *data)
12{
13 printf("\nFishing...\n");
14 ecore_main_loop_quit();
15 return ECORE_CALLBACK_CANCEL;
16}
17
18static void
19on_plus_one(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
20{
21 int num2 = 0;
22
23 if (edbus_message_error_get(msg, NULL, NULL))
24 {
25 printf("Message error\n\n");
26 return;
27 }
28 if (!edbus_message_arguments_get(msg, "i", &num2))
29 {
30 printf("Error getting arguments.");
31 return;
32 }
33
34 printf("on_plus_one() %d\n", num2);
35}
36
37static void
38set_property_resp2(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
39{
40 const char *errname;
41 const char *errmsg;
42
43 printf("set_property_resp2()\n");
44 if (edbus_message_error_get(msg, &errname, &errmsg))
45 {
46 printf("Message error %s - %s\n\n", errname, errmsg);
47 return;
48 }
49}
50
51static void
52get_property_resp2(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
53{
54 EDBus_Proxy *proxy = data;
55 EDBus_Message_Iter *variant = NULL;
56 char *type;
57 char *resp2;
58 const char *errname;
59 const char *errmsg;
60
61 printf("get_property_resp2()\n");
62 if (edbus_message_error_get(msg, &errname, &errmsg))
63 {
64 printf("Message error %s - %s\n\n", errname, errmsg);
65 return;
66 }
67 if (!edbus_message_arguments_get(msg, "v", &variant))
68 {
69 printf("Error getting arguments.");
70 return;
71 }
72
73 type = edbus_message_iter_signature_get(variant);
74 if (type[1])
75 {
76 printf("It is a complex type, not handle yet.\n\n");
77 return;
78 }
79 if (type[0] != 's')
80 {
81 printf("Expected type is string.\n\n");
82 return;
83 }
84 if (!edbus_message_iter_arguments_get(variant, "s", &resp2))
85 {
86 printf("error in edbus_message_iter_arguments_get()\n\n");
87 return;
88 }
89 printf("resp2=%s\n", resp2);
90 free(type);
91
92 edbus_proxy_property_set(proxy, "Resp2", "s", &"lalala", set_property_resp2, NULL);
93 edbus_proxy_property_set(proxy, "int32", "i", (void*)(intptr_t)99, set_property_resp2, NULL);
94}
95
96static void
97on_send_array_int(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
98{
99 EDBus_Message_Iter *array = NULL;
100 int num;
101
102 printf("on_send_array_int()\n");
103 if (edbus_message_error_get(msg, NULL, NULL))
104 {
105 printf("Message error\n\n");
106 return;
107 }
108 if (!edbus_message_arguments_get(msg, "ai", &array))
109 {
110 printf("Error getting arguments.");
111 return;
112 }
113
114 while (edbus_message_iter_get_and_next(array, 'i', &num))
115 {
116 printf("%d\n", num);
117 }
118}
119
120static void
121on_send_array(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
122{
123 EDBus_Message_Iter *array = NULL;
124 char *txt = NULL;
125 char *string[10];
126 int i = 0;
127 int z;
128
129 printf("on_send_array()\n");
130 if (edbus_message_error_get(msg, NULL, NULL))
131 {
132 printf("Message error\n\n");
133 return;
134 }
135 if (!edbus_message_arguments_get(msg, "as", &array))
136 {
137 printf("Error getting arguments.");
138 return;
139 }
140
141 while (edbus_message_iter_get_and_next(array, 's', &txt))
142 {
143 string[i] = txt;
144 i++;
145 }
146
147 for (z = 0; z < i; z++)
148 printf("string = %s\n", string[z]);
149}
150
151static void
152on_receive_array_with_size(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
153{
154 const char *errname;
155 const char *errmsg;
156
157 printf("on_receive_array_with_size()\n");
158 if (edbus_message_error_get(msg, &errname, &errmsg))
159 {
160 fprintf(stderr, "Error: %s %s\n", errname, errmsg);
161 }
162}
163
164static void
165on_send_variant(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
166{
167 printf("on_send_variant()\n\n");
168}
169
170static void
171on_receive_array(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
172{
173 const char *errname;
174 const char *errmsg;
175
176 printf("on_receive_array()\n");
177 if (edbus_message_error_get(msg, &errname, &errmsg))
178 {
179 fprintf(stderr, "Error: %s %s\n", errname, errmsg);
180 }
181}
182
183int
184main(void)
185{
186 EDBus_Object *test2_obj;
187 EDBus_Proxy *test2_proxy;
188 EDBus_Pending *pending;
189 EDBus_Message_Iter *iter, *array_of_string, *variant;
190 EDBus_Message_Iter *array_itr, *structure;
191 EDBus_Message *msg;
192 int size_of_array = 5;
193 const char *array[5] = { "aaaa", "bbbb", "cccc", "dddd", "eeee" };
194 int i;
195 int plus_one = 24;
196
197 ecore_init();
198 edbus_init();
199
200 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
201
202 test2_obj = edbus_object_get(conn, BUS, PATH);
203 test2_proxy = edbus_proxy_get(test2_obj, IFACE);
204
205 msg = edbus_proxy_method_call_new(test2_proxy, "ReceiveArray");
206 iter = edbus_message_iter_get(msg);
207 array_of_string = edbus_message_iter_container_new(iter, 'a',"s");
208 if (!array_of_string) printf("array_of_string == NULL\n\n");
209 for (i = 0; i < 5; i++)
210 edbus_message_iter_basic_append(array_of_string, 's', array[i]);
211 edbus_message_iter_container_close(iter, array_of_string);
212 pending = edbus_proxy_send(test2_proxy, msg, on_receive_array, NULL, -1);
213 if (!pending) printf("Error in edbus_proxy_send()\n\n");
214 edbus_message_unref(msg);
215
216 msg = edbus_proxy_method_call_new(test2_proxy, "ReceiveArrayOfStringIntWithSize");
217 iter = edbus_message_iter_get(msg);
218 if (!edbus_message_iter_arguments_append(iter, "ia(si)", size_of_array, &array_of_string))
219 printf("error on edbus_massage_iterator_arguments_set()\n\n");
220 for (i = 0; i < size_of_array; i++)
221 {
222 EDBus_Message_Iter *struct_of_si;
223 edbus_message_iter_arguments_append(array_of_string, "(si)", &struct_of_si);
224 edbus_message_iter_arguments_append(struct_of_si, "si", array[i], i);
225 edbus_message_iter_container_close(array_of_string, struct_of_si);
226 }
227 edbus_message_iter_container_close(iter, array_of_string);
228 pending = edbus_proxy_send(test2_proxy, msg, on_receive_array_with_size, NULL, -1);
229 edbus_message_unref(msg);
230
231 msg = edbus_proxy_method_call_new(test2_proxy, "SendVariantData");
232 iter = edbus_message_iter_get(msg);
233 variant = edbus_message_iter_container_new(iter, 'v', "s");
234 edbus_message_iter_basic_append(variant, 's', "test");
235 edbus_message_iter_container_close(iter, variant);
236 pending = edbus_proxy_send(test2_proxy, msg, on_send_variant, NULL, -1);
237 edbus_message_unref(msg);
238
239 msg = edbus_proxy_method_call_new(test2_proxy, "DoubleContainner");
240 iter = edbus_message_iter_get(msg);
241 /**
242 * edbus_message_iterator_arguments_set(itr, "a(ii)a(ii)", &array_itr, &array_itr2);
243 * this will cause a error, we could not open another container until
244 * we close the first one
245 */
246 edbus_message_iter_arguments_append(iter, "a(ii)", &array_itr);
247 for (i = 0; i < 5; i++)
248 {
249 edbus_message_iter_arguments_append(array_itr, "(ii)", &structure);
250 edbus_message_iter_arguments_append(structure, "ii", i, i*i);
251 edbus_message_iter_container_close(array_itr, structure);
252 }
253 edbus_message_iter_container_close(iter, array_itr);
254 edbus_message_iter_arguments_append(iter, "a(ii)", &array_itr);
255 for (i = 0; i < 7; i++)
256 {
257 edbus_message_iter_arguments_append(array_itr, "(ii)", &structure);
258 edbus_message_iter_arguments_append(structure, "ii", i, i*i*i);
259 edbus_message_iter_container_close(array_itr, structure);
260 }
261 edbus_message_iter_container_close(iter, array_itr);
262 edbus_proxy_send(test2_proxy, msg, NULL, NULL, -1);
263 edbus_message_unref(msg);
264
265 pending = edbus_proxy_call(test2_proxy, "SendArrayInt", on_send_array_int, NULL,
266 -1 , "");
267
268 pending = edbus_proxy_call(test2_proxy, "SendArray", on_send_array, NULL,
269 -1 , "");
270
271 pending = edbus_proxy_call(test2_proxy, "PlusOne", on_plus_one, NULL,
272 -1 , "i", plus_one);
273
274 pending = edbus_proxy_property_get(test2_proxy, "Resp2", get_property_resp2, test2_proxy);
275
276 ecore_timer_add(10, _timer1_cb, NULL);
277
278 ecore_main_loop_begin();
279
280 edbus_connection_unref(conn);
281
282 edbus_shutdown();
283 ecore_shutdown();
284 return 0;
285}
diff --git a/src/examples/edbus/connman-list-services.c b/src/examples/edbus/connman-list-services.c
new file mode 100644
index 0000000000..a192d8b506
--- /dev/null
+++ b/src/examples/edbus/connman-list-services.c
@@ -0,0 +1,107 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4static void
5on_services_get(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
6{
7 EDBus_Message_Iter *array, *entry;
8 const char *errname, *errmsg;
9
10 if (edbus_message_error_get(msg, &errname, &errmsg))
11 {
12 fprintf(stderr, "Error: %s %s\n", errname, errmsg);
13 return;
14 }
15
16 if (!edbus_message_arguments_get(msg, "a(oa{sv})", &array))
17 {
18 fprintf(stderr, "Error: could not get array\n");
19 return;
20 }
21
22 while (edbus_message_iter_get_and_next(array, 'r', &entry))
23 {
24 EDBus_Message_Iter *properties, *dict_entry;
25 const char *path;
26
27 if (!edbus_message_iter_arguments_get(entry, "oa{sv}", &path, &properties))
28 {
29 fprintf(stderr, "Error: could not get entry contents\n");
30 return;
31 }
32
33 printf("service: %s\n", path);
34
35 while (edbus_message_iter_get_and_next(properties, 'e', &dict_entry))
36 {
37 EDBus_Message_Iter *variant;
38 const char *key;
39
40 if (!edbus_message_iter_arguments_get(dict_entry, "sv", &key,
41 &variant))
42 {
43 fprintf(stderr,
44 "Error: could not get property contents\n");
45 return;
46 }
47
48 printf("\t%s: type %s\n", key,
49 edbus_message_iter_signature_get(variant));
50
51 /* TODO: get the value from variant */
52 }
53 }
54}
55
56int
57main(void)
58{
59 EDBus_Connection *conn;
60 EDBus_Object *obj;
61 EDBus_Proxy *manager;
62 EDBus_Pending *pending;
63
64 ecore_init();
65 edbus_init();
66
67 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SYSTEM);
68 if (!conn)
69 {
70 fprintf(stderr, "Error: could not get system bus\n");
71 return EXIT_FAILURE;
72 }
73
74 obj = edbus_object_get(conn, "net.connman", "/");
75 if (!obj)
76 {
77 fprintf(stderr, "Error: could not get object\n");
78 return EXIT_FAILURE;
79 }
80
81 manager = edbus_proxy_get(obj, "net.connman.Manager");
82 if (!manager)
83 {
84 fprintf(stderr, "Error: could not get proxy\n");
85 return EXIT_FAILURE;
86 }
87
88 pending = edbus_proxy_call(manager, "GetServices", on_services_get, NULL,
89 -1, "");
90
91 if (!pending)
92 {
93 fprintf(stderr, "Error: could not call\n");
94 return EXIT_FAILURE;
95 }
96
97 ecore_main_loop_begin();
98
99 edbus_proxy_unref(manager);
100 edbus_object_unref(obj);
101 edbus_connection_unref(conn);
102
103 edbus_shutdown();
104 ecore_shutdown();
105 return 0;
106}
107
diff --git a/src/examples/edbus/ofono-dial.c b/src/examples/edbus/ofono-dial.c
new file mode 100644
index 0000000000..5e833ddc4b
--- /dev/null
+++ b/src/examples/edbus/ofono-dial.c
@@ -0,0 +1,85 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4static void
5on_dial(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
6{
7 const char *errname, *errmsg;
8 const char *call_path;
9
10 if (edbus_message_error_get(msg, &errname, &errmsg))
11 {
12 fprintf(stderr, "Error: %s %s\n", errname, errmsg);
13 return;
14 }
15
16 if (!edbus_message_arguments_get(msg, "o", &call_path))
17 {
18 fprintf(stderr, "Error: could not get call path\n");
19 return;
20 }
21
22 printf("dialed! call path: %s\n", call_path);
23}
24
25int
26main(int argc, char *argv[])
27{
28 EDBus_Connection *conn;
29 EDBus_Object *obj;
30 EDBus_Proxy *manager;
31 EDBus_Pending *pending;
32 const char *number, *hide_callerid;
33
34 if (argc < 2)
35 {
36 fprintf(stderr, "Usage:\n\t%s <number> [hide_callerid]\n", argv[0]);
37 return EXIT_FAILURE;
38 }
39
40 number = argv[1];
41 hide_callerid = (argc > 2) ? argv[2] : "";
42
43 ecore_init();
44 edbus_init();
45
46 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SYSTEM);
47 if (!conn)
48 {
49 fprintf(stderr, "Error: could not get system bus\n");
50 return EXIT_FAILURE;
51 }
52
53 obj = edbus_object_get(conn, "org.ofono", "/");
54 if (!obj)
55 {
56 fprintf(stderr, "Error: could not get object\n");
57 return EXIT_FAILURE;
58 }
59
60 manager = edbus_proxy_get(obj, "org.ofono.Manager");
61 if (!manager)
62 {
63 fprintf(stderr, "Error: could not get proxy\n");
64 return EXIT_FAILURE;
65 }
66
67 pending = edbus_proxy_call(manager, "Dial", on_dial, NULL,
68 -1, "ss", number, hide_callerid);
69 if (!pending)
70 {
71 fprintf(stderr, "Error: could not call\n");
72 return EXIT_FAILURE;
73 }
74
75 ecore_main_loop_begin();
76
77 edbus_proxy_unref(manager);
78 edbus_object_unref(obj);
79 edbus_connection_unref(conn);
80
81 edbus_shutdown();
82 ecore_shutdown();
83 return 0;
84}
85
diff --git a/src/examples/edbus/server.c b/src/examples/edbus/server.c
new file mode 100644
index 0000000000..4953dde971
--- /dev/null
+++ b/src/examples/edbus/server.c
@@ -0,0 +1,245 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "org.Enlightenment"
5#define PATH "/org/enlightenment"
6#define PATH_TEST_SON "/org/enlightenment/son"
7#define INTERFACE "org.enlightenment.Test"
8
9static EDBus_Connection *conn;
10
11static EDBus_Message *
12_hello(const EDBus_Service_Interface *iface, const EDBus_Message *message)
13{
14 EDBus_Message *reply = edbus_message_method_return_new(message);
15 edbus_message_arguments_append(reply, "s", "Hello World");
16 printf("Hello\n");
17 return reply;
18}
19
20static EDBus_Message *
21_quit(const EDBus_Service_Interface *iface, const EDBus_Message *message)
22{
23 printf("Quit\n");
24 ecore_main_loop_quit();
25 return edbus_message_method_return_new(message);
26}
27
28enum
29{
30 TEST_SIGNAL_ALIVE = 0,
31 TEST_SIGNAL_HELLO
32};
33
34static Eina_Bool
35send_signal_alive(void *data)
36{
37 EDBus_Service_Interface *iface = data;
38 edbus_service_signal_emit(iface, TEST_SIGNAL_ALIVE);
39 return ECORE_CALLBACK_RENEW;
40}
41
42static Eina_Bool
43send_signal_hello(void *data)
44{
45 EDBus_Service_Interface *iface = data;
46 edbus_service_signal_emit(iface, TEST_SIGNAL_HELLO, "Hello World");
47 return ECORE_CALLBACK_RENEW;
48}
49
50static EDBus_Message *
51_send_bool(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
52{
53 EDBus_Message *reply = edbus_message_method_return_new(msg);
54 Eina_Bool bool;
55 if (!edbus_message_arguments_get(msg, "b", &bool))
56 printf("edbus_message_arguments_get() error\n");
57 edbus_message_arguments_append(reply, "b", bool);
58 return reply;
59}
60
61static EDBus_Message *
62_send_byte(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
63{
64 EDBus_Message *reply = edbus_message_method_return_new(msg);
65 unsigned char byte;
66 if (!edbus_message_arguments_get(msg, "y", &byte))
67 printf("edbus_message_arguments_get() error\n");
68 edbus_message_arguments_append(reply, "y", byte);
69 return reply;
70}
71
72static EDBus_Message *
73_send_uint32(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
74{
75 EDBus_Message *reply = edbus_message_method_return_new(msg);
76 unsigned int uint32;
77 if (!edbus_message_arguments_get(msg, "u", &uint32))
78 printf("edbus_message_arguments_get() error\n");
79 edbus_message_arguments_append(reply, "u", uint32);
80 return reply;
81}
82
83static EDBus_Message *
84_send_int32(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
85{
86 EDBus_Message *reply = edbus_message_method_return_new(msg);
87 int int32;
88 if (!edbus_message_arguments_get(msg, "i", &int32))
89 printf("edbus_message_arguments_get() error\n");
90 edbus_message_arguments_append(reply, "i", int32);
91 return reply;
92}
93
94static EDBus_Message *
95_send_int16(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
96{
97 EDBus_Message *reply = edbus_message_method_return_new(msg);
98 short int int16;
99 if (!edbus_message_arguments_get(msg, "n", &int16))
100 printf("edbus_message_arguments_get() error\n");
101 edbus_message_arguments_append(reply, "n", int16);
102 return reply;
103}
104
105static EDBus_Message *
106_send_double(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
107{
108 EDBus_Message *reply = edbus_message_method_return_new(msg);
109 double d;
110 if (!edbus_message_arguments_get(msg, "d", &d))
111 printf("edbus_message_arguments_get() error\n");
112 edbus_message_arguments_append(reply, "d", d);
113 return reply;
114}
115
116static EDBus_Message *
117_send_string(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
118{
119 EDBus_Message *reply = edbus_message_method_return_new(msg);
120 const char *txt;
121 if (!edbus_message_arguments_get(msg, "s", &txt))
122 printf("edbus_message_arguments_get() error\n");
123 edbus_message_arguments_append(reply, "s", txt);
124 return reply;
125}
126
127static Eina_Bool
128_resp_async(void *data)
129{
130 EDBus_Message *msg = data;
131 edbus_message_arguments_append(msg, "s", "Async test ok");
132 edbus_connection_send(conn, msg, NULL, NULL, -1);
133 edbus_message_unref(msg);
134 return ECORE_CALLBACK_CANCEL;
135}
136
137static EDBus_Message *
138_async_test(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
139{
140 EDBus_Message *reply = edbus_message_method_return_new(msg);
141 printf("Received a call to AsyncTest.\n");
142 printf("Response will be send in 5 seconds.\n");
143 ecore_timer_add(5, _resp_async, reply);
144 return NULL;
145}
146
147static const EDBus_Signal signals[] = {
148 [TEST_SIGNAL_ALIVE] = {"Alive", NULL, 0},
149 [TEST_SIGNAL_HELLO] = {"Hello", EDBUS_ARGS({ "s", "message" }), 0},
150 { }
151};
152
153static const EDBus_Method methods[] = {
154 {
155 "Hello", NULL, EDBUS_ARGS({"s", "message"}),
156 _hello
157 },
158 {
159 "Quit", NULL, NULL,
160 _quit, EDBUS_METHOD_FLAG_DEPRECATED
161 },
162 { "SendBool", EDBUS_ARGS({"b", "bool"}), EDBUS_ARGS({"b", "bool"}),
163 _send_bool
164 },
165 { "SendByte", EDBUS_ARGS({"y", "byte"}), EDBUS_ARGS({"y", "byte"}),
166 _send_byte
167 },
168 { "SendUint32", EDBUS_ARGS({"u", "uint32"}), EDBUS_ARGS({"u", "uint32"}),
169 _send_uint32
170 },
171 { "SendInt32", EDBUS_ARGS({"i", "int32"}), EDBUS_ARGS({"i", "int32"}),
172 _send_int32
173 },
174 { "SendInt16", EDBUS_ARGS({"n", "int16"}), EDBUS_ARGS({"n", "int16"}),
175 _send_int16
176 },
177 { "SendDouble", EDBUS_ARGS({"d", "double"}), EDBUS_ARGS({"d", "double"}),
178 _send_double
179 },
180 { "SendString", EDBUS_ARGS({"s", "string"}), EDBUS_ARGS({"s", "string"}),
181 _send_string
182 },
183 { "AsyncTest", NULL, EDBUS_ARGS({"s", "text"}),
184 _async_test
185 },
186 { }
187};
188
189static const EDBus_Service_Interface_Desc iface_desc = {
190 INTERFACE, methods, signals
191};
192
193static void
194on_name_request(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
195{
196 EDBus_Service_Interface *iface;
197 unsigned int reply;
198
199 iface = data;
200 if (edbus_message_error_get(msg, NULL, NULL))
201 {
202 printf("error on on_name_request\n");
203 return;
204 }
205
206 if (!edbus_message_arguments_get(msg, "u", &reply))
207 {
208 printf("error geting arguments on on_name_request\n");
209 return;
210 }
211
212 if (reply != EDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
213 {
214 printf("error name already in use\n");
215 return;
216 }
217
218 ecore_timer_add(5, send_signal_alive, iface);
219 ecore_timer_add(6, send_signal_hello, iface);
220}
221
222int
223main(void)
224{
225 EDBus_Service_Interface *iface;
226
227 ecore_init();
228 edbus_init();
229
230 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
231
232 iface = edbus_service_interface_register(conn, PATH, &iface_desc);
233 edbus_name_request(conn, BUS, EDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
234 on_name_request, iface);
235
236 edbus_service_interface_register(conn, PATH_TEST_SON, &iface_desc);
237
238 ecore_main_loop_begin();
239
240 edbus_connection_unref(conn);
241
242 edbus_shutdown();
243 ecore_shutdown();
244 return 0;
245}
diff --git a/src/examples/edbus/simple-signal-emit.c b/src/examples/edbus/simple-signal-emit.c
new file mode 100644
index 0000000000..960bfe632e
--- /dev/null
+++ b/src/examples/edbus/simple-signal-emit.c
@@ -0,0 +1,83 @@
1#include <EDBus.h>
2
3enum {
4 TEST_SIGNAL_ALIVE,
5 TEST_SIGNAL_PROP,
6 TEST_SIGNAL_NAME,
7};
8
9static const EDBus_Signal test_signals[] = {
10 [TEST_SIGNAL_ALIVE] = { "Alive" },
11 [TEST_SIGNAL_PROP] = { "Properties", EDBUS_ARGS({ "a{ss}", "properties"}) },
12 [TEST_SIGNAL_NAME] = { "Name", EDBUS_ARGS({ "s", "name"}) },
13 { }
14};
15
16/* signal with complex arguments (a dict) */
17static void emit_properties(EDBus_Service_Interface *iface)
18{
19 EDBus_Message *alive2;
20 EDBus_Message_Iter *iter, *dict;
21 struct keyval {
22 const char *key;
23 const char *val;
24 } keyval[] = {
25 { "key1", "val1" },
26 { "key2", "val2" },
27 { }
28 };
29 struct keyval *k;
30
31 alive2 = edbus_service_signal_new(iface, TEST_SIGNAL_PROP);
32 iter = edbus_message_iter_get(alive2);
33 dict = edbus_message_iter_container_new(iter, 'a', "{ss}");
34
35 for (k = keyval; k && k->key; k++)
36 {
37 EDBus_Message_Iter *entry = edbus_message_iter_container_new(dict, 'e',
38 NULL);
39 edbus_message_iter_arguments_append(entry, "ss", k->key, k->val);
40 edbus_message_iter_container_close(dict, entry);
41 }
42
43 edbus_message_iter_container_close(iter, dict);
44 edbus_service_signal_send(iface, alive2);
45}
46
47/* signal with basic args */
48static void emit_name(EDBus_Service_Interface *iface)
49{
50 edbus_service_signal_emit(iface, TEST_SIGNAL_NAME, "TEST");
51}
52
53/* simple signal example */
54static void emit_alive(EDBus_Service_Interface *iface)
55{
56 edbus_service_signal_emit(iface, TEST_SIGNAL_ALIVE);
57}
58
59static const EDBus_Service_Interface_Desc iface_desc = {
60 "org.enlightenment.Test", NULL, test_signals
61};
62
63int main(void)
64{
65 EDBus_Connection *conn;
66 EDBus_Service_Interface *iface;
67
68 edbus_init();
69
70 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
71 iface = edbus_service_interface_register(conn, "/org/enlightenment",
72 &iface_desc);
73
74 emit_alive(iface);
75 emit_name(iface);
76 emit_properties(iface);
77
78 edbus_connection_unref(conn);
79
80 edbus_shutdown();
81
82 return 0;
83}