From 50118e6f2267e5926fdb28897ffc751d496ec5b4 Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Wed, 31 Oct 2012 12:22:14 +0100 Subject: [PATCH] agent: Add __near_agent_handover_push_data function --- include/dbus.h | 17 ++++++++------- src/agent.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/near.h | 8 +++++++ 3 files changed, 85 insertions(+), 8 deletions(-) diff --git a/include/dbus.h b/include/dbus.h index e5c74af..245152a 100644 --- a/include/dbus.h +++ b/include/dbus.h @@ -24,16 +24,17 @@ #define NFC_SERVICE "org.neard" #define NFC_PATH "/org/neard" -#define NFC_ERROR_INTERFACE NFC_SERVICE ".Error" -#define NFC_NDEF_AGENT_INTERFACE NFC_SERVICE ".NDEFAgent" +#define NFC_ERROR_INTERFACE NFC_SERVICE ".Error" +#define NFC_NDEF_AGENT_INTERFACE NFC_SERVICE ".NDEFAgent" +#define NFC_HANDOVER_AGENT_INTERFACE NFC_SERVICE ".HandoverAgent" -#define NFC_MANAGER_INTERFACE NFC_SERVICE ".Manager" -#define NFC_MANAGER_PATH "/" +#define NFC_MANAGER_INTERFACE NFC_SERVICE ".Manager" +#define NFC_MANAGER_PATH "/" -#define NFC_ADAPTER_INTERFACE NFC_SERVICE ".Adapter" -#define NFC_DEVICE_INTERFACE NFC_SERVICE ".Device" -#define NFC_TAG_INTERFACE NFC_SERVICE ".Tag" -#define NFC_RECORD_INTERFACE NFC_SERVICE ".Record" +#define NFC_ADAPTER_INTERFACE NFC_SERVICE ".Adapter" +#define NFC_DEVICE_INTERFACE NFC_SERVICE ".Device" +#define NFC_TAG_INTERFACE NFC_SERVICE ".Tag" +#define NFC_RECORD_INTERFACE NFC_SERVICE ".Record" typedef void (* near_dbus_append_cb_t) (DBusMessageIter *iter, void *user_data); diff --git a/src/agent.c b/src/agent.c index 0f99d1b..42acb93 100644 --- a/src/agent.c +++ b/src/agent.c @@ -222,6 +222,74 @@ static guint handover_agent_watch = 0; static gchar *handover_agent_path = NULL; static gchar *handover_agent_sender = NULL; +static void prepare_bt_data(DBusMessage *message, struct bt_data *data) +{ + DBusMessageIter iter; + DBusMessageIter dict; + char *name; + + DBG("data %p", data); + + dbus_message_iter_init_append(message, &iter); + + near_dbus_dict_open(&iter, &dict); + + if (data != NULL) { + void *pdata = data->data; + + if (data->type == BT_MIME_V2_1) + name = "EIR"; + else + name = "nokia.com:bt"; + + near_dbus_dict_append_fixed_array(&dict, name, DBUS_TYPE_BYTE, + &pdata, data->size); + } + + near_dbus_dict_close(&iter, &dict); +} + +int __near_agent_handover_push_data(struct bt_data *data) +{ + DBusMessage *message; + DBusMessage *reply; + DBusError error; + + DBG("agent %s", handover_agent_path ? : "not present"); + + if (handover_agent_path == NULL) + return -ESRCH; + + message = dbus_message_new_method_call(handover_agent_sender, + handover_agent_path, NFC_HANDOVER_AGENT_INTERFACE, + "PushOOB"); + if (message == NULL) + return -ENOMEM; + + prepare_bt_data(message, data); + + dbus_error_init(&error); + + reply = dbus_connection_send_with_reply_and_block(connection, message, + DBUS_TIMEOUT_USE_DEFAULT, &error); + + dbus_message_unref(message); + + if (reply != NULL) { + dbus_message_unref(reply); + return 0; + } + + if (dbus_error_is_set(&error) == TRUE) { + near_error("PushOOB failed: %s", error.message); + dbus_error_free(&error); + } else { + near_error("PushOOB failed"); + } + + return -EIO; +} + static void handover_agent_free(void) { if (handover_agent_watch > 0) { diff --git a/src/near.h b/src/near.h index 94d011d..5316a05 100644 --- a/src/near.h +++ b/src/near.h @@ -178,6 +178,12 @@ void __near_plugin_cleanup(void); #define OOB_PROPS_COD 0x08 #define OOB_PROPS_SP (OOB_PROPS_SP_HASH | OOB_PROPS_SP_RANDOM) +struct bt_data { + uint8_t type; + uint8_t size; + uint8_t data[UINT8_MAX]; +}; + int __near_bluetooth_init(void); void __near_bluetooth_cleanup(void); int __near_bluetooth_parse_oob_record(uint8_t version, uint8_t *bt_data, @@ -194,5 +200,7 @@ int __near_agent_ndef_unregister(const char *sender, const char *path, int __near_agent_handover_register(const char *sender, const char *path); int __near_agent_handover_unregister(const char *sender, const char *path); +int __near_agent_handover_push_data(struct bt_data *data); + int __near_agent_init(void); void __near_agent_cleanup(void); -- 2.7.4