diff --git a/firmware/lib/basic/AllocAbstraction.h b/firmware/lib/basic/AllocAbstraction.h new file mode 100644 index 0000000..3f1fbcb --- /dev/null +++ b/firmware/lib/basic/AllocAbstraction.h @@ -0,0 +1,13 @@ +#pragma once + +#ifdef PLATFORM_NATIVE + +#include + +static constexpr uint32_t MALLOC_CAP_SPIRAM = -1; + +inline void *heap_caps_malloc(size_t size, uint32_t /*caps*/) { + return std::malloc(size); +} + +#endif \ No newline at end of file diff --git a/firmware/lib/basic/FilesystemAbstraction.h b/firmware/lib/basic/FilesystemAbstraction.h index 6fc20b8..f9ac66b 100644 --- a/firmware/lib/basic/FilesystemAbstraction.h +++ b/firmware/lib/basic/FilesystemAbstraction.h @@ -8,6 +8,12 @@ namespace portablefs { using File = ::File; + using string = String; + + template + 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 - -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 \ No newline at end of file diff --git a/firmware/lib/basic/MockSerial.h b/firmware/lib/basic/MockSerial.h index 6daaccb..26fb51d 100644 --- a/firmware/lib/basic/MockSerial.h +++ b/firmware/lib/basic/MockSerial.h @@ -2,6 +2,7 @@ #include +#ifdef PLATFORM_NATIVE class SerialMock { public: @@ -15,4 +16,7 @@ public: } }; -static SerialMock Serial; \ No newline at end of file +static SerialMock Serial; + + +#endif \ No newline at end of file diff --git a/firmware/lib/basic/PreferencesAbstraction.h b/firmware/lib/basic/PreferencesAbstraction.h new file mode 100644 index 0000000..16f4337 --- /dev/null +++ b/firmware/lib/basic/PreferencesAbstraction.h @@ -0,0 +1,36 @@ +#pragma once + + +#ifdef PLATFORM_ESP32 +#include "Preferences.h" +#endif + + + +#ifdef PLATFORM_NATIVE + +#include + +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 strings_; +}; + +#endif // PLATFORM_NATIVE diff --git a/firmware/lib/basic/TimeAbstraction.h b/firmware/lib/basic/TimeAbstraction.h new file mode 100644 index 0000000..b3015f9 --- /dev/null +++ b/firmware/lib/basic/TimeAbstraction.h @@ -0,0 +1,16 @@ +#pragma once + +#ifdef PLATFORM_ESP32 +#include +#endif + +#ifdef PLATFORM_NATIVE + +#include + +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(timePoint - timeOfFirstCall).count(); +} +#endif \ No newline at end of file diff --git a/firmware/lib/logging/Logger.cpp b/firmware/lib/logging/Logger.cpp index 7c72de7..a8f8be3 100644 --- a/firmware/lib/logging/Logger.cpp +++ b/firmware/lib/logging/Logger.cpp @@ -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() diff --git a/firmware/lib/logging/Logger.h b/firmware/lib/logging/Logger.h index 2666f81..b17a8ad 100644 --- a/firmware/lib/logging/Logger.h +++ b/firmware/lib/logging/Logger.h @@ -1,6 +1,16 @@ #pragma once +#include "TimeAbstraction.h" + +#ifdef PLATFORM_ESP32 #include "Arduino.h" +#endif + +#ifdef PLATFORM_NATIVE +#include +#include +#include +#endif #define LOG_INFO(...) \ { \ diff --git a/firmware/lib/session/MockStorage.h b/firmware/lib/session/MockStorage.h index 9b59164..b926a17 100644 --- a/firmware/lib/session/MockStorage.h +++ b/firmware/lib/session/MockStorage.h @@ -12,7 +12,7 @@ class VectorAdaptor { public: VectorAdaptor(std::vector * 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); } diff --git a/firmware/lib/session/SimpleMeasurementSession.h b/firmware/lib/session/SimpleMeasurementSession.h index 2d1984d..38d684f 100644 --- a/firmware/lib/session/SimpleMeasurementSession.h +++ b/firmware/lib/session/SimpleMeasurementSession.h @@ -1,7 +1,9 @@ #include "Dtypes.h" #include "SessionChunk.h" #include "FilesystemAbstraction.h" +#include "AllocAbstraction.h" #include "Logger.h" +#include "SwimTrackerConfig.h" template @@ -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; diff --git a/firmware/lib/session/StreamingMsgPackEncoder.h b/firmware/lib/session/StreamingMsgPackEncoder.h index ba39e40..80f3928 100644 --- a/firmware/lib/session/StreamingMsgPackEncoder.h +++ b/firmware/lib/session/StreamingMsgPackEncoder.h @@ -1,4 +1,5 @@ #pragma once +#include "Logger.h" template struct TypeToMsgPackCode{}; template<> struct TypeToMsgPackCode { static const char CODE; }; @@ -52,7 +53,7 @@ public: else { size |= 0b10000000; - writer->write((byte*)(&size), 1); + writer->write((uint8_t*)(&size), 1); } } diff --git a/firmware/lib/wifimanager/WifiManager.h b/firmware/lib/wifimanager/WifiManager.h index 5494692..5804608 100644 --- a/firmware/lib/wifimanager/WifiManager.h +++ b/firmware/lib/wifimanager/WifiManager.h @@ -1,6 +1,6 @@ #pragma once -#include "Preferences.h" +#include "PreferencesAbstraction.h" #include /** diff --git a/firmware/src/native_main.cpp b/firmware/src/native_main.cpp new file mode 100644 index 0000000..00fd3c1 --- /dev/null +++ b/firmware/src/native_main.cpp @@ -0,0 +1,5 @@ +#include "Logger.h" + + +int main() +{} \ No newline at end of file