arduino-audio-tools
Loading...
Searching...
No Matches
ZephyrFile.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 <zephyr/fs/fs.h>
8#include <zephyr/storage/disk_access.h>
9
11
12namespace audio_tools {
13
23class ZephyrFile : public Stream {
24 public:
26
28
29 // non-copyable
30 ZephyrFile(const ZephyrFile&) = delete;
31 ZephyrFile& operator=(const ZephyrFile&) = delete;
32
33 // movable
34 ZephyrFile(ZephyrFile&& other) noexcept { moveFrom(other); }
35
37 if (this != &other) {
38 close();
39 moveFrom(other);
40 }
41 return *this;
42 }
43
44 bool open(const char* file_path, fs_mode_t mode = FS_O_READ) {
45 close();
46
48
49 int rc = fs_open(&f, file_path, mode);
50 if (rc != 0) {
51 LOGE("fs_open(%s) failed: %d", file_path, rc);
52 return false;
53 }
54
55 is_open = true;
56 path = file_path;
57
58 updateSize();
59
60 if (mode & FS_O_APPEND) {
61 fs_seek(&f, 0, FS_SEEK_END);
62 }
63
64 return true;
65 }
66
67 void close() {
68 if (!is_open) {
69 return;
70 }
71
72 fs_close(&f);
73
75
76 is_open = false;
77 path = nullptr;
78 file_size = 0;
79 }
80
81 bool isOpen() const { return is_open; }
82
83 operator bool() const { return is_open; }
84
85 //--------------------------------------------------------------------
86 // Stream API
87 //--------------------------------------------------------------------
88
89 int available() override {
90 if (!is_open) {
91 return 0;
92 }
93
94 size_t pos = position();
95
96 if (pos >= file_size) {
97 return 0;
98 }
99
100 return static_cast<int>(file_size - pos);
101 }
102
103 int read() override {
104 if (!is_open) {
105 return -1;
106 }
107
108 uint8_t value;
109
110 ssize_t rc = fs_read(&f, &value, 1);
111
112 if (rc != 1) {
113 return -1;
114 }
115
116 return value;
117 }
118
119 size_t readBytes(char* buffer, size_t len) {
120 if (!is_open || buffer == nullptr || len == 0) {
121 return 0;
122 }
123
124 ssize_t rc = fs_read(&f, buffer, len);
125
126 return rc > 0 ? static_cast<size_t>(rc) : 0;
127 }
128
129 size_t write(uint8_t value) override { return write(&value, 1); }
130
131 size_t write(const uint8_t* buffer, size_t len) override {
132 if (!is_open || buffer == nullptr || len == 0) {
133 return 0;
134 }
135
136 ssize_t rc = fs_write(&f, buffer, len);
137
138 if (rc <= 0) {
139 return 0;
140 }
141
142 size_t written = static_cast<size_t>(rc);
143
144 size_t pos = position();
145 if (pos > file_size) {
146 file_size = pos;
147 }
148
149 return written;
150 }
151
152 void flush() override {
153 if (is_open) {
154 fs_sync(&f);
155 }
156 }
157
158 int peek() override {
159 if (!is_open) {
160 return -1;
161 }
162
163 uint8_t value;
164
165 ssize_t rc = fs_read(&f, &value, 1);
166
167 if (rc != 1) {
168 return -1;
169 }
170
171 fs_seek(&f, -1, FS_SEEK_CUR);
172
173 return value;
174 }
175
176 //--------------------------------------------------------------------
177 // File API
178 //--------------------------------------------------------------------
179
180 bool seek(size_t pos) {
181 if (!is_open) {
182 return false;
183 }
184
185 return fs_seek(&f, static_cast<off_t>(pos), FS_SEEK_SET) == 0;
186 }
187
188 size_t position() const {
189 if (!is_open) {
190 return 0;
191 }
192
193 off_t pos = fs_tell(const_cast<fs_file_t*>(&f));
194
195 return pos >= 0 ? static_cast<size_t>(pos) : 0;
196 }
197
198 size_t size() const { return file_size; }
199
200 const char* name() const { return path; }
201
202 private:
203 fs_file_t f{};
204 bool is_open = false;
205 const char* path = nullptr;
206 size_t file_size = 0;
207
208 void updateSize() {
209 file_size = 0;
210
211 if (path == nullptr) {
212 return;
213 }
214
215 struct fs_dirent entry;
216
217 if (fs_stat(path, &entry) == 0) {
218 file_size = entry.size;
219 }
220 }
221
222 void moveFrom(ZephyrFile& other) {
223 f = other.f;
224 is_open = other.is_open;
225 path = other.path;
226 file_size = other.file_size;
227
229 other.is_open = false;
230 other.path = nullptr;
231 other.file_size = 0;
232 }
233};
234
236
237} // namespace audio_tools
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:136
Arduino File API for Zephyr.
Definition ZephyrFile.h:23
bool isOpen() const
Definition ZephyrFile.h:81
void flush() override
Definition ZephyrFile.h:152
ZephyrFile & operator=(ZephyrFile &&other) noexcept
Definition ZephyrFile.h:36
size_t size() const
Definition ZephyrFile.h:198
size_t write(uint8_t value) override
Definition ZephyrFile.h:129
~ZephyrFile()
Definition ZephyrFile.h:27
int peek() override
Definition ZephyrFile.h:158
void close()
Definition ZephyrFile.h:67
size_t write(const uint8_t *buffer, size_t len) override
Definition ZephyrFile.h:131
int available() override
Definition ZephyrFile.h:89
size_t position() const
Definition ZephyrFile.h:188
size_t readBytes(char *buffer, size_t len)
Definition ZephyrFile.h:119
const char * name() const
Definition ZephyrFile.h:200
bool open(const char *file_path, fs_mode_t mode=FS_O_READ)
Definition ZephyrFile.h:44
ZephyrFile(ZephyrFile &&other) noexcept
Definition ZephyrFile.h:34
ZephyrFile()
Definition ZephyrFile.h:25
bool seek(size_t pos)
Definition ZephyrFile.h:180
int read() override
Definition ZephyrFile.h:103
ZephyrFile & operator=(const ZephyrFile &)=delete
ZephyrFile(const ZephyrFile &)=delete
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508