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:
42
Daemons/teldaemon_astrosib/commands.h
Normal file
42
Daemons/teldaemon_astrosib/commands.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is part of the teldaemon 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_FOCUSABS "FOCUSERGO?"
|
||||
#define TXT_FOCUSIN "FOCUSERI?"
|
||||
#define TXT_FOCUSOUT "FOCUSERO?"
|
||||
#define TXT_FOCSTOP "FOCUSERSTOP?\r"
|
||||
#define TXT_FOCGET "FOCUSERGPOS?\r"
|
||||
#define TXT_OPEN "SHUTTEROPEN?1,1,1,1,1\r"
|
||||
#define TXT_CLOSE "SHUTTERCLOSE?1,1,1,1,1\r"
|
||||
#define TXT_STATUS "SHUTTERSTATUS?\r"
|
||||
#define TXT_COOLERON "COOLERON?100\r"
|
||||
#define TXT_COOLEROFF "COOLEROFF?\r"
|
||||
#define TXT_COOLERT "COOLERT?\r"
|
||||
#define TXT_COOLERSTAT "COOLERSTATUS?\r"
|
||||
#define TXT_PING "PING?\r"
|
||||
#define TXT_ANS_OK "OK"
|
||||
#define TXT_ANS_COOLERSTAT "OK\rCOOLERPWM?"
|
||||
#define TXT_ANS_COOLERT "OK\rCOOLERT?"
|
||||
#define TXT_ANS_STATUS "OK\rSHUTTERS?"
|
||||
#define TXT_ANS_FOCPOS "OK\rFOCUSERPOS?"
|
||||
|
||||
#define FOC_MINPOS 0
|
||||
#define FOC_MAXPOS 65000
|
||||
@@ -41,7 +41,8 @@ typedef struct{
|
||||
|
||||
static parameters G = {
|
||||
.maxclients = 2,
|
||||
.serspeed = 9600
|
||||
.serspeed = 9600,
|
||||
.sertmout = 1000.,
|
||||
};
|
||||
|
||||
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"
|
||||
|
||||
@@ -37,6 +38,7 @@ typedef enum{
|
||||
|
||||
typedef struct{
|
||||
tel_commands_t cmd; // deferred command
|
||||
tel_commands_t errcmd; // command ended with error
|
||||
int focuserpos; // focuser position
|
||||
char *status; // device status
|
||||
int statlen; // size of `status` buffer
|
||||
@@ -54,7 +56,6 @@ static tel_t telescope = {0};
|
||||
static atomic_bool ForbidObservations = 0; // ==1 if all is forbidden -> close dome and not allow to open
|
||||
|
||||
static sl_sock_t *locksock = NULL; // local server socket
|
||||
static sl_ringbuffer_t *rb = NULL; // incoming serial data
|
||||
|
||||
// args of absolute/relative focus move commands
|
||||
static sl_sock_int_t Absmove = {0};
|
||||
@@ -62,7 +63,6 @@ static sl_sock_int_t Relmove = {0};
|
||||
|
||||
void stopserver(){
|
||||
if(locksock) sl_sock_delete(&locksock);
|
||||
if(rb) sl_RB_delete(&rb);
|
||||
}
|
||||
|
||||
// send "measuret=..."
|
||||
@@ -106,20 +106,37 @@ static sl_sock_hresult_e fstoph(_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;
|
||||
double t = NAN, mirt, ambt;
|
||||
tel_commands_t lastecmd;
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
if(!*telescope.status || sl_dtime() - telescope.stattime > 3.*T_INTERVAL) snprintf(buf, 255, "%s=unknown\n", item->key);
|
||||
else{
|
||||
snprintf(buf, 255, "%s=%s\n", item->key, telescope.status);
|
||||
t = telescope.stattime;
|
||||
}
|
||||
mirt = telescope.mirrortemp;
|
||||
ambt = telescope.ambienttemp;
|
||||
lastecmd = telescope.errcmd;
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
if(!isnan(t)) sendtmeasured(client, t);
|
||||
snprintf(buf, 255, "mirrortemp=%.1f\n", telescope.mirrortemp);
|
||||
snprintf(buf, 255, "mirrortemp=%.1f\n", mirt);
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
snprintf(buf, 255, "ambienttemp=%.1f\n", telescope.ambienttemp);
|
||||
snprintf(buf, 255, "ambienttemp=%.1f\n", ambt);
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
if(lastecmd != CMD_NONE){
|
||||
const char *tcmd;
|
||||
switch(lastecmd){
|
||||
case CMD_OPEN: tcmd = "open"; break;
|
||||
case CMD_CLOSE: tcmd = "close"; break;
|
||||
case CMD_FOCSTOP: tcmd = "focstop"; break;
|
||||
case CMD_COOLERON: case CMD_COOLEROFF: tcmd = "cooler"; break;
|
||||
default: tcmd = "unknown";
|
||||
}
|
||||
snprintf(buf, 255, "errored_command=%s\n", tcmd);
|
||||
sl_sock_sendstrmessage(client, buf);
|
||||
|
||||
}
|
||||
if(ForbidObservations) sl_sock_sendstrmessage(client, "FORBIDDEN\n");
|
||||
return RESULT_SILENCE;
|
||||
}
|
||||
@@ -192,13 +209,6 @@ static sl_sock_hresult_e defhandler(struct sl_sock *s, const char *str){
|
||||
}
|
||||
|
||||
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
|
||||
{sl_sock_inthandler, "focrel", "relative focus move", (void*)&Relmove},
|
||||
{sl_sock_inthandler, "focabs", "absolute focus move", (void*)&Absmove},
|
||||
{foch, "focpos", "current focuser position", NULL},
|
||||
@@ -211,74 +221,54 @@ static sl_sock_hitem_t handlers[] = {
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
static void serial_parser(){
|
||||
char line[256];
|
||||
int l = 0;
|
||||
do{
|
||||
l = read_term(line, 256);
|
||||
if(l > 0){
|
||||
if(l != (int)sl_RB_write(rb, (uint8_t*) line, l)) break;
|
||||
}
|
||||
}while(l > 0);
|
||||
|
||||
pthread_mutex_lock(&telescope.mutex); // prepare user buffers
|
||||
// read ringbuffer, run parser and change buffers in `telescope`
|
||||
while((l = sl_RB_readto(rb, '\r', (uint8_t*)line, sizeof(line))) > 0){
|
||||
line[l-1] = 0; // substitute '\r' with 0
|
||||
DBG("IN: %s", line);
|
||||
if(strncmp(line, TXT_ANS_STATUS, strlen(TXT_ANS_STATUS)) == 0){
|
||||
DBG("Got status ans");
|
||||
telescope.stattime = sl_dtime();
|
||||
char *s = line + strlen(TXT_ANS_STATUS);
|
||||
if(strncmp(s, "0,0,0,0,0", 9) == 0) snprintf(telescope.status, telescope.statlen, "closed");
|
||||
else if(strncmp(s, "1,1,1,1,1", 9) == 0) snprintf(telescope.status, telescope.statlen, "opened");
|
||||
else snprintf(telescope.status, telescope.statlen, "intermediate");
|
||||
}else if(strncmp(line, TXT_ANS_COOLERSTAT, strlen(TXT_ANS_COOLERSTAT)) == 0){
|
||||
DBG("Got cooler status");
|
||||
int s;
|
||||
if(sscanf(line + strlen(TXT_ANS_COOLERSTAT), "%d", &s) == 1){
|
||||
telescope.cooler = s;
|
||||
}
|
||||
}else if(strncmp(line, TXT_ANS_COOLERT, strlen(TXT_ANS_COOLERT)) == 0){
|
||||
DBG("Got weather ans");
|
||||
float m, a;
|
||||
if(sscanf(line + strlen(TXT_ANS_COOLERT), "%f,%f", &m, &a) == 2){
|
||||
telescope.ambienttemp = a;
|
||||
telescope.mirrortemp = m;
|
||||
}
|
||||
}else if(strncmp(line, TXT_ANS_FOCPOS, strlen(TXT_ANS_FOCPOS)) == 0){
|
||||
DBG("Got focuser position");
|
||||
int p;
|
||||
if(sscanf(line + strlen(TXT_ANS_FOCPOS), "%d", &p) == 1){
|
||||
telescope.focuserpos = p;
|
||||
}
|
||||
}else{
|
||||
DBG("Unknown answer: %s", line);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
}
|
||||
|
||||
// dome polling; @return TRUE if all OK
|
||||
static int poll_device(){
|
||||
static const char *reqcmds[] = {TXT_FOCGET, TXT_STATUS, TXT_COOLERT, TXT_COOLERSTAT, NULL};
|
||||
for(const char **cmd = reqcmds; *cmd; ++cmd){
|
||||
if(write_cmd(*cmd)){
|
||||
LOGWARN("Can't write command %s", *cmd);
|
||||
DBG("Can't write command %s", *cmd);
|
||||
return FALSE;
|
||||
}
|
||||
serial_parser();
|
||||
char ans[ANSLEN];
|
||||
char *data;
|
||||
int I;
|
||||
|
||||
if(!(data = term_cmdwans(TXT_FOCGET, TXT_ANS_FOCPOS, ans))) return FALSE;
|
||||
DBG("\nGot focuser position");
|
||||
if(sscanf(data, "%d", &I) == 1){
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
telescope.focuserpos = I;
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
}
|
||||
|
||||
if(!(data = term_cmdwans(TXT_STATUS, TXT_ANS_STATUS, ans))) return FALSE;
|
||||
DBG("\nGot status");
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
telescope.stattime = sl_dtime();
|
||||
if(strncmp(data, "0,0,0,0,0", 9) == 0) snprintf(telescope.status, telescope.statlen, "closed");
|
||||
else if(strncmp(data, "1,1,1,1,1", 9) == 0) snprintf(telescope.status, telescope.statlen, "opened");
|
||||
else snprintf(telescope.status, telescope.statlen, "intermediate");
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
|
||||
if(!(data = term_cmdwans(TXT_COOLERT, TXT_ANS_COOLERT, ans))) return FALSE;
|
||||
DBG("\nGot weather ans");
|
||||
float m, a;
|
||||
if(sscanf(data, "%f,%f", &m, &a) == 2){
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
telescope.ambienttemp = a;
|
||||
telescope.mirrortemp = m;
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
}
|
||||
|
||||
if(!(data = term_cmdwans(TXT_COOLERSTAT, TXT_ANS_COOLERSTAT, ans))) return FALSE;
|
||||
DBG("\nGot cooler status");
|
||||
if(sscanf(data, "%d", &I) == 1){
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
telescope.cooler = I;
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void runserver(int isunix, const char *node, int maxclients){
|
||||
char ans[ANSLEN];
|
||||
int forbidden = 0;
|
||||
if(locksock) sl_sock_delete(&locksock);
|
||||
if(rb) sl_RB_delete(&rb);
|
||||
rb = sl_RB_new(BUFSIZ);
|
||||
telescope.cmd = CMD_NONE;
|
||||
telescope.errcmd = telescope.cmd = CMD_NONE;
|
||||
FREE(telescope.status);
|
||||
telescope.statlen = STATBUF_SZ;
|
||||
telescope.status = MALLOC(char, STATBUF_SZ);
|
||||
@@ -294,7 +284,7 @@ void runserver(int isunix, const char *node, int maxclients){
|
||||
while(locksock && locksock->connected){
|
||||
if(ForbidObservations){
|
||||
if(!forbidden){
|
||||
if(0 == write_cmd(TXT_CLOSE)) forbidden = 1;
|
||||
if(term_cmdwans(TXT_CLOSE, TXT_ANS_OK, ans)) forbidden = 1;
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
telescope.cmd = CMD_NONE;
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
@@ -310,33 +300,38 @@ void runserver(int isunix, const char *node, int maxclients){
|
||||
if(tnow - tgot > T_INTERVAL){
|
||||
if(poll_device()) tgot = tnow;
|
||||
}
|
||||
if(ForbidObservations) continue;
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
if(telescope.cmd != CMD_NONE){
|
||||
switch(telescope.cmd){
|
||||
tel_commands_t tcmd = telescope.cmd;
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
if(tcmd != CMD_NONE){
|
||||
switch(tcmd){
|
||||
case CMD_OPEN:
|
||||
DBG("received command: open");
|
||||
if(0 == write_cmd(TXT_OPEN)) telescope.cmd = CMD_NONE;
|
||||
if(term_cmdwans(TXT_OPEN, TXT_ANS_OK, ans)) tcmd = CMD_NONE;
|
||||
break;
|
||||
case CMD_CLOSE:
|
||||
DBG("received command: close");
|
||||
if(0 == write_cmd(TXT_CLOSE)) telescope.cmd = CMD_NONE;
|
||||
if(term_cmdwans(TXT_CLOSE, TXT_ANS_OK, ans)) tcmd = CMD_NONE;
|
||||
break;
|
||||
case CMD_FOCSTOP:
|
||||
DBG("received command: stop focus");
|
||||
if(0 == write_cmd(TXT_FOCSTOP)) telescope.cmd = CMD_NONE;
|
||||
term_write(TXT_FOCSTOP, ans); tcmd = CMD_NONE; // erroreous thing: no answer for this command
|
||||
break;
|
||||
case CMD_COOLEROFF:
|
||||
DBG("received command: cooler off");
|
||||
if(0 == write_cmd(TXT_COOLEROFF)) telescope.cmd = CMD_NONE;
|
||||
if(term_cmdwans(TXT_COOLEROFF, TXT_ANS_OK, ans)) tcmd = CMD_NONE;
|
||||
break;
|
||||
case CMD_COOLERON:
|
||||
DBG("received command: cooler on");
|
||||
if(0 == write_cmd(TXT_COOLERON)) telescope.cmd = CMD_NONE;
|
||||
if(term_cmdwans(TXT_COOLERON, TXT_ANS_OK, ans)) tcmd = CMD_NONE;
|
||||
break;
|
||||
default:
|
||||
DBG("WTF?");
|
||||
}
|
||||
}
|
||||
pthread_mutex_lock(&telescope.mutex);
|
||||
telescope.cmd = telescope.errcmd = tcmd;
|
||||
pthread_mutex_unlock(&telescope.mutex);
|
||||
// check abs/rel move
|
||||
char buf[256];
|
||||
@@ -345,7 +340,7 @@ void runserver(int isunix, const char *node, int maxclients){
|
||||
if(Absmove.val < FOC_MINPOS || Absmove.val > FOC_MAXPOS) Absmove.timestamp = 0.; // reset wrong data
|
||||
else{
|
||||
snprintf(buf, 255, "%s%d\r", TXT_FOCUSABS, (int)Absmove.val);
|
||||
if(0 == write_cmd(buf)){
|
||||
if(term_cmdwans(buf, TXT_ANS_OK, ans)){
|
||||
DBG("STARTED absmove");
|
||||
Absmove.timestamp = 0.;
|
||||
}
|
||||
@@ -360,13 +355,12 @@ void runserver(int isunix, const char *node, int maxclients){
|
||||
DBG("pos=%d", pos);
|
||||
if(pos < 0){ cmd = TXT_FOCUSIN; pos = -pos; }
|
||||
snprintf(buf, 255, "%s%d\r", cmd, pos);
|
||||
if(0 == write_cmd(buf)){
|
||||
if(term_cmdwans(buf, TXT_ANS_OK, ans)){
|
||||
DBG("STARTED relmove");
|
||||
Relmove.timestamp = 0.;
|
||||
}
|
||||
}
|
||||
}
|
||||
serial_parser();
|
||||
}
|
||||
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 (2.0)
|
||||
|
||||
void runserver(int isunix, const char *node, int maxclients);
|
||||
void stopserver();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
commands.h
|
||||
main.c
|
||||
socket.c
|
||||
socket.h
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the teldaemon project.
|
||||
* 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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the teldaemon project.
|
||||
* 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
|
||||
@@ -18,31 +18,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// text commands and answers
|
||||
#define TXT_FOCUSABS "FOCUSERGO?"
|
||||
#define TXT_FOCUSIN "FOCUSERI?"
|
||||
#define TXT_FOCUSOUT "FOCUSERO?"
|
||||
#define TXT_FOCSTOP "FOCUSERSTOP?\r"
|
||||
#define TXT_FOCGET "FOCUSERGPOS?\r"
|
||||
#define TXT_OPEN "SHUTTEROPEN?1,1,1,1,1\r"
|
||||
#define TXT_CLOSE "SHUTTERCLOSE?1,1,1,1,1\r"
|
||||
#define TXT_STATUS "SHUTTERSTATUS?\r"
|
||||
#define TXT_COOLERON "COOLERON?100\r"
|
||||
#define TXT_COOLEROFF "COOLEROFF?\r"
|
||||
#define TXT_COOLERT "COOLERT?\r"
|
||||
#define TXT_COOLERSTAT "COOLERSTATUS?\r"
|
||||
#define TXT_PING "PING?\r"
|
||||
#define TXT_ANS_OK "OK"
|
||||
#define TXT_ANS_COOLERSTAT "COOLERPWM?"
|
||||
#define TXT_ANS_COOLERT "COOLERT?"
|
||||
#define TXT_ANS_STATUS "SHUTTERSTATUS?"
|
||||
#define TXT_ANS_FOCPOS "FOCUSERPOS?"
|
||||
#include <stdint.h>
|
||||
|
||||
#define FOC_MINPOS 0
|
||||
#define FOC_MAXPOS 65000
|
||||
// pause before reading answer: for stupid baader = 50ms
|
||||
#define USLEEP_BEFORE_READ 50000
|
||||
|
||||
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);
|
||||
// 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