From b04ea81edb74543089b5f374b298be929fd8a3c8 Mon Sep 17 00:00:00 2001 From: zacharias Date: Fri, 20 Jun 2025 19:17:41 +0200 Subject: [PATCH] added Arch post install script, and some general updates --- CMakeLists.txt | 5 +- PKGBUILD | 20 ++++++-- arduino_watchdog.install | 4 ++ commands.h | 15 ++++++ main.cpp | 105 ++++++++++++++++++++++++++++++++++++--- str_utils.cpp | 11 ++++ str_utils.h | 1 + 7 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 arduino_watchdog.install create mode 100644 commands.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a05b9ed..212418b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,11 @@ cmake_minimum_required(VERSION 3.31) project(home_lab_watchdog_keepalive) -add_definitions(-DVERSION=${VERSION}) +add_definitions(-DVERSION="VERSION") set(CMAKE_CXX_STANDARD 20) add_executable(home_lab_watchdog_keepalive main.cpp str_utils.h - str_utils.cpp) + str_utils.cpp + commands.h) diff --git a/PKGBUILD b/PKGBUILD index 01d1055..6293fb8 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,14 +1,21 @@ # PKGBUILD +pkgver() { + cd "$srcdir/home-lab_watchdog_keepalive" + git describe --tags | sed 's/^v//;s/-/+/g' +} +pkgver=0.0.1_DEV pkgname=arduino_watchdog pkgrel=1 pkgdesc="Arduino based external watchdog to keep server online" arch=('any') license=('MIT') -makedepends=('cmake: build system used') +makedepends=('cmake>=4.0.0', 'git>=2.50.0') depends=('systemd>=257.6') -source=('git://git.server.4zellen.se/Zacharias/home-lab_watchdog_keepalive.git') +source=('git+https://git.server.4zellen.se/Zacharias/home-lab_watchdog_keepalive.git') maintainer="Zacharias Zellén " url="https://git.server.4zellen.se/Zacharias/home-lab_watchdog_keepalive.git" +install=arduino_watchdog.install +sha256sums=('SKIP') build() { @@ -18,7 +25,10 @@ build() { make -C build } -pkgver() { - cd "$srcdir/home-lab_watchdog_keepalive" - git describe --tags | sed 's/^v//;s/-/+/g' +package() { + install -Dm755 "$srcdir/home-lab_watchdog_keepalive/build/home_lab_watchdog_keepalive" "$pkgdir/usr/bin/arduino_watchdog" + + install -Dm755 "$srcdir/home-lab_watchdog_keepalive/arduino-watchdog-heartbeat.service" "$pkgdir/etc/systemd/system/arduino-watchdog-heartbeat.service" + + install -Dm755 "$srcdir/home-lab_watchdog_keepalive/arduino-watchdog-heartbeat.timer" "$pkgdir/etc/systemd/system/arduino-watchdog-heartbeat.timer" } \ No newline at end of file diff --git a/arduino_watchdog.install b/arduino_watchdog.install new file mode 100644 index 0000000..68fd2b6 --- /dev/null +++ b/arduino_watchdog.install @@ -0,0 +1,4 @@ +post_install() { + getent passwd arduino-watchdog >/dev/null || \ + useradd -r -s /usr/bin/nologin -d /var/lib/arduino-watchdog arduino-watchdog +} \ No newline at end of file diff --git a/commands.h b/commands.h new file mode 100644 index 0000000..dea628a --- /dev/null +++ b/commands.h @@ -0,0 +1,15 @@ +// +// Created by server on 6/20/25. +// + +#ifndef COMMANDS_H +#define COMMANDS_H + +#define HEARTBEAT "002" +#define INFO "001" +#define STATUS "003" + +#define SUCCESS "200" +#define FAIL "201" + +#endif //COMMANDS_H diff --git a/main.cpp b/main.cpp index 92e0cf7..ff6abd4 100644 --- a/main.cpp +++ b/main.cpp @@ -3,15 +3,28 @@ #include #include "str_utils.h" #include +#include +#include + +#include +#include +#include + +#include "commands.h" #ifndef VERSION #define VERSION "unknown-DEV" #endif +namespace fs = std::filesystem; + bool shortHand(const char* str, char shortHand); int main(const int argc, char** argv) { + bool init = false; + char* device = nullptr; + if (argc > 1) { for (int i = 1; i < argc; i++) { const char* arg = argv[i]; @@ -25,17 +38,18 @@ int main(const int argc, char** argv) { << " -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 + << " --arduino Set's the Arduino to be used, skips the testing to find the watchdog device. OPS will fail if the device is not a Watchdog" << std::endl + << std::endl + << "Usages:" << std::endl + << " arduino_watchdog --arduino=/dev/ttyACM0 --init:" << std::endl + << " Will run init on device /dev/ttyACM0" << std::endl + << " arduino_watchdog --init:" << std::endl + << " Will run init and try to find the device" << 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; - - + init = true; } else if (strcmp(arg, "--heartbeat") == 0 || shortHand(arg, 'b')) { // TODO: Send heartbeat @@ -47,6 +61,10 @@ int main(const int argc, char** argv) { else if (strcmp(arg, "--version") == 0 || shortHand(arg, 'v')) { std::cout << VERSION << " by Zacharias (c) 2025" << std::endl; } + else if (startsWith(arg, "--arduino=")) { + device = strdup(arg + 10); + std::cout << device << std::endl; + } else { std::cerr << "Unknown option '" << arg << "'" << std::endl << "Test '" << argv[0] << " --help'" << std::endl; return -1; @@ -57,11 +75,82 @@ int main(const int argc, char** argv) { std::cerr << "Missing arguments, try " << argv[0] << " --help" << std::endl; return -1; } + + + if (init == true) { + if (getuid() != 0) { + std::cerr << "Initialization requires root!" << std::endl; + return -1; + } + //std::cout << "Initialization :)" << std::endl; + + if (device == nullptr) { + std::cerr << "Not implemented yet! use --arduino" << std::endl; + return -1; + + const std::string path = "/dev/serial/by-path/"; + + if (!fs::exists(path)) { + std::cerr << "Serial path does not exist!" << std::endl; + return -1; + } + + for (const auto& p : fs::directory_iterator(path)) { + + } + } + else { + int serial_port = open(device, O_RDWR | O_NOCTTY | O_NDELAY); + if (serial_port < 0) { + std::cerr << "Error " << errno << " opening serial port: " << strerror(errno) << std::endl; + return -1; + } + + struct termios tty; + + if (tcgetattr(serial_port, &tty) != 0) { + std::cerr << "Error " << errno << " getting termios: " << strerror(errno) << std::endl; + return -1; + } + + cfsetispeed(&tty, B9600); + cfsetospeed(&tty, B9600); + + tty.c_cflag &= ~PARENB; + tty.c_cflag &= ~CSTOPB; + tty.c_cflag &= ~CSIZE; + tty.c_cflag |= CS8; + + tty.c_cflag &= ~CRTSCTS; + + tty.c_lflag = 0; + tty.c_iflag = 0; + tty.c_oflag = 0; + + tty.c_cc[VMIN] = 0; + tty.c_cc[VTIME] = 10; + + if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { + std::cerr << "Error " << errno << " setting termios: " << strerror(errno) << std::endl; + return -1; + } + + const char* msg = INFO; + int n_written = write(serial_port, msg, strlen(msg)); + if (n_written < 0) { + std::cerr << "Write failed!" << std::endl; + } + + + } + } + + 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); + return startWith(str, '-') && !startsWith(str, "--") && contains(str, shortHand); } \ No newline at end of file diff --git a/str_utils.cpp b/str_utils.cpp index 63aea22..4803050 100644 --- a/str_utils.cpp +++ b/str_utils.cpp @@ -30,3 +30,14 @@ bool endsWith(const char * str, const char ch) { if (str[len-1] != ch) return false; // Fail if str[len-1] != ch return true; // str[len-1] == ch } + +bool startsWith(const char * str, const char ch[]) { + if (str == nullptr) return false; // Fail if str is null + if (ch == nullptr) return false; // Fail if ch is null + const size_t len = strlen(ch); + if (len == 0) return false; // Fail if str is empty + for (int i = 0; i < len; i++) { + if (str[i] != ch[i]) return false; // Found ch in str + } + return true; // ch is not in str +} \ No newline at end of file diff --git a/str_utils.h b/str_utils.h index 53f5702..2c88383 100644 --- a/str_utils.h +++ b/str_utils.h @@ -8,5 +8,6 @@ bool startWith(const char* str, char ch); bool contains(const char* str, char ch); bool endsWith(const char* str, char ch); +bool startsWith(const char* str, const char* ch); #endif //STR_UTILS_H