Made native tests compile again

This commit is contained in:
Martin Bauer 2023-08-28 15:06:29 +02:00
parent 900d3c8262
commit 2efa985a05
12 changed files with 172 additions and 28 deletions

View File

@ -0,0 +1,13 @@
#pragma once
#ifdef PLATFORM_NATIVE
#include <stdlib.h>
static constexpr uint32_t MALLOC_CAP_SPIRAM = -1;
inline void *heap_caps_malloc(size_t size, uint32_t /*caps*/) {
return std::malloc(size);
}
#endif

View File

@ -8,6 +8,12 @@
namespace portablefs
{
using File = ::File;
using string = String;
template<typename T>
String to_string(const T & value) {
return String(value);
}
class Dir
{
@ -85,20 +91,11 @@ namespace portablefs
return SPIFFS.totalBytes();
}
} // namespace portablefs
#endif
#ifdef PLATFORM_ESP8266
#include <FS.h>
namespace portablefs
{
using Dir;
} // namespace portablefs
#endif
#ifdef PLATFORM_NATIVE
@ -108,18 +105,27 @@ namespace portablefs
namespace fs = std::filesystem;
enum SeekMode {
SeekSet = 0,
SeekCur = 1,
SeekEnd = 2
};
namespace portablefs
{
const std::string basePath = "./base";
using string = std::string;
using std::to_string;
class Dir
{
public:
Dir() {}
Dir(const String &path)
: it_(fs::directory_iterator(path).begin()),
end_(fs::directory_iterator(path).end()),
: it_(fs::directory_iterator(path)),
end_(fs::directory_iterator()),
firstIncremented_(false)
{
}
@ -136,22 +142,22 @@ namespace portablefs
bool isFile()
{
return file.is_regular_file();
return it_->is_regular_file();
}
bool isDirectory()
{
return it_.is_directory();
return it_->is_directory();
}
String fileName() const
{
return it_.path().filename().string();
return it_->path().filename().string();
}
size_t fileSize() const
{
return it_.file_size();
return it_->file_size();
}
private:
@ -165,28 +171,73 @@ namespace portablefs
return Dir(path);
}
class File
{
public:
File(std::FILE * file) : file_(file) {}
bool seek(uint32_t pos, SeekMode mode)
{
if(mode == SeekSet)
return std::fseek(file_, pos, SEEK_SET) == 0;
else if (mode == SeekCur)
return std::fseek(file_, pos, SEEK_CUR) == 0;
else
return std::fseek(file_, pos, SEEK_END) == 0;
}
bool seek(uint32_t pos) { return std::fseek(file_, pos, SEEK_SET); }
size_t write(const uint8_t *buf, size_t size) {
return std::fwrite(buf, size, 1UL, file_);
}
size_t size() {
auto current_position = ftell(file_);
seek(0, SeekEnd);
auto end = ftell(file_);
seek(0, SeekSet);
auto begin = ftell(file_);
seek(current_position, SeekSet);
return end - begin;
}
private:
std::FILE * file_;
};
inline File open(const char *name, const char *mode)
{
if(mode == "r")
return fopen()
return SPIFFS.open(name, mode);
std::string modeStr;
if(modeStr.find("b") == std::string::npos)
modeStr += "b";
return File(std::fopen(name, modeStr.c_str()));
}
inline bool exists(const char *name)
{
return SPIFFS.exists(name);
return fs::exists(fs::path(name));
}
inline bool remove(const char *name)
{
return SPIFFS.remove(name);
return fs::remove(fs::path(name));
}
inline bool mkdir(const char *name)
{
return SPIFFS.mkdir(name);
return fs::create_directory(fs::path(name));
}
//inline size_t totalBytes() {
// return 1024 * 1024 * 4; // some dummy value
//}
//inline size_t usedBytes() {
// // TODO: makes more sense to report total file size of all files in folder
// return 1024; // some dummy value
//}
} // namespace portablefs
#endif

View File

@ -2,6 +2,7 @@
#include <iostream>
#ifdef PLATFORM_NATIVE
class SerialMock {
public:
@ -16,3 +17,6 @@ public:
};
static SerialMock Serial;
#endif

View File

@ -0,0 +1,36 @@
#pragma once
#ifdef PLATFORM_ESP32
#include "Preferences.h"
#endif
#ifdef PLATFORM_NATIVE
#include <map>
class Preferences
{
public:
std::string getString(const char * key, const std::string & defaultValue) const {
std::string strKey(key);
const auto it = strings_.find(strKey);
return (it == string_.end()) ? defaultValue : *it;
}
size_t putString(const char * key, const char * value) {
strings_[std::string(key)] = std::string(value);
return std::strlen(value);
}
bool remove(const char * key) {
return strings_.remove(key);
}
private:
std::map<std::string, std::string> strings_;
};
#endif // PLATFORM_NATIVE

View File

@ -0,0 +1,16 @@
#pragma once
#ifdef PLATFORM_ESP32
#include <Arduino.h>
#endif
#ifdef PLATFORM_NATIVE
#include <chrono>
inline unsigned long millis() {
static auto timeOfFirstCall = std::chrono::steady_clock::now();
const auto timePoint = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(timePoint - timeOfFirstCall).count();
}
#endif

View File

@ -1,4 +1,5 @@
#include "Logger.h"
#include "AllocAbstraction.h"
constexpr size_t LOG_SIZE = 1024 * 1024 * 2;
@ -12,10 +13,12 @@ Logger *Logger::getInstance()
void Logger::init()
{
theLogger = new Logger();
#ifdef PLATFORM_ESP32
Serial.begin(115200);
while (!Serial)
{
}
#endif
}
Logger::Logger()

View File

@ -1,6 +1,16 @@
#pragma once
#include "TimeAbstraction.h"
#ifdef PLATFORM_ESP32
#include "Arduino.h"
#endif
#ifdef PLATFORM_NATIVE
#include <stdint.h>
#include <string.h>
#include <utility>
#endif
#define LOG_INFO(...) \
{ \

View File

@ -12,7 +12,7 @@ class VectorAdaptor {
public:
VectorAdaptor(std::vector<uint8_t> * v) : v_(v) {}
void write(const char *data, uint32_t size) {
void write(const uint8_t *data, uint32_t size) {
v_->insert(v_->end(), data, data + size );
}
@ -38,7 +38,7 @@ public:
FilePtrAdaptor(const FilePtrAdaptor &) = delete;
void operator=(const FilePtrAdaptor &) = delete;
void write(const char *data, uint32_t size) {
void write(const uint8_t *data, uint32_t size) {
fwrite(data, size, 1, fptr);
}

View File

@ -1,7 +1,9 @@
#include "Dtypes.h"
#include "SessionChunk.h"
#include "FilesystemAbstraction.h"
#include "AllocAbstraction.h"
#include "Logger.h"
#include "SwimTrackerConfig.h"
template <typename Measurement_T, uint32_t MAX_SIZE>
@ -85,7 +87,8 @@ private:
deleteUntilBytesFree(CONFIG_SESSION_MAX_SIZE);
LOG_INFO(" %ld after deleteUntilBytesFree()", millis());
String filename = String(CONFIG_DATA_PATH) + "/" + String(chunk->getStartTime());
using fs_string = portablefs::string;
fs_string filename = fs_string(CONFIG_DATA_PATH) + "/" + portablefs::to_string(chunk->getStartTime());
if (portablefs::exists(filename.c_str()))
{
auto file = portablefs::open(filename.c_str(), "r+");
@ -111,6 +114,7 @@ private:
void deleteUntilBytesFree(size_t requiredSpace)
{
#ifdef PLATFORM_ESP32
auto freeBytes = portablefs::totalBytes() - portablefs::usedBytes();
while (freeBytes < requiredSpace)
{
@ -140,6 +144,7 @@ private:
assert(newFreeBytes > freeBytes);
freeBytes = newFreeBytes;
}
#endif // PLATFORM_ESP32
}
ChunkT *chunk;

View File

@ -1,4 +1,5 @@
#pragma once
#include "Logger.h"
template<typename T> struct TypeToMsgPackCode{};
template<> struct TypeToMsgPackCode<uint8_t> { static const char CODE; };
@ -52,7 +53,7 @@ public:
else
{
size |= 0b10000000;
writer->write((byte*)(&size), 1);
writer->write((uint8_t*)(&size), 1);
}
}

View File

@ -1,6 +1,6 @@
#pragma once
#include "Preferences.h"
#include "PreferencesAbstraction.h"
#include <WiFi.h>
/**

View File

@ -0,0 +1,5 @@
#include "Logger.h"
int main()
{}