mirror of
https://github.com/eddyem/BTA_utils.git
synced 2026-11-22 13:50:26 +03:00
Add configuration, fix working with I/O
This commit is contained in:
6
BTA_dome_modbus/client.conf
Normal file
6
BTA_dome_modbus/client.conf
Normal file
@@ -0,0 +1,6 @@
|
||||
ca = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/ca/ca/ca_cert.pem
|
||||
certificate = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/ca/client/client_cert.pem
|
||||
key = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/ca/client/private/client_key.pem
|
||||
logfile = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/client.log
|
||||
server = localhost
|
||||
verbose = 3
|
||||
@@ -29,6 +29,7 @@
|
||||
* here are global parameters initialisation
|
||||
*/
|
||||
static int help;
|
||||
static char *conffile = NULL;
|
||||
|
||||
#ifdef SERVER
|
||||
#define DEFCERT "server_cert.pem"
|
||||
@@ -68,9 +69,10 @@ static sl_option_t cmdlnopts[] = {
|
||||
{"certificate",NEED_ARG,NULL, 'c', arg_string, APTR(&G.cert), _("path to SSL sertificate (default: " DEFCERT ")")},
|
||||
{"key", NEED_ARG, NULL, 'k', arg_string, APTR(&G.key), _("path to SSL key (default: " DEFKEY ")")},
|
||||
{"port", NEED_ARG, NULL, 'p', arg_string, APTR(&G.port), _("port to open (default: " DEFAULT_PORT ")")},
|
||||
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose), _("increase log verbose level (default: LOG_WARN)")},
|
||||
{"verbose", OPT_ARG, NULL, 'v', arg_int, APTR(&G.verbose), _("increase log verbose level (default: WARN; -v - MSG, -v2 - DBG, -v3 - ANY)")},
|
||||
{"ca", NEED_ARG, NULL, 'a', arg_string, APTR(&G.ca), _("path to SSL ca - base cert (default:" DEFCA ")")},
|
||||
{"timeout", NEED_ARG, NULL, 't', arg_double, APTR(&G.acc_timeout),_("network timeout, s (default: 1)")},
|
||||
{"conffile",NEED_ARG, NULL, 'C', arg_string, APTR(&conffile), _("configuration file (config have advantage over cmdline)")},
|
||||
#ifdef SERVER
|
||||
{"emulation",NO_ARGS, NULL, 'e', arg_int, APTR(&G.emulmode), _("run server in emulation mode")},
|
||||
{"serialdev",NEED_ARG, NULL, 'd', arg_string, APTR(&G.serialpath),_("path to RS-485 device")},
|
||||
@@ -104,10 +106,18 @@ void parse_args(int argc, char **argv){
|
||||
// parse arguments
|
||||
sl_parseargs(&argc, &argv, cmdlnopts);
|
||||
if(help) sl_showhelp(-1, cmdlnopts);
|
||||
DBG("Verbose = %d", G.verbose);
|
||||
if(argc > 0){
|
||||
red("Ignored options:\n");
|
||||
for (i = 0; i < argc; i++)
|
||||
printf("\t%s\n", argv[i]);
|
||||
}
|
||||
if(conffile){
|
||||
if(!sl_conf_readopts(conffile, cmdlnopts)){
|
||||
sl_conf_showhelp(-1, cmdlnopts);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
DBG("Verbose = %d", G.verbose);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ typedef struct{
|
||||
char *port; // port number
|
||||
int verbose; // logfile verbose level
|
||||
char *ca; // ca
|
||||
char *conffile; // configuration file
|
||||
#ifdef SERVER
|
||||
int serialspeed; // speed of serial device
|
||||
char *serialpath; // path to RS-485 device
|
||||
|
||||
@@ -78,7 +78,6 @@ int start_daemon(){
|
||||
#endif
|
||||
if(G.logfile){
|
||||
int lvl = LOGLEVEL_WARN + G.verbose;
|
||||
DBG("level = %d", lvl);
|
||||
if(lvl > LOGLEVEL_ANY) lvl = LOGLEVEL_ANY;
|
||||
green("Log file %s @ level %d\n", G.logfile, lvl);
|
||||
OPENLOG(G.logfile, lvl, 1);
|
||||
|
||||
@@ -28,19 +28,121 @@
|
||||
|
||||
static modbus_t *modbus_ctx = NULL;
|
||||
|
||||
static uint8_t RelayBuf[NRELAYS];
|
||||
static uint8_t InputsBuf[NINPUTS];
|
||||
// last state of relays and inputs
|
||||
static uint32_t RelaysState = 0, InputsState = 0;
|
||||
// user requests to On/Off relay
|
||||
static uint32_t OnFlags = 0, OffFlags = 0;
|
||||
// last ADC buffer state
|
||||
static uint16_t ADCBuf[NADCCHNLS];
|
||||
// mutex for flags & states
|
||||
static pthread_mutex_t sync_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
// array of available relay numbers
|
||||
static uint8_t RelaysAvailable[NRELAYS] = {
|
||||
[0] = 1,
|
||||
[1] = 1,
|
||||
[2] = 1,
|
||||
[3] = 1,
|
||||
[4] = 1,
|
||||
[5] = 1,
|
||||
[6] = 1,
|
||||
[7] = 1,
|
||||
[8] = 0,
|
||||
[9] = 0,
|
||||
[10] = 1,
|
||||
[11] = 1,
|
||||
[12] = 0,
|
||||
[13] = 0,
|
||||
[14] = 0,
|
||||
[15] = 0,
|
||||
};
|
||||
|
||||
// == TRUE if something changed in buffers (reset in `io_read_inputs`)
|
||||
static uint8_t GotNew = FALSE;//, GotNewADC = FALSE;
|
||||
//static uint8_t GotNew = FALSE, GotNewADC = FALSE;
|
||||
|
||||
// share modbus_t opened in motors.c
|
||||
void io_set_ctx(modbus_t *ctx){
|
||||
DBG("Get context");
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
modbus_ctx = ctx;
|
||||
}
|
||||
|
||||
static int change_relay(int N, int state){
|
||||
if(!modbus_ctx || N < 0 || N >= NRELAYS || !RelaysAvailable[N]) return FALSE;
|
||||
int ret = FALSE;
|
||||
if(1 == modbus_write_bit(modbus_ctx, N, state)) ret = TRUE;
|
||||
else WARN("Can't set bit %d to %d", N, state);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//#define RON(relayNo) do{if((RelaysState & (1<<relayNo)) == 0) change_relay(relayNo, 1);}while(0)
|
||||
//#define ROFF(relayNo) do{if((RelaysState & (1<<relayNo))) change_relay(relayNo, 0);}while(0)
|
||||
#define RON(relayNo) do{change_relay(relayNo, 1);}while(0)
|
||||
#define ROFF(relayNo) do{change_relay(relayNo, 0);}while(0)
|
||||
|
||||
// check inputs and set/reset relays according to them
|
||||
static void chk_inputs(){
|
||||
int man_at_nest = FALSE;
|
||||
// unlock door & lock motors when man on the "nest"
|
||||
if((InputsState & (INP_FLOOR1 | INP_FLOOR2 | INP_DOOR_OPENED))){
|
||||
//DBG("Man on the \"nest\" (state: %04X, mask: %04X)", InputsState, (INP_FLOOR1 | INP_FLOOR2 | INP_DOOR_OPENED));
|
||||
ROFF(RELAY_BLOCK_DOOR);
|
||||
RON(RELAY_DOOR_UNLOCKED);
|
||||
RON(RELAY_BLOCK_MOTORS);
|
||||
RON(RELAY_LIGHT);
|
||||
man_at_nest = TRUE;
|
||||
}
|
||||
// unlock door when telescope is @ nest
|
||||
if(InputsState & INP_TEL_AT_NEST){
|
||||
//DBG("Telescope at the \"nest\"");
|
||||
ROFF(RELAY_BLOCK_DOOR);
|
||||
RON(RELAY_DOOR_UNLOCKED);
|
||||
// allow to open the door
|
||||
if(InputsState & INP_OPEN_DOOR){
|
||||
RON(RELAY_OPEN_DOOR);
|
||||
RON(RELAY_BLOCK_MOTORS);
|
||||
}else{
|
||||
ROFF(RELAY_OPEN_DOOR);
|
||||
}
|
||||
}else{
|
||||
if(man_at_nest){ // somebody @ nest when there's no telescope there!
|
||||
if(InputsState & INP_OPEN_DOOR){
|
||||
RON(RELAY_OPEN_DOOR);
|
||||
}else{
|
||||
ROFF(RELAY_OPEN_DOOR);
|
||||
}
|
||||
}else{
|
||||
ROFF(RELAY_OPEN_DOOR);
|
||||
RON(RELAY_BLOCK_DOOR);
|
||||
ROFF(RELAY_DOOR_UNLOCKED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void chk_relays(){
|
||||
// if motors are locked turn on light
|
||||
if(RelaysState & (1 << RELAY_BLOCK_MOTORS)){
|
||||
//DBG("Motors locked -> turn on light");
|
||||
RON(RELAY_LIGHT);
|
||||
}else{ // turn off light
|
||||
//DBG("Motors unlocked -> turn off light");
|
||||
ROFF(RELAY_LIGHT);
|
||||
}
|
||||
// check user flags
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
if(OnFlags){
|
||||
uint32_t flag = 1;
|
||||
for(int i = 0; i < NRELAYS; ++i, flag <<= 1){
|
||||
if(OnFlags & flag) RON(i);
|
||||
}
|
||||
OnFlags = 0;
|
||||
}
|
||||
if(OffFlags){
|
||||
uint32_t flag = 1;
|
||||
for(int i = 0; i < NRELAYS; ++i, flag <<= 1){
|
||||
if(OffFlags & flag) ROFF(i);
|
||||
}
|
||||
OffFlags = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
}
|
||||
|
||||
@@ -48,65 +150,91 @@ void io_set_ctx(modbus_t *ctx){
|
||||
void io_process(){
|
||||
uint8_t buf[NRELAYS + NINPUTS];
|
||||
uint16_t adcbuf[NADCCHNLS];
|
||||
uint32_t flag = 1, rstate = 0, istate = 0;
|
||||
|
||||
if(!modbus_ctx || G.io_ID < 1) return;
|
||||
if(-1 == modbus_set_slave(modbus_ctx, G.io_ID)){
|
||||
WARN("Can't set IO slave ID to %d", G.io_ID);
|
||||
return;
|
||||
}
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
if(-1 == modbus_read_bits(modbus_ctx, 0, NRELAYS, buf)){
|
||||
WARN("Can't read relay state");
|
||||
goto ret;
|
||||
}
|
||||
for(int i = 0; i < NRELAYS; ++i){
|
||||
if(buf[i] == RelayBuf[i]) continue;
|
||||
#ifdef EBUG
|
||||
if(buf[i]) printf("Relay %d is ON\n", i);
|
||||
else printf("Relay %d is OFF\n", i);
|
||||
fflush(stdout);
|
||||
#endif
|
||||
RelayBuf[i] = buf[i];
|
||||
GotNew = TRUE;
|
||||
}
|
||||
|
||||
if(-1 == modbus_read_input_bits(modbus_ctx, 0, NINPUTS, buf)){
|
||||
WARN("Can't read inputs");
|
||||
goto ret;
|
||||
return;
|
||||
}
|
||||
for(int i = 0; i < NINPUTS; ++i){
|
||||
if(buf[i] == InputsBuf[i]) continue;
|
||||
for(int i = 0; i < NINPUTS; ++i, flag <<= 1){
|
||||
if(buf[i]) istate |= flag;
|
||||
#ifdef EBUG
|
||||
int oldstate = (InputsState & flag) ? 1 : 0;
|
||||
if(buf[i] == oldstate) continue;
|
||||
if(buf[i]) printf("Input %d is Active\n", i);
|
||||
else printf("Input %d is Inactive\n", i);
|
||||
fflush(stdout);
|
||||
#endif
|
||||
InputsBuf[i] = buf[i];
|
||||
GotNew = TRUE;
|
||||
//GotNew = TRUE;
|
||||
}
|
||||
//DBG("Inputs old/new: 0x%04X / 0x%04X", InputsState, istate);
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
InputsState = istate;
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
// check inputs before reading relays' state
|
||||
chk_inputs();
|
||||
|
||||
if(-1 == modbus_read_bits(modbus_ctx, 0, NRELAYS, buf)){
|
||||
WARN("Can't read relay state");
|
||||
return;
|
||||
}
|
||||
flag = 1;
|
||||
for(int i = 0; i < NRELAYS; ++i, flag <<= 1){
|
||||
if(buf[i]) rstate |= flag;
|
||||
#ifdef EBUG
|
||||
int oldstate = (RelaysState & flag) ? 1 : 0;
|
||||
if(buf[i] == oldstate) continue;
|
||||
if(buf[i]) printf("Relay %d is ON\n", i);
|
||||
else printf("Relay %d is OFF\n", i);
|
||||
fflush(stdout);
|
||||
#endif
|
||||
// GotNew = TRUE;
|
||||
}
|
||||
//DBG("Relays old/new: 0x%04X / 0x%04X", RelaysState, rstate);
|
||||
chk_relays();
|
||||
|
||||
if(-1 == modbus_read_input_registers(modbus_ctx, 0, NADCCHNLS, adcbuf)){
|
||||
WARN("Can't read ADC inputs");
|
||||
goto ret;
|
||||
return;
|
||||
}
|
||||
#ifdef EBUG
|
||||
for(int i = 0; i < NADCCHNLS; ++i){
|
||||
if(abs(adcbuf[i] - ADCBuf[i]) < ADCTHRESH) continue;
|
||||
// GotNewADC = TRUE; break;
|
||||
#ifdef EBUG
|
||||
|
||||
printf("ADC input %d changed to %d\n", i, adcbuf[i]);
|
||||
fflush(stdout);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
RelaysState = rstate;
|
||||
memcpy(ADCBuf, adcbuf, sizeof(uint16_t) * NADCCHNLS);
|
||||
ret:
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
}
|
||||
|
||||
// get relays state
|
||||
void io_read_relays(uint32_t *state){
|
||||
if(!state) return;
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
*state = RelaysState;
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
}
|
||||
|
||||
// get inputs state
|
||||
int io_read_inputs(char inputs[NINPUTS]){
|
||||
void io_read_inputs(uint32_t *state){
|
||||
if(!state) return;
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
int got = GotNew;
|
||||
GotNew = FALSE;
|
||||
memcpy(inputs, InputsBuf, NINPUTS);
|
||||
//int got = GotNew;
|
||||
//GotNew = FALSE;
|
||||
*state = InputsState;
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
return got;
|
||||
//return got;
|
||||
}
|
||||
|
||||
int io_read_adc(int nch, uint16_t *val){
|
||||
@@ -119,44 +247,33 @@ int io_read_adc(int nch, uint16_t *val){
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// get relays state
|
||||
void io_read_relays(char relays[NRELAYS]){
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
memcpy(relays, RelayBuf, NRELAYS);
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
}
|
||||
|
||||
static int change_relay(int N, int state){
|
||||
if(!modbus_ctx || N < 0 || N >= NRELAYS) return FALSE;
|
||||
int ret = FALSE;
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
if(1 == modbus_write_bit(modbus_ctx, N, state)) ret = TRUE;
|
||||
else WARN("Can't set bit %d to %d", N, state);
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// set relay with given number
|
||||
int io_relay_on(int N){
|
||||
return change_relay(N, 1);
|
||||
if(N >= 0 && N < NRELAYS && RelaysAvailable[N]){
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
OnFlags |= 1 << N;
|
||||
OffFlags &= ~(1 << N);
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// reset relay with given number
|
||||
int io_relay_off(int N){
|
||||
return change_relay(N, 0);
|
||||
if(N >= 0 && N < NRELAYS && RelaysAvailable[N]){
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
OffFlags |= 1 << N;
|
||||
OnFlags &= ~(1 << N);
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int io_set_relays(int N){
|
||||
if(!modbus_ctx || N < 0) return FALSE;
|
||||
int ret = FALSE;
|
||||
uint8_t bits[NRELAYS];
|
||||
for(int i = 0; i < NRELAYS; ++i){
|
||||
if(N & (1 << i)) bits[i] = 1;
|
||||
else bits[i] = 0;
|
||||
}
|
||||
void io_set_relays(uint32_t flags){
|
||||
pthread_mutex_lock(&sync_mutex);
|
||||
if(1 == modbus_write_bits(modbus_ctx, 0, NRELAYS, bits)) ret = TRUE;
|
||||
else WARN("Can't write bits %d", N);
|
||||
OnFlags = flags;
|
||||
OffFlags = ~flags;
|
||||
pthread_mutex_unlock(&sync_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <modbus/modbus.h>
|
||||
|
||||
#define NRELAYS 16
|
||||
@@ -26,13 +27,30 @@
|
||||
// ADC level threshold: 50 units
|
||||
#define ADCTHRESH 50
|
||||
|
||||
// INPUTS:
|
||||
#define INP_FLOOR1 0x0001
|
||||
#define INP_FLOOR2 0x0002
|
||||
#define INP_DOOR_OPENED 0x0004
|
||||
#define INP_OPEN_DOOR 0x0008
|
||||
#define INP_TEL_AT_NEST 0x0010
|
||||
//#define INP_
|
||||
|
||||
// RELAYS:
|
||||
#define RELAY_OPEN_DOOR 1
|
||||
#define RELAY_BLOCK_DOOR 2
|
||||
#define RELAY_LIGHT 3
|
||||
#define RELAY_POWER 4
|
||||
#define RELAY_BLOCK_MOTORS 5
|
||||
#define RELAY_DOOR_UNLOCKED 10
|
||||
//#define RELAY_
|
||||
|
||||
void io_set_ctx(modbus_t *ctx);
|
||||
void io_process();
|
||||
|
||||
int io_read_inputs(char inputs[NINPUTS]);
|
||||
void io_read_inputs(uint32_t *state);
|
||||
int io_read_adc(int nch, uint16_t *val);
|
||||
void io_read_relays(char relays[NRELAYS]);
|
||||
void io_read_relays(uint32_t *state);
|
||||
|
||||
int io_relay_on(int N);
|
||||
int io_relay_off(int N);
|
||||
int io_set_relays(int N);
|
||||
void io_set_relays(uint32_t flags);
|
||||
|
||||
@@ -28,9 +28,6 @@
|
||||
// handlers: `index` - command index in list, `value` - setter's value or getter's answer
|
||||
typedef sl_sock_hresult_e (*handler_t)(int index, char value[SL_VAL_LEN]);
|
||||
|
||||
// == TRUE if dome management is forbidden
|
||||
static int forbidden = FALSE;
|
||||
|
||||
// struct for setters/getters
|
||||
typedef struct{
|
||||
const char *command;
|
||||
@@ -231,37 +228,29 @@ static sl_sock_hresult_e forbidden_handler(int _U_ index, char value[SL_VAL_LEN]
|
||||
int I;
|
||||
if(ISSETTER(value)){
|
||||
if(!sl_str2i(&I, value)) return RESULT_BADVAL;
|
||||
forbidden = I;
|
||||
if(I) io_relay_on(RELAY_BLOCK_MOTORS);
|
||||
else io_relay_off(RELAY_BLOCK_MOTORS);
|
||||
return RESULT_OK;
|
||||
}
|
||||
snprintf(value, SL_VAL_LEN-1, "%d", forbidden);
|
||||
uint32_t relays;
|
||||
io_read_relays(&relays);
|
||||
snprintf(value, SL_VAL_LEN-1, "%s", (relays & 1 << RELAY_BLOCK_MOTORS) ? "1" : "0");
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
|
||||
static sl_sock_hresult_e inputs_handler(int _U_ index, char value[SL_VAL_LEN]){
|
||||
if(ISSETTER(value)) return RESULT_BADVAL;
|
||||
char inputs[NINPUTS], *ptr = value;
|
||||
size_t restofline = SL_VAL_LEN - 1;
|
||||
int triggered = io_read_inputs(inputs);
|
||||
for(int i = 0; i < NINPUTS && restofline; ++i, ++ptr, --restofline){
|
||||
if(inputs[i]) *ptr = '1';
|
||||
else *ptr = '0';
|
||||
}
|
||||
if(restofline && triggered) *ptr++ = 'T';
|
||||
*ptr = 0;
|
||||
uint32_t Istate;
|
||||
io_read_inputs(&Istate);
|
||||
snprintf(value, SL_VAL_LEN, "%" PRIu32, Istate);
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
|
||||
// common getter for all relay commands
|
||||
static void read_relays(char value[SL_VAL_LEN]){
|
||||
char relays[NRELAYS], *ptr = value;
|
||||
size_t restofline = SL_VAL_LEN - 1;
|
||||
io_read_relays(relays);
|
||||
for(int i = 0; i < NRELAYS && restofline; ++i, ++ptr, --restofline){
|
||||
if(relays[i]) *ptr = '1';
|
||||
else *ptr = '0';
|
||||
}
|
||||
*ptr = 0;
|
||||
uint32_t Rstate;
|
||||
io_read_relays(&Rstate);
|
||||
snprintf(value, SL_VAL_LEN, "%" PRIu32, Rstate);
|
||||
}
|
||||
|
||||
static sl_sock_hresult_e relayon_handler(int _U_ index, char value[SL_VAL_LEN]){
|
||||
@@ -293,7 +282,7 @@ static sl_sock_hresult_e relayset_handler(int _U_ index, char value[SL_VAL_LEN])
|
||||
}
|
||||
int I;
|
||||
if(!sl_str2i(&I, value)) return RESULT_BADVAL;
|
||||
if(!io_set_relays(I)) return RESULT_FAIL;
|
||||
io_set_relays((uint32_t)I);
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
@@ -350,7 +339,6 @@ sl_sock_hresult_e relay_handler(int _U_ index, char _U_ value[SL_VAL_LEN]){
|
||||
static sl_sock_hresult_e speed_handler(int _U_ index, char _U_ value[SL_VAL_LEN]){
|
||||
double D;
|
||||
if(ISSETTER(value)){
|
||||
if(forbidden) return RESULT_FAIL;
|
||||
if(!sl_str2d(&D, value)) return RESULT_BADVAL;
|
||||
return motors_set_speedsetpoint(D);
|
||||
}
|
||||
|
||||
8
BTA_dome_modbus/server.conf
Normal file
8
BTA_dome_modbus/server.conf
Normal file
@@ -0,0 +1,8 @@
|
||||
ca = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/ca/ca/ca_cert.pem
|
||||
certificate = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/ca/server/server_cert.pem
|
||||
key = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/ca/server/private/server_key.pem
|
||||
logfile = /home/eddy/Docs/SAO/BTA/BTA_utils/BTA_dome_modbus/SSL_socket/server.log
|
||||
serialdev = /dev/ttyUSB0
|
||||
serialspeed = 19200
|
||||
ioid = 20
|
||||
verbose = 3
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "cmdlnopts.h"
|
||||
#include "sslsock.h"
|
||||
#ifdef SERVER
|
||||
#include "io.h"
|
||||
#include "motors.h"
|
||||
#include "server.h"
|
||||
#else
|
||||
@@ -43,7 +44,7 @@ static int OpenConn(int port){
|
||||
// allow reuse of descriptor
|
||||
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (void *)&enable, sizeof(int)) < 0){
|
||||
LOGERR("Can't apply SO_REUSEADDR to socket");
|
||||
ERRX("setsockopt()");
|
||||
ERR("setsockopt()");
|
||||
}
|
||||
struct sockaddr_in addr = {0};
|
||||
addr.sin_family = AF_INET;
|
||||
@@ -51,11 +52,11 @@ static int OpenConn(int port){
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
if(bind(sd, (struct sockaddr*)&addr, sizeof(addr))){
|
||||
LOGWARN("Can't bind port %d", port);
|
||||
ERRX("bind()");
|
||||
ERR("bind()");
|
||||
}
|
||||
if(listen(sd, BACKLOG)){
|
||||
LOGWARN("Can't listen()");
|
||||
ERRX("listen()");
|
||||
ERR("listen()");
|
||||
}
|
||||
return sd;
|
||||
}
|
||||
@@ -67,7 +68,7 @@ static int OpenConn(int port){
|
||||
struct sockaddr_in addr;
|
||||
if((host = gethostbyname(G.serverhost)) == NULL ){
|
||||
LOGWARN("gethostbyname(%s) error", G.serverhost);
|
||||
ERRX("gethostbyname()");
|
||||
ERR("gethostbyname()");
|
||||
}
|
||||
sd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
DBG("sd=%d", sd);
|
||||
@@ -78,7 +79,7 @@ static int OpenConn(int port){
|
||||
if(connect(sd, (struct sockaddr*)&addr, sizeof(addr))){
|
||||
close(sd);
|
||||
LOGWARN("Can't connect to %s", G.serverhost);
|
||||
ERRX("connect()");
|
||||
ERR("connect()");
|
||||
}
|
||||
return sd;
|
||||
}
|
||||
@@ -144,6 +145,11 @@ int open_socket(){
|
||||
WARNX("Can't open %s @%d", G.serialpath, G.serialspeed);
|
||||
return 1;
|
||||
}
|
||||
DBG("Motors opened: Stop them");
|
||||
motors_stop();
|
||||
DBG("Inhibit");
|
||||
io_set_relays(0); // turn off all relays
|
||||
io_relay_on(RELAY_BLOCK_MOTORS); // turn on locking relay
|
||||
serverproc(ctx, fd);
|
||||
#else
|
||||
if(G.terminal) terminal(ctx, fd);
|
||||
|
||||
@@ -3,5 +3,5 @@
|
||||
#define CLIENT
|
||||
#define EBUG
|
||||
|
||||
//#define SERVER
|
||||
#define SERVER
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ daemon.c
|
||||
daemon.h
|
||||
esq770.h
|
||||
handlers_list.h
|
||||
io.c
|
||||
io.h
|
||||
main.c
|
||||
motors.c
|
||||
motors.h
|
||||
|
||||
Reference in New Issue
Block a user