Skip to content

Commit

Permalink
making TextIO more robust to bad input
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Unger committed Jan 16, 2024
1 parent d5748ec commit 76f6514
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
25 changes: 15 additions & 10 deletions src/comms/streams/PacketCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,32 @@ void PacketCommander::run() {
handleRegisterPacket(!_io->is_complete(), curRegister);
lastcommanderror = commanderror;
lastcommandregister = curRegister;
if (commanderror) {
_io->in_sync = false;
//*_io << START_PACKET(PacketType::ALERT, 1) << '?' << END_PACKET;
}
}
else if (curr_packet.type == PacketType::SYNC) {
*_io << START_PACKET(PacketType::SYNC, 1);
*_io << (uint8_t)0x01;
*_io << END_PACKET;
// TODO flush packet
}
else
_io->in_sync = false; // TODO flag in another way?
else {
_io->in_sync = false; // TODO it would be better just to reset the buffer, since we just saw a newline
}

if (! _io->is_complete())
_io->in_sync = false;
}
};



void PacketCommander::handleRegisterPacket(bool write, uint8_t reg) {
if (write) {
bool ok = commsToRegister(reg);
commanderror = commanderror && !ok;
commanderror = commanderror || !ok;
}
if (!write || echo) {
uint8_t size = SimpleFOCRegisters::regs->sizeOfRegister(reg);
Expand All @@ -70,7 +76,7 @@ void PacketCommander::handleRegisterPacket(bool write, uint8_t reg) {
// TODO flush packet
}
}
}
};



Expand All @@ -87,23 +93,22 @@ bool PacketCommander::commsToRegister(uint8_t reg){
default:
return SimpleFOCRegisters::regs->commsToRegister(*_io, reg, motors[curMotor]);
}
}
};



bool PacketCommander::registerToComms(uint8_t reg){
switch (reg) {
case REG_STATUS:
case SimpleFOCRegister::REG_STATUS:
// TODO implement status register
return true;
case REG_MOTOR_ADDRESS:
case SimpleFOCRegister::REG_MOTOR_ADDRESS:
*_io << curMotor;
return true;
case REG_NUM_MOTORS:
case SimpleFOCRegister::REG_NUM_MOTORS:
*_io << numMotors;
return true;
default:
return SimpleFOCRegisters::regs->registerToComms(*_io, reg, motors[curMotor]);
}
}

};
20 changes: 14 additions & 6 deletions src/comms/streams/TextIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ uint32_t TextIO::intFromBuffer() {
}
}
return value;
}
};


TextIO& TextIO::operator>>(float &value) {
Expand Down Expand Up @@ -150,7 +150,8 @@ TextIO& TextIO::operator>>(uint8_t &value) {

TextIO& TextIO::operator>>(Packet &value) {
while (!in_sync && _io.available() > 0) {
if (_io.read() == '\n') {
char skip = _io.read();
if (skip == '\n' || skip == '\r') {
in_sync = true;
buffer_len = 0;
}
Expand All @@ -159,13 +160,16 @@ TextIO& TextIO::operator>>(Packet &value) {
buffer_len = 0;
buffer_index = 0;
}
while (in_sync && _io.available()>0) {
int avail = _io.available();
while (in_sync && avail>0) {
uint8_t peek = _io.peek();
if (peek == '\n' || peek == '\r') {
// skip newlines and carriage returns
while (_io.available()>0 && (peek == '\n' || peek == '\r')) {
while (avail>0 && (peek == '\n' || peek == '\r')) {
_io.read(); // discard the \n
peek = _io.peek();
avail = _io.available();
if (avail>0)
peek = _io.peek();
}
if (buffer_len>1) {
value.type = buffer[0];
Expand All @@ -174,10 +178,14 @@ TextIO& TextIO::operator>>(Packet &value) {
buffer_index = 1;
return *this;
}
else
buffer_len = 0;
}
else {
if (buffer_len < SIMPLEFOC_TEXTIO_BUFFER_SIZE)
if (buffer_len < SIMPLEFOC_TEXTIO_BUFFER_SIZE) {
buffer[buffer_len++] = _io.read();
avail = _io.available();
}
else {
buffer_len = 0;
in_sync = false;
Expand Down

0 comments on commit 76f6514

Please sign in to comment.