diff --git a/Stellarium_control/main.c b/Stellarium_control/main.c index 472eccd..c6e16f5 100644 --- a/Stellarium_control/main.c +++ b/Stellarium_control/main.c @@ -154,7 +154,11 @@ typedef struct __attribute__((__packed__)){ int32_t status; } outdata; -#define ACS_CMD(a) do{green(#a); printf("\n"); a; }while(0) +#ifdef EBUG +#define ACS_CMD(a) do{green(#a); printf("\n"); }while(0) +#else +#define ACS_CMD(a) do{red(#a); printf("\n"); a; }while(0) +#endif /** * send input RA/Decl (j2000!) coordinates to tel * both coords are in seconds (ra in time, dec in angular) @@ -387,8 +391,10 @@ int main(_U_ int argc, char **argv){ if(fabs(M_time - last) < 0.02) ERRX(_("Data stale!")); get_cmd_queue(&ucmd, ClientSide); +#ifndef EBUG passhash pass = {0,0}; get_passhash(&pass); +#endif printf(_("All OK, start socket\n")); diff --git a/bta_mirtemp/Makefile b/bta_mirtemp/Makefile index 8999bd2..9231212 100644 --- a/bta_mirtemp/Makefile +++ b/bta_mirtemp/Makefile @@ -1,9 +1,8 @@ PROGRAM = bta_mirtemp LDFLAGS = -lcrypt -#-lm -lsla SRCS = $(wildcard *.c) CC = gcc -DEFINES = -D_XOPEN_SOURCE=666 +DEFINES = -D_XOPEN_SOURCE=1111 -D_GNU_SOURCE # -DEBUG CXX = gcc CFLAGS = -Wall -Werror -Wextra $(DEFINES) -pthread diff --git a/bta_mirtemp/bta_shdata.c b/bta_mirtemp/bta_shdata.c index 43eafa0..24ab17b 100644 --- a/bta_mirtemp/bta_shdata.c +++ b/bta_mirtemp/bta_shdata.c @@ -1,6 +1,8 @@ #include "bta_shdata.h" #include "usefull_macros.h" +#include + #pragma pack(push, 4) // Main command channel (level 5) struct CMD_Queue mcmd = {{"Mcmd"}, 0200,0,-1,0}; diff --git a/bta_mirtemp/btamirtemp.config b/bta_mirtemp/btamirtemp.config new file mode 100644 index 0000000..e0284f4 --- /dev/null +++ b/bta_mirtemp/btamirtemp.config @@ -0,0 +1,2 @@ +// Add predefined macros for your project here. For example: +// #define THE_ANSWER 42 diff --git a/bta_mirtemp/btamirtemp.creator b/bta_mirtemp/btamirtemp.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/bta_mirtemp/btamirtemp.creator @@ -0,0 +1 @@ +[General] diff --git a/bta_mirtemp/btamirtemp.creator.user b/bta_mirtemp/btamirtemp.creator.user new file mode 100644 index 0000000..4eb12ec --- /dev/null +++ b/bta_mirtemp/btamirtemp.creator.user @@ -0,0 +1,170 @@ + + + + + + EnvironmentId + {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + false + true + false + + + + ProjectExplorer.Project.PluginSettings + + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + /Big/Data/00-BTAmirtemp + + + + all + + false + + + false + true + Сборка + + GenericProjectManager.GenericMakeStep + + 1 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + + clean + + false + + + false + true + Сборка + + GenericProjectManager.GenericMakeStep + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + По умолчанию + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Установка + + ProjectExplorer.BuildSteps.Deploy + + 1 + Конфигурация установки + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + 2 + + + Особая программа + + ProjectExplorer.CustomExecutableRunConfiguration + + 3768 + false + true + false + false + true + + + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 20 + + + Version + 20 + + diff --git a/bta_mirtemp/btamirtemp.files b/bta_mirtemp/btamirtemp.files new file mode 100644 index 0000000..426990b --- /dev/null +++ b/bta_mirtemp/btamirtemp.files @@ -0,0 +1,5 @@ +bta_shdata.c +bta_shdata.h +main.c +usefull_macros.c +usefull_macros.h diff --git a/bta_mirtemp/btamirtemp.includes b/bta_mirtemp/btamirtemp.includes new file mode 100644 index 0000000..9c558e3 --- /dev/null +++ b/bta_mirtemp/btamirtemp.includes @@ -0,0 +1 @@ +. diff --git a/bta_mirtemp/main.c b/bta_mirtemp/main.c new file mode 100644 index 0000000..cde8c48 --- /dev/null +++ b/bta_mirtemp/main.c @@ -0,0 +1,182 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include //prctl +#include +#include +#include // wait +#include +#define __USE_BSD +#include + +#include "usefull_macros.h" +#include "bta_shdata.h" + +#define HOST "mirtemp.sao.ru" +#define PORT "4444" +#define RESOURCE "Tmean" + +static int gotsegm = 0; + +void clear_flags(){ + if(!gotsegm) return; + if(MeteoMode & NET_T3){ // clear "net" & "sensor" flags + MeteoMode &= ~NET_HMD; + if(MeteoMode & SENSOR_T3) MeteoMode &= ~SENSOR_T3; + } +} + +void signals(int sig){ + char ss[10]; + signal(sig, SIG_IGN); + switch(sig){ + case SIGHUP : strcpy(ss,"SIGHUP"); break; + case SIGINT : strcpy(ss,"SIGINT"); break; + case SIGQUIT: strcpy(ss,"SIGQUIT"); break; + case SIGFPE : strcpy(ss,"SIGFPE"); break; + case SIGPIPE: strcpy(ss,"SIGPIPE"); break; + case SIGSEGV: strcpy(ss,"SIGSEGV"); break; + case SIGTERM: strcpy(ss,"SIGTERM"); break; + default: sprintf(ss,"SIG_%d",sig); break; + } + switch(sig){ + default: + case SIGHUP : + LOG("%s - Ignore ...", ss); + fflush(stderr); + signal(sig, signals); + return; + case SIGINT : + case SIGPIPE: + case SIGQUIT: + case SIGFPE : + case SIGSEGV: + case SIGTERM: + signal(SIGALRM, SIG_IGN); + LOG("%s - Stop!", ss); + clear_flags(); + exit(sig); + } +} + +/** + * get mirror temperature over network + * @return 0 if succeed + */ +int get_mirT(double *T){ + int sockfd = 0; + char recvBuff[64]; + memset(recvBuff, 0, sizeof(recvBuff)); + struct addrinfo h, *r, *p; + memset(&h, 0, sizeof(h)); + h.ai_family = AF_INET; + h.ai_socktype = SOCK_STREAM; + h.ai_flags = AI_CANONNAME; + char *host = HOST; + char *port = PORT; + if(getaddrinfo(host, port, &h, &r)) WARNX("getaddrinfo()"); + for(p = r; p; p = p->ai_next){ + if ((sockfd = socket(p->ai_family, p->ai_socktype, + p->ai_protocol)) == -1) { + WARN("socket()"); + continue; + } + if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { + close(sockfd); + WARN("connect()"); + continue; + } + break; // if we get here, we must have connected successfully + } + if(p == NULL){ + WARNX("failed to connect"); + return 1; + } + freeaddrinfo(r); + if(send(sockfd, RESOURCE, sizeof(RESOURCE), 0) != sizeof(RESOURCE)){ + WARN("send()"); + return 1; + } + ssize_t rd = read(sockfd, recvBuff, sizeof(recvBuff)-1); + if(rd < 0){ + WARN("read()"); + return 1; + }else recvBuff[rd] = 0; + close(sockfd); + char *eptr; + *T = strtod(recvBuff, &eptr); + DBG("Got mirror T=%.1f", *T); + if(eptr == recvBuff) return 1; + return 0; +} + +int main (int argc, char *argv[]){ + initial_setup(); + if(argc == 2){ + printf("Log file: %s", argv[1]); + Cl_createlog(argv[1]); + } + signal(SIGHUP, signals); + signal(SIGINT, signals); + signal(SIGQUIT,signals); + signal(SIGFPE, signals); + signal(SIGPIPE,signals); + signal(SIGSEGV,signals); + signal(SIGTERM,signals); + + LOG("\nStarted\n"); + + #ifndef EBUG + if(daemon(1, 0)){ + ERR("daemon()"); + } + while(1){ // guard for dead processes + pid_t childpid = fork(); + if(childpid){ + LOG("create child with PID %d", childpid); + wait(NULL); + LOG("child %d died\n", childpid); + sleep(1); + }else{ + prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies + break; // go out to normal functional + } + } + #endif + + time_t tlast = time(NULL); + while(1){ + if(!gotsegm){ + sdat.mode |= 0200; + sdat.atflag = 0; + gotsegm = get_shm_block( &sdat, ClientSide); + if(!gotsegm){ + LOG("Can't find SHM segment"); + } else get_cmd_queue( &ocmd, ClientSide); + } + double T; + if(time(NULL) - tlast > 900){ // no signal for 15 minutes - clear flags + LOG("15 minutes - no signal!"); + tlast = time(NULL); + clear_flags(); + } + if(get_mirT(&T)){ + sleep(10); + continue; + } + if(gotsegm && 0 == (MeteoMode & INPUT_T3)){ // not manual mode - change Tmir value + val_T3 = T; + MeteoMode |= (SENSOR_T3|NET_T3); + DBG("Change T: %.2f", T); + } + sleep(60); + tlast = time(NULL); + } + return 0; +} diff --git a/bta_mirtemp/usefull_macros.c b/bta_mirtemp/usefull_macros.c index bf06fa3..9ca188b 100644 --- a/bta_mirtemp/usefull_macros.c +++ b/bta_mirtemp/usefull_macros.c @@ -21,6 +21,8 @@ #include "usefull_macros.h" +#include + /** * function for different purposes that need to know time intervals * @return double value: time in seconds @@ -247,77 +249,63 @@ int mygetchar(){ // getchar() without need of pressing ENTER return ret; } +// logging -/******************************************************************************\ - * TTY with select() -\******************************************************************************/ -static struct termio oldtty, tty; // TTY flags -static int comfd = -1; // TTY fd -// run on exit: -void restore_tty(){ - if(comfd == -1) return; - ioctl(comfd, TCSANOW, &oldtty ); // return TTY to previous state - close(comfd); - comfd = -1; -} - -#ifndef BAUD_RATE -#define BAUD_RATE B9600 -#endif -// init: -void tty_init(char *comdev){ - DBG("\nOpen port...\n"); - if ((comfd = open(comdev,O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0){ - WARN("Can't use port %s\n",comdev); - ioctl(comfd, TCSANOW, &oldtty); // return TTY to previous state - close(comfd); - signals(0); // quit? - } - DBG(" OK\nGet current settings... "); - if(ioctl(comfd,TCGETA,&oldtty) < 0){ // Get settings - WARN(_("Can't get settings")); - signals(0); - } - tty = oldtty; - tty.c_lflag = 0; // ~(ICANON | ECHO | ECHOE | ISIG) - tty.c_oflag = 0; - tty.c_cflag = BAUD_RATE|CS8|CREAD|CLOCAL; // 9.6k, 8N1, RW, ignore line ctrl - tty.c_cc[VMIN] = 0; // non-canonical mode - tty.c_cc[VTIME] = 5; - if(ioctl(comfd,TCSETA,&tty) < 0){ - WARN(_("Can't set settings")); - signals(0); - } - DBG(" OK\n"); -} +static Cl_log log = {0}; /** - * Read data from TTY - * @param buff (o) - buffer for data read - * @param length - buffer len - * @return amount of readed bytes + * @brief Cl_createlog - create log file: init mutex, test file open ability + * @param log - log structure + * @return 0 if all OK */ -size_t read_tty(uint8_t *buff, size_t length){ - ssize_t L = 0; - fd_set rfds; - struct timeval tv; - int retval; - FD_ZERO(&rfds); - FD_SET(comfd, &rfds); - tv.tv_sec = 0; tv.tv_usec = 50000; // wait for 50ms - retval = select(comfd + 1, &rfds, NULL, NULL, &tv); - if (!retval) return 0; - if(FD_ISSET(comfd, &rfds)){ - if((L = read(comfd, buff, length)) < 1) return 0; +int Cl_createlog(char *logname){ + if(log.logpath){ + FREE(log.logpath); + pthread_mutex_destroy(&log.mutex); } - return (size_t)L; -} - -int write_tty(uint8_t *buff, size_t length){ - ssize_t L = write(comfd, buff, length); - if((size_t)L != length){ - WARN("Write error!"); - return 1; + FILE *logfd = fopen(logname, "a"); + if(!logfd){ + WARN("Can't open log file"); + return 2; + } + log.logpath = strdup(logname); + fclose(logfd); + if(pthread_mutex_init(&log.mutex, NULL)){ + WARN("Can't init log mutes"); + return 3; } return 0; } + +/** + * @brief Cl_putlog - put message to log file with/without timestamp + * @param timest - ==1 to put timestamp + * @param log - pointer to log structure + * @param lvl - message loglevel (if lvl > loglevel, message won't be printed) + * @param fmt - format and the rest part of message + * @return amount of symbols saved in file + */ +int Cl_putlogt(const char *fmt, ...){ + if(pthread_mutex_lock(&log.mutex)){ + WARN("Can't lock log mutex"); + return 0; + } + int i = 0; + FILE *logfd = fopen(log.logpath, "a"); + if(!logfd) goto rtn; + char strtm[128]; + time_t t = time(NULL); + struct tm *curtm = localtime(&t); + strftime(strtm, 128, "%Y/%m/%d-%H:%M:%S", curtm); + i = fprintf(logfd, "%s\t", strtm); + va_list ar; + va_start(ar, fmt); + i += vfprintf(logfd, fmt, ar); + va_end(ar); + i += fprintf(logfd, "\n"); + fclose(logfd); +rtn: + pthread_mutex_unlock(&log.mutex); + return i; +} + diff --git a/bta_mirtemp/usefull_macros.h b/bta_mirtemp/usefull_macros.h index bc000c2..b4741e3 100644 --- a/bta_mirtemp/usefull_macros.h +++ b/bta_mirtemp/usefull_macros.h @@ -93,6 +93,7 @@ extern void signals(int sig); #define ALLOC(type, var, size) type * var = ((type *)my_alloc(size, sizeof(type))) #define MALLOC(type, size) ((type *)my_alloc(size, sizeof(type))) #define FREE(ptr) do{free(ptr); ptr = NULL;}while(0) +#define LOG(...) do{Cl_putlogt(__VA_ARGS__); WARNX(__VA_ARGS__);}while(0) double dtime(); @@ -116,9 +117,12 @@ void setup_con(); int read_console(); int mygetchar(); -void restore_tty(); -void tty_init(char *comdev); -size_t read_tty(uint8_t *buff, size_t length); -int write_tty(uint8_t *buff, size_t length); +typedef struct{ + char *logpath; // full path to logfile + pthread_mutex_t mutex; // log mutex +} Cl_log; + +int Cl_createlog(char *logname); +int Cl_putlogt(const char *fmt, ...); #endif // __USEFULL_MACROS_H__