command.h 4.43 KB
Newer Older
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
26
27
28
29
30
31
32
33
34
35
36
37
/*
	command support

	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014

	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_COMMAND_H_
#define _INCLUDE_COMMAND_H_

#include <time.h>
#include <stdbool.h>
#include <sys/select.h>
#include <stdint.h>


enum command_ptype {
	COMMAND_PTYPE_NULL = 0,
	COMMAND_PTYPE_LIST = 1,
	COMMAND_PTYPE_NAME = 2,
	COMMAND_PTYPE_ID = 3,
	COMMAND_PTYPE_SETPOINT = 4, /* Simple direct setpoint */
	COMMAND_PTYPE_SPEED = 5, /* Simple direct 1st derivative setpoint */
	COMMAND_PTYPE_SETPOINT_TIME = 6, /* Setpoint to be reached @ time */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
38
	COMMAND_PTYPE_SETPOINT_TRACK = 7, /* Use setpoint from track input */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39
40
	COMMAND_PTYPE_SPEED_TRACK = 8, /* Use 1st derivative from track input */

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	COMMAND_PTYPE_MAX, /* Always the last entry! */
};

char *enum_command_ptype2str(enum command_ptype ptype);

enum command_value_type {
	COMMAND_VALUE_TYPE_FLOAT,	/* 4 bytes */
	COMMAND_VALUE_TYPE_BOOL,	/* 1 byte */
	COMMAND_VALUE_TYPE_UINT8,
	COMMAND_VALUE_TYPE_UINT16,
	COMMAND_VALUE_TYPE_UINT32,
	COMMAND_VALUE_TYPE_SINT8,
	COMMAND_VALUE_TYPE_SINT16,
	COMMAND_VALUE_TYPE_SINT32,
};

char *enum_command_value_type2str(enum command_value_type type);

enum command_state {
	COMMAND_STATE_DISCONNECTED,
	COMMAND_STATE_CONNECTED,
	COMMAND_STATE_READY,
	COMMAND_STATE_RECEIVING,
};

char *enum_command_state2str(enum command_state state);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
68
69
#define COMMAND_ID_NONE	0xffffffff

70
71
struct command_entry {
	enum command_ptype type;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
72
	uint32_t id;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	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;
};

struct command {
	int fd;
	char *host;
	int port;
	
	unsigned char *rx_buffer;
	size_t rx_len;
	size_t rx_buf_size;
	
	size_t packet_size;
	
	char *name;
	char *unit;
	char *id;
	enum command_value_type type;

	bool list_received;
	bool recover;
	time_t recovertime;

	void (*handler_list_entry)(struct command *, 
	    char *name, enum command_value_type type, char *unit,
	    int typec, enum command_ptype typev[]);
	void (*handler_name)(struct command *, char *name);
	void (*handler_id)(struct command *, char *id);
	void (*handler_entry)(struct command *, struct command_entry *entry);
	void (*handler_close)(struct command *);
	
	void *private;
};

struct command_pkt {
	enum command_ptype type;
	unsigned char *data;
	size_t len;
	size_t buflen;
};


struct command *command_alloc(void);
void command_free(struct command *command);
struct command *command_open(char *host, int port);
void command_initialize(struct command *command, size_t buffer);
void command_initialize_fd(struct command *command, int fd);
130
int command_connect(struct command *command);
131
132
133
134
135
136
137
138
139
140
141
void command_close(struct command *command);

struct command *command_open_simple(char *host, int port, char *name, char *id,
    enum command_value_type type);
int command_send(struct command *command, struct command_entry *entry);

int command_name_set(struct command *command, char *name);
int command_id_set(struct command *command, char *id);

bool command_fd_set(struct command *command, fd_set *set, int *high);
int command_handle(struct command *command, fd_set *set);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
int command_handle_err(struct command *command, fd_set *set);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
enum command_state command_state_get(struct command *command);
void command_autorecover(struct command *command, bool value);

bool command_fd_read(struct command * command);

struct command_pkt *command_packet_get(struct command *command);
void command_packet_put(struct command_pkt *pkt);

void command_packet_initialize(struct command_pkt *pkt);
int command_packet_write(struct command *command, struct command_pkt *pkt);

struct command_pkt *command_packet_new(void);
int command_packet_resize(struct command_pkt *pkt, size_t len);
int command_packet_list_add(struct command_pkt *list_pkt, 
    char *name, enum command_value_type type, char *unit,
    int typec, enum command_ptype typev[]);
    
#endif /* _INCLUDE_COMMAND_H_ */