trace.h 4.68 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>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
28
#include <stdint.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
29

30
31
32
33
34
35

enum trace_ptype {
	TRACE_PTYPE_TIMESTAMP = 0,
	TRACE_PTYPE_LIST = 1,
	TRACE_PTYPE_NAME = 2,
	TRACE_PTYPE_INTERVAL = 3,
36
	TRACE_PTYPE_DATA = 4,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
37
	TRACE_PTYPE_VALUE_TYPE = 5,
38
39
40
41
42
43
44
45
46
47
48
49
50
};

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,
};

51
52
53
char *enum_trace_value_type2str(enum trace_value_type type);

enum trace_interval_type {
54
55
56
	TRACE_INTERVAL_TYPE_INTERVAL = 1,
	TRACE_INTERVAL_TYPE_CHANGED = 2,
	TRACE_INTERVAL_TYPE_CHANGED_INTERVAL = 3,
57
58
59
60
61
62
63
64
65
66
67
68
};

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);
69

Jeroen Vreeken's avatar
Jeroen Vreeken committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
struct trace_value {
	union {
		float f;
		bool b;
		uint8_t u8;
		uint16_t u16;
		uint32_t u32;
		int8_t s8;
		int16_t s16;
		int32_t s32;
	} value;
	struct timespec t;
};

84
85
86
87
88
struct trace {
	int fd;
	char *host;
	int port;
	
89
	unsigned char *rx_buffer;
90
91
92
93
94
	size_t rx_len;
	size_t rx_buf_size;
	
	size_t packet_size;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
95
	enum trace_value_type type;
96
	struct timespec timestamp;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
97
	struct timespec interval;
98
	enum trace_interval_type interval_type;
99
100
	struct timespec interval_req;
	enum trace_interval_type interval_type_req;
101
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
102
103
	char *name;

104
	bool interval_set;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
105
106
	bool name_set;
	bool list_received;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
107
108
	bool recover;
	bool type_set;
109
	bool value_set;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
110
	time_t recovertime;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
111
112
	
	struct trace_value value;
113
114
115

	void (*handler_interval)(struct trace *, 
	    struct timespec *interval, enum trace_interval_type type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
116
117
	void (*handler_timestamp)(struct trace *, 
	    struct timespec *timestamp);
118
119
	void (*handler_list_entry)(struct trace *, 
	    char *name, enum trace_value_type type, char *unit);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
120
121
122
123
124
125
126
127
	void (*handler_type)(struct trace *, 
	    enum trace_value_type type);
	void (*handler_value)(struct trace *,
	    struct trace_value *value);
	void (*handler_name)(struct trace *, char *name);
	void (*handler_close)(struct trace *);
	
	void *private;
128
129
130
131
};

struct trace_pkt {
	enum trace_ptype type;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
132
	unsigned char *data;
133
	size_t len;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
134
	size_t buflen;
135
136
137
};


Jeroen Vreeken's avatar
Jeroen Vreeken committed
138
struct trace *trace_alloc(void);
139
struct trace *trace_open(char *host, int port);
140
141
void trace_initialize(struct trace *trace, size_t buffer);
void trace_initialize_fd(struct trace *trace, int fd);
142
143
int trace_interval_set(struct trace *trace,
    struct timespec *interval, enum trace_interval_type type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
144
145
int trace_name_set(struct trace *trace, char *name);
void trace_autorecover(struct trace *trace, bool value);
146
void trace_close(struct trace *trace);
147
void trace_free(struct trace *trace);
148
149
150
151
152
153
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);

154
155
void trace_packet_initialize(struct trace_pkt *pkt);

156
157
int trace_packet_write(struct trace *trace, struct trace_pkt *pkt);

158
struct trace_pkt *trace_packet_new(void);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
159
int trace_packet_resize(struct trace_pkt *pkt, size_t len);
160
161
162
163
164
165

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);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
166
167
int trace_packet_timestamp_set(struct trace_pkt *pkt, 
    struct timespec *timestamp);
168
int trace_packet_name_set(struct trace_pkt *pkt, char *name);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
169
170
int trace_packet_type_set(struct trace_pkt *pkt, enum trace_value_type type);
int trace_packet_value_add(struct trace_pkt *pkt, struct trace_value *value, enum trace_value_type type);
171
172
173
174
175
176

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
177
#endif /* _INCLUDE_TRACE_H_ */