bluetooth: shell: refactor shell print to eliminate ctx_shell usage

This change aims to eliminate the dependency on `ctx_shell` in
the Bluetooth `host/shell/*`, making the code more maintainable.
Replaced `shell_*` functions that depended on `ctx_shell` with
the appropriate `bt_shell_*` functions.

The shell-less functions `bt_do_scan_filter_clear_name`, `bt_do_scan_off`,
and `bt_do_connect_le` were added so they can be called without `sh`.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
This commit is contained in:
Pisit Sawangvonganan 2024-11-11 00:10:06 +07:00 committed by Benjamin Cabé
commit aa1a38e2aa
5 changed files with 323 additions and 297 deletions

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,8 @@
#include <zephyr/bluetooth/cs.h> #include <zephyr/bluetooth/cs.h>
#include <errno.h> #include <errno.h>
#include "bt.h" #include "host/shell/bt.h"
#include "host/shell/bt_shell_private.h"
static int check_cs_sync_antenna_selection_input(uint16_t input) static int check_cs_sync_antenna_selection_input(uint16_t input)
{ {
@ -138,21 +139,21 @@ static int cmd_read_remote_fae_table(const struct shell *sh, size_t argc, char *
static bool process_step_data(struct bt_le_cs_subevent_step *step, void *user_data) static bool process_step_data(struct bt_le_cs_subevent_step *step, void *user_data)
{ {
shell_print(ctx_shell, "Subevent results contained step data: "); bt_shell_print("Subevent results contained step data: ");
shell_print(ctx_shell, "- Step mode %d\n" bt_shell_print("- Step mode %d\n"
"- Step channel %d\n" "- Step channel %d\n"
"- Step data hexdump:", "- Step data hexdump:",
step->mode, step->mode,
step->channel); step->channel);
shell_hexdump(ctx_shell, step->data, step->data_len); bt_shell_hexdump(step->data, step->data_len);
return true; return true;
} }
static void cs_test_subevent_data_cb(struct bt_conn_le_cs_subevent_result *result) static void cs_test_subevent_data_cb(struct bt_conn_le_cs_subevent_result *result)
{ {
shell_print(ctx_shell, "Received subevent results."); bt_shell_print("Received subevent results.");
shell_print(ctx_shell, "Subevent Header:\n" bt_shell_print("Subevent Header:\n"
"- Procedure Counter: %d\n" "- Procedure Counter: %d\n"
"- Frequency Compensation: 0x%04x\n" "- Frequency Compensation: 0x%04x\n"
"- Reference Power Level: %d\n" "- Reference Power Level: %d\n"
@ -179,7 +180,7 @@ static void cs_test_subevent_data_cb(struct bt_conn_le_cs_subevent_result *resul
static void cs_test_end_complete_cb(void) static void cs_test_end_complete_cb(void)
{ {
shell_print(ctx_shell, "CS Test End Complete."); bt_shell_print("CS Test End Complete.");
} }
static int cmd_cs_test_simple(const struct shell *sh, size_t argc, char *argv[]) static int cmd_cs_test_simple(const struct shell *sh, size_t argc, char *argv[])

View file

@ -27,6 +27,7 @@
#include <zephyr/shell/shell.h> #include <zephyr/shell/shell.h>
#include "host/shell/bt.h" #include "host/shell/bt.h"
#include "host/shell/bt_shell_private.h"
#if defined(CONFIG_BT_GATT_CLIENT) || defined(CONFIG_BT_GATT_DYNAMIC_DB) #if defined(CONFIG_BT_GATT_CLIENT) || defined(CONFIG_BT_GATT_DYNAMIC_DB)
extern uint8_t selected_id; extern uint8_t selected_id;
@ -75,8 +76,8 @@ static void update_write_stats(uint16_t len)
static void print_write_stats(void) static void print_write_stats(void)
{ {
shell_print(ctx_shell, "Write #%u: %u bytes (%u bps)", bt_shell_print("Write #%u: %u bytes (%u bps)",
write_stats.count, write_stats.total, write_stats.rate); write_stats.count, write_stats.total, write_stats.rate);
} }
#endif /* CONFIG_BT_GATT_CLIENT || CONFIG_BT_GATT_DYNAMIC_DB */ #endif /* CONFIG_BT_GATT_CLIENT || CONFIG_BT_GATT_DYNAMIC_DB */
@ -94,8 +95,8 @@ static struct bt_gatt_exchange_params exchange_params;
static void exchange_func(struct bt_conn *conn, uint8_t err, static void exchange_func(struct bt_conn *conn, uint8_t err,
struct bt_gatt_exchange_params *params) struct bt_gatt_exchange_params *params)
{ {
shell_print(ctx_shell, "Exchange %s", err == 0U ? "successful" : bt_shell_print("Exchange %s", err == 0U ? "successful" :
"failed"); "failed");
/* Release global `exchange_params`. */ /* Release global `exchange_params`. */
__ASSERT_NO_MSG(params == &exchange_params); __ASSERT_NO_MSG(params == &exchange_params);
@ -139,43 +140,43 @@ static int cmd_exchange_mtu(const struct shell *sh,
static struct bt_gatt_discover_params discover_params; static struct bt_gatt_discover_params discover_params;
static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0); static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0);
static void print_chrc_props(const struct shell *sh, uint8_t properties) static void print_chrc_props(uint8_t properties)
{ {
shell_print(sh, "Properties: "); bt_shell_print("Properties: ");
if (properties & BT_GATT_CHRC_BROADCAST) { if (properties & BT_GATT_CHRC_BROADCAST) {
shell_print(sh, "[bcast]"); bt_shell_print("[bcast]");
} }
if (properties & BT_GATT_CHRC_READ) { if (properties & BT_GATT_CHRC_READ) {
shell_print(sh, "[read]"); bt_shell_print("[read]");
} }
if (properties & BT_GATT_CHRC_WRITE) { if (properties & BT_GATT_CHRC_WRITE) {
shell_print(sh, "[write]"); bt_shell_print("[write]");
} }
if (properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) { if (properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) {
shell_print(sh, "[write w/w rsp]"); bt_shell_print("[write w/w rsp]");
} }
if (properties & BT_GATT_CHRC_NOTIFY) { if (properties & BT_GATT_CHRC_NOTIFY) {
shell_print(sh, "[notify]"); bt_shell_print("[notify]");
} }
if (properties & BT_GATT_CHRC_INDICATE) { if (properties & BT_GATT_CHRC_INDICATE) {
shell_print(sh, "[indicate]"); bt_shell_print("[indicate]");
} }
if (properties & BT_GATT_CHRC_AUTH) { if (properties & BT_GATT_CHRC_AUTH) {
shell_print(sh, "[auth]"); bt_shell_print("[auth]");
} }
if (properties & BT_GATT_CHRC_EXT_PROP) { if (properties & BT_GATT_CHRC_EXT_PROP) {
shell_print(sh, "[ext prop]"); bt_shell_print("[ext prop]");
} }
shell_print(sh, ""); bt_shell_print("");
} }
static uint8_t discover_func(struct bt_conn *conn, static uint8_t discover_func(struct bt_conn *conn,
@ -188,7 +189,7 @@ static uint8_t discover_func(struct bt_conn *conn,
char str[BT_UUID_STR_LEN]; char str[BT_UUID_STR_LEN];
if (!attr) { if (!attr) {
shell_print(ctx_shell, "Discover complete"); bt_shell_print("Discover complete");
(void)memset(params, 0, sizeof(*params)); (void)memset(params, 0, sizeof(*params));
return BT_GATT_ITER_STOP; return BT_GATT_ITER_STOP;
} }
@ -198,29 +199,29 @@ static uint8_t discover_func(struct bt_conn *conn,
case BT_GATT_DISCOVER_PRIMARY: case BT_GATT_DISCOVER_PRIMARY:
gatt_service = attr->user_data; gatt_service = attr->user_data;
bt_uuid_to_str(gatt_service->uuid, str, sizeof(str)); bt_uuid_to_str(gatt_service->uuid, str, sizeof(str));
shell_print(ctx_shell, "Service %s found: start handle %x, " bt_shell_print("Service %s found: start handle %x, "
"end_handle %x", str, attr->handle, "end_handle %x", str, attr->handle,
gatt_service->end_handle); gatt_service->end_handle);
break; break;
case BT_GATT_DISCOVER_CHARACTERISTIC: case BT_GATT_DISCOVER_CHARACTERISTIC:
gatt_chrc = attr->user_data; gatt_chrc = attr->user_data;
bt_uuid_to_str(gatt_chrc->uuid, str, sizeof(str)); bt_uuid_to_str(gatt_chrc->uuid, str, sizeof(str));
shell_print(ctx_shell, "Characteristic %s found: handle %x", bt_shell_print("Characteristic %s found: handle %x",
str, attr->handle); str, attr->handle);
print_chrc_props(ctx_shell, gatt_chrc->properties); print_chrc_props(gatt_chrc->properties);
break; break;
case BT_GATT_DISCOVER_INCLUDE: case BT_GATT_DISCOVER_INCLUDE:
gatt_include = attr->user_data; gatt_include = attr->user_data;
bt_uuid_to_str(gatt_include->uuid, str, sizeof(str)); bt_uuid_to_str(gatt_include->uuid, str, sizeof(str));
shell_print(ctx_shell, "Include %s found: handle %x, start %x, " bt_shell_print("Include %s found: handle %x, start %x, "
"end %x", str, attr->handle, "end %x", str, attr->handle,
gatt_include->start_handle, gatt_include->start_handle,
gatt_include->end_handle); gatt_include->end_handle);
break; break;
default: default:
bt_uuid_to_str(attr->uuid, str, sizeof(str)); bt_uuid_to_str(attr->uuid, str, sizeof(str));
shell_print(ctx_shell, "Descriptor %s found: handle %x", str, bt_shell_print("Descriptor %s found: handle %x", str,
attr->handle); attr->handle);
break; break;
} }
@ -291,13 +292,13 @@ static uint8_t read_func(struct bt_conn *conn, uint8_t err,
struct bt_gatt_read_params *params, struct bt_gatt_read_params *params,
const void *data, uint16_t length) const void *data, uint16_t length)
{ {
shell_print(ctx_shell, "Read complete: err 0x%02x length %u", err, length); bt_shell_print("Read complete: err 0x%02x length %u", err, length);
if (!data) { if (!data) {
(void)memset(params, 0, sizeof(*params)); (void)memset(params, 0, sizeof(*params));
return BT_GATT_ITER_STOP; return BT_GATT_ITER_STOP;
} else { } else {
shell_hexdump(ctx_shell, data, length); bt_shell_hexdump(data, length);
} }
return BT_GATT_ITER_CONTINUE; return BT_GATT_ITER_CONTINUE;
@ -429,7 +430,7 @@ static uint8_t gatt_write_buf[BT_ATT_MAX_ATTRIBUTE_LEN];
static void write_func(struct bt_conn *conn, uint8_t err, static void write_func(struct bt_conn *conn, uint8_t err,
struct bt_gatt_write_params *params) struct bt_gatt_write_params *params)
{ {
shell_print(ctx_shell, "Write complete: err 0x%02x", err); bt_shell_print("Write complete: err 0x%02x", err);
(void)memset(&write_params, 0, sizeof(write_params)); (void)memset(&write_params, 0, sizeof(write_params));
} }
@ -557,14 +558,14 @@ static uint8_t notify_func(struct bt_conn *conn,
const void *data, uint16_t length) const void *data, uint16_t length)
{ {
if (!data) { if (!data) {
shell_print(ctx_shell, "Unsubscribed"); bt_shell_print("Unsubscribed");
params->value_handle = 0U; params->value_handle = 0U;
return BT_GATT_ITER_STOP; return BT_GATT_ITER_STOP;
} }
shell_print(ctx_shell, "Notification: value_handle %u, length %u", bt_shell_print("Notification: value_handle %u, length %u",
params->value_handle, length); params->value_handle, length);
shell_hexdump(ctx_shell, data, length); bt_shell_hexdump(data, length);
return BT_GATT_ITER_CONTINUE; return BT_GATT_ITER_CONTINUE;
} }
@ -793,7 +794,7 @@ static ssize_t write_vnd1(struct bt_conn *conn, const struct bt_gatt_attr *attr,
uint8_t flags) uint8_t flags)
{ {
if (echo_enabled) { if (echo_enabled) {
shell_print(ctx_shell, "Echo attr len %u", len); bt_shell_print("Echo attr len %u", len);
bt_gatt_notify(conn, attr, buf, len); bt_gatt_notify(conn, attr, buf, len);
} }

View file

@ -23,6 +23,7 @@
#include <zephyr/bluetooth/iso.h> #include <zephyr/bluetooth/iso.h>
#include "host/shell/bt.h" #include "host/shell/bt.h"
#include "host/shell/bt_shell_private.h"
#if defined(CONFIG_BT_ISO_TX) #if defined(CONFIG_BT_ISO_TX)
#define DEFAULT_IO_QOS \ #define DEFAULT_IO_QOS \
@ -75,8 +76,8 @@ static void iso_recv(struct bt_iso_chan *chan, const struct bt_iso_recv_info *in
struct net_buf *buf) struct net_buf *buf)
{ {
if (info->flags & BT_ISO_FLAGS_VALID) { if (info->flags & BT_ISO_FLAGS_VALID) {
shell_print(ctx_shell, "Incoming data channel %p len %u, seq: %d, ts: %d", bt_shell_print("Incoming data channel %p len %u, seq: %d, ts: %d",
chan, buf->len, info->seq_num, info->ts); chan, buf->len, info->seq_num, info->ts);
} }
} }
#endif /* CONFIG_BT_ISO_RX */ #endif /* CONFIG_BT_ISO_RX */
@ -86,7 +87,7 @@ static void iso_connected(struct bt_iso_chan *chan)
struct bt_iso_info iso_info; struct bt_iso_info iso_info;
int err; int err;
shell_print(ctx_shell, "ISO Channel %p connected", chan); bt_shell_print("ISO Channel %p connected", chan);
err = bt_iso_chan_get_info(chan, &iso_info); err = bt_iso_chan_get_info(chan, &iso_info);
@ -108,8 +109,8 @@ static void iso_connected(struct bt_iso_chan *chan)
static void iso_disconnected(struct bt_iso_chan *chan, uint8_t reason) static void iso_disconnected(struct bt_iso_chan *chan, uint8_t reason)
{ {
shell_print(ctx_shell, "ISO Channel %p disconnected with reason 0x%02x", bt_shell_print("ISO Channel %p disconnected with reason 0x%02x",
chan, reason); chan, reason);
} }
static struct bt_iso_chan_ops iso_ops = { static struct bt_iso_chan_ops iso_ops = {
@ -474,11 +475,11 @@ static int cmd_connect(const struct shell *sh, size_t argc, char *argv[])
static int iso_accept(const struct bt_iso_accept_info *info, static int iso_accept(const struct bt_iso_accept_info *info,
struct bt_iso_chan **chan) struct bt_iso_chan **chan)
{ {
shell_print(ctx_shell, "Incoming request from %p with CIG ID 0x%02X and CIS ID 0x%02X", bt_shell_print("Incoming request from %p with CIG ID 0x%02X and CIS ID 0x%02X",
info->acl, info->cig_id, info->cis_id); info->acl, info->cig_id, info->cis_id);
if (iso_chan.iso) { if (iso_chan.iso) {
shell_print(ctx_shell, "No channels available"); bt_shell_print("No channels available");
return -ENOMEM; return -ENOMEM;
} }

View file

@ -30,6 +30,7 @@
#include <zephyr/shell/shell.h> #include <zephyr/shell/shell.h>
#include "host/shell/bt.h" #include "host/shell/bt.h"
#include "host/shell/bt_shell_private.h"
#define CREDITS 10 #define CREDITS 10
#define DATA_MTU (23 * CREDITS) #define DATA_MTU (23 * CREDITS)
@ -89,7 +90,7 @@ static void l2cap_recv_cb(struct k_work *work)
struct net_buf *buf; struct net_buf *buf;
while ((buf = k_fifo_get(&l2cap_recv_fifo, K_NO_WAIT))) { while ((buf = k_fifo_get(&l2cap_recv_fifo, K_NO_WAIT))) {
shell_print(ctx_shell, "Confirming reception"); bt_shell_print("Confirming reception");
bt_l2cap_chan_recv_complete(&c->ch.chan, buf); bt_l2cap_chan_recv_complete(&c->ch.chan, buf);
} }
} }
@ -102,18 +103,18 @@ static int l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
return l2cap_recv_metrics(chan, buf); return l2cap_recv_metrics(chan, buf);
} }
shell_print(ctx_shell, "Incoming data channel %p len %u", chan, bt_shell_print("Incoming data channel %p len %u", chan,
buf->len); buf->len);
if (buf->len) { if (buf->len) {
shell_hexdump(ctx_shell, buf->data, buf->len); bt_shell_hexdump(buf->data, buf->len);
} }
if (l2cap_recv_delay_ms > 0) { if (l2cap_recv_delay_ms > 0) {
/* Submit work only if queue is empty */ /* Submit work only if queue is empty */
if (k_fifo_is_empty(&l2cap_recv_fifo)) { if (k_fifo_is_empty(&l2cap_recv_fifo)) {
shell_print(ctx_shell, "Delaying response in %u ms...", bt_shell_print("Delaying response in %u ms...",
l2cap_recv_delay_ms); l2cap_recv_delay_ms);
} }
k_fifo_put(&l2cap_recv_fifo, buf); k_fifo_put(&l2cap_recv_fifo, buf);
@ -127,12 +128,12 @@ static int l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
static void l2cap_sent(struct bt_l2cap_chan *chan) static void l2cap_sent(struct bt_l2cap_chan *chan)
{ {
shell_print(ctx_shell, "Outgoing data channel %p transmitted", chan); bt_shell_print("Outgoing data channel %p transmitted", chan);
} }
static void l2cap_status(struct bt_l2cap_chan *chan, atomic_t *status) static void l2cap_status(struct bt_l2cap_chan *chan, atomic_t *status)
{ {
shell_print(ctx_shell, "Channel %p status %u", chan, (uint32_t)*status); bt_shell_print("Channel %p status %u", chan, (uint32_t)*status);
} }
static void l2cap_connected(struct bt_l2cap_chan *chan) static void l2cap_connected(struct bt_l2cap_chan *chan)
@ -141,19 +142,19 @@ static void l2cap_connected(struct bt_l2cap_chan *chan)
k_work_init_delayable(&c->recv_work, l2cap_recv_cb); k_work_init_delayable(&c->recv_work, l2cap_recv_cb);
shell_print(ctx_shell, "Channel %p connected", chan); bt_shell_print("Channel %p connected", chan);
} }
static void l2cap_disconnected(struct bt_l2cap_chan *chan) static void l2cap_disconnected(struct bt_l2cap_chan *chan)
{ {
shell_print(ctx_shell, "Channel %p disconnected", chan); bt_shell_print("Channel %p disconnected", chan);
} }
static struct net_buf *l2cap_alloc_buf(struct bt_l2cap_chan *chan) static struct net_buf *l2cap_alloc_buf(struct bt_l2cap_chan *chan)
{ {
/* print if metrics is disabled */ /* print if metrics is disabled */
if (!metrics) { if (!metrics) {
shell_print(ctx_shell, "Channel %p requires buffer", chan); bt_shell_print("Channel %p requires buffer", chan);
} }
return net_buf_alloc(&data_rx_pool, K_FOREVER); return net_buf_alloc(&data_rx_pool, K_FOREVER);
@ -217,7 +218,7 @@ static int l2cap_accept(struct bt_conn *conn, struct bt_l2cap_server *server,
{ {
int err; int err;
shell_print(ctx_shell, "Incoming conn %p", conn); bt_shell_print("Incoming conn %p", conn);
err = l2cap_accept_policy(conn); err = l2cap_accept_policy(conn);
if (err < 0) { if (err < 0) {
@ -225,7 +226,7 @@ static int l2cap_accept(struct bt_conn *conn, struct bt_l2cap_server *server,
} }
if (l2ch_chan.ch.chan.conn) { if (l2ch_chan.ch.chan.conn) {
shell_print(ctx_shell, "No channels available"); bt_shell_print("No channels available");
return -ENOMEM; return -ENOMEM;
} }