public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
From: Filippo Giunchedi <filippo@esaurito.net>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH 2/2] test/agent.c: pairing via CreatePairedDevice
Date: Tue, 30 Jun 2009 18:32:37 +0200	[thread overview]
Message-ID: <1246379557-16538-2-git-send-email-filippo@esaurito.net> (raw)
In-Reply-To: <1246379557-16538-1-git-send-email-filippo@esaurito.net>

introduce optional argument target for remote address, if passed pairing
will be called
---
 test/agent.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/test/agent.c b/test/agent.c
index b68c748..cd3f6c5 100644
--- a/test/agent.c
+++ b/test/agent.c
@@ -362,6 +362,39 @@ static int unregister_agent(DBusConnection *conn, const char *device_path,
 	return 0;
 }
 
+static int create_paired_device(DBusConnection *conn, const char *device_path,
+						const char *agent_path,
+						const char *capabilities, const char *target)
+{
+	dbus_bool_t success;
+	DBusMessage *msg;
+
+	msg = dbus_message_new_method_call("org.bluez", device_path,
+					"org.bluez.Adapter", "CreatePairedDevice");
+	if (!msg) {
+		fprintf(stderr, "Can't allocate new method call\n");
+		return -1;
+	}
+
+	dbus_message_append_args(msg, DBUS_TYPE_STRING, &target,
+					DBUS_TYPE_OBJECT_PATH, &agent_path,
+					DBUS_TYPE_STRING, &capabilities,
+							DBUS_TYPE_INVALID);
+
+	success = dbus_connection_send(conn, msg, NULL);
+
+	dbus_message_unref(msg);
+
+	if (!success) {
+		fprintf(stderr, "Not enough memory for message send\n");
+		return -1;
+	}
+
+	dbus_connection_flush(conn);
+
+	return 0;
+}
+
 static char *get_device(DBusConnection *conn, const char *device)
 {
 	DBusMessage *msg, *reply;
@@ -421,7 +454,7 @@ static void usage(void)
 	printf("Bluetooth agent ver %s\n\n", VERSION);
 
 	printf("Usage:\n"
-		"\tagent [--device interface] [--path agent-path] <passkey>\n"
+		"\tagent [--device interface] [--path agent-path] <passkey> [<target_device>]\n"
 		"\n");
 }
 
@@ -440,7 +473,7 @@ int main(int argc, char *argv[])
 	struct sigaction sa;
 	DBusConnection *conn;
 	char match_string[128], default_path[128], *device_id = NULL;
-	char *device_path = NULL, *agent_path = NULL;
+	char *device_path = NULL, *agent_path = NULL, *target = NULL;
 	int opt;
 
 	snprintf(default_path, sizeof(default_path),
@@ -483,6 +516,9 @@ int main(int argc, char *argv[])
 
 	passkey = strdup(argv[0]);
 
+	if (argc > 1)
+		target = strdup(argv[1]);
+
 	if (!agent_path)
 		agent_path = strdup(default_path);
 
@@ -495,9 +531,17 @@ int main(int argc, char *argv[])
 	if (!device_path)
 		device_path = get_device(conn, device_id);
 
-	if (register_agent(conn, device_path, agent_path, capabilities) < 0) {
-		dbus_connection_unref(conn);
-		exit(1);
+	if (!target) {
+		if (register_agent(conn, device_path, agent_path, capabilities) < 0) {
+			dbus_connection_unref(conn);
+			exit(1);
+		}
+	} else {
+		if (create_paired_device(conn, device_path, agent_path,
+						capabilities, target) < 0) {
+			dbus_connection_unref(conn);
+			exit(1);
+		}
 	}
 
 	if (!dbus_connection_add_filter(conn, agent_filter, NULL, NULL))
@@ -520,8 +564,10 @@ int main(int argc, char *argv[])
 			break;
 	}
 
-	if (!__io_terminated)
-		unregister_agent(conn, device_path, agent_path);
+	if (!__io_terminated) {
+		if (!target)
+			unregister_agent(conn, device_path, agent_path);
+	}
 
 	free(device_path);
 	free(agent_path);
-- 
1.6.3.3


  reply	other threads:[~2009-06-30 16:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-30 16:32 [PATCH 1/2] test/agent.c: update dbus API usage Filippo Giunchedi
2009-06-30 16:32 ` Filippo Giunchedi [this message]
2009-06-30 17:03   ` [PATCH 2/2] test/agent.c: pairing via CreatePairedDevice Johan Hedberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1246379557-16538-2-git-send-email-filippo@esaurito.net \
    --to=filippo@esaurito.net \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox