trace_dumpdiff.c 3.55 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
41
}


42
static void handler_value(struct trace *trace, int channel,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
43
    struct trace_value *value)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
45
46
47
48
49
50
51
52
53
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;
	}
}

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

	if (argc < 4) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
87
		printf("Usage:\n\n");
88
		printf("%s [host] [port] [trace]\n", argv[0]);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
89
90
91
92
93
94
95
96
97
		return 0;
	}

	tracename = argv[3];

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

	trace->handler_interval = handler_interval;
	trace->handler_value = handler_value;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
98

Jeroen Vreeken's avatar
Jeroen Vreeken committed
99
100
101
102
103
104
105
106
107
108
109
	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
110
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
111
112
113
	
	printf("Connection ready for tracing\n");
	
114
115
116
117
118
	pkt = trace_packet_new();
	trace_packet_interval_set(pkt, 
	    &t_int, TRACE_INTERVAL_TYPE_CHANGED);
	trace_packet_write(trace, pkt);
	trace_packet_put(pkt);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
119

Jeroen Vreeken's avatar
Jeroen Vreeken committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	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
141
142
	return 0;
}