From fe71d498b56055525c0dc2d72a092a98f677ddb8 Mon Sep 17 00:00:00 2001 From: uvos Date: Thu, 23 May 2024 17:31:21 +0200 Subject: [PATCH] inital commit --- CMakeLists.txt | 7 ++++ main.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..90aa913 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20) + +project(infinilog LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(${PROJECT_NAME} main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..bcc0fcc --- /dev/null +++ b/main.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include + +static constexpr uint8_t magic[] = {0xf6, 0x76, 0xd8, 0x09}; + +struct __attribute__((packed)) Frame { + uint64_t time; + uint32_t steps; + uint8_t batteryPercent; + uint8_t heartRate; + uint8_t pad[2]; +}; + +bool arrayeq(const uint8_t* a, const uint8_t* b, size_t len) +{ + for(size_t i = 0; i < len; ++i) + { + if(a[i] != b[i]) + return false; + } + + return true; +} + +std::vector readFile(const std::filesystem::path& path) +{ + std::ifstream file; + file.open(path, std::ios_base::binary | std::ios_base::in); + if(!file.is_open()) + { + std::cout<<"could not open "<(&magicRef), 4); + + if(!arrayeq(magic, magicRef, 4)) + { + std::cout<(&versionMajor), 1); + file.read(reinterpret_cast(&versionMinor), 1); + + if(versionMajor > 1 || file.bad()) + { + std::cout<(&size), sizeof(size)); + + if(sizeof(Frame) != size) + { + std::cout<(&length), sizeof(length)); + + std::vector frames; + frames.reserve(length); + + for(size_t i = 0; i < length && !file.bad(); ++i) + { + Frame frame = {}; + file.read(reinterpret_cast(&frame), sizeof(frame)); + + if(frame.time != 0) + frames.push_back(frame); + } + + std::sort(frames.begin(), frames.end(), [](const Frame& a, const Frame& b){return a.time < b.time;}); + return frames; +} + +int main(int argc, char** argv) +{ + if(argc < 2) + { + std::cout<<"Usage: "<<(argc > 0 ? argv[0] : "infinilog")<<" [LOG_FILE]\n"; + return 1; + } + + std::vector frames = readFile(argv[1]); + + if(frames.empty()) + return 1; + + std::cout<<"timestamp, steps, battery percent, heart rate\n"; + for(auto& frame : frames) { + std::time_t time = frame.time; + std::tm* ptm = std::localtime(&time); + char timeBuffer[32]; + std::strftime(timeBuffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm); + + std::cout<(frame.batteryPercent)<<", "<(frame.heartRate)<<'\n'; + } + + return 0; +}