trace.h 4.48 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
99
	enum trace_interval_type interval_type;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
100
101
	char *name;

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

	void (*handler_interval)(struct trace *, 
	    struct timespec *interval, enum trace_interval_type type);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
113
114
	void (*handler_timestamp)(struct trace *, 
	    struct timespec *timestamp);
115
116
	void (*handler_list_entry)(struct trace *, 
	    char *name, enum trace_value_type type, char *unit);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
117
118
119
120
121
122
123
124
	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;
125
126
127
128
};

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


Jeroen Vreeken's avatar
Jeroen Vreeken committed
135
struct trace *trace_alloc(void);
136
struct trace *trace_open(char *host, int port);
137
138
void trace_initialize(struct trace *trace, size_t buffer);
void trace_initialize_fd(struct trace *trace, int fd);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
139
140
int trace_name_set(struct trace *trace, char *name);
void trace_autorecover(struct trace *trace, bool value);
141
void trace_close(struct trace *trace);
142
void trace_free(struct trace *trace);
143
144
145
146
147
148
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);

149
150
void trace_packet_initialize(struct trace_pkt *pkt);

151
152
int trace_packet_write(struct trace *trace, struct trace_pkt *pkt);

153
struct trace_pkt *trace_packet_new(void);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
154
int trace_packet_resize(struct trace_pkt *pkt, size_t len);
155
156
157
158
159
160

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
161
162
int trace_packet_timestamp_set(struct trace_pkt *pkt, 
    struct timespec *timestamp);
163
int trace_packet_name_set(struct trace_pkt *pkt, char *name);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
164
165
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);
166
167
168
169
170
171

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