add to stellarium daemon debug port @10001

This commit is contained in:
Edward Emelianov 2020-03-02 08:28:53 +03:00
parent bd09ecef90
commit 33185e174a
11 changed files with 226 additions and 109 deletions

View File

@ -35,6 +35,7 @@ glob_pars G;
#define DEFAULT_COMDEV "/dev/ttyUSB0" #define DEFAULT_COMDEV "/dev/ttyUSB0"
// port for connections // port for connections
#define DEFAULT_PORT "10000" #define DEFAULT_PORT "10000"
#define DEFAULT_DBGPORT "10001"
// accept only local connections // accept only local connections
//#define ACCEPT_IP "192.168.3.225" //#define ACCEPT_IP "192.168.3.225"
// default PID filename: // default PID filename:
@ -47,6 +48,7 @@ glob_pars G;
glob_pars const Gdefault = { glob_pars const Gdefault = {
.device = DEFAULT_COMDEV, .device = DEFAULT_COMDEV,
.port = DEFAULT_PORT, .port = DEFAULT_PORT,
.dbgport = DEFAULT_DBGPORT,
.pidfile = DEFAULT_PIDFILE, .pidfile = DEFAULT_PIDFILE,
.crdsfile = DEFAULT_FITSHDR, .crdsfile = DEFAULT_FITSHDR,
.emulation = 0, .emulation = 0,
@ -67,6 +69,7 @@ myoption cmdlnopts[] = {
{"hdrfile", NEED_ARG, NULL, 'o', arg_string, APTR(&G.crdsfile), _("file to save FITS-header with coordinates and time")}, {"hdrfile", NEED_ARG, NULL, 'o', arg_string, APTR(&G.crdsfile), _("file to save FITS-header with coordinates and time")},
{"pidfile", NEED_ARG, NULL, 'P', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")}, {"pidfile", NEED_ARG, NULL, 'P', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")},
{"port", NEED_ARG, NULL, 'p', arg_string, APTR(&G.port), _("port to connect (default: " DEFAULT_PORT ")")}, {"port", NEED_ARG, NULL, 'p', arg_string, APTR(&G.port), _("port to connect (default: " DEFAULT_PORT ")")},
{"dbgport", NEED_ARG, NULL, 'D', arg_string, APTR(&G.dbgport), _("port to connect for debug console (default: " DEFAULT_DBGPORT ")")},
end_option end_option
}; };

View File

@ -31,6 +31,7 @@
typedef struct{ typedef struct{
char *device; // serial device name char *device; // serial device name
char *port; // port to connect char *port; // port to connect
char *dbgport; // port for debug console
char *pidfile; // name of PID file char *pidfile; // name of PID file
char *logfile; // logging to this file char *logfile; // logging to this file
char *crdsfile; // file where FITS-header should be written char *crdsfile; // file where FITS-header should be written

View File

@ -62,8 +62,8 @@ int getPlace(placeData *p){
int getWeath(placeWeather *w){ int getWeath(placeWeather *w){
if(!w) return 0; if(!w) return 0;
w->relhum = 0.7; w->relhum = 0.7;
w->tc = 0.; w->tc = 1.;
w->php = 780.; w->php = 78.; // temporary, to fix bug of firmware
return 0; return 0;
} }
int getDUT(almDut *a){ int getDUT(almDut *a){

View File

@ -40,31 +40,30 @@
extern void check4running(char *self, char *pidfilename, void (*iffound)(pid_t pid)); extern void check4running(char *self, char *pidfilename, void (*iffound)(pid_t pid));
// Max amount of connections // Max amount of connections
#define BACKLOG (1) #define BACKLOG (10)
#define BUFLEN (1024) #define BUFLEN (1024)
// pause for incoming message waiting (out coordinates sent after that timeout) // pause for incoming message waiting (out coordinates sent after that timeout)
#define SOCK_TMOUT (1) #define SOCK_TMOUT (1)
static uint8_t buff[BUFLEN+1];
// global parameters // global parameters
static glob_pars *GP = NULL; static glob_pars *GP = NULL;
static pid_t childpid = 1; // PID of child process static pid_t childpid = 1; // PID of child process
static volatile int global_quit = 0; volatile int global_quit = 0;
// quit by signal // quit by signal
void signals(int sig){ void signals(int sig){
signal(sig, SIG_IGN); signal(sig, SIG_IGN);
unlink(GP->crdsfile); // remove header file unlink(GP->crdsfile); // remove header file
unlink(GP->pidfile); // and remove pidfile
if(childpid){ // parent process if(childpid){ // parent process
restore_console(); restore_console();
restore_tty(); restore_tty();
unlink(GP->pidfile); // and remove pidfile
} }
DBG("Get signal %d, quit.\n", sig); DBG("Get signal %d, quit.\n", sig);
global_quit = 1; global_quit = 1;
sleep(1); sleep(1);
if(childpid) putlog("PID %d exit with status %d after child's %d death", getpid(), sig, childpid); if(childpid) putlog("PID %d exit with status %d after child's %d death", getpid(), sig, childpid);
else WARN("Child %d died with %d", getpid(), sig); else WARNX("Child %d died with %d", getpid(), sig);
exit(sig); exit(sig);
} }
@ -260,7 +259,6 @@ int proc_data(uint8_t *data, ssize_t len){
void *handle_socket(void *sockd){ void *handle_socket(void *sockd){
FNAME(); FNAME();
if(global_quit) return NULL; if(global_quit) return NULL;
ssize_t rd;
outdata dout; outdata dout;
int sock = *(int*)sockd; int sock = *(int*)sockd;
dout.len = htole16(sizeof(outdata)); dout.len = htole16(sizeof(outdata));
@ -294,7 +292,8 @@ void *handle_socket(void *sockd){
} }
if(!(FD_ISSET(sock, &readfds))) continue; if(!(FD_ISSET(sock, &readfds))) continue;
// fill incoming buffer // fill incoming buffer
rd = read(sock, buff, BUFLEN); uint8_t buff[BUFLEN+1];
ssize_t rd = read(sock, buff, BUFLEN);
buff[rd] = 0; buff[rd] = 0;
DBG("read %zd (%s)", rd, buff); DBG("read %zd (%s)", rd, buff);
if(rd <= 0){ // error or disconnect if(rd <= 0){ // error or disconnect
@ -311,64 +310,74 @@ void *handle_socket(void *sockd){
return NULL; return NULL;
} }
// thread writing FITS-header file
static void *hdrthread(_U_ void *buf){ static void *hdrthread(_U_ void *buf){
// write FITS-header at most once per second // write FITS-header at most once per second
do{ while(!global_quit){
wrhdr(); wrhdr();
usleep(1000); // give a chanse to write/read for others usleep(1000); // give a chanse to write/read for others
}while(1); }
return NULL; return NULL;
} }
static inline void main_proc(){ /**
int sock; * @brief opensocket - open socket to port `port`
* @return socket fd or <0 if failed
*/
static int opensocket(char *port){
if(!port) return -1;
int reuseaddr = 1; int reuseaddr = 1;
pthread_t hthrd; int sock;
// connect to telescope
if(!GP->emulation){
if(!connect_telescope(GP->device, GP->crdsfile)){
ERRX(_("Can't connect to telescope device"));
}
if(pthread_create(&hthrd, NULL, hdrthread, NULL))
ERR(_("Can't create writing thread"));
}
// open socket
struct addrinfo hints, *res, *p; struct addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
DBG("try to open port %s", GP->port); DBG("try to open port %s", port);
if(getaddrinfo(NULL, GP->port, &hints, &res) != 0){ if(getaddrinfo(NULL, port, &hints, &res) != 0){
ERR("getaddrinfo"); WARN("getaddrinfo()");
return 0;
} }
/*
struct sockaddr_in *ia = (struct sockaddr_in*)res->ai_addr; struct sockaddr_in *ia = (struct sockaddr_in*)res->ai_addr;
char str[INET_ADDRSTRLEN]; char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ia->sin_addr), str, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(ia->sin_addr), str, INET_ADDRSTRLEN);
*/
// loop through all the results and bind to the first we can // loop through all the results and bind to the first we can
for(p = res; p != NULL; p = p->ai_next){ for(p = res; p != NULL; p = p->ai_next){
if((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){ if((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){
WARN("socket"); WARN("socket()");
continue; continue;
} }
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1){ if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1){
ERR("setsockopt"); WARN("setsockopt()");
close(sock);
continue;
} }
if(bind(sock, p->ai_addr, p->ai_addrlen) == -1){ if(bind(sock, p->ai_addr, p->ai_addrlen) == -1){
WARN("bind()");
close(sock); close(sock);
WARN("bind");
continue; continue;
} }
break; // if we get here, we must have connected successfully break; // if we get here, we must have connected successfully
} }
freeaddrinfo(res);
// Listen // Listen
if(listen(sock, BACKLOG) == -1){ if(listen(sock, BACKLOG) == -1){
WARN("listen");
putlog("listen() error"); putlog("listen() error");
ERR("listen");
} }
DBG("listen at %s", GP->port); DBG("listen at %s", port);
putlog("listen at %s", GP->port); putlog("listen at %s", port);
//freeaddrinfo(res); return sock;
}
/**
* @brief waitconn
* @param sock - socket fd to accept()
* @param connthread - thread which to run when connection accepted (it's parameter - socket fd)
*/
static void waitconn(int sock, void *(*connthread)(void*)){
// Main loop // Main loop
while(!global_quit){ while(!global_quit){
socklen_t size = sizeof(struct sockaddr_in); socklen_t size = sizeof(struct sockaddr_in);
@ -377,45 +386,81 @@ static inline void main_proc(){
newsock = accept(sock, (struct sockaddr*)&myaddr, &size); newsock = accept(sock, (struct sockaddr*)&myaddr, &size);
if(newsock <= 0){ if(newsock <= 0){
WARN("accept()"); WARN("accept()");
sleep(1);
continue; continue;
} }
struct sockaddr_in peer; struct sockaddr_in peer;
socklen_t peer_len = sizeof(peer); socklen_t peer_len = sizeof(peer);
if (getpeername(newsock, &peer, &peer_len) == -1) { if(getpeername(newsock, (struct sockaddr*)&peer, &peer_len) == -1){
WARN("getpeername()"); WARN("getpeername()");
close(newsock); close(newsock);
continue; continue;
} }
int sockport = -1;
if(getsockname(newsock, (struct sockaddr*)&peer, &peer_len) == 0){
sockport = ntohs(peer.sin_port);
}
char *peerIP = inet_ntoa(peer.sin_addr); char *peerIP = inet_ntoa(peer.sin_addr);
putlog("Got connection from %s", peerIP); putlog("Got connection from %s @ %d", peerIP, sockport);
DBG("Peer's IP address is: %s\n", peerIP); DBG("Peer's IP address is: %s (@port %d)\n", peerIP, sockport);
/*if(strcmp(peerIP, ACCEPT_IP) && strcmp(peerIP, "127.0.0.1")){ /*if(strcmp(peerIP, ACCEPT_IP) && strcmp(peerIP, "127.0.0.1")){
WARNX("Wrong IP"); WARNX("Wrong IP");
close(newsock); close(newsock);
continue; continue;
}*/ }*/
//handle_socket(newsock);
pthread_t rthrd; pthread_t rthrd;
if(pthread_create(&rthrd, NULL, handle_socket, (void*)&newsock)){ if(pthread_create(&rthrd, NULL, connthread, (void*)&newsock)){
putlog("Error creating listen thread"); putlog("Error creating listen thread");
ERR(_("Can't create socket thread")); ERR(_("Can't create socket thread"));
}else{ }else{
DBG("Thread created, detouch"); DBG("Thread created, detouch");
pthread_detach(rthrd); // don't care about thread state pthread_detach(rthrd); // don't care about thread state
} }
} }
pthread_cancel(hthrd); // cancel reading thread
pthread_join(hthrd, NULL);
close(sock); close(sock);
} }
// thread working with terminal
static void *termthread(_U_ void *buf){
int sock = opensocket(GP->dbgport);
if(sock < 0){
putlog("Can't open debugging socket @ port %s", GP->dbgport);
ERRX("Can't open debug socket");
}
waitconn(sock, term_thread);
return NULL;
}
static inline void main_proc(){
pthread_t hthrd, termthrd;
// connect to telescope
if(!GP->emulation){
if(!connect_telescope(GP->device, GP->crdsfile)){
ERRX(_("Can't connect to telescope device"));
}
if(pthread_create(&hthrd, NULL, hdrthread, NULL))
ERR(_("Can't create writing thread"));
if(pthread_create(&termthrd, NULL, termthread, NULL))
ERR(_("Can't create terminal thread"));
}
// open socket
int sock = opensocket(GP->port);
if(sock < 0){
putlog("Can't open socket @ port %s", GP->port);
ERRX("Can't open stellarium socket");
}
waitconn(sock, handle_socket);
usleep(10000);
pthread_cancel(hthrd); // cancel reading thread
pthread_cancel(termthrd);
pthread_join(hthrd, NULL);
pthread_join(termthrd, NULL);
}
int main(int argc, char **argv){ int main(int argc, char **argv){
char *self = strdup(argv[0]); char *self = strdup(argv[0]);
GP = parse_args(argc, argv); GP = parse_args(argc, argv);
initial_setup(); initial_setup();
check4running(self, GP->pidfile, NULL);
if(GP->logfile) openlogfile(GP->logfile);
signal(SIGTERM, signals); // kill (-15) - quit signal(SIGTERM, signals); // kill (-15) - quit
signal(SIGKILL, signals); // kill (-9) - quit signal(SIGKILL, signals); // kill (-9) - quit
@ -429,14 +474,16 @@ int main(int argc, char **argv){
ERR(_("Can't open %s for writing"), GP->crdsfile); ERR(_("Can't open %s for writing"), GP->crdsfile);
close(fd); close(fd);
printf(_("Start socket\n")); printf("Daemonize\n");
putlog("Starting, master PID=%d", getpid());
#ifndef EBUG // daemonize only in release mode #ifndef EBUG // daemonize only in release mode
if(daemon(1, 0)){ if(daemon(1, 0)){
putlog("Err: daemon()"); putlog("Err: daemon()");
ERR("daemon()"); ERR("daemon()");
} }
#endif // EBUG #endif // EBUG
check4running(self, GP->pidfile, NULL);
if(GP->logfile) openlogfile(GP->logfile);
putlog("Starting, master PID=%d", getpid());
while(1){ while(1){
childpid = fork(); childpid = fork();

View File

@ -38,5 +38,6 @@
// global parameters // global parameters
extern glob_pars *Global_parameters; extern glob_pars *Global_parameters;
// global quit flag
extern volatile int global_quit;
#endif // __MAIN_H__ #endif // __MAIN_H__

View File

@ -20,9 +20,13 @@
* MA 02110-1301, USA. * MA 02110-1301, USA.
* *
*/ */
#include <arpa/inet.h> // ntoa
#include <netinet/in.h> // ntoa
#include <pthread.h> #include <pthread.h>
#include <sys/socket.h> // getpeername
#include "libsofa.h" #include "libsofa.h"
#include "main.h" // global_quit
#include "telescope.h" #include "telescope.h"
#include "usefull_macros.h" #include "usefull_macros.h"
@ -35,7 +39,6 @@
#define WAIT_TMOUT (0.01) #define WAIT_TMOUT (0.01)
#endif #endif
#define BUFLEN 80 #define BUFLEN 80
static char *hdname = NULL; static char *hdname = NULL;
@ -45,6 +48,7 @@ static int Target = 0; // target coordinates entered
/** /**
* read strings from terminal (ending with '\n') with timeout * read strings from terminal (ending with '\n') with timeout
* @return NULL if nothing was read or pointer to static buffer * @return NULL if nothing was read or pointer to static buffer
* THREAD UNSAFE!
*/ */
static char *read_string(){ static char *read_string(){
static char buf[BUFLEN]; static char buf[BUFLEN];
@ -78,20 +82,25 @@ static char *read_string(){
/** /**
* write command, thread-safe * write command, thread-safe
* @param cmd (i) - command to write
* @param buff (o) - buffer (WHICH SIZE = BUFLEN!!!) to which write data (or NULL if don't need)
* @return answer or NULL if error occured (or no answer) * @return answer or NULL if error occured (or no answer)
* WARNING!!! data returned is allocated by strdup! You MUST free it when don't need
*/ */
static char *write_cmd(const char *cmd){ static char *write_cmd(const char *cmd, char *buff){
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
//DBG("Write %s", cmd); //DBG("Write %s", cmd);
if(write_tty(cmd, strlen(cmd))) return NULL; if(write_tty(cmd, strlen(cmd))) return NULL;
double t0 = dtime(); double t0 = dtime();
static char *ans; char *ans;
while(dtime() - t0 < T_POLLING_TMOUT){ // read answer while(dtime() - t0 < T_POLLING_TMOUT){ // read answer
if((ans = read_string())){ // parse new data if((ans = read_string())){ // parse new data
// DBG("got answer: %s", ans); // DBG("got answer: %s", ans);
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
return ans; if(!buff) return NULL;
strncpy(buff, ans, BUFLEN-1);
return buff;
} }
} }
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
@ -99,48 +108,60 @@ static char *write_cmd(const char *cmd){
} }
// write to telescope mount corrections: datetime, pressure and temperature // write to telescope mount corrections: datetime, pressure and temperature
static void makecorr(){ // @return 1 if time was corrected
static int makecorr(){
int ret = 0;
// write current date&time // write current date&time
char buf[64], *ans; char buf[64], ibuff[BUFLEN], *ans;
DBG("curtime: %s", write_cmd(":GUDT#")); DBG("curtime: %s", write_cmd(":GUDT#", ibuff));
write_cmd(":gT#"); // correct time by GPS ans = write_cmd(":Gstat#", ibuff);
ans = write_cmd(":gtg#"); /*
if(!ans || *ans != '1'){ * there's no GPS on this mount and there's no need for it!
WARNX("mount don't synchronized with GPS! Refresh datetime"); write_cmd(":gT#", NULL); // correct time by GPS
time_t t = time(NULL); ans = write_cmd(":gtg#", ibuff);
struct tm *stm = localtime(&t); */
struct timeval tv;
gettimeofday(&tv,NULL); if(!ans || *ans == '0'){ // system is in tracking or unknown state - don't update data!
snprintf(buf, 64, ":SLDT%04d-%02d-%02d,%02d:%02d:%02d.%02ld#", 1900+stm->tm_year, stm->tm_mon+1, stm->tm_mday, return 0;
stm->tm_hour, stm->tm_min, stm->tm_sec, tv.tv_usec/10000);
DBG("write: %s", buf);
ans = write_cmd(buf);
if(!ans || *ans != '1'){
WARNX("Can't write current date/time");
putlog("Can't set system time");
}else putlog("Set system time by command %s", buf);
DBG("curtime: %s", write_cmd(":GUDT#"));
} }
WARNX("Refresh datetime");
time_t t = time(NULL);
struct tm *stm = localtime(&t);
struct timeval tv;
gettimeofday(&tv,NULL);
snprintf(buf, 64, ":SLDT%04d-%02d-%02d,%02d:%02d:%02d.%02ld#", 1900+stm->tm_year, stm->tm_mon+1, stm->tm_mday,
stm->tm_hour, stm->tm_min, stm->tm_sec, tv.tv_usec/10000);
DBG("write: %s", buf);
ans = write_cmd(buf, ibuff);
if(!ans || *ans != '1'){
WARNX("Can't write current date/time");
putlog("Can't set system time");
}else{
putlog("Set system time by command %s", buf);
ret = 1;
}
DBG("curtime: %s", write_cmd(":GUDT#", ibuff));
placeWeather w; placeWeather w;
if(getWeath(&w)) putlog("Can't determine weather data"); if(getWeath(&w)) putlog("Can't determine weather data");
else{ // set refraction model data else{ // set refraction model data
snprintf(buf, 64, ":SRPRS%.1f#", w.php); snprintf(buf, 64, ":SRPRS%.1f#", w.php);
ans = write_cmd(buf); ans = write_cmd(buf, ibuff);
if(!ans || *ans != '1') putlog("Can't set pressure data of refraction model"); if(!ans || *ans != '1') putlog("Can't set pressure data of refraction model");
else putlog("Correct pressure to %g", w.php); else putlog("Correct pressure to %g", w.php);
snprintf(buf, 64, ":SRTMP%.1f#", w.tc); snprintf(buf, 64, ":SRTMP%.1f#", w.tc);
ans = write_cmd(buf); ans = write_cmd(buf, ibuff);
if(!ans || *ans != '1') putlog("Can't set temperature data of refraction model"); if(!ans || *ans != '1') putlog("Can't set temperature data of refraction model");
else putlog("Correct temperature to %g", w.tc); else putlog("Correct temperature to %g", w.tc);
} }
return ret;
} }
int chkconn(){ int chkconn(){
char tmpbuf[4096]; char tmpbuf[4096];
read_tty(tmpbuf, 4096); // clear rbuf read_tty(tmpbuf, 4096); // clear rbuf
write_cmd("#"); // clear cmd buffer write_cmd("#", NULL); // clear cmd buffer
if(!write_cmd(":SB0#")) return 0; // 115200 if(!write_cmd(":SB0#", tmpbuf)) return 0; // 115200
//if(!write_cmd(":GR#")) return 0; //if(!write_cmd(":GR#")) return 0;
return 1; return 1;
} }
@ -167,14 +188,16 @@ int connect_telescope(char *dev, char *hdrname){
tty_init(dev, B115200); tty_init(dev, B115200);
if(!chkconn()) return 0; if(!chkconn()) return 0;
} }
write_cmd(":U2#"); write_cmd("#", NULL); // clear previous buffer
write_cmd(":U2#"); // set high precision write_cmd(":STOP#", NULL); // stop tracking after poweron
write_cmd(":So10#"); // set minimum altitude to 10 degrees write_cmd(":U2#", NULL); // set high precision
write_cmd(":So10#", NULL); // set minimum altitude to 10 degrees
putlog("Connected to %s@115200, will write FITS-header into %s", dev, hdrname); putlog("Connected to %s@115200, will write FITS-header into %s", dev, hdrname);
FREE(hdname);
hdname = strdup(hdrname); hdname = strdup(hdrname);
DBG("connected"); DBG("connected");
Target = 0; Target = 0;
write_cmd(":gT#"); // correct time by GPS write_cmd(":gT#", NULL); // correct time by GPS
return 1; return 1;
} }
@ -195,7 +218,7 @@ int point_telescope(double ra, double dec){
ptDECdeg = dec; ptDECdeg = dec;
Target = 0; Target = 0;
int err = 0; int err = 0;
static char buf[80]; char buf[80], ibuff[BUFLEN];
char sign = '+'; char sign = '+';
if(dec < 0){ if(dec < 0){
sign = '-'; sign = '-';
@ -212,19 +235,19 @@ int point_telescope(double ra, double dec){
int dm = (int)dec; int dm = (int)dec;
dec -= dm; dec *= 60.; dec -= dm; dec *= 60.;
snprintf(buf, 80, ":Sr%d:%d:%.2f#", h,m,ra); snprintf(buf, 80, ":Sr%d:%d:%.2f#", h,m,ra);
char *ans = write_cmd(buf); char *ans = write_cmd(buf, ibuff);
if(!ans || *ans != '1'){ if(!ans || *ans != '1'){
err = 1; err = 1;
goto ret; goto ret;
} }
snprintf(buf, 80, ":Sd%c%d:%d:%.1f#", sign,d,dm,dec); snprintf(buf, 80, ":Sd%c%d:%d:%.1f#", sign,d,dm,dec);
ans = write_cmd(buf); ans = write_cmd(buf, ibuff);
if(!ans || *ans != '1'){ if(!ans || *ans != '1'){
err = 2; err = 2;
goto ret; goto ret;
} }
DBG("Move"); DBG("Move");
ans = write_cmd(":MS#"); ans = write_cmd(":MS#", ibuff);
if(!ans || *ans != '0'){ if(!ans || *ans != '0'){
putlog("move error, answer: %s", ans); putlog("move error, answer: %s", ans);
err = 3; err = 3;
@ -314,8 +337,9 @@ int get_telescope_coords(double *ra, double *decl){
} }
void stop_telescope(){ void stop_telescope(){
write_cmd(":RT9#"); // stop tracking write_cmd(":RT9#", NULL); // stop tracking
write_cmd(":STOP#"); // halt moving write_cmd(":AL#", NULL); // stop tracking
write_cmd(":STOP#", NULL); // halt moving
Target = 0; Target = 0;
} }
@ -347,24 +371,24 @@ static char *dups(const char *buf, int astr){
} }
static void getplace(){ static void getplace(){
char *ans; char *ans, ibuff[BUFLEN];
if(!elevation){ if(!elevation){
ans = write_cmd(":Gev#"); ans = write_cmd(":Gev#", ibuff);
elevation = dups(ans, 0); elevation = dups(ans, 0);
} }
if(!longitude){ if(!longitude){
ans = write_cmd(":Gg#"); ans = write_cmd(":Gg#", ibuff);
longitude = dups(ans, 1); longitude = dups(ans, 1);
} }
if(!latitude){ if(!latitude){
ans = write_cmd(":Gt#"); ans = write_cmd(":Gt#", ibuff);
latitude = dups(ans, 1); latitude = dups(ans, 1);
} }
} }
static const char *statuses[12] = { static const char *statuses[12] = {
[0] = "'Tracking'", [0] = "'Tracking'",
[1] = "'Going to stop'", [1] = "'Stoped or homing'",
[2] = "'Slewing to park'", [2] = "'Slewing to park'",
[3] = "'Unparking'", [3] = "'Unparking'",
[4] = "'Slewing to home'", [4] = "'Slewing to home'",
@ -396,12 +420,13 @@ void wrhdr(){
static int failcounter = 0; static int failcounter = 0;
static time_t lastcorr = 0; // last time of corrections made static time_t lastcorr = 0; // last time of corrections made
if(time(NULL) - lastcorr > CORRECTIONS_TIMEDIFF){ if(time(NULL) - lastcorr > CORRECTIONS_TIMEDIFF){
lastcorr = time(NULL); if(makecorr()) lastcorr = time(NULL);
makecorr(); else lastcorr += 30; // failed -> check 30s later
} }
char *ans = NULL, *jd = NULL, *lst = NULL, *date = NULL, *pS = NULL; char *ans = NULL, *jd = NULL, *lst = NULL, *date = NULL, *pS = NULL;
char ibuff[BUFLEN];
// get coordinates for writing to file & sending to stellarium client // get coordinates for writing to file & sending to stellarium client
ans = write_cmd(":GR#"); ans = write_cmd(":GR#", ibuff);
if(!str2coord(ans, &r)){ if(!str2coord(ans, &r)){
if(++failcounter == 10){ if(++failcounter == 10){
putlog("Lost connection with mount"); putlog("Lost connection with mount");
@ -410,7 +435,7 @@ void wrhdr(){
} }
return; return;
} }
ans = write_cmd(":GD#"); ans = write_cmd(":GD#", ibuff);
if(!str2coord(ans, &d)){ if(!str2coord(ans, &d)){
if(++failcounter == 10){ if(++failcounter == 10){
putlog("Lost connection with mount"); putlog("Lost connection with mount");
@ -421,11 +446,14 @@ void wrhdr(){
} }
failcounter = 0; failcounter = 0;
tlast = time(NULL); tlast = time(NULL);
if(!hdname) return; if(!hdname){
DBG("hdname not given!");
return;
}
if(!elevation || !longitude || !latitude) getplace(); if(!elevation || !longitude || !latitude) getplace();
ans = write_cmd(":GJD1#"); jd = dups(ans, 0); ans = write_cmd(":GJD1#", ibuff); jd = dups(ans, 0);
ans = write_cmd(":GS#"); lst = dups(ans, 1); ans = write_cmd(":GS#", ibuff); lst = dups(ans, 1);
ans = write_cmd(":GUDT#"); ans = write_cmd(":GUDT#", ibuff);
if(ans){ if(ans){
char *comma = strchr(ans, ','); char *comma = strchr(ans, ',');
if(comma){ if(comma){
@ -433,15 +461,15 @@ void wrhdr(){
date = dups(ans, 1); date = dups(ans, 1);
} }
} }
ans = write_cmd(":pS#"); pS = dups(ans, 1); ans = write_cmd(":pS#", ibuff); pS = dups(ans, 1);
ans = write_cmd(":Gstat#"); ans = write_cmd(":Gstat#", ibuff);
if(ans){ if(ans){
mountstatus = atoi(ans); mountstatus = atoi(ans);
//DBG("Status: %d", mountstatus); //DBG("Status: %d", mountstatus);
} }
#define WRHDR(k, v, c) do{if(printhdr(hdrfd, k, v, c)){close(hdrfd); return;}}while(0) #define WRHDR(k, v, c) do{if(printhdr(hdrfd, k, v, c)){close(hdrfd); return;}}while(0)
char val[22]; char val[22];
if(unlink(hdname)){ if(unlink(hdname) && errno != ENOENT){ // can't unlink existng file
WARN("unlink(%s)", hdname); WARN("unlink(%s)", hdname);
FREE(jd); FREE(lst); FREE(date); FREE(pS); FREE(jd); FREE(lst); FREE(date); FREE(pS);
return; return;
@ -486,4 +514,42 @@ void wrhdr(){
close(hdrfd); close(hdrfd);
} }
// terminal thread: allows to work with terminal through socket
void *term_thread(void *sockd){
int sock = *(int*)sockd;
char buff[BUFLEN+1], ibuff[BUFLEN+2];
// get client IP from socket fd - for logging
struct sockaddr_in peer;
socklen_t peer_len = sizeof(peer);
char *peerIP = NULL;
if(getpeername(sock, (struct sockaddr*)&peer, &peer_len) == 0){
peerIP = inet_ntoa(peer.sin_addr);
}
while(!global_quit){ // blocking read
ssize_t rd = read(sock, buff, BUFLEN);
if(rd <= 0){ // error or disconnect
DBG("Nothing to read from fd %d (ret: %zd)", sock, rd);
break;
}
buff[rd] = 0;
char *ch = strchr(buff, '\n');
if(ch) *ch = 0;
if(!buff[0]) continue; // empty string
char *ans = write_cmd(buff, ibuff);
putlog("%s COMMAND %s ANSWER %s", peerIP, buff, ibuff);
DBG("%s COMMAND: %s ANSWER: %s", peerIP, buff, ibuff);
if(ans){
ssize_t l = (ssize_t)strlen(ans);
if(l++){
ans[l-1] = '\n';
ans[l] = 0;
if(l != write(sock, ans, l)){
WARN("term_thread, write()");
break;
}
}
}
}
close(sock);
return NULL;
}

View File

@ -34,5 +34,6 @@ int point_telescope(double ra, double decl);
int get_telescope_coords(double *ra, double *decl); int get_telescope_coords(double *ra, double *decl);
void stop_telescope(); void stop_telescope();
void wrhdr(); void wrhdr();
void *term_thread(void *sockd);
#endif // __TELESCOPE_H__ #endif // __TELESCOPE_H__

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.8.2, 2020-02-25T17:15:24. --> <!-- Written by QtCreator 4.8.2, 2020-02-27T16:44:16. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -38,7 +38,6 @@ static glob_pars G;
// DEFAULTS // DEFAULTS
// default global parameters // default global parameters
static glob_pars const Gdefault = { static glob_pars const Gdefault = {
.pidfile = DEFAULT_PIDFILE,
.port = DEFAULT_PORT, .port = DEFAULT_PORT,
.host = DEFAULT_HOST, .host = DEFAULT_HOST,
}; };
@ -50,7 +49,6 @@ static glob_pars const Gdefault = {
static myoption cmdlnopts[] = { static myoption cmdlnopts[] = {
// common options // common options
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")}, {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")},
{"port", NEED_ARG, NULL, 'P', arg_string, APTR(&G.port), _("port to connect (default: " DEFAULT_PORT ")")}, {"port", NEED_ARG, NULL, 'P', arg_string, APTR(&G.port), _("port to connect (default: " DEFAULT_PORT ")")},
{"host", NEED_ARG, NULL, 'H', arg_string, APTR(&G.host), _("host to connect (default: " DEFAULT_HOST ")")}, {"host", NEED_ARG, NULL, 'H', arg_string, APTR(&G.host), _("host to connect (default: " DEFAULT_HOST ")")},
{"ra", NEED_ARG, NULL, 'r', arg_string, APTR(&G.ra), _("target RA: HH:MM:SS.SS")}, {"ra", NEED_ARG, NULL, 'r', arg_string, APTR(&G.ra), _("target RA: HH:MM:SS.SS")},

View File

@ -25,7 +25,6 @@
* here are some typedef's for global data * here are some typedef's for global data
*/ */
typedef struct{ typedef struct{
char *pidfile; // name of PID file
char *port; // port to connect char *port; // port to connect
char *host; // hostname char *host; // hostname
char *ra; // RA in string form char *ra; // RA in string form

View File

@ -36,9 +36,6 @@ void signals(int sig){
signal(sig, SIG_IGN); signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig); DBG("Get signal %d, quit.\n", sig);
} }
WARNX("Exit with status %d", sig);
if(GP->pidfile) // remove unnesessary PID file
unlink(GP->pidfile);
restore_console(); restore_console();
exit(sig); exit(sig);
} }
@ -51,20 +48,24 @@ int main(int argc, char *argv[]){
initial_setup(); initial_setup();
char *self = strdup(argv[0]); char *self = strdup(argv[0]);
GP = parse_args(argc, argv); GP = parse_args(argc, argv);
DBG("here");
if(GP->rest_pars_num){ if(GP->rest_pars_num){
printf("%d extra options:\n", GP->rest_pars_num); printf("%d extra options:\n", GP->rest_pars_num);
for(int i = 0; i < GP->rest_pars_num; ++i) for(int i = 0; i < GP->rest_pars_num; ++i)
printf("%s\n", GP->rest_pars[i]); printf("%s\n", GP->rest_pars[i]);
} }
DBG("here");
if((GP->ra && !GP->dec) || (!GP->ra && GP->dec)) if((GP->ra && !GP->dec) || (!GP->ra && GP->dec))
ERRX("You should point both coordinates"); ERRX("You should point both coordinates");
check4running(self, GP->pidfile); DBG("here");
free(self); free(self);
DBG("here");
signal(SIGTERM, signals); // kill (-15) - quit signal(SIGTERM, signals); // kill (-15) - quit
signal(SIGHUP, SIG_IGN); // hup - ignore signal(SIGHUP, SIG_IGN); // hup - ignore
signal(SIGINT, signals); // ctrl+C - quit signal(SIGINT, signals); // ctrl+C - quit
signal(SIGQUIT, signals); // ctrl+\ - quit signal(SIGQUIT, signals); // ctrl+\ - quit
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
DBG("here");
setup_con(); setup_con();
/* /*
if(GP->rest_pars_num){ if(GP->rest_pars_num){