forgot to commit a long ago

This commit is contained in:
Edward Emelianov 2022-03-24 12:15:24 +03:00
parent 0b30f59a48
commit 5d254b4171
9 changed files with 281 additions and 232 deletions

View File

@ -1,7 +1,8 @@
PROGRAM = bta_control
LDFLAGS = -lcrypt -lm -lsla
SRCS = bta_shdata.c bta_control.c cmdlnopts.c parceargs.c usefull_macros.c ch4run.c
SRCS += angle_functions.c bta_print.c
#SRCS = bta_shdata.c bta_control.c cmdlnopts.c parceargs.c usefull_macros.c ch4run.c
#SRCS += angle_functions.c bta_print.c
SRCS = $(wildcard *.c)
CC = gcc
DEFINES = -D_XOPEN_SOURCE=666 -DEBUG
#-DEMULATION

View File

@ -160,7 +160,7 @@ char *get_degrees(double *ret, char *str){
int res;
while(*str){ // check sign & omit leading shit
char c = *str;
if(c > '0'-1 && c < '9'+1) break;
if(c >= '0' && c <= '9') break;
++str;
if(c == '+') break;
if(c == '-'){ sign = -1.; break;}

View File

@ -19,11 +19,10 @@
* MA 02110-1301, USA.
*/
#define _GNU_SOURCE 666 // for strcasestr
#include <assert.h>
#include <signal.h>
#include <strings.h>
#include <string.h>
#include <pthread.h>
#include <signal.h> // pthread_kill
#include <math.h>
#include "bta_shdata.h"
@ -34,48 +33,21 @@
#include "angle_functions.h"
#include "bta_print.h"
#ifndef PIDFILE
#define PIDFILE "/tmp/bta_control.pid"
#endif
// constants for choosing move/goto (move for near objects)
const double Amove = 1800.; // +-30'
const double Zmove = 3600.; // +-60'
// arcseconds to radians
#define AS2R (M_PI/180./3600.)
glob_pars *GP = NULL;
#define PRINT(...) do{if(!GP->quiet) printf(__VA_ARGS__);}while(0)
// ACS command wrapper
#ifdef EMULATION
#define ACS_CMD(a) do{green(#a); printf("\n");}while(0)
#else
//#define ACS_CMD(a) do{red(#a); printf("\n");}while(0)
#define ACS_CMD(a) do{red(#a); printf("\n");}while(0)
// Uncomment only in final release
#define ACS_CMD(a) do{DBG(#a "\n"); a; }while(0)
//#define ACS_CMD(a) do{DBG(#a "\n"); a; }while(0)
#endif
#ifndef EMULATION
typedef struct{
uint32_t keylev;
uint32_t codelev;
} passhash;
#endif
void signals(int sig){
if(sig)
WARNX(_("Get signal %d, quit.\n"), sig);
else
sig = -1;
unlink(PIDFILE);
restore_console();
exit(sig);
}
volatile int tmout = 0;
pthread_t athread;
void *tmout_thread(void *buf){
@ -115,83 +87,20 @@ void set_timeout(int delay){
tmout = 1;
return;
}
;
}
char indi[] = "|/-\\";
char *iptr = indi;
#define WAIT_EVENT(evt, max_delay) do{int __ = 0; set_timeout(max_delay); \
PRINT(" "); while(!tmout && !(evt)){ \
usleep(100000); if(!*(++iptr)) iptr = indi; if(++__%10==0) PRINT("\b. "); \
PRINT("\b%c", *iptr);}; PRINT("\n");}while(0)
#ifndef EMULATION
void get_passhash(passhash *p){
int fd = -1, i, c, nlev = 0;
char *filename = GP->passfile;
if(filename){ // user give filename with [stored?] hash
struct stat statbuf;
if((fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
ERR(_("Can't open %s for reading"), filename);
if(fstat (fd, &statbuf) < 0)
ERR(_("Can't stat %s"), filename);
if(!S_ISREG(statbuf.st_mode))
ERR(_("%s is not regular file"), filename);
if(statbuf.st_mode != (S_IRUSR | S_IWUSR)){ // wrong mode
if(chmod(filename, S_IRUSR | S_IWUSR))
ERR(_("Can't change permissions of %s to 0600"), filename);
}
if(8 == read(fd, p, 8)){ // check password, if it is good, return
for(i = 5; i > 0; --i){
if(p->codelev == code_Lev(i)) break;
}
if(i){
set_acckey(p->keylev);
close(fd);
return;
}
}
}
// ask user to enter password
setup_con(); // hide echo
for(i = 3; i > 0; --i){ // try 3 times
char pass[256]; int k = 0;
printf("Enter password, you have %d tr%s\n", i, (i > 1) ? "ies":"y");
while ((c = mygetchar()) != '\n' && c != EOF && k < 255){
if(c == '\b' || c == 127){ // use DEL and BACKSPACE to erase previous symbol
if(k) --k;
printf("\b \b");
}else{
pass[k++] = c;
printf("*");
}
fflush(stdout);
}
pass[k] = 0;
printf("\n");
if((nlev = find_lev_passwd(pass, &p->keylev, &p->codelev)))
break;
printf(_("No, not this\n"));
}
restore_console();
if(nlev == 0)
ERRX(_("Tries excess!"));
set_acckey(p->keylev);
DBG("OK, level %d", nlev);
if(fd > 0){
PRINT(_("Store\n"));
if(0 != lseek(fd, 0, SEEK_SET)){
WARN(_("Can't seek to start of %s"), filename);
}else if(8 != write(fd, p, 8))
WARN(_("Can't store password hash in %s"), filename);
close(fd);
}
}
#endif // EMULATION
/***************************************************************
* All functions for changing telescope parameters are boolean *
* returning TRUE in case of succsess or FALSE if failed *
***************************************************************/
#ifndef WAIT_EVENT
#define WAIT_EVENT(evt, max_delay) do{int __ = 0; set_timeout(max_delay); \
PRINT(" "); while(!tmout && !(evt)){ \
usleep(100000); if(!*(++iptr)) iptr = indi; if(++__%10==0) PRINT("\b. "); \
PRINT("\b%c", *iptr);}; PRINT("\n");}while(0)
#endif
/**
* move P2 to the given angle relative to current position +- P2_ANGLE_THRES
@ -255,12 +164,12 @@ bool moveP2(char *arg){
if(!get_degrees(&p2angle, arg)) badarg = 1;
else{ // now check if there a good angle
if(p2angle < -360. || p2angle > 360.) badarg = 1;
else if(eptr){
/* else if(eptr){
if(strcasecmp(eptr, "rel") == 0)
p2rel = 1;
else // wrong degrees format
badarg = 1;
}
}*/
}
bdrg:
if(badarg){
@ -295,7 +204,9 @@ bdrg:
}
#endif
}
int p2oldmode = P2_Mode;
#ifndef EMULATION
_U_ int p2oldmode = P2_Mode;
#endif
ACS_CMD(SetPMode(P2_Off));
DBG("Move P2 to %gdegr", p2angle);
if(fabs(p2angle - p2val) < P2_ANGLE_THRES){
@ -361,6 +272,9 @@ void cmd_Fmoveto(double f){
return;
}
fdt = fabs(fshift) / fvel;
#ifdef EMULATION
printf("Move focus with speed %g''/s for %gseconds\n", fvel, fdt);
#endif
ACS_CMD(MoveFocus(fspeed, fdt));
#ifndef EMULATION
DBG("dt: %g, fvel: %g, fstate: %d, F:%g", fdt, vel_F, Foc_State, val_F);
@ -624,26 +538,19 @@ bool run_correction(char *dxdy, bool isAZ){
if(!myatod(&dx, &eptr) || !*eptr || !*(++eptr)) goto badang;
if(!myatod(&dy, &eptr)) goto badang;
DBG("dx: %g, dy: %g", dx, dy);
if(!testauto() || dx > CORR_MAX_ANGLE || dy > CORR_MAX_ANGLE) return FALSE;
if(!testauto()) return FALSE;
if(fabs(dx) > CORR_MAX_ANGLE || fabs(dy) > CORR_MAX_ANGLE){
WARNX(_("Angle should be from %d'' to %d''!"), -CORR_MAX_ANGLE, CORR_MAX_ANGLE);
return FALSE;
}
#ifndef EMULATION
int32_t oldmode = Sys_Mode;
#endif
if(isAZ){
dx /= sin(val_Z * AS2R); // transform dA to "telescope coordinates"
#ifndef EMULATION
//double targA = val_A+dx, targZ = val_Z+dy;
#endif
ACS_CMD(DoAZcorr(dx, dy));
#ifndef EMULATION
PRINT(_("Wait for A/Z correction ends"));
//WAIT_EVENT((fabs(val_A - targA) < CORR_THRES && fabs(val_Z - targZ) < CORR_THRES), 10.);
#endif
}else{
#ifndef EMULATION
//double targA = val_Alp+dx, targD = val_Del+dy;
#endif
ACS_CMD(DoADcorr(dx, dy));
#ifndef EMULATION
//WAIT_EVENT((fabs(val_Alp - targA) < CORR_THRES && fabs(val_Del - targD) < CORR_THRES), 10.);
#endif
}
#ifndef EMULATION
PRINT(_("Wait for correction starts"));
@ -662,91 +569,3 @@ badang:
WARNX(_("Bad format, need \"dx,dy\" in arcseconds"));
return FALSE;
}
int main(int argc, char **argv){
check4running(argv, PIDFILE, NULL);
int retcode = 0;
initial_setup();
info_level showinfo = NO_INFO;
#ifndef EMULATION
passhash pass = {0,0};
#endif
int needblock = 0, needqueue = 0;
GP = parce_args(argc, argv);
assert(GP);
signal(SIGTERM, signals); // kill (-15) - quit
signal(SIGHUP, signals); // hup - quit
signal(SIGINT, signals); // ctrl+C - quit
signal(SIGQUIT, signals); // ctrl+\ - quit
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
setbuf(stdout, NULL);
if(GP->getinfo){
needblock = 1;
char *infostr = GP->getinfo;
if(strcmp(infostr, "1") == 0){ // show ALL args
showinfo = ALL_INFO;
}else{
showinfo = get_infolevel(infostr);
}
}
if(showinfo == NO_INFO){
if(GP->infoargs){
showinfo = REQUESTED_LIST;
needblock = 1;
}else if(GP->getinfo) show_infolevels();
}
if(GP->p2move || GP->p2mode || GP->focmove > 0. || GP->eqcrds || GP->horcrds
|| GP->azrev || GP->telstop || GP->gotoRaDec || GP->gotoAZ || GP->PCSoff
|| GP->corrAZ || GP->corrRAD){
needqueue = 1;
}
if(needqueue){
needblock = 1;
}
if(needblock){
if(!get_shm_block(&sdat, ClientSide))
ERRX(_("Can't find shared memory block"));
}
if(needqueue) get_cmd_queue(&ucmd, ClientSide);
if(needblock){
if(!check_shm_block(&sdat))
ERRX(_("There's no connection to BTA!"));
#ifndef EMULATION
double last = M_time;
PRINT(_("Test multicast connection\n"));
WAIT_EVENT((fabs(M_time - last) > 0.02), 5);
if(tmout)
ERRX(_("Multicasts stale!"));
if(needqueue) get_passhash(&pass);
#endif
}
if(showinfo != NO_INFO) bta_print(showinfo, GP->infoargs);
else if(GP->listinfo) bta_print(NO_INFO, NULL); // show arguments available
#define RUN(arg) do{if(!arg) retcode = 1;}while(0)
#define RUNBLK(arg) do{if(!arg){retcode = 1; goto restoring;}}while(0)
if(GP->telstop) RUN(stop_telescope());
if(GP->eqcrds) RUNBLK(setCoords(GP->eqcrds, TRUE));
else if(GP->horcrds) RUNBLK(setCoords(GP->horcrds, FALSE));
if(GP->p2move) RUN(moveP2(GP->p2move));
if(GP->p2mode) RUN(setP2mode(GP->p2mode));
if(GP->focmove > 0.) RUN(moveFocus(GP->focmove));
if(GP->azrev) RUN(azreverce());
if(GP->PCSoff) RUNBLK(PCS_state(FALSE));
else if(needqueue) RUNBLK(PCS_state(TRUE));
if(GP->gotoRaDec) RUNBLK(gotopos(TRUE));
else if(GP->gotoAZ) RUNBLK(gotopos(FALSE));
else if(GP->corrAZ) RUN(run_correction(GP->corrAZ, TRUE));
else if(GP->corrRAD) RUN(run_correction(GP->corrRAD, FALSE));
#undef RUN
#undef RUNBLK
restoring:
unlink(PIDFILE);
restore_console();
return retcode;
}
/*
* Добавить:
* коррекция положения по A/Z или RA/Dec
*/

View File

@ -22,6 +22,40 @@
#ifndef __BTA_CONTROL_H__
#define __BTA_CONTROL_H__
#include <stdint.h>
#include <stdbool.h>
#include "cmdlnopts.h"
#ifndef EMULATION
typedef struct{
uint32_t keylev;
uint32_t codelev;
} passhash;
#endif
extern glob_pars *GP;
void set_timeout(int delay);
extern volatile int tmout;
extern char *iptr;
extern char indi[];
bool moveP2(char *arg);
bool setP2mode(char *arg);
bool moveFocus(double val);
bool setCoords(char *coords, bool isEQ);
bool azreverce();
bool stop_telescope();
bool gotopos(bool isradec);
bool PCS_state(bool on);
bool run_correction(char *dxdy, bool isAZ);
#define WAIT_EVENT(evt, max_delay) do{int __ = 0; set_timeout(max_delay); \
PRINT(" "); while(!tmout && !(evt)){ \
usleep(100000); if(!*(++iptr)) iptr = indi; if(++__%10==0) PRINT("\b. "); \
PRINT("\b%c", *iptr);}; PRINT("\n");}while(0)
#define PRINT(...) do{if(!GP->quiet) printf(__VA_ARGS__);}while(0)
// timeout for system messages process - 3 seconds
#define WAITING_TMOUT (3.)
// end-switches position (in degr); prohibited angles are in range [P2_LOW_ES .. P2_HIGH_ES]
@ -29,8 +63,8 @@
#define P2_HIGH_ES (90.0)
// minimal P2 moving time
#define P2_MINTIME (4.5)
// fast speed - 10arcmin per second
#define P2_FAST_SPEED (600.)
// fast speed - 45arcmin per second
#define P2_FAST_SPEED (2700.)
// this value will be substituted from calculated rotation time in case of moving on large speed
#define P2_FAST_T_CORR (1.5)
// angle threshold (for p2 move) in degrees

View File

@ -137,6 +137,9 @@ typedef enum{
PAR_CorrAzim,
PAR_CorrZenD,
PAR_Foc_State,
PAR_polarX,
PAR_polarY,
PAR_DUT1,
PAR_ValTout,
PAR_ValTind,
PAR_ValTmir,
@ -620,6 +623,10 @@ int bta_print(info_level lvl, char *par_list){
}
}
SMSG(Foc_State, "focus motor state", value);
FMSG(polarX, "X polar motion", "%g", polarX);
FMSG(polarY, "Y polar motion", "%g", polarY);
FMSG(DUT1, "UT1 - UTC", "%g", DUT1);
}
/************************** METEO_INFO ****************************************/
if(lvl & METEO_INFO){

View File

@ -76,33 +76,14 @@ void iffound_default(pid_t pid){
* @param pidfilename - name of pidfile or NULL if none
* @param iffound - action to run if file found or NULL for exit(0)
*/
void check4running(char **argv, char *pidfilename, void (*iffound)(pid_t pid)){
void check4running(char *pidfilename, void (*iffound)(pid_t pid)){
DIR *dir;
FILE *pidfile, *fself;
FILE *pidfile;
struct dirent *de;
struct stat s_buf;
pid_t pid = 0, self;
struct flock fl;
char *name, *myname;
if(!iffound) iffound = iffound_default;
if(argv){ // block self
fself = fopen(argv[0], "r"); // open self binary to lock
memset(&fl, 0, sizeof(struct flock));
fl.l_type = F_WRLCK;
if(fcntl(fileno(fself), F_GETLK, &fl) == -1){ // check locking
perror("fcntl");
exit(1);
}
if(fl.l_type != F_UNLCK){ // file is locking - exit
printf("Found locker, PID = %d!\n", fl.l_pid);
exit(1);
}
fl.l_type = F_RDLCK;
if(fcntl(fileno(fself), F_SETLKW, &fl) == -1){
perror("fcntl");
exit(1);
}
}
self = getpid(); // get self PID
if(!(dir = opendir(PROC_BASE))){ // open /proc directory
perror(PROC_BASE);

View File

@ -25,4 +25,4 @@
#include <unistd.h> // pid_t
void iffound_default(pid_t pid);
void check4running(char **argv, char *pidfilename, void (*iffound)(pid_t pid));
void check4running(char *pidfilename, void (*iffound)(pid_t pid));

207
main.c Normal file
View File

@ -0,0 +1,207 @@
/*
* main.c
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#define _GNU_SOURCE 666 // for strcasestr
/*
#include <strings.h>
#include <string.h>
#include <pthread.h>
#include "angle_functions.h"
*/
#include <assert.h>
#include <signal.h>
#include <math.h>
#include "bta_control.h"
#include "ch4run.h"
#include "cmdlnopts.h"
#include "usefull_macros.h"
#include "bta_print.h"
#include "bta_shdata.h"
glob_pars *GP = NULL;
#ifndef PIDFILE
#define PIDFILE "/tmp/bta_control.pid"
#endif
void signals(int sig){
if(sig)
WARNX(_("Get signal %d, quit.\n"), sig);
else
sig = -1;
unlink(PIDFILE);
restore_console();
exit(sig);
}
#ifndef EMULATION
void get_passhash(passhash *p){
int fd = -1, i, c, nlev = 0;
char *filename = GP->passfile;
if(filename){ // user give filename with [stored?] hash
struct stat statbuf;
if((fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
ERR(_("Can't open %s for reading"), filename);
if(fstat (fd, &statbuf) < 0)
ERR(_("Can't stat %s"), filename);
if(!S_ISREG(statbuf.st_mode))
ERR(_("%s is not regular file"), filename);
if(statbuf.st_mode != (S_IRUSR | S_IWUSR)){ // wrong mode
if(chmod(filename, S_IRUSR | S_IWUSR))
ERR(_("Can't change permissions of %s to 0600"), filename);
}
if(8 == read(fd, p, 8)){ // check password, if it is good, return
for(i = 5; i > 0; --i){
if(p->codelev == code_Lev(i)) break;
}
if(i){
set_acckey(p->keylev);
close(fd);
return;
}
}
}
// ask user to enter password
setup_con(); // hide echo
for(i = 3; i > 0; --i){ // try 3 times
char pass[256]; int k = 0;
printf("Enter password, you have %d tr%s\n", i, (i > 1) ? "ies":"y");
while ((c = mygetchar()) != '\n' && c != EOF && k < 255){
if(c == '\b' || c == 127){ // use DEL and BACKSPACE to erase previous symbol
if(k) --k;
printf("\b \b");
}else{
pass[k++] = c;
printf("*");
}
fflush(stdout);
}
pass[k] = 0;
printf("\n");
if((nlev = find_lev_passwd(pass, &p->keylev, &p->codelev)))
break;
printf(_("No, not this\n"));
}
restore_console();
if(nlev == 0)
ERRX(_("Tries excess!"));
set_acckey(p->keylev);
DBG("OK, level %d", nlev);
if(fd > 0){
PRINT(_("Store\n"));
if(0 != lseek(fd, 0, SEEK_SET)){
WARN(_("Can't seek to start of %s"), filename);
}else if(8 != write(fd, p, 8))
WARN(_("Can't store password hash in %s"), filename);
close(fd);
}
}
#endif // EMULATION
int main(int argc, char **argv){
check4running(PIDFILE, NULL);
int retcode = 0;
initial_setup();
info_level showinfo = NO_INFO;
#ifndef EMULATION
passhash pass = {0,0};
#endif
int needblock = 0, needqueue = 0;
GP = parce_args(argc, argv);
assert(GP);
signal(SIGTERM, signals); // kill (-15) - quit
signal(SIGHUP, signals); // hup - quit
signal(SIGINT, signals); // ctrl+C - quit
signal(SIGQUIT, signals); // ctrl+\ - quit
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
setbuf(stdout, NULL);
if(GP->getinfo){
needblock = 1;
char *infostr = GP->getinfo;
if(strcmp(infostr, "1") == 0){ // show ALL args
showinfo = ALL_INFO;
}else{
showinfo = get_infolevel(infostr);
}
}
if(showinfo == NO_INFO){
if(GP->infoargs){
showinfo = REQUESTED_LIST;
needblock = 1;
}else if(GP->getinfo){
show_infolevels();
return 0;
}
}
if(GP->p2move || GP->p2mode || GP->focmove > 0. || GP->eqcrds || GP->horcrds
|| GP->azrev || GP->telstop || GP->gotoRaDec || GP->gotoAZ || GP->PCSoff
|| GP->corrAZ || GP->corrRAD){
needqueue = 1;
}
if(needqueue){
needblock = 1;
}
if(needblock){
if(!get_shm_block(&sdat, ClientSide))
ERRX(_("Can't find shared memory block"));
}
if(needqueue) get_cmd_queue(&ucmd, ClientSide);
if(needblock){
if(!check_shm_block(&sdat))
ERRX(_("There's no connection to BTA!"));
#ifndef EMULATION
double last = M_time;
PRINT(_("Test multicast connection\n"));
WAIT_EVENT((fabs(M_time - last) > 0.02), 5);
if(tmout)
ERRX(_("Multicasts stale!"));
if(needqueue) get_passhash(&pass);
#endif
}
if(showinfo != NO_INFO) bta_print(showinfo, GP->infoargs);
else if(GP->listinfo) bta_print(NO_INFO, NULL); // show arguments available
#define RUN(arg) do{if(!arg) retcode = 1;}while(0)
#define RUNBLK(arg) do{if(!arg){retcode = 1; goto restoring;}}while(0)
if(GP->telstop) RUN(stop_telescope());
if(GP->eqcrds) RUNBLK(setCoords(GP->eqcrds, TRUE));
else if(GP->horcrds) RUNBLK(setCoords(GP->horcrds, FALSE));
if(GP->p2move) RUN(moveP2(GP->p2move));
if(GP->p2mode) RUN(setP2mode(GP->p2mode));
if(GP->focmove > 0.) RUN(moveFocus(GP->focmove));
if(GP->azrev) RUN(azreverce());
if(GP->PCSoff) RUNBLK(PCS_state(FALSE));
else if(needqueue) RUNBLK(PCS_state(TRUE));
if(GP->gotoRaDec) RUNBLK(gotopos(TRUE));
else if(GP->gotoAZ) RUNBLK(gotopos(FALSE));
else if(GP->corrAZ) RUN(run_correction(GP->corrAZ, TRUE));
else if(GP->corrRAD) RUN(run_correction(GP->corrRAD, FALSE));
#undef RUN
#undef RUNBLK
restoring:
unlink(PIDFILE);
restore_console();
return retcode;
}

0
slalib/libsla.so Executable file → Normal file
View File