trace_dumpdiff.c 3.96 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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>

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

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

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
37
38
	memcpy(&t_int, interval, sizeof(struct timespec));
	printf("interval:  %ld.%09ld\n", interval->tv_sec, interval->tv_nsec);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
40
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
41
int cnt =0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
42

Jeroen Vreeken's avatar
Jeroen Vreeken committed
43
44
static void handler_value(struct trace *trace,
    struct trace_value *value)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
45
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
46
	static struct trace_value prev;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
47
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
48
49
50
51
52
	if (cnt || memcmp(&prev.value, &value->value, sizeof(prev.value))) {
		return;
	}
	prev.value = value->value;
	cnt = 1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
53
	
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
87
88
89
90
91
92
93
94
95
96
	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;
	}
}

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

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

	tracename = argv[3];

	trace = trace_open(argv[1], atoi(argv[2]));

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

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

Jeroen Vreeken's avatar
Jeroen Vreeken committed
137
138
139
140
141
		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
142
143
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	pkt = trace_packet_new();
	trace_packet_name_set(pkt, tracename);
	trace_packet_write(trace, pkt);
	trace_packet_put(pkt);

	printf("Wait for data\n");

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