Initial Commit
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -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
|
||||
8
CMakeLists.txt
Normal file
8
CMakeLists.txt
Normal file
@@ -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)
|
||||
63
main.cpp
Normal file
63
main.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#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);
|
||||
}
|
||||
32
str_utils.cpp
Normal file
32
str_utils.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by server on 6/19/25.
|
||||
//
|
||||
#include "str_utils.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
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
|
||||
}
|
||||
12
str_utils.h
Normal file
12
str_utils.h
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user