fix for two-byte enhanced transfer polling, fix unnecessary buffer allocation, fix skip for invalid protocol bytes
This commit is contained in:
+25
-15
@@ -183,6 +183,11 @@ result_t Device::recv(unsigned int timeout, symbol_t* value, ArbitrationState* a
|
||||
if (!isValid()) {
|
||||
return RESULT_ERR_DEVICE;
|
||||
}
|
||||
bool repeat = false;
|
||||
bool repeated = false;
|
||||
ArbitrationState prevState = *arbitrationState;
|
||||
do {
|
||||
repeat = false;
|
||||
bool isAvailable = available();
|
||||
if (!isAvailable && timeout > 0) {
|
||||
int ret;
|
||||
@@ -229,12 +234,17 @@ result_t Device::recv(unsigned int timeout, symbol_t* value, ArbitrationState* a
|
||||
}
|
||||
}
|
||||
|
||||
ArbitrationState prevState = *arbitrationState;
|
||||
// directly read byte from device
|
||||
if (!read(value, isAvailable, arbitrationState)) {
|
||||
close();
|
||||
return RESULT_ERR_DEVICE;
|
||||
bool incomplete = false;
|
||||
if (!read(value, isAvailable, arbitrationState, &incomplete)) {
|
||||
if (!isAvailable && incomplete && !repeated) {
|
||||
// for a two-byte transfer another poll is needed
|
||||
repeat = true;
|
||||
continue;
|
||||
}
|
||||
return RESULT_ERR_TIMEOUT;
|
||||
}
|
||||
} while (repeat);
|
||||
if (m_enhancedProto || *value != SYN || m_arbitrationMaster == SYN) {
|
||||
if (m_listener != nullptr) {
|
||||
m_listener->notifyDeviceData(*value, true);
|
||||
@@ -316,12 +326,16 @@ bool Device::available() {
|
||||
if ((ch&ENH_BYTE_MASK) == ENH_BYTE1) {
|
||||
return pos+1 < m_bufLen;
|
||||
}
|
||||
// TODO check protocol error
|
||||
// TODO protocol error
|
||||
// skip byte from erroneous protocol
|
||||
m_bufPos = (m_bufPos+1)%m_bufSize;
|
||||
m_bufLen--;
|
||||
pos--;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Device::read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrationState) {
|
||||
bool Device::read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrationState, bool* incomplete) {
|
||||
if (!isAvailable) {
|
||||
if (m_bufLen > 0 && m_bufPos != 0) {
|
||||
if (m_bufLen > m_bufSize / 2) {
|
||||
@@ -350,6 +364,9 @@ bool Device::read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrati
|
||||
m_bufLen += size;
|
||||
}
|
||||
if (!available()) {
|
||||
if (incomplete) {
|
||||
*incomplete = m_enhancedProto && m_bufLen > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!m_enhancedProto) {
|
||||
@@ -377,11 +394,11 @@ bool Device::read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrati
|
||||
}
|
||||
// kind is ENH_BYTE1
|
||||
symbol_t ch2 = m_buffer[m_bufPos];
|
||||
m_bufPos = (m_bufPos + 1) % m_bufSize;
|
||||
m_bufLen--;
|
||||
if ((ch2 & ENH_BYTE_MASK) != ENH_BYTE2) {
|
||||
return false; // TODO protocol error
|
||||
}
|
||||
m_bufPos = (m_bufPos + 1) % m_bufSize;
|
||||
m_bufLen--;
|
||||
ch2 = (symbol_t)(((ch&0x03)<<6) | (ch2&0x3f));
|
||||
ch = (ch>>2)&0xf;
|
||||
switch (ch) {
|
||||
@@ -577,13 +594,6 @@ result_t NetworkDevice::open() {
|
||||
close();
|
||||
return RESULT_ERR_GENERIC_IO;
|
||||
}
|
||||
if (m_bufSize == 0) {
|
||||
m_bufSize = MAX_LEN+1;
|
||||
m_buffer = reinterpret_cast<symbol_t*>(malloc(m_bufSize));
|
||||
if (!m_buffer) {
|
||||
m_bufSize = 0;
|
||||
}
|
||||
}
|
||||
m_bufLen = 0;
|
||||
if (m_initialSend && !write(ESC)) {
|
||||
return RESULT_ERR_SEND;
|
||||
|
||||
@@ -199,9 +199,10 @@ class Device {
|
||||
* @param value the reference in which the read byte value is stored.
|
||||
* @param isAvailable the result of the immediately preceding call to @a available().
|
||||
* @param arbitrationState the variable in which to store the received arbitration state (mandatory for enhanced proto).
|
||||
* @param incomplete the variable in which to store when a partial transfer needs another poll.
|
||||
* @return true on success, false on error.
|
||||
*/
|
||||
virtual bool read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrationState=nullptr);
|
||||
virtual bool read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrationState=nullptr, bool* incomplete=nullptr);
|
||||
|
||||
/** the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network). */
|
||||
const char* m_name;
|
||||
|
||||
Reference in New Issue
Block a user