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