From 4557165678de8c3d8c33193853c2a8c8176f81e6 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Tue, 27 Oct 2026 21:25:07 +0300 Subject: [PATCH] alpha pre-release --- Poweroff_daemon/Readme.md | 2 +- Poweroff_daemon/cmdlnopts.c | 6 ++- Poweroff_daemon/cmdlnopts.h | 2 + Poweroff_daemon/main.c | 27 +++++++---- Poweroff_daemon/poweroff_daemon.cflags | 2 +- Poweroff_daemon/poweroff_daemon.files | 2 + Poweroff_daemon/poweroff_daemon.includes | 1 + Poweroff_daemon/serial.c | 38 +++++++++++++-- Poweroff_daemon/serial.h | 2 + Poweroff_daemon/server.c | 62 ++++++++++++++++++++++++ Poweroff_daemon/server.h | 22 +++++++++ 11 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 Poweroff_daemon/server.c create mode 100644 Poweroff_daemon/server.h diff --git a/Poweroff_daemon/Readme.md b/Poweroff_daemon/Readme.md index ebe8ade..2dc6168 100644 --- a/Poweroff_daemon/Readme.md +++ b/Poweroff_daemon/Readme.md @@ -1,3 +1,3 @@ Simple daemon checking voltage & button0 state of my 'fan controller'. -Under development, wait pre-release... +Alpha-version, there may be some problems... diff --git a/Poweroff_daemon/cmdlnopts.c b/Poweroff_daemon/cmdlnopts.c index 41a3597..c93ccd1 100644 --- a/Poweroff_daemon/cmdlnopts.c +++ b/Poweroff_daemon/cmdlnopts.c @@ -32,6 +32,7 @@ static cmdln_t G = { .poll_interval = DEFAULT_INTERVAL, .timeout_us = READ_TIMEOUT_US, .baudrate = DEFAULT_BAUDRATE, + .sockpath = DEFAULT_SOCKPATH, }; static int help = 0; @@ -43,8 +44,9 @@ static sl_option_t options[] = { {"timeout", NEED_ARG, NULL, 't', arg_double, &G.timeout_us, "serial in timeout (useconds), default: " STR(READ_TIMEOUT_US)}, {"verbose", NO_ARGS, NULL, 'v', arg_none, &G.verbose, "verbose output (each -v adds level), default: WARN"}, {"device", NEED_ARG, NULL, 'd', arg_string, &G.serialdev, "serial device, default: " STR(DEFAULT_DEV)}, - {"baudrate",NEED_ARG, NULL, 'b', arg_int, &G.baudrate, "default serial baudrate, default: " STR(DEFAULT_BAUDRATE)}, - {"help", NO_ARGS, &help, 'h', arg_none, NULL, "show this help"}, + {"baudrate",NEED_ARG, NULL, 'b', arg_int, &G.baudrate, "serial baudrate, default: " STR(DEFAULT_BAUDRATE)}, + {"sockpath",NEED_ARG, NULL, 's', arg_string, &G.sockpath, "UNIX-socket path, default: " STR(DEFAULT_SOCKPATH)}, + {"help", NO_ARGS, NULL, 'h', arg_none, &help, "show this help"}, end_option }; diff --git a/Poweroff_daemon/cmdlnopts.h b/Poweroff_daemon/cmdlnopts.h index 64a7328..bf2cf73 100644 --- a/Poweroff_daemon/cmdlnopts.h +++ b/Poweroff_daemon/cmdlnopts.h @@ -19,6 +19,7 @@ #pragma once #define DEFAULT_PIDFILE "/tmp/powerdaemon.pid" +#define DEFAULT_SOCKPATH "@poweroffdaemon" #define DEFAULT_DEV "/dev/ttyUSB0" #define DEFAULT_BAUDRATE 115200 #define DEFAULT_INTERVAL 1.0 @@ -28,6 +29,7 @@ typedef struct{ char *pidfile; char *logfile; char *serialdev; + char *sockpath; double poll_interval; double timeout_us; int verbose; diff --git a/Poweroff_daemon/main.c b/Poweroff_daemon/main.c index 0c64f0c..84a9baf 100644 --- a/Poweroff_daemon/main.c +++ b/Poweroff_daemon/main.c @@ -23,9 +23,10 @@ #include "cmdlnopts.h" #include "serial.h" +#include "server.h" static cmdln_t *G = NULL; -static bool isrunning = false; +bool isrunning = false; static pid_t childpid = 0; void signals(int sig){ @@ -62,10 +63,9 @@ int main(int argc, char **argv){ } /* Daemonize */ -/* if (sl_daemonize() != 0) { + if(sl_daemonize() != 0){ ERR("daemonize failed"); } -*/ /* Set global timeout for sl_tty_read() */ sl_tty_tmout(G->timeout_us); @@ -94,7 +94,7 @@ int main(int argc, char **argv){ } lastd = t; WARNX("Child %d died\n", childpid); - sleep(1); + sleep(5); }else{ prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies break; // go out to normal functional @@ -110,24 +110,33 @@ int main(int argc, char **argv){ isrunning = true; + if(!run_server(G->sockpath)){ + LOGERR("Can't run server @ %s", G->sockpath); + ERRX("Can't run server @ %s", G->sockpath); + } + while(isrunning){ bool trigger = false; if(check_buttons()) trigger = true; if(check_voltage()) trigger = true; if(trigger){ - LOGERR("Trigger condition met, executing poweroff"); + LOGWARN("Trigger condition met, executing poweroff"); DBG("poweroff"); - /* int ret = system("poweroff"); - if (ret == -1) { + int ret = system("/sbin/poweroff"); + if(ret == -1){ LOGERR("Failed to execute poweroff: %s", strerror(errno)); + }else{ + LOGWARN("Poweroff called"); + break; } - break; -*/ } + } usleep((useconds_t)(G->poll_interval * 1e6)); // seconds -> useconds } + kill_server(); + close_serial(); return 0; diff --git a/Poweroff_daemon/poweroff_daemon.cflags b/Poweroff_daemon/poweroff_daemon.cflags index 68d5165..a07354f 100644 --- a/Poweroff_daemon/poweroff_daemon.cflags +++ b/Poweroff_daemon/poweroff_daemon.cflags @@ -1 +1 @@ --std=c17 \ No newline at end of file +-std=c23 \ No newline at end of file diff --git a/Poweroff_daemon/poweroff_daemon.files b/Poweroff_daemon/poweroff_daemon.files index 8b80df2..5a88634 100644 --- a/Poweroff_daemon/poweroff_daemon.files +++ b/Poweroff_daemon/poweroff_daemon.files @@ -3,3 +3,5 @@ cmdlnopts.h main.c serial.c serial.h +server.c +server.h diff --git a/Poweroff_daemon/poweroff_daemon.includes b/Poweroff_daemon/poweroff_daemon.includes index e69de29..9c558e3 100644 --- a/Poweroff_daemon/poweroff_daemon.includes +++ b/Poweroff_daemon/poweroff_daemon.includes @@ -0,0 +1 @@ +. diff --git a/Poweroff_daemon/serial.c b/Poweroff_daemon/serial.c index 3994450..05821f8 100644 --- a/Poweroff_daemon/serial.c +++ b/Poweroff_daemon/serial.c @@ -119,10 +119,20 @@ bool check_buttons(){ /* Check voltage: returns 1 if V12 < 9700 */ bool check_voltage(){ int voltage = 0; + static double tlog = -1.; if(!get_int(CMD_GETVOLTAGE, ANS_V12, &voltage)) return false; if(voltage < LOWEST_V12){ LOGERR("V12 low: %d (%.2f V)", voltage, (double)voltage / 100.); return true; + }else if(voltage < LOG_V12){ // log once per minute + double tnow = sl_dtime(); + if(tnow - tlog > LOG_VOLTAGE_DT){ + LOGMSG("Voltage now: %d (%.2f V)", voltage, (double)voltage / 100.); + tlog = tnow; + } + }else if(tlog > 0.){ + LOGMSG("Power restored, now: %d (%.2f V)", voltage, (double)voltage / 100.); + tlog = -1.; } return false; } @@ -130,11 +140,31 @@ bool check_voltage(){ bool send_custom_command(const char *command, char *answer, size_t anslen){ if(!tty || !command) return false; bool ret = false; + size_t L = anslen; pthread_mutex_lock(&serial_mutex); - if(sl_tty_write(tty->comfd, command, strlen(command)) && sl_tty_read(tty) > 0){ - if(answer && anslen > 1) strncpy(answer, tty->buf, anslen-1); - ret = true; - } + DBG("Send command '%s'", command); + if(0 == sl_tty_write(tty->comfd, command, strlen(command))){ + sl_tty_write(tty->comfd, "\n", 1); // fix for newline + int got = sl_tty_read(tty); + if(got < 0) WARNX("Can't read"); + else while(got > 0){ + DBG("got: %d", got); + ret = true; + DBG("Got ans: %s", tty->buf); + if(answer && anslen > 1){ + size_t len = tty->buflen; + if(tty->buflen > anslen) len = anslen; + strncpy(answer, tty->buf, len); + answer += len; + anslen -= len; + }else break; + got = sl_tty_read(tty); + } + }else WARNX("Can't write to TTY"); pthread_mutex_unlock(&serial_mutex); + if(L > 0){ + if(anslen < 1) answer[-1] = 0; + else *answer = 0; + } return ret; } diff --git a/Poweroff_daemon/serial.h b/Poweroff_daemon/serial.h index a7217ac..b390cd1 100644 --- a/Poweroff_daemon/serial.h +++ b/Poweroff_daemon/serial.h @@ -23,6 +23,8 @@ #define CMD_GETVOLTAGE "V\n" #define ANS_V12 "V12" +#define LOG_V12 1150 +#define LOG_VOLTAGE_DT 60 #define LOWEST_V12 970 #define MAX_WERR_CNT 5 diff --git a/Poweroff_daemon/server.c b/Poweroff_daemon/server.c new file mode 100644 index 0000000..4fc254a --- /dev/null +++ b/Poweroff_daemon/server.c @@ -0,0 +1,62 @@ +/* + * This file is part of the poweroff_daemon project. + * Copyright 2026 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "serial.h" +#include "server.h" + +extern bool isrunning; +static sl_sock_t *s = NULL; + +static sl_sock_hresult_e defhandler(struct sl_sock *s, const char *str){ + if(!s || !str) return RESULT_FAIL; + char buf[BUFSIZ]; + if(!send_custom_command(str, buf, BUFSIZ)) return false; + LOGDBG("Send command '%s', answer:\n%s\n", str, buf); + sl_sock_sendstrmessage(s, buf); + return RESULT_SILENCE; +} + +static sl_sock_hresult_e helphandler(struct sl_sock *s, sl_sock_hitem_t _U_ *item, const char _U_ *req){ + if(!s) return RESULT_FAIL; + char buf[BUFSIZ]; + if(!send_custom_command("help", buf, BUFSIZ)) return false; + LOGDBG("Send command 'help', answer:\n%s\n", buf); + sl_sock_sendstrmessage(s, buf); + return RESULT_SILENCE; +} + +static sl_sock_hitem_t handlers[] = { + {sl_comment, NULL, " Enter any message for USB", NULL}, + {helphandler, "_help", "Send 'help' to serial", NULL}, + {NULL, NULL, NULL, NULL} +}; + +bool run_server(const char *sockpath){ + if(!sockpath) return false; + s = sl_sock_run_server(SOCKT_UNIX, sockpath, -1, handlers); + if(!s) return false; + sl_sock_defmsghandler(s, defhandler); + return true; +} + +void kill_server(){ + if(!s) return; + sl_sock_delete(&s); +} diff --git a/Poweroff_daemon/server.h b/Poweroff_daemon/server.h new file mode 100644 index 0000000..396f37b --- /dev/null +++ b/Poweroff_daemon/server.h @@ -0,0 +1,22 @@ +/* + * This file is part of the poweroff_daemon project. + * Copyright 2026 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +bool run_server(const char *sockpath); +void kill_server();