2020-09-22 03:00:24 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-12-15 07:20:25 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-03-22 06:04:52 +08:00
|
|
|
#include <stdlib.h>
|
2017-11-09 22:29:39 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2012-12-15 07:20:25 +08:00
|
|
|
|
2017-12-05 20:23:48 +08:00
|
|
|
#include <glib.h>
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
#include "src/shared/shell.h"
|
2015-03-01 14:21:46 +08:00
|
|
|
#include "gdbus/gdbus.h"
|
2012-12-15 07:20:25 +08:00
|
|
|
#include "agent.h"
|
|
|
|
|
2012-12-15 08:33:54 +08:00
|
|
|
#define AGENT_PATH "/org/bluez/agent"
|
|
|
|
#define AGENT_INTERFACE "org.bluez.Agent1"
|
|
|
|
|
2013-03-22 06:04:52 +08:00
|
|
|
#define AGENT_PROMPT COLOR_RED "[agent]" COLOR_OFF " "
|
|
|
|
|
2012-12-15 07:20:25 +08:00
|
|
|
static gboolean agent_registered = FALSE;
|
2012-12-19 00:39:56 +08:00
|
|
|
static const char *agent_capability = NULL;
|
2012-12-18 11:20:40 +08:00
|
|
|
static DBusMessage *pending_message = NULL;
|
2013-03-22 06:04:52 +08:00
|
|
|
|
|
|
|
static void agent_release_prompt(void)
|
|
|
|
{
|
2017-08-23 19:03:41 +08:00
|
|
|
if (!pending_message)
|
2013-03-22 06:04:52 +08:00
|
|
|
return;
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_release_prompt("");
|
2013-03-22 06:04:52 +08:00
|
|
|
}
|
2012-12-18 11:20:40 +08:00
|
|
|
|
|
|
|
dbus_bool_t agent_completion(void)
|
|
|
|
{
|
|
|
|
if (!pending_message)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:03:41 +08:00
|
|
|
static void pincode_response(const char *input, void *user_data)
|
2012-12-18 11:20:40 +08:00
|
|
|
{
|
2017-08-23 19:03:41 +08:00
|
|
|
DBusConnection *conn = user_data;
|
|
|
|
|
2012-12-19 00:06:12 +08:00
|
|
|
g_dbus_send_reply(conn, pending_message, DBUS_TYPE_STRING, &input,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
}
|
2012-12-18 11:20:40 +08:00
|
|
|
|
2017-08-23 19:03:41 +08:00
|
|
|
static void passkey_response(const char *input, void *user_data)
|
2013-03-22 06:04:56 +08:00
|
|
|
{
|
2017-08-23 19:03:41 +08:00
|
|
|
DBusConnection *conn = user_data;
|
2013-03-22 06:04:56 +08:00
|
|
|
dbus_uint32_t passkey;
|
2017-08-23 19:03:41 +08:00
|
|
|
|
2013-03-22 06:04:56 +08:00
|
|
|
if (sscanf(input, "%u", &passkey) == 1)
|
|
|
|
g_dbus_send_reply(conn, pending_message, DBUS_TYPE_UINT32,
|
|
|
|
&passkey, DBUS_TYPE_INVALID);
|
|
|
|
else if (!strcmp(input, "no"))
|
|
|
|
g_dbus_send_error(conn, pending_message,
|
|
|
|
"org.bluez.Error.Rejected", NULL);
|
|
|
|
else
|
|
|
|
g_dbus_send_error(conn, pending_message,
|
|
|
|
"org.bluez.Error.Canceled", NULL);
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:03:41 +08:00
|
|
|
static void confirm_response(const char *input, void *user_data)
|
2012-12-19 00:06:12 +08:00
|
|
|
{
|
2017-08-23 19:03:41 +08:00
|
|
|
DBusConnection *conn = user_data;
|
|
|
|
|
2023-12-19 14:28:01 +08:00
|
|
|
if (pending_message != NULL) {
|
|
|
|
if (!strcmp(input, "yes"))
|
|
|
|
g_dbus_send_reply(conn, pending_message,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
else if (!strcmp(input, "no"))
|
|
|
|
g_dbus_send_error(conn, pending_message,
|
2012-12-18 12:18:40 +08:00
|
|
|
"org.bluez.Error.Rejected", NULL);
|
2023-12-19 14:28:01 +08:00
|
|
|
else
|
|
|
|
g_dbus_send_error(conn, pending_message,
|
2012-12-18 12:18:40 +08:00
|
|
|
"org.bluez.Error.Canceled", NULL);
|
2023-12-19 14:28:01 +08:00
|
|
|
}
|
2012-12-19 00:06:12 +08:00
|
|
|
}
|
2012-12-18 12:18:40 +08:00
|
|
|
|
2013-11-26 23:05:09 +08:00
|
|
|
static void agent_release(DBusConnection *conn)
|
2012-12-15 07:20:25 +08:00
|
|
|
{
|
|
|
|
agent_registered = FALSE;
|
2012-12-19 00:39:56 +08:00
|
|
|
agent_capability = NULL;
|
2012-12-15 07:20:25 +08:00
|
|
|
|
2012-12-18 11:20:40 +08:00
|
|
|
if (pending_message) {
|
|
|
|
dbus_message_unref(pending_message);
|
|
|
|
pending_message = NULL;
|
|
|
|
}
|
2012-12-15 10:04:28 +08:00
|
|
|
|
2013-03-22 06:04:52 +08:00
|
|
|
agent_release_prompt();
|
|
|
|
|
2012-12-15 08:33:54 +08:00
|
|
|
g_dbus_unregister_interface(conn, AGENT_PATH, AGENT_INTERFACE);
|
2013-11-26 23:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *release_agent(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Agent released\n");
|
2013-11-26 23:05:09 +08:00
|
|
|
|
|
|
|
agent_release(conn);
|
2012-12-15 07:20:25 +08:00
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:06:12 +08:00
|
|
|
static DBusMessage *request_pincode(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Request PIN code\n");
|
2012-12-19 00:06:12 +08:00
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_prompt_input("agent", "Enter PIN code:", pincode_response,
|
|
|
|
conn);
|
2012-12-19 00:06:12 +08:00
|
|
|
|
|
|
|
pending_message = dbus_message_ref(msg);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-22 06:04:54 +08:00
|
|
|
static DBusMessage *display_pincode(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
const char *pincode;
|
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_STRING, &pincode, DBUS_TYPE_INVALID);
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf(AGENT_PROMPT "PIN code: %s\n", pincode);
|
2013-03-22 06:04:54 +08:00
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
2013-03-22 06:04:56 +08:00
|
|
|
static DBusMessage *request_passkey(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Request passkey\n");
|
2013-03-22 06:04:56 +08:00
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_prompt_input("agent", "Enter passkey (number in 0-999999):",
|
|
|
|
passkey_response, conn);
|
2013-03-22 06:04:56 +08:00
|
|
|
|
|
|
|
pending_message = dbus_message_ref(msg);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-22 06:04:55 +08:00
|
|
|
static DBusMessage *display_passkey(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
dbus_uint32_t passkey;
|
|
|
|
dbus_uint16_t entered;
|
|
|
|
char passkey_full[7];
|
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_UINT32, &passkey, DBUS_TYPE_UINT16, &entered,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
snprintf(passkey_full, sizeof(passkey_full), "%.6u", passkey);
|
|
|
|
passkey_full[6] = '\0';
|
|
|
|
|
|
|
|
if (entered > strlen(passkey_full))
|
|
|
|
entered = strlen(passkey_full);
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf(AGENT_PROMPT "Passkey: "
|
2013-03-22 06:04:55 +08:00
|
|
|
COLOR_BOLDGRAY "%.*s" COLOR_BOLDWHITE "%s\n" COLOR_OFF,
|
|
|
|
entered, passkey_full, passkey_full + entered);
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
2012-12-18 11:20:40 +08:00
|
|
|
static DBusMessage *request_confirmation(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
dbus_uint32_t passkey;
|
|
|
|
char *str;
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Request confirmation\n");
|
2012-12-18 11:20:40 +08:00
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_UINT32, &passkey, DBUS_TYPE_INVALID);
|
|
|
|
|
2017-08-23 19:03:41 +08:00
|
|
|
str = g_strdup_printf("Confirm passkey %06u (yes/no):", passkey);
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_prompt_input("agent", str, confirm_response, conn);
|
2012-12-18 11:20:40 +08:00
|
|
|
g_free(str);
|
|
|
|
|
|
|
|
pending_message = dbus_message_ref(msg);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:23:19 +08:00
|
|
|
static DBusMessage *request_authorization(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Request authorization\n");
|
2012-12-19 00:23:19 +08:00
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_prompt_input("agent", "Accept pairing (yes/no):",
|
|
|
|
confirm_response, conn);
|
2012-12-19 00:23:19 +08:00
|
|
|
|
|
|
|
pending_message = dbus_message_ref(msg);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-20 10:36:13 +08:00
|
|
|
static DBusMessage *authorize_service(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device, *uuid;
|
|
|
|
char *str;
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Authorize service\n");
|
2012-12-20 10:36:13 +08:00
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_STRING, &uuid, DBUS_TYPE_INVALID);
|
|
|
|
|
2017-08-23 19:03:41 +08:00
|
|
|
str = g_strdup_printf("Authorize service %s (yes/no):", uuid);
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_prompt_input("agent", str, confirm_response, conn);
|
2012-12-20 10:36:13 +08:00
|
|
|
g_free(str);
|
|
|
|
|
|
|
|
pending_message = dbus_message_ref(msg);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-18 11:20:40 +08:00
|
|
|
static DBusMessage *cancel_request(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Request canceled\n");
|
2012-12-18 11:20:40 +08:00
|
|
|
|
2013-03-22 06:04:52 +08:00
|
|
|
agent_release_prompt();
|
2012-12-18 11:20:40 +08:00
|
|
|
dbus_message_unref(pending_message);
|
|
|
|
pending_message = NULL;
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
2023-08-24 04:31:07 +08:00
|
|
|
static const GDBusMethodTable agent_methods[] = {
|
2012-12-15 07:20:25 +08:00
|
|
|
{ GDBUS_METHOD("Release", NULL, NULL, release_agent) },
|
2012-12-19 00:06:12 +08:00
|
|
|
{ GDBUS_ASYNC_METHOD("RequestPinCode",
|
|
|
|
GDBUS_ARGS({ "device", "o" }),
|
|
|
|
GDBUS_ARGS({ "pincode", "s" }), request_pincode) },
|
2013-03-22 06:04:54 +08:00
|
|
|
{ GDBUS_METHOD("DisplayPinCode",
|
|
|
|
GDBUS_ARGS({ "device", "o" }, { "pincode", "s" }),
|
|
|
|
NULL, display_pincode) },
|
2013-03-22 06:04:56 +08:00
|
|
|
{ GDBUS_ASYNC_METHOD("RequestPasskey",
|
|
|
|
GDBUS_ARGS({ "device", "o" }),
|
|
|
|
GDBUS_ARGS({ "passkey", "u" }), request_passkey) },
|
2013-03-22 06:04:55 +08:00
|
|
|
{ GDBUS_METHOD("DisplayPasskey",
|
|
|
|
GDBUS_ARGS({ "device", "o" }, { "passkey", "u" },
|
|
|
|
{ "entered", "q" }),
|
|
|
|
NULL, display_passkey) },
|
2012-12-18 11:20:40 +08:00
|
|
|
{ GDBUS_ASYNC_METHOD("RequestConfirmation",
|
2012-12-19 00:06:12 +08:00
|
|
|
GDBUS_ARGS({ "device", "o" }, { "passkey", "u" }),
|
2012-12-18 11:20:40 +08:00
|
|
|
NULL, request_confirmation) },
|
2012-12-19 00:23:19 +08:00
|
|
|
{ GDBUS_ASYNC_METHOD("RequestAuthorization",
|
|
|
|
GDBUS_ARGS({ "device", "o" }),
|
|
|
|
NULL, request_authorization) },
|
2012-12-20 10:36:13 +08:00
|
|
|
{ GDBUS_ASYNC_METHOD("AuthorizeService",
|
|
|
|
GDBUS_ARGS({ "device", "o" }, { "uuid", "s" }),
|
|
|
|
NULL, authorize_service) },
|
2012-12-18 11:20:40 +08:00
|
|
|
{ GDBUS_METHOD("Cancel", NULL, NULL, cancel_request) },
|
2012-12-15 07:20:25 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2023-08-24 04:31:07 +08:00
|
|
|
static DBusMessage *auto_confirmation(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
dbus_uint32_t passkey;
|
|
|
|
|
|
|
|
bt_shell_printf("Request confirmation\n");
|
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_UINT32, &passkey, DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
bt_shell_printf("Confirm passkey %06u (auto)", passkey);
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *auto_authorization(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
|
|
|
|
bt_shell_printf("Request authorization\n");
|
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
bt_shell_printf("Accept pairing (auto)");
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *auto_authorize_service(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *device, *uuid;
|
|
|
|
|
|
|
|
dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &device,
|
|
|
|
DBUS_TYPE_STRING, &uuid, DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
bt_shell_printf("Authorize service %s (auto)", uuid);
|
|
|
|
|
|
|
|
return dbus_message_new_method_return(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GDBusMethodTable auto_methods[] = {
|
|
|
|
{ GDBUS_METHOD("Release", NULL, NULL, release_agent) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("RequestPinCode",
|
|
|
|
GDBUS_ARGS({ "device", "o" }),
|
|
|
|
GDBUS_ARGS({ "pincode", "s" }), request_pincode) },
|
|
|
|
{ GDBUS_METHOD("DisplayPinCode",
|
|
|
|
GDBUS_ARGS({ "device", "o" }, { "pincode", "s" }),
|
|
|
|
NULL, display_pincode) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("RequestPasskey",
|
|
|
|
GDBUS_ARGS({ "device", "o" }),
|
|
|
|
GDBUS_ARGS({ "passkey", "u" }), request_passkey) },
|
|
|
|
{ GDBUS_METHOD("DisplayPasskey",
|
|
|
|
GDBUS_ARGS({ "device", "o" }, { "passkey", "u" },
|
|
|
|
{ "entered", "q" }),
|
|
|
|
NULL, display_passkey) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("RequestConfirmation",
|
|
|
|
GDBUS_ARGS({ "device", "o" }, { "passkey", "u" }),
|
|
|
|
NULL, auto_confirmation) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("RequestAuthorization",
|
|
|
|
GDBUS_ARGS({ "device", "o" }),
|
|
|
|
NULL, auto_authorization) },
|
|
|
|
{ GDBUS_ASYNC_METHOD("AuthorizeService",
|
|
|
|
GDBUS_ARGS({ "device", "o" }, { "uuid", "s" }),
|
|
|
|
NULL, auto_authorize_service) },
|
|
|
|
{ GDBUS_METHOD("Cancel", NULL, NULL, cancel_request) },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2012-12-15 09:16:39 +08:00
|
|
|
static void register_agent_setup(DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
const char *path = AGENT_PATH;
|
2012-12-19 00:39:56 +08:00
|
|
|
const char *capability = agent_capability;
|
2012-12-15 09:16:39 +08:00
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &capability);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void register_agent_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
DBusConnection *conn = user_data;
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == FALSE) {
|
|
|
|
agent_registered = TRUE;
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Agent registered\n");
|
2012-12-15 09:16:39 +08:00
|
|
|
} else {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to register agent: %s\n", error.name);
|
2012-12-15 09:16:39 +08:00
|
|
|
dbus_error_free(&error);
|
|
|
|
|
|
|
|
if (g_dbus_unregister_interface(conn, AGENT_PATH,
|
|
|
|
AGENT_INTERFACE) == FALSE)
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to unregister agent object\n");
|
2012-12-15 09:16:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:39:56 +08:00
|
|
|
void agent_register(DBusConnection *conn, GDBusProxy *manager,
|
|
|
|
const char *capability)
|
|
|
|
|
2012-12-15 07:20:25 +08:00
|
|
|
{
|
2023-08-24 04:31:07 +08:00
|
|
|
const GDBusMethodTable *methods = agent_methods;
|
|
|
|
|
2012-12-15 07:20:25 +08:00
|
|
|
if (agent_registered == TRUE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Agent is already registered\n");
|
2012-12-15 07:20:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:39:56 +08:00
|
|
|
agent_capability = capability;
|
|
|
|
|
2023-08-24 04:31:07 +08:00
|
|
|
if (!strcasecmp(agent_capability, "auto")) {
|
|
|
|
bt_shell_printf("Warning: setting auto response is not secure, "
|
|
|
|
"it bypass user confirmation/authorization, it "
|
|
|
|
"shall only be used for test automation.\n");
|
|
|
|
agent_capability = "";
|
|
|
|
methods = auto_methods;
|
|
|
|
}
|
|
|
|
|
2012-12-15 08:33:54 +08:00
|
|
|
if (g_dbus_register_interface(conn, AGENT_PATH,
|
|
|
|
AGENT_INTERFACE, methods,
|
2012-12-15 07:20:25 +08:00
|
|
|
NULL, NULL, NULL, NULL) == FALSE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to register agent object\n");
|
2012-12-15 07:20:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-15 09:16:39 +08:00
|
|
|
if (g_dbus_proxy_method_call(manager, "RegisterAgent",
|
|
|
|
register_agent_setup,
|
|
|
|
register_agent_reply,
|
|
|
|
conn, NULL) == FALSE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to call register agent method\n");
|
2012-12-15 09:16:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-12-19 00:39:56 +08:00
|
|
|
|
|
|
|
agent_capability = NULL;
|
2012-12-15 09:16:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unregister_agent_setup(DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
const char *path = AGENT_PATH;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unregister_agent_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
DBusConnection *conn = user_data;
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == FALSE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Agent unregistered\n");
|
2013-11-26 23:05:09 +08:00
|
|
|
agent_release(conn);
|
2012-12-15 09:16:39 +08:00
|
|
|
} else {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to unregister agent: %s\n", error.name);
|
2012-12-15 09:16:39 +08:00
|
|
|
dbus_error_free(&error);
|
|
|
|
}
|
2012-12-15 07:20:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void agent_unregister(DBusConnection *conn, GDBusProxy *manager)
|
|
|
|
{
|
|
|
|
if (agent_registered == FALSE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("No agent is registered\n");
|
2012-12-15 07:20:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 23:05:09 +08:00
|
|
|
if (!manager) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Agent unregistered\n");
|
2013-11-26 23:05:09 +08:00
|
|
|
agent_release(conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-15 09:16:39 +08:00
|
|
|
if (g_dbus_proxy_method_call(manager, "UnregisterAgent",
|
|
|
|
unregister_agent_setup,
|
|
|
|
unregister_agent_reply,
|
|
|
|
conn, NULL) == FALSE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to call unregister agent method\n");
|
2012-12-15 07:20:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 10:43:38 +08:00
|
|
|
|
|
|
|
static void request_default_setup(DBusMessageIter *iter, void *user_data)
|
|
|
|
{
|
|
|
|
const char *path = AGENT_PATH;
|
|
|
|
|
|
|
|
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void request_default_reply(DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, message) == TRUE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to request default agent: %s\n",
|
|
|
|
error.name);
|
2012-12-20 10:43:38 +08:00
|
|
|
dbus_error_free(&error);
|
2018-02-23 22:34:58 +08:00
|
|
|
return bt_shell_noninteractive_quit(EXIT_FAILURE);
|
2012-12-20 10:43:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Default agent request successful\n");
|
2018-02-23 22:34:58 +08:00
|
|
|
|
|
|
|
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
|
2012-12-20 10:43:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void agent_default(DBusConnection *conn, GDBusProxy *manager)
|
|
|
|
{
|
|
|
|
if (agent_registered == FALSE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("No agent is registered\n");
|
2018-02-23 22:34:58 +08:00
|
|
|
return bt_shell_noninteractive_quit(EXIT_FAILURE);
|
2012-12-20 10:43:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (g_dbus_proxy_method_call(manager, "RequestDefaultAgent",
|
|
|
|
request_default_setup,
|
|
|
|
request_default_reply,
|
|
|
|
NULL, NULL) == FALSE) {
|
2017-11-09 22:29:39 +08:00
|
|
|
bt_shell_printf("Failed to call RequestDefaultAgent method\n");
|
2018-02-23 22:34:58 +08:00
|
|
|
return bt_shell_noninteractive_quit(EXIT_FAILURE);
|
2012-12-20 10:43:38 +08:00
|
|
|
}
|
|
|
|
}
|