1 /** @file
2 * @brief Bluetooth Public Broadcast Profile shell
3 */
4
5 /*
6 * Copyright 2023 NXP
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 #include <errno.h>
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include <zephyr/bluetooth/audio/audio.h>
16 #include <zephyr/bluetooth/audio/pbp.h>
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/gap.h>
19 #include <zephyr/kernel.h>
20 #include <zephyr/net_buf.h>
21 #include <zephyr/shell/shell.h>
22 #include <zephyr/shell/shell_string_conv.h>
23 #include <zephyr/sys/__assert.h>
24 #include <zephyr/sys/util.h>
25
26 #include "common/bt_shell_private.h"
27
28 #define PBS_DEMO 'P', 'B', 'P'
29
30 const uint8_t pba_metadata[] = {
31 BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO, PBS_DEMO)
32 };
33
34 /* Buffer to hold the Public Broadcast Announcement */
35 NET_BUF_SIMPLE_DEFINE_STATIC(pbp_ad_buf, BT_PBP_MIN_PBA_SIZE + ARRAY_SIZE(pba_metadata));
36
37 enum bt_pbp_announcement_feature pbp_features;
38
cmd_pbp_set_features(const struct shell * sh,size_t argc,char ** argv)39 static int cmd_pbp_set_features(const struct shell *sh, size_t argc, char **argv)
40 {
41 int err = 0;
42 enum bt_pbp_announcement_feature features;
43
44 features = shell_strtoul(argv[1], 16, &err);
45 if (err != 0) {
46 shell_error(sh, "Could not parse received features: %d", err);
47
48 return -ENOEXEC;
49 }
50
51 pbp_features = features;
52
53 return err;
54 }
55
pbp_ad_data_add(struct bt_data data[],size_t data_size)56 size_t pbp_ad_data_add(struct bt_data data[], size_t data_size)
57 {
58 int err;
59
60 net_buf_simple_reset(&pbp_ad_buf);
61
62 err = bt_pbp_get_announcement(pba_metadata,
63 ARRAY_SIZE(pba_metadata),
64 pbp_features,
65 &pbp_ad_buf);
66
67 if (err != 0) {
68 bt_shell_info("Create Public Broadcast Announcement");
69 } else {
70 bt_shell_error("Failed to create Public Broadcast Announcement: %d", err);
71 }
72
73 __ASSERT(data_size > 0, "No space for Public Broadcast Announcement");
74 data[0].type = BT_DATA_SVC_DATA16;
75 data[0].data_len = pbp_ad_buf.len;
76 data[0].data = pbp_ad_buf.data;
77
78 return 1U;
79 }
80
cmd_pbp(const struct shell * sh,size_t argc,char ** argv)81 static int cmd_pbp(const struct shell *sh, size_t argc, char **argv)
82 {
83 if (argc > 1) {
84 shell_error(sh, "%s unknown parameter: %s", argv[0], argv[1]);
85 } else {
86 shell_error(sh, "%s missing subcomand", argv[0]);
87 }
88
89 return -ENOEXEC;
90 }
91
92 SHELL_STATIC_SUBCMD_SET_CREATE(pbp_cmds,
93 SHELL_CMD_ARG(set_features, NULL,
94 "Set the Public Broadcast Announcement features",
95 cmd_pbp_set_features, 2, 0),
96 SHELL_SUBCMD_SET_END
97 );
98
99 SHELL_CMD_ARG_REGISTER(pbp, &pbp_cmds, "Bluetooth pbp shell commands", cmd_pbp, 1, 1);
100