some fixes with dictionary; add aliases

This commit is contained in:
2025-07-17 17:06:13 +03:00
parent 0074eac5d8
commit 5f4e06bb7b
10 changed files with 685 additions and 276 deletions

View File

@@ -20,6 +20,7 @@
#include <stdio.h>
#include <usefull_macros.h>
#include "dictionary.h"
#include "modbus.h"
#include "server.h"
#include "verbose.h"
@@ -29,7 +30,7 @@ typedef struct{
int verbose; // verbose level (not used yet)
int slave; // slave ID
int isunix; // Unix-socket
char **dumpcodes; // keycodes to dump into file
char **read_keycodes; // keycodes to dump into file
char **writeregs; // reg=val to write
char **writecodes; // keycode=val to write
int **readregs; // regs to write
@@ -37,6 +38,7 @@ typedef struct{
char *dumpfile; // dump file name
char *outdic; // output dictionary to save everything read from slave
char *dicfile; // file with dictionary
char *aliasesfile; // file with aliases
char *device; // serial device
char *node; // server port or path
int baudrate; // baudrate
@@ -54,7 +56,7 @@ static sl_option_t cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), "show this help"},
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose), "verbose level (each -v adds 1)"},
{"outfile", NEED_ARG, NULL, 'o', arg_string, APTR(&G.dumpfile), "file with parameter's dump"},
{"dumpkey", MULT_PAR, NULL, 'k', arg_string, APTR(&G.dumpcodes), "dump entry with this keycode; multiply parameter"},
{"dumpkey", MULT_PAR, NULL, 'k', arg_string, APTR(&G.read_keycodes), "dump entry with this keycode; multiply parameter"},
{"dumptime", NEED_ARG, NULL, 't', arg_double, APTR(&G.dTdump), "dumping time interval (seconds, default: 0.1)"},
{"dictionary", NEED_ARG, NULL, 'D', arg_string, APTR(&G.dicfile), "file with dictionary (format: code register value writeable)"},
{"slave", NEED_ARG, NULL, 's', arg_int, APTR(&G.slave), "slave ID (default: 1)"},
@@ -67,12 +69,15 @@ static sl_option_t cmdlnopts[] = {
{"readc", MULT_PAR, NULL, 'R', arg_string, APTR(&G.readcodes), "registers (by keycodes, checked by dictionary) to read; multiply parameter"},
{"node", NEED_ARG, NULL, 'N', arg_string, APTR(&G.node), "node \"IP\", or path (could be \"\\0path\" for anonymous UNIX-socket)"},
{"unixsock", NO_ARGS, NULL, 'U', arg_int, APTR(&G.isunix), "UNIX socket instead of INET"},
{"alias", NEED_ARG, NULL, 'a', arg_string, APTR(&G.aliasesfile),"file with aliases in format 'name : command to run'"},
end_option
};
void signals(int sig){
if(sig > 0) WARNX("Exig with signal %d", sig);
close_modbus();
closedict();
closealiases();
exit(sig);
}
@@ -83,9 +88,12 @@ int main(int argc, char **argv){
sl_loglevel_e lvl = G.verbose + LOGLEVEL_ERR;
set_verbose_level(lvl);
if(lvl >= LOGLEVEL_AMOUNT) lvl = LOGLEVEL_AMOUNT - 1;
if(!G.dicfile) ERRX("Point filename of dictionary");
if(!opendict(G.dicfile)) signals(-1);
if(G.dumpcodes && !setdumppars(G.dumpcodes)) signals(-1);
if(!G.dicfile) WARNX("Dictionary is absent");
else if(!opendict(G.dicfile)) signals(-1);
if(G.aliasesfile){
if(!openaliases(G.aliasesfile)) WARNX("No aliases found in '%s'", G.aliasesfile);
}
if(G.read_keycodes && !setdumppars(G.read_keycodes)) signals(-1);
if(G.dumpfile && !opendumpfile(G.dumpfile)) signals(-1);
if(!open_modbus(G.device, G.baudrate)) signals(-1);
if(!set_slave(G.slave)) signals(-1);
@@ -108,13 +116,13 @@ int main(int argc, char **argv){
}
if(G.outdic){
DBG("outdic");
int N = dumpall(G.outdic);
int N = read_dict_entries(G.outdic);
if(N < 1) WARNX("Dump full dictionary failed");
else green("Read %N registers, dump to %s\n", N, G.outdic);
fflush(stdout);
}
if(G.readregs) dumpregs(G.readregs);
if(G.readcodes) dumpcodes(G.readcodes);
if(G.readregs) read_registers(G.readregs);
if(G.readcodes) read_keycodes(G.readcodes);
if(G.dumpfile){
if(!setDumpT(G.dTdump)) ERRX("Can't set dumptime %g", G.dTdump);
DBG("dumpfile");