trace_dump.c 4.09 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
struct timespec t_int;
char *tracename;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
33

34
35
static void handler_interval(struct trace *trace, 
    struct timespec *interval, enum trace_interval_type type)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
36
{
37
	memcpy(&t_int, interval, sizeof(struct timespec));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
38
39
40
41
42
43
44
45
46
47
	printf("interval:  %ld.%09ld\n", interval->tv_sec, interval->tv_nsec);
}

int cnt =0;

static void handler_timestamp(struct trace *trace, 
    struct timespec *t)
{
	printf("timestamp: %ld.%09ld, cnt=%d\n", t->tv_sec, t->tv_nsec, cnt);
	cnt = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
48
49
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
50
51
static void handler_value(struct trace *trace,
    struct trace_value *value)
52
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
53
	cnt++;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	switch (trace->type) {
		case TRACE_VALUE_TYPE_FLOAT:
			printf("%ld.%09ld %e\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.f);
			break;
		case TRACE_VALUE_TYPE_BOOL:
			printf("%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.b);
			break;
		case TRACE_VALUE_TYPE_UINT8:
			printf("%ld.%09ld %u\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.u8);
			break;
		case TRACE_VALUE_TYPE_UINT16:
			printf("%ld.%09ld %u\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.u16);
			break;
		case TRACE_VALUE_TYPE_UINT32:
			printf("%ld.%09ld %u\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.u32);
			break;
		case TRACE_VALUE_TYPE_SINT8:
			printf("%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.s8);
			break;
		case TRACE_VALUE_TYPE_SINT16:
			printf("%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.s16);
			break;
		case TRACE_VALUE_TYPE_SINT32:
			printf("%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.s32);
			break;
87
88
	}
}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
89
90
91

int main(int argc, char **argv)
{
92
93
94
	struct trace *trace;
	struct trace_pkt *pkt;
	int interval = 1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
95
	uint64_t nsec_interval;
96

Jeroen Vreeken's avatar
Jeroen Vreeken committed
97
	if (argc < 4) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
98
		printf("Usage:\n\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
99
100
		printf("%s [host] [port] [trace] <interval>\n", argv[0]);
		return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
101
	}
102

Jeroen Vreeken's avatar
Jeroen Vreeken committed
103
	tracename = argv[3];
104

Jeroen Vreeken's avatar
Jeroen Vreeken committed
105
	trace = trace_open(argv[1], atoi(argv[2]));
106

Jeroen Vreeken's avatar
Jeroen Vreeken committed
107
108
	if (argc >= 5) {
		interval = atoi(argv[4]);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
109
	}
110
111
	
	trace->handler_interval = handler_interval;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
112
	trace->handler_value = handler_value;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
113
	trace->handler_timestamp = handler_timestamp;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
114

115
116
117
118
	while (trace_state_get(trace) == TRACE_STATE_CONNECTED) {
		fd_set fdrx;
		int high = 0;
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
119
120
		FD_ZERO(&fdrx);
		
121
122
123
124
125
		trace_fd_set(trace, &fdrx, &high);
		
		select(high + 1, &fdrx, NULL, NULL, NULL);
		
		trace_handle(trace, &fdrx);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
126
	}
127
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
128
129
	printf("Connection ready for tracing\n");
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
130
131
132
	if (interval) {
		nsec_interval = t_int.tv_sec * 1000000000;
		nsec_interval += t_int.tv_nsec;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
133
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
134
135
136
137
		nsec_interval *= interval;
		
		t_int.tv_nsec = nsec_interval % 1000000000;
		t_int.tv_sec = nsec_interval / 1000000000;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
138

139
140
141
142
143
		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
144
145
	}

146
147
148
149
150
	pkt = trace_packet_new();
	trace_packet_name_set(pkt, tracename);
	trace_packet_write(trace, pkt);
	trace_packet_put(pkt);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
151
152
	printf("Wait for data\n");

153
154
155
156
157
	while (trace_state_get(trace) == TRACE_STATE_RECEIVING ||
	       trace_state_get(trace) == TRACE_STATE_READY) {
		fd_set fdrx;
		int high = 0;
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
158
159
		FD_ZERO(&fdrx);
		
160
161
162
163
164
165
166
		trace_fd_set(trace, &fdrx, &high);
		
		select(high + 1, &fdrx, NULL, NULL, NULL);
		
		trace_handle(trace, &fdrx);
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
167
168
	return 0;
}