mirror of
https://github.com/eddyem/small_tel.git
synced 2026-05-01 18:37:07 +03:00
new baader-dome & astrosib-telescope daemons
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 19.0.0, 2026-03-22T21:14:01. -->
|
||||
<!-- Written by QtCreator 19.0.0, 2026-03-24T22:16:27. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
commands.h
|
||||
main.c
|
||||
socket.c
|
||||
socket.h
|
||||
|
||||
31
Daemons/domedaemon_baader/commands.h
Normal file
31
Daemons/domedaemon_baader/commands.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of the baader_dome project.
|
||||
* Copyright 2026 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// text commands and answers
|
||||
#define TXT_GETWARN "d#warning"
|
||||
#define TXT_OPENDOME "d#opendom"
|
||||
#define TXT_CLOSEDOME "d#closdom"
|
||||
#define TXT_STOPDOME "d#stopdom"
|
||||
#define TXT_GETSTAT "d#get_dom"
|
||||
#define TXT_GETWEAT "d#ask_wea"
|
||||
#define TXT_ANS_MSGOK "d#gotmess"
|
||||
#define TXT_ANS_WEAT "d#wea"
|
||||
#define TXT_ANS_STAT "d#pos"
|
||||
#define TXT_ANS_ERR "d#erro"
|
||||
@@ -41,7 +41,8 @@ typedef struct{
|
||||
|
||||
static parameters G = {
|
||||
.maxclients = 2,
|
||||
.serspeed = 9600
|
||||
.serspeed = 9600,
|
||||
.sertmout = 5000
|
||||
};
|
||||
|
||||
static sl_option_t cmdlnopts[] = {
|
||||
@@ -77,7 +78,7 @@ void signals(int sig){
|
||||
DBG("Stop server");
|
||||
stopserver();
|
||||
DBG("Close terminal");
|
||||
close_term();
|
||||
term_close();
|
||||
DBG("Exit");
|
||||
exit(sig);
|
||||
}
|
||||
@@ -94,7 +95,7 @@ int main(int argc, char **argv){
|
||||
if(lvl >= LOGLEVEL_AMOUNT) lvl = LOGLEVEL_AMOUNT - 1;
|
||||
if(G.logfile) OPENLOG(G.logfile, lvl, 1);
|
||||
LOGMSG("Started");
|
||||
if(!open_term(G.termpath, G.serspeed, G.sertmout)){
|
||||
if(!term_open(G.termpath, G.serspeed, G.sertmout)){
|
||||
LOGERR("Can't open %s", G.termpath);
|
||||
ERRX("Fatal error");
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "commands.h"
|
||||
#include "socket.h"
|
||||
#include "term.h"
|
||||
|
||||
@@ -33,6 +34,7 @@ typedef enum{
|
||||
|
||||
typedef struct{
|
||||
dome_commands_t cmd;
|
||||
dome_commands_t erroredcmd; // command didn't run - waiting or stalled
|
||||
int errcode; // error code
|
||||
char *status; // device status
|
||||
int statlen; // size of `status` buffer
|
||||
@@ -56,24 +58,6 @@ void stopserver(){
|
||||
if(rb) sl_RB_delete(&rb);
|
||||
}
|
||||
|
||||
#if 0
|
||||
// flags for standard handlers
|
||||
static sl_sock_int_t iflag = {0};
|
||||
static sl_sock_double_t dflag = {0};
|
||||
static sl_sock_string_t sflag = {0};
|
||||
static uint32_t bitflags = 0;
|
||||
|
||||
static sl_sock_hresult_e show(sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U_ const char *req){
|
||||
if(locksock && locksock->type != SOCKT_UNIX){
|
||||
if(*client->IP){
|
||||
printf("Client \"%s\" (fd=%d) ask for flags:\n", client->IP, client->fd);
|
||||
}else printf("Can't get client's IP, flags:\n");
|
||||
}else printf("Socket fd=%d asks for flags:\n", client->fd);
|
||||
printf("\tiflag=%" PRId64 ", dflag=%g\n", iflag.val, dflag.val);
|
||||
return RESULT_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
// send "measuret=..."
|
||||
static void sendtmeasured(sl_sock_t *client, double t){
|
||||
char buf[256];
|
||||
@@ -116,17 +100,18 @@ static sl_sock_hresult_e stoph(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item,
|
||||
static sl_sock_hresult_e statush(sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U_ const char *req){
|
||||
char buf[256];
|
||||
double t = NAN;
|
||||
int ecode;
|
||||
int ecode, lastecmd;
|
||||
pthread_mutex_lock(&Dome.mutex);
|
||||
if(!*Dome.status || sl_dtime() - Dome.stattime > 3.*T_INTERVAL) snprintf(buf, 255, "%s=unknown\n", item->key);
|
||||
else{
|
||||
snprintf(buf, 255, "%s=%s\n", item->key, Dome.status);
|
||||
t = Dome.stattime;
|
||||
}
|
||||
ecode = Dome.errcode;
|
||||
lastecmd = Dome.erroredcmd;
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
if(!isnan(t)) sendtmeasured(client, t);
|
||||
ecode = Dome.errcode;
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
if(ecode){
|
||||
int l = snprintf(buf, 255, "error=closed");
|
||||
if(ecode > 99){
|
||||
@@ -147,6 +132,17 @@ static sl_sock_hresult_e statush(sl_sock_t *client, _U_ sl_sock_hitem_t *item, _
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
}
|
||||
if(ForbidObservations) sl_sock_sendstrmessage(client, "FORBIDDEN\n");
|
||||
if(lastecmd != CMD_NONE){
|
||||
const char *tcmd;
|
||||
switch(lastecmd){
|
||||
case CMD_OPEN: tcmd = "open"; break;
|
||||
case CMD_CLOSE: tcmd = "close"; break;
|
||||
case CMD_STOP: tcmd = "stop"; break;
|
||||
default: tcmd = "unknown";
|
||||
}
|
||||
snprintf(buf, 255, "errored_command=%s\n", tcmd);
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
}
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
|
||||
@@ -202,44 +198,8 @@ static sl_sock_hresult_e defhandler(struct sl_sock *s, const char *str){
|
||||
sl_sock_sendstrmessage(s, "\n```\nTry \"help\"\n");
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
#if 0
|
||||
// if we use this macro, there's no need to run `sl_sock_keyno_init` later
|
||||
static sl_sock_keyno_t kph_number = SL_SOCK_KEYNO_DEFAULT;
|
||||
// handler for key with optional parameter number
|
||||
static sl_sock_hresult_e keyparhandler(struct sl_sock *s, sl_sock_hitem_t *item, const char *req){
|
||||
if(!item->data) return RESULT_FAIL;
|
||||
char buf[1024];
|
||||
int no = sl_sock_keyno_check((sl_sock_keyno_t*)item->data);
|
||||
long long newval = -1;
|
||||
if(req){
|
||||
if(!sl_str2ll(&newval, req) || newval < 0 || newval > 0xffffffff) return RESULT_BADVAL;
|
||||
}
|
||||
printf("no = %d\n", no);
|
||||
if(no < 0){ // flags as a whole
|
||||
if(req) bitflags = (uint32_t)newval;
|
||||
snprintf(buf, 1023, "flags = 0x%08X\n", bitflags);
|
||||
sl_sock_sendstrmessage(s, buf);
|
||||
}else if(no < 32){ // bit access
|
||||
int bitmask = 1 << no;
|
||||
if(req){
|
||||
if(newval) bitflags |= bitmask;
|
||||
else bitflags &= ~bitmask;
|
||||
}
|
||||
snprintf(buf, 1023, "flags[%d] = %d\n", no, bitflags & bitmask ? 1 : 0);
|
||||
sl_sock_sendstrmessage(s, buf);
|
||||
}else return RESULT_BADKEY;
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
#endif
|
||||
|
||||
static sl_sock_hitem_t handlers[] = {
|
||||
#if 0
|
||||
{sl_sock_inthandler, "int", "set/get integer flag", (void*)&iflag},
|
||||
{sl_sock_dblhandler, "dbl", "set/get double flag", (void*)&dflag},
|
||||
{sl_sock_strhandler, "str", "set/get string variable", (void*)&sflag},
|
||||
{keyparhandler, "flags", "set/get bit flags as whole (flags=val) or by bits (flags[bit]=val)", (void*)&kph_number},
|
||||
{show, "show", "show current flags @ server console", NULL},
|
||||
#endif
|
||||
{openh, "open", "open dome", NULL},
|
||||
{closeh, "close", "close dome", NULL},
|
||||
{statush, "status", "get dome status", NULL},
|
||||
@@ -251,68 +211,49 @@ static sl_sock_hitem_t handlers[] = {
|
||||
|
||||
// dome polling; @return TRUE if all OK
|
||||
static int poll_device(){
|
||||
char line[256];
|
||||
if(write_cmd(TXT_GETWARN)){
|
||||
DBG("Can't write command warning");
|
||||
return FALSE;
|
||||
char ans[ANSLEN];
|
||||
char *data;
|
||||
if(!(data = term_cmdwans(TXT_GETWARN, TXT_ANS_ERR, ans))) return FALSE;
|
||||
DBG("Got status errno");
|
||||
int I;
|
||||
if(sscanf(data, "%d", &I) == 1){
|
||||
pthread_mutex_lock(&Dome.mutex);
|
||||
Dome.errcode = I;
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
DBG("errcode: %d", I);
|
||||
}
|
||||
if(write_cmd(TXT_GETSTAT)){
|
||||
DBG("Can't write command getstat");
|
||||
return FALSE;
|
||||
if(!(data = term_cmdwans(TXT_GETSTAT, TXT_ANS_STAT, ans))) return FALSE;
|
||||
DBG("Got status ans");
|
||||
if(sscanf(data, "%d", &I) == 1){
|
||||
pthread_mutex_lock(&Dome.mutex);
|
||||
if(I == 1111)
|
||||
snprintf(Dome.status, Dome.statlen, "opened");
|
||||
else if(I == 2222)
|
||||
snprintf(Dome.status, Dome.statlen, "closed");
|
||||
else
|
||||
snprintf(Dome.status, Dome.statlen, "intermediate");
|
||||
Dome.stattime = sl_dtime();
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
}
|
||||
if(write_cmd(TXT_GETWEAT)){
|
||||
DBG("Can't write command getweath");
|
||||
return FALSE;
|
||||
if(!(data = term_cmdwans(TXT_GETWEAT, TXT_ANS_WEAT, ans))) return FALSE;
|
||||
DBG("Got weather ans");
|
||||
if(sscanf(data, "%d", &I) == 1){
|
||||
pthread_mutex_lock(&Dome.mutex);
|
||||
if(I == 0)
|
||||
snprintf(Dome.weather, Dome.weathlen, "good");
|
||||
else if (I == 1)
|
||||
snprintf(Dome.weather, Dome.weathlen, "rain/clouds");
|
||||
else
|
||||
snprintf(Dome.weather, Dome.weathlen, "unknown");
|
||||
Dome.weathtime = sl_dtime();
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
}
|
||||
int l = 0;
|
||||
do{
|
||||
l = read_term(line, 256);
|
||||
if(l > 0) sl_RB_write(rb, (uint8_t*) line, l);
|
||||
}while(l > 0);
|
||||
pthread_mutex_lock(&Dome.mutex); // prepare user buffers
|
||||
// read ringbuffer, run parser and change buffers in `Dome`
|
||||
while(sl_RB_readline(rb, line, sizeof(line)) > 0){
|
||||
if(strncmp(line, TXT_ANS_STAT, strlen(TXT_ANS_STAT)) == 0){
|
||||
DBG("Got status ans");
|
||||
int stat;
|
||||
Dome.stattime = sl_dtime();
|
||||
if(sscanf(line + strlen(TXT_ANS_STAT), "%d", &stat) == 1){
|
||||
if(stat == 1111)
|
||||
snprintf(Dome.status, Dome.statlen, "opened");
|
||||
else if(stat == 2222)
|
||||
snprintf(Dome.status, Dome.statlen, "closed");
|
||||
else
|
||||
snprintf(Dome.status, Dome.statlen, "intermediate");
|
||||
}
|
||||
}else if(strncmp(line, TXT_ANS_ERR, strlen(TXT_ANS_ERR)) == 0){
|
||||
DBG("Got status errno");
|
||||
int ecode;
|
||||
if(sscanf(line + strlen(TXT_ANS_ERR), "%d", &ecode) == 1){
|
||||
Dome.errcode = ecode;
|
||||
DBG("errcode: %d", ecode);
|
||||
}
|
||||
}else if(strncmp(line, TXT_ANS_WEAT, strlen(TXT_ANS_WEAT)) == 0){
|
||||
DBG("Got weather ans");
|
||||
int weather;
|
||||
Dome.weathtime = sl_dtime();
|
||||
if(sscanf(line + strlen(TXT_ANS_WEAT), "%d", &weather) == 1){
|
||||
if(weather == 0)
|
||||
snprintf(Dome.weather, Dome.weathlen, "good weather");
|
||||
else if (weather == 1)
|
||||
snprintf(Dome.weather, Dome.weathlen, "rain or clouds");
|
||||
else
|
||||
snprintf(Dome.weather, Dome.weathlen, "unknown");
|
||||
}
|
||||
}else{
|
||||
DBG("Unknown answer: %s", line);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void runserver(int isunix, const char *node, int maxclients){
|
||||
int forbidden = 0;
|
||||
char ans[ANSLEN];
|
||||
if(locksock) sl_sock_delete(&locksock);
|
||||
if(rb) sl_RB_delete(&rb);
|
||||
rb = sl_RB_new(BUFSIZ);
|
||||
@@ -335,11 +276,14 @@ void runserver(int isunix, const char *node, int maxclients){
|
||||
while(locksock && locksock->connected){
|
||||
if(ForbidObservations){
|
||||
if(!forbidden){
|
||||
if(0 == write_cmd(TXT_CLOSEDOME)) forbidden = 1;
|
||||
pthread_mutex_lock(&Dome.mutex);
|
||||
Dome.cmd = CMD_NONE;
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
if(term_cmdwans(TXT_CLOSEDOME, TXT_ANS_MSGOK, ans)) forbidden = 1;
|
||||
}
|
||||
pthread_mutex_lock(&Dome.mutex);
|
||||
if(Dome.cmd != CMD_NONE){
|
||||
Dome.erroredcmd = Dome.cmd;
|
||||
Dome.cmd = CMD_NONE;
|
||||
}
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
}else forbidden = 0;
|
||||
usleep(1000);
|
||||
if(!locksock->rthread){
|
||||
@@ -347,28 +291,31 @@ void runserver(int isunix, const char *node, int maxclients){
|
||||
LOGERR("Server handlers thread is dead");
|
||||
break;
|
||||
}
|
||||
if(sl_dtime() - tgot > T_INTERVAL){
|
||||
if(poll_device()) tgot = sl_dtime();
|
||||
double tnow = sl_dtime();
|
||||
if(tnow - tgot > T_INTERVAL){
|
||||
if(poll_device()) tgot = tnow;
|
||||
}
|
||||
if(ForbidObservations) continue;
|
||||
pthread_mutex_lock(&Dome.mutex);
|
||||
if(Dome.cmd != CMD_NONE){
|
||||
switch(Dome.cmd){
|
||||
case CMD_OPEN:
|
||||
DBG("received command: open");
|
||||
if(0 == write_cmd(TXT_OPENDOME)) Dome.cmd = CMD_NONE;
|
||||
if(term_cmdwans(TXT_OPENDOME, TXT_ANS_MSGOK, ans)) Dome.cmd = CMD_NONE;
|
||||
break;
|
||||
case CMD_CLOSE:
|
||||
DBG("received command: close");
|
||||
if(0 == write_cmd(TXT_CLOSEDOME)) Dome.cmd = CMD_NONE;
|
||||
if(term_cmdwans(TXT_CLOSEDOME, TXT_ANS_MSGOK, ans)) Dome.cmd = CMD_NONE;
|
||||
break;
|
||||
case CMD_STOP:
|
||||
DBG("received command: stop");
|
||||
if(0 == write_cmd(TXT_STOPDOME)) Dome.cmd = CMD_NONE;
|
||||
if(term_cmdwans(TXT_STOPDOME, TXT_ANS_MSGOK, ans)) Dome.cmd = CMD_NONE;
|
||||
break;
|
||||
default:
|
||||
DBG("WTF?");
|
||||
}
|
||||
}
|
||||
Dome.erroredcmd = Dome.cmd; // if command didn't run last time, it will store here
|
||||
pthread_mutex_unlock(&Dome.mutex);
|
||||
}
|
||||
stopserver();
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
// size of weather/status buffers
|
||||
#define STATBUF_SZ 256
|
||||
// dome polling interval (clear watchdog & get status)
|
||||
#define T_INTERVAL (1.0)
|
||||
#define T_INTERVAL (5.0)
|
||||
|
||||
void runserver(int isunix, const char *node, int maxclients);
|
||||
void stopserver();
|
||||
|
||||
@@ -16,15 +16,17 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <usefull_macros.h>
|
||||
|
||||
#include "term.h"
|
||||
|
||||
static sl_tty_t *dev = NULL; // shoul be global to restore if die
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
// close serial device
|
||||
void close_term(){
|
||||
void term_close(){
|
||||
if(dev) sl_tty_close(&dev);
|
||||
}
|
||||
|
||||
@@ -35,19 +37,19 @@ void close_term(){
|
||||
* @param usec - timeout (us), if < 1e-6 - leave default
|
||||
* @return FALSE if failed
|
||||
*/
|
||||
int open_term(char *path, int speed, double usec){
|
||||
int term_open(char *path, int speed, double usec){
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(dev) sl_tty_close(&dev);
|
||||
LOGMSG("Try to open serial %s at speed %d", path, speed);
|
||||
DBG("Open serial");
|
||||
DBG("Init serial");
|
||||
dev = sl_tty_new(path, speed, 4096);
|
||||
DBG("Open serial");
|
||||
if(dev) dev = sl_tty_open(dev, 1);
|
||||
if(!dev){
|
||||
LOGERR("Can't open %s with speed %d. Exit.", path, speed);
|
||||
return FALSE;
|
||||
}
|
||||
if(!dev) goto rtn;
|
||||
DBG("Opened");
|
||||
if(usec >= 1e-6){
|
||||
DBG("set timeout to %gus", usec);
|
||||
if(!sl_tty_tmout(usec)){
|
||||
if(sl_tty_tmout(usec) < 0){
|
||||
LOGWARN("Can't set timeout to %gus", usec);
|
||||
WARNX("Can't set timeout to %gus", usec);
|
||||
}
|
||||
@@ -56,60 +58,129 @@ int open_term(char *path, int speed, double usec){
|
||||
LOGWARN("Can't set exact speed! Opened %s at speed %d", dev->portname, dev->speed);
|
||||
WARNX("Can't set speed %d (try %d)", speed, dev->speed);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
rtn:
|
||||
if(dev) return TRUE;
|
||||
LOGERR("Can't open %s with speed %d. Exit.", path, speed);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief read_term - read data from serial terminal
|
||||
* @param buf - buffer for data
|
||||
* @param length - size of `buf`
|
||||
* @return amount of data read
|
||||
* @brief nonblk_read - internal read
|
||||
* @param ans (o) - buffer for data
|
||||
* @return NULL if failed or `ans`
|
||||
*/
|
||||
int read_term(char *buf, int length){
|
||||
static char *nonblk_read(char ans[ANSLEN]){
|
||||
static char *bufptr = NULL; // last message, if it was longer than `length`
|
||||
static int lastL = 0;
|
||||
if(!dev || !buf || length < 1) return 0;
|
||||
FNAME();
|
||||
if(!dev) return NULL;
|
||||
int length = ANSLEN - 1;
|
||||
DBG("ok");
|
||||
if(!ans){ // clear
|
||||
DBG("clr");
|
||||
while(sl_tty_read(dev) > 0);
|
||||
bufptr = NULL;
|
||||
lastL = 0;
|
||||
return NULL;
|
||||
}
|
||||
if(bufptr && lastL){
|
||||
if(length > lastL) length = lastL;
|
||||
DBG("got %d bytes from old record", length);
|
||||
memcpy(buf, bufptr, length);
|
||||
memcpy(ans, bufptr, length);
|
||||
if((lastL -= length) < 1){
|
||||
lastL = 0; bufptr = NULL;
|
||||
}
|
||||
return length;
|
||||
ans[length] = 0;
|
||||
DBG("got %d bytes from old record: '%s'", length, ans);
|
||||
return ans;
|
||||
}
|
||||
if(!sl_tty_read(dev)) return 0;
|
||||
DBG("Got from serial %zd bytes", dev->buflen);
|
||||
LOGDBG("Got from serial: %zd bytes", dev->buflen);
|
||||
if(length >= (int)dev->buflen){
|
||||
DBG("Full buffer can be copied");
|
||||
length = (int)dev->buflen;
|
||||
bufptr = NULL;
|
||||
lastL = 0;
|
||||
}else{ // store part of data in buffer
|
||||
lastL = dev->buflen - length;
|
||||
DBG("Store %d bytes for next read", lastL);
|
||||
bufptr = dev->buf + length;
|
||||
int totlen = 0;
|
||||
while(length > 0 && sl_tty_read(dev) > 0){
|
||||
int lmax = length;
|
||||
if(lmax >= (int)dev->buflen){
|
||||
DBG("Full buffer can be copied");
|
||||
lmax = (int)dev->buflen;
|
||||
}else{ // store part of data in buffer
|
||||
lastL = dev->buflen - lmax;
|
||||
DBG("Store %d bytes for next read", lastL);
|
||||
bufptr = dev->buf + lmax;
|
||||
}
|
||||
memcpy(ans + totlen, dev->buf, lmax);
|
||||
length -= lmax;
|
||||
totlen += lmax;
|
||||
ans[totlen] = 0;
|
||||
}
|
||||
memcpy(buf, dev->buf, length);
|
||||
return length;
|
||||
DBG("totlen: %d", totlen);
|
||||
if(totlen == 0) return NULL;
|
||||
DBG("copied %d: '%s'", totlen, ans);
|
||||
return ans;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief write_term - write data
|
||||
* @param buf - buffer
|
||||
* @brief nonblk_write - internal write
|
||||
* @param buf (i) - data to write
|
||||
* @param length - its length
|
||||
* @return 0 if OK and 1 if failed
|
||||
* @return 0 if failed or `length`
|
||||
*/
|
||||
int write_term(const char *buf, int length){
|
||||
if(!dev || !buf || length < 1) return 0;
|
||||
return sl_tty_write(dev->comfd, buf, length);
|
||||
static int nonblk_write(const char *buf, int length){
|
||||
if(!buf || length < 1) return 0;
|
||||
if(0 == sl_tty_write(dev->comfd, buf, length)) return length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// write string command
|
||||
int write_cmd(const char *buf){
|
||||
if(!buf || !*buf) return 0;
|
||||
DBG("Ask to write %s", buf);
|
||||
return write_term(buf, strlen(buf));
|
||||
/**
|
||||
* @brief term_read - read string from terminal
|
||||
* @param ans (o) - buffer or NULL to clear last data
|
||||
* @return NULL or pointer to zero-terminated `ans`
|
||||
*/
|
||||
char *term_read(char ans[ANSLEN]){
|
||||
pthread_mutex_lock(&mutex);
|
||||
char *ret = nonblk_read(ans);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
DBG("read: '%s'", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief term_write - write data and got answer
|
||||
* @param str (i) - zero-terminated string to write
|
||||
* @param ans (o) - NULL to clear incoming data or buffer to read
|
||||
* @return
|
||||
*/
|
||||
char *term_write(const char *str, char ans[ANSLEN]){
|
||||
if(!str || !*str) return NULL;
|
||||
DBG("Send cmd '%s'", str);
|
||||
char *ret = NULL;
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(nonblk_write(str, strlen(str))){
|
||||
usleep(USLEEP_BEFORE_READ);
|
||||
ret = nonblk_read(ans);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
DBG("read: '%s'", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief term_cmdwans - write command and get answer
|
||||
* @param str (i) - command to write
|
||||
* @param prefix (i) - prefix of answer (string should begin with this text)
|
||||
* @param ans (o) - buffer for answer (non-NULL!!!)
|
||||
* @return pointer to data just after `prefix` in `ans` or NULL if no `prefix` found
|
||||
*/
|
||||
char *term_cmdwans(const char *str, const char *prefix, char ans[ANSLEN]){
|
||||
if(!str || !prefix || !ans) return NULL;
|
||||
DBG("Send cmd '%s'", str);
|
||||
char *ret = NULL;
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(nonblk_write(str, strlen(str))){
|
||||
usleep(USLEEP_BEFORE_READ);
|
||||
ret = nonblk_read(ans);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
int l = strlen(prefix);
|
||||
DBG("compare %s with %s", ret ? (ret) : "null", prefix);
|
||||
if(!ret || strncmp(ans, prefix, l)) return NULL; // no answer or not found
|
||||
DBG("found");
|
||||
return ans + l;
|
||||
}
|
||||
|
||||
@@ -18,19 +18,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// text commands and answers
|
||||
#define TXT_GETWARN "d#warning\n"
|
||||
#define TXT_OPENDOME "d#opendom\n"
|
||||
#define TXT_CLOSEDOME "d#closdom\n"
|
||||
#define TXT_STOPDOME "d#stopdom\n"
|
||||
#define TXT_GETSTAT "d#get_dom\n"
|
||||
#define TXT_GETWEAT "d#ask_wea\n"
|
||||
#define TXT_ANS_WEAT "d#wea"
|
||||
#define TXT_ANS_STAT "d#pos"
|
||||
#define TXT_ANS_ERR "d#erro"
|
||||
#include <stdint.h>
|
||||
|
||||
int open_term(char *path, int speed, double usec);
|
||||
void close_term();
|
||||
int read_term(char *buf, int length);
|
||||
int write_term(const char *buf, int length);
|
||||
int write_cmd(const char *buf);
|
||||
// pause before reading answer: for stupid baader = 50ms
|
||||
#define USLEEP_BEFORE_READ 50000
|
||||
|
||||
// length of answer (including terminating zero)
|
||||
#define ANSLEN 128
|
||||
|
||||
int term_open(char *path, int speed, double usec);
|
||||
void term_close();
|
||||
char *term_read(char ans[ANSLEN]);
|
||||
char *term_write(const char *str, char ans[ANSLEN]);
|
||||
char *term_cmdwans(const char *str, const char *prefix, char ans[ANSLEN]);
|
||||
|
||||
Reference in New Issue
Block a user