Bluetooth: Shell: Remove usage of auto name in AD

The advertiser options to automatically add the name in the scan
response and the advertising data will be deprecated.

Update the Bluetooth shell and its documentation to not use those
options anymore.

The option of the legacy advertiser run with `bt advertise` and the
behavior hasn't changed. The code has been updated to no longer use the
advertiser option but the original behavior has been reproduced.

The option of the extended advertiser have been updated to be more
explicit. The options `name` and `name-ad` have been removed from the
`bt adv-create` and `bt adv-param`. The option `name` and `dev-name`
have been added to the `bt adv-data` command.

- `name <str>` will advertise the given name.
- `dev-name` will advertise the device name.

Signed-off-by: Théo Battrel <theo.battrel@nordicsemi.no>
This commit is contained in:
Théo Battrel 2024-04-19 09:36:28 +02:00 committed by Carles Cufí
parent 7bc63985af
commit 6c9010be9f
2 changed files with 103 additions and 31 deletions

View file

@ -166,19 +166,20 @@ For example, if you want to create a connectable and scannable advertiser and st
Advertiser[0] 0x200022f0 set started
You may notice that with this, the custom advertiser does not advertise the device name; you need to
enable it. Continuing from the previous example:
add it. Continuing from the previous example:
.. code-block:: console
uart:~$ bt adv-stop
Advertiser set stopped
uart:~$ bt adv-param conn-scan name
uart:~$ bt adv-data dev-name
uart:~$ bt adv-start
Advertiser[0] 0x200022f0 set started
You should now see the name of the device in the advertising data. You can also set the advertising
data manually by using the :code:`bt adv-data` command. The following example shows how to set the
advertiser name with it:
You should now see the name of the device in the advertising data. You can also set a custom name by
using :code:`name <custom name>` instead of :code:`dev-name`. It is also possible to set the
advertising data manually with the :code:`bt adv-data` command. The following example shows how
to set the advertiser name with it using raw advertising data:
.. code-block:: console

View file

@ -1762,14 +1762,33 @@ static ssize_t ad_init(struct bt_data *data_array, const size_t data_array_size,
return ad_len;
}
void set_ad_name_complete(struct bt_data *ad, const char *name)
{
ad->type = BT_DATA_NAME_COMPLETE;
ad->data_len = strlen(name);
ad->data = name;
}
void set_ad_device_name_complete(struct bt_data *ad)
{
const char *name = bt_get_name();
set_ad_name_complete(ad, name);
}
static int cmd_advertise(const struct shell *sh, size_t argc, char *argv[])
{
struct bt_le_adv_param param = {};
struct bt_data ad[3];
struct bt_data ad[4];
struct bt_data sd[4];
bool discoverable = true;
bool appearance = false;
ssize_t ad_len;
ssize_t ad_len = 0;
ssize_t sd_len = 0;
int err;
bool with_name = true;
bool name_ad = false;
bool name_sd = true;
if (!strcmp(argv[1], "off")) {
if (bt_le_adv_stop() < 0) {
@ -1787,10 +1806,7 @@ static int cmd_advertise(const struct shell *sh, size_t argc, char *argv[])
param.interval_max = BT_GAP_ADV_FAST_INT_MAX_2;
if (!strcmp(argv[1], "on")) {
param.options = (BT_LE_ADV_OPT_CONNECTABLE |
BT_LE_ADV_OPT_USE_NAME);
} else if (!strcmp(argv[1], "scan")) {
param.options = BT_LE_ADV_OPT_USE_NAME;
param.options = BT_LE_ADV_OPT_CONNECTABLE;
} else if (!strcmp(argv[1], "nconn")) {
param.options = 0U;
} else {
@ -1816,10 +1832,10 @@ static int cmd_advertise(const struct shell *sh, size_t argc, char *argv[])
} else if (!strcmp(arg, "identity")) {
param.options |= BT_LE_ADV_OPT_USE_IDENTITY;
} else if (!strcmp(arg, "no-name")) {
param.options &= ~BT_LE_ADV_OPT_USE_NAME;
with_name = false;
} else if (!strcmp(arg, "name-ad")) {
param.options |= BT_LE_ADV_OPT_USE_NAME;
param.options |= BT_LE_ADV_OPT_FORCE_NAME_IN_AD;
name_ad = true;
name_sd = false;
} else if (!strcmp(arg, "one-time")) {
param.options |= BT_LE_ADV_OPT_ONE_TIME;
} else if (!strcmp(arg, "disable-37")) {
@ -1833,18 +1849,30 @@ static int cmd_advertise(const struct shell *sh, size_t argc, char *argv[])
}
}
if (name_ad && with_name) {
set_ad_device_name_complete(&ad[0]);
ad_len++;
}
if (name_sd && with_name) {
set_ad_device_name_complete(&sd[0]);
sd_len++;
}
atomic_clear(adv_opt);
atomic_set_bit_to(adv_opt, SHELL_ADV_OPT_CONNECTABLE,
(param.options & BT_LE_ADV_OPT_CONNECTABLE) > 0);
atomic_set_bit_to(adv_opt, SHELL_ADV_OPT_DISCOVERABLE, discoverable);
atomic_set_bit_to(adv_opt, SHELL_ADV_OPT_APPEARANCE, appearance);
ad_len = ad_init(ad, ARRAY_SIZE(ad), adv_opt);
err = ad_init(&ad[ad_len], ARRAY_SIZE(ad) - ad_len, adv_opt);
if (ad_len < 0) {
return -ENOEXEC;
}
ad_len += err;
err = bt_le_adv_start(&param, ad_len > 0 ? ad : NULL, ad_len, NULL, 0);
err = bt_le_adv_start(&param, ad_len > 0 ? ad : NULL, ad_len, sd_len > 0 ? sd : NULL,
sd_len);
if (err < 0) {
shell_error(sh, "Failed to start advertising (err %d)",
err);
@ -1954,11 +1982,6 @@ static bool adv_param_parse(size_t argc, char *argv[],
param->options |= BT_LE_ADV_OPT_FILTER_CONN;
} else if (!strcmp(arg, "identity")) {
param->options |= BT_LE_ADV_OPT_USE_IDENTITY;
} else if (!strcmp(arg, "name")) {
param->options |= BT_LE_ADV_OPT_USE_NAME;
} else if (!strcmp(arg, "name-ad")) {
param->options |= BT_LE_ADV_OPT_USE_NAME;
param->options |= BT_LE_ADV_OPT_FORCE_NAME_IN_AD;
} else if (!strcmp(arg, "low")) {
param->options |= BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY;
} else if (!strcmp(arg, "dir-rpa")) {
@ -2069,6 +2092,9 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[])
bool discoverable = false;
size_t *data_len;
int err;
bool name = false;
bool dev_name = false;
const char *name_value = NULL;
if (!adv) {
return -EINVAL;
@ -2081,11 +2107,31 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[])
for (size_t argn = 1; argn < argc; argn++) {
const char *arg = argv[argn];
if (strcmp(arg, "scan-response") &&
*data_len == ARRAY_SIZE(ad)) {
if (name && !dev_name && name_value == NULL) {
if (*data_len == ARRAY_SIZE(ad)) {
/* Maximum entries limit reached. */
shell_print(sh, "Failed to set advertising data: "
"Maximum entries limit reached");
return -ENOEXEC;
}
len = strlen(arg);
memcpy(&hex_data[hex_data_len], arg, len);
name_value = &hex_data[hex_data_len];
set_ad_name_complete(&data[*data_len], name_value);
(*data_len)++;
hex_data_len += len;
continue;
}
if (strcmp(arg, "scan-response") && *data_len == ARRAY_SIZE(ad)) {
/* Maximum entries limit reached. */
shell_print(sh, "Failed to set advertising data: "
"Maximum entries limit reached");
"Maximum entries limit reached");
return -ENOEXEC;
}
@ -2099,19 +2145,24 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[])
} else if (!strcmp(arg, "scan-response")) {
if (data == sd) {
shell_print(sh, "Failed to set advertising data: "
"duplicate scan-response option");
"duplicate scan-response option");
return -ENOEXEC;
}
data = sd;
data_len = &sd_len;
} else if (!strcmp(arg, "name") && !name) {
name = true;
} else if (!strcmp(arg, "dev-name")) {
name = true;
dev_name = true;
} else {
len = hex2bin(arg, strlen(arg), &hex_data[hex_data_len],
sizeof(hex_data) - hex_data_len);
if (!len || (len - 1) != (hex_data[hex_data_len])) {
shell_print(sh, "Failed to set advertising data: "
"malformed hex data");
"malformed hex data");
return -ENOEXEC;
}
@ -2123,6 +2174,25 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[])
}
}
if (name && !dev_name && name_value == NULL) {
shell_error(sh, "Failed to set advertising data: Expected a value for 'name'");
return -ENOEXEC;
}
if (name && dev_name && name_value == NULL) {
if (*data_len == ARRAY_SIZE(ad)) {
/* Maximum entries limit reached. */
shell_print(sh, "Failed to set advertising data: "
"Maximum entries limit reached");
return -ENOEXEC;
}
set_ad_device_name_complete(&data[*data_len]);
(*data_len)++;
}
atomic_set_bit_to(adv_set_opt[selected_adv], SHELL_ADV_OPT_DISCOVERABLE, discoverable);
atomic_set_bit_to(adv_set_opt[selected_adv], SHELL_ADV_OPT_APPEARANCE,
appearance);
@ -2324,7 +2394,7 @@ static int cmd_adv_rpa_expire(const struct shell *sh, size_t argc, char *argv[])
return 0;
}
#endif
#endif /* CONFIG_BT_PRIVACY */
#if defined(CONFIG_BT_PER_ADV)
static int cmd_per_adv(const struct shell *sh, size_t argc, char *argv[])
@ -4472,8 +4542,8 @@ static int cmd_default_handler(const struct shell *sh, size_t argc, char **argv)
#define EXT_ADV_PARAM \
"<type: conn-scan conn-nscan, nconn-scan nconn-nscan> " \
"[ext-adv] [no-2m] [coded] [anon] [tx-power] [scan-reports] " \
"[filter-accept-list: fal, fal-scan, fal-conn] [identity] [name] " \
"[name-ad] [directed " HELP_ADDR_LE "] [mode: low] [dir-rpa] " \
"[filter-accept-list: fal, fal-scan, fal-conn] [identity] " \
"[directed " HELP_ADDR_LE "] [mode: low] [dir-rpa] " \
"[disable-37] [disable-38] [disable-39]"
#else
#define EXT_ADV_SCAN_OPT ""
@ -4551,7 +4621,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(bt_cmds,
#endif
#if defined(CONFIG_BT_BROADCASTER)
SHELL_CMD_ARG(advertise, NULL,
"<type: off, on, scan, nconn> [mode: discov, non_discov] "
"<type: off, on, nconn> [mode: discov, non_discov] "
"[filter-accept-list: fal, fal-scan, fal-conn] [identity] [no-name] "
"[one-time] [name-ad] [appearance] "
"[disable-37] [disable-38] [disable-39]",
@ -4565,7 +4635,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(bt_cmds,
SHELL_CMD_ARG(adv-create, NULL, EXT_ADV_PARAM, cmd_adv_create, 2, 11),
SHELL_CMD_ARG(adv-param, NULL, EXT_ADV_PARAM, cmd_adv_param, 2, 11),
SHELL_CMD_ARG(adv-data, NULL, "<data> [scan-response <data>] "
"<type: discov, hex> [appearance] ",
"<type: discov, hex> [appearance] "
"[name <str>] [dev-name]",
cmd_adv_data, 1, 16),
SHELL_CMD_ARG(adv-start, NULL,
"[timeout <timeout>] [num-events <num events>]",