2018-11-10 09:32:33 +00:00
|
|
|
#include <stdint.h>
|
2018-08-14 05:28:03 +00:00
|
|
|
#include "hardware.h"
|
|
|
|
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS)
|
2018-08-14 05:28:03 +00:00
|
|
|
#include <SPIFFS.h>
|
2023-10-12 17:27:56 +00:00
|
|
|
#elif defined(USE_LITTLEFS)
|
2018-11-10 13:35:41 +00:00
|
|
|
#include <FS.h>
|
2023-10-12 17:27:56 +00:00
|
|
|
#include <LittleFS.h>
|
2018-08-14 05:28:03 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-11 18:28:38 +00:00
|
|
|
#include "serialio.h"
|
2019-02-10 13:58:52 +00:00
|
|
|
#include "filer.h"
|
2019-03-30 11:55:34 +00:00
|
|
|
#include "flash_filer.h"
|
2014-10-26 17:54:01 +00:00
|
|
|
|
2023-11-18 13:23:52 +00:00
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
2023-10-19 14:04:28 +00:00
|
|
|
static File files[MAX_FILES];
|
2023-11-18 13:23:52 +00:00
|
|
|
#endif
|
2023-10-19 14:04:28 +00:00
|
|
|
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS)
|
2023-10-19 14:04:28 +00:00
|
|
|
static File dir;
|
2023-10-12 17:27:56 +00:00
|
|
|
#elif defined(USE_LITTLEFS)
|
2018-11-10 13:35:41 +00:00
|
|
|
static Dir dir;
|
2018-11-10 09:32:33 +00:00
|
|
|
#endif
|
2014-10-26 17:54:01 +00:00
|
|
|
|
2023-10-19 14:04:28 +00:00
|
|
|
bool flash_file::seek(uint32_t pos)
|
2023-05-25 14:05:04 +00:00
|
|
|
{
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
2023-10-19 14:04:28 +00:00
|
|
|
return files[_fd].seek(pos);
|
2023-09-30 11:51:19 +00:00
|
|
|
#else
|
2023-05-25 14:05:04 +00:00
|
|
|
return false;
|
2023-09-30 11:51:19 +00:00
|
|
|
#endif
|
2023-05-25 14:05:04 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 14:04:28 +00:00
|
|
|
bool flash_file::more()
|
2014-10-26 17:54:01 +00:00
|
|
|
{
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
2023-10-19 14:04:28 +00:00
|
|
|
return files[_fd].available() > 0;
|
2023-09-30 11:51:19 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:04:28 +00:00
|
|
|
uint8_t flash_file::read() {
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
2023-10-19 14:04:28 +00:00
|
|
|
return files[_fd].read();
|
2023-09-30 11:51:19 +00:00
|
|
|
#else
|
|
|
|
return 0xff;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-10-20 12:01:15 +00:00
|
|
|
flash_file::operator bool() const {
|
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
|
|
|
return files[_fd];
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-10-19 14:04:28 +00:00
|
|
|
void flash_file::write(uint8_t b) {
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
2023-10-19 14:04:28 +00:00
|
|
|
files[_fd].write(b);
|
|
|
|
files[_fd].flush();
|
2018-11-16 12:35:07 +00:00
|
|
|
#endif
|
2014-10-26 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 12:01:15 +00:00
|
|
|
bool flash_filer::start()
|
|
|
|
{
|
|
|
|
#if defined(USE_LITTLEFS)
|
|
|
|
dir = LittleFS.openDir(_programs);
|
|
|
|
#elif defined(USE_SPIFFS)
|
|
|
|
dir = SPIFFS.open(_programs);
|
|
|
|
if (!dir)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void flash_filer::stop()
|
|
|
|
{
|
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
|
|
|
for (int i = 0; i < MAX_FILES; i++)
|
|
|
|
files[i].close();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:58:52 +00:00
|
|
|
const char *flash_filer::advance() {
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS) || defined(USE_LITTLEFS)
|
2023-10-19 14:04:28 +00:00
|
|
|
files[_current].close();
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_LITTLEFS)
|
2018-11-10 13:35:41 +00:00
|
|
|
static char buf[32];
|
|
|
|
while (true) {
|
|
|
|
if (dir.next()) {
|
2023-10-19 14:04:28 +00:00
|
|
|
files[_current] = dir.openFile("r+");
|
2018-11-10 13:35:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-10-12 17:27:56 +00:00
|
|
|
dir = LittleFS.openDir(_programs);
|
2018-11-10 13:35:41 +00:00
|
|
|
}
|
|
|
|
strncpy(buf, dir.fileName().c_str(), sizeof(buf));
|
|
|
|
return buf;
|
|
|
|
#else
|
2023-11-15 18:22:28 +00:00
|
|
|
bool rewound = false;
|
2014-10-26 17:54:01 +00:00
|
|
|
while (true) {
|
2023-10-19 14:04:28 +00:00
|
|
|
files[_current] = dir.openNextFile();
|
|
|
|
if (files[_current]) {
|
|
|
|
if (files[_current].isDirectory())
|
|
|
|
files[_current].close();
|
2014-10-26 17:54:01 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else if (!rewound) {
|
|
|
|
dir.rewindDirectory();
|
|
|
|
rewound = true;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
2023-10-19 14:04:28 +00:00
|
|
|
return files[_current].name();
|
2018-11-10 13:35:41 +00:00
|
|
|
#endif
|
2018-11-10 09:32:33 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2014-10-26 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 13:58:52 +00:00
|
|
|
const char *flash_filer::rewind() {
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS)
|
2014-10-26 17:54:01 +00:00
|
|
|
dir.rewindDirectory();
|
2018-11-10 09:32:33 +00:00
|
|
|
#endif
|
2014-10-26 17:54:01 +00:00
|
|
|
return advance();
|
|
|
|
}
|
2019-02-12 18:17:46 +00:00
|
|
|
|
2019-02-24 11:50:10 +00:00
|
|
|
#if !defined(NO_CHECKPOINT)
|
2023-11-15 18:22:28 +00:00
|
|
|
#if defined(USE_SPIFFS)
|
2019-02-12 18:17:46 +00:00
|
|
|
static char buf[32];
|
|
|
|
static char chkpt[] = { "CHKPOINT" };
|
|
|
|
static int cpid = 0;
|
2023-11-15 18:22:28 +00:00
|
|
|
#endif
|
2019-02-12 18:17:46 +00:00
|
|
|
|
|
|
|
const char *flash_filer::checkpoint() {
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS)
|
2019-02-12 18:17:46 +00:00
|
|
|
stop();
|
2019-03-26 19:27:25 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s%s.%03d", _programs, chkpt, cpid++);
|
2019-02-12 18:17:46 +00:00
|
|
|
|
2023-10-12 17:27:56 +00:00
|
|
|
File file = SPIFFS.open(buf, FILE_WRITE);
|
2019-02-12 18:17:46 +00:00
|
|
|
hardware_checkpoint(file);
|
|
|
|
file.close();
|
2019-03-26 19:27:25 +00:00
|
|
|
start();
|
2019-02-12 18:17:46 +00:00
|
|
|
return buf;
|
2023-11-18 10:58:15 +00:00
|
|
|
#else
|
|
|
|
return "";
|
|
|
|
#endif
|
2019-02-12 18:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void flash_filer::restore(const char *filename) {
|
2023-10-12 17:27:56 +00:00
|
|
|
#if defined(USE_SPIFFS)
|
2019-02-12 18:17:46 +00:00
|
|
|
stop();
|
2019-03-26 19:27:25 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s%s", _programs, filename);
|
2019-02-12 18:17:46 +00:00
|
|
|
|
2023-10-12 17:27:56 +00:00
|
|
|
File file = SPIFFS.open(buf, FILE_READ);
|
2019-02-12 18:17:46 +00:00
|
|
|
hardware_restore(file);
|
|
|
|
file.close();
|
2019-03-26 19:27:25 +00:00
|
|
|
int n = sscanf(buf + strlen(_programs), "%[A-Z0-9].%d", chkpt, &cpid);
|
2019-02-12 18:17:46 +00:00
|
|
|
cpid = (n == 1)? 0: cpid+1;
|
|
|
|
#endif
|
2019-03-26 19:27:25 +00:00
|
|
|
start();
|
2019-02-12 18:17:46 +00:00
|
|
|
}
|
2019-02-24 11:50:10 +00:00
|
|
|
#endif
|