Skip to content

Commit

Permalink
bct: Added autocomplete
Browse files Browse the repository at this point in the history
OCT-3137
Working autocomplete

Signed-off-by: Kristoffer Rist Skøien <kristoffer.skoien@nordicsemi.no>
  • Loading branch information
koffes committed Sep 26, 2024
1 parent cdbb02a commit 7ed3167
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 15 deletions.
18 changes: 13 additions & 5 deletions applications/nrf5340_audio/src/modules/sd_card.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions applications/nrf5340_audio/src/modules/sd_card.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion applications/nrf5340_audio/src/modules/sd_card_playback.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions samples/bluetooth/broadcast_config_tool/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
130 changes: 127 additions & 3 deletions samples/bluetooth/broadcast_config_tool/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 7ed3167

Please sign in to comment.