trace.h 4.12 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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);
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
98
99
100
101
102
	struct timespec interval;

	bool name_set;
	bool list_received;
	
	struct trace_value value;
103
104
105

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

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


struct trace *trace_open(char *host, int port);
128
129
void trace_initialize(struct trace *trace, size_t buffer);
void trace_initialize_fd(struct trace *trace, int fd);
130
void trace_close(struct trace *trace);
131
void trace_free(struct trace *trace);
132
133
134
135
136
137
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);

138
139
void trace_packet_initialize(struct trace_pkt *pkt);

140
141
int trace_packet_write(struct trace *trace, struct trace_pkt *pkt);

142
143
144
145
146
147
148
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);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
149
150
int trace_packet_timestamp_set(struct trace_pkt *pkt, 
    struct timespec *timestamp);
151
int trace_packet_name_set(struct trace_pkt *pkt, char *name);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
152
153
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);
154
155
156
157
158
159

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