156 lines
5.3 KiB
C++
156 lines
5.3 KiB
C++
#include <cstring>
|
|
#include <iostream>
|
|
#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];
|
|
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
|
|
<< " --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) {
|
|
init = true;
|
|
}
|
|
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 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;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
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, '-') && !startsWith(str, "--") && contains(str, shortHand);
|
|
} |