Browse Source

Add trace2port

jeroen
Tammo Jan Dijkema 5 years ago
parent
commit
58bfaf5b37
3 changed files with 203 additions and 0 deletions
  1. +1
    -0
      common/trace/.gitignore
  2. +9
    -0
      common/trace/build.mk
  3. +193
    -0
      common/trace/trace2port.c

+ 1
- 0
common/trace/.gitignore View File

@ -4,3 +4,4 @@ trace_fft
trace_list
trace_view
trace2file
trace2port

+ 9
- 0
common/trace/build.mk View File

@ -6,6 +6,7 @@ TRACE_TARGETS += $(DIR)/trace_dumpdiff
TRACE_TARGETS += $(DIR)/trace_list
TRACE_TARGETS += $(DIR)/trace_view
TRACE_TARGETS += $(DIR)/trace2file
TRACE_TARGETS += $(DIR)/trace2port
ARCHSRCS := $(DIR)/trace.c $(DIR)/trace_tcp.c
@ -54,6 +55,12 @@ $(DIR)/trace2file: libtrace.la
$(DIR)/trace2file_LDFLAGS += -ltrace
$(DIR)/trace2file: $(TRACE2FILE_OBJS)
TRACE2PORT_SRCS := $(DIR)/trace2port.c
TRACE2PORT_OBJS := $(TRACE2PORT_SRCS:.c=.o)
$(DIR)/trace2port.o: CFLAGS += -Wall -O3
$(DIR)/trace2port_LDFLAGS += -lm -ltrace
$(DIR)/trace2port: libtrace.la
$(DIR)/trace2port: $(TRACE2PORT_OBJS)
TARGETS += $(TRACE_TARGETS)
SRCS += $(ARCHSRCS)
@ -63,6 +70,7 @@ SRCS += $(TRACE_FFT_SRCS)
SRCS += $(TRACE_LIST_SRCS)
SRCS += $(TRACE_VIEW_SRCS)
SRCS += $(TRACE2FILE_SRCS)
SRCS += $(TRACE2PORT_SRCS)
CLEAN += $(TRACE_TARGETS) $(ARCHOBJS) $(LIBDIR)/libtrace.a
CLEAN += $(TRACE_DUMP_OBJS)
CLEAN += $(TRACE_DUMPDIFF_OBJS)
@ -70,3 +78,4 @@ CLEAN += $(TRACE_FFT_OBJS)
CLEAN += $(TRACE_LIST_OBJS)
CLEAN += $(TRACE_VIEW_OBJS)
CLEAN += $(TRACE2FILE_OBJS)
CLEAN += $(TRACE2PORT_OBJS)

+ 193
- 0
common/trace/trace2port.c View File

@ -0,0 +1,193 @@
/*
Program to echo a trace periodically in plain text to a port
Based on work from Jeroen Vreeken (pe1rxq@amsat.org)
Adapted by Tammo Jan Dijkema (t.j.dijkema@camras.nl)
Copyright Stichting C.A. Muller Radioastronomiestation, 2017
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/>.
*/
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include <ctype.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdbool.h>
#include <command/command.h>
#include <trace/trace.h>
#include <dt_port_numbers.h>
#include <utils/status_server.h>
#include <utils/dt_host.h>
#include <log/log.h>
#define TIME_OFFSET 2
int trace_port = CONSOLE_TRACE_PORT;
int stat_port = 11042;
void output(struct status_server *stat_srv,
int ntraces,
struct trace **tracevals,
bool toDegrees)
{
static time_t last = 0;
time_t now;
time_t now_t = time(NULL);
char statline[200];
int do_output = 1;
int tracenum;
now = tracevals[0]->value.t.tv_sec;
for (tracenum=0; tracenum<ntraces; ++tracenum) {
if (tracevals[tracenum]->value.t.tv_sec != now) {
do_output = 0;
}
}
if (now_t - now > 2) {
printf("No trace for more than 2 seconds, using last known position\n");
now = now_t;
}
if (now <= last) {
do_output = 0;
}
if (do_output == 0) {
return;
} else {
last = now;
}
int statlinelen = 0;
for (tracenum=0; tracenum<ntraces; ++tracenum) {
double val = tracevals[tracenum]->value.value.f;
// Do not convert the status server values to degrees
statlinelen += sprintf(statline+statlinelen, "%f ", val);
// Convert console output to degrees if specified
if (toDegrees) {
val *= 360.0 / (2*M_PI);
}
printf("%f ", val);
}
printf("\n");
sprintf(statline+statlinelen, "\n");
status_server_send(stat_srv, statline);
}
static void handler_interval(struct trace *trace,
struct timespec *interval, enum trace_interval_type type)
{
printf("interval: %ld.%09ld\n", interval->tv_sec, interval->tv_nsec);
}
int main(int argc, char **argv)
{
bool toDegrees = false;
int opt;
while ((opt = getopt(argc, argv, "d")) != -1) {
toDegrees = true;
printf("Using degrees\n");
}
printf("optind=%d\n", optind);
if (argc-optind<1) {
printf("Usage: %s [-d] trace_host trace1 [trace2 ...]\n", argv[0]);
printf(" -d: convert all traces from radians to degrees\n");
printf("Example: trace2port -d console Azimuth_Position Elevation_Position azimuth_setpoint_error.difference elevation_setpoint_error.difference\n");
return 1;
}
int ntraces = argc-optind-1;
struct trace *tracevals[ntraces];
struct status_server *stat_srv;
time_t lastt = 0;
struct timespec t_int;
signal(SIGPIPE, SIG_IGN);
log_client_start(dt_host_console(), CONSOLE_LOG_PORT_IN,
LOG_T_DEBUG, LOG_T_INFO, "trace/trace2port");
printf("Number of traces: %d\n", ntraces);
t_int.tv_sec = 1;
t_int.tv_nsec = 0;
int tracenum;
printf("Using host %s\n", argv[optind]);
for (tracenum=0; tracenum<ntraces; ++tracenum) {
tracevals[tracenum] = trace_open(argv[optind], trace_port);
trace_name_set(tracevals[tracenum], 0, argv[optind+tracenum+1]);
trace_autorecover(tracevals[tracenum], true);
tracevals[tracenum]->handler_interval = handler_interval;
trace_interval_set(tracevals[tracenum], &t_int, TRACE_INTERVAL_TYPE_INTERVAL);
}
stat_srv = status_server_create(stat_port, 0, 100);
while (1)
{
time_t t;
int high = 0;
fd_set fdset_rx;
struct timeval tv;
t = time(NULL);
if (t != lastt) {
lastt = t;
t += TIME_OFFSET;
}
FD_ZERO(&fdset_rx);
status_server_fdset_add(stat_srv, &fdset_rx, &high);
for (tracenum=0; tracenum<ntraces; ++tracenum) {
trace_fd_set(tracevals[tracenum], &fdset_rx, &high);
}
tv.tv_sec = 1;
tv.tv_usec = 0;
select(high + 1, &fdset_rx, NULL, NULL, &tv);
for (tracenum=0; tracenum<ntraces; ++tracenum) {
trace_handle(tracevals[tracenum], &fdset_rx);
}
status_server_fdset_handle(stat_srv, &fdset_rx);
output(stat_srv, ntraces, tracevals, toDegrees);
}
}

Loading…
Cancel
Save