-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueuefs.h
159 lines (132 loc) · 5.68 KB
/
queuefs.h
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
/*
* QUEUE_FS
* Manages the queue on the filesystem
*
*/
int transmitQueue = 0;
void listFirstFileWithExtension(char const * extension, int * FileCount, char * FirstFile) { // Defined incoming string array, defined incoming integer pointers
//SerialMon.println("Starting list files");
//if ( ! heap_caps_check_integrity_all(true) ) SerialMon.println(F("Heap corrupt! @ listFirstFileWithExtension"));
//_delay(100);
File root = LITTLEFS.open("/", FILE_READ);
//SerialMon.println("Opened root");
if (!root) {
SerialMon.println(F("- failed to open directory"));
return;
}
if ( !root.isDirectory() ) {
SerialMon.println(F(" - not a directory"));
return;
}
*FileCount = 0; // Changed to *FileCount ; this is a pointer!
bool fileFound = false;
File file = root.openNextFile();
//SerialMon.println("Opened next file");
while ( file ) {
if (file.isDirectory()) {
SerialMon.print(F(" DIR : "));
SerialMon.println(file.name());
} else {
if ( ( HIDE_CERTIFICATES && is_hidden(file.name())) ) continue;
if ( String(file.name()).endsWith(extension) ) {
if ( ! fileFound ) {
#ifdef VERBOSE_QUEUE
SerialMon.print(*FileCount);
SerialMon.print(F(" FILE: ["));
SerialMon.print(file.name());
SerialMon.print(F("]\tSIZE: "));
SerialMon.println(file.size());
#endif
//strlcpy(FirstFile,file.name(), strnlen(file.name()), MAX_FILENAME_SIZE); // We should close the array here!!!
if ( strnlen(file.name(), MAX_FILENAME_SIZE) >= MAX_FILENAME_SIZE ) {
SerialMon.println(F("Filename size exceeds maximum!"));
} else {
snprintf(FirstFile, MAX_FILENAME_SIZE, "%s", file.name());
// strcpy(FirstFile,file.name()); // strlcpy IS null terminated but NOT advised; strcpy and strncpy ARE NOT
// Also c_str() is null terminated! also snprintf is NULL terminated
// strcpy can flow over the target size though (buffer overflow)!
fileFound = true; // We have got the first file...
}
}
*FileCount = *FileCount + 1;
}
}
file.close(); // Might not be needed
/*
if ( fileFound ) {
while ( root.openNextFile() ) { // We have got the file; just count the remaining number of files for now...
*FileCount = *FileCount + 1;
SerialMon.println("Opened next file");
yield();
}
} else {
file = root.openNextFile();
SerialMon.println("Opened next file");
}
*/
if ( fileFound ) break; // Speed up the queue handling; having an idea of the files with .log extension would be nice though!
file = root.openNextFile();
yield(); // Disabled in attempt to speed things up
}
root.close(); // Might not be needed
//SerialMon.println("Done");
}
void showSentMessages() {
SerialMon.printf("Sent %lu messages\n", messagesTransmitted);
}
void updateTransmitQueue(bool verboseOutput=false) { // Not using the file content here!
int returnedCount = 0; // Needs to obey the MAX_DIRLIST_COUNT
char FileToBetransMitted[MAX_FILENAME_SIZE];
listFirstFileWithExtension(SERIAL_LOG_EXT, &returnedCount, FileToBetransMitted);
transmitQueue = returnedCount;
if (verboseOutput) SerialMon.printf("Queue holds %i items\n", returnedCount);
}
void updateSentMessages() {
int returnedCount = 0;
char FileToBetransMitted[MAX_FILENAME_SIZE];
listFirstFileWithExtension(FILE_SENT_EXT, &returnedCount, FileToBetransMitted);
SerialMon.printf("Sent history holds %i items\n ", returnedCount);
}
bool reQueue(char * FileName) {
char ToName[MAX_FILENAME_SIZE];
// SerialMon.printf("From %s\n", FileName);
if ( String(FileName).indexOf(FILE_SENT_EXT,0) > 0 ) {
snprintf(ToName, MAX_FILENAME_SIZE, "%s%s", (String(FileName).substring(0, String(FileName).indexOf(FILE_SENT_EXT, 0))).c_str(), SERIAL_LOG_EXT);
} else {
snprintf(ToName, MAX_FILENAME_SIZE, "%s%s", (String(FileName).substring(0, String(FileName).indexOf(SEND_FAIL_EXT, 0))).c_str(), SERIAL_LOG_EXT);
}
// SerialMon.printf("To %s'n", ToName);
return rename_file(FileName, ToName);
}
bool Queue(char * FileName) {
char ToName[MAX_FILENAME_SIZE];
//SerialMon.printf("From %s\n", FileName);
snprintf(ToName, MAX_FILENAME_SIZE, "%s%s", (String(FileName).substring(0, String(FileName).indexOf(TEMP_EXT, 0))).c_str(), SERIAL_LOG_EXT);
//SerialMon.printf("To %s\n", ToName);
return rename_file(FileName, ToName);
}
bool markFailed(char * FileName) {
char ToName[MAX_FILENAME_SIZE];
snprintf(ToName, MAX_FILENAME_SIZE, "%s%s", (String(FileName).substring(0, String(FileName).indexOf(SERIAL_LOG_EXT, 0))).c_str(), SEND_FAIL_EXT);
return rename_file(FileName, ToName);
}
bool markTransmitted(char * FileName) {
char ToName[MAX_FILENAME_SIZE];
//if ( ! heap_caps_check_integrity_all(true) ) SerialMon.println(F("Heap corrupt! @ markTransmitted"));
snprintf(ToName, MAX_FILENAME_SIZE, "%s%s", (String(FileName).substring(0, String(FileName).indexOf(SERIAL_LOG_EXT, 0))).c_str(), FILE_SENT_EXT);
switch ( ON_TRANSMIT_SUCCESS ) {
case RENAME:
return rename_file(FileName, ToName);
break;
case DELETE:
return remove_file(FileName);
break;
case NOACTION:
return true;
break;
default:
SerialMon.println(F("No action after transmitting"));
return true;
break;
}
}