4# error("ZephyrSD only supported by zephyr")
8#include <zephyr/fs/fs.h>
9#include <zephyr/storage/disk_access.h>
11#include <zephyr/sys/util.h>
13BUILD_ASSERT(IS_ENABLED(CONFIG_FILE_SYSTEM),
"Filesystem required");
51 ZephyrFile open(
const char *path,
const char *mode =
"r",
bool create =
false) {
53 LOGE(
"SD not mounted");
57 fs_mode_t fs_mode =
parseMode(mode, create);
60 file.
open(path, fs_mode);
66 LOGE(
"SD not mounted");
71 file.
open(path, mode);
75 bool exists(
const char *path)
const {
76 struct fs_dirent entry;
77 return fs_stat(path, &entry) == 0;
81 int ret = fs_unlink(path);
82 return logErr(ret,
"fs_unlink", path);
85 bool rename(
const char *from,
const char *to) {
86 int ret = fs_rename(from, to);
87 return logErr(ret,
"fs_rename", from, to);
91 int ret = fs_mkdir(path);
92 return logErr(ret,
"fs_mkdir", path);
96 int ret = fs_unlink(path);
97 return logErr(ret,
"fs_rmdir", path);
101 struct fs_dirent entry;
102 if (fs_stat(path, &entry) == 0) {
109 struct fs_dirent entry;
111 int rc = fs_stat(path, &entry);
116 return (entry.type == FS_DIR_ENTRY_DIR);
135 fs_mode_t fs_mode = FS_O_READ;
137 if (!mode)
return FS_O_READ;
139 if (mode[0] ==
'r') {
142 else if (mode[0] ==
'w') {
143 fs_mode = FS_O_WRITE | FS_O_TRUNC;
145 else if (mode[0] ==
'a') {
146 fs_mode = FS_O_WRITE | FS_O_APPEND;
150 fs_mode |= FS_O_CREATE;
157 int ret = disk_access_init(
disk_name.c_str());
158 if (ret != 0 && ret != -EALREADY) {
159 LOGE(
"disk_access_init(%s) failed: %d",
disk_name.c_str(), ret);
163 uint32_t sector_count = 0;
164 uint32_t sector_size = 0;
166 disk_access_ioctl(
disk_name.c_str(), DISK_IOCTL_GET_SECTOR_COUNT, §or_count);
167 disk_access_ioctl(
disk_name.c_str(), DISK_IOCTL_GET_SECTOR_SIZE, §or_size);
169 if (sector_count && sector_size) {
170 uint64_t mb = ((uint64_t)sector_count * sector_size) >> 20;
171 LOGI(
"SD: %llu MiB", (
unsigned long long)mb);
174 int ret2 = fs_mount(&
mp);
176 LOGE(
"fs_mount failed: %d", ret2);
181 LOGI(
"SD mounted at %s",
mp.mnt_point);
188 int ret = fs_unmount(&
mp);
190 LOGE(
"fs_unmount failed: %d", ret);
196 bool logErr(
int ret,
const char *op,
const char *path) {
198 LOGE(
"%s(%s) failed: %d", op, path, ret);
204 bool logErr(
int ret,
const char *op,
const char *a,
const char *b) {
206 LOGE(
"%s(%s -> %s) failed: %d", op, a, b, ret);
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
BUILD_ASSERT(IS_ENABLED(CONFIG_FILE_SYSTEM), "Filesystem required")