-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialSDManager.cpp
More file actions
312 lines (273 loc) · 8.06 KB
/
SerialSDManager.cpp
File metadata and controls
312 lines (273 loc) · 8.06 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
#include "SerialSDManager.h"
#include "SerialSDManager.h"
SerialSDManager::SerialSDManager(int chipSelectPin) : chipSelect(chipSelectPin) {}
void SerialSDManager::begin() {
Serial.begin(115200);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed. Card inserted?");
return;
}
Serial.println("SD ok");
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("Initialization failed. Check your wiring.");
while (1);
} else {
Serial.println("Card is present.");
}
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition. Format the card.");
while (1);
}
if (!root.openRoot(volume)) {
Serial.println("Could not open root of volume.");
while (1);
}
}
void SerialSDManager::checkForSerialInput() {
recvWithStartEndMarkers();
if (newData) {
strcpy(tempChars, receivedChars);
parseData();
executeCommand();
newData = false;
}
}
void SerialSDManager::recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && !newData) {
rc = Serial.read();
if (recvInProgress) {
if (rc != endMarker) {
receivedChars[ndx++] = rc;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
} else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void SerialSDManager::parseData() {
char *strtokIndx = strtok(tempChars, " ");
cmdId = atoi(strtokIndx);
// For command 5: first token is filename, rest is content
if (cmdId == 5) {
strtokIndx = strtok(NULL, " ");
if (strtokIndx != nullptr) {
strcpy(cmdFileName, strtokIndx);
}
// Get the rest as content (everything after filename)
strtokIndx = strtok(NULL, ""); // Empty delimiter = rest of string
if (strtokIndx != nullptr) {
strcpy(cmdMsg, strtokIndx);
}
} else {
// For other commands: everything after cmdId is cmdMsg
strtokIndx = strtok(NULL, "");
if (strtokIndx != nullptr) {
strcpy(cmdMsg, strtokIndx);
}
}
}
void SerialSDManager::executeCommand() {
char *configFilename = findConfigFile();
switch(cmdId) {
case 1:
printRoot();
break;
case 2:
printFileContent(cmdMsg);
break;
case 3: {
printFileContent(configFilename);
break;
}
case 4:
deleteFile(cmdMsg);
break;
case 5:
// Generic file write: <5 filename content>
writeFile(cmdFileName, cmdMsg);
break;
case 7:
// Write config.cfg and delete all other .cfg files
writeFile("config.cfg", cmdMsg);
deleteAllCfgExcept("config.cfg");
break;
default:
Serial.println("Unknown command.");
break;
}
}
void SerialSDManager::printRoot() {
int pathidx = 0;
SdFile parentdir = getParentDir("/", &pathidx);
parentdir.ls(LS_SIZE);
Serial.write("|");
Serial.write("end");
}
void SerialSDManager::printFileContent(const char* filename) {
File file = SD.open(filename);
Serial.write(filename);
Serial.write("|");
if (file) {
// CRC32 calculation (if needed)
CRC32 crc;
for (int i = 0; filename[i] != '\0'; i++) {
crc.update(filename[i]);
}
String firstLine = "";
bool isFirstLine = true;
while (file.available()) {
char c = file.read();
Serial.write(c);
if (isFirstLine && c != '\n') {
firstLine += c;
crc.update(c);
} else if (c == '\n') {
isFirstLine = false;
}
}
file.close();
uint32_t checksum = crc.finalize();
Serial.write("|");
Serial.print(checksum, HEX);
Serial.write("|");
Serial.write("end");
} else {
Serial.println("Error opening the file.");
}
}
void SerialSDManager::deleteFile(const char* fileName) {
if (SD.exists(fileName)) {
if (SD.remove(fileName)) {
Serial.println("File successfully deleted.");
} else {
Serial.println("Error deleting the file.");
}
} else {
Serial.println("The file does not exist.");
}
}
void SerialSDManager::deleteAllCfgExcept(const char* keepFile) {
File root = SD.open("/");
if (!root) {
Serial.println("Error opening root directory.");
return;
}
// Collect filenames first (can't delete while iterating)
char filesToDelete[10][50];
int count = 0;
while (count < 10) {
File file = root.openNextFile();
if (!file) break;
if (!file.isDirectory()) {
String name = file.name();
String lowerName = name;
lowerName.toLowerCase();
if (lowerName.indexOf(".cfg") != -1) {
// Check if this is the file to keep (case-insensitive)
String keepLower = keepFile;
keepLower.toLowerCase();
if (lowerName != keepLower) {
name.toCharArray(filesToDelete[count], 50);
count++;
}
}
}
file.close();
}
root.close();
// Now delete collected files
for (int i = 0; i < count; i++) {
if (SD.remove(filesToDelete[i])) {
Serial.print("Deleted old config: ");
Serial.println(filesToDelete[i]);
}
}
}
void SerialSDManager::writeFile(const char* fileName, const char* fileContent) {
File file = SD.open(fileName, FILE_WRITE);
if (file) {
Serial.print("Writing to ");
Serial.println(fileName);
file.print(fileContent);
file.close();
Serial.println("File written successfully.");
} else {
Serial.println("Error opening the file for writing.");
}
}
char* SerialSDManager::findConfigFile() {
const char* confFileName = nullptr;
static char buffer[50];
File root = SD.open("/");
if (!root) {
Serial.println("Error opening root directory.");
return "Error opening root folder";
}
while (true) {
File file = root.openNextFile();
if (!file) {
break;
}
if (!file.isDirectory()) {
String name = file.name();
name.toLowerCase();
if (name.indexOf(".cfg") != -1) {
name.toCharArray(buffer, sizeof(buffer));
file.close();
root.close();
return buffer;
}
}
file.close();
}
root.close();
return "No config file found";
}
SdFile SerialSDManager::getParentDir(const char* filepath, int* index) {
SdFile d1, d2;
d1.openRoot(volume);
SdFile* parent = &d1;
SdFile* subdir = &d2;
const char* origpath = filepath;
while (strchr(filepath, '/')) {
if (filepath[0] == '/') {
filepath++;
continue;
}
if (!strchr(filepath, '/')) {
break;
}
uint8_t idx = strchr(filepath, '/') - filepath;
if (idx > 12) {
idx = 12;
}
char subdirname[13];
strncpy(subdirname, filepath, idx);
subdirname[idx] = 0;
subdir->close();
if (!subdir->open(parent, subdirname, O_READ)) {
return SdFile();
}
filepath += idx;
parent->close();
SdFile* t = parent;
parent = subdir;
subdir = t;
}
*index = (int)(filepath - origpath);
return *parent;
}