-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.cpp
More file actions
230 lines (204 loc) · 5.7 KB
/
Copy pathstorage.cpp
File metadata and controls
230 lines (204 loc) · 5.7 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
#include "storage.h"
#include "config.h"
#include <FS.h>
#if USE_SD
#include <SD.h>
#endif
#include <LittleFS.h>
#include <ArduinoJson.h>
#include <esp_heap_caps.h>
namespace {
fs::FS* g_fs = nullptr;
store::Backend g_backend = store::BK_NONE;
const char* kDir = "/photos";
const char* kIndex = "/photos/index.json";
// PSRAM-backed upload buffer (raw JPEG), so no temp file is needed on flash.
uint8_t* g_up = nullptr;
size_t g_upLen = 0, g_upCap = 0;
void* psAlloc(size_t n) {
void* p = heap_caps_malloc(n, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
return p ? p : malloc(n);
}
String epdPath(const String& id) { return String(kDir) + "/" + id + ".epd"; }
void loadIndex(JsonDocument& doc) {
doc.clear();
if (g_fs) {
File f = g_fs->open(kIndex, FILE_READ);
if (f) { deserializeJson(doc, f); f.close(); }
}
if (!doc["items"].is<JsonArray>()) {
doc.clear();
doc["next"] = 1;
doc["current"] = "";
doc["items"].to<JsonArray>();
}
}
void saveIndex(JsonDocument& doc) {
if (!g_fs) return;
File f = g_fs->open(kIndex, FILE_WRITE);
if (!f) return;
serializeJson(doc, f);
f.close();
}
} // namespace
bool store::begin(SPIClass& spi) {
(void)spi;
#if USE_SD
pinMode(SD_EN, OUTPUT);
digitalWrite(SD_EN, HIGH); // power the SD slot
pinMode(SD_DET, INPUT_PULLUP);
delay(50);
if (SD.begin(SD_CS, spi, 20000000)) {
g_fs = &SD;
g_backend = BK_SD;
} else
#endif
if (LittleFS.begin(true, "/littlefs", 10, "littlefs")) { // internal flash; format on first use
g_fs = &LittleFS;
g_backend = BK_FLASH;
} else {
g_backend = BK_NONE;
return false;
}
if (!g_fs->exists(kDir)) g_fs->mkdir(kDir);
return true;
}
bool store::ready() { return g_backend != BK_NONE; }
store::Backend store::backend() { return g_backend; }
bool store::beginUpload() {
g_upLen = 0;
if (!g_up) { g_upCap = 512 * 1024; g_up = (uint8_t*)psAlloc(g_upCap); }
return g_up != nullptr;
}
bool store::appendUpload(const uint8_t* data, size_t len) {
if (!g_up) return false;
if (g_upLen + len > g_upCap) {
size_t cap = g_upCap ? g_upCap : 512 * 1024;
while (cap < g_upLen + len) cap *= 2;
uint8_t* n = (uint8_t*)heap_caps_realloc(g_up, cap, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!n) return false;
g_up = n;
g_upCap = cap;
}
memcpy(g_up + g_upLen, data, len);
g_upLen += len;
return true;
}
const uint8_t* store::uploadData() { return g_up; }
size_t store::uploadLen() { return g_upLen; }
void store::clearUpload() {
free(g_up);
g_up = nullptr;
g_upLen = g_upCap = 0;
}
String store::addPhoto(const String& name, const uint8_t* epd, size_t len) {
if (g_backend == BK_NONE) return "";
JsonDocument doc;
loadIndex(doc);
int n = doc["next"] | 1;
char id[8];
snprintf(id, sizeof(id), "%06d", n);
File f = g_fs->open(epdPath(id), FILE_WRITE);
if (!f) return "";
bool ok = f.write(epd, len) == len;
f.close();
if (!ok) { g_fs->remove(epdPath(id)); return ""; }
JsonObject it = doc["items"].as<JsonArray>().add<JsonObject>();
it["id"] = id;
it["name"] = name.length() ? name : String(id);
it["ts"] = (uint32_t)(millis() / 1000);
doc["next"] = n + 1;
saveIndex(doc);
return String(id);
}
uint8_t* store::readEpd(const String& id, size_t* outLen) {
*outLen = 0;
if (g_backend == BK_NONE) return nullptr;
File f = g_fs->open(epdPath(id), FILE_READ);
if (!f) return nullptr;
size_t n = f.size();
uint8_t* buf = (uint8_t*)psAlloc(n);
if (!buf) { f.close(); return nullptr; }
size_t got = f.read(buf, n);
f.close();
if (got != n) { free(buf); return nullptr; }
*outLen = n;
return buf;
}
int store::count() {
JsonDocument doc;
loadIndex(doc);
return doc["items"].as<JsonArray>().size();
}
String store::listJson() {
JsonDocument doc;
loadIndex(doc);
JsonDocument resp;
resp["current"] = doc["current"];
JsonArray items = resp["items"].to<JsonArray>();
for (JsonObject it : doc["items"].as<JsonArray>()) {
JsonObject o = items.add<JsonObject>();
o["id"] = it["id"];
o["name"] = it["name"];
o["ts"] = it["ts"];
}
String out;
serializeJson(resp, out);
return out;
}
bool store::exists(const String& id) { return g_fs && g_fs->exists(epdPath(id)); }
bool store::remove(const String& id) {
if (g_backend == BK_NONE) return false;
g_fs->remove(epdPath(id));
JsonDocument doc;
loadIndex(doc);
JsonArray items = doc["items"].as<JsonArray>();
for (size_t i = 0; i < items.size(); ++i) {
if (String((const char*)items[i]["id"]) == id) { items.remove(i); break; }
}
if (String((const char*)doc["current"]) == id) doc["current"] = "";
saveIndex(doc);
return true;
}
void store::clearAll() {
if (g_backend == BK_NONE) return;
JsonDocument doc;
loadIndex(doc);
for (JsonObject it : doc["items"].as<JsonArray>())
g_fs->remove(epdPath(String((const char*)it["id"])));
JsonDocument fresh;
fresh["next"] = 1;
fresh["current"] = "";
fresh["items"].to<JsonArray>();
saveIndex(fresh);
}
String store::currentId() {
JsonDocument doc;
loadIndex(doc);
return String((const char*)doc["current"]);
}
void store::setCurrent(const String& id) {
JsonDocument doc;
loadIndex(doc);
doc["current"] = id;
saveIndex(doc);
}
String store::firstId() {
JsonDocument doc;
loadIndex(doc);
JsonArray items = doc["items"].as<JsonArray>();
if (items.size() == 0) return "";
return String((const char*)items[0]["id"]);
}
String store::nextId(const String& id) {
JsonDocument doc;
loadIndex(doc);
JsonArray items = doc["items"].as<JsonArray>();
int n = items.size();
if (n == 0) return "";
for (int i = 0; i < n; ++i) {
if (String((const char*)items[i]["id"]) == id)
return String((const char*)items[(i + 1) % n]["id"]);
}
return String((const char*)items[0]["id"]);
}