arduino-audio-tools
Loading...
Searching...
No Matches
ZephyrSD.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef IS_ZEPHYR
4# error("ZephyrSD only supported by zephyr")
5#endif
6
7#include "ZephyrFile.h"
8#include <zephyr/fs/fs.h>
9#include <zephyr/storage/disk_access.h>
10#include <string>
11#include <zephyr/sys/util.h>
12
13BUILD_ASSERT(IS_ENABLED(CONFIG_FILE_SYSTEM), "Filesystem required");
14
15namespace audio_tools {
26public:
27 ZephyrSDClass() = default;
29 unmountSD();
30 }
31
32 bool begin(const char* diskName="SD", const char* mountPoint="/SD" ) {
33 mp.mnt_point = mountPoint;
34 return begin(diskName, mp);
35 }
36
37 bool begin(const char* diskName, fs_mount_t mountInfo){
39 mp = mountInfo;
40 return mountSD();
41 }
42
43 void end() {
44 unmountSD();
45 }
46
47 bool isMounted() const {
48 return mounted;
49 }
50
51 ZephyrFile open(const char *path, const char *mode = "r", bool create = false) {
52 if (!mounted) {
53 LOGE("SD not mounted");
54 return ZephyrFile();
55 }
56
57 fs_mode_t fs_mode = parseMode(mode, create);
58
59 ZephyrFile file;
60 file.open(path, fs_mode);
61 return file;
62 }
63
64 ZephyrFile open(const char *path, fs_mode_t mode) {
65 if (!mounted) {
66 LOGE("SD not mounted");
67 return ZephyrFile();
68 }
69
70 ZephyrFile file;
71 file.open(path, mode);
72 return file;
73 }
74
75 bool exists(const char *path) const {
76 struct fs_dirent entry;
77 return fs_stat(path, &entry) == 0;
78 }
79
80 bool remove(const char *path) {
81 int ret = fs_unlink(path);
82 return logErr(ret, "fs_unlink", path);
83 }
84
85 bool rename(const char *from, const char *to) {
86 int ret = fs_rename(from, to);
87 return logErr(ret, "fs_rename", from, to);
88 }
89
90 bool mkdir(const char *path) {
91 int ret = fs_mkdir(path);
92 return logErr(ret, "fs_mkdir", path);
93 }
94
95 bool rmdir(const char *path) {
96 int ret = fs_unlink(path);
97 return logErr(ret, "fs_rmdir", path);
98 }
99
100 size_t fileSize(const char *path) const {
101 struct fs_dirent entry;
102 if (fs_stat(path, &entry) == 0) {
103 return entry.size;
104 }
105 return 0;
106 }
107
108 bool isDirectory(const char *path) {
109 struct fs_dirent entry;
110
111 int rc = fs_stat(path, &entry);
112 if (rc != 0) {
113 return false; // doesn't exist or not accessible
114 }
115
116 return (entry.type == FS_DIR_ENTRY_DIR);
117 }
118
119 const char* mountPoint() {
120 return mp.mnt_point;
121 }
122
124
125protected:
126 std::string disk_name;
127 bool mounted = false;
129 .type = FS_FATFS,
130 .mnt_point = "/SD",
131 .fs_data = nullptr
132 };
133
134 fs_mode_t parseMode(const char *mode, bool create) {
136
137 if (!mode) return FS_O_READ;
138
139 if (mode[0] == 'r') {
141 }
142 else if (mode[0] == 'w') {
144 }
145 else if (mode[0] == 'a') {
147 }
148
149 if (create) {
151 }
152
153 return fs_mode;
154 }
155
156 bool mountSD() {
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);
160 return false;
161 }
162
165
168
169 if (sector_count && sector_size) {
171 LOGI("SD: %llu MiB", (unsigned long long)mb);
172 }
173
174 int ret2 = fs_mount(&mp);
175 if (ret2 != 0) {
176 LOGE("fs_mount failed: %d", ret2);
177 return false;
178 }
179
180 mounted = true;
181 LOGI("SD mounted at %s", mp.mnt_point);
182 return true;
183 }
184
185 void unmountSD() {
186 if (!mounted) return;
187
188 int ret = fs_unmount(&mp);
189 if (ret != 0) {
190 LOGE("fs_unmount failed: %d", ret);
191 }
192
193 mounted = false;
194 }
195
196 bool logErr(int ret, const char *op, const char *path) {
197 if (ret != 0) {
198 LOGE("%s(%s) failed: %d", op, path, ret);
199 return false;
200 }
201 return true;
202 }
203
204 bool logErr(int ret, const char *op, const char *a, const char *b) {
205 if (ret != 0) {
206 LOGE("%s(%s -> %s) failed: %d", op, a, b, ret);
207 return false;
208 }
209 return true;
210 }
211
212};
213
216
217} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
BUILD_ASSERT(IS_ENABLED(CONFIG_FILE_SYSTEM), "Filesystem required")
Arduino File API for Zephyr.
Definition ZephyrFile.h:23
bool open(const char *file_path, fs_mode_t mode=FS_O_READ)
Definition ZephyrFile.h:44
Arduino SD API for Zephyr.
Definition ZephyrSD.h:25
~ZephyrSDClass()
Definition ZephyrSD.h:28
bool rename(const char *from, const char *to)
Definition ZephyrSD.h:85
bool mounted
Definition ZephyrSD.h:127
bool exists(const char *path) const
Definition ZephyrSD.h:75
bool logErr(int ret, const char *op, const char *path)
Definition ZephyrSD.h:196
bool remove(const char *path)
Definition ZephyrSD.h:80
bool isDirectory(const char *path)
Definition ZephyrSD.h:108
ZephyrFile open(const char *path, const char *mode="r", bool create=false)
Definition ZephyrSD.h:51
std::string disk_name
Definition ZephyrSD.h:126
fs_mode_t parseMode(const char *mode, bool create)
Definition ZephyrSD.h:134
bool rmdir(const char *path)
Definition ZephyrSD.h:95
size_t fileSize(const char *path) const
Definition ZephyrSD.h:100
ZephyrFile open(const char *path, fs_mode_t mode)
Definition ZephyrSD.h:64
void unmountSD()
Definition ZephyrSD.h:185
void end()
Definition ZephyrSD.h:43
bool isMounted() const
Definition ZephyrSD.h:47
bool mkdir(const char *path)
Definition ZephyrSD.h:90
bool begin(const char *diskName, fs_mount_t mountInfo)
Definition ZephyrSD.h:37
bool mountSD()
Definition ZephyrSD.h:156
fs_mount_t & mountInfo()
Definition ZephyrSD.h:123
bool begin(const char *diskName="SD", const char *mountPoint="/SD")
Definition ZephyrSD.h:32
bool logErr(int ret, const char *op, const char *a, const char *b)
Definition ZephyrSD.h:204
const char * mountPoint()
Definition ZephyrSD.h:119
fs_mount_t mp
Definition ZephyrSD.h:128
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
static ZephyrSDClass SD
Access to SD object.
Definition ZephyrSD.h:215
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508