trace_dump.c 2.69 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
2
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007, 2008, 2011, 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007, 2008, 2011

	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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <math.h>

29
#include <trace/trace.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30

31
32
33
struct timespec t_int;
char *tracename;
enum trace_value_type tracetype;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
34

35
36
static void handler_interval(struct trace *trace, 
    struct timespec *interval, enum trace_interval_type type)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
37
{
38
	memcpy(&t_int, interval, sizeof(struct timespec));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
40
}

41
42
43
44
45
46
47
static void handler_list_entry(struct trace *trace, 
    char *name, enum trace_value_type type, char *unit)
{
	if (!strcmp(name, tracename)) {
		tracetype = type;
	}
}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
48
49
50

int main(int argc, char **argv)
{
51
52
53
54
	struct trace *trace;
	struct trace_pkt *pkt;
	int interval = 1;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
55
56
57
58
	if (argc < 2) {
		printf("Usage:\n\n");
		printf("%s [trace] <interval>\n", argv[0]);
	}
59
60
61
62
63

	tracename = argv[1];

	trace = trace_open("localhost", 10000);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
64
65
66
	if (argc >= 3) {
		interval = atoi(argv[2]);
	}
67
68
69
	
	trace->handler_interval = handler_interval;
	trace->handler_list_entry = handler_list_entry;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
70

71
72
73
74
75
76
77
78
79
	while (trace_state_get(trace) == TRACE_STATE_CONNECTED) {
		fd_set fdrx;
		int high = 0;
		
		trace_fd_set(trace, &fdrx, &high);
		
		select(high + 1, &fdrx, NULL, NULL, NULL);
		
		trace_handle(trace, &fdrx);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
80
	}
81
82
83
84
	
	if (interval != 1) {
		t_int.tv_sec *= interval;
		t_int.tv_nsec *= interval;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
85
		
86
87
88
89
		if (t_int.tv_nsec > 1000000000) {
			t_int.tv_sec++;
			t_int.tv_nsec -= 1000000000;
		}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
90

91
92
93
94
95
		pkt = trace_packet_new();
		trace_packet_interval_set(pkt, 
		    &t_int, TRACE_INTERVAL_TYPE_INTERVAL);
		trace_packet_write(trace, pkt);
		trace_packet_put(pkt);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
96
97
	}

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
	pkt = trace_packet_new();
	trace_packet_name_set(pkt, tracename);
	trace_packet_write(trace, pkt);
	trace_packet_put(pkt);

	while (trace_state_get(trace) == TRACE_STATE_RECEIVING ||
	       trace_state_get(trace) == TRACE_STATE_READY) {
		fd_set fdrx;
		int high = 0;
		
		trace_fd_set(trace, &fdrx, &high);
		
		select(high + 1, &fdrx, NULL, NULL, NULL);
		
		trace_handle(trace, &fdrx);
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
115
116
	return 0;
}