Skip to content

Commit

Permalink
Bluetooth: AVRCP: add shell tools for AVRCP functions.
Browse files Browse the repository at this point in the history
Only the basic functions for establishing an AVCTP connection
are provided at this stage.
An BR/EDR ACL connection is necessary before AVRCP function.
Register callbacks before utilizing AVRCP.

Signed-off-by: Zihao Gao <[email protected]>
  • Loading branch information
Zihao Gao committed Jul 10, 2024
1 parent 10d0430 commit 6c6aab4
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions subsys/bluetooth/shell/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ zephyr_library_sources_ifdef(
CONFIG_BT_A2DP
a2dp.c
)
zephyr_library_sources_ifdef(
CONFIG_BT_AVRCP
avrcp.c
)
zephyr_library_sources_ifdef(
CONFIG_BT_ISO
iso.c
Expand Down
127 changes: 127 additions & 0 deletions subsys/bluetooth/shell/avrcp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/** @file
* @brief Audio Video Remote Control Profile shell functions.
*/

/*
* Copyright (c) 2024 Xiaomi InC.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <errno.h>
#include <zephyr/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/kernel.h>

#include <zephyr/settings/settings.h>

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/classic/avrcp.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/l2cap.h>

#include <zephyr/shell/shell.h>

#include "bt.h"

struct bt_avrcp *default_avrcp;
static uint8_t avrcp_registered;
static void avrcp_connected(struct bt_avrcp *avrcp)
{
default_avrcp = avrcp;
shell_print(ctx_shell, "AVRCP connected");
}

static void avrcp_disconnected(struct bt_avrcp *avrcp)
{
shell_print(ctx_shell, "AVRCP disconnected");
}

static struct bt_avrcp_cb avrcp_cb = {
.connected = avrcp_connected,
.disconnected = avrcp_disconnected,
};

static int cmd_register_cb(const struct shell *sh, int32_t argc, char *argv[])
{
int err;

if (avrcp_registered) {
shell_print(sh, "already registered");
return 0;
}

avrcp_registered = 1;
err = bt_avrcp_register_cb(&avrcp_cb);
if (!err) {
shell_print(sh, "success");
} else {
shell_print(sh, "fail");
}

return 0;
}

static int cmd_connect(const struct shell *sh, int32_t argc, char *argv[])
{
if (avrcp_registered == 0) {
shell_print(sh, "need to register AVRCP callbacks");
return -ENOEXEC;
}

if (!default_conn) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}

default_avrcp = bt_avrcp_connect(default_conn);
if (NULL == default_avrcp) {
shell_error(sh, "fail to connect AVRCP");
}

return 0;
}

static int cmd_disconnect(const struct shell *sh, int32_t argc, char *argv[])
{
if (avrcp_registered == 0) {
shell_print(sh, "need to register AVRCP callbacks");
return -ENOEXEC;
}

if (default_avrcp != NULL) {
bt_avrcp_disconnect(default_avrcp);
default_avrcp = NULL;
} else {
shell_error(sh, "AVRCP is not connected");
}

return 0;
}

SHELL_STATIC_SUBCMD_SET_CREATE(avrcp_cmds,
SHELL_CMD_ARG(register_cb, NULL, "register avrcp callbacks",
cmd_register_cb, 1, 0),
SHELL_CMD_ARG(connect, NULL, "<address>", cmd_connect, 2, 0),
SHELL_CMD_ARG(disconnect, NULL, "<address>", cmd_disconnect, 2, 0),
SHELL_SUBCMD_SET_END);

static int cmd_avrcp(const struct shell *sh, size_t argc, char **argv)
{
if (argc == 1) {
shell_help(sh);
/* sh returns 1 when help is printed */
return 1;
}

shell_error(sh, "%s unknown parameter: %s", argv[0], argv[1]);

return -ENOEXEC;
}

SHELL_CMD_ARG_REGISTER(avrcp, &avrcp_cmds, "Bluetooth AVRCP sh commands",
cmd_avrcp, 1, 1);

0 comments on commit 6c6aab4

Please sign in to comment.