Skip to content

Commit 6e36119

Browse files
authored
Remote log name (#496)
* Use 'remote.log' for file name on device since name length is limited. * Put filename in instance variable. * Use shared remote log file name.
1 parent 91e0eb3 commit 6e36119

12 files changed

Lines changed: 73 additions & 81 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const String gitVersion = 'v24.10.2-15-gbc+';
1+
const String gitVersion = 'v24.10.2-19-g1d+';

extras/scripts/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /bin/bash
22
bundle config set --local path 'vendor/bundle'
33
bundle install
4-
bundle exec arduino_ci.rb --min-free-space=5654 --skip-unittests 1> >(tee stdout.txt ) 2> >(tee stderr.txt >&2 ); result=$?
4+
bundle exec arduino_ci.rb --min-free-space=5640 --skip-unittests 1> >(tee stdout.txt ) 2> >(tee stderr.txt >&2 ); result=$?
55
exit "$result"

extras/scripts/testAndBuild.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
cp extras/scripts/p*-commit .git/hooks/
44
bundle config set --local path 'vendor/bundle'
55
bundle install
6-
bundle exec arduino_ci.rb --min-free-space=5654 1> >(tee stdout.txt ) 2> >(tee stderr.txt >&2 )
6+
bundle exec arduino_ci.rb --min-free-space=5640 1> >(tee stdout.txt ) 2> >(tee stderr.txt >&2 )
77
result="${PIPESTATUS[0]}"
88
exit "$result"

src/TankController.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ TankController *TankController::instance(const char *remoteLogName, const char *
4040
if (!_instance) {
4141
serial(F("\r\n##############\r\nTankController %s"), TANK_CONTROLLER_VERSION);
4242
_instance = new TankController();
43-
SD_TC::instance()->setRemoteLogName(remoteLogName);
43+
SD_TC::instance();
44+
RemoteLogPusher::instance()->setRemoteLogName(remoteLogName);
4445
EEPROM_TC::instance();
4546
Keypad_TC::instance();
4647
LiquidCrystal_TC::instance(TANK_CONTROLLER_VERSION);

src/Version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define VERSION "v24.10.2-15-gbc+"
1+
#define VERSION "v24.10.2-19-g1d+"

src/model/RemoteLogPusher.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ RemoteLogPusher* RemoteLogPusher::instance() {
2525
return _instance;
2626
}
2727

28+
void RemoteLogPusher::deleteInstance() {
29+
if (_instance) {
30+
delete _instance;
31+
_instance = nullptr;
32+
}
33+
}
34+
2835
// instance methods
2936
RemoteLogPusher::RemoteLogPusher() {
3037
this->buffer[0] = '\0';
@@ -154,18 +161,14 @@ void RemoteLogPusher::pushSoon() {
154161
*
155162
*/
156163
void RemoteLogPusher::sendHeadRequest() {
157-
const char* logName = SD_TC::instance()->getRemoteLogName();
158-
if (logName[0] == '\0') {
159-
return;
160-
}
161164
static const char format[] PROGMEM =
162165
"HEAD /logs/%s HTTP/1.1\r\n"
163166
"Host: %s\r\n"
164167
"User-Agent: TankController/%s\r\n"
165168
"Accept: text/plain\r\n"
166169
"Connection: Close\r\n"
167170
"\r\n";
168-
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, logName, serverDomain, VERSION);
171+
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, remoteLogName, serverDomain, VERSION);
169172
if (client.connected() || client.connect(serverDomain, PORT) == 1) { // this is a blocking step
170173
client.write(buffer, strnlen(buffer, sizeof(buffer)));
171174
} else {
@@ -177,10 +180,6 @@ void RemoteLogPusher::sendHeadRequest() {
177180
}
178181

179182
void RemoteLogPusher::sendPostRequest() {
180-
const char* logName = SD_TC::instance()->getRemoteLogName();
181-
if (logName[0] == '\0') {
182-
return;
183-
}
184183
char data[300];
185184
SD_TC::instance()->getRemoteLogContents(data, sizeof(data), serverFileSize);
186185
static const char format[] PROGMEM =
@@ -191,7 +190,7 @@ void RemoteLogPusher::sendPostRequest() {
191190
"Content-Length: %i\r\n"
192191
"Connection: Close\r\n"
193192
"\r\n";
194-
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, logName, serverDomain, VERSION, strnlen(data, sizeof(data)));
193+
snprintf_P(buffer, sizeof(buffer), (PGM_P)format, remoteLogName, serverDomain, VERSION, strnlen(data, sizeof(data)));
195194
if (client.connected() || client.connect(serverDomain, PORT) == 1) { // this is a blocking step
196195
client.write(buffer, strnlen(buffer, sizeof(buffer)));
197196
client.write(data, strnlen(data, sizeof(data)));
@@ -202,6 +201,18 @@ void RemoteLogPusher::sendPostRequest() {
202201
state = PROCESS_POST_RESPONSE;
203202
}
204203

204+
void RemoteLogPusher::setRemoteLogName(const char* newFileName) {
205+
if (newFileName == nullptr || newFileName[0] == '\0') {
206+
remoteLogName[0] = '\0';
207+
return;
208+
}
209+
// See TankController.ino for the definition of remoteLogName
210+
if (strnlen(newFileName, MAX_FILE_NAME_LENGTH + 1) <= MAX_FILE_NAME_LENGTH) {
211+
// valid file name has been provided (See TankController.ino)
212+
snprintf_P(remoteLogName, MAX_FILE_NAME_LENGTH + 5, PSTR("%s.log"), newFileName);
213+
}
214+
}
215+
205216
bool RemoteLogPusher::shouldSendHeadRequest() {
206217
return _shouldSendHeadRequest && millis() > delayRequestsUntilTime && !(PHControl::instance()->isOn());
207218
}

src/model/RemoteLogPusher.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "wrappers/Ethernet_TC.h"
55

6+
#define MAX_FILE_NAME_LENGTH 28
7+
68
/*
79
* @brief RemoteLogPusher is a singleton that sends data records and remote logs to the server.
810
*
@@ -45,12 +47,17 @@ class RemoteLogPusher {
4547
public:
4648
// class methods
4749
static RemoteLogPusher *instance();
50+
static void deleteInstance();
4851

4952
// instance methods
5053
RemoteLogPusher();
54+
const char *getRemoteLogName() {
55+
return remoteLogName;
56+
}
5157
bool isReadyToPost();
5258
void loop();
5359
void pushSoon();
60+
void setRemoteLogName(const char *newFileName = nullptr);
5461
bool shouldSendHeadRequest();
5562

5663
#if defined(ARDUINO_CI_COMPILATION_MOCKS)
@@ -84,6 +91,7 @@ class RemoteLogPusher {
8491
clientState_t state = CLIENT_NOT_CONNECTED;
8592
uint32_t delayRequestsUntilTime = 40000; // wait a bit before first request
8693
const char *serverDomain = "oap.cs.wallawalla.edu";
94+
char remoteLogName[MAX_FILE_NAME_LENGTH + 5] = ""; // add ".log" with null-terminator
8795
char buffer[300];
8896
unsigned int index = 0;
8997
bool _isReadyToPost = false;

src/wrappers/SD_TC.cpp

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ SD_TC::SD_TC() {
4242
if (!sd.begin(SD_SELECT_PIN)) {
4343
Serial.println(F("SD_TC failed to initialize!"));
4444
}
45-
remoteLogName[0] = '\0';
4645
}
4746

4847
/**
@@ -123,11 +122,7 @@ bool SD_TC::format() {
123122

124123
void SD_TC::getRemoteLogContents(char* buffer, int size, uint32_t index) {
125124
buffer[0] = '\0';
126-
const char* logName = getRemoteLogName();
127-
if (logName[0] == '\0') {
128-
return;
129-
}
130-
File file = open(logName, O_RDONLY);
125+
File file = open(remoteFileName, O_RDONLY);
131126
if (file) {
132127
file.seek(index);
133128
int remaining = file.available();
@@ -254,29 +249,13 @@ bool SD_TC::remove(const char* path) {
254249
return sd.remove(path);
255250
}
256251

257-
void SD_TC::setRemoteLogName(const char* newFileName) {
258-
if (newFileName == nullptr || newFileName[0] == '\0') {
259-
remoteLogName[0] = '\0';
260-
return;
261-
}
262-
// See TankController.ino for the definition of remoteLogName
263-
if (strnlen(newFileName, MAX_FILE_NAME_LENGTH + 1) <= MAX_FILE_NAME_LENGTH) {
264-
// valid file name has been provided (See TankController.ino)
265-
snprintf_P(remoteLogName, MAX_FILE_NAME_LENGTH + 5, PSTR("%s.log"), newFileName);
266-
}
267-
}
268-
269252
void SD_TC::todaysDataFileName(char* path, int size) {
270253
DateTime_TC now = DateTime_TC::now();
271254
snprintf_P(path, size, (PGM_P)F("%4i%02i%02i.csv"), now.year(), now.month(), now.day());
272255
}
273256

274257
void SD_TC::updateRemoteFileSize() {
275-
if (remoteLogName[0] == '\0') {
276-
remoteFileSize = 0;
277-
return;
278-
}
279-
File file = open(remoteLogName, O_RDONLY);
258+
File file = open(remoteFileName, O_RDONLY);
280259
if (file) {
281260
remoteFileSize = file.size();
282261
file.close();
@@ -295,23 +274,19 @@ void SD_TC::writeToRemoteLog(const char* line) {
295274
strncpy(mostRecentRemoteLogEntry, line, sizeof(mostRecentRemoteLogEntry)); // Flawfinder: ignore
296275
mostRecentRemoteLogEntry[sizeof(mostRecentRemoteLogEntry) - 1] = '\0'; // Ensure null-terminated string
297276
#endif
298-
const char* logName = getRemoteLogName();
299-
if (logName[0] == '\0') {
300-
return;
301-
}
302-
if (!sd.exists(logName)) {
277+
if (!sd.exists(remoteFileName)) {
303278
// rather than write an entire header line in one buffer, we break it into chunks to save memory
304279
char buffer[200];
305280
DataLogger::instance()->writeRemoteFileHeader(buffer, sizeof(buffer), 0);
306-
appendStringToPath(buffer, remoteLogName, false);
281+
appendStringToPath(buffer, remoteFileName, false);
307282
DataLogger::instance()->writeRemoteFileHeader(buffer, sizeof(buffer), 1);
308-
appendStringToPath(buffer, remoteLogName, false);
283+
appendStringToPath(buffer, remoteFileName, false);
309284
DataLogger::instance()->writeRemoteFileHeader(buffer, sizeof(buffer), 2);
310-
appendStringToPath(buffer, remoteLogName, false);
285+
appendStringToPath(buffer, remoteFileName, false);
311286
DataLogger::instance()->writeRemoteFileHeader(buffer, sizeof(buffer), 3);
312-
appendStringToPath(buffer, remoteLogName);
287+
appendStringToPath(buffer, remoteFileName);
313288
}
314-
appendStringToPath(line, remoteLogName);
289+
appendStringToPath(line, remoteFileName);
315290
updateRemoteFileSize();
316291
RemoteLogPusher::instance()->pushSoon();
317292
}

src/wrappers/SD_TC.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,11 @@ class SD_TC {
3131
uint32_t getRemoteFileSize() {
3232
return remoteFileSize;
3333
}
34-
const char* getRemoteLogName() {
35-
return remoteLogName;
36-
}
3734
bool listRootToBuffer(void (*callWhenFull)(const char*, bool));
3835
bool mkdir(const char* path);
3936
File open(const char* path, oflag_t oflag = 0x00);
4037
void printRootDirectory();
4138
bool remove(const char* path);
42-
void setRemoteLogName(const char* newFileName = nullptr);
4339
void todaysDataFileName(char* path, int size);
4440
void writeToRemoteLog(const char* line);
4541

@@ -60,8 +56,8 @@ class SD_TC {
6056
const uint8_t SD_SELECT_PIN = SS;
6157
bool hasHadError = false;
6258
SdFat sd;
63-
char remoteLogName[MAX_FILE_NAME_LENGTH + 5] = ""; // add ".log" with null-terminator
6459
uint32_t remoteFileSize = 0;
60+
const char remoteFileName[12] PROGMEM = "remote.log";
6561

6662
// Max depth of file system search for rootdir()
6763
// Two is minimum: First for root, second for files

test/PushingBoxTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "EthernetServer_TC.h"
77
#include "PHControl.h"
88
#include "PushingBox.h"
9+
#include "RemoteLogPusher.h"
910
#include "Serial_TC.h"
1011
#include "TankController.h"
1112
#include "ThermalControl.h"
@@ -46,7 +47,7 @@ unittest_teardown() {
4647
}
4748

4849
unittest(NoTankID) {
49-
SD_TC::instance()->setRemoteLogName("90A2DA807B76");
50+
RemoteLogPusher::instance()->setRemoteLogName("90A2DA807B76");
5051
// set tank id to 0, set time interval to 1 minute
5152
EEPROM_TC::instance()->setTankID(0);
5253

0 commit comments

Comments
 (0)