From 25d35e22942545ded81a161029b45dd33fd55e6c Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Tue, 11 Aug 2026 10:15:20 +0300 Subject: [PATCH] add simplest terminal --- BTA_dome_modbus/client.c | 69 ++++++++++++++++++++++++++++++------- BTA_dome_modbus/client.h | 1 + BTA_dome_modbus/cmdlnopts.c | 1 + BTA_dome_modbus/cmdlnopts.h | 1 + BTA_dome_modbus/daemon.c | 4 +++ BTA_dome_modbus/main.c | 1 - BTA_dome_modbus/sslsock.c | 3 +- 7 files changed, 65 insertions(+), 15 deletions(-) diff --git a/BTA_dome_modbus/client.c b/BTA_dome_modbus/client.c index 12d6fa9..ef9333a 100644 --- a/BTA_dome_modbus/client.c +++ b/BTA_dome_modbus/client.c @@ -328,18 +328,9 @@ static char *time_asc(double t){ } */ -void clientproc(SSL_CTX *ctx, int fd){ - FNAME(); - SSL *ssl; - char buf[1024]; - int bytes; - sdat.mode |= 0200; // allow W - sdat.atflag = 0; // clear SHM_RDONLY - if(!get_shm_block(&sdat, ClientSide)){ - LOGERR("Can't get SHM block"); - ERRX("Can't get SHM block"); - } - ssl = SSL_new(ctx); +// open SSL connection for client +static SSL *openConn(SSL_CTX *ctx, int fd){ + SSL *ssl = SSL_new(ctx); SSL_set_fd(ssl, fd); int c = SSL_connect(ssl); if(c < 0){ @@ -351,6 +342,21 @@ void clientproc(SSL_CTX *ctx, int fd){ LOGERR("Can't make socket nonblocking"); ERRX("ioctl()"); } + return ssl; +} + +// run main client process +void clientproc(SSL_CTX *ctx, int fd){ + FNAME(); + char buf[1024]; + SSL *ssl = openConn(ctx, fd); + if(!ssl) return; + sdat.mode |= 0200; // allow W + sdat.atflag = 0; // clear SHM_RDONLY + if(!get_shm_block(&sdat, ClientSide)){ + LOGERR("Can't get SHM block"); + ERRX("Can't get SHM block"); + } while(isrunning){ if(!process_system(ssl)){ LOGERR("Motors error"); @@ -360,7 +366,7 @@ void clientproc(SSL_CTX *ctx, int fd){ break; } // clear receiving buffer (TODO: parse it?) - bytes = SSL_nbread(ssl, buf, sizeof(buf)); + int bytes = SSL_nbread(ssl, buf, sizeof(buf)-1); if(bytes > 0){ buf[bytes] = 0; fprintf(stderr, "Received: \"%s\"\n", buf); @@ -373,3 +379,40 @@ void clientproc(SSL_CTX *ctx, int fd){ DBG("Exit; isrunning=%d", isrunning); SSL_free(ssl); } + +// run in terminal mode +void terminal(SSL_CTX *ctx, int fd){ + FNAME(); + char buf[1024], *lptr = NULL; + size_t N = 0; + SSL *ssl = openConn(ctx, fd); + if(!ssl) return; + int printed = FALSE; + while(isrunning){ + if(!printed){ + printf("> "); + fflush(stdout); + printed = TRUE; + } + if(sl_canread(0)){ + ssize_t L = getline(&lptr, &N, stdin); + if(L > (ssize_t)(sizeof(buf)-1)) WARNX("String too long!"); + else if(L > 0){ + SSL_write(ssl, lptr, L); + } + } + int bytes = 0; + while((bytes = SSL_nbread(ssl, buf, sizeof(buf)-1))){ + if(bytes > 0){ + buf[bytes] = 0; + printf("< %s\n", buf); + }else{ + LOGWARN("Server disconnected or other error"); + ERRX("Disconnected"); + } + usleep(10000); + printed = FALSE; + } + } + SSL_free(ssl); +} diff --git a/BTA_dome_modbus/client.h b/BTA_dome_modbus/client.h index 5fa2171..a804300 100644 --- a/BTA_dome_modbus/client.h +++ b/BTA_dome_modbus/client.h @@ -21,3 +21,4 @@ #include "sslsock.h" void clientproc(SSL_CTX *ctx, int fd); +void terminal(SSL_CTX *ctx, int fd); diff --git a/BTA_dome_modbus/cmdlnopts.c b/BTA_dome_modbus/cmdlnopts.c index 038ef79..a170270 100644 --- a/BTA_dome_modbus/cmdlnopts.c +++ b/BTA_dome_modbus/cmdlnopts.c @@ -72,6 +72,7 @@ static sl_option_t cmdlnopts[] = { {"server", NEED_ARG, NULL, 's', arg_string, APTR(&G.serverhost), _("server node, IP:port")}, {"lostsync",NEED_ARG, NULL, 'L', arg_double, APTR(&G.T_sync_lost),_("\"lost synchronization\" timeout, s (default: 5)")}, {"emulation",NO_ARGS, NULL, 'e', arg_int, APTR(&G.emulmode), _("run even in emulation mode")}, + {"terminal",NO_ARGS, NULL, 'T', arg_int, APTR(&G.terminal), _("run client in terminal mode")}, #endif end_option }; diff --git a/BTA_dome_modbus/cmdlnopts.h b/BTA_dome_modbus/cmdlnopts.h index 5f87074..07d8540 100644 --- a/BTA_dome_modbus/cmdlnopts.h +++ b/BTA_dome_modbus/cmdlnopts.h @@ -35,6 +35,7 @@ */ typedef struct{ int emulmode; // emulation mode: don't check server & run in model mode + int terminal; // run client in terminal mode double acc_timeout; // network timeout, s double T_sync_lost; // "lost synchronization" timeout, s double speedchk_interval;// interval of motors' speed checking (resend command if don't reach yet) diff --git a/BTA_dome_modbus/daemon.c b/BTA_dome_modbus/daemon.c index 2ead888..34c2faf 100644 --- a/BTA_dome_modbus/daemon.c +++ b/BTA_dome_modbus/daemon.c @@ -72,6 +72,10 @@ int start_daemon(){ #ifdef CLIENT //DBG("server: %s", G.serverhost); if(!G.serverhost) ERRX("Point server name"); + if(G.terminal){ + isrunning = TRUE; + return open_socket(); + } #endif if(G.logfile){ int lvl = LOGLEVEL_WARN + G.verbose; diff --git a/BTA_dome_modbus/main.c b/BTA_dome_modbus/main.c index e59ac0a..92cc6f1 100644 --- a/BTA_dome_modbus/main.c +++ b/BTA_dome_modbus/main.c @@ -24,6 +24,5 @@ int main(int argc, char **argv){ sl_init(); parse_args(argc, argv); - DBG("START"); return start_daemon(); } diff --git a/BTA_dome_modbus/sslsock.c b/BTA_dome_modbus/sslsock.c index a208184..ccb3245 100644 --- a/BTA_dome_modbus/sslsock.c +++ b/BTA_dome_modbus/sslsock.c @@ -139,7 +139,8 @@ int open_socket(){ #ifdef SERVER serverproc(ctx, fd); #else - clientproc(ctx, fd); + if(G.terminal) terminal(ctx, fd); + else clientproc(ctx, fd); #endif // newer reached close(fd);