Migration with svn revision 66792
[framework/uifw/edbus.git] / src / bin / e_dbus_bluez_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_Bluez.h"
12
13 typedef struct _E_Msgbus_Data E_Msgbus_Data;
14
15 struct _E_Msgbus_Data {
16         E_DBus_Connection *conn;
17         E_DBus_Object     *obj;
18 };
19
20 static E_Msgbus_Data *_msgbus_data = NULL;
21
22 static E_DBus_Interface *iface = NULL;
23
24 static void
25 _method_success_check(void *data, __UNUSED__ DBusMessage *msg, DBusError *error)
26 {
27    const char *name = data;
28
29    if ((!error) || (!dbus_error_is_set(error)))
30      {
31         printf("SUCCESS: method %s() finished successfully.\n", name);
32         return;
33      }
34
35    printf("FAILURE: method %s() finished with error: %s %s\n",
36           name, error->name, error->message);
37    dbus_error_free(error);
38 }
39
40 static void
41 _default_adapter_callback(__UNUSED__ void *data, DBusMessage *msg, __UNUSED__ DBusError *err)
42 {
43    E_Bluez_Element *element;
44    const char *path;
45
46    if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
47                            DBUS_TYPE_INVALID) == EINA_FALSE)
48            printf("FAILURE: failed to get default adapter\n");
49
50    printf("SUCCESS: default adapter: %s\n", path);
51
52    element = e_bluez_element_get(path);
53    e_bluez_element_print(stdout, element);
54    return;
55
56 }
57
58 static void
59 _create_paired_device_cb(void *data, DBusMessage *msg, DBusError *err)
60 {
61         e_dbus_object_interface_detach(_msgbus_data->obj, iface);
62         e_dbus_object_free(_msgbus_data->obj);
63         e_dbus_interface_unref(iface);
64         _method_success_check(data, msg, err);
65 }
66
67 static DBusMessage*
68 _request_pincode_cb(__UNUSED__ E_DBus_Object *obj, DBusMessage *msg)
69 {
70         DBusMessage *reply;
71         char pin[16];
72         char *p = pin;
73         int ret;
74
75         printf("Enter PIN Code:\n");
76         ret = scanf("%15s", p);
77         if (ret != 1)
78                 return NULL;
79
80         reply = dbus_message_new_method_return(msg);
81         dbus_message_append_args(reply, DBUS_TYPE_STRING, &p,
82                                  DBUS_TYPE_INVALID);
83
84         return reply;
85 }
86
87 static void
88 _elements_print(E_Bluez_Element **elements, unsigned int count)
89 {
90    unsigned int i;
91    for (i = 0; i < count; i++)
92      {
93         printf("--- element %d:\n", i);
94         e_bluez_element_print(stdout, elements[i]);
95      }
96    free(elements);
97    printf("END: all elements count = %u\n", count);
98 }
99
100 static Eina_Bool
101 _on_element_add(__UNUSED__ void *data, __UNUSED__ int type, void *info)
102 {
103    E_Bluez_Element *element = info;
104    printf(">>> %s\n", element->path);
105    return ECORE_CALLBACK_RENEW;
106 }
107
108 static Eina_Bool
109 _on_element_del(__UNUSED__ void *data, __UNUSED__ int type, void *info)
110 {
111    E_Bluez_Element *element = info;
112    printf("<<< %s\n", element->path);
113    return ECORE_CALLBACK_RENEW;
114 }
115
116 static Eina_Bool
117 _on_element_updated(__UNUSED__ void *data, __UNUSED__ int type, void *info)
118 {
119    E_Bluez_Element *element = info;
120    printf("!!! %s\n", element->path);
121    e_bluez_element_print(stderr, element);
122    return ECORE_CALLBACK_RENEW;
123 }
124
125 static Eina_Bool
126 _on_device_found(__UNUSED__ void *data, int __UNUSED__ type, void *info)
127 {
128    E_Bluez_Device_Found *device = info;
129    printf("!!! %s\n", device->adapter->path);
130    printf(":::DeviceFound %s\n", device->name);
131    e_bluez_element_array_print(stderr, device->array);
132    printf("\n");
133
134    e_bluez_devicefound_free(device);
135    return ECORE_CALLBACK_RENEW;
136 }
137
138 static Eina_Bool
139 _on_cmd_quit(__UNUSED__ char *cmd, __UNUSED__ char *args)
140 {
141    fputs("Bye!\n", stderr);
142    ecore_main_loop_quit();
143    return EINA_FALSE;
144 }
145
146 static Eina_Bool
147 _on_cmd_sync(__UNUSED__ char *cmd, __UNUSED__ char *args)
148 {
149    e_bluez_manager_sync_elements();
150    return ECORE_CALLBACK_RENEW;
151 }
152
153 static char *
154 _tok(char *p)
155 {
156    p = strchr(p, ' ');
157    if (!p)
158      return NULL;
159
160    *p = '\0';
161    p++;
162    while (isspace(*p))
163      p++;
164    if (*p == '\0')
165      return NULL;
166
167    return p;
168 }
169
170 static Eina_Bool
171 _on_cmd_get_all(__UNUSED__ char *cmd, char *args)
172 {
173    E_Bluez_Element **elements;
174    char *type;
175    unsigned int count;
176    Eina_Bool ret;
177
178    if (!args)
179      type = NULL;
180    else
181      type = args;
182
183    if (type)
184      ret = e_bluez_elements_get_all_type(type, &count, &elements);
185    else
186      ret = e_bluez_elements_get_all(&count, &elements);
187
188    if (!ret)
189      fputs("ERROR: could not get elements\n", stderr);
190    else
191      {
192         printf("BEG: all elements type=%s count = %d\n", type, count);
193         _elements_print(elements, count);
194      }
195
196    return ECORE_CALLBACK_RENEW;
197 }
198
199 static E_Bluez_Element *
200 _element_from_args(char *args, char **next_args)
201 {
202    E_Bluez_Element *element;
203
204    if (!args)
205      {
206         fputs("ERROR: missing element path\n", stderr);
207         *next_args = NULL;
208         return NULL;
209      }
210
211    *next_args = _tok(args);
212    element = e_bluez_element_get(args);
213    if (!element)
214      fprintf(stderr, "ERROR: no element called \"%s\".\n", args);
215
216    return element;
217 }
218
219 static Eina_Bool
220 _on_cmd_print(__UNUSED__ char *cmd, char *args)
221 {
222    char *next_args;
223    E_Bluez_Element *element = _element_from_args(args, &next_args);
224    if (element)
225      e_bluez_element_print(stdout, element);
226    return ECORE_CALLBACK_RENEW;
227 }
228
229 static Eina_Bool
230 _on_cmd_get_properties(__UNUSED__ char *cmd, char *args)
231 {
232    char *next_args;
233    E_Bluez_Element *element = _element_from_args(args, &next_args);
234    if (element)
235      e_bluez_element_properties_sync(element);
236    return ECORE_CALLBACK_RENEW;
237 }
238
239 static Eina_Bool
240 _on_cmd_property_set(__UNUSED__ char *cmd, char *args)
241 {
242    char *next_args, *name, *p;
243    E_Bluez_Element *element = _element_from_args(args, &next_args);
244    void *value;
245    long vlong;
246    unsigned short vu16;
247    unsigned int vu32;
248    int type;
249
250    if (!element)
251      return ECORE_CALLBACK_RENEW;
252
253    if (!next_args)
254      {
255         fputs("ERROR: missing parameters name, type and value.\n", stderr);
256         return ECORE_CALLBACK_RENEW;
257      }
258
259    name = next_args;
260    p = _tok(name);
261    if (!p)
262      {
263         fputs("ERROR: missing parameters type and value.\n", stderr);
264         return ECORE_CALLBACK_RENEW;
265      }
266
267    next_args = _tok(p);
268    if (!next_args)
269      {
270         fputs("ERROR: missing parameter value.\n", stderr);
271         return ECORE_CALLBACK_RENEW;
272      }
273
274    type = p[0];
275    switch (type)
276      {
277       case DBUS_TYPE_BOOLEAN:
278          vlong = !!atol(next_args);
279          value = &vlong;
280          fprintf(stderr, "DBG: boolean is: %ld\n", vlong);
281          break;
282       case DBUS_TYPE_UINT16:
283          vu16 = strtol(next_args, &p, 0);
284          if (p == next_args)
285            {
286               fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
287               return ECORE_CALLBACK_RENEW;
288            }
289          value = &vu16;
290          fprintf(stderr, "DBG: u16 is: %hu\n", vu16);
291          break;
292       case DBUS_TYPE_UINT32:
293          vu32 = strtol(next_args, &p, 0);
294          if (p == next_args)
295            {
296               fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
297               return ECORE_CALLBACK_RENEW;
298            }
299          value = &vu32;
300          fprintf(stderr, "DBG: u16 is: %u\n", vu32);
301          break;
302       case DBUS_TYPE_STRING:
303       case DBUS_TYPE_OBJECT_PATH:
304          p = next_args + strlen(next_args);
305          if (p > next_args)
306            p--;
307          while (p > next_args && isspace(*p))
308            p--;
309          if (p <= next_args)
310            {
311               fprintf(stderr, "ERROR: invalid string \"%s\".\n", next_args);
312            }
313          p[1] = '\0';
314          value = next_args;
315          fprintf(stderr, "DBG: string is: \"%s\"\n", next_args);
316          break;
317       default:
318          fprintf(stderr, "ERROR: don't know how to parse type '%c' (%d)\n",
319                  type, type);
320          return ECORE_CALLBACK_RENEW;
321      }
322
323    fprintf(stderr, "set_property %s [%p] %s %c %p...\n",
324            args, element, name, type, value);
325    if (!e_bluez_element_property_set(element, name, type, value))
326         fputs("ERROR: error setting property.\n", stderr);
327
328    return ECORE_CALLBACK_RENEW;
329 }
330
331 /* Manager Commands */
332
333 static Eina_Bool
334 _on_cmd_manager_get(__UNUSED__ char *cmd, __UNUSED__ char *args)
335 {
336    E_Bluez_Element *element;
337    element = e_bluez_manager_get();
338    e_bluez_element_print(stderr, element);
339    return ECORE_CALLBACK_RENEW;
340 }
341
342 static Eina_Bool
343 _on_cmd_manager_default_adapter(__UNUSED__ char *cmd, __UNUSED__ char *args)
344 {
345    return e_bluez_manager_default_adapter(_default_adapter_callback, NULL);
346 }
347
348 /* Adapter Commands */
349
350 static Eina_Bool
351 _on_cmd_adapter_register_agent(__UNUSED__ char *cmd, char *args)
352 {
353    char *next_args, *path, *cap;
354    E_Bluez_Element *element = _element_from_args(args, &next_args);
355
356    if (!element)
357            return ECORE_CALLBACK_RENEW;
358
359    if (!next_args) {
360            fputs("ERROR: missing parameters name, type and value.\n", stderr);
361            return ECORE_CALLBACK_RENEW;
362    }
363
364    path = next_args;
365    cap = _tok(path);
366    if (!cap) {
367            fputs("ERROR: missing parameters name, type and value.\n", stderr);
368            return ECORE_CALLBACK_RENEW;
369    }
370
371    if (e_bluez_adapter_agent_register(element,
372        path, cap, _method_success_check, "adapter_register_agent"))
373      printf(":::Registering agent %s (%s)...\n", path, cap);
374    else
375      fprintf(stderr, "ERROR: can't register agent %s\n", path);
376
377    return ECORE_CALLBACK_RENEW;
378 }
379
380 static Eina_Bool
381 _on_cmd_adapter_unregister_agent(__UNUSED__ char *cmd, char *args)
382 {
383    char *path, *next_args;
384    E_Bluez_Element *element = _element_from_args(args, &next_args);
385
386    if (!element)
387            return ECORE_CALLBACK_RENEW;
388
389    if (!args)
390      {
391         fputs("ERROR: missing the object path\n", stderr);
392         return ECORE_CALLBACK_RENEW;
393      }
394
395    path = next_args;
396    if (e_bluez_adapter_agent_unregister(element,
397        path, _method_success_check, "adapter_unregister_agent"))
398      printf(":::Unregistering agent %s...\n", path);
399    else
400      fprintf(stderr, "ERROR: can't unregister agent %s\n", path);
401
402    return ECORE_CALLBACK_RENEW;
403 }
404
405 static Eina_Bool
406 _on_cmd_adapter_get_address(__UNUSED__ char *cmd, char *args)
407 {
408    const char *address;
409    char *next_args;
410    E_Bluez_Element *element = _element_from_args(args, &next_args);
411
412    if (!element)
413            return ECORE_CALLBACK_RENEW;
414
415    if (e_bluez_adapter_address_get(element, &address))
416      printf(":::Adapter address = \"%s\"\n", address);
417    else
418      fputs("ERROR: can't get adapter address\n", stderr);
419    return ECORE_CALLBACK_RENEW;
420 }
421
422 static Eina_Bool
423 _on_cmd_adapter_get_name(__UNUSED__ char *cmd, char *args)
424 {
425    const char *name;
426    char *next_args;
427    E_Bluez_Element *element = _element_from_args(args, &next_args);
428
429    if (!element)
430            return ECORE_CALLBACK_RENEW;
431
432    if (e_bluez_adapter_name_get(element, &name))
433      printf(":::Adapter name = \"%s\"\n", name);
434    else
435      fputs("ERROR: can't get adapter name\n", stderr);
436    return ECORE_CALLBACK_RENEW;
437 }
438
439 static Eina_Bool
440 _on_cmd_adapter_set_name(__UNUSED__ char *cmd, char *args)
441 {
442    char *next_args;
443    E_Bluez_Element *element = _element_from_args(args, &next_args);
444
445    if (!element)
446            return ECORE_CALLBACK_RENEW;
447
448    if (!next_args) {
449       fprintf(stderr, "ERROR: missing name value\n");
450       return ECORE_CALLBACK_RENEW;
451    }
452
453    if (e_bluez_adapter_name_set(element, next_args, _method_success_check,
454                            "adapter_set_name"))
455      printf(":::Adapter %s Name set to %s\n", element->path, next_args);
456    else
457      fputs("ERROR: can't set adapter name\n", stderr);
458    return ECORE_CALLBACK_RENEW;
459 }
460
461 static Eina_Bool
462 _on_cmd_adapter_get_powered(__UNUSED__ char *cmd, char *args)
463 {
464    char *next_args;
465    Eina_Bool powered;
466    E_Bluez_Element *element = _element_from_args(args, &next_args);
467
468    if (!element)
469            return ECORE_CALLBACK_RENEW;
470
471    if (e_bluez_adapter_powered_get(element, &powered))
472      printf(":::Adapter powered = \"%hhu\"\n", powered);
473    else
474      fputs("ERROR: can't get adapter powered\n", stderr);
475    return ECORE_CALLBACK_RENEW;
476 }
477
478 static Eina_Bool
479 _on_cmd_adapter_set_powered(__UNUSED__ char *cmd, char *args)
480 {
481    char *next_args;
482    Eina_Bool powered;
483    E_Bluez_Element *element = _element_from_args(args, &next_args);
484
485    if (!element)
486            return ECORE_CALLBACK_RENEW;
487
488    if (!args)
489      {
490         fputs("ERROR: missing the powered value\n", stderr);
491         return ECORE_CALLBACK_RENEW;
492      }
493
494    powered = !!atol(next_args);
495
496    if (e_bluez_adapter_powered_set
497        (element, powered, _method_success_check, "adapter_set_powered"))
498      printf(":::Adapter %s Powered set to %hhu\n", element->path, powered);
499    else
500      fputs("ERROR: can't set device powered\n", stderr);
501    return ECORE_CALLBACK_RENEW;
502 }
503
504 static Eina_Bool
505 _on_cmd_adapter_get_discoverable(__UNUSED__ char *cmd, char *args)
506 {
507    char *next_args;
508    Eina_Bool discoverable;
509    E_Bluez_Element *element = _element_from_args(args, &next_args);
510
511    if (!element)
512            return ECORE_CALLBACK_RENEW;
513
514    if (e_bluez_adapter_discoverable_get(element, &discoverable))
515      printf(":::Adapter discoverable = \"%hhu\"\n", discoverable);
516    else
517      fputs("ERROR: can't get adapter discoverable\n", stderr);
518    return ECORE_CALLBACK_RENEW;
519 }
520
521 static Eina_Bool
522 _on_cmd_adapter_set_discoverable(__UNUSED__ char *cmd, char *args)
523 {
524    char *next_args;
525    Eina_Bool discoverable;
526    E_Bluez_Element *element = _element_from_args(args, &next_args);
527
528    if (!element)
529            return ECORE_CALLBACK_RENEW;
530
531    if (!args)
532      {
533         fputs("ERROR: missing the discoverable value\n", stderr);
534         return ECORE_CALLBACK_RENEW;
535      }
536
537    discoverable = !!atol(next_args);
538
539    if (e_bluez_adapter_discoverable_set
540        (element, discoverable, _method_success_check, "adapter_set_discoverable"))
541      printf(":::Adapter %s discoverable set to %hhu\n", element->path, discoverable);
542    else
543      fputs("ERROR: can't set adapter discoverable\n", stderr);
544    return ECORE_CALLBACK_RENEW;
545 }
546
547 static Eina_Bool
548 _on_cmd_adapter_get_discoverable_timeout(__UNUSED__ char *cmd, char *args)
549 {
550    char *next_args;
551    unsigned int timeout;
552    E_Bluez_Element *element = _element_from_args(args, &next_args);
553
554    if (!element)
555            return ECORE_CALLBACK_RENEW;
556
557    if (e_bluez_adapter_discoverable_timeout_get(element, &timeout))
558      printf(":::Adapter %s DiscovableTimeout = %hu\n", element->path, timeout);
559    else
560      fputs("ERROR: can't get adapter discoverable timeout\n", stderr);
561    return ECORE_CALLBACK_RENEW;
562 }
563
564 static Eina_Bool
565 _on_cmd_adapter_set_discoverable_timeout(__UNUSED__ char *cmd, char *args)
566 {
567    char *next_args, *p;
568    unsigned int timeout;
569    E_Bluez_Element *element = _element_from_args(args, &next_args);
570
571    if (!element)
572            return ECORE_CALLBACK_RENEW;
573
574    if (!next_args) {
575       fprintf(stderr, "ERROR: missing timeout value\n");
576       return ECORE_CALLBACK_RENEW;
577    }
578
579    timeout = strtol(next_args, &p, 0);
580    if (p == next_args)
581      {
582         fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
583         return ECORE_CALLBACK_RENEW;
584      }
585
586    if (e_bluez_adapter_discoverable_timeout_set(element, timeout,
587                         _method_success_check,
588                         "adapter_set_discoverable_timeout"))
589      printf(":::Adapter %s scan interval set to %hu\n", element->path, timeout);
590    else
591      fputs("ERROR: can't set adapter discoverable timeout\n", stderr);
592    return ECORE_CALLBACK_RENEW;
593 }
594
595 static Eina_Bool
596 _on_cmd_adapter_get_discovering(__UNUSED__ char *cmd, char *args)
597 {
598    char *next_args;
599    Eina_Bool discovering;
600    E_Bluez_Element *element = _element_from_args(args, &next_args);
601
602    if (!element)
603            return ECORE_CALLBACK_RENEW;
604
605    if (e_bluez_adapter_discovering_get(element, &discovering))
606      printf(":::Adapter discovering = \"%hhu\"\n", discovering);
607    else
608      fputs("ERROR: can't get adapter's Discovering\n", stderr);
609    return ECORE_CALLBACK_RENEW;
610 }
611
612 static Eina_Bool
613 _on_cmd_adapter_start_discovery(__UNUSED__ char *cmd, char *args)
614 {
615    char *next_args;
616    E_Bluez_Element *element = _element_from_args(args, &next_args);
617
618    if (!element)
619            return ECORE_CALLBACK_RENEW;
620
621    if (e_bluez_adapter_start_discovery(element,
622         _method_success_check, "adapter_start_discovery"))
623      printf(":::Adapter Start Discovery for %s\n", element->path);
624    else
625      fputs("ERROR: can't start discovery on adapter \n", stderr);
626    return ECORE_CALLBACK_RENEW;
627 }
628
629 static Eina_Bool
630 _on_cmd_adapter_stop_discovery(__UNUSED__ char *cmd, char *args)
631 {
632    char *next_args;
633    E_Bluez_Element *element = _element_from_args(args, &next_args);
634
635    if (!element)
636            return ECORE_CALLBACK_RENEW;
637
638    if (e_bluez_adapter_stop_discovery(element,
639         _method_success_check, "adapter_stop_discovery"))
640      printf(":::Adapter Stop Discovery for %s\n", element->path);
641    else
642      fputs("ERROR: can't stop discovery on adapter \n", stderr);
643    return ECORE_CALLBACK_RENEW;
644 }
645
646 static Eina_Bool
647 _on_cmd_adapter_create_paired_device(__UNUSED__ char *cmd, char *args)
648 {
649    char *next_args, *path, *cap, *device;
650    E_Bluez_Element *element = _element_from_args(args, &next_args);
651
652    if (!element)
653            return ECORE_CALLBACK_RENEW;
654
655    if (!next_args) {
656            fputs("ERROR: missing parameters name, type and value.\n", stderr);
657            return ECORE_CALLBACK_RENEW;
658    }
659
660    path = next_args;
661    cap = _tok(path);
662    if (!cap) {
663            fputs("ERROR: missing parameters name, type and value.\n", stderr);
664            return ECORE_CALLBACK_RENEW;
665    }
666    device = _tok(cap);
667    if (!device) {
668            fputs("ERROR: missing parameters name, type and value.\n", stderr);
669            return ECORE_CALLBACK_RENEW;
670    }
671
672    if (e_bluez_adapter_create_paired_device(element, path, cap, device,
673                 _create_paired_device_cb, "adapter_create_paired_device")) {
674      printf(":::Creating Paired Device %s (%s)...\n", path, cap);
675      iface = e_dbus_interface_new("org.bluez.Agent");
676      if (!iface) {
677         fputs("WARNING: Cannot add org.bluez.Agent interface",stderr);
678         return EINA_FALSE;
679      }
680      _msgbus_data->obj = e_dbus_object_add(_msgbus_data->conn, path, NULL);
681      e_dbus_object_interface_attach(_msgbus_data->obj, iface);
682      e_dbus_interface_method_add(iface, "RequestPinCode", "o", "s",
683                      _request_pincode_cb);
684
685    }
686    else
687      fprintf(stderr, "ERROR: can't create paired device %s\n", path);
688
689    return ECORE_CALLBACK_RENEW;
690 }
691
692 /* Devices Commands */
693
694 static Eina_Bool
695 _on_cmd_device_get_name(__UNUSED__ char *cmd, char *args)
696 {
697    const char *name;
698    char *next_args;
699    E_Bluez_Element *element = _element_from_args(args, &next_args);
700
701    if (!element)
702            return ECORE_CALLBACK_RENEW;
703
704    if (e_bluez_device_name_get(element, &name))
705      printf(":::Device name = \"%s\"\n", name);
706    else
707      fputs("ERROR: can't get device name\n", stderr);
708    return ECORE_CALLBACK_RENEW;
709 }
710
711 static Eina_Bool
712 _on_input(__UNUSED__ void *data, Ecore_Fd_Handler *fd_handler)
713 {
714    char buf[256];
715    char *cmd, *args;
716    const struct {
717       const char *cmd;
718       Eina_Bool (*cb)(char *cmd, char *args);
719    } *itr, maps[] = {
720      {"quit", _on_cmd_quit},
721      {"sync", _on_cmd_sync},
722      {"get_all", _on_cmd_get_all},
723      {"print", _on_cmd_print},
724      {"get_properties", _on_cmd_get_properties},
725      {"set_property", _on_cmd_property_set},
726      {"manager_get", _on_cmd_manager_get},
727      {"manager_default_adapter", _on_cmd_manager_default_adapter},
728      {"adapter_register_agent", _on_cmd_adapter_register_agent},
729      {"adapter_unregister_agent", _on_cmd_adapter_unregister_agent},
730      {"adapter_get_address", _on_cmd_adapter_get_address},
731      {"adapter_get_name", _on_cmd_adapter_get_name},
732      {"adapter_set_name", _on_cmd_adapter_set_name},
733      {"adapter_get_powered", _on_cmd_adapter_get_powered},
734      {"adapter_set_powered", _on_cmd_adapter_set_powered},
735      {"adapter_get_discoverable", _on_cmd_adapter_get_discoverable},
736      {"adapter_set_discoverable", _on_cmd_adapter_set_discoverable},
737      {"adapter_get_discoverable_timeout", _on_cmd_adapter_get_discoverable_timeout},
738      {"adapter_set_discoverable_timeout", _on_cmd_adapter_set_discoverable_timeout},
739      {"adapter_get_discovering", _on_cmd_adapter_get_discovering},
740      {"adapter_start_discovery", _on_cmd_adapter_start_discovery},
741      {"adapter_stop_discovery", _on_cmd_adapter_stop_discovery},
742      {"adapter_create_paired_device", _on_cmd_adapter_create_paired_device},
743      {"device_get_name", _on_cmd_device_get_name},
744      {NULL, NULL}
745    };
746
747
748    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
749      {
750         fputs("ERROR: reading from stdin, exit\n", stderr);
751         return EINA_FALSE;
752      }
753
754    if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
755      {
756         fputs("ERROR: nothing to read?\n", stderr);
757         return EINA_FALSE;
758      }
759
760    if (!fgets(buf, sizeof(buf), stdin))
761      {
762         fprintf(stderr, "ERROR: could not read command: %s\n", strerror(errno));
763         ecore_main_loop_quit();
764         return EINA_FALSE;
765      }
766
767    cmd = buf;
768    while (isspace(*cmd))
769      cmd++;
770
771    args = strchr(cmd, ' ');
772    if (args)
773      {
774         char *p;
775
776         *args = '\0';
777         args++;
778
779         while (isspace(*args))
780           args++;
781
782         p = args + strlen(args) - 1;
783         if (*p == '\n')
784           *p = '\0';
785      }
786    else
787      {
788         char *p;
789
790         p = cmd + strlen(cmd) - 1;
791         if (*p == '\n')
792           *p = '\0';
793      }
794
795    if (strcmp(cmd, "help") == 0)
796      {
797         if (args)
798           {
799              printf("Commands with '%s' in the name:\n", args);
800              for (itr = maps; itr->cmd; itr++)
801                if (strstr(itr->cmd, args))
802                  printf("\t%s\n", itr->cmd);
803           }
804         else
805           {
806              fputs("Commands:\n", stdout);
807              for (itr = maps; itr->cmd; itr++)
808                printf("\t%s\n", itr->cmd);
809           }
810         fputc('\n', stdout);
811         return ECORE_CALLBACK_RENEW;
812      }
813
814    for (itr = maps; itr->cmd; itr++)
815      if (strcmp(itr->cmd, cmd) == 0)
816        return itr->cb(cmd, args);
817
818    printf("unknown command \"%s\", args=%s\n", cmd, args);
819    return ECORE_CALLBACK_RENEW;
820 }
821
822 int
823 main(__UNUSED__ int argc,__UNUSED__ char *argv[])
824 {
825    ecore_init();
826    e_dbus_init();
827    eina_init();
828
829    _msgbus_data = calloc(1, sizeof(E_Msgbus_Data));
830    _msgbus_data->conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
831    if (!_msgbus_data->conn) {
832       printf("ERROR: can't connect to system session\n");
833       return -1;
834    }
835
836    e_bluez_system_init(_msgbus_data->conn);
837
838    ecore_event_handler_add(E_BLUEZ_EVENT_ELEMENT_ADD, _on_element_add, NULL);
839    ecore_event_handler_add(E_BLUEZ_EVENT_ELEMENT_DEL, _on_element_del, NULL);
840    ecore_event_handler_add(E_BLUEZ_EVENT_ELEMENT_UPDATED,
841                            _on_element_updated, NULL);
842    ecore_event_handler_add(E_BLUEZ_EVENT_DEVICE_FOUND,
843                            _on_device_found, NULL);
844
845    ecore_main_fd_handler_add
846      (0, ECORE_FD_READ | ECORE_FD_ERROR, _on_input, NULL, NULL, NULL);
847
848    ecore_main_loop_begin();
849
850    e_bluez_system_shutdown();
851
852    e_dbus_connection_close(_msgbus_data->conn);
853    eina_shutdown();
854    e_dbus_shutdown();
855    ecore_shutdown();
856
857    fputs("DBG: clean exit.\n", stderr);
858
859    return 0;
860 }