trace_dump.c 4.02 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
95
	struct trace *trace;
	struct trace_pkt *pkt;
	int interval = 1;

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

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

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

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

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

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

145
146
147
148
149
	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
150
151
	printf("Wait for data\n");

152
153
154
155
156
	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
157
158
		FD_ZERO(&fdrx);
		
159
160
161
162
163
164
165
		trace_fd_set(trace, &fdrx, &high);
		
		select(high + 1, &fdrx, NULL, NULL, NULL);
		
		trace_handle(trace, &fdrx);
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
166
167
	return 0;
}