|
| 1 | +#include "spiffs.h" |
| 2 | +#include <esp_vfs.h> |
| 3 | +#include <esp_log.h> |
| 4 | +#include <fcntl.h> |
| 5 | +#include <errno.h> |
| 6 | +#include "spiffs.h" |
| 7 | +#include "sdkconfig.h" |
| 8 | + |
| 9 | +static char tag[] = "spiffs_vfs"; |
| 10 | + |
| 11 | +/* |
| 12 | +static int spiffsErrMap(spiffs *fs) { |
| 13 | + int errorCode = SPIFFS_errno(fs); |
| 14 | + switch (errorCode) { |
| 15 | + case SPIFFS_ERR_FULL: |
| 16 | + return ENOSPC; |
| 17 | + case SPIFFS_ERR_NOT_FOUND: |
| 18 | + return ENOENT; |
| 19 | + case SPIFFS_ERR_FILE_EXISTS: |
| 20 | + return EEXIST; |
| 21 | + case SPIFFS_ERR_NOT_A_FILE: |
| 22 | + return EBADF; |
| 23 | + case SPIFFS_ERR_OUT_OF_FILE_DESCS: |
| 24 | + return ENFILE; |
| 25 | + default: { |
| 26 | + ESP_LOGE(tag, "We received SPIFFs error code %d but didn't know how to map to an errno", errorCode); |
| 27 | + return ENOMSG; |
| 28 | + } |
| 29 | + } |
| 30 | +} // spiffsErrMap |
| 31 | +*/ |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * Log the flags that are specified in an open() call. |
| 36 | + */ |
| 37 | +static void logFlags(int flags) { |
| 38 | + ESP_LOGD(tag, "flags:"); |
| 39 | + if (flags & O_APPEND) { |
| 40 | + ESP_LOGD(tag, "- O_APPEND"); |
| 41 | + } |
| 42 | + if (flags & O_CREAT) { |
| 43 | + ESP_LOGD(tag, "- O_CREAT"); |
| 44 | + } |
| 45 | + if (flags & O_TRUNC) { |
| 46 | + ESP_LOGD(tag, "- O_TRUNC"); |
| 47 | + } |
| 48 | + if (flags & O_RDONLY) { |
| 49 | + ESP_LOGD(tag, "- O_RDONLY"); |
| 50 | + } |
| 51 | + if (flags & O_WRONLY) { |
| 52 | + ESP_LOGD(tag, "- O_WRONLY"); |
| 53 | + } |
| 54 | + if (flags & O_RDWR) { |
| 55 | + ESP_LOGD(tag, "- O_RDWR"); |
| 56 | + } |
| 57 | +} // End of logFlags |
| 58 | + |
| 59 | + |
| 60 | +static size_t vfs_write(void *ctx, int fd, const void *data, size_t size) { |
| 61 | + ESP_LOGI(tag, ">> write fd=%d, data=0x%lx, size=%d", fd, (unsigned long)data, size); |
| 62 | + spiffs *fs = (spiffs *)ctx; |
| 63 | + size_t retSize = SPIFFS_write(fs, (spiffs_file)fd, (void *)data, size); |
| 64 | + return retSize; |
| 65 | +} // vfs_write |
| 66 | + |
| 67 | + |
| 68 | +static off_t vfs_lseek(void *ctx, int fd, off_t offset, int whence) { |
| 69 | + ESP_LOGI(tag, ">> lseek fd=%d, offset=%d, whence=%d", fd, (int)offset, whence); |
| 70 | + return 0; |
| 71 | +} // vfs_lseek |
| 72 | + |
| 73 | + |
| 74 | +static ssize_t vfs_read(void *ctx, int fd, void *dst, size_t size) { |
| 75 | + ESP_LOGI(tag, ">> read fd=%d, dst=0x%lx, size=%d", fd, (unsigned long)dst, size); |
| 76 | + spiffs *fs = (spiffs *)ctx; |
| 77 | + ssize_t retSize = SPIFFS_read(fs, (spiffs_file)fd, dst, size); |
| 78 | + return retSize; |
| 79 | +} // vfs_read |
| 80 | + |
| 81 | + |
| 82 | +/** |
| 83 | + * Open the file specified by path. The flags contain the instructions |
| 84 | + * on how the file is to be opened. For example: |
| 85 | + * |
| 86 | + * O_CREAT - Create the named file. |
| 87 | + * O_TRUNC - Truncate (empty) the file. |
| 88 | + * O_RDONLY - Open the file for reading only. |
| 89 | + * O_WRONLY - Open the file for writing only. |
| 90 | + * O_RDWR - Open the file for reading and writing. |
| 91 | + * O_APPEND - Append to the file. |
| 92 | + * |
| 93 | + * The mode are access mode flags. |
| 94 | + */ |
| 95 | +static int vfs_open(void *ctx, const char *path, int flags, int accessMode) { |
| 96 | + ESP_LOGI(tag, ">> open path=%s, flags=0x%x, accessMode=0x%x", path, flags, accessMode); |
| 97 | + logFlags(flags); |
| 98 | + spiffs *fs = (spiffs *)ctx; |
| 99 | + int spiffsFlags = 0; |
| 100 | + if (flags & O_CREAT) { |
| 101 | + spiffsFlags |= SPIFFS_O_CREAT; |
| 102 | + } |
| 103 | + if (flags & O_TRUNC) { |
| 104 | + spiffsFlags |= SPIFFS_O_TRUNC; |
| 105 | + } |
| 106 | + if (flags & O_RDONLY) { |
| 107 | + spiffsFlags |= SPIFFS_O_RDONLY; |
| 108 | + } |
| 109 | + if (flags & O_WRONLY) { |
| 110 | + spiffsFlags |= SPIFFS_O_WRONLY; |
| 111 | + } |
| 112 | + if (flags & O_RDWR) { |
| 113 | + spiffsFlags |= SPIFFS_O_RDWR; |
| 114 | + } |
| 115 | + if (flags & O_APPEND) { |
| 116 | + spiffsFlags |= SPIFFS_O_APPEND; |
| 117 | + } |
| 118 | + int rc = SPIFFS_open(fs, path, spiffsFlags, accessMode); |
| 119 | + return rc; |
| 120 | +} // vfs_open |
| 121 | + |
| 122 | + |
| 123 | +static int vfs_close(void *ctx, int fd) { |
| 124 | + ESP_LOGI(tag, ">> close fd=%d", fd); |
| 125 | + spiffs *fs = (spiffs *)ctx; |
| 126 | + int rc = SPIFFS_close(fs, (spiffs_file)fd); |
| 127 | + return rc; |
| 128 | +} // vfs_close |
| 129 | + |
| 130 | + |
| 131 | +static int vfs_fstat(void *ctx, int fd, struct stat *st) { |
| 132 | + ESP_LOGI(tag, ">> fstat fd=%d", fd); |
| 133 | + return 0; |
| 134 | +} // vfs_fstat |
| 135 | + |
| 136 | + |
| 137 | +static int vfs_stat(void *ctx, const char *path, struct stat *st) { |
| 138 | + ESP_LOGI(tag, ">> stat path=%s", path); |
| 139 | + return 0; |
| 140 | +} // vfs_stat |
| 141 | + |
| 142 | + |
| 143 | +static int vfs_link(void *ctx, const char *oldPath, const char *newPath) { |
| 144 | + ESP_LOGI(tag, ">> link oldPath=%s, newPath=%s", oldPath, newPath); |
| 145 | + return 0; |
| 146 | +} // vfs_link |
| 147 | + |
| 148 | + |
| 149 | +static int vfs_rename(void *ctx, const char *oldPath, const char *newPath) { |
| 150 | + ESP_LOGI(tag, ">> rename oldPath=%s, newPath=%s", oldPath, newPath); |
| 151 | + spiffs *fs = (spiffs *)ctx; |
| 152 | + int rc = SPIFFS_rename(fs, oldPath, newPath); |
| 153 | + return rc; |
| 154 | +} // vfs_rename |
| 155 | + |
| 156 | + |
| 157 | +/** |
| 158 | + * Register the VFS at the specified mount point. |
| 159 | + * The callback functions are registered to handle the |
| 160 | + * different functions that may be requested against the |
| 161 | + * VFS. |
| 162 | + */ |
| 163 | +void spiffs_registerVFS(char *mountPoint, spiffs *fs) { |
| 164 | + esp_vfs_t vfs; |
| 165 | + esp_err_t err; |
| 166 | + |
| 167 | + vfs.fd_offset = 0; |
| 168 | + vfs.flags = ESP_VFS_FLAG_CONTEXT_PTR; |
| 169 | + vfs.write_p = vfs_write; |
| 170 | + vfs.lseek_p = vfs_lseek; |
| 171 | + vfs.read_p = vfs_read; |
| 172 | + vfs.open_p = vfs_open; |
| 173 | + vfs.close_p = vfs_close; |
| 174 | + vfs.fstat_p = vfs_fstat; |
| 175 | + vfs.stat_p = vfs_stat; |
| 176 | + vfs.link_p = vfs_link; |
| 177 | + vfs.rename_p = vfs_rename; |
| 178 | + |
| 179 | + err = esp_vfs_register(mountPoint, &vfs, (void *)fs); |
| 180 | + if (err != ESP_OK) { |
| 181 | + ESP_LOGE(tag, "esp_vfs_register: err=%d", err); |
| 182 | + } |
| 183 | +} // spiffs_registerVFS |
0 commit comments