-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsdcard.cpp
More file actions
170 lines (145 loc) · 3.74 KB
/
sdcard.cpp
File metadata and controls
170 lines (145 loc) · 3.74 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
#include "GrowduinoFirmware.h"
#include <SD.h>
#include <aJSON.h>
const int chipSelect = 4;
bool sdcard_init() {
bool ret;
pinMode(53, OUTPUT);
ret = SD.begin(chipSelect);
return ret;
}
bool file_exists(const char * dirname) {
// check if directory (or full filename) exists
return file_exists(dirname, NULL);
}
bool file_exists(const char * dirname, const char * filename) {
char full_file_name[70];
strlcpy(full_file_name, dirname, 55);
if (filename == NULL) {
return SD.exists(full_file_name);
} else {
if (strlen(dirname) > 50) {
//we dont do that deep dirs
return false;
}
strlcpy(full_file_name, dirname, 55);
strlcat(full_file_name, "/", 70);
strlcat(full_file_name, filename, 70);
return SD.exists(full_file_name);
}
}
int file_for_write(const char * dirname, const char * filename, File * dataFile) {
char filepath[128];
if (strlen(dirname) > 50) {
//we dont do that deep dirs
return -1;
}
strlcpy(filepath, dirname, 55);
if (!file_exists(filepath) && dirname[0] == '/') {
#ifdef DEBUG_SDCARD
SERIAL.print(F("Creating directory: "));
#endif
SD.mkdir(filepath);
}
strlcat(filepath, "/", 60);
strlcat(filepath, filename, 60);
#ifdef DEBUG_SDCARD
SERIAL.print(F("Writing file "));
SERIAL.println(filepath);
#endif
if (file_exists(filepath)) {
SD.remove(filepath);
}
digitalWrite(13, 1);
#ifdef DEBUG_SDCARD
SERIAL.print(F("."));
#endif
*dataFile = SD.open(filepath, FILE_WRITE);
return 1;
}
void file_passthru(const char * dirname, const char * filename, Stream * input) {
File dataFile;
char buf[128];
int res = file_for_write(dirname, filename, &dataFile);
#ifdef WATCHDOG
wdt_reset();
#endif
#ifdef DEBUG_SDCARD
SERIAL.println(F("file_passthru"));
#endif
digitalWrite(13, 1);
if (res != -1) {
SERIAL.print(F("saving data to disk"));
int remain = 0;
while ((remain = input->available())) {
remain = min(remain, 127);
for (int i = 0; i < remain; i++) {
buf[i] = (char) input->read();
}
buf[remain] = 0;
dataFile.write(buf);
SERIAL.print(F("."));
};
dataFile.close();
} else {
SERIAL.print(F("Failed to open "));
SERIAL.println(filename);
}
#ifdef DEBUG_SDCARD
SERIAL.println(F(";"));
#endif
digitalWrite(13, 0);
}
void file_write(const char * dirname, const char * filename, aJsonObject * data) {
File dataFile;
int res = file_for_write(dirname, filename, &dataFile);
#ifdef DEBUG_SDCARD
SERIAL.println(F("file_write"));
#endif
if (res != -1) {
aJsonStream sd_stream(&dataFile);
aJson.print(data, &sd_stream);
#ifdef DEBUG_SDCARD
SERIAL.print(F("."));
#endif
dataFile.close();
#ifdef DEBUG_SDCARD
SERIAL.print(F("."));
#endif
} else {
SERIAL.print(F("Failed to open "));
SERIAL.println(filename);
}
#ifdef DEBUG_SDCARD
SERIAL.println(F(";"));
#endif
digitalWrite(13, 0);
}
aJsonObject * file_read(const char * dirname, const char * filename) {
char filepath[60];
if (strlen(dirname) > 50 || strstr(dirname, "..") != NULL) {
//we dont do that deep dirs, we dont support taversal up
return NULL;
}
strcpy(filepath, dirname);
strncat(filepath, "/", sizeof(filepath) - 2);
strncat(filepath, filename, sizeof(filepath) - strlen(filename) - 1);
//strcat(filepath, "/");
//strcat(filepath, filename);
SERIAL.print(F("opening file "));
SERIAL.println(filepath);
if (!SD.exists(filepath)) {
SERIAL.println(F("File does not exist"));
return NULL;
}
File dataFile = SD.open(filepath, FILE_READ);
if (dataFile) {
aJsonStream sd_stream(&dataFile);
aJsonObject * data = aJson.parse(&sd_stream);
dataFile.close();
return data;
} else {
SERIAL.println(F("File read failure"));
return NULL;
}
}