Use permanent connection to SDP server

This commit is contained in:
Marcel Holtmann 2006-11-17 22:36:47 +00:00
parent 3ace2361ad
commit fdd685636f
5 changed files with 57 additions and 79 deletions

View File

@ -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;
}

View File

@ -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 */

View File

@ -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);

View File

@ -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;
}

View File

@ -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);