mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-22 23:56:13 +01:00
serial manager loop that handles quoted strings
This commit is contained in:
parent
dd898d3c16
commit
14360b4198
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* © 2022 Paul M. Antoine
|
* © 2022 Paul M. Antoine
|
||||||
* © 2021 Chris Harlow
|
* © 2021 Chris Harlow
|
||||||
* © 2022 Harald Barth
|
* © 2022 2024 Harald Barth
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* This file is part of DCC++EX
|
* This file is part of DCC++EX
|
||||||
|
@ -23,6 +23,7 @@
|
||||||
#include "SerialManager.h"
|
#include "SerialManager.h"
|
||||||
#include "DCCEXParser.h"
|
#include "DCCEXParser.h"
|
||||||
#include "StringFormatter.h"
|
#include "StringFormatter.h"
|
||||||
|
#include "DIAG.h"
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
#ifdef SERIAL_BT_COMMANDS
|
#ifdef SERIAL_BT_COMMANDS
|
||||||
|
@ -36,6 +37,10 @@ BluetoothSerial SerialBT;
|
||||||
#endif //COMMANDS
|
#endif //COMMANDS
|
||||||
#endif //ESP32
|
#endif //ESP32
|
||||||
|
|
||||||
|
static const byte PAYLOAD_FALSE = 0;
|
||||||
|
static const byte PAYLOAD_NORMAL = 1;
|
||||||
|
static const byte PAYLOAD_STRING = 2;
|
||||||
|
|
||||||
SerialManager * SerialManager::first=NULL;
|
SerialManager * SerialManager::first=NULL;
|
||||||
|
|
||||||
SerialManager::SerialManager(Stream * myserial) {
|
SerialManager::SerialManager(Stream * myserial) {
|
||||||
|
@ -43,7 +48,7 @@ SerialManager::SerialManager(Stream * myserial) {
|
||||||
next=first;
|
next=first;
|
||||||
first=this;
|
first=this;
|
||||||
bufferLength=0;
|
bufferLength=0;
|
||||||
inCommandPayload=false;
|
inCommandPayload=PAYLOAD_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SerialManager::init() {
|
void SerialManager::init() {
|
||||||
|
@ -112,23 +117,39 @@ void SerialManager::loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SerialManager::loop2() {
|
void SerialManager::loop2() {
|
||||||
while (serial->available()) {
|
while (serial->available()) {
|
||||||
char ch = serial->read();
|
char ch = serial->read();
|
||||||
if (ch == '<') {
|
if (!inCommandPayload) {
|
||||||
inCommandPayload = true;
|
if (ch == '<') {
|
||||||
bufferLength = 0;
|
inCommandPayload = PAYLOAD_NORMAL;
|
||||||
buffer[0] = '\0';
|
bufferLength = 0;
|
||||||
}
|
buffer[0] = '\0';
|
||||||
else if (inCommandPayload) {
|
}
|
||||||
if (bufferLength < (COMMAND_BUFFER_SIZE-1))
|
} else { // if (inCommandPayload)
|
||||||
buffer[bufferLength++] = ch;
|
if (bufferLength < (COMMAND_BUFFER_SIZE-1))
|
||||||
if (ch == '>') {
|
buffer[bufferLength++] = ch;
|
||||||
buffer[bufferLength] = '\0';
|
if (inCommandPayload > PAYLOAD_NORMAL) {
|
||||||
DCCEXParser::parse(serial, buffer, NULL);
|
if (inCommandPayload > 32 + 2) { // String way too long
|
||||||
inCommandPayload = false;
|
ch = '>'; // we end this nonsense
|
||||||
break;
|
inCommandPayload = PAYLOAD_NORMAL;
|
||||||
}
|
DIAG(F("Parse error: Unbalanced string"));
|
||||||
|
// fall through to ending parsing below
|
||||||
|
} else if (ch == '"') { // String end
|
||||||
|
inCommandPayload = PAYLOAD_NORMAL;
|
||||||
|
continue; // do not fall through
|
||||||
|
} else
|
||||||
|
inCommandPayload++;
|
||||||
|
}
|
||||||
|
if (inCommandPayload == PAYLOAD_NORMAL) {
|
||||||
|
if (ch == '>') {
|
||||||
|
buffer[bufferLength] = '\0';
|
||||||
|
DCCEXParser::parse(serial, buffer, NULL);
|
||||||
|
inCommandPayload = PAYLOAD_FALSE;
|
||||||
|
break;
|
||||||
|
} else if (ch == '"') {
|
||||||
|
inCommandPayload = PAYLOAD_STRING;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,6 @@ private:
|
||||||
SerialManager * next;
|
SerialManager * next;
|
||||||
byte bufferLength;
|
byte bufferLength;
|
||||||
byte buffer[COMMAND_BUFFER_SIZE];
|
byte buffer[COMMAND_BUFFER_SIZE];
|
||||||
bool inCommandPayload;
|
byte inCommandPayload;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user