diff --git a/subsys/bluetooth/host/shell/CMakeLists.txt b/subsys/bluetooth/host/shell/CMakeLists.txt index 98f1e6ac2a590d..73ff18086f3152 100644 --- a/subsys/bluetooth/host/shell/CMakeLists.txt +++ b/subsys/bluetooth/host/shell/CMakeLists.txt @@ -1,7 +1,10 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_library() -zephyr_library_sources(bt.c) +zephyr_library_sources( + bt.c + bt_shell_private.c + ) zephyr_library_sources_ifdef(CONFIG_BT_CONN gatt.c) zephyr_library_sources_ifdef(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL l2cap.c) zephyr_library_sources_ifdef(CONFIG_BT_ISO iso.c) diff --git a/subsys/bluetooth/host/shell/bt.c b/subsys/bluetooth/host/shell/bt.c index e53e8123aa0d66..d21b10171ea627 100644 --- a/subsys/bluetooth/host/shell/bt.c +++ b/subsys/bluetooth/host/shell/bt.c @@ -35,6 +35,7 @@ #include #include +#include "bt_shell_private.h" #include "audio/shell/audio.h" #include "controller/ll_sw/shell/ll.h" @@ -125,8 +126,8 @@ static void print_le_addr(const char *desc, const bt_addr_le_t *addr) bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); - shell_print(ctx_shell, "%s address: %s (%s)", desc, addr_str, - addr_desc); + bt_shell_print("%s address: %s (%s)", desc, addr_str, + addr_desc); } #endif /* CONFIG_BT_CONN || (CONFIG_BT_BROADCASTER && CONFIG_BT_EXT_ADV) */ @@ -210,10 +211,9 @@ static const char *plm_report_zone_str(enum bt_conn_le_path_loss_zone zone) #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 void bt_do_scan_filter_clear_name(void); +static int bt_do_scan_off(void); +static int bt_do_connect_le(int *ercd, size_t argc, char *argv[]); static struct bt_auto_connect { bt_addr_le_t addr; @@ -227,18 +227,17 @@ static void active_scan_timeout(struct k_work *work) { int err; - shell_print(ctx_shell, "Scan timeout"); + bt_shell_print("Scan timeout"); err = bt_le_scan_stop(); if (err) { - shell_error(ctx_shell, "Failed to stop scan (err %d)", err); + bt_shell_error("Failed to stop scan (err %d)", err); } #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" }); + bt_do_scan_filter_clear_name(); } #endif /* CONFIG_BT_CENTRAL */ } @@ -326,12 +325,12 @@ static void print_data_hex(const uint8_t *data, uint8_t len, enum shell_vt100_co return; } - shell_fprintf(ctx_shell, color, "0x"); + bt_shell_fprintf(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]); + bt_shell_fprintf(color, "%02x", data[i]); } } @@ -346,7 +345,7 @@ static void print_data_set(uint8_t set_value_len, do { if (idx > 0) { - shell_fprintf(ctx_shell, SHELL_INFO, ADV_DATA_DELIMITER); + bt_shell_fprintf_info(ADV_DATA_DELIMITER); } print_data_hex(&scan_data[idx], set_value_len, SHELL_INFO); @@ -354,15 +353,15 @@ static void print_data_set(uint8_t set_value_len, } while (idx + set_value_len <= scan_data_len); if (idx < scan_data_len) { - shell_fprintf(ctx_shell, SHELL_WARNING, " Excess data: "); + bt_shell_fprintf_warn(" 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); + bt_shell_fprintf_info("%*sType 0x%02x: ", + strlen(scan_response_label), "", data->type); switch (data->type) { case BT_DATA_UUID16_SOME: @@ -375,14 +374,14 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) * 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); + bt_shell_fprintf_warn( + "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); + bt_shell_fprintf_info(ADV_DATA_DELIMITER); print_data_set(1, data->data + BT_UUID_SIZE_16, data->data_len - BT_UUID_SIZE_16); } @@ -396,14 +395,14 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) * 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); + bt_shell_fprintf_warn( + "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); + bt_shell_fprintf_info(ADV_DATA_DELIMITER); print_data_set(1, data->data + BT_UUID_SIZE_32, data->data_len - BT_UUID_SIZE_32); } @@ -418,14 +417,14 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) * 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); + bt_shell_fprintf_warn( + "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); + bt_shell_fprintf_info(ADV_DATA_DELIMITER); print_data_set(1, data->data + BT_UUID_SIZE_128, data->data_len - BT_UUID_SIZE_128); } @@ -433,7 +432,7 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) 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); + bt_shell_fprintf_info("%.*s", data->data_len, data->data); break; case BT_DATA_PUB_TARGET_ADDR: case BT_DATA_RAND_TARGET_ADDR: @@ -444,13 +443,13 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) 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: "); + bt_shell_fprintf_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), ""); + bt_shell_fprintf_info("\n%*s[START DECRYPTED DATA]\n", + strlen(scan_response_label), ""); int ead_err; struct net_buf_simple decrypted_buf; @@ -460,7 +459,7 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) 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); + bt_shell_error("Error during decryption (err %d)", ead_err); } net_buf_simple_init_with_data(&decrypted_buf, &decrypted_data[0], @@ -468,8 +467,8 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) bt_data_parse(&decrypted_buf, &data_verbose_cb, user_data); - shell_fprintf(ctx_shell, SHELL_INFO, "%*s[END DECRYPTED DATA]", - strlen(scan_response_label), ""); + bt_shell_fprintf_info("%*s[END DECRYPTED DATA]", + strlen(scan_response_label), ""); #endif } break; @@ -477,7 +476,7 @@ static bool data_verbose_cb(struct bt_data *data, void *user_data) print_data_set(1, data->data, data->data_len); } - shell_fprintf(ctx_shell, SHELL_INFO, "\n"); + bt_shell_fprintf_info("\n"); return true; } @@ -560,27 +559,26 @@ static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_si 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); + bt_shell_print("%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_shell_info("%*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), ""); + bt_shell_info("%*s[SCAN DATA END]", strlen(scan_response_label), ""); } #if defined(CONFIG_BT_CENTRAL) @@ -595,15 +593,16 @@ static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_si /* Use the above auto_connect.addr address to automatically connect */ if (auto_connect.connect_name) { + __maybe_unused int ercd; + auto_connect.connect_name = false; - cmd_scan_off(ctx_shell); + bt_do_scan_off(); - /* "name" is what would be in argv[0] normally */ - cmd_scan_filter_clear_name(ctx_shell, 1, (char *[]){"name"}); + bt_do_scan_filter_clear_name(); /* "connect" is what would be in argv[0] normally */ - cmd_connect_le(ctx_shell, 1, (char *[]){"connect"}); + bt_do_connect_le(&ercd, 1, (char *[]){"connect"}); } } else { bt_conn_unref(conn); @@ -614,7 +613,7 @@ static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_si static void scan_timeout(void) { - shell_print(ctx_shell, "Scan timeout"); + bt_shell_print("Scan timeout"); } #endif /* CONFIG_BT_OBSERVER */ @@ -623,8 +622,8 @@ static void scan_timeout(void) 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); + bt_shell_print("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, @@ -634,8 +633,8 @@ static void adv_scanned(struct bt_le_ext_adv *adv, 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); + bt_shell_print("Advertiser[%d] %p scanned by %s", + bt_le_ext_adv_get_index(adv), adv, str); } #endif /* CONFIG_BT_BROADCASTER */ @@ -647,8 +646,8 @@ static void adv_connected(struct bt_le_ext_adv *adv, 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); + bt_shell_print("Advertiser[%d] %p connected by %s", + bt_le_ext_adv_get_index(adv), adv, str); } #endif /* CONFIG_BT_PERIPHERAL */ @@ -659,9 +658,9 @@ static bool adv_rpa_expired(struct bt_le_ext_adv *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"); + bt_shell_print("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 */ @@ -758,16 +757,16 @@ static void connected(struct bt_conn *conn, uint8_t err) conn_addr_str(conn, addr, sizeof(addr)); if (err) { - shell_error(ctx_shell, "Failed to connect to %s 0x%02x %s", addr, - err, bt_hci_err_to_str(err)); + bt_shell_error("Failed to connect to %s 0x%02x %s", addr, + err, bt_hci_err_to_str(err)); goto done; } - shell_print(ctx_shell, "Connected: %s", addr); + bt_shell_print("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); + bt_shell_error("Failed to connection information: %d", info_err); goto done; } @@ -801,7 +800,7 @@ static void disconnected_set_new_default_conn_cb(struct bt_conn *conn, void *use } if (bt_conn_get_info(conn, &info) != 0) { - shell_error(ctx_shell, "Unable to get info: conn %p", conn); + bt_shell_error("Unable to get info: conn %p", conn); return; } @@ -811,7 +810,7 @@ static void disconnected_set_new_default_conn_cb(struct bt_conn *conn, void *use 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); + bt_shell_print("Selected conn is now: %s", addr_str); } } @@ -820,7 +819,7 @@ 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); + bt_shell_print("Disconnected: %s (reason 0x%02x)", addr, reason); if (default_conn == conn) { bt_conn_unref(default_conn); @@ -833,9 +832,9 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) 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); + bt_shell_print("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; } @@ -843,8 +842,8 @@ static bool le_param_req(struct bt_conn *conn, struct bt_le_conn_param *param) 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); + bt_shell_print("LE conn param updated: int 0x%04x lat %d " + "to %d", interval, latency, timeout); } #if defined(CONFIG_BT_SMP) @@ -857,8 +856,8 @@ static void identity_resolved(struct bt_conn *conn, const bt_addr_le_t *rpa, 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); + bt_shell_print("Identity resolved %s -> %s", addr_rpa, + addr_identity); } #endif @@ -897,12 +896,12 @@ static void security_changed(struct bt_conn *conn, bt_security_t level, conn_addr_str(conn, addr, sizeof(addr)); if (!err) { - shell_print(ctx_shell, "Security changed: %s level %u", addr, - level); + bt_shell_print("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); + bt_shell_print("Security failed: %s level %u " + "reason: %s (%d)", + addr, level, security_err_str(err), err); } } #endif @@ -916,11 +915,10 @@ static void remote_info_available(struct bt_conn *conn, 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", bt_hci_get_ver_str(remote_info->version), - remote_info->version, remote_info->subversion, - remote_info->manufacturer); + bt_shell_print("Remote LMP version %s (0x%02x) subversion 0x%04x " + "manufacturer 0x%04x", bt_hci_get_ver_str(remote_info->version), + remote_info->version, remote_info->subversion, + remote_info->manufacturer); } if (info.type == BT_CONN_TYPE_LE) { @@ -931,7 +929,7 @@ static void remote_info_available(struct bt_conn *conn, sizeof(features)); bin2hex(features, sizeof(features), features_str, sizeof(features_str)); - shell_print(ctx_shell, "LE Features: 0x%s ", features_str); + bt_shell_print("LE Features: 0x%s ", features_str); } } #endif /* defined(CONFIG_BT_REMOTE_INFO) */ @@ -940,10 +938,9 @@ static void remote_info_available(struct bt_conn *conn, 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); + bt_shell_print("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 @@ -951,8 +948,8 @@ void le_data_len_updated(struct bt_conn *conn, 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)); + bt_shell_print("LE PHY updated: TX PHY %s, RX PHY %s", + phy2str(info->tx_phy), phy2str(info->rx_phy)); } #endif @@ -960,11 +957,11 @@ void le_phy_updated(struct bt_conn *conn, 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); + bt_shell_print("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); + bt_shell_print("Tx Power Level Flag Info: %s, Delta: %d", + tx_power_flag2str(report->tx_power_level_flag), report->delta); } #endif @@ -972,8 +969,8 @@ void tx_power_report(struct bt_conn *conn, 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); + bt_shell_print("Path Loss Threshold event: Zone: %s, Path loss dbm: %d", + plm_report_zone_str(report->zone), report->path_loss); } #endif @@ -982,18 +979,18 @@ void subrate_changed(struct bt_conn *conn, const struct bt_conn_le_subrate_changed *params) { if (params->status == BT_HCI_ERR_SUCCESS) { - shell_print(ctx_shell, "Subrate parameters changed: " - "Subrate Factor: %d " - "Continuation Number: %d " - "Peripheral latency: 0x%04x " - "Supervision timeout: 0x%04x (%d ms)", - params->factor, - params->continuation_number, - params->peripheral_latency, - params->supervision_timeout, - params->supervision_timeout * 10); + bt_shell_print("Subrate parameters changed: " + "Subrate Factor: %d " + "Continuation Number: %d " + "Peripheral latency: 0x%04x " + "Supervision timeout: 0x%04x (%d ms)", + params->factor, + params->continuation_number, + params->peripheral_latency, + params->supervision_timeout, + params->supervision_timeout * 10); } else { - shell_print(ctx_shell, "Subrate change failed (HCI status 0x%02x)", params->status); + bt_shell_print("Subrate change failed (HCI status 0x%02x)", params->status); } } #endif @@ -1001,8 +998,7 @@ void subrate_changed(struct bt_conn *conn, #if defined(CONFIG_BT_CHANNEL_SOUNDING) void print_remote_cs_capabilities(struct bt_conn *conn, struct bt_conn_le_cs_capabilities *params) { - shell_print( - ctx_shell, + bt_shell_print( "Received remote channel sounding capabilities:\n" "- Num CS configurations: %d\n" "- Max consecutive CS procedures: %d\n" @@ -1065,8 +1061,8 @@ void print_remote_cs_capabilities(struct bt_conn *conn, struct bt_conn_le_cs_cap void print_remote_cs_fae_table(struct bt_conn *conn, struct bt_conn_le_cs_fae_table *params) { - shell_print(ctx_shell, "Received FAE Table: "); - shell_hexdump(ctx_shell, params->remote_fae_table, 72); + bt_shell_print("Received FAE Table: "); + bt_shell_hexdump(params->remote_fae_table, 72); } static void le_cs_config_created(struct bt_conn *conn, struct bt_conn_le_cs_config *config) @@ -1091,40 +1087,40 @@ static void le_cs_config_created(struct bt_conn *conn, struct bt_conn_le_cs_conf uint8_t chsel_type_idx = MIN(config->channel_selection_type, 2); uint8_t ch3c_shape_idx = MIN(config->ch3c_shape, 2); - shell_print(ctx_shell, - "New CS config created:\n" - "- ID: %d\n" - "- Role: %s\n" - "- Main mode: %s\n" - "- Sub mode: %s\n" - "- RTT type: %s\n" - "- Main mode steps: %d - %d\n" - "- Main mode repetition: %d\n" - "- Mode 0 steps: %d\n" - "- CS sync PHY: %s\n" - "- T_IP1 time: %d\n" - "- T_IP2 time: %d\n" - "- T_FCS time: %d\n" - "- T_PM time: %d\n" - "- Channel map: 0x%08X%08X%04X\n" - "- Channel map repetition: %d\n" - "- Channel selection type: %s\n" - "- Ch3c shape: %s\n" - "- Ch3c jump: %d\n", - config->id, role_str[role_idx], mode_str[main_mode_idx], mode_str[sub_mode_idx], - rtt_type_str[rtt_type_idx], config->min_main_mode_steps, - config->max_main_mode_steps, config->main_mode_repetition, config->mode_0_steps, - phy_str[phy_idx], config->t_ip1_time_us, config->t_ip2_time_us, - config->t_fcs_time_us, config->t_pm_time_us, - sys_get_le32(&config->channel_map[6]), sys_get_le32(&config->channel_map[2]), - sys_get_le16(&config->channel_map[0]), config->channel_map_repetition, - chsel_type_str[chsel_type_idx], ch3c_shape_str[ch3c_shape_idx], - config->ch3c_jump); + bt_shell_print( + "New CS config created:\n" + "- ID: %d\n" + "- Role: %s\n" + "- Main mode: %s\n" + "- Sub mode: %s\n" + "- RTT type: %s\n" + "- Main mode steps: %d - %d\n" + "- Main mode repetition: %d\n" + "- Mode 0 steps: %d\n" + "- CS sync PHY: %s\n" + "- T_IP1 time: %d\n" + "- T_IP2 time: %d\n" + "- T_FCS time: %d\n" + "- T_PM time: %d\n" + "- Channel map: 0x%08X%08X%04X\n" + "- Channel map repetition: %d\n" + "- Channel selection type: %s\n" + "- Ch3c shape: %s\n" + "- Ch3c jump: %d\n", + config->id, role_str[role_idx], mode_str[main_mode_idx], mode_str[sub_mode_idx], + rtt_type_str[rtt_type_idx], config->min_main_mode_steps, + config->max_main_mode_steps, config->main_mode_repetition, config->mode_0_steps, + phy_str[phy_idx], config->t_ip1_time_us, config->t_ip2_time_us, + config->t_fcs_time_us, config->t_pm_time_us, + sys_get_le32(&config->channel_map[6]), sys_get_le32(&config->channel_map[2]), + sys_get_le16(&config->channel_map[0]), config->channel_map_repetition, + chsel_type_str[chsel_type_idx], ch3c_shape_str[ch3c_shape_idx], + config->ch3c_jump); } static void le_cs_config_removed(struct bt_conn *conn, uint8_t config_id) { - shell_print(ctx_shell, "CS config %d is removed", config_id); + bt_shell_print("CS config %d is removed", config_id); } #endif @@ -1207,12 +1203,12 @@ static void per_adv_sync_sync_cb(struct bt_le_per_adv_sync *sync, 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"); + bt_shell_print("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++) { @@ -1238,8 +1234,8 @@ static void per_adv_sync_terminated_cb( } 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); + bt_shell_print("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( @@ -1250,10 +1246,10 @@ static void per_adv_sync_recv_cb( 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); + bt_shell_print("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, @@ -1262,16 +1258,16 @@ static void per_adv_sync_biginfo_cb(struct bt_le_per_adv_sync *sync, 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 "); + bt_shell_print("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 = { @@ -1285,15 +1281,15 @@ static struct bt_le_per_adv_sync_cb per_adv_sync_cb = { static void bt_ready(int err) { if (err) { - shell_error(ctx_shell, "Bluetooth init failed (err %d)", err); + bt_shell_error("Bluetooth init failed (err %d)", err); return; } - shell_print(ctx_shell, "Bluetooth initialized"); + bt_shell_print("Bluetooth initialized"); if (IS_ENABLED(CONFIG_SETTINGS) && !no_settings_load) { settings_load(); - shell_print(ctx_shell, "Settings Loaded"); + bt_shell_print("Settings Loaded"); } if (IS_ENABLED(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)) { @@ -1580,7 +1576,7 @@ static int cmd_id_show(const struct shell *sh, size_t argc, char *argv[]) bt_addr_le_to_str(&addrs[i], addr_str, sizeof(addr_str)); shell_print(sh, "%s%zu: %s", i == selected_id ? "*" : " ", i, - addr_str); + addr_str); } return 0; @@ -1625,7 +1621,7 @@ static int cmd_active_scan_on(const struct shell *sh, uint32_t options, err = bt_le_scan_start(¶m, NULL); if (err) { shell_error(sh, "Bluetooth set active scan failed " - "(err %d)", err); + "(err %d)", err); return err; } else { shell_print(sh, "Bluetooth active scan enabled"); @@ -1664,7 +1660,7 @@ static int cmd_passive_scan_on(const struct shell *sh, uint32_t options, return 0; } -static int cmd_scan_off(const struct shell *sh) +static int bt_do_scan_off(void) { int err; @@ -1672,6 +1668,15 @@ static int cmd_scan_off(const struct shell *sh) (void)k_work_cancel_delayable(&active_scan_timeout_work); err = bt_le_scan_stop(); + + return err; +} + +static int cmd_scan_off(const struct shell *sh) +{ + int err; + + err = bt_do_scan_off(); if (err) { shell_error(sh, "Stopping scanning failed (err %d)", err); return err; @@ -1753,7 +1758,7 @@ static int cmd_scan_filter_set_name(const struct shell *sh, size_t argc, 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", + shell_error(sh, "Name is too long (max %zu): %s\n", sizeof(scan_filter.name), name_arg); return -ENOEXEC; } @@ -1772,7 +1777,7 @@ static int cmd_scan_filter_set_addr(const struct shell *sh, size_t argc, /* Validate length including null terminator. */ if (strlen(addr_arg) > max_cpy_len) { - shell_error(ctx_shell, "Invalid address string: %s\n", + shell_error(sh, "Invalid address string: %s\n", addr_arg); return -ENOEXEC; } @@ -1783,9 +1788,9 @@ static int cmd_scan_filter_set_addr(const struct shell *sh, size_t argc, uint8_t tmp; if (c != ':' && char2hex(c, &tmp) < 0) { - shell_error(ctx_shell, - "Invalid address string: %s\n", - addr_arg); + shell_error(sh, + "Invalid address string: %s\n", + addr_arg); return -ENOEXEC; } } @@ -1864,11 +1869,20 @@ static int cmd_scan_filter_clear_all(const struct shell *sh, size_t argc, return 0; } -static int cmd_scan_filter_clear_name(const struct shell *sh, size_t argc, - char *argv[]) +static void bt_do_scan_filter_clear_name(void) { (void)memset(scan_filter.name, 0, sizeof(scan_filter.name)); scan_filter.name_set = false; +} + +static int cmd_scan_filter_clear_name(const struct shell *sh, size_t argc, + char *argv[]) +{ + ARG_UNUSED(sh); + ARG_UNUSED(argc); + ARG_UNUSED(argv); + + bt_do_scan_filter_clear_name(); return 0; } @@ -1876,6 +1890,10 @@ static int cmd_scan_filter_clear_name(const struct shell *sh, size_t argc, static int cmd_scan_filter_clear_addr(const struct shell *sh, size_t argc, char *argv[]) { + ARG_UNUSED(sh); + ARG_UNUSED(argc); + ARG_UNUSED(argv); + (void)memset(scan_filter.addr, 0, sizeof(scan_filter.addr)); scan_filter.addr_set = false; @@ -1936,7 +1954,7 @@ static ssize_t ad_init(struct bt_data *data_array, const size_t data_array_size, 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); + bt_shell_error("Failed to add CSIS data (err %d)", csis_ad_len); return ad_len; } @@ -2306,7 +2324,7 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[]) if (*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; } @@ -2326,7 +2344,7 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[]) 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; } @@ -2340,7 +2358,7 @@ 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; } @@ -2357,7 +2375,7 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[]) 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; } @@ -2378,7 +2396,7 @@ static int cmd_adv_data(const struct shell *sh, size_t argc, char *argv[]) if (*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; } @@ -2498,7 +2516,7 @@ static int cmd_adv_delete(const struct shell *sh, size_t argc, char *argv[]) err = bt_le_ext_adv_delete(adv); if (err) { - shell_error(ctx_shell, "Failed to delete advertiser set"); + shell_error(sh, "Failed to delete advertiser set"); return err; } @@ -3108,7 +3126,7 @@ static int cmd_set_power_report_enable(const struct shell *sh, size_t argc, char return -ENOEXEC; } err = bt_conn_le_set_tx_power_report_enable(default_conn, local_enable, - remote_enable); + remote_enable); if (!err) { shell_print(sh, "Tx Power Report: local: %s, remote: %s", enabled2str(local_enable), enabled2str(remote_enable)); @@ -3259,28 +3277,27 @@ static int cmd_subrate_request(const struct shell *sh, size_t argc, char *argv[] #if defined(CONFIG_BT_CONN) #if defined(CONFIG_BT_CENTRAL) -static int cmd_connect_le(const struct shell *sh, size_t argc, char *argv[]) +static int bt_do_connect_le(int *ercd, size_t argc, char *argv[]) { int err; bt_addr_le_t addr; struct bt_conn *conn; uint32_t options = 0; + *ercd = 0; + /* When no arguments are specified, connect to the last scanned device. */ if (argc == 1) { if (auto_connect.addr_set) { bt_addr_le_copy(&addr, &auto_connect.addr); } else { - shell_error(sh, "No connectable adv stored, please trigger a scan first."); - shell_help(sh); - - return SHELL_CMD_HELP_PRINTED; + return -ENOENT; } } else { err = bt_addr_le_from_str(argv[1], argv[2], &addr); if (err) { - shell_error(sh, "Invalid peer address (err %d)", err); - return err; + *ercd = err; + return -EINVAL; } } @@ -3293,7 +3310,6 @@ static int cmd_connect_le(const struct shell *sh, size_t argc, char *argv[]) } else if (!strcmp(arg, "no-1m")) { options |= BT_CONN_LE_OPT_NO_1M; } else { - shell_help(sh); return SHELL_CMD_HELP_PRINTED; } } @@ -3307,12 +3323,9 @@ static int cmd_connect_le(const struct shell *sh, size_t argc, char *argv[]) err = bt_conn_le_create(&addr, create_params, BT_LE_CONN_PARAM_DEFAULT, &conn); if (err) { - shell_error(sh, "Connection failed (%d)", err); + *ercd = err; return -ENOEXEC; } else { - - shell_print(sh, "Connection pending"); - /* unref connection obj in advance as app user */ bt_conn_unref(conn); } @@ -3320,6 +3333,32 @@ static int cmd_connect_le(const struct shell *sh, size_t argc, char *argv[]) return 0; } +static int cmd_connect_le(const struct shell *sh, size_t argc, char *argv[]) +{ + int err; + int ercd; + + err = bt_do_connect_le(&ercd, argc, argv); + switch (err) { + case -ENOENT: + shell_error(sh, "No connectable adv stored. Please trigger a scan first."); + shell_help(sh); + return SHELL_CMD_HELP_PRINTED; + case -EINVAL: + shell_error(sh, "Invalid peer address (err %d)", ercd); + return ercd; + case SHELL_CMD_HELP_PRINTED: + shell_help(sh); + return SHELL_CMD_HELP_PRINTED; + case -ENOEXEC: + shell_error(sh, "Connection failed (%d)", ercd); + return -ENOEXEC; + default: + shell_print(sh, "Connection pending"); + return 0; + } +} + #if !defined(CONFIG_BT_FILTER_ACCEPT_LIST) static int cmd_auto_conn(const struct shell *sh, size_t argc, char *argv[]) { @@ -3520,11 +3559,11 @@ static int cmd_info(const struct shell *sh, size_t argc, char *argv[]) err = bt_conn_get_info(conn, &info); if (err) { - shell_print(ctx_shell, "Failed to get info"); + shell_print(sh, "Failed to get info"); goto done; } - shell_print(ctx_shell, "Type: %s, Role: %s, Id: %u", + shell_print(sh, "Type: %s, Role: %s, Id: %u", get_conn_type_str(info.type), get_conn_role_str(info.role), info.id); @@ -3535,20 +3574,20 @@ static int cmd_info(const struct shell *sh, size_t argc, char *argv[]) print_le_addr("Remote on-air", info.le.remote); print_le_addr("Local on-air", info.le.local); - shell_print(ctx_shell, "Interval: 0x%04x (%u us)", + shell_print(sh, "Interval: 0x%04x (%u us)", info.le.interval, BT_CONN_INTERVAL_TO_US(info.le.interval)); - shell_print(ctx_shell, "Latency: 0x%04x", + shell_print(sh, "Latency: 0x%04x", info.le.latency); - shell_print(ctx_shell, "Supervision timeout: 0x%04x (%d ms)", + shell_print(sh, "Supervision timeout: 0x%04x (%d ms)", info.le.timeout, info.le.timeout * 10); #if defined(CONFIG_BT_USER_PHY_UPDATE) - shell_print(ctx_shell, "LE PHY: TX PHY %s, RX PHY %s", + shell_print(sh, "LE PHY: TX PHY %s, RX PHY %s", phy2str(info.le.phy->tx_phy), phy2str(info.le.phy->rx_phy)); #endif #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE) - shell_print(ctx_shell, "LE data len: TX (len: %d time: %d)" + shell_print(sh, "LE data len: TX (len: %d time: %d)" " RX (len: %d time: %d)", info.le.data_len->tx_max_len, info.le.data_len->tx_max_time, @@ -3556,7 +3595,7 @@ static int cmd_info(const struct shell *sh, size_t argc, char *argv[]) info.le.data_len->rx_max_time); #endif #if defined(CONFIG_BT_SUBRATING) - shell_print(ctx_shell, "LE Subrating: Subrate Factor: %d" + shell_print(sh, "LE Subrating: Subrate Factor: %d" " Continuation Number: %d", info.le.subrate->factor, info.le.subrate->continuation_number); @@ -3568,7 +3607,7 @@ static int cmd_info(const struct shell *sh, size_t argc, char *argv[]) char addr_str[BT_ADDR_STR_LEN]; bt_addr_to_str(info.br.dst, addr_str, sizeof(addr_str)); - shell_print(ctx_shell, "Peer address %s", addr_str); + shell_print(sh, "Peer address %s", addr_str); } #endif /* defined(CONFIG_BT_CLASSIC) */ @@ -3585,8 +3624,8 @@ static int cmd_conn_update(const struct shell *sh, size_t argc, char *argv[]) if (default_conn == NULL) { shell_error(sh, - "%s: at least, one connection is required", - sh->ctx->active_cmd.syntax); + "%s: at least, one connection is required", + sh->ctx->active_cmd.syntax); return -ENOEXEC; } @@ -3634,8 +3673,8 @@ static int cmd_conn_data_len_update(const struct shell *sh, size_t argc, if (default_conn == NULL) { shell_error(sh, - "%s: at least, one connection is required", - sh->ctx->active_cmd.syntax); + "%s: at least, one connection is required", + sh->ctx->active_cmd.syntax); return -ENOEXEC; } @@ -3659,8 +3698,6 @@ static int cmd_conn_data_len_update(const struct shell *sh, size_t argc, shell_print(sh, "Calculated tx time: %d", param.tx_max_time); } - - err = bt_conn_le_data_len_update(default_conn, ¶m); if (err) { shell_error(sh, "data len update failed (err %d).", err); @@ -3681,8 +3718,8 @@ static int cmd_conn_phy_update(const struct shell *sh, size_t argc, if (default_conn == NULL) { shell_error(sh, - "%s: at least, one connection is required", - sh->ctx->active_cmd.syntax); + "%s: at least, one connection is required", + sh->ctx->active_cmd.syntax); return -ENOEXEC; } @@ -3798,7 +3835,7 @@ static int cmd_clear(const struct shell *sh, size_t argc, char *argv[]) err = bt_unpair(selected_id, NULL); if (err) { shell_error(sh, "Failed to clear pairings (err %d)", - err); + err); return err; } else { shell_print(sh, "Pairings successfully cleared"); @@ -3935,7 +3972,7 @@ static void bond_info(const struct bt_bond_info *info, void *user_data) int *bond_count = user_data; bt_addr_le_to_str(&info->addr, addr, sizeof(addr)); - shell_print(ctx_shell, "Remote Identity: %s", addr); + bt_shell_print("Remote Identity: %s", addr); (*bond_count)++; } @@ -3969,7 +4006,7 @@ static void connection_info(struct bt_conn *conn, void *user_data) struct bt_conn_info info; if (bt_conn_get_info(conn, &info) < 0) { - shell_error(ctx_shell, "Unable to get info: conn %p", conn); + bt_shell_error("Unable to get info: conn %p", conn); return; } @@ -3977,19 +4014,19 @@ static void connection_info(struct bt_conn *conn, void *user_data) #if defined(CONFIG_BT_CLASSIC) case BT_CONN_TYPE_BR: bt_addr_to_str(info.br.dst, addr, sizeof(addr)); - shell_print(ctx_shell, " #%u [BR][%s] %s", info.id, role_str(info.role), addr); + bt_shell_print(" #%u [BR][%s] %s", info.id, role_str(info.role), addr); break; #endif case BT_CONN_TYPE_LE: bt_addr_le_to_str(info.le.dst, addr, sizeof(addr)); - shell_print(ctx_shell, "%s#%u [LE][%s] %s: Interval %u latency %u timeout %u", - conn == default_conn ? "*" : " ", info.id, role_str(info.role), addr, - info.le.interval, info.le.latency, info.le.timeout); + bt_shell_print("%s#%u [LE][%s] %s: Interval %u latency %u timeout %u", + conn == default_conn ? "*" : " ", info.id, role_str(info.role), addr, + info.le.interval, info.le.latency, info.le.timeout); break; #if defined(CONFIG_BT_ISO) case BT_CONN_TYPE_ISO: bt_addr_le_to_str(info.le.dst, addr, sizeof(addr)); - shell_print(ctx_shell, " #%u [ISO][%s] %s", info.id, role_str(info.role), addr); + bt_shell_print(" #%u [ISO][%s] %s", info.id, role_str(info.role), addr); break; #endif default: @@ -4019,7 +4056,7 @@ static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) snprintk(passkey_str, 7, "%06u", passkey); - shell_print(ctx_shell, "Passkey for %s: %s", addr, passkey_str); + bt_shell_print("Passkey for %s: %s", addr, passkey_str); } #if defined(CONFIG_BT_PASSKEY_KEYPRESS) @@ -4030,8 +4067,8 @@ static void auth_passkey_display_keypress(struct bt_conn *conn, bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - shell_print(ctx_shell, "Passkey keypress notification from %s: type %d", - addr, type); + bt_shell_print("Passkey keypress notification from %s: type %d", + addr, type); } #endif @@ -4044,7 +4081,7 @@ static void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey) snprintk(passkey_str, 7, "%06u", passkey); - shell_print(ctx_shell, "Confirm passkey for %s: %s", addr, passkey_str); + bt_shell_print("Confirm passkey for %s: %s", addr, passkey_str); } static void auth_passkey_entry(struct bt_conn *conn) @@ -4053,7 +4090,7 @@ static void auth_passkey_entry(struct bt_conn *conn) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - shell_print(ctx_shell, "Enter passkey for %s", addr); + bt_shell_print("Enter passkey for %s", addr); } static void auth_cancel(struct bt_conn *conn) @@ -4062,7 +4099,7 @@ static void auth_cancel(struct bt_conn *conn) conn_addr_str(conn, addr, sizeof(addr)); - shell_print(ctx_shell, "Pairing cancelled: %s", addr); + bt_shell_print("Pairing cancelled: %s", addr); /* clear connection reference for sec mode 3 pairing */ if (pairing_conn) { @@ -4077,7 +4114,7 @@ static void auth_pairing_confirm(struct bt_conn *conn) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - shell_print(ctx_shell, "Confirm pairing for %s", addr); + bt_shell_print("Confirm pairing for %s", addr); } #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) @@ -4123,9 +4160,8 @@ static void auth_pairing_oob_data_request(struct bt_conn *conn, if (oobd_remote && !bt_addr_le_eq(info.le.remote, &oob_remote.addr)) { bt_addr_le_to_str(info.le.remote, addr, sizeof(addr)); - shell_print(ctx_shell, - "No OOB data available for remote %s", - addr); + bt_shell_print("No OOB data available for remote %s", + addr); bt_conn_auth_cancel(conn); return; } @@ -4133,9 +4169,8 @@ static void auth_pairing_oob_data_request(struct bt_conn *conn, if (oobd_local && !bt_addr_le_eq(info.le.local, &oob_local.addr)) { bt_addr_le_to_str(info.le.local, addr, sizeof(addr)); - shell_print(ctx_shell, - "No OOB data available for local %s", - addr); + bt_shell_print("No OOB data available for local %s", + addr); bt_conn_auth_cancel(conn); return; } @@ -4143,14 +4178,14 @@ static void auth_pairing_oob_data_request(struct bt_conn *conn, bt_le_oob_set_sc_data(conn, oobd_local, oobd_remote); bt_addr_le_to_str(info.le.dst, addr, sizeof(addr)); - shell_print(ctx_shell, "Set %s OOB SC data for %s, ", - oob_config_str(oob_info->lesc.oob_config), addr); + bt_shell_print("Set %s OOB SC data for %s, ", + oob_config_str(oob_info->lesc.oob_config), addr); return; } #endif /* CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY */ bt_addr_le_to_str(info.le.dst, addr, sizeof(addr)); - shell_print(ctx_shell, "Legacy OOB TK requested from remote %s", addr); + bt_shell_print("Legacy OOB TK requested from remote %s", addr); } static void auth_pairing_complete(struct bt_conn *conn, bool bonded) @@ -4159,8 +4194,8 @@ static void auth_pairing_complete(struct bt_conn *conn, bool bonded) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - shell_print(ctx_shell, "%s with %s", bonded ? "Bonded" : "Paired", - addr); + bt_shell_print("%s with %s", bonded ? "Bonded" : "Paired", + addr); } static void auth_pairing_failed(struct bt_conn *conn, enum bt_security_err err) @@ -4169,8 +4204,8 @@ static void auth_pairing_failed(struct bt_conn *conn, enum bt_security_err err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - shell_print(ctx_shell, "Pairing failed with %s reason: %s (%d)", addr, - security_err_str(err), err); + bt_shell_print("Pairing failed with %s reason: %s (%d)", addr, + security_err_str(err), err); } #if defined(CONFIG_BT_CLASSIC) @@ -4190,10 +4225,10 @@ static void auth_pincode_entry(struct bt_conn *conn, bool highsec) bt_addr_to_str(info.br.dst, addr, sizeof(addr)); if (highsec) { - shell_print(ctx_shell, "Enter 16 digits wide PIN code for %s", - addr); + bt_shell_print("Enter 16 digits wide PIN code for %s", + addr); } else { - shell_print(ctx_shell, "Enter PIN code for %s", addr); + bt_shell_print("Enter PIN code for %s", addr); } /* @@ -4210,12 +4245,12 @@ static void auth_pincode_entry(struct bt_conn *conn, bool highsec) enum bt_security_err pairing_accept( struct bt_conn *conn, const struct bt_conn_pairing_feat *const feat) { - shell_print(ctx_shell, "Remote pairing features: " - "IO: 0x%02x, OOB: %d, AUTH: 0x%02x, Key: %d, " - "Init Kdist: 0x%02x, Resp Kdist: 0x%02x", - feat->io_capability, feat->oob_data_flag, - feat->auth_req, feat->max_enc_key_size, - feat->init_key_dist, feat->resp_key_dist); + bt_shell_print("Remote pairing features: " + "IO: 0x%02x, OOB: %d, AUTH: 0x%02x, Key: %d, " + "Init Kdist: 0x%02x, Resp Kdist: 0x%02x", + feat->io_capability, feat->oob_data_flag, + feat->auth_req, feat->max_enc_key_size, + feat->init_key_dist, feat->resp_key_dist); return BT_SECURITY_ERR_SUCCESS; } @@ -4226,7 +4261,7 @@ void bond_deleted(uint8_t id, const bt_addr_le_t *peer) char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(peer, addr, sizeof(addr)); - shell_print(ctx_shell, "Bond deleted for %s, id %u", addr, id); + bt_shell_print("Bond deleted for %s, id %u", addr, id); } static struct bt_conn_auth_cb auth_cb_display = { @@ -4794,7 +4829,7 @@ int ead_encrypt_ad(const uint8_t *payload, uint8_t payload_size, uint8_t *encryp err = bt_ead_encrypt(bt_shell_ead_session_key, bt_shell_ead_iv, payload, payload_size, encrypted_payload); if (err != 0) { - shell_error(ctx_shell, "Failed to encrypt AD."); + bt_shell_error("Failed to encrypt AD."); return -1; } @@ -4822,7 +4857,7 @@ int ead_update_ad(void) if (ad->data_len < 0) { /* if the len is less than 0 that mean there is not even a type field */ - shell_error(ctx_shell, "Failed to update AD due to malformed AD."); + bt_shell_error("Failed to update AD due to malformed AD."); return -ENOEXEC; } @@ -4850,11 +4885,11 @@ int ead_update_ad(void) err = bt_le_ext_adv_set_data(adv, ad_structs, bt_shell_ead_ad_len, NULL, 0); if (err != 0) { - shell_error(ctx_shell, "Failed to set advertising data (err %d)", err); + bt_shell_error("Failed to set advertising data (err %d)", err); return -ENOEXEC; } - shell_info(ctx_shell, "Advertising data for Advertiser[%d] %p updated.", selected_adv, adv); + bt_shell_info("Advertising data for Advertiser[%d] %p updated.", selected_adv, adv); return 0; } diff --git a/subsys/bluetooth/host/shell/bt_shell_private.c b/subsys/bluetooth/host/shell/bt_shell_private.c new file mode 100644 index 00000000000000..a01b968a27a2da --- /dev/null +++ b/subsys/bluetooth/host/shell/bt_shell_private.c @@ -0,0 +1,89 @@ +/** + * @file bt_shell_private.c + * @brief Bluetooth shell private module + * + * Provide common function which can be shared using privately inside Bluetooth shell. + */ + +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "bt_shell_private.h" + +static void wall_vfprintf(enum shell_vt100_color color, const char *fmt, va_list args) +{ + int count; + const struct shell *sh; + + count = shell_backend_count_get(); + for (int i = 0; i < count; i++) { + va_list args_copy; + + va_copy(args_copy, args); /* Create a copy of 'args' to safely reuse */ + sh = shell_backend_get(i); + shell_vfprintf(sh, color, fmt, args_copy); + va_end(args_copy); /* Clean up to prevent resource leaks */ + } +} + +void bt_shell_hexdump(const uint8_t *data, size_t len) +{ + int count; + const struct shell *sh; + + count = shell_backend_count_get(); + for (int i = 0; i < count; i++) { + sh = shell_backend_get(i); + shell_hexdump(sh, data, len); + } +} + +void bt_shell_fprintf(enum shell_vt100_color color, + const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(color, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_info(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_INFO, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_print(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_NORMAL, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_warn(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_WARNING, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_error(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_ERROR, fmt, args); + va_end(args); +} diff --git a/subsys/bluetooth/host/shell/bt_shell_private.h b/subsys/bluetooth/host/shell/bt_shell_private.h new file mode 100644 index 00000000000000..69460797686538 --- /dev/null +++ b/subsys/bluetooth/host/shell/bt_shell_private.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef __BT_SHELL_PRIVATE_H +#define __BT_SHELL_PRIVATE_H + +#include + +/** + * @brief printf-like function which sends formatted data stream to the shell. + * (Bluetooth context specific) + * + * This function can be used from the command handler or from threads, but not + * from an interrupt context. + * + * @param[in] color Printed text color. + * @param[in] fmt Format string. + * @param[in] ... List of parameters to print. + */ +__printf_like(2, 3) void bt_shell_fprintf(enum shell_vt100_color color, + const char *fmt, ...); + +/** + * @brief printf-like function which sends formatted data stream to the shell. + * (Bluetooth context specific) + * + * This function can be used from the command handler or from threads, but not + * from an interrupt context. + * + * @param[in] fmt Format string. + * @param[in] ... List of parameters to print. + */ +__printf_like(1, 2) void bt_shell_fprintf_info(const char *fmt, ...); +__printf_like(1, 2) void bt_shell_fprintf_print(const char *fmt, ...); +__printf_like(1, 2) void bt_shell_fprintf_warn(const char *fmt, ...); +__printf_like(1, 2) void bt_shell_fprintf_error(const char *fmt, ...); + +/** + * @brief Print data in hexadecimal format. + * (Bluetooth context specific) + * + * @param[in] data Pointer to data. + * @param[in] len Length of data. + */ +void bt_shell_hexdump(const uint8_t *data, size_t len); + +/** + * @brief Print info message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_info(_ft, ...) \ + bt_shell_fprintf_info(_ft "\n", ##__VA_ARGS__) + +/** + * @brief Print normal message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_print(_ft, ...) \ + bt_shell_fprintf_print(_ft "\n", ##__VA_ARGS__) + +/** + * @brief Print warning message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_warn(_ft, ...) \ + bt_shell_fprintf_warn(_ft "\n", ##__VA_ARGS__) + +/** + * @brief Print error message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_error(_ft, ...) \ + bt_shell_fprintf_error(_ft "\n", ##__VA_ARGS__) + +#endif /* __BT_SHELL_PRIVATE_H */ diff --git a/subsys/bluetooth/host/shell/cs.c b/subsys/bluetooth/host/shell/cs.c index b1f6f18aceb385..81ba2e41fa6be1 100644 --- a/subsys/bluetooth/host/shell/cs.c +++ b/subsys/bluetooth/host/shell/cs.c @@ -24,7 +24,8 @@ #include #include -#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) { @@ -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) { - shell_print(ctx_shell, "Subevent results contained step data: "); - shell_print(ctx_shell, "- Step mode %d\n" + bt_shell_print("Subevent results contained step data: "); + bt_shell_print("- Step mode %d\n" "- Step channel %d\n" "- Step data hexdump:", step->mode, step->channel); - shell_hexdump(ctx_shell, step->data, step->data_len); + bt_shell_hexdump(step->data, step->data_len); return true; } static void cs_test_subevent_data_cb(struct bt_conn_le_cs_subevent_result *result) { - shell_print(ctx_shell, "Received subevent results."); - shell_print(ctx_shell, "Subevent Header:\n" + bt_shell_print("Received subevent results."); + bt_shell_print("Subevent Header:\n" "- Procedure Counter: %d\n" "- Frequency Compensation: 0x%04x\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) { - 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[]) diff --git a/subsys/bluetooth/host/shell/gatt.c b/subsys/bluetooth/host/shell/gatt.c index 8581a494818c43..b3ce7fcb224836 100644 --- a/subsys/bluetooth/host/shell/gatt.c +++ b/subsys/bluetooth/host/shell/gatt.c @@ -27,6 +27,7 @@ #include #include "host/shell/bt.h" +#include "host/shell/bt_shell_private.h" #if defined(CONFIG_BT_GATT_CLIENT) || defined(CONFIG_BT_GATT_DYNAMIC_DB) extern uint8_t selected_id; @@ -75,8 +76,8 @@ static void update_write_stats(uint16_t len) static void print_write_stats(void) { - shell_print(ctx_shell, "Write #%u: %u bytes (%u bps)", - write_stats.count, write_stats.total, write_stats.rate); + bt_shell_print("Write #%u: %u bytes (%u bps)", + write_stats.count, write_stats.total, write_stats.rate); } #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, struct bt_gatt_exchange_params *params) { - shell_print(ctx_shell, "Exchange %s", err == 0U ? "successful" : - "failed"); + bt_shell_print("Exchange %s", err == 0U ? "successful" : + "failed"); /* Release global `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_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) { - shell_print(sh, "[bcast]"); + bt_shell_print("[bcast]"); } if (properties & BT_GATT_CHRC_READ) { - shell_print(sh, "[read]"); + bt_shell_print("[read]"); } if (properties & BT_GATT_CHRC_WRITE) { - shell_print(sh, "[write]"); + bt_shell_print("[write]"); } 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) { - shell_print(sh, "[notify]"); + bt_shell_print("[notify]"); } if (properties & BT_GATT_CHRC_INDICATE) { - shell_print(sh, "[indicate]"); + bt_shell_print("[indicate]"); } if (properties & BT_GATT_CHRC_AUTH) { - shell_print(sh, "[auth]"); + bt_shell_print("[auth]"); } 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, @@ -188,7 +189,7 @@ static uint8_t discover_func(struct bt_conn *conn, char str[BT_UUID_STR_LEN]; if (!attr) { - shell_print(ctx_shell, "Discover complete"); + bt_shell_print("Discover complete"); (void)memset(params, 0, sizeof(*params)); return BT_GATT_ITER_STOP; } @@ -198,29 +199,29 @@ static uint8_t discover_func(struct bt_conn *conn, case BT_GATT_DISCOVER_PRIMARY: gatt_service = attr->user_data; bt_uuid_to_str(gatt_service->uuid, str, sizeof(str)); - shell_print(ctx_shell, "Service %s found: start handle %x, " - "end_handle %x", str, attr->handle, - gatt_service->end_handle); + bt_shell_print("Service %s found: start handle %x, " + "end_handle %x", str, attr->handle, + gatt_service->end_handle); break; case BT_GATT_DISCOVER_CHARACTERISTIC: gatt_chrc = attr->user_data; bt_uuid_to_str(gatt_chrc->uuid, str, sizeof(str)); - shell_print(ctx_shell, "Characteristic %s found: handle %x", - str, attr->handle); - print_chrc_props(ctx_shell, gatt_chrc->properties); + bt_shell_print("Characteristic %s found: handle %x", + str, attr->handle); + print_chrc_props(gatt_chrc->properties); break; case BT_GATT_DISCOVER_INCLUDE: gatt_include = attr->user_data; bt_uuid_to_str(gatt_include->uuid, str, sizeof(str)); - shell_print(ctx_shell, "Include %s found: handle %x, start %x, " - "end %x", str, attr->handle, - gatt_include->start_handle, - gatt_include->end_handle); + bt_shell_print("Include %s found: handle %x, start %x, " + "end %x", str, attr->handle, + gatt_include->start_handle, + gatt_include->end_handle); break; default: bt_uuid_to_str(attr->uuid, str, sizeof(str)); - shell_print(ctx_shell, "Descriptor %s found: handle %x", str, - attr->handle); + bt_shell_print("Descriptor %s found: handle %x", str, + attr->handle); break; } @@ -291,13 +292,13 @@ static uint8_t read_func(struct bt_conn *conn, uint8_t err, struct bt_gatt_read_params *params, 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) { (void)memset(params, 0, sizeof(*params)); return BT_GATT_ITER_STOP; } else { - shell_hexdump(ctx_shell, data, length); + bt_shell_hexdump(data, length); } 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, 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)); } @@ -557,14 +558,14 @@ static uint8_t notify_func(struct bt_conn *conn, const void *data, uint16_t length) { if (!data) { - shell_print(ctx_shell, "Unsubscribed"); + bt_shell_print("Unsubscribed"); params->value_handle = 0U; return BT_GATT_ITER_STOP; } - shell_print(ctx_shell, "Notification: value_handle %u, length %u", - params->value_handle, length); - shell_hexdump(ctx_shell, data, length); + bt_shell_print("Notification: value_handle %u, length %u", + params->value_handle, length); + bt_shell_hexdump(data, length); 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) { 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); } diff --git a/subsys/bluetooth/host/shell/iso.c b/subsys/bluetooth/host/shell/iso.c index 7c30b09bce91ef..77b79fbb8cc9f0 100644 --- a/subsys/bluetooth/host/shell/iso.c +++ b/subsys/bluetooth/host/shell/iso.c @@ -23,6 +23,7 @@ #include #include "host/shell/bt.h" +#include "host/shell/bt_shell_private.h" #if defined(CONFIG_BT_ISO_TX) #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) { if (info->flags & BT_ISO_FLAGS_VALID) { - shell_print(ctx_shell, "Incoming data channel %p len %u, seq: %d, ts: %d", - chan, buf->len, info->seq_num, info->ts); + bt_shell_print("Incoming data channel %p len %u, seq: %d, ts: %d", + chan, buf->len, info->seq_num, info->ts); } } #endif /* CONFIG_BT_ISO_RX */ @@ -86,7 +87,7 @@ static void iso_connected(struct bt_iso_chan *chan) struct bt_iso_info iso_info; 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); @@ -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) { - shell_print(ctx_shell, "ISO Channel %p disconnected with reason 0x%02x", - chan, reason); + bt_shell_print("ISO Channel %p disconnected with reason 0x%02x", + chan, reason); } 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, struct bt_iso_chan **chan) { - shell_print(ctx_shell, "Incoming request from %p with CIG ID 0x%02X and CIS ID 0x%02X", - info->acl, info->cig_id, info->cis_id); + 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); if (iso_chan.iso) { - shell_print(ctx_shell, "No channels available"); + bt_shell_print("No channels available"); return -ENOMEM; } diff --git a/subsys/bluetooth/host/shell/l2cap.c b/subsys/bluetooth/host/shell/l2cap.c index 10b8a4517ea26b..05c0af9fe24c24 100644 --- a/subsys/bluetooth/host/shell/l2cap.c +++ b/subsys/bluetooth/host/shell/l2cap.c @@ -30,6 +30,7 @@ #include #include "host/shell/bt.h" +#include "host/shell/bt_shell_private.h" #define CREDITS 10 #define DATA_MTU (23 * CREDITS) @@ -89,7 +90,7 @@ static void l2cap_recv_cb(struct k_work *work) struct net_buf *buf; 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); } } @@ -102,18 +103,18 @@ static int l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf) return l2cap_recv_metrics(chan, buf); } - shell_print(ctx_shell, "Incoming data channel %p len %u", chan, - buf->len); + bt_shell_print("Incoming data channel %p len %u", chan, + 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) { /* Submit work only if queue is empty */ if (k_fifo_is_empty(&l2cap_recv_fifo)) { - shell_print(ctx_shell, "Delaying response in %u ms...", - l2cap_recv_delay_ms); + bt_shell_print("Delaying response in %u ms...", + l2cap_recv_delay_ms); } 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) { - 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) { - 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) @@ -141,19 +142,19 @@ static void l2cap_connected(struct bt_l2cap_chan *chan) 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) { - 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) { /* print if metrics is disabled */ 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); @@ -217,7 +218,7 @@ static int l2cap_accept(struct bt_conn *conn, struct bt_l2cap_server *server, { int err; - shell_print(ctx_shell, "Incoming conn %p", conn); + bt_shell_print("Incoming conn %p", conn); err = l2cap_accept_policy(conn); 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) { - shell_print(ctx_shell, "No channels available"); + bt_shell_print("No channels available"); return -ENOMEM; }