-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathESP8266pro_Parser.cpp
394 lines (336 loc) · 8.16 KB
/
ESP8266pro_Parser.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// ESP8266pro
// This software distributed under the terms of the MIT license
// (c) Skurydin Alexey, 2014
#include "ESP8266pro_Parser.h"
ESP8266pro_Parser::ESP8266pro_Parser(Stream &espStreamReference)
: espStream(espStreamReference), debugStream(NULL)
{
state = ePS_NotInitialized;
}
ESP8266pro_Parser::ESP8266pro_Parser(Stream &espStreamReference, Stream &debugStreamReference)
: espStream(espStreamReference), debugStream(&debugStreamReference)
{
state = ePS_NotInitialized;
}
ESP8266pro_Parser::~ESP8266pro_Parser()
{
}
bool ESP8266pro_Parser::initializeParser(OutputDebugMode debugOutMode/* = eODM_Data*/)
{
#ifndef NODEBUG
debugMode = debugOutMode;
#endif
if (!execute(debugOutMode == eODM_FullDump ? "ATE1" : "ATE0")) // Enable/Disable echo
return false;
state = ePS_Ready;
return execute("AT");
}
void ESP8266pro_Parser::restart()
{
execute("AT+RST");
delay(3500);
while (espStream.available())
{
char dbg[] = {0, 0};
dbg[0] = espStream.read();
#ifndef NODEBUG
if ((debugMode & eODM_Dump) == eODM_Dump)
debugPrint(dbg, false);
#endif
if (!espStream.available())
delay(180);
}
#ifndef NODEBUG
initializeParser(debugMode);
#else
initializeParser(eODM_None);
#endif
#ifndef NODEBUG
if ((debugMode & eODM_Dump) == eODM_Dump)
debugPrint("\r\n");
#endif
}
ProcessingState ESP8266pro_Parser::getState()
{
return state;
}
bool ESP8266pro_Parser::execute(const String& msg, CommandExecutionMode mode/* = eCEM_DeafaultWithSelector*/)
{
resetParser();
// First selector start
int splt = msg.indexOf("+");
if (splt == -1)
splt = 0;
else
splt++;
String selector = "";
for (int i = splt + 1; true; i++)
{
if (!isalpha(msg[i]))
{
selector = msg.substring(splt, i);
break;
}
else if (i == msg.length() - 1)
{
selector = msg.substring(splt);
break;
}
}
#ifndef NODEBUG
if (debugMode == eODM_Data || debugMode == eODM_Dump)
debugPrint(msg);
#endif
espStream.print(msg);
if (mode != eCEM_NoLineBreak)
espStream.print(ESP_LINE_WRAPPER);
delay(3);
int timeOut = 1800;
if (mode == eCEM_ShortTimeOut)
timeOut = 1100;
else if (mode == eCEM_LongTimeOut)
timeOut = 7500;
if (mode != eCEM_NoResponse)
parseResponse(selector.c_str(), timeOut);
else
state = ePS_OK;
/*debugPrint("RESP0: " + getLine(0));
debugPrint("RESP1: " + getLine(1));
debugPrint("RESP00: " + getLineItem(0, 0));
debugPrint("RESP10: " + getLineItem(1, 0));
debugPrint("RESP11: " + getLineItem(1, 1));
debugPrint("RESP04: " + getLineItem(0, 4));*/
return state == ePS_OK || state == ePS_Completed;
}
void ESP8266pro_Parser::resetParser()
{
response = "";
state = ePS_Ready;
}
String ESP8266pro_Parser::getLine(uint8_t lineId)
{
uint8_t line = 0;
int start = 0;
for (int i = 0; i < response.length(); i++)
{
if (response[i] == '\n')
{
if (line == lineId)
{
return trimResponse(response.substring(start, i));
}
line++;
start = i + 1;
}
}
return "";
}
String ESP8266pro_Parser::getLineItem(uint8_t lineId, uint8_t itemId)
{
String line = getLine(lineId);
line += ","; // to simplify parsing loop breaking
uint8_t item = 0;
int start = 0;
bool insideItem = false;
for (int i = 0; i < line.length(); i++)
{
if (line[i] == '\"')
insideItem = !insideItem;
if (line[i] == ',' && !insideItem)
{
if (item == itemId)
{
return trimResponse(line.substring(start, i));
}
item++;
start = i + 1;
}
}
return "";
}
int ESP8266pro_Parser::getLinesCount()
{
int k = 0;
for (int i = 0; i < response.length(); i++)
if (response[i] == '\n')
k++;
return k;
}
bool ESP8266pro_Parser::connectionDataReceive(bool waitData/* = false*/)
{
if (!waitData && espStream.available() < 5)
return false;
if (!espStream.find(ESP_IP_DATA_HANDLER))
return false;
processConnectionDataReceive();
return true;
}
void ESP8266pro_Parser::processConnectionDataReceive(bool processData/* = true*/)
{
String sid = espStream.readStringUntil(',');
String slen = espStream.readStringUntil(':');
int id = sid.toInt();
int len = slen.toInt();
#ifndef NODEBUG
if (debugMode != eODM_None)
{
debugPrint("<IP data receive #", false);
debugPrint(sid, false);
debugPrint(" size=", false);
debugPrint(slen, false);
debugPrint("/>", true);
}
#endif
onDataReceive(id, NULL, 0, eDRA_Begin);
char *buffer = (char*)malloc(ESP_RECEIVE_BUFSIZE + 1);
unsigned char repeats = 0;
while (len > 0 && repeats < 2)
{
int k = min(len, ESP_RECEIVE_BUFSIZE);
size_t readed = espStream.readBytes(buffer, k);
if (readed == 0)
{
repeats++;
continue;
}
else
repeats = 0;
buffer[readed] = 0;
len -= readed;
#ifndef NODEBUG
if ((debugMode & eODM_Dump) == eODM_Dump)
debugPrint(buffer, false);
#endif
if (processData)
onDataReceive(id, buffer, readed, (len == 0 ? eDRA_End : eDRA_Packet));
}
// We don't receive full data
if (len > 0)
{
// May be enlarge buffer sizes? =)
#ifndef NODEBUG
debugPrint("\r\n<Data lost>");
#endif
onDataReceive(id, "", 0, eDRA_End); // Force complete request processing if data corrupted
}
#ifndef NODEBUG
debugPrint("\r\n</IP data receive>");
#endif
free(buffer);
buffer = NULL;
}
void ESP8266pro_Parser::onDataReceive(uint8_t connectionId, char* buffer, int length, DataReceiveAction action)
{
}
String ESP8266pro_Parser::trimResponse(String originalLine)
{
originalLine.trim();
unsigned char quotesCount = 0;
unsigned char bracketsCount = 0;
const char* cstr = originalLine.c_str();
const char* cend;
for (const char* p = cstr; *p; p++)
{
if (*p == '\"')
quotesCount++;
if (*p == '(' || *p == ')')
bracketsCount++;
cend = p;
}
if ( (*cstr == '(' && *cend == ')' && bracketsCount == 2) || (*cstr == '\"' && *cend == '\"' && quotesCount == 2) )
return trimResponse(originalLine.substring(1, originalLine.length()-1));
else
return originalLine;
}
void ESP8266pro_Parser::writeString(const __FlashStringHelper* data)
{
espStream.print(data);
}
bool ESP8266pro_Parser::parseResponse(const char* selector, int msTimeOut/* = 1500*/)
{
//String selectorMsg = (String)"+" + selector + ":";
int sellen = strlen(selector);
resetParser();
unsigned long startMillis = millis();
while (millis() - startMillis < msTimeOut)
{
String line = readLine();
const char* lstr = line.c_str();
if (*lstr) startMillis = millis();
//if (line.startsWith(selectorMsg))
if (lstr[0] == '+' && strncmp(lstr + 1, selector, sellen) == 0 && lstr[1 + sellen] == ':')
{
String responseData = line.substring(sellen + 2);
response += responseData + "\n";
#ifndef NODEBUG
if (debugMode == eODM_Data)
debugPrint(responseData);
#endif
}
#ifndef NODEBUG
if ((debugMode & eODM_Dump) == eODM_Dump)
debugPrint(lstr);
#endif
if (strcmp(lstr, ESP_STATUS_OK)==0 || strcmp(lstr, ESP_STATUS_NO_CHANGE)==0)
{
state = ePS_OK;
return true;
}
else if (strcmp(lstr, ESP_STATUS_SEND_OK)==0)
{
state = ePS_Completed;
return true;
}
else if (strcmp(lstr, ESP_STATUS_ERR)==0 || strcmp(lstr, ESP_STATUS_NO_LINK)==0 || strcmp(lstr, ESP_STATUS_ONLINK)==0)
{
state = ePS_Error;
#ifndef NODEBUG
if (debugMode == eODM_Data)
debugPrint("<ERROR>");
#endif
return false;
}
}
#ifndef NODEBUG
debugPrint("<NoResponse>");
#endif
state = ePS_NoResponse;
return false;
}
String ESP8266pro_Parser::readLine()
{
char lineSplitChar = ESP_LINE_WRAPPER[strlen(ESP_LINE_WRAPPER) - 1];
String data = "";
if (espStream.peek() == '+')
{
char buffer[8] = {0};
espStream.readBytes(buffer, strlen(ESP_IP_DATA_HANDLER));
if (strcmp(buffer, ESP_IP_DATA_HANDLER) == 0) // Begin of IP data
{
#ifndef NODEBUG
debugPrint("<WRAP processing>");
#endif
processConnectionDataReceive(true);
}
else
data = String(buffer);
}
data += espStream.readStringUntil(lineSplitChar);
if (data.endsWith("\r"))
data.remove(data.length() - 1);
return data;
}
#ifndef NODEBUG
void ESP8266pro_Parser::debugPrint(const String& text, bool lineWrap/* = true*/)
{
debugPrint(text.c_str(), lineWrap);
}
void ESP8266pro_Parser::debugPrint(const char* text, bool lineWrap/* = true*/)
{
if (debugStream == NULL || debugMode == eODM_None) return;
if (lineWrap)
debugStream->println(text);
else
debugStream->print(text);
}
#endif