mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-11-22 22:00:19 +03:00
a lot of fixes
This commit is contained in:
@@ -30,6 +30,8 @@ static sl_sock_t *s = NULL;
|
||||
// amount of input/output bytes
|
||||
static int input_bytes = 0;
|
||||
static int coils_bytes = 0;
|
||||
// if exitjob == 1 we should stop server
|
||||
static bool exitjob = false;
|
||||
|
||||
// specific bit to read/write
|
||||
static sl_sock_keyno_t rinputs = SL_SOCK_KEYNO_DEFAULT,
|
||||
@@ -42,26 +44,26 @@ void set_io_sizes(int inputs, int coils){
|
||||
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;
|
||||
static bool 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;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int num_to_bytes(uint64_t in, uint8_t *buf, int n){
|
||||
if(!buf || n < 1 || n > 64) return FALSE;
|
||||
static bool 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;
|
||||
return true;
|
||||
}
|
||||
|
||||
// stop dump and close dump file
|
||||
@@ -73,7 +75,8 @@ static sl_sock_hresult_e closedump(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *i
|
||||
static sl_sock_hresult_e newdump(sl_sock_t *client, _U_ sl_sock_hitem_t *item, const char *req){
|
||||
char buf[BUFSIZ];
|
||||
if(!req){ // getter
|
||||
snprintf(buf, BUFSIZ-1, "%s = %s\n", item->key, getdumpname());
|
||||
const char *n = getdumpname();
|
||||
snprintf(buf, BUFSIZ-1, "%s = %s\n", item->key, n ? n : "(none)");
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
return RESULT_SILENCE;
|
||||
}else{
|
||||
@@ -195,31 +198,47 @@ static sl_sock_hresult_e writecoils(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *
|
||||
return RESULT_BADVAL;
|
||||
}
|
||||
bits = (uint64_t) ll;
|
||||
int ok = FALSE;
|
||||
bool 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");
|
||||
WARNX("Bad parameters for writecoils");
|
||||
return RESULT_BADVAL;
|
||||
}
|
||||
ok = write_coils(coils_bytes, buf);
|
||||
}
|
||||
if(0 == get_slave()) return RESULT_SILENCE;
|
||||
return ok ? RESULT_OK : RESULT_FAIL;
|
||||
}
|
||||
|
||||
static sl_sock_hresult_e setid(sl_sock_t *client, _U_ sl_sock_hitem_t *item, const char *req){
|
||||
char buf[BUFSIZ];
|
||||
int slaveID;
|
||||
if(!req){ // getter
|
||||
slaveID = get_slave();
|
||||
snprintf(buf, BUFSIZ-1, "%s = %d\n", item->key, slaveID);
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
if(!sl_str2i(&slaveID, req)) return RESULT_BADVAL;
|
||||
if(!set_slave(slaveID)) return RESULT_FAIL;
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
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},
|
||||
{listdict, "list", "list all dictionary (as getter) or given register (as setter: by codename or value)", NULL},
|
||||
{newdump, "newdump", "open new dump file or get name of current", 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},
|
||||
{setid, "setid", "set slave's ID", NULL},
|
||||
{writecoils, "writecoils", "write all coils from HEX number (uses --coils bytes)", (void*)&wcoils},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
// new connections handler (return FALSE to reject client)
|
||||
// new connections handler (return FALSE to reject client); should be INT!
|
||||
static int connected(sl_sock_t *c){
|
||||
if(c->type == SOCKT_UNIX) LOGMSG("New client fd=%d connected", c->fd);
|
||||
else LOGMSG("New client fd=%d, IP=%s connected", c->fd, c->IP);
|
||||
@@ -243,6 +262,7 @@ static sl_sock_hresult_e defhandler(sl_sock_t *s, const char *str){
|
||||
int n = sl_get_keyval(str, key, value);
|
||||
if(n == 0){
|
||||
WARNX("Can't parse `%s` as reg[=val]", str);
|
||||
deepcounter = 0;
|
||||
return RESULT_BADKEY;
|
||||
}
|
||||
dicentry_t *entry = NULL;
|
||||
@@ -262,41 +282,52 @@ static sl_sock_hresult_e defhandler(sl_sock_t *s, const char *str){
|
||||
}
|
||||
}
|
||||
WARNX("Entry %s not found", key);
|
||||
deepcounter = 0;
|
||||
return RESULT_BADKEY;
|
||||
}
|
||||
deepcounter = 0;
|
||||
if(n == 1){ // getter
|
||||
if(!read_entry(entry)) return RESULT_FAIL;
|
||||
if(!read_entry(entry)){
|
||||
return RESULT_FAIL;
|
||||
}
|
||||
snprintf(value, SL_VAL_LEN-1, "%s=%u\n", key, entry->value);
|
||||
}else{ // setterrunserver
|
||||
if(!sl_str2i(&N, value)){
|
||||
}else{ // setter
|
||||
if(!sl_str2i(&N, value) || N < 0 || N > 0xffff){
|
||||
WARNX("%s isn't a value of register", value);
|
||||
return RESULT_BADVAL;
|
||||
}
|
||||
entry->value = (uint16_t)N;
|
||||
if(entry->readonly){
|
||||
WARNX("%s is readonly register", entry->code);
|
||||
return RESULT_FAIL;
|
||||
}
|
||||
if(0 == get_slave()){ // we can't read anything by broadcast address
|
||||
write_entry(entry);
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
if(!write_entry(entry)) return RESULT_FAIL;
|
||||
entry->value = (uint16_t)N;
|
||||
return RESULT_OK;
|
||||
}
|
||||
sl_sock_sendstrmessage(s, value);
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
|
||||
int runserver(const char *node, int isunix){
|
||||
bool runserver(const char *node, int isunix){
|
||||
if(!node){
|
||||
WARNX("Point node");
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
sl_socktype_e type = (isunix) ? SOCKT_UNIX : SOCKT_NET;
|
||||
if(s) sl_sock_delete(&s);
|
||||
s = sl_sock_run_server(type, node, -1, handlers);
|
||||
if(!s){
|
||||
WARNX("Can't run server");
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
sl_sock_connhandler(s, connected);
|
||||
sl_sock_dischandler(s, disconnected);
|
||||
sl_sock_defmsghandler(s, defhandler);
|
||||
while(s && s->connected){
|
||||
while(!exitjob && s && s->connected ){
|
||||
if(!s->rthread){
|
||||
WARNX("Server handlers thread is dead");
|
||||
break;
|
||||
@@ -305,5 +336,7 @@ int runserver(const char *node, int isunix){
|
||||
}
|
||||
DBG("Close");
|
||||
sl_sock_delete(&s);
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
void stop_server(){exitjob = true; }
|
||||
|
||||
Reference in New Issue
Block a user