diff options
-rw-r--r-- | hcid/Makefile.am | 6 | ||||
-rw-r--r-- | hcid/adapter.c | 23 | ||||
-rw-r--r-- | hcid/dbus-common.c | 6 | ||||
-rw-r--r-- | hcid/device.c | 88 | ||||
-rw-r--r-- | hcid/device.h | 32 |
5 files changed, 148 insertions, 7 deletions
diff --git a/hcid/Makefile.am b/hcid/Makefile.am index 399a2e87..4967c25a 100644 --- a/hcid/Makefile.am +++ b/hcid/Makefile.am @@ -13,10 +13,10 @@ endif noinst_LIBRARIES = libhciserver.a -libhciserver_a_SOURCES = hcid.h security.c device.c \ - storage.c parser.h parser.y lexer.l kword.c kword.h \ +libhciserver_a_SOURCES = hcid.h security.c storage.c \ + parser.h parser.y lexer.l kword.c kword.h \ server.h server.c manager.h manager.c \ - adapter.h adapter.c \ + adapter.h adapter.c device.h device.c \ dbus-common.c dbus-common.h dbus-error.c dbus-error.h \ dbus-database.c dbus-database.h dbus-security.c dbus-security.h \ dbus-service.c dbus-service.h dbus-test.c dbus-test.h \ diff --git a/hcid/adapter.c b/hcid/adapter.c index 70bac90f..16db0ba7 100644 --- a/hcid/adapter.c +++ b/hcid/adapter.c @@ -49,6 +49,7 @@ #include "hcid.h" #include "dbus.h" +#include "device.h" #include "textfile.h" #include "oui.h" @@ -3099,13 +3100,19 @@ static DBusHandlerResult adapter_list_trusts(DBusConnection *conn, return send_message_and_unref(conn, reply); } +static void do_append_device(struct device_data *device, DBusMessageIter *iter) +{ + const char *path = device->path; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &path); +} + static DBusHandlerResult list_devices(DBusConnection *conn, DBusMessage *msg, void *data) { DBusMessage *reply; DBusMessageIter iter; DBusMessageIter array_iter; - //const char *path = "/org/bluez/device"; if (!dbus_message_has_signature(msg, DBUS_TYPE_INVALID_AS_STRING)) return error_invalid_arguments(conn, msg); @@ -3118,7 +3125,7 @@ static DBusHandlerResult list_devices(DBusConnection *conn, dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &array_iter); - //dbus_message_iter_append_basic(&array_iter, DBUS_TYPE_STRING, &path); + device_foreach((GFunc) do_append_device, &array_iter); dbus_message_iter_close_container(&iter, &array_iter); @@ -3128,8 +3135,10 @@ static DBusHandlerResult list_devices(DBusConnection *conn, static DBusHandlerResult create_device(DBusConnection *conn, DBusMessage *msg, void *data) { + struct adapter *adapter = data; + struct device_data *device; DBusMessage *reply; - const char *address, *path = "/org/bluez/device"; + const char *address, *path; if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &address, DBUS_TYPE_INVALID) == FALSE) @@ -3142,8 +3151,12 @@ static DBusHandlerResult create_device(DBusConnection *conn, if (!reply) return DBUS_HANDLER_RESULT_NEED_MEMORY; + device = device_create(adapter->address, address); + + path = device->path; + dbus_message_append_args(reply, DBUS_TYPE_STRING, &path, - DBUS_TYPE_INVALID); + DBUS_TYPE_INVALID); return send_message_and_unref(conn, reply); } @@ -3162,6 +3175,8 @@ static DBusHandlerResult remove_device(DBusConnection *conn, if (!reply) return DBUS_HANDLER_RESULT_NEED_MEMORY; + device_remove(path); + return send_message_and_unref(conn, reply); } diff --git a/hcid/dbus-common.c b/hcid/dbus-common.c index 4caef1ef..4cbc42aa 100644 --- a/hcid/dbus-common.c +++ b/hcid/dbus-common.c @@ -54,6 +54,7 @@ #include "dbus-error.h" #include "manager.h" #include "adapter.h" +#include "device.h" #include "dbus-hci.h" #include "dbus-service.h" #include "dbus-database.h" @@ -294,6 +295,8 @@ void hcid_dbus_exit(void) release_default_auth_agent(); release_services(conn); + device_cleanup(); + /* Unregister all paths in Adapter path hierarchy */ if (!dbus_connection_list_registered(conn, BASE_PATH, &children)) goto done; @@ -337,6 +340,9 @@ int hcid_dbus_init(void) if (!security_init(conn, BASE_PATH)) return -1; + if (device_init(conn) == FALSE) + return -1; + set_dbus_connection(conn); return 0; diff --git a/hcid/device.c b/hcid/device.c index e7e3fbda..3c4f960d 100644 --- a/hcid/device.c +++ b/hcid/device.c @@ -38,11 +38,20 @@ #include <bluetooth/hci.h> #include <bluetooth/hci_lib.h> +#include <glib.h> + +#include <dbus/dbus.h> + +#include "dbus-helper.h" + #include "hcid.h" +#include "logging.h" #include "textfile.h" #include "oui.h" +#include "device.h" + #define MAX_DEVICES 16 struct hci_peer { @@ -660,3 +669,82 @@ int get_encryption_key_size(uint16_t dev_id, const bdaddr_t *baddr) return size; } + +static DBusConnection *connection = NULL; + +static GSList *device_list = NULL; + +gboolean device_init(DBusConnection *conn) +{ + connection = dbus_connection_ref(conn); + if (connection == NULL) + return FALSE; + + return TRUE; +} + +static void device_destroy(struct device_data *device) +{ + debug("Removing device %s", device->path); + + dbus_connection_destroy_object_path(connection, device->path); +} + +void device_cleanup(void) +{ + g_slist_foreach(device_list, (GFunc) device_destroy, NULL); + g_slist_free(device_list); + + if (connection == NULL) + return; + + dbus_connection_unref(connection); +} + +void device_foreach(GFunc func, gpointer user_data) +{ + g_slist_foreach(device_list, func, user_data); +} + +static void device_free(struct device_data *device) +{ + g_free(device->path); + g_free(device); +} + +static void device_unregister(DBusConnection *conn, void *user_data) +{ + struct device_data *device = user_data; + + device_list = g_slist_remove(device_list, device); + + device_free(device); +} + +struct device_data *device_create(const char *adapter, const char *address) +{ + struct device_data *device; + + device = g_try_malloc0(sizeof(struct device_data)); + if (device == NULL) + return NULL; + + device->path = g_strdup_printf("/device/%s_%s", adapter, address); + g_strdelimit(device->path, ":", '_'); + + debug("Creating device %s", device->path); + + if (dbus_connection_create_object_path(connection, device->path, + device, device_unregister) == FALSE) { + device_free(device); + return NULL; + } + + device_list = g_slist_append(device_list, device); + + return device; +} + +void device_remove(const char *path) +{ +} diff --git a/hcid/device.h b/hcid/device.h new file mode 100644 index 00000000..73c2b718 --- /dev/null +++ b/hcid/device.h @@ -0,0 +1,32 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org> + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +struct device_data { + char *path; +}; + +gboolean device_init(DBusConnection *conn); +void device_cleanup(void); +void device_foreach(GFunc func, gpointer user_data); +struct device_data *device_create(const char *adapter, const char *address); +void device_remove(const char *path); |