13 typedef struct _E_Msgbus_Data E_Msgbus_Data;
15 struct _E_Msgbus_Data {
16 E_DBus_Connection *conn;
20 static E_Msgbus_Data *_msgbus_data = NULL;
22 static E_DBus_Interface *iface = NULL;
25 _method_success_check(void *data, __UNUSED__ DBusMessage *msg, DBusError *error)
27 const char *name = data;
29 if ((!error) || (!dbus_error_is_set(error)))
31 printf("SUCCESS: method %s() finished successfully.\n", name);
35 printf("FAILURE: method %s() finished with error: %s %s\n",
36 name, error->name, error->message);
37 dbus_error_free(error);
41 _default_adapter_callback(__UNUSED__ void *data, DBusMessage *msg, __UNUSED__ DBusError *err)
43 E_Bluez_Element *element;
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");
50 printf("SUCCESS: default adapter: %s\n", path);
52 element = e_bluez_element_get(path);
53 e_bluez_element_print(stdout, element);
59 _create_paired_device_cb(void *data, DBusMessage *msg, DBusError *err)
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);
68 _request_pincode_cb(__UNUSED__ E_DBus_Object *obj, DBusMessage *msg)
75 printf("Enter PIN Code:\n");
76 ret = scanf("%15s", p);
80 reply = dbus_message_new_method_return(msg);
81 dbus_message_append_args(reply, DBUS_TYPE_STRING, &p,
88 _elements_print(E_Bluez_Element **elements, unsigned int count)
91 for (i = 0; i < count; i++)
93 printf("--- element %d:\n", i);
94 e_bluez_element_print(stdout, elements[i]);
97 printf("END: all elements count = %u\n", count);
101 _on_element_add(__UNUSED__ void *data, __UNUSED__ int type, void *info)
103 E_Bluez_Element *element = info;
104 printf(">>> %s\n", element->path);
105 return ECORE_CALLBACK_RENEW;
109 _on_element_del(__UNUSED__ void *data, __UNUSED__ int type, void *info)
111 E_Bluez_Element *element = info;
112 printf("<<< %s\n", element->path);
113 return ECORE_CALLBACK_RENEW;
117 _on_element_updated(__UNUSED__ void *data, __UNUSED__ int type, void *info)
119 E_Bluez_Element *element = info;
120 printf("!!! %s\n", element->path);
121 e_bluez_element_print(stderr, element);
122 return ECORE_CALLBACK_RENEW;
126 _on_device_found(__UNUSED__ void *data, int __UNUSED__ type, void *info)
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);
134 e_bluez_devicefound_free(device);
135 return ECORE_CALLBACK_RENEW;
139 _on_cmd_quit(__UNUSED__ char *cmd, __UNUSED__ char *args)
141 fputs("Bye!\n", stderr);
142 ecore_main_loop_quit();
147 _on_cmd_sync(__UNUSED__ char *cmd, __UNUSED__ char *args)
149 e_bluez_manager_sync_elements();
150 return ECORE_CALLBACK_RENEW;
171 _on_cmd_get_all(__UNUSED__ char *cmd, char *args)
173 E_Bluez_Element **elements;
184 ret = e_bluez_elements_get_all_type(type, &count, &elements);
186 ret = e_bluez_elements_get_all(&count, &elements);
189 fputs("ERROR: could not get elements\n", stderr);
192 printf("BEG: all elements type=%s count = %d\n", type, count);
193 _elements_print(elements, count);
196 return ECORE_CALLBACK_RENEW;
199 static E_Bluez_Element *
200 _element_from_args(char *args, char **next_args)
202 E_Bluez_Element *element;
206 fputs("ERROR: missing element path\n", stderr);
211 *next_args = _tok(args);
212 element = e_bluez_element_get(args);
214 fprintf(stderr, "ERROR: no element called \"%s\".\n", args);
220 _on_cmd_print(__UNUSED__ char *cmd, char *args)
223 E_Bluez_Element *element = _element_from_args(args, &next_args);
225 e_bluez_element_print(stdout, element);
226 return ECORE_CALLBACK_RENEW;
230 _on_cmd_get_properties(__UNUSED__ char *cmd, char *args)
233 E_Bluez_Element *element = _element_from_args(args, &next_args);
235 e_bluez_element_properties_sync(element);
236 return ECORE_CALLBACK_RENEW;
240 _on_cmd_property_set(__UNUSED__ char *cmd, char *args)
242 char *next_args, *name, *p;
243 E_Bluez_Element *element = _element_from_args(args, &next_args);
251 return ECORE_CALLBACK_RENEW;
255 fputs("ERROR: missing parameters name, type and value.\n", stderr);
256 return ECORE_CALLBACK_RENEW;
263 fputs("ERROR: missing parameters type and value.\n", stderr);
264 return ECORE_CALLBACK_RENEW;
270 fputs("ERROR: missing parameter value.\n", stderr);
271 return ECORE_CALLBACK_RENEW;
277 case DBUS_TYPE_BOOLEAN:
278 vlong = !!atol(next_args);
280 fprintf(stderr, "DBG: boolean is: %ld\n", vlong);
282 case DBUS_TYPE_UINT16:
283 vu16 = strtol(next_args, &p, 0);
286 fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
287 return ECORE_CALLBACK_RENEW;
290 fprintf(stderr, "DBG: u16 is: %hu\n", vu16);
292 case DBUS_TYPE_UINT32:
293 vu32 = strtol(next_args, &p, 0);
296 fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
297 return ECORE_CALLBACK_RENEW;
300 fprintf(stderr, "DBG: u16 is: %u\n", vu32);
302 case DBUS_TYPE_STRING:
303 case DBUS_TYPE_OBJECT_PATH:
304 p = next_args + strlen(next_args);
307 while (p > next_args && isspace(*p))
311 fprintf(stderr, "ERROR: invalid string \"%s\".\n", next_args);
315 fprintf(stderr, "DBG: string is: \"%s\"\n", next_args);
318 fprintf(stderr, "ERROR: don't know how to parse type '%c' (%d)\n",
320 return ECORE_CALLBACK_RENEW;
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);
328 return ECORE_CALLBACK_RENEW;
331 /* Manager Commands */
334 _on_cmd_manager_get(__UNUSED__ char *cmd, __UNUSED__ char *args)
336 E_Bluez_Element *element;
337 element = e_bluez_manager_get();
338 e_bluez_element_print(stderr, element);
339 return ECORE_CALLBACK_RENEW;
343 _on_cmd_manager_default_adapter(__UNUSED__ char *cmd, __UNUSED__ char *args)
345 return e_bluez_manager_default_adapter(_default_adapter_callback, NULL);
348 /* Adapter Commands */
351 _on_cmd_adapter_register_agent(__UNUSED__ char *cmd, char *args)
353 char *next_args, *path, *cap;
354 E_Bluez_Element *element = _element_from_args(args, &next_args);
357 return ECORE_CALLBACK_RENEW;
360 fputs("ERROR: missing parameters name, type and value.\n", stderr);
361 return ECORE_CALLBACK_RENEW;
367 fputs("ERROR: missing parameters name, type and value.\n", stderr);
368 return ECORE_CALLBACK_RENEW;
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);
375 fprintf(stderr, "ERROR: can't register agent %s\n", path);
377 return ECORE_CALLBACK_RENEW;
381 _on_cmd_adapter_unregister_agent(__UNUSED__ char *cmd, char *args)
383 char *path, *next_args;
384 E_Bluez_Element *element = _element_from_args(args, &next_args);
387 return ECORE_CALLBACK_RENEW;
391 fputs("ERROR: missing the object path\n", stderr);
392 return ECORE_CALLBACK_RENEW;
396 if (e_bluez_adapter_agent_unregister(element,
397 path, _method_success_check, "adapter_unregister_agent"))
398 printf(":::Unregistering agent %s...\n", path);
400 fprintf(stderr, "ERROR: can't unregister agent %s\n", path);
402 return ECORE_CALLBACK_RENEW;
406 _on_cmd_adapter_get_address(__UNUSED__ char *cmd, char *args)
410 E_Bluez_Element *element = _element_from_args(args, &next_args);
413 return ECORE_CALLBACK_RENEW;
415 if (e_bluez_adapter_address_get(element, &address))
416 printf(":::Adapter address = \"%s\"\n", address);
418 fputs("ERROR: can't get adapter address\n", stderr);
419 return ECORE_CALLBACK_RENEW;
423 _on_cmd_adapter_get_name(__UNUSED__ char *cmd, char *args)
427 E_Bluez_Element *element = _element_from_args(args, &next_args);
430 return ECORE_CALLBACK_RENEW;
432 if (e_bluez_adapter_name_get(element, &name))
433 printf(":::Adapter name = \"%s\"\n", name);
435 fputs("ERROR: can't get adapter name\n", stderr);
436 return ECORE_CALLBACK_RENEW;
440 _on_cmd_adapter_set_name(__UNUSED__ char *cmd, char *args)
443 E_Bluez_Element *element = _element_from_args(args, &next_args);
446 return ECORE_CALLBACK_RENEW;
449 fprintf(stderr, "ERROR: missing name value\n");
450 return ECORE_CALLBACK_RENEW;
453 if (e_bluez_adapter_name_set(element, next_args, _method_success_check,
455 printf(":::Adapter %s Name set to %s\n", element->path, next_args);
457 fputs("ERROR: can't set adapter name\n", stderr);
458 return ECORE_CALLBACK_RENEW;
462 _on_cmd_adapter_get_powered(__UNUSED__ char *cmd, char *args)
466 E_Bluez_Element *element = _element_from_args(args, &next_args);
469 return ECORE_CALLBACK_RENEW;
471 if (e_bluez_adapter_powered_get(element, &powered))
472 printf(":::Adapter powered = \"%hhu\"\n", powered);
474 fputs("ERROR: can't get adapter powered\n", stderr);
475 return ECORE_CALLBACK_RENEW;
479 _on_cmd_adapter_set_powered(__UNUSED__ char *cmd, char *args)
483 E_Bluez_Element *element = _element_from_args(args, &next_args);
486 return ECORE_CALLBACK_RENEW;
490 fputs("ERROR: missing the powered value\n", stderr);
491 return ECORE_CALLBACK_RENEW;
494 powered = !!atol(next_args);
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);
500 fputs("ERROR: can't set device powered\n", stderr);
501 return ECORE_CALLBACK_RENEW;
505 _on_cmd_adapter_get_discoverable(__UNUSED__ char *cmd, char *args)
508 Eina_Bool discoverable;
509 E_Bluez_Element *element = _element_from_args(args, &next_args);
512 return ECORE_CALLBACK_RENEW;
514 if (e_bluez_adapter_discoverable_get(element, &discoverable))
515 printf(":::Adapter discoverable = \"%hhu\"\n", discoverable);
517 fputs("ERROR: can't get adapter discoverable\n", stderr);
518 return ECORE_CALLBACK_RENEW;
522 _on_cmd_adapter_set_discoverable(__UNUSED__ char *cmd, char *args)
525 Eina_Bool discoverable;
526 E_Bluez_Element *element = _element_from_args(args, &next_args);
529 return ECORE_CALLBACK_RENEW;
533 fputs("ERROR: missing the discoverable value\n", stderr);
534 return ECORE_CALLBACK_RENEW;
537 discoverable = !!atol(next_args);
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);
543 fputs("ERROR: can't set adapter discoverable\n", stderr);
544 return ECORE_CALLBACK_RENEW;
548 _on_cmd_adapter_get_discoverable_timeout(__UNUSED__ char *cmd, char *args)
551 unsigned int timeout;
552 E_Bluez_Element *element = _element_from_args(args, &next_args);
555 return ECORE_CALLBACK_RENEW;
557 if (e_bluez_adapter_discoverable_timeout_get(element, &timeout))
558 printf(":::Adapter %s DiscovableTimeout = %hu\n", element->path, timeout);
560 fputs("ERROR: can't get adapter discoverable timeout\n", stderr);
561 return ECORE_CALLBACK_RENEW;
565 _on_cmd_adapter_set_discoverable_timeout(__UNUSED__ char *cmd, char *args)
568 unsigned int timeout;
569 E_Bluez_Element *element = _element_from_args(args, &next_args);
572 return ECORE_CALLBACK_RENEW;
575 fprintf(stderr, "ERROR: missing timeout value\n");
576 return ECORE_CALLBACK_RENEW;
579 timeout = strtol(next_args, &p, 0);
582 fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
583 return ECORE_CALLBACK_RENEW;
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);
591 fputs("ERROR: can't set adapter discoverable timeout\n", stderr);
592 return ECORE_CALLBACK_RENEW;
596 _on_cmd_adapter_get_discovering(__UNUSED__ char *cmd, char *args)
599 Eina_Bool discovering;
600 E_Bluez_Element *element = _element_from_args(args, &next_args);
603 return ECORE_CALLBACK_RENEW;
605 if (e_bluez_adapter_discovering_get(element, &discovering))
606 printf(":::Adapter discovering = \"%hhu\"\n", discovering);
608 fputs("ERROR: can't get adapter's Discovering\n", stderr);
609 return ECORE_CALLBACK_RENEW;
613 _on_cmd_adapter_start_discovery(__UNUSED__ char *cmd, char *args)
616 E_Bluez_Element *element = _element_from_args(args, &next_args);
619 return ECORE_CALLBACK_RENEW;
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);
625 fputs("ERROR: can't start discovery on adapter \n", stderr);
626 return ECORE_CALLBACK_RENEW;
630 _on_cmd_adapter_stop_discovery(__UNUSED__ char *cmd, char *args)
633 E_Bluez_Element *element = _element_from_args(args, &next_args);
636 return ECORE_CALLBACK_RENEW;
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);
642 fputs("ERROR: can't stop discovery on adapter \n", stderr);
643 return ECORE_CALLBACK_RENEW;
647 _on_cmd_adapter_create_paired_device(__UNUSED__ char *cmd, char *args)
649 char *next_args, *path, *cap, *device;
650 E_Bluez_Element *element = _element_from_args(args, &next_args);
653 return ECORE_CALLBACK_RENEW;
656 fputs("ERROR: missing parameters name, type and value.\n", stderr);
657 return ECORE_CALLBACK_RENEW;
663 fputs("ERROR: missing parameters name, type and value.\n", stderr);
664 return ECORE_CALLBACK_RENEW;
668 fputs("ERROR: missing parameters name, type and value.\n", stderr);
669 return ECORE_CALLBACK_RENEW;
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");
677 fputs("WARNING: Cannot add org.bluez.Agent interface",stderr);
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);
687 fprintf(stderr, "ERROR: can't create paired device %s\n", path);
689 return ECORE_CALLBACK_RENEW;
692 /* Devices Commands */
695 _on_cmd_device_get_name(__UNUSED__ char *cmd, char *args)
699 E_Bluez_Element *element = _element_from_args(args, &next_args);
702 return ECORE_CALLBACK_RENEW;
704 if (e_bluez_device_name_get(element, &name))
705 printf(":::Device name = \"%s\"\n", name);
707 fputs("ERROR: can't get device name\n", stderr);
708 return ECORE_CALLBACK_RENEW;
712 _on_input(__UNUSED__ void *data, Ecore_Fd_Handler *fd_handler)
718 Eina_Bool (*cb)(char *cmd, char *args);
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},
748 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
750 fputs("ERROR: reading from stdin, exit\n", stderr);
754 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
756 fputs("ERROR: nothing to read?\n", stderr);
760 if (!fgets(buf, sizeof(buf), stdin))
762 fprintf(stderr, "ERROR: could not read command: %s\n", strerror(errno));
763 ecore_main_loop_quit();
768 while (isspace(*cmd))
771 args = strchr(cmd, ' ');
779 while (isspace(*args))
782 p = args + strlen(args) - 1;
790 p = cmd + strlen(cmd) - 1;
795 if (strcmp(cmd, "help") == 0)
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);
806 fputs("Commands:\n", stdout);
807 for (itr = maps; itr->cmd; itr++)
808 printf("\t%s\n", itr->cmd);
811 return ECORE_CALLBACK_RENEW;
814 for (itr = maps; itr->cmd; itr++)
815 if (strcmp(itr->cmd, cmd) == 0)
816 return itr->cb(cmd, args);
818 printf("unknown command \"%s\", args=%s\n", cmd, args);
819 return ECORE_CALLBACK_RENEW;
823 main(__UNUSED__ int argc,__UNUSED__ char *argv[])
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");
836 e_bluez_system_init(_msgbus_data->conn);
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);
845 ecore_main_fd_handler_add
846 (0, ECORE_FD_READ | ECORE_FD_ERROR, _on_input, NULL, NULL, NULL);
848 ecore_main_loop_begin();
850 e_bluez_system_shutdown();
852 e_dbus_connection_close(_msgbus_data->conn);
857 fputs("DBG: clean exit.\n", stderr);