summaryrefslogtreecommitdiff
path: root/src/examples/edbus/complex-types.c
blob: e60badfafff1cb1b3e8a127a41307c0f1acabf67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "EDBus.h"
#include <Ecore.h>

#define BUS "com.profusion"
#define PATH "/com/profusion/Test"
#define IFACE "com.profusion.Test"

EDBus_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 EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   int num2 = 0;

   if (edbus_message_error_get(msg, NULL, NULL))
     {
        printf("Message error\n\n");
        return;
     }
   if (!edbus_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 EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   const char *errname;
   const char *errmsg;

   printf("set_property_resp2()\n");
   if (edbus_message_error_get(msg, &errname, &errmsg))
     {
        printf("Message error %s - %s\n\n", errname, errmsg);
        return;
     }
}

static void
get_property_resp2(void *data, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   EDBus_Proxy *proxy = data;
   EDBus_Message_Iter *variant = NULL;
   char *type;
   char *resp2;
   const char *errname;
   const char *errmsg;

   printf("get_property_resp2()\n");
   if (edbus_message_error_get(msg, &errname, &errmsg))
     {
        printf("Message error %s - %s\n\n", errname, errmsg);
        return;
     }
   if (!edbus_message_arguments_get(msg, "v", &variant))
     {
        printf("Error getting arguments.");
        return;
     }

   type = edbus_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 (!edbus_message_iter_arguments_get(variant, "s", &resp2))
     {
        printf("error in edbus_message_iter_arguments_get()\n\n");
        return;
     }
   printf("resp2=%s\n", resp2);
   free(type);

   edbus_proxy_property_set(proxy, "Resp2", "s", &"lalala", set_property_resp2, NULL);
   edbus_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 EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   EDBus_Message_Iter *array = NULL;
   int num;

   printf("on_send_array_int()\n");
   if (edbus_message_error_get(msg, NULL, NULL))
     {
        printf("Message error\n\n");
        return;
     }
   if (!edbus_message_arguments_get(msg, "ai", &array))
     {
        printf("Error getting arguments.");
        return;
     }

   while (edbus_message_iter_get_and_next(array, 'i', &num))
     {
        printf("%d\n", num);
     }
}

static void
on_send_array(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   EDBus_Message_Iter *array = NULL;
   char *txt = NULL;
   char *string[10];
   int i = 0;
   int z;

   printf("on_send_array()\n");
   if (edbus_message_error_get(msg, NULL, NULL))
     {
        printf("Message error\n\n");
        return;
     }
   if (!edbus_message_arguments_get(msg, "as", &array))
     {
        printf("Error getting arguments.");
        return;
     }

   while (edbus_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 EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   const char *errname;
   const char *errmsg;

   printf("on_receive_array_with_size()\n");
   if (edbus_message_error_get(msg, &errname, &errmsg))
     {
        fprintf(stderr, "Error: %s %s\n", errname, errmsg);
     }
}

static void
on_send_variant(void *data EINA_UNUSED, const EDBus_Message *msg EINA_UNUSED, EDBus_Pending *pending EINA_UNUSED)
{
   printf("on_send_variant()\n\n");
}

static void
on_receive_array(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   const char *errname;
   const char *errmsg;

   printf("on_receive_array()\n");
   if (edbus_message_error_get(msg, &errname, &errmsg))
     {
        fprintf(stderr, "Error: %s %s\n", errname, errmsg);
     }
}

int
main(void)
{
   EDBus_Object *test2_obj;
   EDBus_Proxy *test2_proxy;
   EDBus_Pending *pending;
   EDBus_Message_Iter *iter, *array_of_string, *variant;
   EDBus_Message_Iter *array_itr, *structure;
   EDBus_Message *msg;
   int size_of_array = 5;
   const char *array[5] = { "aaaa", "bbbb", "cccc", "dddd", "eeee" };
   int i;
   int plus_one = 24;

   ecore_init();
   edbus_init();

   conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);

   test2_obj = edbus_object_get(conn, BUS, PATH);
   test2_proxy = edbus_proxy_get(test2_obj, IFACE);

   msg = edbus_proxy_method_call_new(test2_proxy, "ReceiveArray");
   iter = edbus_message_iter_get(msg);
   array_of_string = edbus_message_iter_container_new(iter, 'a',"s");
   if (!array_of_string) printf("array_of_string == NULL\n\n");
   for (i = 0; i < 5; i++)
     edbus_message_iter_basic_append(array_of_string, 's', array[i]);
   edbus_message_iter_container_close(iter, array_of_string);
   pending = edbus_proxy_send(test2_proxy, msg, on_receive_array, NULL, -1);
   if (!pending) printf("Error in edbus_proxy_send()\n\n");

   msg = edbus_proxy_method_call_new(test2_proxy, "ReceiveArrayOfStringIntWithSize");
   iter = edbus_message_iter_get(msg);
   if (!edbus_message_iter_arguments_append(iter, "ia(si)", size_of_array, &array_of_string))
     printf("error on edbus_massage_iterator_arguments_set()\n\n");
   for (i = 0; i < size_of_array; i++)
     {
         EDBus_Message_Iter *struct_of_si;
         edbus_message_iter_arguments_append(array_of_string, "(si)", &struct_of_si);
         edbus_message_iter_arguments_append(struct_of_si, "si", array[i], i);
         edbus_message_iter_container_close(array_of_string, struct_of_si);
     }
   edbus_message_iter_container_close(iter, array_of_string);
   pending = edbus_proxy_send(test2_proxy, msg, on_receive_array_with_size, NULL, -1);

   msg = edbus_proxy_method_call_new(test2_proxy, "SendVariantData");
   iter = edbus_message_iter_get(msg);
   variant = edbus_message_iter_container_new(iter, 'v', "s");
   edbus_message_iter_basic_append(variant, 's', "test");
   edbus_message_iter_container_close(iter, variant);
   pending = edbus_proxy_send(test2_proxy, msg, on_send_variant, NULL, -1);

   msg = edbus_proxy_method_call_new(test2_proxy, "DoubleContainner");
   iter = edbus_message_iter_get(msg);
   /**
    * edbus_message_iterator_arguments_set(itr, "a(ii)a(ii)", &array_itr, &array_itr2);
    * this will cause a error, we could not open another container until
    * we close the first one
    */
   edbus_message_iter_arguments_append(iter, "a(ii)", &array_itr);
   for (i = 0; i < 5; i++)
     {
        edbus_message_iter_arguments_append(array_itr, "(ii)", &structure);
        edbus_message_iter_arguments_append(structure, "ii", i, i*i);
        edbus_message_iter_container_close(array_itr, structure);
     }
   edbus_message_iter_container_close(iter, array_itr);
   edbus_message_iter_arguments_append(iter, "a(ii)", &array_itr);
   for (i = 0; i < 7; i++)
     {
        edbus_message_iter_arguments_append(array_itr, "(ii)", &structure);
        edbus_message_iter_arguments_append(structure, "ii", i, i*i*i);
        edbus_message_iter_container_close(array_itr, structure);
     }
   edbus_message_iter_container_close(iter, array_itr);
   edbus_proxy_send(test2_proxy, msg, NULL, NULL, -1);

   pending = edbus_proxy_call(test2_proxy, "SendArrayInt", on_send_array_int, NULL,
                                 -1 , "");

   pending = edbus_proxy_call(test2_proxy, "SendArray", on_send_array, NULL,
                              -1 , "");

   pending = edbus_proxy_call(test2_proxy, "PlusOne", on_plus_one, NULL,
                              -1 , "i", plus_one);

   pending = edbus_proxy_property_get(test2_proxy, "Resp2", get_property_resp2, test2_proxy);

   ecore_timer_add(10, _timer1_cb, NULL);

   ecore_main_loop_begin();

   edbus_connection_unref(conn);

   edbus_shutdown();
   ecore_shutdown();
   return 0;
}