/** @file
* @brief Bluetooth shell module
*
* Provide some Bluetooth shell commands that can be useful to applications.
*/
/*
* Copyright (c) 2017 Intel Corporation
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/types.h>
#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/kernel.h>
#include <zephyr/settings/settings.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/classic/rfcomm.h>
#include <zephyr/bluetooth/classic/sdp.h>
#include <zephyr/bluetooth/iso.h>
#include <zephyr/bluetooth/ead.h>
#include <zephyr/shell/shell.h>
#include "bt.h"
#include "ll.h"
#include "hci.h"
#include "../audio/shell/audio.h"
static bool no_settings_load;
uint8_t selected_id = BT_ID_DEFAULT;
const struct shell *ctx_shell;
#if defined(CONFIG_BT_CONN)
struct bt_conn *default_conn;
/* Connection context for BR/EDR legacy pairing in sec mode 3 */
static struct bt_conn *pairing_conn;
static struct bt_le_oob oob_local;
#if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CLASSIC)
static struct bt_le_oob oob_remote;
#endif /* CONFIG_BT_SMP || CONFIG_BT_CLASSIC) */
#endif /* CONFIG_BT_CONN */
#if defined(CONFIG_BT_SMP)
static struct bt_conn_auth_info_cb auth_info_cb;
#endif /* CONFIG_BT_SMP */
#define NAME_LEN 30
#define KEY_STR_LEN 33
#define ADV_DATA_DELIMITER ", "
#define AD_SIZE 9
/*
* Based on the maximum number of parameters for HCI_LE_Generate_DHKey
* See BT Core Spec V5.2 Vol. 4, Part E, section 7.8.37
*/
#define HCI_CMD_MAX_PARAM 65
#if defined(CONFIG_BT_BROADCASTER)
enum {
SHELL_ADV_OPT_CONNECTABLE,
SHELL_ADV_OPT_DISCOVERABLE,
SHELL_ADV_OPT_EXT_ADV,
SHELL_ADV_OPT_APPEARANCE,
SHELL_ADV_OPT_KEEP_RPA,
SHELL_ADV_OPT_NUM,
};
static ATOMIC_DEFINE(adv_opt, SHELL_ADV_OPT_NUM);
#if defined(CONFIG_BT_EXT_ADV)
uint8_t selected_adv;
struct bt_le_ext_adv *adv_sets[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
static ATOMIC_DEFINE(adv_set_opt, SHELL_ADV_OPT_NUM)[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
#endif /* CONFIG_BT_EXT_ADV */
#endif /* CONFIG_BT_BROADCASTER */
#if defined(CONFIG_BT_OBSERVER) || defined(CONFIG_BT_USER_PHY_UPDATE)
static const char *phy2str(uint8_t phy)
{
switch (phy) {
case 0: return "No packets";
case BT_GAP_LE_PHY_1M:
return "LE 1M";
case BT_GAP_LE_PHY_2M:
return "LE 2M";
case BT_GAP_LE_PHY_CODED:
return "LE Coded";
default:
return "Unknown";
}
}
#endif
#if defined(CONFIG_BT_CONN) || (defined(CONFIG_BT_BROADCASTER) && defined(CONFIG_BT_EXT_ADV))
static void print_le_addr(const char *desc, const bt_addr_le_t *addr)
{
char addr_str[BT_ADDR_LE_STR_LEN];
const char *addr_desc = bt_addr_le_is_identity(addr) ? "identity" :
bt_addr_le_is_rpa(addr) ? "resolvable" :
"non-resolvable";
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
shell_print(ctx_shell, "%s address: %s (%s)", desc, addr_str,
addr_desc);
}
#endif /* CONFIG_BT_CONN || (CONFIG_BT_BROADCASTER && CONFIG_BT_EXT_ADV) */
#if defined(CONFIG_BT_TRANSMIT_POWER_CONTROL)
static const char *tx_power_flag2str(int8_t flag)
{
switch (flag) {
case 0:
return "Neither Max nor Min Tx Power";
case 1:
return "Tx Power Level is at minimum";
case 2:
return "Tx Power Level is at maximum";
/* Current Tx Power Level is the only available one*/
case 3:
return "Tx Power Level is at minimum & maximum.";
default:
return "Unknown";
}
}
static const char *tx_power_report_reason2str(uint8_t reason)
{
switch (reason) {
case BT_HCI_LE_TX_POWER_REPORT_REASON_LOCAL_CHANGED:
return "Local Tx Power changed";
case BT_HCI_LE_TX_POWER_REPORT_REASON_REMOTE_CHANGED:
return "Remote Tx Power changed";
case BT_HCI_LE_TX_POWER_REPORT_REASON_READ_REMOTE_COMPLETED:
return "Completed to read remote Tx Power";
default:
return "Unknown";
}
}
static const char *tx_pwr_ctrl_phy2str(enum bt_conn_le_tx_power_phy phy)
{
switch (phy) {
case BT_CONN_LE_TX_POWER_PHY_NONE:
return "None";
case BT_CONN_LE_TX_POWER_PHY_1M:
return "LE 1M";
case BT_CONN_LE_TX_POWER_PHY_2M:
return "LE 2M";
case BT_CONN_LE_TX_POWER_PHY_CODED_S8:
return "LE Coded S8";
case BT_CONN_LE_TX_POWER_PHY_CODED_S2:
return "LE Coded S2";
default:
return "Unknown";
}
}
static const char *enabled2str(bool enabled)
{
if (enabled) {
return "Enabled";
} else {
return "Disabled";
}
}
#endif /* CONFIG_BT_TRANSMIT_POWER_CONTROL */
#if defined(CONFIG_BT_PATH_LOSS_MONITORING)
static const char *plm_report_zone_str(enum bt_conn_le_path_loss_zone zone)
{
switch (zone) {
case BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_LOW:
return "Entered low zone";
case BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_MIDDLE:
return "Entered middle zone";
case BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_HIGH:
return "Entered high zone";
case BT_CONN_LE_PATH_LOSS_ZONE_UNAVAILABLE:
return "Path loss unavailable";
default:
return "Unknown";
}
}
#endif /* CONFIG_BT_PATH_LOSS_MONITORING */
#if defined(CONFIG_BT_CENTRAL)
static int cmd_scan_off(const struct shell *sh);
static int cmd_connect_le(const struct shell *sh, size_t argc, char *argv[]);
static int cmd_scan_filter_clear_name(const struct shell *sh, size_t argc,
char *argv[]);
static struct bt_auto_connect {
bt_addr_le_t addr;
bool addr_set;
bool connect_name;
} auto_connect;
#endif
#if defined(CONFIG_BT_OBSERVER)
static struct bt_scan_filter {
char name[NAME_LEN];
bool name_set;
char addr[BT_ADDR_STR_LEN];
bool addr_set;
int8_t rssi;
bool rssi_set;
uint16_t pa_interval;
bool pa_interval_set;
} scan_filter;
static const char scan_response_label[] = "[DEVICE]: ";
static bool scan_verbose_output;
#if defined(CONFIG_BT_EAD)
static uint8_t bt_shell_ead_session_key[BT_EAD_KEY_SIZE] = {0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5,
0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB,
0xCC, 0xCD, 0xCE, 0xCF};
static uint8_t bt_shell_ead_iv[BT_EAD_IV_SIZE] = {0xFB, 0x56, 0xE1, 0xDA, 0xDC, 0x7E, 0xAD, 0xF5};
/* this is the number of ad struct allowed */
#define BT_SHELL_EAD_MAX_AD 10
static size_t bt_shell_ead_ad_len;
#if defined(CONFIG_BT_CTLR_ADV_DATA_LEN_MAX)
/* this is the maximum total size of the ad data */
#define BT_SHELL_EAD_DATA_MAX_SIZE CONFIG_BT_CTLR_ADV_DATA_LEN_MAX
#else
#define BT_SHELL_EAD_DATA_MAX_SIZE 31
#endif
static size_t bt_shell_ead_data_size;
static uint8_t bt_shell_ead_data[BT_SHELL_EAD_DATA_MAX_SIZE] = {0};
int ead_update_ad(void);
#endif
static bool bt_shell_ead_decrypt_scan;
bool is_substring(const char *substr, const char *str)
{
const size_t str_len = strlen(str);
const size_t sub_str_len = strlen(substr);
if (sub_str_len > str_len) {
return false;
}
for (size_t pos = 0; pos < str_len; pos++) {
if (pos + sub_str_len > str_len) {
return false;
}
if (strncasecmp(substr, &str[pos], sub_str_len) == 0) {
return true;
}
}
return false;
}
static bool data_cb(struct bt_data *data, void *user_data)
{
char *name = user_data;
switch (data->type) {
case BT_DATA_NAME_SHORTENED:
case BT_DATA_NAME_COMPLETE:
case BT_DATA_BROADCAST_NAME:
memcpy(name, data->data, MIN(data->data_len, NAME_LEN - 1));
return false;
default:
return true;
}
}
static void print_data_hex(const uint8_t *data, uint8_t len, enum shell_vt100_color color)
{
if (len == 0)
return;
shell_fprintf(ctx_shell, color, "0x");
/* Reverse the byte order when printing as advertising data is LE
* and the MSB should be first in the printed output.
*/
for (int16_t i = len - 1; i >= 0; i--) {
shell_fprintf(ctx_shell, color, "%02x", data[i]);
}
}
static void print_data_set(uint8_t set_value_len,
const uint8_t *scan_data, uint8_t scan_data_len)
{
uint8_t idx = 0;
if (scan_data_len == 0 || set_value_len > scan_data_len) {
return;
}
do {
if (idx > 0) {
shell_fprintf(ctx_shell, SHELL_INFO, ADV_DATA_DELIMITER);
}
print_data_hex(&scan_data[idx], set_value_len, SHELL_INFO);
idx += set_value_len;
} while (idx + set_value_len <= scan_data_len);
if (idx < scan_data_len) {
shell_fprintf(ctx_shell, SHELL_WARNING, " Excess data: ");
print_data_hex(&scan_data[idx], scan_data_len - idx, SHELL_WARNING);
}
}
static bool data_verbose_cb(struct bt_data *data, void *user_data)
{
shell_fprintf(ctx_shell, SHELL_INFO, "%*sType 0x%02x: ",
strlen(scan_response_label), "", data->type);
switch (data->type) {
case BT_DATA_UUID16_SOME:
case BT_DATA_UUID16_ALL:
case BT_DATA_SOLICIT16:
print_data_set(BT_UUID_SIZE_16, data->data, data->data_len);
break;
case BT_DATA_SVC_DATA16:
/* Data starts with a UUID16 (2 bytes),
* the rest is unknown and printed as single bytes
*/
if (data->data_len < BT_UUID_SIZE_16) {
shell_fprintf(ctx_shell, SHELL_WARNING,
"BT_DATA_SVC_DATA16 data length too short (%u)",
data->data_len);
break;
}
print_data_set(BT_UUID_SIZE_16, data->data, BT_UUID_SIZE_16);
if (data->data_len > BT_UUID_SIZE_16) {
shell_fprintf(ctx_shell, SHELL_INFO, ADV_DATA_DELIMITER);
print_data_set(1, data->data + BT_UUID_SIZE_16,
data->data_len - BT_UUID_SIZE_16);
}
break;
case BT_DATA_UUID32_SOME:
case BT_DATA_UUID32_ALL:
print_data_set(BT_UUID_SIZE_32, data->data, data->data_len);
break;
case BT_DATA_SVC_DATA32:
/* Data starts with a UUID32 (4 bytes),
* the rest is unknown and printed as single bytes
*/
if (data->data_len < BT_UUID_SIZE_32) {
shell_fprintf(ctx_shell, SHELL_WARNING,
"BT_DATA_SVC_DATA32 data length too short (%u)",
data->data_len);
break;
}
print_data_set(BT_UUID_SIZE_32, data->data, BT_UUID_SIZE_32);
if (data->data_len > BT_UUID_SIZE_32) {
shell_fprintf(ctx_shell, SHELL_INFO, ADV_DATA_DELIMITER);
print_data_set(1, data->data + BT_UUID_SIZE_32,
data->data_len - BT_UUID_SIZE_32);
}
break;
case BT_DATA_UUID128_SOME:
case BT_DATA_UUID128_ALL:
case BT_DATA_SOLICIT128:
print_data_set(BT_UUID_SIZE_128, data->data, data->data_len);
break;
case BT_DATA_SVC_DATA128:
/* Data starts with a UUID128 (16 bytes),
* the rest is unknown and printed as single bytes
*/
if (data->data_len < BT_UUID_SIZE_128) {
shell_fprintf(ctx_shell, SHELL_WARNING,
"BT_DATA_SVC_DATA128 data length too short (%u)",
data->data_len);
break;
}
print_data_set(BT_UUID_SIZE_128, data->data, BT_UUID_SIZE_128);
if (data->data_len > BT_UUID_SIZE_128) {
shell_fprintf(ctx_shell, SHELL_INFO, ADV_DATA_DELIMITER);
print_data_set(1, data->data + BT_UUID_SIZE_128,
data->data_len - BT_UUID_SIZE_128);
}
break;
case BT_DATA_NAME_SHORTENED:
case BT_DATA_NAME_COMPLETE:
case BT_DATA_BROADCAST_NAME:
shell_fprintf(ctx_shell, SHELL_INFO, "%.*s", data->data_len, data->data);
break;
case BT_DATA_PUB_TARGET_ADDR:
case BT_DATA_RAND_TARGET_ADDR:
case BT_DATA_LE_BT_DEVICE_ADDRESS:
print_data_set(BT_ADDR_SIZE, data->data, data->data_len);
break;
case BT_DATA_CSIS_RSI:
print_data_set(3, data->data, data->data_len);
break;
case BT_DATA_ENCRYPTED_AD_DATA:
shell_fprintf(ctx_shell, SHELL_INFO, "Encrypted Advertising Data: ");
print_data_set(1, data->data, data->data_len);
if (bt_shell_ead_decrypt_scan) {
#if defined(CONFIG_BT_EAD)
shell_fprintf(ctx_shell, SHELL_INFO, "\n%*s[START DECRYPTED DATA]\n",
strlen(scan_response_label), "");
int ead_err;
struct net_buf_simple decrypted_buf;
size_t decrypted_data_size = BT_EAD_DECRYPTED_PAYLOAD_SIZE(data->data_len);
uint8_t decrypted_data[decrypted_data_size];
ead_err = bt_ead_decrypt(bt_shell_ead_session_key, bt_shell_ead_iv,
data->data, data->data_len, decrypted_data);
if (ead_err) {
shell_error(ctx_shell, "Error during decryption (err %d)", ead_err);
}
net_buf_simple_init_with_data(&decrypted_buf, &decrypted_data[0],
decrypted_data_size);
bt_data_parse(&decrypted_buf, &data_verbose_cb, user_data);
shell_fprintf(ctx_shell, SHELL_INFO, "%*s[END DECRYPTED DATA]",
strlen(scan_response_label), "");
#endif
}
break;
default:
print_data_set(1, data->data, data->data_len);
}
shell_fprintf(ctx_shell, SHELL_INFO, "\n");
return true;
}
static const char *scan_response_type_txt(uint8_t type)
{
switch (type) {
case BT_GAP_ADV_TYPE_ADV_IND:
return "ADV_IND";
case BT_GAP_ADV_TYPE_ADV_DIRECT_IND:
return "ADV_DIRECT_IND";
case BT_GAP_ADV_TYPE_ADV_SCAN_IND:
return "ADV_SCAN_IND";
case BT_GAP_ADV_TYPE_ADV_NONCONN_IND:
return "ADV_NONCONN_IND";
case BT_GAP_ADV_TYPE_SCAN_RSP:
return "SCAN_RSP";
case BT_GAP_ADV_TYPE_EXT_ADV:
return "EXT_ADV";
default:
return "UNKNOWN";
}
}
bool passes_scan_filter(const struct bt_le_scan_recv_info *info, const struct net_buf_simple *buf)
{
if (scan_filter.rssi_set && (scan_filter.rssi > info->rssi)) {
return false;
}
if (scan_filter.pa_interval_set &&
(scan_filter.pa_interval > BT_CONN_INTERVAL_TO_MS(info->interval))) {
return false;
}
if (scan_filter.addr_set) {
char le_addr[BT_ADDR_LE_STR_LEN] = {0};
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
if (!is_substring(scan_filter.addr, le_addr)) {
return false;
}
}
if (scan_filter.name_set) {
struct net_buf_simple buf_copy;
char name[NAME_LEN] = {0};
/* call to bt_data_parse consumes netbufs so shallow clone for verbose output */
net_buf_simple_clone(buf, &buf_copy);
bt_data_parse(&buf_copy, data_cb, name);
if (!is_substring(scan_filter.name, name)) {
return false;
}
}
return true;
}
static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_simple *buf)
{
char le_addr[BT_ADDR_LE_STR_LEN];
char name[NAME_LEN];
struct net_buf_simple buf_copy;
if (!passes_scan_filter(info, buf)) {
return;
}
if (scan_verbose_output) {
/* call to bt_data_parse consumes netbufs so shallow clone for verbose output */
net_buf_simple_clone(buf, &buf_copy);
}
(void)memset(name, 0, sizeof(name));
bt_data_parse(buf, data_cb, name);
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
shell_print(ctx_shell, "%s%s, AD evt type %u, RSSI %i %s "
"C:%u S:%u D:%d SR:%u E:%u Prim: %s, Secn: %s, "
"Interval: 0x%04x (%u us), SID: 0x%x",
scan_response_label,
le_addr, info->adv_type, info->rssi, name,
(info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) != 0,
(info->adv_props & BT_GAP_ADV_PROP_SCANNABLE) != 0,
(info->adv_props & BT_GAP_ADV_PROP_DIRECTED) != 0,
(info->adv_props & BT_GAP_ADV_PROP_SCAN_RESPONSE) != 0,
(info->adv_props & BT_GAP_ADV_PROP_EXT_ADV) != 0,
phy2str(info->primary_phy), phy2str(info->secondary_phy),
info->interval, BT_CONN_INTERVAL_TO_US(info->interval),
info->sid);
if (scan_verbose_output) {
shell_info(ctx_shell,
"%*s[SCAN DATA START - %s]",
strlen(scan_response_label), "",
scan_response_type_txt(info->adv_type));
bt_data_parse(&buf_copy, data_verbose_cb, NULL);
shell_info(ctx_shell, "%*s[SCAN DATA END]", strlen(scan_response_label), "");
}
#if defined(CONFIG_BT_CENTRAL)
if ((info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) != 0U) {
struct bt_conn *conn = bt_conn_lookup_addr_le(selected_id, info->addr);
/* Only store auto-connect address for devices we are not already connected to */
if (conn == NULL) {
/* Store address for later use */
auto_connect.addr_set = true;
bt_addr_le_copy(&auto_connect.addr, info->addr);
/* Use the above auto_connect.addr address to automatically connect */
if (auto_connect.connect_name) {
auto_connect.connect_name = false;
cmd_scan_off(ctx_shell);
/* "name" is what would be in argv[0] normally */
cmd_scan_filter_clear_name(ctx_shell, 1, (char *[]){"name"});
/* "connect" is what would be in argv[0] normally */
cmd_connect_le(ctx_shell, 1, (char *[]){"connect"});
}
} else {
bt_conn_unref(conn);
}
}
#endif /* CONFIG_BT_CENTRAL */
}
static void scan_timeout(void)
{
shell_print(ctx_shell, "Scan timeout");
#if defined(CONFIG_BT_CENTRAL)
if (auto_connect.connect_name) {
auto_connect.connect_name = false;
/* "name" is what would be in argv[0] normally */
cmd_scan_filter_clear_name(ctx_shell, 1, (char *[]){ "name" });
}
#endif /* CONFIG_BT_CENTRAL */
}
#endif /* CONFIG_BT_OBSERVER */
#if defined(CONFIG_BT_EXT_ADV)
#if defined(CONFIG_BT_BROADCASTER)
static void adv_sent(struct bt_le_ext_adv *adv,
struct bt_le_ext_adv_sent_info *info)
{
shell_print(ctx_shell, "Advertiser[%d] %p sent %d",
bt_le_ext_adv_get_index(adv), adv, info->num_sent);
}
static void adv_scanned(struct bt_le_ext_adv *adv,
struct bt_le_ext_adv_scanned_info *info)
{
char str[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(info->addr, str, sizeof(str));
shell_print(ctx_shell, "Advertiser[%d] %p scanned by %s",
bt_le_ext_adv_get_index(adv), adv, str);
}
#endif /* CONFIG_BT_BROADCASTER */
#if defined(CONFIG_BT_PERIPHERAL)
static void adv_connected(struct bt_le_ext_adv *adv,
struct bt_le_ext_adv_connected_info *info)
{
char str[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(info->conn), str, sizeof(str));
shell_print(ctx_shell, "Advertiser[%d] %p connected by %s",
bt_le_ext_adv_get_index(adv), adv, str);
}
#endif /* CONFIG_BT_PERIPHERAL */
#if defined(CONFIG_BT_PRIVACY)
static bool adv_rpa_expired(struct bt_le_ext_adv *adv)
{
uint8_t adv_index = bt_le_ext_adv_get_index(adv);
bool keep_rpa = atomic_test_bit(adv_set_opt[adv_index],
SHELL_ADV_OPT_KEEP_RPA);
shell_print(ctx_shell, "Advertiser[%d] %p RPA %s",
adv_index, adv,
keep_rpa ? "not expired" : "expired");
#if defined(CONFIG_BT_EAD)
/* EAD must be updated each time the RPA is updated */
if (!keep_rpa) {
ead_update_ad();
}
#endif
return keep_rpa;
}
#endif /* defined(CONFIG_BT_PRIVACY) */
#endif /* CONFIG_BT_EXT_ADV */
#if !defined(CONFIG_BT_CONN)
#if 0 /* FIXME: Add support for changing prompt */
static const char *current_prompt(void)
{
return NULL;
}
#endif
#endif /* !CONFIG_BT_CONN */
#if defined(CONFIG_BT_CONN)
#if 0 /* FIXME: Add support for changing prompt */
static const char *current_prompt(void)
{
static char str[BT_ADDR_LE_STR_LEN + 2];
static struct bt_conn_info info;
if (!default_conn) {
return NULL;
}
if (bt_conn_get_info(default_conn, &info) < 0) {
return NULL;
}
if (info.type != BT_CONN_TYPE_LE) {
return NULL;
}
bt_addr_le_to_str(info.le.dst, str, sizeof(str) - 2);
strcat(str, "> ");
return str;
}
#endif
void conn_addr_str(struct bt_conn *conn, char *addr, size_t len)
{
struct bt_conn_info info;
if (bt_conn_get_info(conn, &info) < 0) {
addr[0] = '\0';
return;
}
switch (info.type) {
#if defined(CONFIG_BT_CLASSIC)
case BT_CONN_TYPE_BR:
bt_addr_to_str(info.br.dst, addr, len);
break;
#endif
case BT_CONN_TYPE_LE:
bt_addr_le_to_str(info.le.dst, addr, len);
break;
default:
break;
}
}
static void print_le_oob(const struct shell *sh, struct bt_le_oob *oob)
{
char addr[BT_ADDR_LE_STR_LEN];
char c[KEY_STR_LEN];
char r[KEY_STR_LEN];
bt_addr_le_to_str(&oob->addr, addr, sizeof(addr));
bin2hex(oob->le_sc_data.c, sizeof(oob->le_sc_data.c), c, sizeof(c));
bin2hex(oob->le_sc_data.r, sizeof(oob->le_sc_data.r), r, sizeof(r));
shell_print(sh, "OOB data:");
shell_print(sh, "%-29s %-32s %-32s", "addr", "random", "confirm");
shell_print(sh, "%29s %32s %32s", addr, r, c);
}
static void connected(struct bt_conn *conn, uint8_t err)
{
char addr[BT_ADDR_LE_STR_LEN];
struct bt_conn_info info;
int info_err;
conn_addr_str(conn, addr, sizeof(addr));
if (err) {
shell_error(ctx_shell, "Failed to connect to %s (0x%02x)", addr,
err);
goto done;
}
shell_print(ctx_shell, "Connected: %s", addr);
info_err = bt_conn_get_info(conn, &info);
if (info_err != 0) {
shell_error(ctx_shell, "Failed to connection information: %d", info_err);
goto done;
}
if (info.role == BT_CONN_ROLE_CENTRAL) {
if (default_conn != NULL) {
bt_conn_unref(default_conn);
}
default_conn = bt_conn_ref(conn);
} else if (info.role == BT_CONN_ROLE_PERIPHERAL) {
if (default_conn == NULL) {
default_conn = bt_conn_ref(conn);
}
}
done:
/* clear connection reference for sec mode 3 pairing */
if (pairing_conn) {
bt_conn_unref(pairing_conn);
pairing_conn = NULL;
}
}
static void disconnected_set_new_default_conn_cb(struct bt_conn *conn, void *user_data)
{
struct bt_conn_info info;
if (default_conn != NULL) {
/* nop */
return;
}
if (bt_conn_get_info(conn, &info) != 0) {
shell_error(ctx_shell, "Unable to get info: conn %p", conn);
return;
}
if (info.state == BT_CONN_STATE_CONNECTED) {
char addr_str[BT_ADDR_LE_STR_LEN];
default_conn = bt_conn_ref(conn);
bt_addr_le_to_str(info.le.dst, addr_str, sizeof(addr_str));
shell_print(ctx_shell, "Selected conn is now: %s", addr_str);
}
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
conn_addr_str(conn, addr, sizeof(addr));
shell_print(ctx_shell, "Disconnected: %s (reason 0x%02x)", addr, reason);
if (default_conn == conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
/* If we are connected to other devices, set one of them as default */
bt_conn_foreach(BT_CONN_TYPE_LE, disconnected_set_new_default_conn_cb, NULL);
}
}
static bool le_param_req(struct bt_conn *conn, struct bt_le_conn_param *param)
{
shell_print(ctx_shell, "LE conn param req: int (0x%04x, 0x%04x) lat %d"
" to %d", param->interval_min, param->interval_max,
param->latency, param->timeout);
return true;
}
static void le_param_updated(struct bt_conn *conn, uint16_t interval,
uint16_t latency, uint16_t timeout)
{
shell_print(ctx_shell, "LE conn param updated: int 0x%04x lat %d "
"to %d", interval, latency, timeout);
}
#if defined(CONFIG_BT_SMP)
static void identity_resolved(struct bt_conn *conn, const bt_addr_le_t *rpa,
const bt_addr_le_t *identity)
{
char addr_identity[BT_ADDR_LE_STR_LEN];
char addr_rpa[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(identity, addr_identity, sizeof(addr_identity));
bt_addr_le_to_str(rpa, addr_rpa, sizeof(addr_rpa));
shell_print(ctx_shell, "Identity resolved %s -> %s", addr_rpa,
addr_identity);
}
#endif
#if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CLASSIC)
static const char *security_err_str(enum bt_security_err err)
{
switch (err) {
case BT_SECURITY_ERR_SUCCESS:
return "Success";
case BT_SECURITY_ERR_AUTH_FAIL:
return "Authentication failure";
case BT_SECURITY_ERR_PIN_OR_KEY_MISSING:
return "PIN or key missing";
case BT_SECURITY_ERR_OOB_NOT_AVAILABLE:
return "OOB not available";
case BT_SECURITY_ERR_AUTH_REQUIREMENT:
return "Authentication requirements";
case BT_SECURITY_ERR_PAIR_NOT_SUPPORTED:
return "Pairing not supported";
case BT_SECURITY_ERR_PAIR_NOT_ALLOWED:
return "Pairing not allowed";
case BT_SECURITY_ERR_INVALID_PARAM:
return "Invalid parameters";
case BT_SECURITY_ERR_UNSPECIFIED:
return "Unspecified";
default:
return "Unknown";
}
}
static void security_changed(struct bt_conn *conn, bt_security_t level,
enum bt_security_err err)
{
char addr[BT_ADDR_LE_STR_LEN];
conn_addr_str(conn, addr, sizeof(addr));
if (!err) {
shell_print(ctx_shell, "Security changed: %s level %u", addr,
level);
} else {
shell_print(ctx_shell, "Security failed: %s level %u "
"reason: %s (%d)",
addr, level, security_err_str(err), err);
}
}
#endif
#if defined(CONFIG_BT_REMOTE_INFO)
static const char *ver_str(uint8_t ver)
{
const char * const str[] = {
"1.0b", "1.1", "1.2", "2.0", "2.1", "3.0", "4.0", "4.1", "4.2",
"5.0", "5.1", "5.2", "5.3", "5.4"
};
if (ver < ARRAY_SIZE(str)) {
return str[ver];
}
return "unknown";
}
static void remote_info_available(struct bt_conn *conn,
struct bt_conn_remote_info *remote_info)
{
struct bt_conn_info info;
bt_conn_get_info(conn, &info);
if (IS_ENABLED(CONFIG_BT_REMOTE_VERSION)) {
shell_print(ctx_shell,
"Remote LMP version %s (0x%02x) subversion 0x%04x "
"manufacturer 0x%04x", ver_str(remote_info->version),
remote_info->version, remote_info->subversion,
remote_info->manufacturer);
}
if (info.type == BT_CONN_TYPE_LE) {
uint8_t features[8];
char features_str[2 * sizeof(features) + 1];
sys_memcpy_swap(features, remote_info->le.features,
sizeof(features));
bin2hex(features, sizeof(features),
features_str, sizeof(features_str));
shell_print(ctx_shell, "LE Features: 0x%s ", features_str);
}
}
#endif /* defined(CONFIG_BT_REMOTE_INFO) */
#if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
void le_data_len_updated(struct bt_conn *conn,
struct bt_conn_le_data_len_info *info)
{
shell_print(ctx_shell,
"LE data len updated: TX (len: %d time: %d)"
" RX (len: %d time: %d)", info->tx_max_len,
info->tx_max_time, info->rx_max_len, info->rx_max_time);
}
#endif
#if defined(CONFIG_BT_USER_PHY_UPDATE)
void le_phy_updated(struct bt_conn *conn,
struct bt_conn_le_phy_info *info)
{
shell_print(ctx_shell, "LE PHY updated: TX PHY %s, RX PHY %s",
phy2str(info->tx_phy), phy2str(info->rx_phy));
}
#endif
#if defined(CONFIG_BT_TRANSMIT_POWER_CONTROL)
void tx_power_report(struct bt_conn *conn,
const struct bt_conn_le_tx_power_report *report)
{
shell_print(ctx_shell, "Tx Power Report: Reason: %s, PHY: %s, Tx Power Level: %d",
tx_power_report_reason2str(report->reason), tx_pwr_ctrl_phy2str(report->phy),
report->tx_power_level);
shell_print(ctx_shell, "Tx Power Level Flag Info: %s, Delta: %d",
tx_power_flag2str(report->tx_power_level_flag), report->delta);
}
#endif
#if defined(CONFIG_BT_PATH_LOSS_MONITORING)
void path_loss_threshold_report(struct bt_conn *conn,
const struct bt_conn_le_path_loss_threshold_report *report)
{
shell_print(ctx_shell, "Path Loss Threshold event: Zone: %s, Path loss dbm: %d",
plm_report_zone_str(report->zone), report->path_loss);
}
#endif
static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
.le_param_req = le_param_req,
.le_param_updated = le_param_updated,
#if defined(CONFIG_BT_SMP)
.identity_resolved = identity_resolved,
#endif
#if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CLASSIC)
.security_changed = security_changed,
#endif
#if defined(CONFIG_BT_REMOTE_INFO)
.remote_info_available = remote_info_available,
#endif
#if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
.le_data_len_updated = le_data_len_updated,
#endif
#if defined(CONFIG_BT_USER_PHY_UPDATE)
.le_phy_updated = le_phy_updated,
#endif
#if defined(CONFIG_BT_TRANSMIT_POWER_CONTROL)
.tx_power_report = tx_power_report,
#endif
#if defined(CONFIG_BT_PATH_LOSS_MONITORING)
.path_loss_threshold_report = path_loss_threshold_report,
#endif
};
#endif /* CONFIG_BT_CONN */
#if defined(CONFIG_BT_OBSERVER)
static struct bt_le_scan_cb scan_callbacks = {
.recv = scan_recv,
.timeout = scan_timeout,
};
#endif /* defined(CONFIG_BT_OBSERVER) */
#if defined(CONFIG_BT_EXT_ADV)
#if defined(CONFIG_BT_BROADCASTER)
static struct bt_le_ext_adv_cb adv_callbacks = {
.sent = adv_sent,
.scanned = adv_scanned,
#if defined(CONFIG_BT_PERIPHERAL)
.connected = adv_connected,
#endif /* CONFIG_BT_PERIPHERAL */
#if defined(CONFIG_BT_PRIVACY)
.rpa_expired = adv_rpa_expired,
#endif /* defined(CONFIG_BT_PRIVACY) */
};
#endif /* CONFIG_BT_BROADCASTER */
#endif /* CONFIG_BT_EXT_ADV */
#if defined(CONFIG_BT_PER_ADV_SYNC)
struct bt_le_per_adv_sync *per_adv_syncs[CONFIG_BT_PER_ADV_SYNC_MAX];
size_t selected_per_adv_sync;
static void per_adv_sync_sync_cb(struct bt_le_per_adv_sync *sync,
struct bt_le_per_adv_sync_synced_info *info)
{
const bool is_past_peer = info->conn != NULL;
char le_addr[BT_ADDR_LE_STR_LEN];
char past_peer[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
if (is_past_peer) {
conn_addr_str(info->conn, past_peer, sizeof(past_peer));
}
shell_print(ctx_shell, "PER_ADV_SYNC[%u]: [DEVICE]: %s synced, "
"Interval 0x%04x (%u us), PHY %s, SD 0x%04X, PAST peer %s",
bt_le_per_adv_sync_get_index(sync), le_addr,
info->interval, BT_CONN_INTERVAL_TO_US(info->interval),
phy2str(info->phy), info->service_data,
is_past_peer ? past_peer : "not present");
if (info->conn) { /* if from PAST */
for (int i = 0; i < ARRAY_SIZE(per_adv_syncs); i++) {
if (!per_adv_syncs[i]) {
per_adv_syncs[i] = sync;
break;
}
}
}
}
static void per_adv_sync_terminated_cb(
struct bt_le_per_adv_sync *sync,
const struct bt_le_per_adv_sync_term_info *info)
{
char le_addr[BT_ADDR_LE_STR_LEN];
for (int i = 0; i < ARRAY_SIZE(per_adv_syncs); i++) {
if (per_adv_syncs[i] == sync) {
per_adv_syncs[i] = NULL;
break;
}
}
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
shell_print(ctx_shell, "PER_ADV_SYNC[%u]: [DEVICE]: %s sync terminated",
bt_le_per_adv_sync_get_index(sync), le_addr);
}
static void per_adv_sync_recv_cb(
struct bt_le_per_adv_sync *sync,
const struct bt_le_per_adv_sync_recv_info *info,
struct net_buf_simple *buf)
{
char le_addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
shell_print(ctx_shell, "PER_ADV_SYNC[%u]: [DEVICE]: %s, tx_power %i, "
"RSSI %i, CTE %u, data length %u",
bt_le_per_adv_sync_get_index(sync), le_addr, info->tx_power,
info->rssi, info->cte_type, buf->len);
}
static void per_adv_sync_biginfo_cb(struct bt_le_per_adv_sync *sync,
const struct bt_iso_biginfo *biginfo)
{
char le_addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(biginfo->addr, le_addr, sizeof(le_addr));
shell_print(ctx_shell, "BIG_INFO PER_ADV_SYNC[%u]: [DEVICE]: %s, sid 0x%02x, num_bis %u, "
"nse 0x%02x, interval 0x%04x (%u us), bn 0x%02x, pto 0x%02x, irc 0x%02x, "
"max_pdu 0x%04x, sdu_interval 0x%04x, max_sdu 0x%04x, phy %s, framing 0x%02x, "
"%sencrypted",
bt_le_per_adv_sync_get_index(sync), le_addr, biginfo->sid, biginfo->num_bis,
biginfo->sub_evt_count, biginfo->iso_interval,
BT_CONN_INTERVAL_TO_US(biginfo->iso_interval), biginfo->burst_number,
biginfo->offset, biginfo->rep_count, biginfo->max_pdu, biginfo->sdu_interval,
biginfo->max_sdu, phy2str(biginfo->phy), biginfo->framing,
biginfo->encryption ? "" : "not ");
}
static struct bt_le_per_adv_sync_cb per_adv_sync_cb = {
.synced = per_adv_sync_sync_cb,
.term = per_adv_sync_terminated_cb,
.recv = per_adv_sync_recv_cb,
.biginfo = per_adv_sync_biginfo_cb,
};
#endif /* CONFIG_BT_PER_ADV_SYNC */
static void bt_ready(int err)
{
if (err) {
shell_error(ctx_shell, "Bluetooth init failed (err %d)", err);
return;
}
shell_print(ctx_shell, "Bluetooth initialized");
if (IS_ENABLED(CONFIG_SETTINGS) && !no_settings_load) {
settings_load();
shell_print(ctx_shell, "Settings Loaded");
}
if (IS_ENABLED(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)) {
bt_le_oob_set_legacy_flag(true);
}
#if defined(CONFIG_BT_OBSERVER)
bt_le_scan_cb_register(&scan_callbacks);
#endif
#if defined(CONFIG_BT_CONN)
default_conn = NULL;
/* Unregister to avoid register repeatedly */
bt_conn_cb_unregister(&conn_callbacks);
bt_conn_cb_register(&conn_callbacks);
#endif /* CONFIG_BT_CONN */
#if defined(CONFIG_BT_PER_ADV_SYNC)
bt_le_per_adv_sync_cb_register(&per_adv_sync_cb);
#endif /* CONFIG_BT_PER_ADV_SYNC */
#if defined(CONFIG_BT_SMP)
bt_conn_auth_info_cb_register(&auth_info_cb);
#endif /* CONFIG_BT_SMP */
}
static int cmd_init(const struct shell *sh, size_t argc, char *argv[])
{
int err;
bool sync = false;
ctx_shell = sh;
for (size_t argn = 1; argn < argc; argn++) {
const char *arg = argv[argn];
if (!strcmp(arg, "no-settings-load")) {
no_settings_load = true;
} else if (!strcmp(arg, "sync")) {
sync = true;
} else {
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}
}
if (sync) {
err = bt_enable(NULL);
bt_ready(err);
} else {
err = bt_enable(bt_ready);
if (err) {
shell_error(sh, "Bluetooth init failed (err %d)",
err);
}
}
return err;
}
static int cmd_disable(const struct shell *sh, size_t argc, char *argv[])
{
return bt_disable();
}
#ifdef CONFIG_SETTINGS
static int cmd_settings_load(const struct shell *sh, size_t argc,
char *argv[])
{
int err;
err = settings_load();
if (err) {
shell_error(sh, "Settings load failed (err %d)", err);
return err;
}
shell_print(sh, "Settings loaded");
return 0;
}
#endif
#if defined(CONFIG_BT_HCI)
static int cmd_hci_cmd(const struct shell *sh, size_t argc, char *argv[])
{
uint8_t ogf;
uint16_t ocf;
struct net_buf *buf = NULL, *rsp;
int err;
static uint8_t hex_data[HCI_CMD_MAX_PARAM];
int hex_data_len;
hex_data_len = 0;
ogf = strtoul(argv[1], NULL, 16);
ocf = strtoul(argv[2], NULL, 16);
if (argc > 3) {
size_t len;
if (strlen(argv[3]) > 2 * HCI_CMD_MAX_PARAM) {
shell_error(sh, "Data field too large\n");
return -ENOEXEC;
}
len = hex2bin(argv[3], strlen(argv[3]), &hex_data[hex_data_len],
sizeof(hex_data) - hex_data_len);
if (!len) {
shell_error(sh, "HCI command illegal data field\n");
return -ENOEXEC;
}
buf = bt_hci_cmd_create(BT_OP(ogf, ocf), len);
if (buf == NULL) {
shell_error(sh, "Unable to allocate HCI buffer");
return -ENOMEM;
}
net_buf_add_mem(buf, hex_data, len);
}
err = bt_hci_cmd_send_sync(BT_OP(ogf, ocf), buf, &rsp);
if (err) {
shell_error(sh, "HCI command failed (err %d)", err);
return err;
} else {
shell_hexdump(sh, rsp->data, rsp->len);
net_buf_unref(rsp);
}
return 0;
}
#endif /* CONFIG_BT_HCI */
static int cmd_name(const struct shell *sh, size_t argc, char *argv[])
{
int err;
if (argc < 2) {
shell_print(sh, "Bluetooth Local Name: %s", bt_get_name());
return 0;
}
err = bt_set_name(argv[1]);
if (err) {
shell_error(sh, "Unable to set name %s (err %d)", argv[1],
err);
return err;
}
return 0;
}
static int cmd_appearance(const struct shell *sh, size_t argc, char *argv[])
{
if (argc == 1) {
shell_print(sh, "Bluetooth Appearance: 0x%04x", bt_get_appearance());
return 0;
}
#if defined(CONFIG_BT_DEVICE_APPEARANCE_DYNAMIC)
uint16_t app;
int err = 0;
const char *val;
val = argv[1];
if (strlen(val) != 6 || strncmp(val, "0x", 2)) {
shell_error(sh, "Argument must be 0x followed by exactly 4 hex digits.");
return -EINVAL;
}
app = shell_strtoul(val, 16, &err);
if (err) {
shell_error(sh, "Argument must be 0x followed by exactly 4 hex digits.");
return -EINVAL;
}
err = bt_set_appearance(app);
if (err) {
shell_error(sh, "bt_set_appearance(0x%04x) failed with err %d", app, err);
return err;
}
#endif /* defined(CONFIG_BT_DEVICE_APPEARANCE_DYNAMIC) */
return 0;
}
static int cmd_id_create(const struct shell *sh, size_t argc, char *argv[])
{
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_t addr;
int err;
if (argc > 1) {
err = bt_addr_le_from_str(argv[1], "random", &addr);
if (err) {
shell_error(sh, "Invalid address");
}
} else {
bt_addr_le_copy(&addr, BT_ADDR_LE_ANY);
}
err = bt_id_create(&addr, NULL);
if (err < 0) {
shell_error(sh, "Creating new ID failed (err %d)", err);
return err;
}
bt_addr_le_to_str(&addr, addr_str, sizeof(addr_str));
shell_print(sh, "New identity (%d) created: %s", err, addr_str);
return 0;
}
static int cmd_id_reset(const struct shell *sh, size_t argc, char *argv[])
{
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_t addr;
uint8_t id;
int err;
if (argc < 2) {
shell_error(sh, "Identity identifier not specified");
return -ENOEXEC;
}
id = strtol(argv[1], NULL, 10);
if (argc > 2) {
err = bt_addr_le_from_str(argv[2], "random", &addr);
if (err) {
shell_print(sh, "Invalid address");
return err;
}
} else {
bt_addr_le_copy(&addr, BT_ADDR_LE_ANY);
}
err = bt_id_reset(id, &addr, NULL);
if (err < 0) {
shell_print(sh, "Resetting ID %u failed (err %d)", id, err);
return err;
}
bt_addr_le_to_str(&addr, addr_str, sizeof(addr_str));
shell_print(sh, "Identity %u reset: %s", id, addr_str);
return 0;
}
static int cmd_id_delete(const struct shell *sh, size_t argc, char *argv[])
{
uint8_t id;
int err;
if (argc < 2) {
shell_error(sh, "Identity identifier not specified");
return -ENOEXEC;
}
id = strtol(argv[1], NULL, 10);
err = bt_id_delete(id);
if (err < 0) {
shell_error(sh, "Deleting ID %u failed (err %d)", id, err);
return err;
}
shell_print(sh, "Identity %u deleted", id);
return 0;
}
static int cmd_id_show(const struct shell *sh, size_t argc, char *argv[])
{
bt_addr_le_t addrs[CONFIG_BT_ID_MAX];
size_t i, count = CONFIG_BT_ID_MAX;
bt_id_get(addrs, &count);
for (i = 0; i < count; i++) {
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(&addrs[i], addr_str, sizeof(addr_str));
shell_print(sh, "%s%zu: %s", i == selected_id ? "*" : " ", i,
addr_str);
}
return 0;
}
static int cmd_id_select(const struct shell *sh, size_t argc, char *argv[])
{
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_t addrs[CONFIG_BT_ID_MAX];
size_t count = CONFIG_BT_ID_MAX;
uint8_t id;
id = strtol(argv[1], NULL, 10);
bt_id_get(addrs, &count);
if (count <= id) {
shell_error(sh, "Invalid identity");
return -ENOEXEC;
}
bt_addr_le_to_str(&addrs[id], addr_str, sizeof(addr_str));
shell_print(sh, "Selected identity: %s", addr_str);
selected_id = id;
return 0;
}
#if defined(CONFIG_BT_OBSERVER)
static int cmd_active_scan_on(const struct shell *sh, uint32_t options,
uint16_t timeout)
{
int err;
struct bt_le_scan_param param = {
.type = BT_LE_SCAN_TYPE_ACTIVE,
.options = BT_LE_SCAN_OPT_NONE,
.interval = BT_GAP_SCAN_FAST_INTERVAL,
.window = BT_GAP_SCAN_FAST_WINDOW,
.timeout = timeout, };
param.options |= options;
err = bt_le_scan_start(¶m, NULL);
if (err) {
shell_error(sh, "Bluetooth set active scan failed "
"(err %d)", err);
return err;
} else {
shell_print(sh, "Bluetooth active scan enabled");
}
return 0;
}
static int cmd_passive_scan_on(const struct shell *sh, uint32_t options,
uint16_t timeout)
{
struct bt_le_scan_param param = {
.type = BT_LE_SCAN_TYPE_PASSIVE,
.options = BT_LE_SCAN_OPT_NONE,
.interval = 0x10,
.window = 0x10,
.timeout = timeout, };
int err;
param.options |= options;
err = bt_le_scan_start(¶m, NULL);
if (err) {
shell_error(sh, "Bluetooth set passive scan failed "
"(err %d)", err);
return err;
} else {
shell_print(sh, "Bluetooth passive scan enabled");
}
return 0;
}
static int cmd_scan_off(const struct shell *sh)
{
int err;
err = bt_le_scan_stop();
if (err) {
shell_error(sh, "Stopping scanning failed (err %d)", err);
return err;
} else {
shell_print(sh, "Scan successfully stopped");
}
return 0;
}
static int cmd_scan(const struct shell *sh, size_t argc, char *argv[])
{
const char *action;
uint32_t options = 0;
uint16_t timeout = 0;
/* Parse duplicate filtering data */
for (size_t argn = 2; argn < argc; argn++) {
const char *arg = argv[argn];
if (!strcmp(arg, "dups")) {
options |= BT_LE_SCAN_OPT_FILTER_DUPLICATE;
} else if (!strcmp(arg, "nodups")) {
options &= ~BT_LE_SCAN_OPT_FILTER_DUPLICATE;
} else if (!strcmp(arg, "fal")) {
options |= BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST;
} else if (!strcmp(arg, "coded")) {
options |= BT_LE_SCAN_OPT_CODED;
} else if (!strcmp(arg, "no-1m")) {
options |= BT_LE_SCAN_OPT_NO_1M;
} else if (!strcmp(arg, "timeout")) {
if (++argn == argc) {
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}
timeout = strtoul(argv[argn], NULL, 16);
} else {
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}
}
action = argv[1];
if (!strcmp(action, "on")) {
return cmd_active_scan_on(sh, options, timeout);
} else if (!strcmp(action, "off")) {
return cmd_scan_off(sh);
} else if (!strcmp(action, "passive")) {
return cmd_passive_scan_on(sh, options, timeout);
} else {
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}
return 0;
}
static int cmd_scan_verbose_output(const struct shell *sh, size_t argc, char *argv[])
{
const char *verbose_state;
verbose_state = argv[1];
if (!strcmp(verbose_state, "on")) {
scan_verbose_output = true;
} else if (!strcmp(verbose_state, "off")) {
scan_verbose_output = false;
} else {
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}
return 0;
}
static int cmd_scan_filter_set_name(const struct shell *sh, size_t argc,
char *argv[])
{
const char *name_arg = argv[1];
if (strlen(name_arg) >= sizeof(scan_filter.name)) {
shell_error(ctx_shell, "Name is too long (max %zu): %s\n",
sizeof(scan_filter.name), name_arg);
return -ENOEXEC;
}
strcpy(scan_filter.name, name_arg);
scan_filter.name_set = true;
return 0;
}
static int cmd_scan_filter_set_addr(const struct shell *sh, size_t argc,
char *argv[])
{
const size_t max_cpy_len = sizeof(scan_filter.addr) - 1;
const char *addr_arg = argv[1];
/* Validate length including null terminator. */
if (strlen(addr_arg) > max_cpy_len) {
shell_error(ctx_shell, "Invalid address string: %s\n",
addr_arg);
return -ENOEXEC;
}
/* Validate input to check if valid (subset of) BT address */
for (size_t i = 0; i < strlen(addr_arg); i++) {
const char c = addr_arg[i];
uint8_t tmp;
if (c != ':' && char2hex(c, &tmp) < 0) {
shell_error(ctx_shell,
"Invalid address string: %s\n",
addr_arg);
return -ENOEXEC;
}
}
strncpy(scan_filter.addr, addr_arg, max_cpy_len);
scan_filter.addr[max_cpy_len] = '\0'; /* ensure NULL termination */
scan_filter.addr_set = true;
return 0;
}
static int cmd_scan_filter_set_rssi(const struct shell *sh, size_t argc, char *argv[])
{
int err = 0;
long rssi;
rssi = shell_strtol(argv[1], 10, &err);
if (!err) {
if (IN_RANGE(rssi, INT8_MIN, INT8_MAX)) {
scan_filter.rssi = (int8_t)rssi;
scan_filter.rssi_set = true;
shell_print(sh, "RSSI cutoff set at %d dB", scan_filter.rssi);
return 0;
}
shell_print(sh, "value out of bounds (%d to %d)", INT8_MIN, INT8_MAX);
err = -ERANGE;
}
shell_print(sh, "error %d", err);
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}
static int cmd_scan_filter_set_pa_interval(const struct shell *sh, size_t argc,
char *argv[])
{
unsigned long pa_interval;
int err = 0;
pa_interval = shell_strtoul(argv[1], 10, &err);
if (!err) {
if (IN_RANGE(pa_interval,
BT_GAP_PER_ADV_MIN_INTERVAL,
BT_GAP_PER_ADV_MAX_INTERVAL)) {
scan_filter.pa_interval = (uint16_t)pa_interval;
scan_filter.pa_interval_set = true;
shell_print(sh, "PA interval cutoff set at %u",
scan_filter.pa_interval);
return 0;
}
shell_print(sh, "value out of bounds (%d to %d)",
BT_GAP_PER_ADV_MIN_INTERVAL,
BT_GAP_PER_ADV_MAX_INTERVAL);
err = -ERANGE;
}
shell_print(sh, "error %d", err);
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}
static int cmd_scan_filter_clear_all(const struct shell *sh, size_t argc,
char *argv[])
{
(void)memset(&scan_filter, 0, sizeof(scan_filter));
return 0;
}
static int cmd_scan_filter_clear_name(const struct shell *sh, size_t argc,
char *argv[])
{
(void)memset(scan_filter.name, 0, sizeof(scan_filter.name));
scan_filter.name_set = false;
return 0;
}
static int cmd_scan_filter_clear_addr(const struct shell *sh, size_t argc,
char *argv[])
{
(void)memset(scan_filter.addr, 0, sizeof(scan_filter.addr));
scan_filter.addr_set = false;
return 0;
}
#endif /* CONFIG_BT_OBSERVER */
#if defined(CONFIG_BT_BROADCASTER)
static ssize_t ad_init(struct bt_data *data_array, const size_t data_array_size,
const atomic_t *adv_options)
{
const bool discoverable = atomic_test_bit(adv_options, SHELL_ADV_OPT_DISCOVERABLE);
const bool appearance = atomic_test_bit(adv_options, SHELL_ADV_OPT_APPEARANCE);
const bool adv_ext = atomic_test_bit(adv_options, SHELL_ADV_OPT_EXT_ADV);
static uint8_t ad_flags;
size_t ad_len = 0;
/* Set BR/EDR Not Supported if LE-only device */
ad_flags = IS_ENABLED(CONFIG_BT_CLASSIC) ? 0 : BT_LE_AD_NO_BREDR;
if (discoverable) {
/* A privacy-enabled Set Member should advertise RSI values only when in
* the GAP Limited Discoverable mode.
*/
if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
IS_ENABLED(CONFIG_BT_CSIP_SET_MEMBER) &&
svc_inst != NULL) {
ad_flags |= BT_LE_AD_LIMITED;
} else {
ad_flags |= BT_LE_AD_GENERAL;
}
}
if (ad_flags != 0) {
__ASSERT(data_array_size > ad_len, "No space for AD_FLAGS");
data_array[ad_len].type = BT_DATA_FLAGS;
data_array[ad_len].data_len = sizeof(ad_flags);
data_array[ad_len].data = &ad_flags;
ad_len++;
}
if (appearance) {
const uint16_t appearance2 = bt_get_appearance();
static uint8_t appearance_data[sizeof(appearance2)];
__ASSERT(data_array_size > ad_len, "No space for appearance");
sys_put_le16(appearance2, appearance_data);
data_array[ad_len].type = BT_DATA_GAP_APPEARANCE;
data_array[ad_len].data_len = sizeof(appearance_data);
data_array[ad_len].data = appearance_data;
ad_len++;
}
if (IS_ENABLED(CONFIG_BT_CSIP_SET_MEMBER)) {
ssize_t csis_ad_len;
csis_ad_len = csis_ad_data_add(&data_array[ad_len],
data_array_size - ad_len, discoverable);
if (csis_ad_len < 0) {
shell_error(ctx_shell, "Failed to add CSIS data (err %d)", csis_ad_len);
return ad_len;
}
ad_len += csis_ad_len;
}
if (IS_ENABLED(CONFIG_BT_AUDIO) && IS_ENABLED(CONFIG_BT_EXT_ADV) && adv_ext) {
const bool connectable = atomic_test_bit(adv_options, SHELL_ADV_OPT_CONNECTABLE);
ssize_t audio_ad_len;
audio_ad_len = audio_ad_data_add(&data_array[ad_len], data_array_size - ad_len,
discoverable, connectable);
if (audio_ad_len < 0) {
return audio_ad_len;
}
ad_len += audio_ad_len;
}
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[4];
struct bt_data sd[4];
bool discoverable = true;
bool appearance = false;
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) {
shell_error(sh, "Failed to stop advertising");
return -ENOEXEC;
} else {
shell_print(sh, "Advertising stopped");
}
return 0;
}
param.id = selected_id;
param.interval_min = BT_GAP_ADV_FAST_INT_MIN_2;
param.interval_max = BT_GAP_ADV_FAST_INT_MAX_2;
if (!strcmp(argv[1], "on")) {
param.options = BT_LE_ADV_OPT_CONNECTABLE;
} else if (!strcmp(argv[1], "nconn")) {
param.options = 0U;
} else {
goto fail;
}
for (size_t argn = 2; argn < argc; argn++) {
const char *arg = argv[argn];
if (!strcmp(arg, "discov")) {
discoverable = true;
} else if (!strcmp(arg, "non_discov")) {
discoverable = false;
} else if (!strcmp(arg, "appearance")) {
appearance = true;
} else if (!strcmp(arg, "fal")) {
param.options |= BT_LE_ADV_OPT_FILTER_SCAN_REQ;
param.options |= BT_LE_ADV_OPT_FILTER_CONN;
} else if (!strcmp(arg, "fal-scan")) {
param.options |= BT_LE_ADV_OPT_FILTER_SCAN_REQ;
} else if (!strcmp(arg, "fal-conn")) {
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, "no-name")) {
with_name = false;
} else if (!strcmp(arg, "name-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")) {
param.options |= BT_LE_ADV_OPT_DISABLE_CHAN_37;
} else if (!strcmp(arg, "disable-38")) {
param.options |= BT_LE_ADV_OPT_DISABLE_CHAN_38;
} else if (!strcmp(arg, "disable-39")) {
param.options |= BT_LE_ADV_OPT_DISABLE_CHAN_39;
} else {
goto fail;
}
}
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);
err = ad_init(&ad[ad_len], ARRAY_SIZE(ad) - ad_len, adv_opt);
if (err < 0) {
return -ENOEXEC;
}
ad_len += err;
err = bt_le_adv_start(¶m, 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);
return err;
} else {
shell_print(sh, "Advertising started");
}
return 0;
fail:
shell_help(sh);
return -ENOEXEC;
}
#if defined(CONFIG_BT_PERIPHERAL)
static int cmd_directed_adv(const struct shell *sh,
size_t argc, char *argv[])
{
int err;
bt_addr_le_t addr;
struct bt_le_adv_param param;
err = bt_addr_le_from_str(argv[1], argv[2], &addr);
param = *BT_LE_ADV_CONN_DIR(&addr);
if (err) {
shell_error(sh, "Invalid peer address (err %d)", err);
return err;
}
for (size_t argn = 3; argn < argc; argn++) {
const char *arg = argv[argn];
if (!strcmp(arg, "low")) {
param.options |= BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY;
param.interval_max = BT_GAP_ADV_FAST_INT_MAX_2;
param.interval_min = BT_GAP_ADV_FAST_INT_MIN_2;
} else if (!strcmp(arg, "identity")) {
param.options |= BT_LE_ADV_OPT_USE_IDENTITY;
} else if (!strcmp(arg, "dir-rpa")) {
param.options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
} else if (!strcmp(arg, "disable-37")) {
param.options |= BT_LE_ADV_OPT_DISABLE_CHAN_37;
} else if (!strcmp(arg, "disable-38")) {
param.options |= BT_LE_ADV_OPT_DISABLE_CHAN_38;
} else if (!strcmp(arg, "disable-39")) {
param.options |= BT_LE_ADV_OPT_DISABLE_CHAN_39;
} else {
shell_help(sh);
return -ENOEXEC;
}
}
err = bt_le_adv_start(¶m, NULL, 0, NULL, 0);
if (err) {
shell_error(sh, "Failed to start directed advertising (%d)",
err);
return -ENOEXEC;
} else {
shell_print(sh, "Started directed advertising");
}
return 0;
}
#endif /* CONFIG_BT_PERIPHERAL */
#if defined(CONFIG_BT_EXT_ADV)
static bool adv_param_parse(size_t argc, char *argv[],
struct bt_le_adv_param *param)
{
memset(param, 0, sizeof(struct bt_le_adv_param));
if (!strcmp(argv[1], "conn-scan")) {
param->options |= BT_LE_ADV_OPT_CONNECTABLE;
param->options |= BT_LE_ADV_OPT_SCANNABLE;
} else if (!strcmp(argv[1], "conn-nscan")) {
param->options |= BT_LE_ADV_OPT_CONNECTABLE;
} else if (!strcmp(argv[1], "nconn-scan")) {
param->options |= BT_LE_ADV_OPT_SCANNABLE;
} else if (!strcmp(argv[1], "nconn-nscan")) {
/* Acceptable option, nothing to do */
} else {
return false;
}
for (size_t argn = 2; argn < argc; argn++) {
const char *arg = argv[argn];
if (!strcmp(arg, "ext-adv")) {
param->options |= BT_LE_ADV_OPT_EXT_ADV;
} else if (!strcmp(arg, "coded")) {
param->options |= BT_LE_ADV_OPT_CODED;
} else if (!strcmp(arg, "no-2m")) {
param->options |= BT_LE_ADV_OPT_NO_2M;
} else if (!strcmp(arg, "anon")) {
param->options |= BT_LE_ADV_OPT_ANONYMOUS;
} else if (!strcmp(arg, "tx-power")) {
param->options |= BT_LE_ADV_OPT_USE_TX_POWER;
} else if (!strcmp(arg, "scan-reports")) {
param->options |= BT_LE_ADV_OPT_NOTIFY_SCAN_REQ;
} else if (!strcmp(arg, "fal")) {
param->options |= BT_LE_ADV_OPT_FILTER_SCAN_REQ;
param->options |= BT_LE_ADV_OPT_FILTER_CONN;
} else if (!strcmp(arg, "fal-scan")) {
param->options |= BT_LE_ADV_OPT_FILTER_SCAN_REQ;
} else if (!strcmp(arg, "fal-conn")) {
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, "low")) {
param->options |= BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY;
} else if (!strcmp(arg, "dir-rpa")) {
param->options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
} else if (!strcmp(arg, "disable-37")) {
param->options |= BT_LE_ADV_OPT_DISABLE_CHAN_37;
} else if (!strcmp(arg, "disable-38")) {
param->options |= BT_LE_ADV_OPT_DISABLE_CHAN_38;
} else if (!strcmp(arg, "disable-39")) {
param->options |= BT_LE_ADV_OPT_DISABLE_CHAN_39;
} else if (!strcmp(arg, "directed")) {
static bt_addr_le_t addr;
if ((argn + 2) >= argc) {
return false;
}
if (bt_addr_le_from_str(argv[argn + 1], argv[argn + 2],
&addr)) {
return false;
}
param->peer = &addr;
argn += 2;
} else {
return false;
}
}
param->id = selected_id;
param->sid = 0;
if (param->peer &&
!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
param->interval_min = 0;
param->interval_max = 0;
} else {
param->interval_min = BT_GAP_ADV_FAST_INT_MIN_2;
param->interval_max = BT_GAP_ADV_FAST_INT_MAX_2;
}
return true;
}
static int cmd_adv_create(const struct shell *sh, size_t argc, char *argv[])
{
struct bt_le_adv_param param;
struct bt_le_ext_adv *adv;
uint8_t adv_index;
int err;
if (!adv_param_parse(argc, argv, ¶m)) {
shell_help(sh);
return -ENOEXEC;
}
err = bt_le_ext_adv_create(¶m, &adv_callbacks, &adv);
if (err) {
shell_error(sh, "Failed to create advertiser set (%d)", err);
return -ENOEXEC;
}
adv_index = bt_le_ext_adv_get_index(adv);
adv_sets[adv_index] = adv;
atomic_clear(adv_set_opt[adv_index]);
atomic_set_bit_to(adv_set_opt[adv_index], SHELL_ADV_OPT_CONNECTABLE,
(param.options & BT_LE_ADV_OPT_CONNECTABLE) > 0);
atomic_set_bit_to(adv_set_opt[adv_index], SHELL_ADV_OPT_EXT_ADV,
(param.options & BT_LE_ADV_OPT_EXT_ADV) > 0);