3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <dbus/dbus.h>
38 static char *passkey = NULL;
40 static int do_reject = 0;
42 static volatile sig_atomic_t __io_canceled = 0;
43 static volatile sig_atomic_t __io_terminated = 0;
45 static void sig_term(int sig)
50 static DBusHandlerResult agent_filter(DBusConnection *conn,
51 DBusMessage *msg, void *data)
53 const char *name, *old, *new;
55 if (!dbus_message_is_signal(msg, DBUS_INTERFACE_DBUS,
57 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
59 if (!dbus_message_get_args(msg, NULL,
60 DBUS_TYPE_STRING, &name,
61 DBUS_TYPE_STRING, &old,
62 DBUS_TYPE_STRING, &new,
65 "Invalid arguments for NameOwnerChanged signal");
66 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
69 if (!strcmp(name, "org.bluez") && *new == '\0') {
70 fprintf(stderr, "Agent has been terminated\n");
74 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
77 static DBusHandlerResult request_pincode_message(DBusConnection *conn,
78 DBusMessage *msg, void *data)
84 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
86 if (!dbus_message_get_args(msg, NULL,
87 DBUS_TYPE_OBJECT_PATH, &path,
89 fprintf(stderr, "Invalid arguments for RequestPinCode method");
90 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
94 reply = dbus_message_new_error(msg,
95 "org.bluez.Error.Rejected", "");
99 reply = dbus_message_new_method_return(msg);
101 fprintf(stderr, "Can't create reply message\n");
102 return DBUS_HANDLER_RESULT_NEED_MEMORY;
105 printf("Pincode request for device %s\n", path);
107 dbus_message_append_args(reply, DBUS_TYPE_STRING, &passkey,
111 dbus_connection_send(conn, reply, NULL);
113 dbus_connection_flush(conn);
115 dbus_message_unref(reply);
117 return DBUS_HANDLER_RESULT_HANDLED;
120 static DBusHandlerResult request_passkey_message(DBusConnection *conn,
121 DBusMessage *msg, void *data)
125 unsigned int int_passkey;
128 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
131 if (!dbus_message_get_args(msg, NULL,
132 DBUS_TYPE_OBJECT_PATH, &path,
133 DBUS_TYPE_INVALID)) {
134 fprintf(stderr, "Invalid arguments for RequestPasskey method");
135 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
139 reply = dbus_message_new_error(msg,
140 "org.bluez.Error.Rejected", "");
144 reply = dbus_message_new_method_return(msg);
146 fprintf(stderr, "Can't create reply message\n");
147 return DBUS_HANDLER_RESULT_NEED_MEMORY;
150 printf("Passkey request for device %s\n", path);
152 int_passkey = strtoul(passkey, NULL, 10);
154 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &int_passkey,
158 dbus_connection_send(conn, reply, NULL);
160 dbus_connection_flush(conn);
162 dbus_message_unref(reply);
164 return DBUS_HANDLER_RESULT_HANDLED;
167 static DBusHandlerResult cancel_message(DBusConnection *conn,
168 DBusMessage *msg, void *data)
172 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID)) {
173 fprintf(stderr, "Invalid arguments for passkey Confirm method");
174 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
177 printf("Request canceled\n");
179 reply = dbus_message_new_method_return(msg);
181 fprintf(stderr, "Can't create reply message\n");
182 return DBUS_HANDLER_RESULT_NEED_MEMORY;
185 dbus_connection_send(conn, reply, NULL);
187 dbus_connection_flush(conn);
189 dbus_message_unref(reply);
191 return DBUS_HANDLER_RESULT_HANDLED;
194 static DBusHandlerResult release_message(DBusConnection *conn,
195 DBusMessage *msg, void *data)
199 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID)) {
200 fprintf(stderr, "Invalid arguments for Release method");
201 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
205 fprintf(stderr, "Agent has been released\n");
209 reply = dbus_message_new_method_return(msg);
211 fprintf(stderr, "Can't create reply message\n");
212 return DBUS_HANDLER_RESULT_NEED_MEMORY;
215 dbus_connection_send(conn, reply, NULL);
217 dbus_connection_flush(conn);
219 dbus_message_unref(reply);
221 return DBUS_HANDLER_RESULT_HANDLED;
224 static DBusHandlerResult authorize_message(DBusConnection *conn,
225 DBusMessage *msg, void *data)
228 const char *path, *uuid;
230 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
231 DBUS_TYPE_STRING, &uuid,
232 DBUS_TYPE_INVALID)) {
233 fprintf(stderr, "Invalid arguments for Authorize method");
234 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
238 reply = dbus_message_new_error(msg,
239 "org.bluez.Error.Rejected", "");
243 reply = dbus_message_new_method_return(msg);
245 fprintf(stderr, "Can't create reply message\n");
246 return DBUS_HANDLER_RESULT_NEED_MEMORY;
249 printf("Authorizing request for %s\n", path);
252 dbus_connection_send(conn, reply, NULL);
254 dbus_connection_flush(conn);
256 dbus_message_unref(reply);
258 return DBUS_HANDLER_RESULT_HANDLED;
261 static DBusHandlerResult agent_message(DBusConnection *conn,
262 DBusMessage *msg, void *data)
264 if (dbus_message_is_method_call(msg, "org.bluez.Agent",
266 return request_pincode_message(conn, msg, data);
268 if (dbus_message_is_method_call(msg, "org.bluez.Agent",
270 return request_passkey_message(conn, msg, data);
272 if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Cancel"))
273 return cancel_message(conn, msg, data);
275 if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Release"))
276 return release_message(conn, msg, data);
278 if (dbus_message_is_method_call(msg, "org.bluez.Agent", "Authorize"))
279 return authorize_message(conn, msg, data);
281 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
284 static const DBusObjectPathVTable agent_table = {
285 .message_function = agent_message,
288 static int register_agent(DBusConnection *conn, const char *adapter_path,
289 const char *agent_path,
290 const char *capabilities)
292 DBusMessage *msg, *reply;
295 if (!dbus_connection_register_object_path(conn, agent_path,
296 &agent_table, NULL)) {
297 fprintf(stderr, "Can't register object path for agent\n");
301 msg = dbus_message_new_method_call("org.bluez", adapter_path,
302 "org.bluez.Adapter", "RegisterAgent");
304 fprintf(stderr, "Can't allocate new method call\n");
308 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &agent_path,
309 DBUS_TYPE_STRING, &capabilities,
312 dbus_error_init(&err);
314 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
316 dbus_message_unref(msg);
319 fprintf(stderr, "Can't register agent\n");
320 if (dbus_error_is_set(&err)) {
321 fprintf(stderr, "%s\n", err.message);
322 dbus_error_free(&err);
327 dbus_message_unref(reply);
329 dbus_connection_flush(conn);
334 static int unregister_agent(DBusConnection *conn, const char *adapter_path,
335 const char *agent_path)
337 DBusMessage *msg, *reply;
340 msg = dbus_message_new_method_call("org.bluez", adapter_path,
341 "org.bluez.Adapter", "UnregisterAgent");
343 fprintf(stderr, "Can't allocate new method call\n");
347 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &agent_path,
350 dbus_error_init(&err);
352 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
354 dbus_message_unref(msg);
357 fprintf(stderr, "Can't unregister agent\n");
358 if (dbus_error_is_set(&err)) {
359 fprintf(stderr, "%s\n", err.message);
360 dbus_error_free(&err);
365 dbus_message_unref(reply);
367 dbus_connection_flush(conn);
369 dbus_connection_unregister_object_path(conn, agent_path);
374 static int create_paired_device(DBusConnection *conn, const char *adapter_path,
375 const char *agent_path,
376 const char *capabilities,
382 msg = dbus_message_new_method_call("org.bluez", adapter_path,
384 "CreatePairedDevice");
386 fprintf(stderr, "Can't allocate new method call\n");
390 dbus_message_append_args(msg, DBUS_TYPE_STRING, &device,
391 DBUS_TYPE_OBJECT_PATH, &agent_path,
392 DBUS_TYPE_STRING, &capabilities,
395 success = dbus_connection_send(conn, msg, NULL);
397 dbus_message_unref(msg);
400 fprintf(stderr, "Not enough memory for message send\n");
404 dbus_connection_flush(conn);
409 static char *get_default_adapter_path(DBusConnection *conn)
411 DBusMessage *msg, *reply;
413 const char *reply_path;
416 msg = dbus_message_new_method_call("org.bluez", "/",
417 "org.bluez.Manager", "DefaultAdapter");
420 fprintf(stderr, "Can't allocate new method call\n");
424 dbus_error_init(&err);
426 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
428 dbus_message_unref(msg);
432 "Can't get default adapter\n");
433 if (dbus_error_is_set(&err)) {
434 fprintf(stderr, "%s\n", err.message);
435 dbus_error_free(&err);
440 if (!dbus_message_get_args(reply, &err,
441 DBUS_TYPE_OBJECT_PATH, &reply_path,
442 DBUS_TYPE_INVALID)) {
444 "Can't get reply arguments\n");
445 if (dbus_error_is_set(&err)) {
446 fprintf(stderr, "%s\n", err.message);
447 dbus_error_free(&err);
452 path = strdup(reply_path);
454 dbus_message_unref(reply);
456 dbus_connection_flush(conn);
461 static char *get_adapter_path(DBusConnection *conn, const char *adapter)
463 DBusMessage *msg, *reply;
465 const char *reply_path;
469 return get_default_adapter_path(conn);
471 msg = dbus_message_new_method_call("org.bluez", "/",
472 "org.bluez.Manager", "FindAdapter");
475 fprintf(stderr, "Can't allocate new method call\n");
479 dbus_message_append_args(msg, DBUS_TYPE_STRING, &adapter,
482 dbus_error_init(&err);
484 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
486 dbus_message_unref(msg);
490 "Can't find adapter %s\n", adapter);
491 if (dbus_error_is_set(&err)) {
492 fprintf(stderr, "%s\n", err.message);
493 dbus_error_free(&err);
498 if (!dbus_message_get_args(reply, &err,
499 DBUS_TYPE_OBJECT_PATH, &reply_path,
500 DBUS_TYPE_INVALID)) {
502 "Can't get reply arguments\n");
503 if (dbus_error_is_set(&err)) {
504 fprintf(stderr, "%s\n", err.message);
505 dbus_error_free(&err);
510 path = strdup(reply_path);
512 dbus_message_unref(reply);
514 dbus_connection_flush(conn);
519 static void usage(void)
521 printf("Bluetooth agent ver %s\n\n", VERSION);
524 "\tagent [--adapter adapter-path] [--path agent-path] <passkey> [<device>]\n"
528 static struct option main_options[] = {
529 { "adapter", 1, 0, 'a' },
530 { "path", 1, 0, 'p' },
531 { "capabilites",1, 0, 'c' },
532 { "reject", 0, 0, 'r' },
533 { "help", 0, 0, 'h' },
537 int main(int argc, char *argv[])
539 const char *capabilities = "DisplayYesNo";
541 DBusConnection *conn;
542 char match_string[128], default_path[128], *adapter_id = NULL;
543 char *adapter_path = NULL, *agent_path = NULL, *device = NULL;
546 snprintf(default_path, sizeof(default_path),
547 "/org/bluez/agent_%d", getpid());
549 while ((opt = getopt_long(argc, argv, "+a:p:c:rh", main_options, NULL)) != EOF) {
555 if (optarg[0] != '/') {
556 fprintf(stderr, "Invalid path\n");
559 agent_path = strdup(optarg);
562 capabilities = optarg;
584 passkey = strdup(argv[0]);
587 device = strdup(argv[1]);
590 agent_path = strdup(default_path);
592 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
594 fprintf(stderr, "Can't get on system bus");
598 adapter_path = get_adapter_path(conn, adapter_id);
603 if (create_paired_device(conn, adapter_path, agent_path,
604 capabilities, device) < 0) {
605 dbus_connection_unref(conn);
609 if (register_agent(conn, adapter_path, agent_path,
611 dbus_connection_unref(conn);
616 if (!dbus_connection_add_filter(conn, agent_filter, NULL, NULL))
617 fprintf(stderr, "Can't add signal filter");
619 snprintf(match_string, sizeof(match_string),
620 "interface=%s,member=NameOwnerChanged,arg0=%s",
621 DBUS_INTERFACE_DBUS, "org.bluez");
623 dbus_bus_add_match(conn, match_string, NULL);
625 memset(&sa, 0, sizeof(sa));
626 sa.sa_flags = SA_NOCLDSTOP;
627 sa.sa_handler = sig_term;
628 sigaction(SIGTERM, &sa, NULL);
629 sigaction(SIGINT, &sa, NULL);
631 while (!__io_canceled && !__io_terminated) {
632 if (dbus_connection_read_write_dispatch(conn, 500) != TRUE)
636 if (!__io_terminated && !device)
637 unregister_agent(conn, adapter_path, agent_path);
644 dbus_connection_unref(conn);