diff --git a/audio/main.c b/audio/main.c index f54f74dec..5b4e395b2 100644 --- a/audio/main.c +++ b/audio/main.c @@ -96,17 +96,13 @@ static int audio_init(void) g_key_file_free(config); - register_service("audio"); - - register_uuids("audio", uuids); + register_service("audio", uuids); return 0; } static void audio_exit(void) { - unregister_uuids("audio"); - unregister_service("audio"); audio_manager_exit(); diff --git a/hcid/dbus-service.c b/hcid/dbus-service.c index 5282ffd1e..192ff7381 100644 --- a/hcid/dbus-service.c +++ b/hcid/dbus-service.c @@ -481,6 +481,98 @@ int service_unregister(DBusConnection *conn, struct service *service) return unregister_service_for_connection(conn, service); } +static gint name_cmp(struct service_uuids *su, const char *name) +{ + return strcmp(su->name, name); +} + +static gint uuid_cmp(struct service_uuids *su, const char *uuid) +{ + int i; + + for (i = 0; su->uuids[i]; i++) { + if (!strcasecmp(su->uuids[i], uuid)) + return 0; + } + + return -1; +} + +struct service *search_service_by_uuid(const char *uuid) +{ + struct service_uuids *su; + struct service *service; + GSList *l; + + if (!services_uuids) + return NULL; + + l = g_slist_find_custom(services_uuids, uuid, (GCompareFunc) uuid_cmp); + if (!l) + return NULL; + + su = l->data; + service = search_service(su->name); + if (!service) + return NULL; + + return service; +} + +static void register_uuids(const char *ident, const char **uuids) +{ + struct service_uuids *su; + int i; + + if (!ident) + return; + + su = g_new0(struct service_uuids, 1); + su->name = g_strdup(ident); + + for (i = 0; uuids[i]; i++); + + su->uuids = g_new0(char *, i + 1); + + for (i = 0; uuids[i]; i++) + su->uuids[i] = g_strdup(uuids[i]); + + services_uuids = g_slist_append(services_uuids, su); +} + +static void service_uuids_free(struct service_uuids *su) +{ + int i; + + if (!su) + return; + + g_free(su->name); + + for (i = 0; su->uuids[i]; i++) + g_free(su->uuids[i]); + + g_free(su); +} + +static void unregister_uuids(const char *ident) +{ + struct service_uuids *su; + GSList *l; + + if (!services_uuids) + return; + + l = g_slist_find_custom(services_uuids, ident, (GCompareFunc) name_cmp); + if (!l) + return; + + su = l->data; + services_uuids = g_slist_remove(services_uuids, su); + + service_uuids_free(su); +} + static struct service *create_external_service(const char *ident) { struct service *service; @@ -509,7 +601,7 @@ static struct service *create_external_service(const char *ident) return service; } -int register_service(const char *ident) +int register_service(const char *ident, const char **uuids) { DBusConnection *conn = get_dbus_connection(); struct service *service; @@ -553,6 +645,9 @@ int register_service(const char *ident) services = g_slist_append(services, service); + if (uuids) + register_uuids(ident, uuids); + dbus_connection_emit_signal(conn, BASE_PATH, MANAGER_INTERFACE, "ServiceAdded", DBUS_TYPE_STRING, &service->object_path, @@ -567,98 +662,7 @@ int register_service(const char *ident) void unregister_service(const char *ident) { -} - -static gint name_cmp(struct service_uuids *su, const char *name) -{ - return strcmp(su->name, name); -} - -static gint uuid_cmp(struct service_uuids *su, const char *uuid) -{ - int i; - - for (i = 0; su->uuids[i]; i++) { - if (!strcasecmp(su->uuids[i], uuid)) - return 0; - } - - return -1; -} - -struct service *search_service_by_uuid(const char *uuid) -{ - struct service_uuids *su; - struct service *service; - GSList *l; - - if (!services_uuids) - return NULL; - - l = g_slist_find_custom(services_uuids, uuid, (GCompareFunc) uuid_cmp); - if (!l) - return NULL; - - su = l->data; - service = search_service(su->name); - if (!service) - return NULL; - - return service; -} - -void register_uuids(const char *ident, const char **uuids) -{ - struct service_uuids *su; - int i; - - if (!ident) - return; - - su = g_new0(struct service_uuids, 1); - su->name = g_strdup(ident); - - for (i = 0; uuids[i]; i++); - - su->uuids = g_new0(char *, i + 1); - - for (i = 0; uuids[i]; i++) - su->uuids[i] = g_strdup(uuids[i]); - - services_uuids = g_slist_append(services_uuids, su); -} - -static void service_uuids_free(struct service_uuids *su) -{ - int i; - - if (!su) - return; - - g_free(su->name); - - for (i = 0; su->uuids[i]; i++) - g_free(su->uuids[i]); - - g_free(su); -} - -void unregister_uuids(const char *ident) -{ - struct service_uuids *su; - GSList *l; - - if (!services_uuids) - return; - - l = g_slist_find_custom(services_uuids, ident, (GCompareFunc) name_cmp); - if (!l) - return; - - su = l->data; - services_uuids = g_slist_remove(services_uuids, su); - - service_uuids_free(su); + unregister_uuids(ident); } static struct adapter *ba2adapter(bdaddr_t *src) diff --git a/hcid/dbus-service.h b/hcid/dbus-service.h index c96179ea4..8923907a8 100644 --- a/hcid/dbus-service.h +++ b/hcid/dbus-service.h @@ -38,12 +38,9 @@ struct service *search_service_by_uuid(const char *uuid); int service_unregister(DBusConnection *conn, struct service *service); -int register_service(const char *ident); +int register_service(const char *ident, const char **uuids); void unregister_service(const char *ident); -void register_uuids(const char *ident, const char **uuids); -void unregister_uuids(const char *ident); - typedef void (*service_auth_cb) (DBusError *derr, void *user_data); int service_req_auth(bdaddr_t *src, bdaddr_t *dst, const char *uuid, service_auth_cb cb, void *user_data); diff --git a/input/main.c b/input/main.c index a98bc72fe..c9432c217 100644 --- a/input/main.c +++ b/input/main.c @@ -55,17 +55,13 @@ static int input_init(void) return -EIO; } - register_service("input"); - - register_uuids("input", uuids); + register_service("input", uuids); return 0; } static void input_exit(void) { - unregister_uuids("input"); - unregister_service("input"); input_manager_exit(); diff --git a/network/main.c b/network/main.c index 54bdf3d61..c84ba59b5 100644 --- a/network/main.c +++ b/network/main.c @@ -180,17 +180,13 @@ static int network_init(void) return -EIO; } - register_service("network"); - - register_uuids("network", uuids); + register_service("network", uuids); return 0; } static void network_exit(void) { - unregister_uuids("network"); - unregister_service("network"); network_manager_exit(); diff --git a/serial/main.c b/serial/main.c index 0916f7607..57a65f88c 100644 --- a/serial/main.c +++ b/serial/main.c @@ -50,7 +50,7 @@ static int serial_init(void) return -EIO; } - register_service("serial"); + register_service("serial", NULL); return 0; }