mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-11-22 13:50:17 +03:00
some fixes; added operations with coils and inputs
This commit is contained in:
3
modbus_params/FX3U.aliases
Normal file
3
modbus_params/FX3U.aliases
Normal file
@@ -0,0 +1,3 @@
|
||||
ledon : led = 1
|
||||
ledoff : led = 0
|
||||
recours : recours
|
||||
6
modbus_params/FX3U.dic
Normal file
6
modbus_params/FX3U.dic
Normal file
@@ -0,0 +1,6 @@
|
||||
# name register default readonly
|
||||
reset 0 0 0
|
||||
time 1 0 0
|
||||
led 2 0 0
|
||||
inputs 3 0 1
|
||||
outputs 4 0 1
|
||||
@@ -12,9 +12,11 @@ The project is hosted at:
|
||||
|
||||
- Read and write Modbus holding registers by **address** or **mnemonic code**.
|
||||
- Define a dictionary (`code → register`) with optional comments and read‑only flags.
|
||||
- Read discrete inputs and coils (all at once or one bit), write coils (all at once or one bit).
|
||||
- Dump selected registers periodically to a TSV file.
|
||||
- Start a **TCP or UNIX socket server** that accepts commands to:
|
||||
- Read/write registers (by code or address).
|
||||
- Read discrete inputs / coils; write coils.
|
||||
- List dictionary entries or aliases.
|
||||
- Start/stop and rename the dump file.
|
||||
- Define **aliases** – macros that expand to other commands or expressions.
|
||||
@@ -77,6 +79,13 @@ modbus_params [options]
|
||||
| `-W`, `--writec` *code=val* | Write value to register by dictionary code (multiply) |
|
||||
| `-O`, `--outdic` *file* | Read **all** registers listed in the dictionary and save them (with current values) into a new dictionary file |
|
||||
|
||||
### Discrete I/O sizes
|
||||
|
||||
| Option | Argument | Description |
|
||||
|--------|-------------------|-------------|
|
||||
| `-i`, `--inputs` | N | Number of bytes for discrete inputs (default: 1) |
|
||||
| `-c`, `--coils` | N | Number of bytes for coils/outputs (default: 1) |
|
||||
|
||||
### Dumping (periodic TSV output)
|
||||
|
||||
| Option | Description |
|
||||
@@ -176,6 +185,9 @@ Each command is a key‑value pair: `key[=value]`.
|
||||
| `alias` | Getter: prints all aliases. Setter (e.g. `alias=myalias`): prints that specific alias. |
|
||||
| `newdump` | Getter: returns current dump file name. Setter (e.g. `newdump=/path/file.dump`): closes current dump file (if any), opens a new one and starts dumping. |
|
||||
| `clodump` | Stops the dump thread and closes the dump file. No value expected. |
|
||||
| `readcoils` | Getter: read **all** coils as HEX (uses `--coils` bytes, e.g. `readcoils = 0x00FF`). With `[N]` (e.g. `readcoils[0]`): read a **single** coil (returns 0 or 1). |
|
||||
| `readinputs` | Getter: read **all** discrete inputs as HEX (uses --inputs bytes). With [N]: read a **single** discrete input. |
|
||||
| `writecoils` | Setter: write **all** coils from a number (e.g. `writecoils = 0` to clear all, `writecoils = 0x0F` to set several). With `[N]` (e.g. `writecoils[4] = 1`): write a **single** coil. |
|
||||
|
||||
### Default handler (register access)
|
||||
|
||||
@@ -187,8 +199,8 @@ If the key is **not** a built‑in handler, the program tries to interpret it as
|
||||
|
||||
Then:
|
||||
|
||||
- **Getter** (only key) → reads the register and replies `key=value`.
|
||||
- **Setter** (`key=value`) → writes the value to the register (if not read‑only) and replies `OK`.
|
||||
- **Getter** (only key) → reads the register and replies `key = value`.
|
||||
- **Setter** (`key = value`) → writes the value to the register (if not read‑only) and replies `OK`.
|
||||
|
||||
### Examples (using `netcat`, `telnet` or `socat`)
|
||||
|
||||
@@ -216,6 +228,33 @@ F00.11
|
||||
F00.11=5000
|
||||
```
|
||||
|
||||
**Discrete I/O** (assuming `-c 2 -i 1`):
|
||||
|
||||
```
|
||||
readcoils
|
||||
readcoils = 0x0000
|
||||
writecoils = 0x000F
|
||||
OK
|
||||
readcoils
|
||||
readcoils = 0x000F
|
||||
readcoils[0]
|
||||
readcoils[0] = 1
|
||||
readcoils[1]
|
||||
readcoils[1] = 1
|
||||
writecoils[4] = 1
|
||||
OK
|
||||
readcoils[4]
|
||||
readcoils[4] = 1
|
||||
writecoils = 0
|
||||
OK
|
||||
readcoils
|
||||
readcoils = 0x0000
|
||||
readinputs
|
||||
readinputs = 0x05
|
||||
readinputs[0]
|
||||
readinputs[0] = 1
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### 1. Simple read by address
|
||||
@@ -252,6 +291,14 @@ Now you can connect to port 5020 and use `newdump` to change the output file, or
|
||||
modbus_params -D dictionary.dic -O current_state.dic
|
||||
```
|
||||
|
||||
### 6. Server with discrete I/O
|
||||
|
||||
```bash
|
||||
modbus_params -D dictionary.dic -c 2 -i 1 -N :5020
|
||||
```
|
||||
|
||||
Now readcoils/writecoils operate on 16 coils, readinputs on 8 inputs.
|
||||
|
||||
## Signals
|
||||
|
||||
- `SIGINT` (Ctrl+C), `SIGTERM`, `SIGQUIT` – gracefully close Modbus, dump files, and exit.
|
||||
|
||||
@@ -93,6 +93,8 @@ static char *getcomment(char *str){
|
||||
// dictionary format: "'code' 'register' 'value' 'readonly flag'\n", e.g.
|
||||
// "F00.09 61444 5000 1"
|
||||
int opendict(const char *dic){
|
||||
if(!dic) return FALSE;
|
||||
DBG("Open %s", dic);
|
||||
closedict(); // close early opened dictionary to prevent problems
|
||||
FILE *f = fopen(dic, "r");
|
||||
if(!f){
|
||||
@@ -106,28 +108,33 @@ int opendict(const char *dic){
|
||||
curentry.code = MALLOC(char, BUFSIZ);
|
||||
// `help` field of `curentry` isn't used here
|
||||
int retcode = TRUE;
|
||||
char formatbuf[64];
|
||||
snprintf(formatbuf, 64, "%%%ds %%" SCNu16 " %%" SCNu16 " %%" SCNu8, BUFSIZ-1);
|
||||
while(1){
|
||||
if(getline(&line, &linesz, f) < 0) break;
|
||||
// DBG("Original LINE: '%s'", line);
|
||||
//DBG("Original LINE: '%s'", line);
|
||||
char *comment = getcomment(line);
|
||||
char *newline = strchr(line, '\n');
|
||||
if(newline) *newline = 0;
|
||||
// DBG("LINE: '%s'", line);
|
||||
//DBG("LINE: '%s'", line);
|
||||
if(*line == 0) continue;
|
||||
if(4 != sscanf(line, "%s %" SCNu16 " %" SCNu16 " %" SCNu8, curentry.code, &curentry.reg, &curentry.value, &curentry.readonly)){
|
||||
|
||||
if(4 != sscanf(line, formatbuf, curentry.code, &curentry.reg, &curentry.value, &curentry.readonly)){
|
||||
WARNX("Can't understand this line: '%s'", line);
|
||||
continue;
|
||||
}
|
||||
// DBG("Got line: '%s %" PRIu16 " %" PRIu16 " %" PRIu8, curentry.code, curentry.reg, curentry.value, curentry.readonly);
|
||||
DBG("Got line: '%s %" PRIu16 " %" PRIu16 " %" PRIu8, curentry.code, curentry.reg, curentry.value, curentry.readonly);
|
||||
if(++dictsize >= dicsz){
|
||||
dicsz += 50;
|
||||
dicentry_t *newdic = realloc(dictionary, sizeof(dicentry_t) * dicsz);
|
||||
size_t newsize = dicsz + 50;
|
||||
dicentry_t *newdic = realloc(dictionary, sizeof(dicentry_t) * newsize);
|
||||
if(!newdic){
|
||||
WARN("Can't allocate memory for dictionary");
|
||||
retcode = FALSE;
|
||||
--dictsize;
|
||||
goto ret;
|
||||
}
|
||||
dictionary = newdic;
|
||||
dicsz = newsize;
|
||||
}
|
||||
dicentry_t *entry = &dictionary[dictsize-1];
|
||||
entry->code = strdup(curentry.code);
|
||||
@@ -223,17 +230,21 @@ int setdumppars(char **pars){
|
||||
if(!e){
|
||||
WARNX("Can't find entry with code %s", *p);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
dumpsize = 0;
|
||||
return FALSE;
|
||||
}
|
||||
DBG("found entry do dump, reg=%d", e->reg);
|
||||
if(cursz <= N){
|
||||
cursz += 50;
|
||||
int newsz = cursz + 50;
|
||||
DBG("realloc list to %d", cursz);
|
||||
dicentry_t *newpars = realloc(dumppars, sizeof(dicentry_t) * (cursz));
|
||||
dicentry_t *newpars = realloc(dumppars, sizeof(dicentry_t) * newsz);
|
||||
if(!newpars){
|
||||
WARN("realloc()"); cursz -= 50;
|
||||
WARN("realloc()");
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return FALSE;
|
||||
}else{
|
||||
dumppars = newpars;
|
||||
cursz = newsz;
|
||||
DBG("zero mem");
|
||||
bzero(&dumppars[N], sizeof(dicentry_t)*(cursz-N));
|
||||
}
|
||||
@@ -278,12 +289,13 @@ char *getdumpname(){ return dumpname;}
|
||||
|
||||
void closedumpfile(){
|
||||
int stpd = atomic_load(&isstopped);
|
||||
if(dumpfile && !stpd){
|
||||
if(dumpfile){
|
||||
if(!stpd){
|
||||
atomic_store(&stopdump, 1);
|
||||
while(!atomic_load(&isstopped));
|
||||
}
|
||||
fclose(dumpfile);
|
||||
dumpfile = NULL;
|
||||
FREE(dumpname);
|
||||
}
|
||||
}
|
||||
@@ -294,7 +306,7 @@ static void *dumpthread(void *p){
|
||||
double dT = *(double*)p;
|
||||
DBG("Dump thread started. Period: %gs", dT);
|
||||
double startT = sl_dtime();
|
||||
while(!atomic_load(&isstopped)){
|
||||
while(!atomic_load(&stopdump)){
|
||||
double t0 = sl_dtime();
|
||||
fprintf(dumpfile, "%10.3f ", t0 - startT);
|
||||
for(int i = 0; i < dumpsize; ++i){
|
||||
@@ -344,13 +356,15 @@ int rundump(){
|
||||
char *dicentry_descr(dicentry_t *entry, char *buf, size_t bufsize){
|
||||
//DBG("descr of %s", entry->code);
|
||||
if(!entry || !buf || !bufsize) return NULL;
|
||||
size_t L;
|
||||
if(entry->help){
|
||||
snprintf(buf, bufsize-1, "%s %4" PRIu16 " %4" PRIu16 " %" PRIu8 " # %s\n",
|
||||
L = snprintf(buf, bufsize, "%s %4" PRIu16 " %4" PRIu16 " %" PRIu8 " # %s\n",
|
||||
entry->code, entry->reg, entry->value, entry->readonly, entry->help);
|
||||
}else{
|
||||
snprintf(buf, bufsize-1, "%s %4" PRIu16 " %4" PRIu16 " %" PRIu8 "\n",
|
||||
L = snprintf(buf, bufsize, "%s %4" PRIu16 " %4" PRIu16 " %" PRIu8 "\n",
|
||||
entry->code, entry->reg, entry->value, entry->readonly);
|
||||
}
|
||||
if(L >= bufsize) buf[bufsize-1] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -439,14 +453,16 @@ int openaliases(const char *filename){
|
||||
continue;
|
||||
}
|
||||
if(++aliasessize >= asz){
|
||||
asz += 50;
|
||||
alias_t *newaliases = realloc(aliases, sizeof(alias_t) * asz);
|
||||
size_t newsize = asz + 50;
|
||||
alias_t *newaliases = realloc(aliases, sizeof(alias_t) * newsize);
|
||||
if(!newaliases){
|
||||
WARNX("Can't allocate memory for aliases list");
|
||||
retcode = FALSE;
|
||||
--aliasessize;
|
||||
goto ret;
|
||||
}
|
||||
aliases = newaliases;
|
||||
asz = newsize;
|
||||
}
|
||||
alias_t *cur = &aliases[aliasessize - 1];
|
||||
cur->name = strdup(name);
|
||||
|
||||
@@ -30,11 +30,13 @@ typedef struct{
|
||||
int verbose; // verbose level (not used yet)
|
||||
int slave; // slave ID
|
||||
int isunix; // Unix-socket
|
||||
int inputs; // number of bytes for discrete inputs
|
||||
int coils; // number of bytes for coils
|
||||
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
|
||||
char **readcodes; // keycodes to write
|
||||
int **readregs; // regs to read
|
||||
char **readcodes; // keycodes to read
|
||||
char *dumpfile; // dump file name
|
||||
char *outdic; // output dictionary to save everything read from slave
|
||||
char *dicfile; // file with dictionary
|
||||
@@ -50,6 +52,8 @@ static parameters G = {
|
||||
.device = "/dev/ttyUSB0",
|
||||
.baudrate = 9600,
|
||||
.dTdump = 0.1,
|
||||
.inputs = 1,
|
||||
.coils = 1,
|
||||
};
|
||||
|
||||
static sl_option_t cmdlnopts[] = {
|
||||
@@ -70,6 +74,8 @@ static sl_option_t cmdlnopts[] = {
|
||||
{"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'"},
|
||||
{"inputs", NEED_ARG, NULL, 'i', arg_int, APTR(&G.inputs), "number of bytes for discrete inputs"},
|
||||
{"coils", NEED_ARG, NULL, 'c', arg_int, APTR(&G.coils), "number of bytes for coils (outputs)"},
|
||||
end_option
|
||||
};
|
||||
|
||||
@@ -90,10 +96,12 @@ int main(int argc, char **argv){
|
||||
if(lvl >= LOGLEVEL_AMOUNT) lvl = LOGLEVEL_AMOUNT - 1;
|
||||
if(!G.dicfile) WARNX("Dictionary is absent");
|
||||
else if(!opendict(G.dicfile)) signals(-1);
|
||||
DBG("dictionary %s opened", G.dicfile);
|
||||
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) DBG("Dump file: %s", G.dumpfile);
|
||||
if(G.dumpfile && !opendumpfile(G.dumpfile)) signals(-1);
|
||||
if(!open_modbus(G.device, G.baudrate)) signals(-1);
|
||||
if(!set_slave(G.slave)) signals(-1);
|
||||
@@ -118,7 +126,7 @@ int main(int argc, char **argv){
|
||||
DBG("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);
|
||||
else green("Read %d registers, dump to %s\n", N, G.outdic);
|
||||
fflush(stdout);
|
||||
}
|
||||
if(G.readregs) read_registers(G.readregs);
|
||||
@@ -129,12 +137,13 @@ int main(int argc, char **argv){
|
||||
if(!rundump()) signals(-1);
|
||||
}
|
||||
if(G.node){
|
||||
set_io_sizes(G.inputs, G.coils);
|
||||
DBG("Create server");
|
||||
if(!runserver(G.node, G.isunix)) signals(-1); // this function exits only after server death
|
||||
}
|
||||
if(G.dumpfile){
|
||||
DBG("Done, wait for ctrl+C");
|
||||
while(1);
|
||||
while(1) usleep(100000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -26,26 +26,34 @@
|
||||
#include "modbus.h"
|
||||
#include "verbose.h"
|
||||
|
||||
#define MAX_READ_BITS 64
|
||||
#define MAX_WRITE_BITS 64
|
||||
|
||||
static modbus_t *modbus_ctx = NULL;
|
||||
static pthread_mutex_t modbus_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
void close_modbus(){
|
||||
closedumpfile();
|
||||
if(modbus_ctx){
|
||||
closedumpfile();
|
||||
pthread_mutex_lock(&modbus_mutex);
|
||||
modbus_close(modbus_ctx);
|
||||
modbus_free(modbus_ctx);
|
||||
modbus_ctx = NULL;
|
||||
pthread_mutex_unlock(&modbus_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
// read register and modify entry->reg; return FALSE if failed
|
||||
int read_entry(dicentry_t *entry){
|
||||
if(!modbus_ctx) return FALSE;
|
||||
if(!entry){
|
||||
WARNX("NULL instead of entry");
|
||||
return FALSE;
|
||||
}
|
||||
int ret = TRUE;
|
||||
pthread_mutex_lock(&modbus_mutex);
|
||||
if(modbus_read_registers(modbus_ctx, entry->reg, 1, &entry->value) < 0){
|
||||
if(modbus_read_registers(modbus_ctx, entry->reg, 1, &entry->value) < 0 &&
|
||||
modbus_read_input_registers(modbus_ctx, entry->reg, 1, &entry->value) < 0){
|
||||
WARNX("Can't read entry by reg %u", entry->reg);
|
||||
ret = FALSE;
|
||||
}
|
||||
@@ -54,6 +62,7 @@ int read_entry(dicentry_t *entry){
|
||||
}
|
||||
// write register value; FALSE - if failed or read-only
|
||||
int write_entry(dicentry_t *entry){
|
||||
if(!modbus_ctx) return FALSE;
|
||||
if(!entry || entry->readonly){
|
||||
if(!entry) WARNX("NULL instead of entry");
|
||||
else WARNX("Can't write readonly entry %u", entry->reg);
|
||||
@@ -105,7 +114,7 @@ int write_codeval(char **codeval){
|
||||
while(*codeval){
|
||||
char key[SL_KEY_LEN], value[SL_VAL_LEN];
|
||||
if(2 != sl_get_keyval(*codeval, key, value)){
|
||||
WARNX("%s isn't in format keycode=val");
|
||||
WARNX("%s isn't in format keycode=val", *codeval);
|
||||
}else{
|
||||
int v=-1;
|
||||
if(!sl_str2i(&v, value) || v < 0 || v > UINT16_MAX){
|
||||
@@ -158,6 +167,7 @@ rtn:
|
||||
|
||||
int set_slave(int ID){
|
||||
int ret = TRUE;
|
||||
if(!modbus_ctx) return FALSE;
|
||||
pthread_mutex_lock(&modbus_mutex);
|
||||
if(modbus_set_slave(modbus_ctx, ID)){
|
||||
WARNX("Can't set slave ID to %d", ID);
|
||||
@@ -202,3 +212,64 @@ void read_keycodes(char **keycodes){
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int read_coils(int nbytes, uint8_t *dest){
|
||||
if(!modbus_ctx || !dest || nbytes < 1 || nbytes > MAX_READ_BITS / 8){
|
||||
WARNX("Bad coils read request");
|
||||
return FALSE;
|
||||
}
|
||||
int ret = TRUE;
|
||||
DBG("Try to read %d bits", nbytes * 8);
|
||||
pthread_mutex_lock(&modbus_mutex);
|
||||
if(modbus_read_bits(modbus_ctx, 0, nbytes * 8, dest) < 0){
|
||||
WARNX("Can't read coils");
|
||||
ret = FALSE;
|
||||
}
|
||||
pthread_mutex_unlock(&modbus_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int read_inputs(int nbytes, uint8_t *dest){
|
||||
if(!modbus_ctx || !dest || nbytes < 1 || nbytes > MAX_READ_BITS / 8){
|
||||
WARNX("Bad inputs read request");
|
||||
return FALSE;
|
||||
}
|
||||
int ret = TRUE;
|
||||
pthread_mutex_lock(&modbus_mutex);
|
||||
if(modbus_read_input_bits(modbus_ctx, 0, nbytes * 8, dest) < 0){
|
||||
WARNX("Can't read discrete inputs");
|
||||
ret = FALSE;
|
||||
}
|
||||
pthread_mutex_unlock(&modbus_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int write_coils(int nbytes, const uint8_t *src){
|
||||
if(!modbus_ctx || !src || nbytes < 1 || nbytes > MAX_WRITE_BITS / 8){
|
||||
WARNX("Bad coils write request");
|
||||
return FALSE;
|
||||
}
|
||||
int ret = TRUE;
|
||||
pthread_mutex_lock(&modbus_mutex);
|
||||
if(modbus_write_bits(modbus_ctx, 0, nbytes * 8, src) < 0){
|
||||
WARNX("Can't write coils");
|
||||
ret = FALSE;
|
||||
}
|
||||
pthread_mutex_unlock(&modbus_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int write_coil(int index, int value){
|
||||
if(!modbus_ctx || index < 0 || index > MAX_WRITE_BITS - 1){
|
||||
WARNX("Bad coil write request");
|
||||
return FALSE;
|
||||
}
|
||||
int ret = TRUE;
|
||||
pthread_mutex_lock(&modbus_mutex);
|
||||
if(modbus_write_bit(modbus_ctx, index, value) < 0){
|
||||
ret = FALSE;
|
||||
WARNX("Can't set coil %d to %d", index, value);
|
||||
}
|
||||
pthread_mutex_unlock(&modbus_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ int open_modbus(const char *path, int baudrate);
|
||||
void close_modbus();
|
||||
int set_slave(int ID);
|
||||
|
||||
int read_coils(int nbytes, uint8_t *dest);
|
||||
int read_inputs(int nbytes, uint8_t *dest);
|
||||
int write_coils(int nbytes, const uint8_t *src);
|
||||
int write_coil(int index, int value);
|
||||
|
||||
int read_entry(dicentry_t *entry);
|
||||
int write_entry(dicentry_t *entry);
|
||||
int write_regval(char **regval);
|
||||
|
||||
@@ -16,7 +16,10 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dictionary.h"
|
||||
@@ -24,6 +27,42 @@
|
||||
#include "server.h"
|
||||
|
||||
static sl_sock_t *s = NULL;
|
||||
// amount of input/output bytes
|
||||
static int input_bytes = 0;
|
||||
static int coils_bytes = 0;
|
||||
|
||||
// specific bit to read/write
|
||||
static sl_sock_keyno_t rinputs = SL_SOCK_KEYNO_DEFAULT,
|
||||
rcoils = SL_SOCK_KEYNO_DEFAULT,
|
||||
wcoils = SL_SOCK_KEYNO_DEFAULT;
|
||||
|
||||
|
||||
void set_io_sizes(int inputs, int coils){
|
||||
input_bytes = (inputs > 0) ? inputs : 0;
|
||||
coils_bytes = (coils > 0) ? coils : 0;
|
||||
}
|
||||
|
||||
static int bytes_to_num(const uint8_t *buf, int n, uint64_t *out){
|
||||
if(!buf || !out || n < 1 || n > 64) return FALSE;
|
||||
uint64_t result = 0;
|
||||
for(int i = n - 1; i > -1; --i){
|
||||
result <<= 1;
|
||||
if(buf[i]) result |= 1;
|
||||
}
|
||||
*out = result;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static int num_to_bytes(uint64_t in, uint8_t *buf, int n){
|
||||
if(!buf || n < 1 || n > 64) return FALSE;
|
||||
uint64_t mask = 1;
|
||||
for(int i = 0; i < n; ++i){
|
||||
if(in & mask) buf[i] = 1;
|
||||
else buf[i] = 0;
|
||||
mask <<= 1;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// stop dump and close dump file
|
||||
static sl_sock_hresult_e closedump(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U_ const char *req){
|
||||
@@ -89,11 +128,94 @@ static sl_sock_hresult_e listaliases(sl_sock_t *client, _U_ sl_sock_hitem_t *ite
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
|
||||
static sl_sock_hresult_e readcoils(sl_sock_t *client, sl_sock_hitem_t *item, const char *req){
|
||||
if(req) return RESULT_BADVAL;
|
||||
if(coils_bytes < 1){
|
||||
WARNX("Set --coils first");
|
||||
return RESULT_FAIL;
|
||||
}
|
||||
int no = sl_sock_keyno_check((sl_sock_keyno_t*)item->data);
|
||||
if(no > coils_bytes*8 - 1) return RESULT_BADKEY;
|
||||
uint64_t bits;
|
||||
char buf[BUFSIZ];
|
||||
if(!read_coils(coils_bytes, (uint8_t*)buf)) return RESULT_FAIL;
|
||||
if(no > -1){ // read specific coil
|
||||
int state = buf[no];
|
||||
snprintf(buf, sizeof(buf), "%s[%d] = %d\n", item->key, no, state);
|
||||
}else{
|
||||
if(!bytes_to_num((uint8_t*)buf, coils_bytes*8, &bits)) return RESULT_FAIL;
|
||||
char format[32];
|
||||
snprintf(format, 32, "%s = 0x%%0%d" PRIx64 "\n", item->key, coils_bytes*2);
|
||||
snprintf(buf, sizeof(buf), format, bits);
|
||||
}
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
|
||||
static sl_sock_hresult_e readinputs(sl_sock_t *client, sl_sock_hitem_t *item, const char *req){
|
||||
if(req) return RESULT_BADVAL;
|
||||
if(input_bytes < 1){
|
||||
WARNX("Set --inputs first");
|
||||
return RESULT_FAIL;
|
||||
}
|
||||
int no = sl_sock_keyno_check((sl_sock_keyno_t*)item->data);
|
||||
if(no > input_bytes*8 - 1) return RESULT_BADKEY;
|
||||
uint64_t bits;
|
||||
char buf[BUFSIZ];
|
||||
if(!read_inputs(input_bytes, (uint8_t*)buf)) return RESULT_FAIL;
|
||||
if(no > -1){ // read specific input
|
||||
int state = buf[no];
|
||||
snprintf(buf, sizeof(buf), "%s[%d] = %d\n", item->key, no, state);
|
||||
}else{
|
||||
if(!bytes_to_num((uint8_t*)buf, input_bytes*8, &bits)) return RESULT_FAIL;
|
||||
char format[32];
|
||||
snprintf(format, 32, "%s = 0x%%0%d" PRIx64 "\n", item->key, input_bytes*2);
|
||||
snprintf(buf, sizeof(buf), format, bits);
|
||||
}
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
|
||||
static sl_sock_hresult_e writecoils(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item, const char *req){
|
||||
if(!req){
|
||||
WARNX("getter");
|
||||
return RESULT_BADVAL;
|
||||
}
|
||||
if(coils_bytes < 1){
|
||||
WARNX("Set --coils first");
|
||||
return RESULT_FAIL;
|
||||
}
|
||||
int no = sl_sock_keyno_check((sl_sock_keyno_t*)item->data);
|
||||
if(no > coils_bytes*8 - 1) return RESULT_BADKEY;
|
||||
uint64_t bits;
|
||||
long long ll;
|
||||
uint8_t buf[BUFSIZ];
|
||||
if(!sl_str2ll(&ll, req) || ll < 0){
|
||||
WARNX("bad integer '%s' (%lld)", req, ll);
|
||||
return RESULT_BADVAL;
|
||||
}
|
||||
bits = (uint64_t) ll;
|
||||
int ok = FALSE;
|
||||
if(no > -1){ // change state of specific coil
|
||||
ok = write_coil(no, bits);
|
||||
}else{
|
||||
if(!num_to_bytes(bits, buf, coils_bytes*8)){
|
||||
WARNX("Bad HEX string for writecoils");
|
||||
return RESULT_BADVAL;
|
||||
}
|
||||
ok = write_coils(coils_bytes, buf);
|
||||
}
|
||||
return ok ? RESULT_OK : RESULT_FAIL;
|
||||
}
|
||||
|
||||
static sl_sock_hitem_t handlers[] = {
|
||||
{closedump, "clodump", "stop dump and close current dump file", NULL},
|
||||
{newdump, "newdump", "open new dump file or get name of current", NULL},
|
||||
{listdict, "list", "list all dictionary (as getter) or given register (as setter: by codename or value)", NULL},
|
||||
{listaliases, "alias", "list all of aliases (as getter) or with given name (by setter)", NULL},
|
||||
{readcoils, "readcoils", "read all coils as HEX (uses --coils bytes)", (void*)&rcoils},
|
||||
{readinputs, "readinputs", "read all discrete inputs as HEX (uses --inputs bytes)", (void*)&rinputs},
|
||||
{writecoils, "writecoils", "write all coils from HEX number (uses --coils bytes)", (void*)&wcoils},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -112,6 +234,12 @@ static void disconnected(sl_sock_t *c){
|
||||
static sl_sock_hresult_e defhandler(sl_sock_t *s, const char *str){
|
||||
if(!s || !str) return RESULT_FAIL;
|
||||
char key[SL_KEY_LEN], value[SL_VAL_LEN];
|
||||
static int deepcounter = 0; // recourcive calls counter
|
||||
if(deepcounter > 5){
|
||||
WARNX("Too much recoursive calls");
|
||||
deepcounter = 0;
|
||||
return RESULT_FAIL;
|
||||
}
|
||||
int n = sl_get_keyval(str, key, value);
|
||||
if(n == 0){
|
||||
WARNX("Can't parse `%s` as reg[=val]", str);
|
||||
@@ -128,16 +256,19 @@ static sl_sock_hresult_e defhandler(sl_sock_t *s, const char *str){
|
||||
// check alias - should be non-setter!!!
|
||||
if(n == 1){
|
||||
alias_t *alias = find_alias(key);
|
||||
if(alias) // recoursive call of defhandler
|
||||
if(alias){ // recoursive call of defhandler
|
||||
++deepcounter;
|
||||
return defhandler(s, alias->expr);
|
||||
}
|
||||
}
|
||||
WARNX("Entry %s not found", key);
|
||||
return RESULT_BADKEY;
|
||||
}
|
||||
deepcounter = 0;
|
||||
if(n == 1){ // getter
|
||||
if(!read_entry(entry)) return RESULT_FAIL;
|
||||
snprintf(value, SL_VAL_LEN-1, "%s=%u\n", key, entry->value);
|
||||
}else{ // setter
|
||||
}else{ // setterrunserver
|
||||
if(!sl_str2i(&N, value)){
|
||||
WARNX("%s isn't a value of register", value);
|
||||
return RESULT_BADVAL;
|
||||
@@ -170,6 +301,7 @@ int runserver(const char *node, int isunix){
|
||||
WARNX("Server handlers thread is dead");
|
||||
break;
|
||||
}
|
||||
usleep(10000);
|
||||
}
|
||||
DBG("Close");
|
||||
sl_sock_delete(&s);
|
||||
|
||||
@@ -21,3 +21,4 @@
|
||||
#include <usefull_macros.h>
|
||||
|
||||
int runserver(const char *node, int isunix);
|
||||
void set_io_sizes(int inputs, int coils);
|
||||
|
||||
Reference in New Issue
Block a user