trace.h 3.8 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
	trace support

	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2011
	Copyright Stichting C.A. Muller Radioastronomiestation, 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/>.

*/

#ifndef _INCLUDE_TRACE_H_
#define _INCLUDE_TRACE_H_

#include <time.h>
26
#include <stdbool.h>
27
#include <sys/select.h>
28

Jeroen Vreeken's avatar
Jeroen Vreeken committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

struct traceval;

struct traceval *trace_create(void);
int trace_init(char *host, int port, char *trace_name, struct traceval *trc);

int trace_handler_add(struct traceval *trc, void (*func)(struct traceval *trc));

struct traceval {
	time_t seconds;
	int sample;

	float value;
	int wasnan;
	int new;
	float newval;
	int fd;
	
	void (*handler_func)(struct traceval *trc);
	
	double period;
};


#define trace_val(trace) ((trace)->value)
#define trace_fd(trace) ((trace)->fd)
#define trace_seconds(trace) ((trace)->seconds)
#define trace_sample(trace) ((trace)->sample)
#define trace_period(trace) ((trace)->period)

59
60
61
62
63
64

enum trace_ptype {
	TRACE_PTYPE_TIMESTAMP = 0,
	TRACE_PTYPE_LIST = 1,
	TRACE_PTYPE_NAME = 2,
	TRACE_PTYPE_INTERVAL = 3,
65
	TRACE_PTYPE_DATA = 4,
66
67
68
69
70
71
72
73
74
75
76
77
78
};

enum trace_value_type {
	TRACE_VALUE_TYPE_FLOAT,	/* 4 bytes */
	TRACE_VALUE_TYPE_BOOL,	/* 1 byte */
	TRACE_VALUE_TYPE_UINT8,
	TRACE_VALUE_TYPE_UINT16,
	TRACE_VALUE_TYPE_UINT32,
	TRACE_VALUE_TYPE_SINT8,
	TRACE_VALUE_TYPE_SINT16,
	TRACE_VALUE_TYPE_SINT32,
};

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
char *enum_trace_value_type2str(enum trace_value_type type);

enum trace_interval_type {
	TRACE_INTERVAL_TYPE_INTERVAL,
	TRACE_INTERVAL_TYPE_CHANGED,
	TRACE_INTERVAL_TYPE_CHANGED_INTERVAL,
};

char *enum_trace_interval_type2str(enum trace_interval_type type);

enum trace_state {
	TRACE_STATE_DISCONNECTED,
	TRACE_STATE_CONNECTED,
	TRACE_STATE_READY,
	TRACE_STATE_RECEIVING,
};

char *enum_trace_state2str(enum trace_state state);
97
98
99
100
101
102

struct trace {
	int fd;
	char *host;
	int port;
	
103
	unsigned char *rx_buffer;
104
105
106
107
108
109
	size_t rx_len;
	size_t rx_buf_size;
	
	size_t packet_size;
	
	struct timespec timestamp;
110
111
112
113
114

	void (*handler_interval)(struct trace *, 
	    struct timespec *interval, enum trace_interval_type type);
	void (*handler_list_entry)(struct trace *, 
	    char *name, enum trace_value_type type, char *unit);
115
116
117
118
119
120
121
122
123
124
};

struct trace_pkt {
	enum trace_ptype type;
	char *data;
	size_t len;
};


struct trace *trace_open(char *host, int port);
125
126
void trace_initialize(struct trace *trace, size_t buffer);
void trace_initialize_fd(struct trace *trace, int fd);
127
void trace_close(struct trace *trace);
128
void trace_free(struct trace *trace);
129
130
131
132
133
134
int trace_fd_get(struct trace *trace);
bool trace_fd_read(struct trace *trace);

struct trace_pkt *trace_packet_get(struct trace *trace);
void trace_packet_put(struct trace_pkt *pkt);

135
136
void trace_packet_initialize(struct trace_pkt *pkt);

137
138
int trace_packet_write(struct trace *trace, struct trace_pkt *pkt);

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
struct trace_pkt *trace_packet_new(void);

int trace_packet_list_add(struct trace_pkt *list_pkt, 
    char *name, enum trace_value_type type, char *unit);

int trace_packet_interval_set(struct trace_pkt *pkt, 
    struct timespec *interval, enum trace_interval_type type);

int trace_packet_name_set(struct trace_pkt *pkt, char *name);

void trace_fd_set(struct trace *trace, fd_set *set, int *high);
int trace_handle(struct trace *trace, fd_set *set);

enum trace_state trace_state_get(struct trace *trace);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
154
#endif /* _INCLUDE_TRACE_H_ */