From 7ed3167f3dd5ce301a43aa5a43b6d39ce28d1a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Rist=20Sk=C3=B8ien?= Date: Wed, 4 Sep 2024 22:25:14 +0200 Subject: [PATCH] bct: Added autocomplete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OCT-3137 Working autocomplete Signed-off-by: Kristoffer Rist Skøien --- .../nrf5340_audio/src/modules/sd_card.c | 18 ++- .../nrf5340_audio/src/modules/sd_card.h | 5 +- .../src/modules/sd_card_playback.c | 2 +- .../bluetooth/broadcast_config_tool/prj.conf | 9 +- .../broadcast_config_tool/src/main.c | 130 +++++++++++++++++- 5 files changed, 149 insertions(+), 15 deletions(-) diff --git a/applications/nrf5340_audio/src/modules/sd_card.c b/applications/nrf5340_audio/src/modules/sd_card.c index 5e973449c388..ff68bceb2dab 100644 --- a/applications/nrf5340_audio/src/modules/sd_card.c +++ b/applications/nrf5340_audio/src/modules/sd_card.c @@ -31,7 +31,7 @@ static struct fs_mount_t mnt_pt = { .fs_data = &fat_fs, }; -int sd_card_list_files(char const *const path, char *buf, size_t *buf_size) +int sd_card_list_files(char const *const path, char *buf, size_t *buf_size, bool extra_info) { int ret; struct fs_dir_t dirp; @@ -61,7 +61,7 @@ int sd_card_list_files(char const *const path, char *buf, size_t *buf_size) ret = fs_opendir(&dirp, abs_path_name); if (ret) { - LOG_ERR("Open assigned path failed"); + LOG_WRN("Open assigned path failed %d. %s", ret, abs_path_name); return ret; } } @@ -78,9 +78,17 @@ int sd_card_list_files(char const *const path, char *buf, size_t *buf_size) if (buf != NULL) { size_t remaining_buf_size = *buf_size - used_buf_size; - ssize_t len = snprintk( - &buf[used_buf_size], remaining_buf_size, "[%s]\t%s\n", - entry.type == FS_DIR_ENTRY_DIR ? "DIR " : "FILE", entry.name); + ssize_t len; + + if (extra_info) { + len = snprintk(&buf[used_buf_size], remaining_buf_size, + "[%s]\t%s\r\n", + entry.type == FS_DIR_ENTRY_DIR ? "DIR " : "FILE", + entry.name); + } else { + len = snprintk(&buf[used_buf_size], remaining_buf_size, "%s\r\n", + entry.name); + } if (len >= remaining_buf_size) { LOG_ERR("Failed to append to buffer, error: %d", len); diff --git a/applications/nrf5340_audio/src/modules/sd_card.h b/applications/nrf5340_audio/src/modules/sd_card.h index a51bd37fcee2..969f60150566 100644 --- a/applications/nrf5340_audio/src/modules/sd_card.h +++ b/applications/nrf5340_audio/src/modules/sd_card.h @@ -14,12 +14,13 @@ * @brief Print out the contents under SD card root path and write the content to buffer. * * @param[in] path Path of the folder which is going to be listed. - * If assigned path is null, then listing the contents under + * If assigned path is NULL, then listing the contents under * root. If assigned path doesn't exist, an error will be * returned. * @param[out] buf Buffer where data is written. If set to NULL, it will be * ignored. * @param[in, out] buf_size Buffer size. + * @param[in] extra_info Will append DIR/FILE info to string. * * @retval 0 on success. * @retval -EPERM SD card operation is ongoing somewhere else. @@ -28,7 +29,7 @@ * @retval -FR_INVALID_NAME Path is too long. * @retval Otherwise, error from underlying drivers. */ -int sd_card_list_files(char const *const path, char *buf, size_t *buf_size); +int sd_card_list_files(char const *const path, char *buf, size_t *buf_size, bool extra_info); /** * @brief Write data from buffer into the file. diff --git a/applications/nrf5340_audio/src/modules/sd_card_playback.c b/applications/nrf5340_audio/src/modules/sd_card_playback.c index f3725c218699..406c7cb7347e 100644 --- a/applications/nrf5340_audio/src/modules/sd_card_playback.c +++ b/applications/nrf5340_audio/src/modules/sd_card_playback.c @@ -564,7 +564,7 @@ static int cmd_list_files(const struct shell *shell, size_t argc, char **argv) char buf[LIST_FILES_BUF_SIZE]; size_t buf_size = LIST_FILES_BUF_SIZE; - ret = sd_card_list_files(playback_file_path, buf, &buf_size); + ret = sd_card_list_files(playback_file_path, buf, &buf_size, true); if (ret) { shell_error(shell, "List files err: %d", ret); return ret; diff --git a/samples/bluetooth/broadcast_config_tool/prj.conf b/samples/bluetooth/broadcast_config_tool/prj.conf index dc0e214b7f01..49962077cdc8 100644 --- a/samples/bluetooth/broadcast_config_tool/prj.conf +++ b/samples/bluetooth/broadcast_config_tool/prj.conf @@ -15,7 +15,7 @@ CONFIG_THREAD_RUNTIME_STATS=y CONFIG_MAIN_THREAD_PRIORITY=10 CONFIG_STACK_SENTINEL=y CONFIG_INIT_STACKS=y -CONFIG_MAIN_STACK_SIZE=1800 +CONFIG_MAIN_STACK_SIZE=8800 CONFIG_THREAD_NAME=y CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1200 @@ -29,7 +29,7 @@ CONFIG_LOG_TAG_MAX_LEN=2 CONFIG_LOG_TAG_DEFAULT="--" CONFIG_USE_SEGGER_RTT=y CONFIG_LOG_BACKEND_RTT=y -CONFIG_LOG_BUFFER_SIZE=4096 +CONFIG_LOG_BUFFER_SIZE=8000 CONFIG_BOARD_ENABLE_DCDC_NET=y CONFIG_BOARD_ENABLE_CPUNET=y @@ -96,7 +96,7 @@ CONFIG_KERNEL_SHELL=y CONFIG_SHELL_BACKEND_SERIAL=y CONFIG_SHELL_VT100_COMMANDS=y CONFIG_SHELL_VT100_COLORS=y -CONFIG_SHELL_STACK_SIZE=4096 +CONFIG_SHELL_STACK_SIZE=8096 CONFIG_SHELL_CMD_BUFF_SIZE=128 ## Reduce shell memory usage CONFIG_SHELL_WILDCARD=n @@ -150,6 +150,7 @@ CONFIG_SW_CODEC_LC3=n CONFIG_NRF5340_AUDIO_SD_CARD_MODULE=y CONFIG_NRF5340_AUDIO_SD_CARD_LC3_FILE=y CONFIG_NRF5340_AUDIO_SD_CARD_LC3_STREAMER=y -CONFIG_SD_CARD_LC3_STREAMER_STACK_SIZE=3000 + +CONFIG_SD_CARD_LC3_STREAMER_STACK_SIZE=8000 CONFIG_MODULE_SD_CARD_LOG_LEVEL_WRN=y diff --git a/samples/bluetooth/broadcast_config_tool/src/main.c b/samples/bluetooth/broadcast_config_tool/src/main.c index a67517bb43f3..92aff40d0bdd 100644 --- a/samples/bluetooth/broadcast_config_tool/src/main.c +++ b/samples/bluetooth/broadcast_config_tool/src/main.c @@ -571,6 +571,111 @@ static void broadcast_config_clear(void) } } +/** + * @brief Replaces first carriage return or line feed with null terminator. + */ +static void remove_cr_lf(char *str) +{ + char *p = str; + + while (*p != '\0') { + if (*p == '\r' || *p == '\n') { + *p = '\0'; + break; + } + p++; + } +} + +#define SD_FILECOUNT_MAX 120 +#define SD_PATHLEN_MAX 140 +#define FOLDER_BUF_MAX 800 +#define SD_LEVEL_MAX 7 +static char sd_paths_and_files[SD_FILECOUNT_MAX][SD_PATHLEN_MAX] = {'\0'}; +uint32_t num_files_added; + +static int traverse_down(char *path, uint8_t level) +{ + int ret; + char tmp_file_buf[FOLDER_BUF_MAX] = {'\0'}; + char *tmp_file_ptr = tmp_file_buf; + size_t buf_size = FOLDER_BUF_MAX; + + if (level > SD_LEVEL_MAX) { + LOG_DBG("At level %d, max level reached", level); + return 0; + } + + ret = sd_card_list_files(path, tmp_file_buf, &buf_size, false); + if (ret == -ENOENT) { + /* Not able to open, hence likely not a folder */ + return 0; + } else if (ret) { + return ret; + } + + LOG_DBG("level %d tmp_file_buf is: %s", level, tmp_file_buf); + + char *token = strtok_r(tmp_file_ptr, "\r\n", &tmp_file_ptr); + + while (token != NULL) { + if (strstr(token, "System Volume Information") != NULL) { + LOG_DBG("Skipping System Volume Information"); + token = strtok_r(NULL, "\n", &tmp_file_ptr); + } + + if (strstr(token, ".wav") != NULL) { + LOG_DBG("Skipping wav files"); + token = strtok_r(NULL, "\n", &tmp_file_ptr); + } + + remove_cr_lf(token); + LOG_DBG("level %d, token is: %s.", level, token); + char fullPath[SD_PATHLEN_MAX] = {'\0'}; + + if (path != NULL) { + remove_cr_lf(path); + strcat(fullPath, path); + strcat(fullPath, "/"); + } + + strcat(fullPath, token); + LOG_DBG("Fullpath: %s", fullPath); + + if (strstr(token, ".lc3") != NULL) { + strcpy(sd_paths_and_files[num_files_added], fullPath); + num_files_added++; + LOG_DBG("Added file num %d %s", num_files_added, fullPath); + if (num_files_added >= SD_FILECOUNT_MAX) { + LOG_WRN("Max file count reached"); + } + } else { + ret = traverse_down(fullPath, level + 1); + if (ret) { + LOG_DBG("Going up on level."); + } + } + token = strtok_r(NULL, "\n", &tmp_file_ptr); + } + + return 0; +} + +static int sd_card_toc_gen(void) +{ + /* Traverse SD tree */ + int ret; + + ret = traverse_down(NULL, 0); + if (ret) { + return ret; + } + + LOG_INF("Number of *.lc3 files on SD card: %d", num_files_added); + + return 0; +} + int main(void) { int ret; @@ -609,6 +714,9 @@ int main(void) ret = zbus_subscribers_create(); ERR_CHK_MSG(ret, "Failed to create zbus subscriber threads"); + /*TODO: Should be within the lc3_streamer_module */ + ret = sd_card_toc_gen(); + ERR_CHK_MSG(ret, "Failed to generate SD card table"); return 0; } @@ -1578,7 +1686,7 @@ static int cmd_file_list(const struct shell *shell, size_t argc, char **argv) dir_path = argv[1]; } - ret = sd_card_list_files(dir_path, buf, &buf_size); + ret = sd_card_list_files(dir_path, buf, &buf_size, true); if (ret) { shell_error(shell, "List files err: %d", ret); return ret; @@ -2325,11 +2433,27 @@ static int cmd_clear(const struct shell *shell, size_t argc, char **argv) return 0; } +static void file_paths_get(size_t idx, struct shell_static_entry *entry); + +SHELL_DYNAMIC_CMD_CREATE(folder_names, file_paths_get); + +static void file_paths_get(size_t idx, struct shell_static_entry *entry) +{ + if (idx < num_files_added) { + entry->syntax = sd_paths_and_files[idx]; + } else { + entry->syntax = NULL; + } + entry->handler = NULL; + entry->help = NULL; + entry->subcmd = &folder_names; +} + SHELL_STATIC_SUBCMD_SET_CREATE(sub_file_cmd, SHELL_COND_CMD(CONFIG_SHELL, list, NULL, "List files on SD card", cmd_file_list), - SHELL_COND_CMD(CONFIG_SHELL, select, NULL, "Select file on SD card", - cmd_file_select), + SHELL_CMD_ARG(select, &folder_names, "List files on SD card", + cmd_file_select, 4, 2), SHELL_SUBCMD_SET_END); SHELL_STATIC_SUBCMD_SET_CREATE(