summaryrefslogtreecommitdiff
path: root/src/examples/edbus/complex-types-server.c
blob: 7441690c717aab818d7eb7f5e6ad9aa222a7b740 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "EDBus.h"
#include <Ecore.h>

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

static char *resp2;
/* dummy, incremented each time DBus.Properties.Get() is called */
static int int32 = 35;
static Ecore_Timer *timer;

static EDBus_Message *
_receive_array(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message *reply = edbus_message_method_return_new(msg);
   EDBus_Message_Iter *array;
   const char *txt;

   printf("- receiveArray\n");
   if (!edbus_message_arguments_get(msg, "as", &array))
     {
        printf("Error on edbus_message_arguments_get()\n");
        return reply;
     }

   while (edbus_message_iter_get_and_next(array, 's', &txt))
     printf("%s\n", txt);
   printf("}\n\n");

   return reply;
}

static EDBus_Message *
_receive_array_of_string_int_with_size(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message *reply = edbus_message_method_return_new(msg);
   EDBus_Message_Iter *array, *struct_si;
   int size, i = 0;

   printf("- receiveArrayOfStringIntWithSize\n{\n");
   if (!edbus_message_arguments_get(msg, "ia(si)", &size, &array))
     {
        printf("Error on edbus_message_arguments_get()\n");
        return reply;
     }

   while (edbus_message_iter_get_and_next(array, 'r', &struct_si))
     {
        const char *txt;
        int num;
        if (!edbus_message_iter_arguments_get(struct_si, "si", &txt, &num))
          {
             printf("Error on edbus_message_arguments_get()\n");
             return reply;
          }
        printf("%s | %d\n", txt, num);
        i++;
     }
   printf("size in msg %d | size read %d\n", size, i);
   printf("}\n\n");

   return reply;
}

static EDBus_Message *
_receive_variant(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message *reply = edbus_message_method_return_new(msg);
   EDBus_Message_Iter *var, *array, *main_iter;

   main_iter = edbus_message_iter_get(reply);
   var = edbus_message_iter_container_new(main_iter, 'v', "as");
   edbus_message_iter_arguments_append(var, "as", &array);

   edbus_message_iter_arguments_append(array, "s", "item1");
   edbus_message_iter_arguments_append(array, "s", "item2");
   edbus_message_iter_arguments_append(array, "s", "item3");

   edbus_message_iter_container_close(var, array);
   edbus_message_iter_container_close(main_iter, var);

   return reply;
}

static EDBus_Message *
_send_variant(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message *reply = edbus_message_method_return_new(msg);
   EDBus_Message_Iter *variant;
   char *type;

   printf("- sendVariantData\n{\n");
   if (!edbus_message_arguments_get(msg, "v", &variant))
     {
        printf("Error on edbus_message_arguments_get()\n");
        return reply;
     }

   type = edbus_message_iter_signature_get(variant);
   if (type[1])
     {
        printf("It is a complex type, not handle yet.\n");
        free(type);
        return reply;
     }

   switch (type[0])
     {
      case 's':
      case 'o':
        {
           char *txt;
           edbus_message_iter_arguments_get(variant, type, &txt);
           printf("type = %c value = %s\n", type[0], txt);
           break;
        }
      case 'i':
        {
           int num;
           edbus_message_iter_arguments_get(variant, type, &num);
           printf("type = %c value = %d\n", type[0], num);
           break;
        }
      default:
        {
           printf("Unhandled type\n");
        }
     }

   printf("}\n\n");

   free(type);
   return reply;
}

static EDBus_Message *
_send_array_int(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message *reply = edbus_message_method_return_new(msg);
   EDBus_Message_Iter *iter, *array;
   int i;

   printf("- sendArrayInt\n\n");

   iter = edbus_message_iter_get(reply);
   array = edbus_message_iter_container_new(iter, 'a', "i");
   for (i = 0; i < 5; i++)
     edbus_message_iter_arguments_append(array, "i", i);
   edbus_message_iter_container_close(iter, array);

   return reply;
}

static EDBus_Message *
_send_array(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message *reply = edbus_message_method_return_new(msg);
   EDBus_Message_Iter *iter, *array;
   const char *array_string[5] = {"qqqq", "wwwww", "eeeeee", "rrrrr", "ttttt"};
   int i;

   printf("sendArray\n\n");

   iter = edbus_message_iter_get(reply);
   array = edbus_message_iter_container_new(iter, 'a', "s");
   for (i = 0; i < 5; i++)
     edbus_message_iter_arguments_append(array, "s", array_string[i]);
   edbus_message_iter_container_close(iter, array);

   return reply;
}

static EDBus_Message *
_plus_one(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message *reply = edbus_message_method_return_new(msg);
   int num;

   printf("- plusOne\n\n");
   if (!edbus_message_arguments_get(msg, "i", &num))
     {
        printf("Error on edbus_message_arguments_get()\n");
        return reply;
     }
   num++;
   edbus_message_arguments_append(reply, "i", num);

   return reply;
}

static EDBus_Message *
_double_container(const EDBus_Service_Interface *iface EINA_UNUSED, const EDBus_Message *msg)
{
   EDBus_Message_Iter *array1, *array2, *structure;
   int num1, num2;
   EDBus_Message *reply = edbus_message_method_return_new(msg);

   if (!edbus_message_arguments_get(msg, "a(ii)a(ii)", &array1, &array2))
     {
        printf("Error on edbus_message_arguments_get()\n");
        return NULL;
     }

   printf("DoubleCountainer\n{\nArray1:\n");
   while (edbus_message_iter_get_and_next(array1, 'r', &structure))
     {
        edbus_message_iter_arguments_get(structure, "ii", &num1, &num2);
        printf("1 %d - 2 %d\n", num1, num2);
     }

   printf("Array2:\n");
   while (edbus_message_iter_get_and_next(array2, 'r', &structure))
     {
         edbus_message_iter_arguments_get(structure, "ii", &num1, &num2);
         printf("1 %d - 2 %d\n", num1, num2);
     }
   printf("}\n\n");
   return reply;
}

static Eina_Bool
_properties_get(const EDBus_Service_Interface *iface EINA_UNUSED, const char *propname, EDBus_Message_Iter *iter, const EDBus_Message *request_msg EINA_UNUSED, EDBus_Message **error EINA_UNUSED)
{
   printf("Properties_get - %s\n", propname);
   if (!strcmp(propname, "Resp2"))
     edbus_message_iter_basic_append(iter, 's', resp2);
   else if (!strcmp(propname, "text"))
     edbus_message_iter_basic_append(iter, 's', "lalalala");
   else if (!strcmp(propname, "int32"))
     {
        edbus_message_iter_arguments_append(iter, "i", int32);
        int32++;
     }
   else if (!strcmp(propname, "st"))
     {
        EDBus_Message_Iter *st;
        edbus_message_iter_arguments_append(iter, "(ss)", &st);
        edbus_message_iter_arguments_append(st, "ss", "string1", "string2");
        edbus_message_iter_container_close(iter, st);
     }
   return EINA_TRUE;
}

static EDBus_Message *
_properties_set(const EDBus_Service_Interface *iface EINA_UNUSED, const char *propname, EDBus_Message_Iter *iter, const EDBus_Message *msg)
{
   char *type;

   type = edbus_message_iter_signature_get(iter);

   if (!strcmp(propname, "int32"))
     {
        int num;
        if (type[0] != 'i')
          goto invalid_signature;
        edbus_message_iter_arguments_get(iter, "i", &num);
        printf("int32 was set to: %d, previously was: %d\n", num, int32);
        int32 = num;
     }
   else if (!strcmp(propname, "Resp2"))
     {
        const char *txt;
        if (type[0] != 's')
          goto invalid_signature;
        edbus_message_iter_arguments_get(iter, "s", &txt);
        printf("Resp2 was set to: %s, previously was: %s\n", txt, resp2);
        free(resp2);
        resp2 = strdup(txt);
     }
   free(type);
   return edbus_message_method_return_new(msg);

invalid_signature:
   free(type);
   return edbus_message_error_new(msg, "org.freedesktop.DBus.Error.InvalidSignature",
                                  "Invalid type.");
}

static const EDBus_Method methods[] = {
      {
        "ReceiveArray", EDBUS_ARGS({"as", "array_of_strings"}),
        NULL, _receive_array
      },
      {
        "ReceiveArrayOfStringIntWithSize",
        EDBUS_ARGS({"i", "size_of_array"}, {"a(si)", "array"}),
        NULL, _receive_array_of_string_int_with_size, 0
      },
      {
        "SendVariantData", EDBUS_ARGS({"v", "variant_data"}),
        NULL, _send_variant
      },
      {
       "ReceiveVariantData", NULL, EDBUS_ARGS({"v", "variant_data"}),
       _receive_variant
      },
      {
        "SendArrayInt", NULL,
        EDBUS_ARGS({"ai", "array_of_int"}), _send_array_int, 0
      },
      {
        "SendArray", NULL, EDBUS_ARGS({"as", "array_string"}),
        _send_array
      },
      {
        "PlusOne", EDBUS_ARGS({"i", "integer"}),
        EDBUS_ARGS({"i", "integer_plus_one"}), _plus_one
      },
      {
        "DoubleContainner", EDBUS_ARGS({"a(ii)", "array1"}, {"a(ii)", "array2"}),
        NULL, _double_container
      },
      { }
};

static const EDBus_Property properties[] = {
      { "Resp2", "s", NULL, _properties_set },
      { "text", "s" },
      { "int32", "i", NULL, _properties_set },
      { "st", "(ss)" },
      { }
};

static const EDBus_Service_Interface_Desc iface_desc = {
   IFACE, methods, NULL, properties, _properties_get
};

static Eina_Bool _emit_changed(void *data)
{
   EDBus_Service_Interface *iface = data;
   edbus_service_property_changed(iface, "int32");
   edbus_service_property_invalidate_set(iface, "Resp2", EINA_TRUE);
   return ECORE_CALLBACK_RENEW;
}

static void
on_name_request(void *data, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   unsigned int reply;
   EDBus_Service_Interface *iface = data;

   if (edbus_message_error_get(msg, NULL, NULL))
     {
        printf("error on on_name_request\n");
        return;
     }

   if (!edbus_message_arguments_get(msg, "u", &reply))
     {
        printf("error geting arguments on on_name_request\n");
        return;
     }

   if (reply != EDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
     {
        printf("error name already in use\n");
        return;
     }

   timer = ecore_timer_add(3, _emit_changed, iface);
}

int
main(void)
{
   EDBus_Connection *conn;
   EDBus_Service_Interface *iface;

   ecore_init();
   edbus_init();

   conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);

   resp2 = malloc(sizeof(char) * 5);
   strcpy(resp2, "test");
   iface = edbus_service_interface_register(conn, PATH, &iface_desc);
   edbus_name_request(conn, BUS, EDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
                      on_name_request, iface);

   ecore_main_loop_begin();

   free(resp2);
   ecore_timer_del(timer);
   edbus_connection_unref(conn);

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