-
Notifications
You must be signed in to change notification settings - Fork 3
/
protocol.h
281 lines (240 loc) · 7.58 KB
/
protocol.h
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
38
39
40
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
68
69
70
71
72
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @file protocol.h
* @brief Defines the communication protocol between the bartender
* and the control device.
* @author Stefan Bossbaly (sbossb@gmail.com)
* @date April, 2014
*
* This is where the protocol between the bartender and the control device is defined. The
* data is organized into structured format called a message. The message has a size of MSG_SIZE.
* The message structure can be broken down into different sections as shown below.
*
* [START] [TYPE] [CMD] [RSP_CODE] [CONTENT] [STOP]
*
* The first and the last section of a message are called the start and stop bytes and are
* defined as MSG_START and MSG_END at location I_START and I_END respectively. The message
* must have these start and stop bytes so in case we drop a byte during transmission we are
* able to tell which message is malformed.
*
* The next section is the Type section at location I_TYPE. The type header defined what kind
* of message we are sending or receiving. If we are issuing a command then the type header is
* set to TYPE_CMD and if we are responding to a command then the type header should be set to
* TYPE_RSP.
*
* The next section is the command section at location I_CMD. The acceptable value of this command
* is any of the command values (CMD_STOP, CMD_MOVE, CMD_POUR, CMD_STATUS, CMD_LOCATION). If
* the message is of type TYPE_CMD then the command section represents the command that the
* message is issuing. If the message is of type TYPE_RSP then the command section represents
* the command that the message is responding to or BLANK if the response is not responding
* to a command.
*
* The next section is the response code section located at I_RSP_CODE. This section lets
* the response define a response code that defines the status of the response. This is
* like the http status code section.
* If the message is of type TYPE_CMD then the response code section is BLANK.
*
*/
#ifndef PROTOCOL_H_
#define PROTOCOL_H_
#include "inttypes.h"
// -------------------------------------------------------------------------------------------
// General Section
// -------------------------------------------------------------------------------------------
/**
* The size of the message in bytes
*/
#define MSG_SIZE 32
/**
* Default value if a message header is not required
*/
#define BLANK 0x00
// -------------------------------------------------------------------------------------------
// Start and Stop Section
// -------------------------------------------------------------------------------------------
/**
* Location of the start byte
*/
#define I_START 0x00
/**
* Location of the stop byte
*/
#define I_END 0x1F
/**
* Value of the start byte
*/
#define MSG_START 0xFF
/**
* Value of the stop byte
*/
#define MSG_END 0xFE
// -------------------------------------------------------------------------------------------
// Type Section
// -------------------------------------------------------------------------------------------
/**
* Location of the type header
*/
#define I_TYPE 0x01
/**
* Value if the message is a response
*/
#define TYPE_RSP 0x01
/**
* Value if the message is a command
*/
#define TYPE_CMD 0x02
// -------------------------------------------------------------------------------------------
// Command Section
// -------------------------------------------------------------------------------------------
/**
* Location of the command byte
*/
#define I_CMD 0x02
/**
* Stop Command
*
* Tells the bartender to stop any actions
* and any pending actions.
*/
#define CMD_STOP 0x01
/**
* Move Command <location>
*
* Tells the bartender to move to a pin location
* 0 is a magic value for the home position
*/
#define CMD_MOVE 0x02
/**
* The parameter of the move command. Must be in between 0-12.
*/
#define PARAM_MOVE_LOC 0x04
/**
* Pour Command <amount>
*
* Tells the bartender to pour an amount from the
* current location
*/
#define CMD_POUR 0x03
/**
* The parameter of the pour command. Defines the amount of shots to be poured
* into the glass.
*/
#define PARAM_POUR_AMOUNT 0x04
/**
* Status Command
*
* Returns the status of the bartender. If the bartender
* is busy executing a command, the response will be
* RSP_BUSY but if the bartender is not executing a command
* RSP_WAITING is returned
*/
#define CMD_STATUS 0x04
/**
* The response to the status command. Contains the current status of the
* bartender.
*/
#define RES_STATUS_STATUS 0x04
/**
* Location Command
*
* Returns the current location of bartender
*/
#define CMD_LOCATION 0x05
// --------------------------------------------------------
// Response Section
// --------------------------------------------------------
/**
* Location of the response code
*/
#define I_RSP_CODE 0x03
/**
* We have received the command and it is of valid syntax
*/
#define RSP_OK 0x01
/**
* We have completed the command
*/
#define RSP_COMPLETE 0x05
/**
* An general error response. Try to avoid this code and use a more useful code.
*/
#define RSP_ERROR 0x02
/**
* Malformed message error. Sent when a message is received that does not have the
* start and stop byte.
*/
#define RSP_MAL_MSG 0x03
/**
* Unknown command error. Send when a message is received with a command code that is not
* recognized by the handler.
*/
#define RSP_UNK_CMD 0x04
/**
* Unknown type error. Sent when a message is received with response code that is not defined
* by the protocol.
*/
#define RSP_UNK_TYPE 0x06
/**
* Not implemented error. Sent when a message is received that is defined by the protocol but
* the command can not be executed due to lack of implementation. Mostly used when debugging however
* this code is useful if new versions of the protocol arise.
*/
#define RSP_NOT_IMPL 0x07
/**
* Fatal error code. This means that a fatal error occurred and the device's state is no longer known.
* Basically if this error code is received the device can not be counted on to do normal operation and
* should be reset.
*/
#define RSP_FATAL_ERROR 0x08
/**
* Queue full response. Send when a message would overflow the message queue. This means that the message
* has been ignored.
*/
#define RSP_QUEUE_FULL 0x09
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @name Protocol Build Error Response
* @brief Build a protocol error response
* @ingroup protocol
*
* This function build a protocol error response in the passed in parameter of buffer.
*
* @param [out] buffer a buffer that is of length MSG_SIZE that the error response will be
* written to
* @param [in] cmd the command code that the error is responding to
* @param [in] code the error response code that the message should contain
*
*/
void protocol_build_error_rsp(uint8_t *buffer, uint8_t cmd, uint8_t code);
/**
* @name Protocol Build Ok Response
* @brief Build a protocol ok response
* @ingroup protocol
*
* This function build a protocol ok response in the passed in parameter of buffer.
*
* @param [out] buffer a buffer that is of length MSG_SIZE that the response will be
* written to
* @param [in] cmd the command code that the message is responding to
*
*/
void protocol_build_ok_rsp(uint8_t *buffer, uint8_t cmd);
/**
* @name Protocol Build Complete Response
* @brief Build a protocol complete response
* @ingroup protocol
*
* This function build a protocol complete response in the passed in parameter of buffer.
*
* @param [out] buffer a buffer that is of length MSG_SIZE that the response will be
* written to
* @param [in] cmd the command code that the message is responding to
*
*/
void protocol_build_complete_rsp(uint8_t *buffer, uint8_t cmd);
#ifdef __cplusplus
}
#endif
#endif /* PROTOCOL_H_ */