-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ino
313 lines (282 loc) · 8.95 KB
/
parser.ino
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#ifndef __rolloff_linear_actuator_parser__
#define __rolloff_linear_actuator_parser__
/*
Command Format: (command:target|state:value)
Response Format: (response:target|state:value)
Command:
CON (CON:0:0) Establish initial connection with Arduino
GET (GET:state:value) Get state of a switch
SET (SET:target:value) Set relay closed or open
state: OPENED | CLOSED | LOCKED | AUXSTATE
target: OPEN | CLOSE | ABORT | LOCK | AUXSET
value: ON | OFF | 0 | text-message
Response:
ACK Success returned from Arduino
NAK Failure returned from Arduino
Examples: From the Driver Response From the Arduino
---------------- --------------------------
Initial connect (CON:0:0) >
< (ACK:0:version) | (NAK:ERROR:message)
Read a switch (GET:OPENED:0) >
< (ACK:OPENED:ON|OFF) | (NAK:ERROR:message)
Set a relay (SET:CLOSE:ON|OFF) >
< (ACK:CLOSE:ON|OFF) | (NAK:ERROR:message)
*/
void sendAck(char* val) {
char response[MAX_RESPONSE_TEXT];
DEBUG_INFO("ACK=%s", val);
if (strlen(val) > MAX_MESSAGE_TEXT) {
strncpy(response, val, MAX_MESSAGE_TEXT - 3);
strcpy(&response[MAX_MESSAGE_TEXT - 3], "...");
sendNak(ERROR1);
sendNak(response);
} else {
strcpy(response, "(ACK:");
strcat(response, target);
strcat(response, ":");
strcat(response, val);
strcat(response, ")");
if (USE_WIFI == 1) {
DEBUG_VERBOSE("about to send response: %s", response);
client.println(response);
client.flush();
} else {
Serial.println(response);
Serial.flush();
}
}
}
void sendNak(const char* errorMsg) {
char buffer[MAX_RESPONSE_TEXT];
DEBUG_INFO("NACK=%s", errorMsg);
if (strlen(errorMsg) > MAX_MESSAGE_TEXT) {
strncpy(buffer, errorMsg, MAX_MESSAGE_TEXT - 3);
strcpy(&buffer[MAX_MESSAGE_TEXT - 3], "...");
sendNak(ERROR2);
sendNak(buffer);
} else {
strcpy(buffer, "(NAK:ERROR:");
strcat(buffer, value);
strcat(buffer, ":");
strcat(buffer, errorMsg);
strcat(buffer, ")");
if (USE_WIFI == 1) {
client.println(buffer);
// client.flush();
} else {
Serial.println(buffer);
Serial.flush();
}
}
}
int read_data(char* inpBuf, int offset) {
int recv_count = 0;
if (USE_WIFI == 1) {
if (client.available() > 0) {
recv_count = client.read((unsigned char*)inpBuf + offset, 1);
DEBUG_DEBUG("Reading data: %d '%s'", recv_count, inpBuf);
} else {
DEBUG_VERBOSE("read data no data available");
}
} else {
if (Serial.available() > 0) {
Serial.setTimeout(1000);
recv_count = Serial.readBytes((inpBuf + offset), 1);
}
}
return recv_count;
}
char* safeStrTok(char* input, const char* delimiter, char* output) {
char* pointer = NULL;
pointer = strtok(input, delimiter);
if (pointer)
strcpy(output, pointer);
else
output[0] = '\0';
return pointer;
}
bool receiveCommand() // (command:target:value)
{
bool start = false;
bool eof = false;
int recv_count = 0;
int wait = 0;
int offset = 0;
char startToken = '(';
char endToken = ')';
const int bLen = MAX_INPUT_TEXT;
char inpBuf[bLen + 1];
memset(inpBuf, 0, sizeof(inpBuf));
memset(command, 0, sizeof(command));
memset(target, 0, sizeof(target));
memset(value, 0, sizeof(value));
while (!eof && (wait < 20)) {
recv_count = read_data(inpBuf, offset);
if (recv_count == 1) {
offset++;
if (offset >= MAX_INPUT_TEXT) {
sendNak(ERROR3);
return false;
}
if (inpBuf[offset - 1] == startToken) {
start = true;
}
if (inpBuf[offset - 1] == endToken) {
eof = true;
inpBuf[offset] = '\0';
}
continue;
}
wait++;
// delay(100);
}
DEBUG_INFO("Received command=%s", inpBuf);
if (!start || !eof) {
if (!start && !eof) {
sendNak(ERROR4);
} else if (!start) {
sendNak(ERROR5);
} else if (!eof) {
sendNak(ERROR6);
}
return false;
} else {
safeStrTok(inpBuf, "(:", command);
safeStrTok(NULL, ":", target);
safeStrTok(NULL, ")", value);
if ((strlen(command) >= 3) && (strlen(target) >= 1) && (strlen(value) >= 1)) {
DEBUG_INFO("cmd=%s, t=%s, v=%s", command, target, value);
return true;
} else {
sendNak(ERROR7);
return false;
}
}
}
bool is_data_available() {
bool result = false;
if (USE_WIFI == 1)
result = (client && (client.available() > 0));
else
result = (Serial && (Serial.available() > 0));
return result;
}
/*
* Use the parseCommand routine to decode message
* Determine associated action in the message. Resolve the relay or switch associated
* pin with the target identity. Acknowledge any initial connection request. Return
* negative acknowledgement with message for any errors found. Dispatch to commandReceived
* or requestReceived routines to activate the command or get the requested switch state
*/
void parseCommand(Motor* m) {
// Confirm there is input available, read and parse it.
if (is_data_available()) {
DEBUG_VERBOSE("Data is available");
if (receiveCommand()) {
unsigned long timeNow = millis();
int relay = -1; // -1 = not found, 0 = not implemented, pin number = supported
int sw = -1; // " " "
bool connecting = false;
const char* error = ERROR8;
// On initial connection return the version
if (strcmp(command, "CON") == 0) {
connecting = true;
strcpy(value, m->getVersion()); // Can be seen on host to confirm what is running
m->runCommand(CMD_CONNECT, value);
sendAck(value);
}
// Map the general input command term to the local action
// SET: OPEN, CLOSE, ABORT, LOCK, AUXSET
else if (strcmp(command, "SET") == 0) {
// Prepare to OPEN
if (strcmp(target, "OPEN") == 0) {
command_input = CMD_OPEN;
relay = FUNC_ACTIVATION;
}
// Prepare to CLOSE
else if (strcmp(target, "CLOSE") == 0) {
command_input = CMD_CLOSE;
relay = FUNC_ACTIVATION;
}
// Prepare to ABORT
else if (strcmp(target, "ABORT") == 0) {
command_input = CMD_STOP;
// Test whether or not to Abort
if (!m->isStopAllowed()) {
error = ERROR10;
} else {
relay = FUNC_STOP;
}
}
// Prepare to ABORT
else if (strcmp(target, "STOP") == 0) {
command_input = CMD_STOP;
relay = FUNC_STOP;
}
// Prepare to ABORT
else if (strcmp(target, "DISABLE") == 0) {
command_input = CMD_STOP;
relay = FUNC_STOP;
}
// Prepare for the Lock function
else if (strcmp(target, "LOCK") == 0) {
command_input = CMD_LOCK;
relay = FUNC_LOCK;
}
// Prepare for the Auxiliary function
else if (strcmp(target, "AUXSET") == 0) {
command_input = CMD_AUXSET;
relay = FUNC_AUX;
}
}
// Handle requests to obtain the status of switches
// GET: OPENED, CLOSED, LOCKED, AUXSTATE
else if (strcmp(command, "GET") == 0) {
if (strcmp(target, "OPENED") == 0) {
sw = SWITCH_OPENED;
} else if (strcmp(target, "CLOSED") == 0) {
sw = SWITCH_CLOSED;
} else if (strcmp(target, "LOCKED") == 0) {
sw = SWITCH_LOCKED;
} else if (strcmp(target, "AUXSTATE") == 0) {
sw = SWITCH_AUX;
}
}
/*
* See if there was a valid command or request
*/
if (!connecting) {
if ((relay == -1) && (sw == -1)) {
sendNak(error); // Unknown input or Abort command was rejected
}
// Command or Request not implemented
else if ((relay == 0 || relay == -1) && (sw == 0 || sw == -1)) {
DEBUG_ERROR("Command or request not implemented");
strcpy(value, "OFF"); // Request Not implemented
//sendNak(ERROR9);
sendAck(value);
}
// Valid input received
// A command was received
// Set the relay associated with the command and send acknowlege to host
else if (relay > 0) // Set Relay response
{
m->runCommand(command_input, value);
// Send acknowledgement that relay pin associated with "target" was activated to value requested
sendAck(value);
}
// A state request was received
else if (sw > 0) // Get switch response
{
DEBUG_VERBOSE("about to get Status");
getSwitch(sw, value);
sendAck(value); // Send result of reading pin associated with "target"
}
} // end !connecting
} // end command parsed
} // end input found
else {
DEBUG_DEBUG("No data available. Continue...");
}
}
#endif