added Arch post install script, and some general updates

This commit is contained in:
2025-06-20 19:17:41 +02:00
parent 25c0dd16ca
commit b04ea81edb
7 changed files with 146 additions and 15 deletions

View File

@@ -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)

View File

@@ -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 <zacharias@4zellen.se>"
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"
}

4
arduino_watchdog.install Normal file
View File

@@ -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
}

15
commands.h Normal file
View File

@@ -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

105
main.cpp
View File

@@ -3,15 +3,28 @@
#include <unistd.h>
#include "str_utils.h"
#include <sys/stat.h>
#include <filesystem>
#include <string>
#include <fcntl.h>
#include <termios.h>
#include <cstring>
#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);
}

View File

@@ -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
}

View File

@@ -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