commit d62635c7d9d1f0f3feeeb8feb90b3931d70a0273 Author: zacharias Date: Thu Jun 19 16:25:25 2025 +0200 Initial Commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b99f260 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.31) +project(home_lab_watchdog_keepalive) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(home_lab_watchdog_keepalive main.cpp + str_utils.h + str_utils.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a308b76 --- /dev/null +++ b/main.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include "str_utils.h" + +const char* VERSION = "0.0.1-DEV"; + +bool shortHand(const char* str, char shortHand); + +int main(const int argc, char** argv) { + + if (argc > 1) { + for (int i = 1; i < argc; i++) { + const char* arg = argv[i]; + if (strcmp(arg, "--help") == 0 || shortHand(arg, 'h')) { + std::cout << "Server Arduino Watchdog version '" << VERSION << "' by Zacharias (c) 2025" << std::endl + << std::endl + << "Options:" << std::endl + << "[requires root] -short --long-option Description" << std::endl + << "[X] --init Initializes the Server end of this system, find the arduino and creates systemd services" << std::endl + << " -h --help Prints this help message" << std::endl + << " -b --heartbeat Sends a heartbeat to the arduino, Intended to be send by systemd unit and timer" << std::endl + << " -i --info Queries the Arduino Watchdog for status, like time before time-out" << std::endl + << " -v --version Print's the version" << std::endl + ; + return 0; + } + else if (strcmp(arg, "--init") == 0) { + if (getuid() != 0) { + std::cerr << "Initialization requires root!" << std::endl; + return -1; + } + std::cout << "Initialization :)" << std::endl; + // TODO: Add init system + } + else if (strcmp(arg, "--heartbeat") == 0 || shortHand(arg, 'b')) { + // TODO: Send heartbeat + } + else if (strcmp(arg, "--info") == 0 || shortHand(arg, 'i')) { + // TODO: Send info + // TODO: Print response status + } + else if (strcmp(arg, "--version") == 0 || shortHand(arg, 'v')) { + std::cout << VERSION << " by Zacharias (c) 2025" << std::endl; + } + else { + std::cerr << "Unknown option '" << arg << "'" << std::endl << "Test '" << argv[0] << " --help'" << std::endl; + return -1; + } + } + } + else { + std::cerr << "Missing arguments, try " << argv[0] << " --help" << std::endl; + return -1; + } + return -1; +} + +bool shortHand(const char* str, char shortHand) { + if (str == nullptr) return false; + if (shortHand == 0) return false; + return startWith(str, '-') && contains(str, shortHand); +} \ No newline at end of file diff --git a/str_utils.cpp b/str_utils.cpp new file mode 100644 index 0000000..63aea22 --- /dev/null +++ b/str_utils.cpp @@ -0,0 +1,32 @@ +// +// Created by server on 6/19/25. +// +#include "str_utils.h" + +#include + +bool startWith(const char * str, const char ch) { + if (str == nullptr) return false; // Fail if str is empty + if (ch == 0) return false; // Fail if ch is NULL + if (str[0] != ch) return false; // Fail if str[0] != ch + return true; // str[0] == ch +} + +bool contains(const char * str, const char ch) { + if (str == nullptr) return false; // Fail if str is null + if (ch == 0) return false; // Fail if ch is null + const size_t len = strlen(str); + if (len == 0) return false; // Fail if str is empty + for (int i = 0; i < len; i++) { + if (str[i] == ch) return true; // Found ch in str + } + return false; // ch is not in str +} + +bool endsWith(const char * str, const char ch) { + if (str == nullptr) return false; // Fail if str is empty + if (ch == 0) return false; // Fail if ch is NULL + const size_t len = strlen(str); + if (str[len-1] != ch) return false; // Fail if str[len-1] != ch + return true; // str[len-1] == ch +} diff --git a/str_utils.h b/str_utils.h new file mode 100644 index 0000000..53f5702 --- /dev/null +++ b/str_utils.h @@ -0,0 +1,12 @@ +// +// Created by server on 6/19/25. +// + +#ifndef STR_UTILS_H +#define STR_UTILS_H + +bool startWith(const char* str, char ch); +bool contains(const char* str, char ch); +bool endsWith(const char* str, char ch); + +#endif //STR_UTILS_H