summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hcid/dbus-common.c33
-rw-r--r--hcid/dbus-common.h5
-rw-r--r--hcid/dbus-manager.c47
-rw-r--r--hcid/dbus-service.c49
-rw-r--r--hcid/main.c2
5 files changed, 57 insertions, 79 deletions
diff --git a/hcid/dbus-common.c b/hcid/dbus-common.c
index 5d6976b5..d21327a9 100644
--- a/hcid/dbus-common.c
+++ b/hcid/dbus-common.c
@@ -67,6 +67,8 @@
static DBusConnection *conn = NULL;
+static sdp_session_t *sess = NULL;
+
static int experimental = 0;
service_handler_func_t find_service_handler(struct service_data *handlers, DBusMessage *msg)
@@ -367,3 +369,34 @@ int hcid_dbus_init(void)
return 0;
}
+
+int register_sdp_record(uint8_t *data, uint32_t size, uint32_t *handle)
+{
+ if (!sess) {
+ sess = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
+ if (!sess) {
+ error("Can't connect to SDP daemon:(%s, %d)",
+ strerror(errno), errno);
+ return -1;
+ }
+ }
+
+ return sdp_device_record_register_binary(sess, BDADDR_ANY,
+ data, size, 0, handle);
+}
+
+int unregister_sdp_record(uint32_t handle)
+{
+ if (!sess)
+ return -ENOENT;
+
+ return sdp_device_record_unregister_binary(sess, BDADDR_ANY, handle);
+}
+
+void cleanup_sdp_session(void)
+{
+ if (sess)
+ sdp_close(sess);
+
+ sess = NULL;
+}
diff --git a/hcid/dbus-common.h b/hcid/dbus-common.h
index b9065982..f84bd48b 100644
--- a/hcid/dbus-common.h
+++ b/hcid/dbus-common.h
@@ -52,7 +52,10 @@ int check_address(const char *addr);
DBusHandlerResult handle_method_call(DBusConnection *conn, DBusMessage *msg, void *data);
void hcid_dbus_exit(void);
-
int hcid_dbus_init(void);
+int register_sdp_record(uint8_t *data, uint32_t size, uint32_t *handle);
+int unregister_sdp_record(uint32_t handle);
+void cleanup_sdp_session(void);
+
#endif /* __BLUEZ_DBUS_COMMON_H */
diff --git a/hcid/dbus-manager.c b/hcid/dbus-manager.c
index 216b1102..b3e79150 100644
--- a/hcid/dbus-manager.c
+++ b/hcid/dbus-manager.c
@@ -34,8 +34,6 @@
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
-#include <bluetooth/sdp.h>
-#include <bluetooth/sdp_lib.h>
#include <dbus/dbus.h>
@@ -417,7 +415,7 @@ static DBusHandlerResult add_service_record(DBusConnection *conn,
dbus_message_iter_init(msg, &iter);
dbus_message_iter_get_basic(&iter, &path);
- if(!dbus_connection_get_object_path_data(conn, path,
+ if (!dbus_connection_get_object_path_data(conn, path,
(void *) &agent)) {
/* If failed the path is invalid! */
return error_invalid_arguments(conn, msg);
@@ -448,32 +446,18 @@ static DBusHandlerResult add_service_record(DBusConnection *conn,
rec->ext_handle = next_handle++;
if (agent->running) {
- sdp_session_t *sess;
uint32_t handle = 0;
- sess = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
- if (!sess) {
+ if (register_sdp_record(rec->buf->data, rec->buf->data_size, &handle) < 0) {
err = errno;
- error("Can't connect to sdp daemon: %s (%d)",
- strerror(err), err);
- goto fail;
- }
-
- if (sdp_device_record_register_binary(sess, BDADDR_ANY, rec->buf->data,
- rec->buf->data_size, SDP_RECORD_PERSIST, &handle) < 0) {
- err = errno;
- sdp_close(sess);
- error("Record registration failed: %s (%d)",
- strerror(err), err);
+ error("Service record registration failed: %s (%d)",
+ strerror(err), err);
goto fail;
}
rec->handle = handle;
-
- sdp_close(sess);
}
-
agent->records = slist_append(agent->records, rec);
dbus_message_append_args(reply,
@@ -481,9 +465,11 @@ static DBusHandlerResult add_service_record(DBusConnection *conn,
DBUS_TYPE_INVALID);
return send_message_and_unref(conn, reply);
+
fail:
binary_record_free(rec);
dbus_message_unref(reply);
+
return error_failed(conn, msg, err);
}
@@ -503,7 +489,7 @@ static DBusHandlerResult remove_service_record(DBusConnection *conn,
DBUS_TYPE_INVALID))
return error_invalid_arguments(conn, msg);
- if(!dbus_connection_get_object_path_data(conn, path,
+ if (!dbus_connection_get_object_path_data(conn, path,
(void *) &agent)) {
/* If failed the path is invalid! */
return error_invalid_arguments(conn, msg);
@@ -526,25 +512,12 @@ static DBusHandlerResult remove_service_record(DBusConnection *conn,
/* If the service agent is running: remove it from the from sdpd */
if (agent->running && rec->handle != 0xffffffff) {
- sdp_session_t *sess;
-
- /* FIXME: attach to a specific adapter */
- sess = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
- if (!sess) {
- error("Can't connect to sdp daemon:(%s, %d)",
- strerror(errno), errno);
- goto fail;
+ if (unregister_sdp_record(rec->handle) < 0) {
+ error("Service record unregistration failed: %s (%d)",
+ strerror(errno), errno);
}
-
- if (sdp_device_record_unregister_binary(sess, BDADDR_ANY,
- rec->handle) < 0) {
- error("Service Record unregistration failed:(%s, %d)",
- strerror(errno), errno);
- }
- sdp_close(sess);
}
-fail:
binary_record_free(rec);
return send_message_and_unref(conn, reply);
diff --git a/hcid/dbus-service.c b/hcid/dbus-service.c
index 208dd160..324ac681 100644
--- a/hcid/dbus-service.c
+++ b/hcid/dbus-service.c
@@ -30,9 +30,6 @@
#include <unistd.h>
#include <stdlib.h>
-#include <bluetooth/sdp.h>
-#include <bluetooth/sdp_lib.h>
-
#include <dbus/dbus.h>
#include "hcid.h"
@@ -43,7 +40,6 @@
#include "dbus-manager.h"
#include "dbus-service.h"
-
static struct slist *services = NULL;
struct binary_record *binary_record_new()
@@ -208,73 +204,44 @@ mem_fail:
int register_agent_records(struct slist *lrecords)
{
- sdp_session_t *sess;
- struct binary_record *rec;
- uint32_t handle;
- int err;
-
- /* FIXME: attach to a specific adapter */
- sess = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
- if (!sess) {
- error("Can't connect to sdp daemon:(%s, %d)", strerror(errno), errno);
- return -1;
- }
-
while (lrecords) {
- rec = lrecords->data;
+ struct binary_record *rec = lrecords->data;
lrecords = lrecords->next;
+ uint32_t handle = 0;
if (!rec || !rec->buf || rec->handle != 0xffffffff)
continue;
- handle = 0;
- if (sdp_device_record_register_binary(sess, BDADDR_ANY, rec->buf->data,
- rec->buf->data_size, SDP_RECORD_PERSIST, &handle) < 0) {
+ if (register_sdp_record(rec->buf->data, rec->buf->data_size, &handle) < 0) {
/* FIXME: If just one of the service record registration fails */
error("Service Record registration failed:(%s, %d)",
strerror(errno), errno);
}
+
rec->handle = handle;
}
- err = errno;
- sdp_close(sess);
- errno = err;
-
return 0;
}
static int unregister_agent_records(struct slist *lrecords)
{
- sdp_session_t *sess;
- struct binary_record *rec;
- int err;
-
- /* FIXME: attach to a specific adapter */
- sess = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
- if (!sess) {
- error("Can't connect to sdp daemon:(%s, %d)", strerror(errno), errno);
- return -1;
- }
-
while (lrecords) {
- rec = lrecords->data;
+ struct binary_record *rec = lrecords->data;
lrecords = lrecords->next;
+
if (!rec || rec->handle == 0xffffffff)
continue;
- if (sdp_device_record_unregister_binary(sess, BDADDR_ANY, rec->handle) < 0) {
+ if (unregister_sdp_record(rec->handle) < 0) {
/* FIXME: If just one of the service record registration fails */
error("Service Record unregistration failed:(%s, %d)",
strerror(errno), errno);
}
+
rec->handle = 0xffffffff;
}
- err = errno;
- sdp_close(sess);
- errno = err;
-
return 0;
}
diff --git a/hcid/main.c b/hcid/main.c
index ce3c527e..8abd3d40 100644
--- a/hcid/main.c
+++ b/hcid/main.c
@@ -741,6 +741,8 @@ int main(int argc, char *argv[])
hcid_dbus_exit();
+ cleanup_sdp_session();
+
g_main_unref(event_loop);
g_io_channel_unref(ctl_io);