diff options
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/adapter.c | 21 | ||||
-rw-r--r-- | daemon/adapter.h | 4 | ||||
-rw-r--r-- | daemon/manager.c | 90 | ||||
-rw-r--r-- | daemon/service.c | 138 | ||||
-rw-r--r-- | daemon/service.h | 4 |
5 files changed, 161 insertions, 96 deletions
diff --git a/daemon/adapter.c b/daemon/adapter.c index 12837507..e4192f96 100644 --- a/daemon/adapter.c +++ b/daemon/adapter.c @@ -38,7 +38,8 @@ static DBusConnection *connection = NULL; -DBusMessage *adapter_list(DBusMessage *msg) +DBusHandlerResult manager_list_adapters(DBusConnection *conn, + DBusMessage *msg, void *data) { DBusMessage *reply; DBusMessageIter iter, array; @@ -46,7 +47,7 @@ DBusMessage *adapter_list(DBusMessage *msg) reply = dbus_message_new_method_return(msg); if (!reply) - return NULL; + return DBUS_HANDLER_RESULT_NEED_MEMORY; dbus_message_iter_init_append(reply, &iter); @@ -57,10 +58,11 @@ DBusMessage *adapter_list(DBusMessage *msg) dbus_message_iter_close_container(&iter, &array); - return reply; + return dbus_connection_send_and_unref(conn, reply); } -DBusMessage *adapter_find(DBusMessage *msg) +DBusHandlerResult manager_find_adapter(DBusConnection *conn, + DBusMessage *msg, void *data) { DBusMessage *reply; const char *pattern; @@ -73,27 +75,28 @@ DBusMessage *adapter_find(DBusMessage *msg) reply = dbus_message_new_method_return(msg); if (!reply) - return NULL; + return DBUS_HANDLER_RESULT_NEED_MEMORY; dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr, DBUS_TYPE_INVALID); - return reply; + return dbus_connection_send_and_unref(conn, reply); } -DBusMessage *adapter_default(DBusMessage *msg) +DBusHandlerResult manager_default_adapter(DBusConnection *conn, + DBusMessage *msg, void *data) { DBusMessage *reply; const char path[] = "/org/bluez/hci0", *ptr = path; reply = dbus_message_new_method_return(msg); if (!reply) - return NULL; + return DBUS_HANDLER_RESULT_NEED_MEMORY; dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr, DBUS_TYPE_INVALID); - return reply; + return dbus_connection_send_and_unref(conn, reply); } int adapter_init(DBusConnection *conn) diff --git a/daemon/adapter.h b/daemon/adapter.h index 25c54d52..60834365 100644 --- a/daemon/adapter.h +++ b/daemon/adapter.h @@ -23,7 +23,3 @@ int adapter_init(DBusConnection *conn); void adapter_exit(void); - -DBusMessage *adapter_list(DBusMessage *msg); -DBusMessage *adapter_find(DBusMessage *msg); -DBusMessage *adapter_default(DBusMessage *msg); diff --git a/daemon/manager.c b/daemon/manager.c index 301e3aba..8693486c 100644 --- a/daemon/manager.c +++ b/daemon/manager.c @@ -39,90 +39,36 @@ static DBusConnection *connection = NULL; -static DBusHandlerResult list_adapters(DBusConnection *conn, - DBusMessage *msg, void *data) -{ - DBusMessage *reply; - - reply = adapter_list(msg); - if (!reply) - return DBUS_HANDLER_RESULT_NEED_MEMORY; - - return dbus_connection_send_and_unref(conn, reply); -} - -static DBusHandlerResult find_adapter(DBusConnection *conn, - DBusMessage *msg, void *data) -{ - DBusMessage *reply; - - reply = adapter_find(msg); - if (!reply) - return DBUS_HANDLER_RESULT_NEED_MEMORY; - - return dbus_connection_send_and_unref(conn, reply); -} - -static DBusHandlerResult default_adapter(DBusConnection *conn, - DBusMessage *msg, void *data) -{ - DBusMessage *reply; - - reply = adapter_default(msg); - if (!reply) - return DBUS_HANDLER_RESULT_NEED_MEMORY; +extern DBusHandlerResult manager_list_adapters(DBusConnection *conn, + DBusMessage *msg, void *data); - return dbus_connection_send_and_unref(conn, reply); -} - -static DBusHandlerResult list_services(DBusConnection *conn, - DBusMessage *msg, void *data) -{ - DBusMessage *reply; - - reply = service_list(msg); - if (!reply) - return DBUS_HANDLER_RESULT_NEED_MEMORY; - - return dbus_connection_send_and_unref(conn, reply); -} +extern DBusHandlerResult manager_find_adapter(DBusConnection *conn, + DBusMessage *msg, void *data); -static DBusHandlerResult find_service(DBusConnection *conn, - DBusMessage *msg, void *data) -{ - DBusMessage *reply; - - reply = service_find(msg); - if (!reply) - return DBUS_HANDLER_RESULT_NEED_MEMORY; - - return dbus_connection_send_and_unref(conn, reply); -} +extern DBusHandlerResult manager_default_adapter(DBusConnection *conn, + DBusMessage *msg, void *data); -static DBusHandlerResult activate_service(DBusConnection *conn, - DBusMessage *msg, void *data) -{ - DBusMessage *reply; +extern DBusHandlerResult manager_list_services(DBusConnection *conn, + DBusMessage *msg, void *data); - reply = service_activate(msg); - if (!reply) - return DBUS_HANDLER_RESULT_NEED_MEMORY; +extern DBusHandlerResult manager_find_service(DBusConnection *conn, + DBusMessage *msg, void *data); - return dbus_connection_send_and_unref(conn, reply); -} +extern DBusHandlerResult manager_activate_service(DBusConnection *conn, + DBusMessage *msg, void *data); static DBusMethodVTable manager_table[] = { - { "ListAdapters", list_adapters, + { "ListAdapters", manager_list_adapters, DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_STRING_ARRAY_AS_STRING }, - { "FindAdapter", find_adapter, + { "FindAdapter", manager_find_adapter, DBUS_TYPE_STRING_AS_STRING, DBUS_TYPE_STRING_AS_STRING }, - { "DefaultAdapter", default_adapter, + { "DefaultAdapter", manager_default_adapter, DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_STRING_AS_STRING }, - { "ListServices", list_services, + { "ListServices", manager_list_services, DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_STRING_ARRAY_AS_STRING }, - { "FindService", find_service, + { "FindService", manager_find_service, DBUS_TYPE_STRING_AS_STRING, DBUS_TYPE_STRING_AS_STRING }, - { "ActivateService", activate_service, + { "ActivateService", manager_activate_service, DBUS_TYPE_STRING_AS_STRING, DBUS_TYPE_STRING_AS_STRING }, { } }; diff --git a/daemon/service.c b/daemon/service.c index b56087c4..4d024b3a 100644 --- a/daemon/service.c +++ b/daemon/service.c @@ -38,26 +38,31 @@ static DBusConnection *connection = NULL; -DBusMessage *service_list(DBusMessage *msg) +DBusHandlerResult manager_list_services(DBusConnection *conn, + DBusMessage *msg, void *data) { DBusMessage *reply; DBusMessageIter iter, array; + const char path[] = "/org/bluez/service", *ptr = path; reply = dbus_message_new_method_return(msg); if (!reply) - return NULL; + return DBUS_HANDLER_RESULT_NEED_MEMORY; dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &array); + dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &ptr); + dbus_message_iter_close_container(&iter, &array); - return reply; + return dbus_connection_send_and_unref(conn, reply); } -DBusMessage *service_find(DBusMessage *msg) +DBusHandlerResult manager_find_service(DBusConnection *conn, + DBusMessage *msg, void *data) { DBusMessage *reply; const char *pattern; @@ -69,11 +74,14 @@ DBusMessage *service_find(DBusMessage *msg) reply = dbus_message_new_error(msg, ERROR_INTERFACE ".NotFound", "Service does not exists"); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; - return reply; + return dbus_connection_send_and_unref(conn, reply); } -DBusMessage *service_activate(DBusMessage *msg) +DBusHandlerResult manager_activate_service(DBusConnection *conn, + DBusMessage *msg, void *data) { DBusMessage *reply; const char *pattern; @@ -85,10 +93,106 @@ DBusMessage *service_activate(DBusMessage *msg) reply = dbus_message_new_error(msg, ERROR_INTERFACE ".NotFound", "Service does not exists"); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; - return reply; + return dbus_connection_send_and_unref(conn, reply); } +static DBusHandlerResult service_get_name(DBusConnection *conn, + DBusMessage *msg, void *data) +{ + DBusMessage *reply; + const char name[] = "Demo service", *ptr = name; + + reply = dbus_message_new_method_return(msg); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; + + dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr, + DBUS_TYPE_INVALID); + + return dbus_connection_send_and_unref(conn, reply); +} + +static DBusHandlerResult service_get_description(DBusConnection *conn, + DBusMessage *msg, void *data) +{ + DBusMessage *reply; + const char text[] = "Demo service for testing", *ptr = text; + + reply = dbus_message_new_method_return(msg); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; + + dbus_message_append_args(reply, DBUS_TYPE_STRING, &ptr, + DBUS_TYPE_INVALID); + + return dbus_connection_send_and_unref(conn, reply); +} + +static DBusHandlerResult service_start(DBusConnection *conn, + DBusMessage *msg, void *data) +{ + DBusMessage *reply; + + debug("Starting service"); + + reply = dbus_message_new_method_return(msg); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; + + dbus_message_append_args(reply, DBUS_TYPE_INVALID); + + return dbus_connection_send_and_unref(conn, reply); +} + +static DBusHandlerResult service_is_running(DBusConnection *conn, + DBusMessage *msg, void *data) +{ + DBusMessage *reply; + dbus_bool_t running = FALSE; + + reply = dbus_message_new_method_return(msg); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; + + dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &running, + DBUS_TYPE_INVALID); + + return dbus_connection_send_and_unref(conn, reply); +} + +static DBusHandlerResult service_stop(DBusConnection *conn, + DBusMessage *msg, void *data) +{ + DBusMessage *reply; + + debug("Stopping service"); + + reply = dbus_message_new_method_return(msg); + if (!reply) + return DBUS_HANDLER_RESULT_NEED_MEMORY; + + dbus_message_append_args(reply, DBUS_TYPE_INVALID); + + return dbus_connection_send_and_unref(conn, reply); +} + +static DBusMethodVTable service_table[] = { + { "GetName", service_get_name, + DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_STRING_AS_STRING }, + { "GetDescription", service_get_description, + DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_STRING_AS_STRING }, + { "Start", service_start, + DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_INVALID_AS_STRING }, + { "Stop", service_stop, + DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_INVALID_AS_STRING }, + { "IsRunning", service_is_running, + DBUS_TYPE_INVALID_AS_STRING, DBUS_TYPE_BOOLEAN_AS_STRING }, + { } +}; + static void config_notify(int action, const char *name, void *data) { switch (action) { @@ -114,6 +218,21 @@ int service_init(DBusConnection *conn) notify_add(CONFIGDIR, config_notify, NULL); + if (dbus_connection_create_object_path(connection, + "/org/bluez/service", NULL, NULL) == FALSE) { + error("Service path registration failed"); + dbus_connection_unref(connection); + return -1; + } + + if (dbus_connection_register_interface(connection, "/org/bluez/service", + SERVICE_INTERFACE, service_table, NULL) == FALSE) { + error("Service interface registration failed"); + dbus_connection_destroy_object_path(connection, "/org/bluez/service"); + dbus_connection_unref(connection); + return -1; + } + return 0; } @@ -123,6 +242,11 @@ void service_exit(void) notify_remove(CONFIGDIR); + dbus_connection_unregister_interface(connection, + "/org/bluez/service", SERVICE_INTERFACE); + + dbus_connection_destroy_object_path(connection, "/org/bluez/service"); + dbus_connection_unref(connection); connection = NULL; diff --git a/daemon/service.h b/daemon/service.h index 396a6841..65e5ab84 100644 --- a/daemon/service.h +++ b/daemon/service.h @@ -23,7 +23,3 @@ int service_init(DBusConnection *conn); void service_exit(void); - -DBusMessage *service_list(DBusMessage *msg); -DBusMessage *service_find(DBusMessage *msg); -DBusMessage *service_activate(DBusMessage *msg); |