forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemAdapter.cpp
More file actions
474 lines (387 loc) · 11.8 KB
/
Copy pathFilesystemAdapter.cpp
File metadata and controls
474 lines (387 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// matth-x/ArduinoOcpp
// Copyright Matthias Akstaller 2019 - 2023
// MIT License
#include <ArduinoOcpp/Core/FilesystemAdapter.h>
#include <ArduinoOcpp/Core/ConfigurationOptions.h> //FilesystemOpt
#include <ArduinoOcpp/Debug.h>
#ifndef AO_DEACTIVATE_FLASH
/*
* Platform specific implementations. Currently supported:
* - Arduino LittleFs
* - Arduino SPIFFS
* - ESP-IDF SPIFFS
* - POSIX-like API (tested on Ubuntu 20.04)
*
* You can add support for other file systems by passing a custom adapter to ocpp_initialize(...)
*/
#if AO_USE_FILEAPI == ARDUINO_LITTLEFS || AO_USE_FILEAPI == ARDUINO_SPIFFS
#if AO_USE_FILEAPI == ARDUINO_LITTLEFS
#include <LITTLEFS.h>
#define USE_FS LITTLEFS
#elif AO_USE_FILEAPI == ARDUINO_SPIFFS
#include <FS.h>
#define USE_FS SPIFFS
#endif
namespace ArduinoOcpp {
class ArduinoFileAdapter : public FileAdapter {
File file;
public:
ArduinoFileAdapter(File&& file) : file(file) {}
~ArduinoFileAdapter() {
if (file) {
file.close();
}
}
int read() override {
return file.read();
};
size_t read(char *buf, size_t len) override {
return file.readBytes(buf, len);
}
size_t write(const char *buf, size_t len) override {
return file.printf("%.*s", len, buf);
}
size_t seek(size_t offset) override {
return file.seek(offset);
}
};
class ArduinoFilesystemAdapter : public FilesystemAdapter {
private:
bool valid = false;
FilesystemOpt config;
public:
ArduinoFilesystemAdapter(FilesystemOpt config) : config(config) {
valid = true;
if (config.mustMount()) {
#if AO_USE_FILEAPI == ARDUINO_LITTLEFS
if(!USE_FS.begin(config.formatOnFail())) {
AO_DBG_ERR("Error while mounting LITTLEFS");
valid = false;
} else {
AO_DBG_DEBUG("LittleFS mount success");
}
#elif AO_USE_FILEAPI == ARDUINO_SPIFFS
//ESP8266
SPIFFSConfig cfg;
cfg.setAutoFormat(config.formatOnFail());
SPIFFS.setConfig(cfg);
if (!SPIFFS.begin()) {
AO_DBG_ERR("Unable to initialize: unable to mount SPIFFS");
valid = false;
}
#else
#error
#endif
} //end if mustMount()
}
~ArduinoFilesystemAdapter() {
if (config.mustMount()) {
USE_FS.end();
}
}
operator bool() {return valid;}
int stat(const char *path, size_t *size) override {
if (!USE_FS.exists(path)) {
return -1;
}
File f = USE_FS.open(path, "r");
if (!f) {
return -1;
}
int status = -1;
if (!f.isDirectory()) {
*size = f.size();
status = 0;
} else {
//fetch more information for directory when ArduinoOcpp also uses them
//status = 0;
}
f.close();
return status;
}
std::unique_ptr<FileAdapter> open(const char *fn, const char *mode) override {
File file = USE_FS.open(fn, mode);
if (file && !file.isDirectory()) {
AO_DBG_DEBUG("File open successful: %s", fn);
return std::unique_ptr<FileAdapter>(new ArduinoFileAdapter(std::move(file)));
} else {
return nullptr;
}
}
bool remove(const char *fn) override {
return USE_FS.remove(fn);
};
int ftw_root(std::function<int(const char *fpath)> fn) {
#if AO_USE_FILEAPI == ARDUINO_LITTLEFS
auto dir = USE_FS.open(AO_FILENAME_PREFIX);
if (!dir) {
AO_DBG_ERR("cannot open root directory: " AO_FILENAME_PREFIX);
return -1;
}
int err = 0;
while (auto entry = dir.openNextFile()) {
char fname [AO_MAX_PATH_SIZE];
auto ret = snprintf(fname, AO_MAX_PATH_SIZE, "%s", entry.name() + strlen(AO_FILENAME_PREFIX));
entry.close();
if (ret < 0 || ret >= AO_MAX_PATH_SIZE) {
AO_DBG_ERR("fn error: %i", ret);
return -1;
}
err = fn(fname);
if (err) {
break;
}
}
return err;
#elif AO_USE_FILEAPI == ARDUINO_SPIFFS
auto dir = USE_FS.openDir(AO_FILENAME_PREFIX);
int err = 0;
while (dir.next()) {
auto fname = dir.fileName();
if (fname.c_str()) {
err = fn(fname.c_str() + strlen(AO_FILENAME_PREFIX));
} else {
AO_DBG_ERR("fs error");
err = -1;
}
if (err) {
break;
}
}
return err;
#else
#error
#endif
}
};
std::weak_ptr<FilesystemAdapter> filesystemCache;
std::shared_ptr<FilesystemAdapter> makeDefaultFilesystemAdapter(FilesystemOpt config) {
if (auto cached = filesystemCache.lock()) {
return cached;
}
if (!config.accessAllowed()) {
AO_DBG_DEBUG("Access to Arduino FS not allowed by config");
return nullptr;
}
auto fs_concrete = new ArduinoFilesystemAdapter(config);
auto fs = std::shared_ptr<FilesystemAdapter>(fs_concrete);
filesystemCache = fs;
if (*fs_concrete) {
return fs;
} else {
return nullptr;
}
}
} //end namespace ArduinoOcpp
#elif AO_USE_FILEAPI == ESPIDF_SPIFFS
#include <sys/stat.h>
#include <dirent.h>
#include "esp_spiffs.h"
namespace ArduinoOcpp {
class EspIdfFileAdapter : public FileAdapter {
FILE *file {nullptr};
public:
EspIdfFileAdapter(FILE *file) : file(file) {}
~EspIdfFileAdapter() {
fclose(file);
}
size_t read(char *buf, size_t len) override {
return fread(buf, 1, len, file);
}
size_t write(const char *buf, size_t len) override {
return fwrite(buf, 1, len, file);
}
size_t seek(size_t offset) override {
return fseek(file, offset, SEEK_SET);
}
int read() {
return fgetc(file);
}
};
class EspIdfFilesystemAdapter : public FilesystemAdapter {
public:
FilesystemOpt config;
public:
EspIdfFilesystemAdapter(FilesystemOpt config) : config(config) { }
~EspIdfFilesystemAdapter() {
if (config.mustMount()) {
esp_vfs_spiffs_unregister("ao"); //partition label
AO_DBG_DEBUG("SPIFFS unmounted");
}
}
int stat(const char *path, size_t *size) override {
struct ::stat st;
auto ret = ::stat(path, &st);
if (ret == 0) {
*size = st.st_size;
}
return ret;
}
std::unique_ptr<FileAdapter> open(const char *fn, const char *mode) override {
auto file = fopen(fn, mode);
if (file) {
return std::unique_ptr<FileAdapter>(new EspIdfFileAdapter(std::move(file)));
} else {
AO_DBG_DEBUG("Failed to open file path %s", fn);
return nullptr;
}
}
bool remove(const char *fn) override {
return unlink(fn) == 0;
}
int ftw_root(std::function<int(const char *fpath)> fn) {
//open AO root directory
char dname [AO_MAX_PATH_SIZE];
auto dlen = snprintf(dname, AO_MAX_PATH_SIZE, "%s", AO_FILENAME_PREFIX);
if (dlen < 0 || dlen >= AO_MAX_PATH_SIZE) {
AO_DBG_ERR("fn error: %i", dlen);
return -1;
}
// trim trailing '/' if not root directory
if (dlen >= 2 && dname[dlen - 1] == '/') {
dname[dlen - 1] = '\0';
}
auto dir = opendir(dname);
if (!dir) {
AO_DBG_ERR("cannot open root directory: %s", dname);
return -1;
}
int err = 0;
while (auto entry = readdir(dir)) {
err = fn(entry->d_name);
if (err) {
break;
}
}
closedir(dir);
return err;
}
};
std::weak_ptr<FilesystemAdapter> filesystemCache;
std::shared_ptr<FilesystemAdapter> makeDefaultFilesystemAdapter(FilesystemOpt config) {
if (auto cached = filesystemCache.lock()) {
return cached;
}
if (!config.accessAllowed()) {
AO_DBG_DEBUG("Access to ESP-IDF SPIFFS not allowed by config");
return nullptr;
}
bool mounted = true;
if (config.mustMount()) {
mounted = false;
esp_vfs_spiffs_conf_t conf = {
.base_path = AO_FILENAME_PREFIX,
.partition_label = "ao", //also see deconstructor
.max_files = 5,
.format_if_mount_failed = config.formatOnFail()
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret == ESP_OK) {
mounted = true;
AO_DBG_DEBUG("SPIFFS mounted");
} else {
if (ret == ESP_FAIL) {
AO_DBG_ERR("Failed to mount or format filesystem");
} else if (ret == ESP_ERR_NOT_FOUND) {
AO_DBG_ERR("Failed to find SPIFFS partition");
} else {
AO_DBG_ERR("Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
}
}
}
if (mounted) {
auto fs = std::shared_ptr<FilesystemAdapter>(new EspIdfFilesystemAdapter(config));
filesystemCache = fs;
return fs;
} else {
return nullptr;
}
}
} //end namespace ArduinoOcpp
#elif AO_USE_FILEAPI == POSIX_FILEAPI
#include <cstdio>
#include <sys/stat.h>
#include <dirent.h>
namespace ArduinoOcpp {
class PosixFileAdapter : public FileAdapter {
FILE *file {nullptr};
public:
PosixFileAdapter(FILE *file) : file(file) {}
~PosixFileAdapter() {
fclose(file);
}
size_t read(char *buf, size_t len) override {
return fread(buf, 1, len, file);
}
size_t write(const char *buf, size_t len) override {
return fwrite(buf, 1, len, file);
}
size_t seek(size_t offset) override {
return fseek(file, offset, SEEK_SET);
}
int read() {
return fgetc(file);
}
};
class PosixFilesystemAdapter : public FilesystemAdapter {
public:
FilesystemOpt config;
public:
PosixFilesystemAdapter(FilesystemOpt config) : config(config) { }
~PosixFilesystemAdapter() = default;
int stat(const char *path, size_t *size) override {
struct ::stat st;
auto ret = ::stat(path, &st);
if (ret == 0) {
*size = st.st_size;
}
return ret;
}
std::unique_ptr<FileAdapter> open(const char *fn, const char *mode) override {
auto file = fopen(fn, mode);
if (file) {
return std::unique_ptr<FileAdapter>(new PosixFileAdapter(std::move(file)));
} else {
AO_DBG_DEBUG("Failed to open file path %s", fn);
return nullptr;
}
}
bool remove(const char *fn) override {
return ::remove(fn) == 0;
}
int ftw_root(std::function<int(const char *fpath)> fn) {
auto dir = opendir(AO_FILENAME_PREFIX); // use c_str() to convert the path string to a C-style string
if (!dir) {
AO_DBG_ERR("cannot open root directory: " AO_FILENAME_PREFIX);
return -1;
}
int err = 0;
while (auto entry = readdir(dir)) {
err = fn(entry->d_name);
if (err) {
break;
}
}
closedir(dir);
return err;
}
};
std::weak_ptr<FilesystemAdapter> filesystemCache;
std::shared_ptr<FilesystemAdapter> makeDefaultFilesystemAdapter(FilesystemOpt config) {
if (auto cached = filesystemCache.lock()) {
return cached;
}
if (!config.accessAllowed()) {
AO_DBG_DEBUG("Access to FS not allowed by config");
return nullptr;
}
if (config.mustMount()) {
AO_DBG_DEBUG("Skip mounting on UNIX host");
}
auto fs = std::shared_ptr<FilesystemAdapter>(new PosixFilesystemAdapter(config));
filesystemCache = fs;
return fs;
}
} //end namespace ArduinoOcpp
#endif //switch-case AO_USE_FILEAPI
#endif //!AO_DEACTIVATE_FLASH