mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-11-22 13:50:17 +03:00
alpha pre-release
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
Simple daemon checking voltage & button0 state of my 'fan controller'.
|
Simple daemon checking voltage & button0 state of my 'fan controller'.
|
||||||
|
|
||||||
Under development, wait pre-release...
|
Alpha-version, there may be some problems...
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ static cmdln_t G = {
|
|||||||
.poll_interval = DEFAULT_INTERVAL,
|
.poll_interval = DEFAULT_INTERVAL,
|
||||||
.timeout_us = READ_TIMEOUT_US,
|
.timeout_us = READ_TIMEOUT_US,
|
||||||
.baudrate = DEFAULT_BAUDRATE,
|
.baudrate = DEFAULT_BAUDRATE,
|
||||||
|
.sockpath = DEFAULT_SOCKPATH,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int help = 0;
|
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)},
|
{"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"},
|
{"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)},
|
{"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)},
|
{"baudrate",NEED_ARG, NULL, 'b', arg_int, &G.baudrate, "serial baudrate, default: " STR(DEFAULT_BAUDRATE)},
|
||||||
{"help", NO_ARGS, &help, 'h', arg_none, NULL, "show this help"},
|
{"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
|
end_option
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define DEFAULT_PIDFILE "/tmp/powerdaemon.pid"
|
#define DEFAULT_PIDFILE "/tmp/powerdaemon.pid"
|
||||||
|
#define DEFAULT_SOCKPATH "@poweroffdaemon"
|
||||||
#define DEFAULT_DEV "/dev/ttyUSB0"
|
#define DEFAULT_DEV "/dev/ttyUSB0"
|
||||||
#define DEFAULT_BAUDRATE 115200
|
#define DEFAULT_BAUDRATE 115200
|
||||||
#define DEFAULT_INTERVAL 1.0
|
#define DEFAULT_INTERVAL 1.0
|
||||||
@@ -28,6 +29,7 @@ typedef struct{
|
|||||||
char *pidfile;
|
char *pidfile;
|
||||||
char *logfile;
|
char *logfile;
|
||||||
char *serialdev;
|
char *serialdev;
|
||||||
|
char *sockpath;
|
||||||
double poll_interval;
|
double poll_interval;
|
||||||
double timeout_us;
|
double timeout_us;
|
||||||
int verbose;
|
int verbose;
|
||||||
|
|||||||
@@ -23,9 +23,10 @@
|
|||||||
|
|
||||||
#include "cmdlnopts.h"
|
#include "cmdlnopts.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
|
#include "server.h"
|
||||||
|
|
||||||
static cmdln_t *G = NULL;
|
static cmdln_t *G = NULL;
|
||||||
static bool isrunning = false;
|
bool isrunning = false;
|
||||||
static pid_t childpid = 0;
|
static pid_t childpid = 0;
|
||||||
|
|
||||||
void signals(int sig){
|
void signals(int sig){
|
||||||
@@ -62,10 +63,9 @@ int main(int argc, char **argv){
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Daemonize */
|
/* Daemonize */
|
||||||
/* if (sl_daemonize() != 0) {
|
if(sl_daemonize() != 0){
|
||||||
ERR("daemonize failed");
|
ERR("daemonize failed");
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
/* Set global timeout for sl_tty_read() */
|
/* Set global timeout for sl_tty_read() */
|
||||||
sl_tty_tmout(G->timeout_us);
|
sl_tty_tmout(G->timeout_us);
|
||||||
@@ -94,7 +94,7 @@ int main(int argc, char **argv){
|
|||||||
}
|
}
|
||||||
lastd = t;
|
lastd = t;
|
||||||
WARNX("Child %d died\n", childpid);
|
WARNX("Child %d died\n", childpid);
|
||||||
sleep(1);
|
sleep(5);
|
||||||
}else{
|
}else{
|
||||||
prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies
|
prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies
|
||||||
break; // go out to normal functional
|
break; // go out to normal functional
|
||||||
@@ -110,24 +110,33 @@ int main(int argc, char **argv){
|
|||||||
|
|
||||||
isrunning = true;
|
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){
|
while(isrunning){
|
||||||
bool trigger = false;
|
bool trigger = false;
|
||||||
if(check_buttons()) trigger = true;
|
if(check_buttons()) trigger = true;
|
||||||
if(check_voltage()) trigger = true;
|
if(check_voltage()) trigger = true;
|
||||||
|
|
||||||
if(trigger){
|
if(trigger){
|
||||||
LOGERR("Trigger condition met, executing poweroff");
|
LOGWARN("Trigger condition met, executing poweroff");
|
||||||
DBG("poweroff");
|
DBG("poweroff");
|
||||||
/* int ret = system("poweroff");
|
int ret = system("/sbin/poweroff");
|
||||||
if (ret == -1) {
|
if(ret == -1){
|
||||||
LOGERR("Failed to execute poweroff: %s", strerror(errno));
|
LOGERR("Failed to execute poweroff: %s", strerror(errno));
|
||||||
|
}else{
|
||||||
|
LOGWARN("Poweroff called");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
*/ }
|
|
||||||
|
|
||||||
usleep((useconds_t)(G->poll_interval * 1e6)); // seconds -> useconds
|
usleep((useconds_t)(G->poll_interval * 1e6)); // seconds -> useconds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kill_server();
|
||||||
|
|
||||||
close_serial();
|
close_serial();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
-std=c17
|
-std=c23
|
||||||
@@ -3,3 +3,5 @@ cmdlnopts.h
|
|||||||
main.c
|
main.c
|
||||||
serial.c
|
serial.c
|
||||||
serial.h
|
serial.h
|
||||||
|
server.c
|
||||||
|
server.h
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
.
|
||||||
|
|||||||
@@ -119,10 +119,20 @@ bool check_buttons(){
|
|||||||
/* Check voltage: returns 1 if V12 < 9700 */
|
/* Check voltage: returns 1 if V12 < 9700 */
|
||||||
bool check_voltage(){
|
bool check_voltage(){
|
||||||
int voltage = 0;
|
int voltage = 0;
|
||||||
|
static double tlog = -1.;
|
||||||
if(!get_int(CMD_GETVOLTAGE, ANS_V12, &voltage)) return false;
|
if(!get_int(CMD_GETVOLTAGE, ANS_V12, &voltage)) return false;
|
||||||
if(voltage < LOWEST_V12){
|
if(voltage < LOWEST_V12){
|
||||||
LOGERR("V12 low: %d (%.2f V)", voltage, (double)voltage / 100.);
|
LOGERR("V12 low: %d (%.2f V)", voltage, (double)voltage / 100.);
|
||||||
return true;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -130,11 +140,31 @@ bool check_voltage(){
|
|||||||
bool send_custom_command(const char *command, char *answer, size_t anslen){
|
bool send_custom_command(const char *command, char *answer, size_t anslen){
|
||||||
if(!tty || !command) return false;
|
if(!tty || !command) return false;
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
size_t L = anslen;
|
||||||
pthread_mutex_lock(&serial_mutex);
|
pthread_mutex_lock(&serial_mutex);
|
||||||
if(sl_tty_write(tty->comfd, command, strlen(command)) && sl_tty_read(tty) > 0){
|
DBG("Send command '%s'", command);
|
||||||
if(answer && anslen > 1) strncpy(answer, tty->buf, anslen-1);
|
if(0 == sl_tty_write(tty->comfd, command, strlen(command))){
|
||||||
ret = true;
|
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);
|
pthread_mutex_unlock(&serial_mutex);
|
||||||
|
if(L > 0){
|
||||||
|
if(anslen < 1) answer[-1] = 0;
|
||||||
|
else *answer = 0;
|
||||||
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
#define CMD_GETVOLTAGE "V\n"
|
#define CMD_GETVOLTAGE "V\n"
|
||||||
#define ANS_V12 "V12"
|
#define ANS_V12 "V12"
|
||||||
|
#define LOG_V12 1150
|
||||||
|
#define LOG_VOLTAGE_DT 60
|
||||||
#define LOWEST_V12 970
|
#define LOWEST_V12 970
|
||||||
|
|
||||||
#define MAX_WERR_CNT 5
|
#define MAX_WERR_CNT 5
|
||||||
|
|||||||
62
Poweroff_daemon/server.c
Normal file
62
Poweroff_daemon/server.c
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the poweroff_daemon project.
|
||||||
|
* Copyright 2026 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <usefull_macros.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
22
Poweroff_daemon/server.h
Normal file
22
Poweroff_daemon/server.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the poweroff_daemon project.
|
||||||
|
* Copyright 2026 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
bool run_server(const char *sockpath);
|
||||||
|
void kill_server();
|
||||||
Reference in New Issue
Block a user