2008-11-25 16:04:34

by Alan Carvalho de Assis

[permalink] [raw]
Subject: agent.c

Hi list,

I am updating agent.c in order to it works with new bluez versions.

Everything appears to work, but after "hcitool cc ..." my agent is not
called (at least Methods on agent_message are not called).

Any tests and help are welcome!

Best Regards,

Alan

/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2008 Marcel Holtmann <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <getopt.h>
#include <string.h>

#include <dbus/dbus.h>

static char *passkey = NULL;

static int do_reject = 0;

static volatile sig_atomic_t __io_canceled = 0;
static volatile sig_atomic_t __io_terminated = 0;

static void sig_term(int sig)
{
__io_canceled = 1;
}

static DBusHandlerResult agent_filter(DBusConnection *conn,
DBusMessage *msg, void *data)
{
const char *name, *old, *new;

if (!dbus_message_is_signal(msg, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

if (!dbus_message_get_args(msg, NULL,
DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old,
DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
fprintf(stderr, "Invalid arguments for NameOwnerChanged signal");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

if (!strcmp(name, "org.bluez") && *new == '\0') {
fprintf(stderr, "Agent has been terminated\n");
__io_terminated = 1;
}

return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusHandlerResult request_message(DBusConnection *conn,
DBusMessage *msg, void *data)
{
DBusMessage *reply;
const char *path, *address;
dbus_bool_t numeric;

if (!passkey)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

if (!dbus_message_get_args(msg, NULL,
DBUS_TYPE_STRING, &path, DBUS_TYPE_STRING, &address,
DBUS_TYPE_BOOLEAN, &numeric, DBUS_TYPE_INVALID)) {
fprintf(stderr, "Invalid arguments for passkey Request method");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

if (do_reject) {
reply = dbus_message_new_error(msg,
"org.bluez.Error.Rejected", "");
goto send;
}

reply = dbus_message_new_method_return(msg);
if (!reply) {
fprintf(stderr, "Can't create reply message\n");
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}

printf("Passkey request for device %s\n", address);

dbus_message_append_args(reply, DBUS_TYPE_STRING, &passkey,
DBUS_TYPE_INVALID);

send:
dbus_connection_send(conn, reply, NULL);

dbus_connection_flush(conn);

dbus_message_unref(reply);

return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusHandlerResult cancel_message(DBusConnection *conn,
DBusMessage *msg, void *data)
{
DBusMessage *reply;
const char *path, *address;

if (!dbus_message_get_args(msg, NULL,
DBUS_TYPE_STRING, &path, DBUS_TYPE_STRING, &address,
DBUS_TYPE_INVALID)) {
fprintf(stderr, "Invalid arguments for passkey Confirm method");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

printf("Request canceled for device %s\n", address);

reply = dbus_message_new_method_return(msg);
if (!reply) {
fprintf(stderr, "Can't create reply message\n");
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}

dbus_connection_send(conn, reply, NULL);

dbus_connection_flush(conn);

dbus_message_unref(reply);

return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusHandlerResult release_message(DBusConnection *conn,
DBusMessage *msg, void *data)
{
DBusMessage *reply;

if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID)) {
fprintf(stderr, "Invalid arguments for Release method");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

if (!__io_canceled)
fprintf(stderr, "Agent has been released\n");

__io_terminated = 1;

reply = dbus_message_new_method_return(msg);
if (!reply) {
fprintf(stderr, "Can't create reply message\n");
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}

dbus_connection_send(conn, reply, NULL);

dbus_connection_flush(conn);

dbus_message_unref(reply);

return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusHandlerResult agent_message(DBusConnection *conn,
DBusMessage *msg, void *data)
{
printf("Agent_message was called!\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Release"))
printf("Agent called: Release\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "RequestPinCode"))
printf("Agent called: RequestPinCode\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "RequestPasskey"))
printf("Agent called: RequestPasskey\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "DisplayPasskey"))
printf("Agent called: DisplayPasskey\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "RequestConfirmation"))
printf("Agent called: RequestConfirmation\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Authorize"))
printf("Agent called: Authorize\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "ConfirmModeChange"))
printf("Agent called: ConfirmModeChange\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Cancel"))
printf("Agent called: Cancel\n");

return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static const DBusObjectPathVTable agent_table = {
.message_function = agent_message,
};

static int register_agent(DBusConnection *conn, const char *device_path,
const char *agent_path,
const char *capabilities)
{
DBusMessage *msg, *reply;
DBusError err;

if (!dbus_connection_register_object_path(conn, agent_path,
&agent_table, NULL)) {
fprintf(stderr, "Can't register object path for agent\n");
return -1;
}

msg = dbus_message_new_method_call("org.bluez", device_path,
"org.bluez.Adapter", "RegisterAgent");
if (!msg) {
fprintf(stderr, "Can't allocate new method call\n");
return -1;
}

dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &agent_path,
DBUS_TYPE_STRING, &capabilities,
DBUS_TYPE_INVALID);

dbus_error_init(&err);

reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);

dbus_message_unref(msg);

if (!reply) {
fprintf(stderr, "Can't register agent\n");
if (dbus_error_is_set(&err)) {
fprintf(stderr, "%s\n", err.message);
dbus_error_free(&err);
}
return -1;
}

dbus_message_unref(reply);

dbus_connection_flush(conn);

return 0;
}

static int unregister_agent(DBusConnection *conn, const char *device_path,
const char *agent_path)
{
DBusMessage *msg, *reply;
DBusError err;

msg = dbus_message_new_method_call("org.bluez", device_path,
"org.bluez.Adapter", "UnregisterAgent");
if (!msg) {
fprintf(stderr, "Can't allocate new method call\n");
return -1;
}

dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &agent_path,
DBUS_TYPE_INVALID);

dbus_error_init(&err);

reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);

dbus_message_unref(msg);

if (!reply) {
fprintf(stderr, "Can't unregister agent\n");
if (dbus_error_is_set(&err)) {
fprintf(stderr, "%s\n", err.message);
dbus_error_free(&err);
}
return -1;
}

dbus_message_unref(reply);

dbus_connection_flush(conn);

dbus_connection_unregister_object_path(conn, agent_path);

return 0;
}

static char *get_device(DBusConnection *conn)
{
char *path, *device_path = NULL;
DBusMessage *msg, *reply;
DBusError err;

msg = dbus_message_new_method_call("org.bluez", "/",
"org.bluez.Manager", "DefaultAdapter");
if (!msg) {
fprintf(stderr, "Can't allocate new method call\n");
return NULL;
}

dbus_error_init(&err);

reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);

dbus_message_unref(msg);

if (!reply) {
fprintf(stderr, "Can't get Adapter path\n");
if (dbus_error_is_set(&err)) {
fprintf(stderr, "%s\n", err.message);
dbus_error_free(&err);
}
return NULL;
}

dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH,
&device_path, DBUS_TYPE_INVALID);

dbus_message_unref(reply);

dbus_connection_flush(conn);

return device_path;
}

static void usage(void)
{
printf("Bluetooth agent ver %s\n\n", "4.18");

printf("Usage:\n"
"\tagent [--device interface] [--path agent-path] <passkey>\n"
"\n");
}

static struct option main_options[] = {
{ "device", 1, 0, 'i' },
{ "path", 1, 0, 'p' },
{ "capabilites",1, 0, 'c' },
{ "reject", 0, 0, 'r' },
{ "help", 0, 0, 'h' },
{ 0, 0, 0, 0 }
};

int main(int argc, char *argv[])
{
const char *capabilities = "DisplayYesNo";
struct sigaction sa;
DBusConnection *conn;
char match_string[128], default_path[128], *device_id = NULL;
char *device_path = NULL, *agent_path = NULL;
int opt;

snprintf(default_path, sizeof(default_path),
"/org/bluez/agent_%d", getpid());

while ((opt = getopt_long(argc, argv, "+i:p:c:rh", main_options,
NULL)) != EOF) {
switch(opt) {
case 'i':
device_id = optarg;
break;
case 'p':
if (optarg[0] != '/') {
fprintf(stderr, "Invalid path\n");
exit(1);
}
agent_path = strdup(optarg);
break;
case 'c':
capabilities = optarg;
break;
case 'r':
do_reject = 1;
break;
case 'h':
usage();
exit(0);
default:
exit(1);
}
}

argc -= optind;
argv += optind;
optind = 0;

if (argc < 1) {
usage();
exit(1);
}

passkey = strdup(argv[0]);

if (!agent_path)
agent_path = strdup(default_path);

conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
if (!conn) {
fprintf(stderr, "Can't get on system bus");
exit(1);
}

if (!device_path)
device_path = get_device(conn);

printf("Adapter path: %s\n", device_path);

if (register_agent(conn, device_path, agent_path, capabilities) < 0) {
dbus_connection_unref(conn);
exit(1);
}

if (!dbus_connection_add_filter(conn, agent_filter, NULL, NULL))
fprintf(stderr, "Can't add signal filter");

snprintf(match_string, sizeof(match_string),
"interface=%s,member=NameOwnerChanged,arg0=%s",
DBUS_INTERFACE_DBUS, "org.bluez");

dbus_bus_add_match(conn, match_string, NULL);

memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_NOCLDSTOP;
sa.sa_handler = sig_term;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);

while (!__io_canceled && !__io_terminated) {
if (dbus_connection_read_write_dispatch(conn, 500) != TRUE)
break;
}

if (!__io_terminated)
unregister_agent(conn, device_path, agent_path);

free(device_path);
free(agent_path);

free(passkey);

dbus_connection_unref(conn);

return 0;
}


2008-11-26 15:19:33

by Alan Carvalho de Assis

[permalink] [raw]
Subject: Re: agent.c

Sorry, now reply to all

On Wed, Nov 26, 2008 at 4:57 AM, Marcel Holtmann <[email protected]> wrote:
> Hi Alan,
>
>> I am updating agent.c in order to it works with new bluez versions.
>
> please send patches and not full *.c source code.
>

Sorry!

Please find it below (I hope my email client don't mess this code)

--- agent_orig.c 2008-10-04 16:32:08.000000000 +0000
+++ agent.c 2008-11-25 13:59:57.000000000 +0000
@@ -177,14 +177,31 @@
static DBusHandlerResult agent_message(DBusConnection *conn,
DBusMessage *msg, void *data)
{
- if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Request"))
- return request_message(conn, msg, data);
-
- if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Cancel"))
- return cancel_message(conn, msg, data);
+ printf("Agent_message was called!\n");

if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Release"))
- return release_message(conn, msg, data);
+ printf("Agent called: Release\n");
+
+ if (dbus_message_is_method_call(msg, "org.bluez.Agent",
"RequestPinCode"))
+ printf("Agent called: RequestPinCode\n");
+
+ if (dbus_message_is_method_call(msg, "org.bluez.Agent",
"RequestPasskey"))
+ printf("Agent called: RequestPasskey\n");
+
+ if (dbus_message_is_method_call(msg, "org.bluez.Agent",
"DisplayPasskey"))
+ printf("Agent called: DisplayPasskey\n");
+
+ if (dbus_message_is_method_call(msg, "org.bluez.Agent",
"RequestConfirmation"))
+ printf("Agent called: RequestConfirmation\n");
+
+ if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Authorize"))
+ printf("Agent called: Authorize\n");
+
+ if (dbus_message_is_method_call(msg, "org.bluez.Agent",
"ConfirmModeChange"))
+ printf("Agent called: ConfirmModeChange\n");
+
+ if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Cancel"))
+ printf("Agent called: Cancel\n");

return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
@@ -279,18 +296,46 @@
return 0;
}

-static char *get_device(const char *device)
+static char *get_device(DBusConnection *conn)
{
- char *path;
+ char *path, *device_path = NULL;
+ DBusMessage *msg, *reply;
+ DBusError err;
+
+ msg = dbus_message_new_method_call("org.bluez", "/",
+ "org.bluez.Manager", "DefaultAdapter");
+ if (!msg) {
+ fprintf(stderr, "Can't allocate new method call\n");
+ return NULL;
+ }
+
+ dbus_error_init(&err);

- path = strdup("/org/bluez/hci0");
+ reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);

- return path;
+ dbus_message_unref(msg);
+
+ if (!reply) {
+ fprintf(stderr, "Can't get Adapter path\n");
+ if (dbus_error_is_set(&err)) {
+ fprintf(stderr, "%s\n", err.message);
+ dbus_error_free(&err);
+ }
+ return NULL;
+ }
+
+ dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH,
&device_path, DBUS_TYPE_INVALID);
+
+ dbus_message_unref(reply);
+
+ dbus_connection_flush(conn);
+
+ return device_path;
}

static void usage(void)
{
- printf("Bluetooth agent ver %s\n\n", VERSION);
+ printf("Bluetooth agent ver %s\n\n", "4.18");

printf("Usage:\n"
"\tagent [--device interface] [--path agent-path] <passkey>\n"
@@ -365,7 +410,9 @@
}

if (!device_path)
- device_path = get_device(device_id);
+ device_path = get_device(conn);
+
+ printf("Adapter path: %s\n", device_path);

if (register_agent(conn, device_path, agent_path, capabilities) < 0) {
dbus_connection_unref(conn);


> Regards
>
> Marcel
>
>

Best Regards,

Alan

2008-11-26 08:18:05

by Liu, Raymond

[permalink] [raw]
Subject: Pbap Client code for review

Hi Marcel

I have finished the initial code for PBAP client. Can you help to have a r=
eview? I have to write another get function for PBAP since the current cod=
e does not read application header information

The code is at:
http://git.moblin.org/repos/users/raymond/obexd.git dev

Thanks.
Raymond

2008-11-26 04:57:07

by Marcel Holtmann

[permalink] [raw]
Subject: Re: agent.c

Hi Alan,

> I am updating agent.c in order to it works with new bluez versions.

please send patches and not full *.c source code.

Regards

Marcel