diff options
Diffstat (limited to 'hcid/dbus-service.c')
-rw-r--r-- | hcid/dbus-service.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/hcid/dbus-service.c b/hcid/dbus-service.c index be41b22e..c446a6e4 100644 --- a/hcid/dbus-service.c +++ b/hcid/dbus-service.c @@ -36,6 +36,9 @@ #include <sys/types.h> #include <sys/wait.h> +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> #include <bluetooth/sdp.h> #include <glib.h> @@ -52,6 +55,8 @@ #include "error.h" #include "manager.h" #include "adapter.h" +#include "agent.h" +#include "device.h" #include "dbus-service.h" #include "dbus-hci.h" @@ -70,6 +75,11 @@ struct service_uuids { char **uuids; }; +struct service_auth { + service_auth_cb cb; + void *user_data; +}; + static GSList *services = NULL; static GSList *services_uuids = NULL; static GSList *removed = NULL; @@ -1196,3 +1206,101 @@ void unregister_uuids(const char *name) service_uuids_free(su); } + +static struct adapter *ba2adapter(bdaddr_t *src) +{ + DBusConnection *conn = get_dbus_connection(); + struct adapter *adapter = NULL; + char address[18], path[6]; + int dev_id; + + ba2str(src, address); + dev_id = hci_devid(address); + if (dev_id < 0) + return NULL; + + /* FIXME: id2adapter? Create a list of adapters? */ + snprintf(path, sizeof(path), "/hci%d", dev_id); + if (dbus_connection_get_object_user_data(conn, + path, (void *) &adapter) == FALSE) + return NULL; + + return adapter; +} + +static void agent_auth_cb(struct agent *agent, DBusError *derr, void *user_data) +{ + struct service_auth *auth = user_data; + + auth->cb(derr, auth->user_data); + + g_free(auth); +} +int service_req_auth(bdaddr_t *src, bdaddr_t *dst, + const char *uuid, service_auth_cb cb, void *user_data) +{ + struct service_auth *auth; + struct adapter *adapter; + struct device *device; + struct agent *agent; + char address[18]; + + adapter = ba2adapter(src); + if (!adapter) + return -EPERM; + + /* Device connected? */ + if (!g_slist_find_custom(adapter->active_conn, + dst, active_conn_find_by_bdaddr)) + return -ENOTCONN; + + ba2str(dst, address); + device = adapter_find_device(adapter, address); + if (!device) + return -EPERM; + + if (!search_service_by_uuid(uuid)) + return -EPERM; + + /* FIXME: Missing check trusted file entries */ + + agent = (device->agent ? : adapter->agent); + if (!agent) + return -EPERM; + + auth = g_try_new0(struct service_auth, 1); + if (!auth) + return -ENOMEM; + + auth->cb = cb; + auth->user_data = user_data; + + return agent_authorize(agent, device->path, uuid, agent_auth_cb, auth); +} + +int service_cancel_auth(bdaddr_t *src) +{ + struct adapter *adapter = ba2adapter(src); + struct device *device; + struct agent *agent; + char address[18]; + + if (!adapter) + return -EPERM; + + ba2str(src, address); + device = adapter_find_device(adapter, address); + if (!device) + return -EPERM; + + /* + * FIXME: Cancel fails if authorization is requested to adapter's + * agent and in the meanwhile CreatePairedDevice is called. + */ + + agent = (device->agent ? : adapter->agent); + if (!agent) + return -EPERM; + + return agent_cancel(agent); +} |