client: Add agent auto argument support
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 23 Aug 2023 20:31:07 +0000 (13:31 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 5 Jan 2024 13:34:03 +0000 (19:04 +0530)
This adds "auto" capability which behaves like "on" but instead of
asking user to confirm/autorize it automatically accepts, which is
not secure to be used thus a warning is printed when user selects it.

Usage:

  [bluetoothctl]# agent auto
  Warning: setting auto response is not secure, it bypass user
  confirmation/authorization, it shall only be used for test automation.

  or

  client/bluetoothctl -a auto

client/agent.c
client/main.c

index c8e1560..35b4041 100755 (executable)
@@ -258,7 +258,7 @@ static DBusMessage *cancel_request(DBusConnection *conn,
        return dbus_message_new_method_return(msg);
 }
 
-static const GDBusMethodTable methods[] = {
+static const GDBusMethodTable agent_methods[] = {
        { GDBUS_METHOD("Release", NULL, NULL, release_agent) },
        { GDBUS_ASYNC_METHOD("RequestPinCode",
                        GDBUS_ARGS({ "device", "o" }),
@@ -286,6 +286,78 @@ static const GDBusMethodTable methods[] = {
        { }
 };
 
+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) },
+       { }
+};
+
 static void register_agent_setup(DBusMessageIter *iter, void *user_data)
 {
        const char *path = AGENT_PATH;
@@ -319,6 +391,8 @@ void agent_register(DBusConnection *conn, GDBusProxy *manager,
                                                const char *capability)
 
 {
+       const GDBusMethodTable *methods = agent_methods;
+
        if (agent_registered == TRUE) {
                bt_shell_printf("Agent is already registered\n");
                return;
@@ -326,6 +400,14 @@ void agent_register(DBusConnection *conn, GDBusProxy *manager,
 
        agent_capability = capability;
 
+       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;
+       }
+
        if (g_dbus_register_interface(conn, AGENT_PATH,
                                        AGENT_INTERFACE, methods,
                                        NULL, NULL, NULL, NULL) == FALSE) {
index ee8076b..4410a6b 100644 (file)
@@ -65,6 +65,7 @@ static GList *battery_proxies;
 static const char *agent_arguments[] = {
        "on",
        "off",
+       "auto",
        "DisplayOnly",
        "DisplayYesNo",
        "KeyboardDisplay",
@@ -3102,7 +3103,7 @@ static const struct bt_shell_menu main_menu = {
                                                        NULL },
        { "discoverable-timeout", "[value]", cmd_discoverable_timeout,
                                        "Set discoverable timeout", NULL },
-       { "agent",        "<on/off/capability>", cmd_agent,
+       { "agent",        "<on/off/auto/capability>", cmd_agent,
                                "Enable/disable agent with given capability",
                                                        capability_generator},
        { "default-agent",NULL,       cmd_default_agent,