Imported Upstream version 1.7.2
[platform/upstream/edbus.git] / src / bin / e_dbus_ofono_test.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <errno.h>
10
11 #include "E_Ofono.h"
12
13 static void
14 _method_success_check(void *data, DBusMessage *msg __UNUSED__, DBusError *error)
15 {
16    const char *name = data;
17
18    if ((!error) || (!dbus_error_is_set(error)))
19      {
20         printf("SUCCESS: method %s() finished successfully.\n", name);
21         return;
22      }
23
24    printf("FAILURE: method %s() finished with error: %s %s\n",
25           name, error->name, error->message);
26    dbus_error_free(error);
27 }
28
29 static void
30 _elements_print(E_Ofono_Element **elements, unsigned int count)
31 {
32    unsigned int i;
33    for (i = 0; i < count; i++)
34      {
35         printf("--- element %d:\n", i);
36         e_ofono_element_print(stdout, elements[i]);
37      }
38    free(elements);
39    printf("END: all elements count = %u\n", count);
40 }
41
42 static Eina_Bool
43 _on_element_add(void *data __UNUSED__, int type __UNUSED__, void *info)
44 {
45    E_Ofono_Element *element = info;
46    printf(">>> %s %s\n", element->path, element->interface);
47    return EINA_TRUE;
48 }
49
50 static Eina_Bool
51 _on_element_del(void *data __UNUSED__, int type __UNUSED__, void *info)
52 {
53    E_Ofono_Element *element = info;
54    printf("<<< %s %s\n", element->path, element->interface);
55    return EINA_TRUE;
56 }
57
58 static Eina_Bool
59 _on_element_updated(void *data __UNUSED__, int type __UNUSED__, void *info)
60 {
61    E_Ofono_Element *element = info;
62    printf("!!! %s %s\n", element->path, element->interface);
63    e_ofono_element_print(stderr, element);
64    return EINA_TRUE;
65 }
66 static Eina_Bool
67 _on_cmd_quit(char *cmd __UNUSED__, char *args __UNUSED__)
68 {
69    fputs("Bye!\n", stderr);
70    ecore_main_loop_quit();
71    return EINA_FALSE;
72 }
73
74 static Eina_Bool
75 _on_cmd_sync(char *cmd __UNUSED__, char *args __UNUSED__)
76 {
77    e_ofono_manager_sync_elements();
78    return EINA_TRUE;
79 }
80
81 static char *
82 _tok(char *p)
83 {
84    p = strchr(p, ' ');
85    if (!p)
86      return NULL;
87
88    *p = '\0';
89    p++;
90    while (isspace(*p))
91      p++;
92    if (*p == '\0')
93      return NULL;
94
95    return p;
96 }
97
98 static Eina_Bool
99 _on_cmd_get_all(char *cmd __UNUSED__, char *args)
100 {
101    E_Ofono_Element **elements;
102    char *type;
103    unsigned int count;
104    Eina_Bool ret;
105
106    if (!args)
107      type = NULL;
108    else
109      type = args;
110
111    if (type)
112      ret = e_ofono_elements_get_all_type(type, &count, &elements);
113    else
114      ret = e_ofono_elements_get_all(&count, &elements);
115
116    if (!ret)
117      fputs("ERROR: could not get elements\n", stderr);
118    else
119      {
120         printf("BEG: all elements type=%s count = %d\n", type, count);
121         _elements_print(elements, count);
122      }
123
124    return EINA_TRUE;
125 }
126
127 static E_Ofono_Element *
128 _element_from_args(char *interface, char *args, char **next_args)
129 {
130    E_Ofono_Element *element;
131
132    if (!args)
133      {
134         fputs("ERROR: missing element path\n", stderr);
135         *next_args = NULL;
136         return NULL;
137      }
138
139    if (!interface)
140      {
141         interface = _tok(args);
142         *next_args = _tok(interface);
143      }
144    else
145      *next_args = _tok(args);
146
147    element = e_ofono_element_get(args, interface);
148    if (!element)
149      fprintf(stderr, "ERROR: no element called \"%s %s\".\n", args, interface);
150
151    return element;
152 }
153
154 static Eina_Bool
155 _on_cmd_print(char *cmd __UNUSED__, char *args)
156 {
157    char *next_args;
158    E_Ofono_Element *element = _element_from_args(NULL, args, &next_args);
159    if (element)
160      e_ofono_element_print(stdout, element);
161    return EINA_TRUE;
162 }
163
164 static Eina_Bool
165 _on_cmd_get_properties(char *cmd __UNUSED__, char *args)
166 {
167    char *next_args;
168    E_Ofono_Element *element = _element_from_args(NULL, args, &next_args);
169    if (element)
170      e_ofono_element_properties_sync(element);
171    return EINA_TRUE;
172 }
173
174 static Eina_Bool
175 _on_cmd_property_set(char *cmd __UNUSED__, char *args)
176 {
177    char *next_args, *name, *p;
178    E_Ofono_Element *element = _element_from_args(NULL, args, &next_args);
179    void *value;
180    long vlong;
181    unsigned short vu16;
182    unsigned int vu32;
183    int type;
184
185    if (!element)
186      return EINA_TRUE;
187
188    if (!next_args)
189      {
190         fputs("ERROR: missing parameters name, type and value.\n", stderr);
191         return EINA_TRUE;
192      }
193
194    name = next_args;
195    p = _tok(name);
196    if (!p)
197      {
198         fputs("ERROR: missing parameters type and value.\n", stderr);
199         return EINA_TRUE;
200      }
201
202    next_args = _tok(p);
203    if (!next_args)
204      {
205         fputs("ERROR: missing parameter value.\n", stderr);
206         return EINA_TRUE;
207      }
208
209    type = p[0];
210    switch (type)
211      {
212       case DBUS_TYPE_BOOLEAN:
213          vlong = !!atol(next_args);
214          value = &vlong;
215          fprintf(stderr, "DBG: boolean is: %ld\n", vlong);
216          break;
217       case DBUS_TYPE_UINT16:
218          vu16 = strtol(next_args, &p, 0);
219          if (p == next_args)
220            {
221               fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
222               return EINA_TRUE;
223            }
224          value = &vu16;
225          fprintf(stderr, "DBG: u16 is: %hu\n", vu16);
226          break;
227       case DBUS_TYPE_UINT32:
228          vu32 = strtol(next_args, &p, 0);
229          if (p == next_args)
230            {
231               fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
232               return EINA_TRUE;
233            }
234          value = &vu32;
235          fprintf(stderr, "DBG: u16 is: %u\n", vu32);
236          break;
237       case DBUS_TYPE_STRING:
238       case DBUS_TYPE_OBJECT_PATH:
239          p = next_args + strlen(next_args);
240          if (p > next_args)
241            p--;
242          while (p > next_args && isspace(*p))
243            p--;
244          if (p <= next_args)
245            {
246               fprintf(stderr, "ERROR: invalid string \"%s\".\n", next_args);
247            }
248          p[1] = '\0';
249          value = next_args;
250          fprintf(stderr, "DBG: string is: \"%s\"\n", next_args);
251          break;
252       default:
253          fprintf(stderr, "ERROR: don't know how to parse type '%c' (%d)\n",
254                  type, type);
255          return EINA_TRUE;
256      }
257
258    fprintf(stderr, "set_property %s [%p] %s %c %p...\n",
259            args, element, name, type, value);
260    if (!e_ofono_element_property_set(element, name, type, value))
261         fputs("ERROR: error setting property.\n", stderr);
262
263    return EINA_TRUE;
264 }
265
266 /* Manager Commands */
267
268 static Eina_Bool
269 _on_cmd_manager_get(char *cmd __UNUSED__, char *args __UNUSED__)
270 {
271    E_Ofono_Element *element;
272    element = e_ofono_manager_get();
273    e_ofono_element_print(stderr, element);
274    return EINA_TRUE;
275 }
276
277 static Eina_Bool
278 _on_cmd_manager_modems_get(char *cmd __UNUSED__, char *args __UNUSED__)
279 {
280    char *path;
281    Eina_Array_Iterator iterator;
282    unsigned int i;
283    Eina_Array *modems = NULL;
284
285    if(e_ofono_manager_modems_get(&modems))
286      {
287         printf("[");
288         if (modems)
289           EINA_ARRAY_ITER_NEXT(modems, i, path, iterator)
290              printf(" %s", path);
291         printf(" ]\n");
292      }
293
294    return EINA_TRUE;
295 }
296
297 /* Modem Commands */
298
299 static Eina_Bool
300 _on_cmd_modem_set_powered(char *cmd __UNUSED__, char *args)
301 {
302    char *next_args;
303    Eina_Bool powered;
304    E_Ofono_Element *element = _element_from_args("org.ofono.Modem", args, &next_args);
305
306    if (!element)
307            return EINA_TRUE;
308
309    if (!args)
310      {
311         fputs("ERROR: missing the powered value\n", stderr);
312         return EINA_TRUE;
313      }
314
315    powered = !!atol(next_args);
316
317    if (e_ofono_modem_powered_set
318        (element, powered, _method_success_check, "modem_set_powered"))
319      printf(":::Modem %s Powered set to %hhu\n", element->path, powered);
320    else
321      fputs("ERROR: can't set Modem Powered\n", stderr);
322    return EINA_TRUE;
323 }
324
325 /* SMS Commands */
326
327 static Eina_Bool
328 _on_cmd_sms_sca_set(char *cmd __UNUSED__, char *args)
329 {
330    char *next_args, *sca;
331    E_Ofono_Element *element = _element_from_args("org.ofono.SmsManager", args,
332                                                  &next_args);
333
334    if (!element)
335      return EINA_TRUE;
336
337    if (!args)
338      {
339         fputs("ERROR: missing service center address\n", stderr);
340         return EINA_TRUE;
341      }
342
343    sca = next_args;
344    if (e_ofono_sms_sca_set(element, sca, _method_success_check,
345                            "sms_sca_set"))
346      printf(":::Service Center Address on modem %s set to %s\n",
347             element->path, sca);
348    else
349      fputs("ERROR: couldn't change Service Center Address\n", stderr);
350
351    return EINA_TRUE;
352 }
353
354 static Eina_Bool
355 _on_cmd_sms_send_message(char *cmd __UNUSED__, char *args)
356 {
357    char *next_args, *number, *message;
358    E_Ofono_Element *element = _element_from_args("org.ofono.SmsManager", args,
359                                                  &next_args);
360
361    if (!element)
362      return EINA_TRUE;
363
364    number = next_args;
365    if (!number)
366      {
367         fputs("ERROR: missing recipient number and message text.\n", stderr);
368         return EINA_TRUE;
369      }
370
371    message = _tok(number);
372    if (!message)
373      {
374         fputs("ERROR: missing message text.\n", stderr);
375         return EINA_TRUE;
376      }
377
378    if (!e_ofono_sms_send_message(element, number, message,
379                                  _method_success_check, "sms_send_message"))
380         fputs("ERROR: error setting property.\n", stderr);
381
382    return EINA_TRUE;
383 }
384
385 static Eina_Bool
386 _on_input(void *data __UNUSED__, Ecore_Fd_Handler *fd_handler)
387 {
388    char buf[256];
389    char *cmd, *args;
390    const struct {
391       const char *cmd;
392       Eina_Bool (*cb)(char *cmd, char *args);
393    } *itr, maps[] = {
394      {"quit", _on_cmd_quit},
395      {"sync", _on_cmd_sync},
396      {"get_all", _on_cmd_get_all},
397      {"print", _on_cmd_print},
398      {"get_properties", _on_cmd_get_properties},
399      {"set_property", _on_cmd_property_set},
400      {"manager_get", _on_cmd_manager_get},
401      {"manager_modems_get", _on_cmd_manager_modems_get},
402      {"modem_set_powered", _on_cmd_modem_set_powered},
403      {"sms_sca_set", _on_cmd_sms_sca_set},
404      {"sms_send_message", _on_cmd_sms_send_message},
405      {NULL, NULL}
406    };
407
408
409    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
410      {
411         fputs("ERROR: reading from stdin, exit\n", stderr);
412         return EINA_FALSE;
413      }
414
415    if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
416      {
417         fputs("ERROR: nothing to read?\n", stderr);
418         return EINA_FALSE;
419      }
420
421    if (!fgets(buf, sizeof(buf), stdin))
422      {
423         fprintf(stderr, "ERROR: could not read command: %s\n", strerror(errno));
424         ecore_main_loop_quit();
425         return EINA_FALSE;
426      }
427
428    cmd = buf;
429    while (isspace(*cmd))
430      cmd++;
431
432    args = strchr(cmd, ' ');
433    if (args)
434      {
435         char *p;
436
437         *args = '\0';
438         args++;
439
440         while (isspace(*args))
441           args++;
442
443         p = args + strlen(args) - 1;
444         if (*p == '\n')
445           *p = '\0';
446      }
447    else
448      {
449         char *p;
450
451         p = cmd + strlen(cmd) - 1;
452         if (*p == '\n')
453           *p = '\0';
454      }
455
456    if (strcmp(cmd, "help") == 0)
457      {
458         if (args)
459           {
460              printf("Commands with '%s' in the name:\n", args);
461              for (itr = maps; itr->cmd; itr++)
462                if (strstr(itr->cmd, args))
463                  printf("\t%s\n", itr->cmd);
464           }
465         else
466           {
467              fputs("Commands:\n", stdout);
468              for (itr = maps; itr->cmd; itr++)
469                printf("\t%s\n", itr->cmd);
470           }
471         fputc('\n', stdout);
472         return EINA_TRUE;
473      }
474
475    for (itr = maps; itr->cmd; itr++)
476      if (strcmp(itr->cmd, cmd) == 0)
477        return itr->cb(cmd, args);
478
479    printf("unknown command \"%s\", args=%s\n", cmd, args);
480    return EINA_TRUE;
481 }
482
483 int
484 main(int argc __UNUSED__, char *argv[] __UNUSED__)
485 {
486    E_DBus_Connection *c;
487
488    ecore_init();
489    e_dbus_init();
490    eina_init();
491
492    c = e_dbus_bus_get(DBUS_BUS_SYSTEM);
493    if (!c) {
494       printf("ERROR: can't connect to system session\n");
495       return -1;
496    }
497
498    e_ofono_system_init(c);
499
500    ecore_event_handler_add(E_OFONO_EVENT_ELEMENT_ADD, _on_element_add, NULL);
501    ecore_event_handler_add(E_OFONO_EVENT_ELEMENT_DEL, _on_element_del, NULL);
502    ecore_event_handler_add(E_OFONO_EVENT_ELEMENT_UPDATED,
503                            _on_element_updated, NULL);
504
505    ecore_main_fd_handler_add
506      (0, ECORE_FD_READ | ECORE_FD_ERROR, _on_input, NULL, NULL, NULL);
507
508    ecore_main_loop_begin();
509
510    e_ofono_system_shutdown();
511
512    e_dbus_connection_close(c);
513    eina_shutdown();
514    e_dbus_shutdown();
515    ecore_shutdown();
516
517    fputs("DBG: clean exit.\n", stderr);
518
519    return 0;
520 }