diff options
author | Joey Castillo <joeycastillo@utexas.edu> | 2022-05-09 12:14:42 -0400 |
---|---|---|
committer | Joey Castillo <joeycastillo@utexas.edu> | 2022-05-09 12:14:42 -0400 |
commit | 21ee056e26c3e158cf56f8577169f86b8d577a65 (patch) | |
tree | 12d2841a2d2ccd33eb1fbb73f7f1ad990d1cd140 /movement | |
parent | 91b436237aaf39e2328b9380042aa40e06faa4cb (diff) | |
download | Sensor-Watch-21ee056e26c3e158cf56f8577169f86b8d577a65.tar.gz Sensor-Watch-21ee056e26c3e158cf56f8577169f86b8d577a65.tar.bz2 Sensor-Watch-21ee056e26c3e158cf56f8577169f86b8d577a65.zip |
first pass at filesystem in movement
Diffstat (limited to 'movement')
-rw-r--r-- | movement/filesystem.c | 243 | ||||
-rw-r--r-- | movement/filesystem.h | 79 | ||||
-rwxr-xr-x | movement/make/Makefile | 4 |
3 files changed, 326 insertions, 0 deletions
diff --git a/movement/filesystem.c b/movement/filesystem.c new file mode 100644 index 00000000..2a1319fb --- /dev/null +++ b/movement/filesystem.c @@ -0,0 +1,243 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <peripheral_clk_config.h> +#include "filesystem.h" +#include "watch.h" +#include "lfs.h" +#include "hpl_flash.h" + +int lfs_storage_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); +int lfs_storage_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); +int lfs_storage_erase(const struct lfs_config *cfg, lfs_block_t block); +int lfs_storage_sync(const struct lfs_config *cfg); + +int lfs_storage_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) { + (void) cfg; + return !watch_storage_read(block, off, (void *)buffer, size); +} + +int lfs_storage_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) { + (void) cfg; + return !watch_storage_write(block, off, (void *)buffer, size); +} + +int lfs_storage_erase(const struct lfs_config *cfg, lfs_block_t block) { + (void) cfg; + return !watch_storage_erase(block); +} + +int lfs_storage_sync(const struct lfs_config *cfg) { + (void) cfg; + return !watch_storage_sync(); +} + +const struct lfs_config cfg = { + // block device operations + .read = lfs_storage_read, + .prog = lfs_storage_prog, + .erase = lfs_storage_erase, + .sync = lfs_storage_sync, + + // block device configuration + .read_size = 16, + .prog_size = NVMCTRL_PAGE_SIZE, + .block_size = NVMCTRL_ROW_SIZE, + .block_count = NVMCTRL_RWWEE_PAGES / 4, + .cache_size = NVMCTRL_PAGE_SIZE, + .lookahead_size = 16, + .block_cycles = 100, +}; + +static lfs_t lfs; +static lfs_file_t file; +static struct lfs_info info; + +static int _traverse_df_cb(void *p, lfs_block_t block) { + (void) block; + uint32_t *nb = p; + *nb += 1; + return 0; +} + +int filesystem_get_free_space(void) { + int err; + + uint32_t free_blocks = 0; + err = lfs_fs_traverse(&lfs, _traverse_df_cb, &free_blocks); + if(err < 0){ + return err; + } + + uint32_t available = cfg.block_count * cfg.block_size - free_blocks * cfg.block_size; + + return available; +} + +static int filesystem_ls(lfs_t *lfs, const char *path) { + lfs_dir_t dir; + int err = lfs_dir_open(lfs, &dir, path); + if (err) { + return err; + } + + struct lfs_info info; + while (true) { + int res = lfs_dir_read(lfs, &dir, &info); + if (res < 0) { + return res; + } + + if (res == 0) { + break; + } + + switch (info.type) { + case LFS_TYPE_REG: printf("file "); break; + case LFS_TYPE_DIR: printf("dir "); break; + default: printf("? "); break; + } + + printf("%4ld bytes ", info.size); + + printf("%s\n", info.name); + } + + err = lfs_dir_close(lfs, &dir); + if (err) { + return err; + } + + return 0; +} + +bool filesystem_init(void) { + int err = lfs_mount(&lfs, &cfg); + + // reformat if we can't mount the filesystem + // this should only happen on the first boot + if (err) { + err = lfs_format(&lfs, &cfg); + if (err) return false; + err = lfs_mount(&lfs, &cfg) == LFS_ERR_OK; + } + + return err == LFS_ERR_OK; +} + +bool filesystem_file_exists(char *filename) { + info.type = 0; + lfs_stat(&lfs, filename, &info); + return info.type == LFS_TYPE_REG; +} + +bool filesystem_rm(char *filename) { + info.type = 0; + lfs_stat(&lfs, filename, &info); + if (filesystem_file_exists(filename)) { + return lfs_remove(&lfs, filename) == LFS_ERR_OK; + } else { + printf("rm: %s: No such file\n", filename); + return false; + } +} + +static int32_t filesystem_get_file_size(char *filename) { + if (filesystem_file_exists(filename)) { + return info.size; // info struct was just populated by filesystem_file_exists + } + + return -1; +} + +bool filesystem_read_file(char *filename, char *buf, int32_t length) { + memset(buf, 0, length); + int32_t file_size = filesystem_get_file_size(filename); + if (file_size > 0) { + int err = lfs_file_open(&lfs, &file, filename, LFS_O_RDONLY); + if (err) return false; + err = lfs_file_read(&lfs, &file, buf, min(length, file_size)); + if (err) return false; + return lfs_file_close(&lfs, &file) == LFS_ERR_OK; + } + + return false; +} + +static void filesystem_cat(char *filename) { + info.type = 0; + lfs_stat(&lfs, filename, &info); + if (filesystem_file_exists(filename)) { + if (info.size > 0) { + char *buf = malloc(info.size + 1); + filesystem_read_file(filename, buf, info.size); + printf("%s\n", buf); + free(buf); + } else { + printf("\n"); + } + } else { + printf("cat: %s: No such file\n", filename); + } +} + +bool filesystem_write_file(char *filename, char *text, int32_t length) { + int err = lfs_file_open(&lfs, &file, filename, LFS_O_RDWR | LFS_O_CREAT | LFS_O_TRUNC); + if (err) return false; + err = lfs_file_write(&lfs, &file, text, length); + if (err) return false; + return lfs_file_close(&lfs, &file) == LFS_ERR_OK; +} + +void filesystem_process_command(char *line) { + printf("$ %s", line); + char *command = strtok(line, " \n"); + + if (strcmp(command, "ls") == 0) { + char *directory = strtok(NULL, " \n"); + if (directory == NULL) { + filesystem_ls(&lfs, "/"); + } else { + printf("usage: ls\n"); + } + } else if (strcmp(command, "cat") == 0) { + char *filename = strtok(NULL, " \n"); + if (filename == NULL) { + printf("usage: cat file\n"); + } else { + filesystem_cat(filename); + } + } else if (strcmp(command, "df") == 0) { + printf("free space: %d bytes\n", filesystem_get_free_space()); + } else if (strcmp(command, "rm") == 0) { + char *filename = strtok(NULL, " \n"); + if (filename == NULL) { + printf("usage: rm file\n"); + } else { + filesystem_rm(filename); + } + } else if (strcmp(command, "echo") == 0) { + char *text = malloc(248); + memset(text, 0, 248); + size_t pos = 0; + char *word = strtok(NULL, " \n"); + while (strcmp(word, ">")) { + sprintf(text + pos, "%s ", word); + pos += strlen(word) + 1; + word = strtok(NULL, " \n"); + if (word == NULL) break; + } + text[strlen(text) - 1] = 0; + char *filename = strtok(NULL, " \n"); + if (filename == NULL) { + printf("usage: echo text > file\n"); + } else if (strchr(filename, '/') || strchr(filename, '\\')) { + printf("subdirectories are not supported\n"); + } else { + filesystem_write_file(filename, text, strlen(text)); + } + free(text); + } else { + printf("%s: command not found\n", command); + } +} diff --git a/movement/filesystem.h b/movement/filesystem.h new file mode 100644 index 00000000..c9a5804c --- /dev/null +++ b/movement/filesystem.h @@ -0,0 +1,79 @@ +/* + * MIT License + * + * Copyright (c) 2022 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef FILESYSTEM_H_ +#define FILESYSTEM_H_ +#include <stdio.h> +#include <stdbool.h> +#include "watch.h" + +/** @brief Initializes and mounts the tiny 8kb filesystem, formatting it if need be. + * @return true if the filesystem was mounted successfully. + */ +bool filesystem_init(void); + +/** @brief Gets the space available on the filesystem. + * @return the free space in bytes + */ +int filesystem_get_free_space(void); + +/** @brief Checks for the existence of a file on the filesystem. + * @param filename the file you wish to check + * @return true if the file exists; false otherwise + */ +bool filesystem_file_exists(char *filename); + +/** @brief Removes a file on the filesystem. + * @param filename the file you wish to remove + * @return true if the file was deleted successfully; false otherwise + */ +bool filesystem_rm(char *filename); + +/** @brief Reads a file from the filesystem into a buffer + * @param filename the file you wish to read + * @param buf A buffer of at least length bytes; the file will be read into this buffer + * @param length The number of bytes to read + * @return true if the read was successful; false otherwise + * @note This function will set buf to zero and read all bytes of the file into the buffer. + * If you are reading a raw value (say you wrote a uint32 to a file), you can read back + * the value by passing in the file's length for length. If you wish to treat the buffer + * as a null-terminated string, allocate a buffer one byte longer than the file's length, + * and the last byte will be guaranteed to be 0. + */ +bool filesystem_read_file(char *filename, char *buf, int32_t length); + +/** @brief Writes file to the filesystem + * @param filename the file you wish to write + * @param text The contents of the file + * @param length The number of bytes to write + * @return true if the write was successful; false otherwise + */ +bool filesystem_write_file(char *filename, char *text, int32_t length); + +/** @brief Handles the interactive file browser when Movement is plugged in to USB. + * @param line The command that the user typed into the serial console. + */ +void filesystem_process_command(char *line); + +#endif // FILESYSTEM_H_ diff --git a/movement/make/Makefile b/movement/make/Makefile index 0936a26c..1c880fc2 100755 --- a/movement/make/Makefile +++ b/movement/make/Makefile @@ -16,6 +16,7 @@ INCLUDES += \ -I../watch_faces/complication/ \ -I../watch_faces/sensor/ \ -I../watch_faces/demo/ \ + -I../../littlefs/ \ -I../lib/TOTP-MCU/ \ -I../lib/sunriset/ \ -I../lib/vsop87/ \ @@ -33,7 +34,10 @@ SRCS += \ ../lib/sunriset/sunriset.c \ ../lib/vsop87/vsop87a_milli.c \ ../lib/astrolib/astrolib.c \ + ../../littlefs/lfs.c \ + ../../littlefs/lfs_util.c \ ../movement.c \ + ../filesystem.c \ ../watch_faces/clock/simple_clock_face.c \ ../watch_faces/clock/world_clock_face.c \ ../watch_faces/clock/beats_face.c \ |