removed unused buffering in port and count dumped bytes instead of using tellp(), small fix in wait for bus after lost arbitration

This commit is contained in:
john30
2014-12-25 12:09:27 +01:00
parent 5d02119d1e
commit dd4127f57b
5 changed files with 63 additions and 145 deletions
+11 -9
View File
@@ -312,8 +312,10 @@ result_t BusHandler::handleSymbol()
} }
// send symbol if necessary // send symbol if necessary
result_t result;
if (sending == true) { if (sending == true) {
if (m_port->send(&sendSymbol, 1) == 1) result = m_port->send(sendSymbol);
if (result == RESULT_OK)
if (m_state == bs_ready) if (m_state == bs_ready)
timeout = m_busAcquireTimeout; timeout = m_busAcquireTimeout;
else else
@@ -321,26 +323,26 @@ result_t BusHandler::handleSymbol()
else { else {
sending = false; sending = false;
timeout = 0; timeout = 0;
setState(bs_skip, RESULT_ERR_SEND); setState(bs_skip, result);
} }
} }
// receive next symbol (optionally check reception of sent symbol) // receive next symbol (optionally check reception of sent symbol)
unsigned char recvSymbol; unsigned char recvSymbol;
ssize_t count = m_port->recv(timeout, 1, &recvSymbol); result = m_port->recv(timeout, recvSymbol);
if (count < 0) // count < 0 is a RESULT_ERR_ code if (result != RESULT_OK)
return setState(bs_skip, count); // TODO keep "no signal" within auto-syn state return setState(bs_skip, result); // TODO keep "no signal" within auto-syn state
//unsigned char recvSymbol = m_port->byte(); // TODO remove me
if (recvSymbol == SYN) { if (recvSymbol == SYN) {
if (sending == false && m_remainLockCount > 0) if (sending == false && m_remainLockCount > 0 && m_command.size() != 1)
m_remainLockCount--; m_remainLockCount--;
else if (sending == false && m_remainLockCount == 0 && m_command.size() == 1)
m_remainLockCount = 1; // wait for next AUTO-SYN after SYN / address / SYN (bus locked for own priority)
return setState(bs_ready, RESULT_SYN); return setState(bs_ready, RESULT_SYN);
} }
unsigned char headerLen, crcPos; unsigned char headerLen, crcPos;
result_t result;
switch (m_state) switch (m_state)
{ {
@@ -360,7 +362,7 @@ result_t BusHandler::handleSymbol()
return setState(bs_sendCmd, RESULT_OK); return setState(bs_sendCmd, RESULT_OK);
} }
// arbitration lost. if same priority class found, try again after next AUTO-SYN // arbitration lost. if same priority class found, try again after next AUTO-SYN
m_remainLockCount = isMaster(recvSymbol) ? 2 : 1; m_remainLockCount = isMaster(recvSymbol) ? 2 : 1; // number of SYN to wait for before next send try
if ((recvSymbol & 0x0f) != (sendSymbol & 0x0f) if ((recvSymbol & 0x0f) != (sendSymbol & 0x0f)
&& m_lockCount > m_remainLockCount) && m_lockCount > m_remainLockCount)
// if different priority class found, try again after N AUTO-SYN symbols (at least next AUTO-SYN) // if different priority class found, try again after N AUTO-SYN symbols (at least next AUTO-SYN)
+28 -72
View File
@@ -60,16 +60,16 @@ bool Device::isValid()
return true; return true;
} }
ssize_t Device::sendBytes(const unsigned char* buffer, size_t nbytes) result_t Device::send(const unsigned char value)
{ {
if (isValid() == false) if (isValid() == false)
return RESULT_ERR_DEVICE; return RESULT_ERR_DEVICE;
// write bytes to device // write bytes to device
return write(m_fd, buffer, nbytes); return write(m_fd, &value, 1) == 1 ? RESULT_OK : RESULT_ERR_SEND;
} }
ssize_t Device::recvBytes(const long timeout, size_t maxCount, unsigned char* buffer) result_t Device::recv(const long timeout, unsigned char& value)
{ {
if (isValid() == false) if (isValid() == false)
return RESULT_ERR_DEVICE; return RESULT_ERR_DEVICE;
@@ -106,41 +106,12 @@ ssize_t Device::recvBytes(const long timeout, size_t maxCount, unsigned char* bu
if (ret == 0) return RESULT_ERR_TIMEOUT; if (ret == 0) return RESULT_ERR_TIMEOUT;
} }
if (buffer != NULL) { // directly read byte from device
// read bytes from device directly into provided buffer ssize_t nbytes = read(m_fd, &value, 1);
ssize_t nbytes = read(m_fd, buffer, maxCount);
if (nbytes == 0)
return RESULT_ERR_EOF;
return nbytes;
}
if (maxCount > sizeof(m_buffer))
maxCount = sizeof(m_buffer);
// read bytes from device into temporary buffer
ssize_t nbytes = read(m_fd, m_buffer, maxCount);
if (nbytes == 0) if (nbytes == 0)
return RESULT_ERR_EOF; return RESULT_ERR_EOF;
for (int i = 0; i < nbytes; i++) return nbytes < 0 ? RESULT_ERR_DEVICE : RESULT_OK;
m_recvBuffer.push(m_buffer[i]);
return nbytes;
}
unsigned char Device::getByte()
{
unsigned char byte;
if (m_recvBuffer.empty() == false) {
byte = m_recvBuffer.front();
m_recvBuffer.pop();
return byte;
}
return 0;
} }
@@ -266,7 +237,8 @@ Port::Port(const string deviceName, const bool noDeviceCheck,
const bool dumpRaw, const char* dumpRawFile, const long dumpRawMaxSize) const bool dumpRaw, const char* dumpRawFile, const long dumpRawMaxSize)
: m_deviceName(deviceName), m_noDeviceCheck(noDeviceCheck), : m_deviceName(deviceName), m_noDeviceCheck(noDeviceCheck),
m_logRaw(logRaw), m_logRawFunc(logRawFunc), m_logRaw(logRaw), m_logRawFunc(logRawFunc),
m_dumpRawFile(dumpRawFile), m_dumpRawMaxSize(dumpRawMaxSize) m_dumpRawFile(dumpRawFile), m_dumpRawMaxSize(dumpRawMaxSize),
m_dumpRawFileSize(0)
{ {
m_device = NULL; m_device = NULL;
@@ -281,32 +253,34 @@ Port::Port(const string deviceName, const bool noDeviceCheck,
setDumpRaw(dumpRaw); // open fstream if necessary setDumpRaw(dumpRaw); // open fstream if necessary
} }
ssize_t Port::send(const unsigned char* buffer, size_t nbytes) result_t Port::send(const unsigned char value)
{ {
ssize_t ret = m_device->sendBytes(buffer, nbytes); result_t ret = m_device->send(value);
if (ret>0 && m_logRaw == true && m_logRawFunc != NULL) if (ret == RESULT_OK && m_logRaw == true && m_logRawFunc != NULL)
(*m_logRawFunc)(buffer[0], false); (*m_logRawFunc)(value, false);
return ret; return ret;
} }
ssize_t Port::recv(const long timeout, size_t maxCount, unsigned char* buffer) result_t Port::recv(const long timeout, unsigned char& value)
{ {
ssize_t ret = m_device->recvBytes(timeout, maxCount, buffer); result_t ret = m_device->recv(timeout, value);
if (buffer && ret > 0) { if (ret == RESULT_OK) {
if (m_logRaw == true && m_logRawFunc != NULL) { if (m_logRaw == true && m_logRawFunc != NULL) {
for (ssize_t pos = 0; pos < ret; pos++) (*m_logRawFunc)(value, true);
(*m_logRawFunc)(buffer[pos], true);
} }
if (m_dumpRaw == true && m_dumpRawStream.is_open() == true) { if (m_dumpRaw == true && m_dumpRawStream.is_open() == true) {
m_dumpRawStream.write((char*)buffer, ret); m_dumpRawStream.write((char*)&value, 1);
m_dumpRawStream.flush(); m_dumpRawFileSize++;
if ((m_dumpRawFileSize%1024) == 0)
m_dumpRawStream.flush();
if (m_dumpRawStream.tellp() >= m_dumpRawMaxSize * 1024) { if (m_dumpRawFileSize >= m_dumpRawMaxSize * 1024) {
string oldfile = m_dumpRawFile + ".old"; string oldfile = m_dumpRawFile + ".old";
if (rename(m_dumpRawFile.c_str(), oldfile.c_str()) == 0) { if (rename(m_dumpRawFile.c_str(), oldfile.c_str()) == 0) {
m_dumpRawStream.close(); m_dumpRawStream.close();
m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app); m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app);
m_dumpRawFileSize = 0;
} }
} }
} }
@@ -315,28 +289,6 @@ ssize_t Port::recv(const long timeout, size_t maxCount, unsigned char* buffer)
return ret; return ret;
} }
unsigned char Port::byte()
{
unsigned char byte = m_device->getByte();
if (m_logRaw == true && m_logRawFunc != NULL)
(*m_logRawFunc)(byte, true);
if (m_dumpRaw == true && m_dumpRawStream.is_open() == true) {
m_dumpRawStream.write((char*)&byte, 1);
if (m_dumpRawStream.tellp() >= m_dumpRawMaxSize * 1024) {
string oldfile = m_dumpRawFile + ".old";
if (rename(m_dumpRawFile.c_str(), oldfile.c_str()) == 0) {
m_dumpRawStream.close();
m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app);
}
}
}
return byte;
}
void Port::setDumpRaw(bool dumpRaw) void Port::setDumpRaw(bool dumpRaw)
{ {
if (dumpRaw == m_dumpRaw) if (dumpRaw == m_dumpRaw)
@@ -346,8 +298,10 @@ void Port::setDumpRaw(bool dumpRaw)
if (dumpRaw == false) if (dumpRaw == false)
m_dumpRawStream.close(); m_dumpRawStream.close();
else else {
m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app); m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app);
m_dumpRawFileSize = 0;
}
} }
void Port::setDumpRawFile(const string& dumpFile) { void Port::setDumpRawFile(const string& dumpFile) {
@@ -357,8 +311,10 @@ void Port::setDumpRawFile(const string& dumpFile) {
m_dumpRawStream.close(); m_dumpRawStream.close();
m_dumpRawFile = dumpFile; m_dumpRawFile = dumpFile;
if (m_dumpRaw == true) if (m_dumpRaw == true) {
m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app); m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app);
m_dumpRawFileSize = 0;
}
} }
void Port::setType(const DeviceType type) void Port::setType(const DeviceType type)
+20 -57
View File
@@ -38,12 +38,6 @@ enum DeviceType {
dt_network /*!< network device */ dt_network /*!< network device */
}; };
/** @brief max bytes write to bus. */
#define MAX_WRITE_SIZE 1
/** @brief max size of receive buffer. */
#define MAX_READ_SIZE 100
/** /**
* @brief base class for input devices. * @brief base class for input devices.
@@ -82,33 +76,19 @@ public:
bool isOpen(); bool isOpen();
/** /**
* @brief sendBytes write bytes to opened file descriptor. * @brief Write a single byte to opened file descriptor.
* @param buffer data to send. * @param value the value to send.
* @param nbytes number of bytes to send. * @return the result_t code.
* @return number of written bytes or -1 if an error has occured.
*/ */
ssize_t sendBytes(const unsigned char* buffer, size_t nbytes); result_t send(const unsigned char value);
/** /**
* @brief recvBytes read bytes from opened file descriptor. * @brief Read a single byte from opened file descriptor.
* @param timeout time for new input data [usec]. * @param timeout max time out for new input data [usec], or 0 for infinite.
* @param maxCount max size of receive buffer. * @param value the reference in which the value is stored.
* @param buffer optional direct buffer to write to (instead of queuing the data). * @return the result_t code.
* @return number of read bytes or -1 if an error has occured.
*/ */
ssize_t recvBytes(const long timeout, size_t maxCount, unsigned char* buffer=NULL); result_t recv(const long timeout, unsigned char& value);
/**
* @brief fetch first byte from receive buffer.
* @return first byte (raw)
*/
unsigned char getByte();
/**
* @brief get current size (bytes) of the receive buffer.
* @return number of bytes in queued.
*/
ssize_t sizeRecvBuffer() const { return m_recvBuffer.size(); }
protected: protected:
/** file descriptor from input device */ /** file descriptor from input device */
@@ -120,12 +100,6 @@ protected:
/** true if device check is disabled */ /** true if device check is disabled */
bool m_noDeviceCheck; bool m_noDeviceCheck;
/** queue for received bytes */
queue<unsigned char> m_recvBuffer;
/** receive buffer */
unsigned char m_buffer[MAX_READ_SIZE];
private: private:
/** /**
* @brief system check if opened file descriptor is valid * @brief system check if opened file descriptor is valid
@@ -224,33 +198,19 @@ public:
bool isOpen() { return m_device->isOpen(); } bool isOpen() { return m_device->isOpen(); }
/** /**
* @brief send write bytes into opened file descriptor. * @brief Write a single byte to opened file descriptor.
* @param buffer data to send. * @param value the value to send.
* @param nbytes number of bytes to send. * @return the result_t code.
* @return number of written bytes or -1 if an error has occured.
*/ */
ssize_t send(const unsigned char* buffer, size_t nbytes = MAX_WRITE_SIZE); result_t send(const unsigned char value);
/** /**
* @brief recv read bytes from opened file descriptor. * @brief Read a single byte from opened file descriptor.
* @param timeout max time out for new input data [usec], or 0 for infinite. * @param timeout max time out for new input data [usec], or 0 for infinite.
* @param maxCount max size of receive buffer. * @param value the reference in which the value is stored.
* @param buffer optional direct buffer to write to (instead of queuing the data). * @return the result_t code.
* @return number of read bytes (never 0) or a negative result_t code.
*/ */
ssize_t recv(const long timeout, size_t maxCount = MAX_READ_SIZE, unsigned char* buffer = NULL); result_t recv(const long timeout, unsigned char& value);
/**
* @brief fetch first byte from receive buffer.
* @return first byte (raw)
*/
unsigned char byte();
/**
* @brief get current size (bytes) of the receive buffer.
* @return number of bytes in queued.
*/
ssize_t size() const { return m_device->sizeRecvBuffer(); }
/** /**
* @brief Get whether logging of raw data is enabled. * @brief Get whether logging of raw data is enabled.
@@ -322,6 +282,9 @@ private:
/** the @a ofstream for dumping raw data to. */ /** the @a ofstream for dumping raw data to. */
ofstream m_dumpRawStream; ofstream m_dumpRawStream;
/** the number of bytes already written to the @a m_dumpFile. */
long m_dumpRawFileSize;
/** /**
* @brief internal setter for device type. * @brief internal setter for device type.
* @param type of device * @param type of device
+3 -6
View File
@@ -36,17 +36,14 @@ int main ()
int count = 0; int count = 0;
while (1) { while (1) {
ssize_t bytes_read; result_t result;
unsigned char byte = 0; unsigned char byte = 0;
bytes_read = port.recv(0); result = port.recv(0, byte);
for (int i = 0; i < bytes_read; i++) if (result == RESULT_OK)
byte = port.byte();
cout << hex << setw(2) << setfill('0') cout << hex << setw(2) << setfill('0')
<< static_cast<unsigned>(byte) << endl; << static_cast<unsigned>(byte) << endl;
bytes_read = 0;
count++; count++;
} }
+1 -1
View File
@@ -82,7 +82,7 @@ int main(int argc, char* argv[])
cout << hex << setw(2) << setfill('0') cout << hex << setw(2) << setfill('0')
<< static_cast<unsigned>(byte) << endl; << static_cast<unsigned>(byte) << endl;
port.send(&byte, 1); port.send(byte);
usleep(A.getOptVal<long>("time")); usleep(A.getOptVal<long>("time"));
} }