2020-10-13 18:37:40 +02:00
|
|
|
/*
|
2020-10-20 13:48:32 +02:00
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of DCC-EX CommandStation-EX
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-10-13 18:37:40 +02:00
|
|
|
|
|
|
|
#include "RingStream.h"
|
|
|
|
#include "DIAG.h"
|
|
|
|
|
|
|
|
RingStream::RingStream( const uint16_t len)
|
|
|
|
{
|
|
|
|
_len=len;
|
|
|
|
_buffer=new byte[len];
|
|
|
|
_pos_write=0;
|
|
|
|
_pos_read=0;
|
|
|
|
_buffer[0]=0;
|
|
|
|
_overflow=false;
|
2020-10-26 13:59:40 +01:00
|
|
|
_mark=0;
|
2020-10-27 11:38:30 +01:00
|
|
|
_count=0;
|
2020-10-13 18:37:40 +02:00
|
|
|
}
|
|
|
|
|
2020-10-26 13:59:40 +01:00
|
|
|
size_t RingStream::write(uint8_t b) {
|
2020-10-13 18:37:40 +02:00
|
|
|
if (_overflow) return 0;
|
2020-10-26 13:59:40 +01:00
|
|
|
_buffer[_pos_write] = b;
|
2020-10-13 18:37:40 +02:00
|
|
|
++_pos_write;
|
2020-10-27 11:38:30 +01:00
|
|
|
if (_pos_write==_len) _pos_write=0;
|
2020-10-13 18:37:40 +02:00
|
|
|
if (_pos_write==_pos_read) {
|
|
|
|
_overflow=true;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-27 11:38:30 +01:00
|
|
|
_count++;
|
2020-10-13 18:37:40 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RingStream::read() {
|
2020-10-27 11:38:30 +01:00
|
|
|
if ((_pos_read==_pos_write) && !_overflow) return -1; // empty
|
2020-10-13 18:37:40 +02:00
|
|
|
byte b=_buffer[_pos_read];
|
|
|
|
_pos_read++;
|
2020-10-27 11:38:30 +01:00
|
|
|
if (_pos_read==_len) _pos_read=0;
|
2020-10-13 18:37:40 +02:00
|
|
|
_overflow=false;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int RingStream::count() {
|
2020-10-27 11:38:30 +01:00
|
|
|
return (read()<<8) | read();
|
2020-10-13 18:37:40 +02:00
|
|
|
}
|
2020-10-26 13:59:40 +01:00
|
|
|
|
|
|
|
int RingStream::freeSpace() {
|
2020-10-27 11:38:30 +01:00
|
|
|
// allow space for client flag and length bytes
|
|
|
|
if (_pos_read>_pos_write) return _pos_read-_pos_write-3;
|
|
|
|
else return _len - _pos_write + _pos_read-3;
|
2020-10-26 13:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-27 11:38:30 +01:00
|
|
|
// mark start of message with client id (0...9)
|
|
|
|
void RingStream::mark(uint8_t b) {
|
2020-10-26 13:59:40 +01:00
|
|
|
_mark=_pos_write;
|
2020-10-27 11:38:30 +01:00
|
|
|
write(b); // client id
|
|
|
|
write((uint8_t)0); // count MSB placemarker
|
|
|
|
write((uint8_t)0); // count LSB placemarker
|
|
|
|
_count=0;
|
2020-10-26 13:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RingStream::commit() {
|
|
|
|
if (_overflow) {
|
2020-10-27 11:38:30 +01:00
|
|
|
DIAG(F("\nRingStream(%d) commit(%d) OVERFLOW\n"),_len, _count);
|
|
|
|
// just throw it away
|
|
|
|
_pos_write=_mark;
|
|
|
|
_overflow=false;
|
|
|
|
return false; // commit failed
|
|
|
|
}
|
|
|
|
if (_count==0) {
|
|
|
|
// ignore empty response
|
2020-10-26 13:59:40 +01:00
|
|
|
_pos_write=_mark;
|
2020-10-27 11:38:30 +01:00
|
|
|
return true; // true=commit ok
|
2020-10-26 13:59:40 +01:00
|
|
|
}
|
2020-10-27 11:38:30 +01:00
|
|
|
// Go back to the _mark and inject the count 1 byte later
|
|
|
|
_mark++;
|
|
|
|
if (_mark==_len) _mark=0;
|
|
|
|
_buffer[_mark]=highByte(_count);
|
|
|
|
_mark++;
|
|
|
|
if (_mark==_len) _mark=0;
|
|
|
|
_buffer[_mark]=lowByte(_count);
|
2020-10-26 13:59:40 +01:00
|
|
|
return true; // commit worked
|
|
|
|
}
|
2020-11-09 15:41:17 +01:00
|
|
|
|
|
|
|
//grbba
|
|
|
|
byte *RingStream::getBuffer() {
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RingStream::printStream() {
|
|
|
|
DIAG(F(" _len %d _pos_write %d _pos_read %d _overflow %d _mark %d _count %d\n"), _len, _pos_write, _pos_read, _overflow, _mark, _count);
|
|
|
|
};
|
|
|
|
|
|
|
|
void RingStream::resetStream()
|
|
|
|
{
|
|
|
|
memset(_buffer, 0, _len);
|
|
|
|
_pos_write=0;
|
|
|
|
_pos_read=0;
|
|
|
|
_buffer[0]=0;
|
|
|
|
_overflow=false;
|
|
|
|
_mark=0;
|
|
|
|
_count=0;
|
|
|
|
}
|
|
|
|
//grbba
|