better defines for debugging

This commit is contained in:
john30
2023-09-17 15:50:11 +02:00
parent f633e07840
commit 689c38610b
+29 -53
View File
@@ -75,6 +75,19 @@ namespace ebusd {
#define ENH_BYTE2 ((uint8_t)0x80) #define ENH_BYTE2 ((uint8_t)0x80)
#define makeEnhancedSequence(cmd, data) {(uint8_t)(ENH_BYTE1 | ((cmd)<<2) | (((data)&0xc0)>>6)), (uint8_t)(ENH_BYTE2 | ((data)&0x3f))} #define makeEnhancedSequence(cmd, data) {(uint8_t)(ENH_BYTE1 | ((cmd)<<2) | (((data)&0xc0)>>6)), (uint8_t)(ENH_BYTE2 | ((data)&0x3f))}
#ifdef DEBUG_RAW_TRAFFIC
#define DEBUG_RAW_TRAFFIC_HEAD(format, args...) fprintf(stdout, "%lld raw: " format, clockGetMillis(), args)
#define DEBUG_RAW_TRAFFIC_ITEM(args...) fprintf(stdout, args)
#define DEBUG_RAW_TRAFFIC_FINAL() fprintf(stdout, "\n"); fflush(stdout)
#undef DEBUG_RAW_TRAFFIC
#define DEBUG_RAW_TRAFFIC(format, args...) fprintf(stdout, "%lld raw: " format "\n", clockGetMillis(), args); fflush(stdout)
#else
#define DEBUG_RAW_TRAFFIC_HEAD(format, args...)
#undef DEBUG_RAW_TRAFFIC_ITEM
#define DEBUG_RAW_TRAFFIC_FINAL()
#define DEBUG_RAW_TRAFFIC(format, args...)
#endif
Device::Device(const char* name, bool readOnly, bool initialSend) Device::Device(const char* name, bool readOnly, bool initialSend)
: m_name(name), m_readOnly(readOnly), m_initialSend(initialSend), m_listener(nullptr) { : m_name(name), m_readOnly(readOnly), m_initialSend(initialSend), m_listener(nullptr) {
} }
@@ -199,10 +212,7 @@ result_t FileDevice::open() {
result_t FileDevice::afterOpen() { result_t FileDevice::afterOpen() {
if (m_enhancedProto) { if (m_enhancedProto) {
symbol_t buf[2] = makeEnhancedSequence(ENH_REQ_INIT, 0x01); // extra feature: info symbol_t buf[2] = makeEnhancedSequence(ENH_REQ_INIT, 0x01); // extra feature: info
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("enhanced > %2.2x %2.2x", buf[0], buf[1]);
fprintf(stdout, "raw enhanced > %2.2x %2.2x\n", buf[0], buf[1]);
fflush(stdout);
#endif
if (::write(m_fd, buf, 2) != 2) { if (::write(m_fd, buf, 2) != 2) {
return RESULT_ERR_SEND; return RESULT_ERR_SEND;
} }
@@ -266,10 +276,7 @@ result_t FileDevice::requestEnhancedInfo(symbol_t infoId) {
result_t FileDevice::sendEnhancedInfoRequest(symbol_t infoId) { result_t FileDevice::sendEnhancedInfoRequest(symbol_t infoId) {
symbol_t buf[2] = makeEnhancedSequence(ENH_REQ_INFO, infoId); symbol_t buf[2] = makeEnhancedSequence(ENH_REQ_INFO, infoId);
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("enhanced > %2.2x %2.2x", buf[0], buf[1]);
fprintf(stdout, "raw enhanced > %2.2x %2.2x\n", buf[0], buf[1]);
fflush(stdout);
#endif
if (::write(m_fd, buf, 2) != 2) { if (::write(m_fd, buf, 2) != 2) {
return RESULT_ERR_DEVICE; return RESULT_ERR_DEVICE;
} }
@@ -415,9 +422,7 @@ result_t FileDevice::recv(unsigned int timeout, symbol_t* value, ArbitrationStat
#endif #endif
#endif #endif
if (ret == -1) { if (ret == -1) {
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("poll error %d", errno);
fprintf(stdout, "poll error %d\n", errno);
#endif
close(); close();
cancelRunningArbitration(arbitrationState); cancelRunningArbitration(arbitrationState);
return RESULT_ERR_DEVICE; return RESULT_ERR_DEVICE;
@@ -508,16 +513,10 @@ result_t FileDevice::startArbitration(symbol_t masterAddress) {
bool FileDevice::write(symbol_t value, bool startArbitration) { bool FileDevice::write(symbol_t value, bool startArbitration) {
if (m_enhancedProto) { if (m_enhancedProto) {
symbol_t buf[2] = makeEnhancedSequence(startArbitration ? ENH_REQ_START : ENH_REQ_SEND, value); symbol_t buf[2] = makeEnhancedSequence(startArbitration ? ENH_REQ_START : ENH_REQ_SEND, value);
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("enhanced > %2.2x %2.2x", buf[0], buf[1]);
fprintf(stdout, "raw enhanced > %2.2x %2.2x\n", buf[0], buf[1]);
fflush(stdout);
#endif
return ::write(m_fd, buf, 2) == 2; return ::write(m_fd, buf, 2) == 2;
} }
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("> %2.2x", value);
fprintf(stdout, "raw > %2.2x\n", value);
fflush(stdout);
#endif
#ifdef SIMULATE_NON_WRITABILITY #ifdef SIMULATE_NON_WRITABILITY
return true; return true;
#else #else
@@ -536,10 +535,7 @@ bool FileDevice::available() {
for (size_t pos = 0; pos < m_bufLen; pos++) { for (size_t pos = 0; pos < m_bufLen; pos++) {
symbol_t ch = m_buffer[(pos+m_bufPos)%m_bufSize]; symbol_t ch = m_buffer[(pos+m_bufPos)%m_bufSize];
if (!(ch&ENH_BYTE_FLAG)) { if (!(ch&ENH_BYTE_FLAG)) {
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("avail direct @%d+%d %2.2x", m_bufPos, pos, ch);
fprintf(stdout, "raw avail direct @%d+%d %2.2x\n", m_bufPos, pos, ch);
fflush(stdout);
#endif
return true; return true;
} }
if ((ch&ENH_BYTE_MASK) == ENH_BYTE1) { if ((ch&ENH_BYTE_MASK) == ENH_BYTE1) {
@@ -550,11 +546,8 @@ bool FileDevice::available() {
// peek into next byte to check if enhanced sequence is ok // peek into next byte to check if enhanced sequence is ok
ch = m_buffer[(pos+m_bufPos+1)%m_bufSize]; ch = m_buffer[(pos+m_bufPos+1)%m_bufSize];
if (!(ch&ENH_BYTE_FLAG) || (ch&ENH_BYTE_MASK) != ENH_BYTE2) { if (!(ch&ENH_BYTE_FLAG) || (ch&ENH_BYTE_MASK) != ENH_BYTE2) {
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("avail enhanced following bad @%d+%d %2.2x %2.2x", m_bufPos, pos,
fprintf(stdout, "raw avail enhanced following bad @%d+%d %2.2x %2.2x\n", m_bufPos, pos,
m_buffer[(pos+m_bufPos)%m_bufSize], ch); m_buffer[(pos+m_bufPos)%m_bufSize], ch);
fflush(stdout);
#endif
if (m_listener != nullptr) { if (m_listener != nullptr) {
m_listener->notifyStatus(true, "unexpected available enhanced following byte 1"); m_listener->notifyStatus(true, "unexpected available enhanced following byte 1");
} }
@@ -566,23 +559,14 @@ bool FileDevice::available() {
} }
if (cmd == ENH_RES_RECEIVED || cmd == ENH_RES_STARTED || cmd == ENH_RES_FAILED) { if (cmd == ENH_RES_RECEIVED || cmd == ENH_RES_STARTED || cmd == ENH_RES_FAILED) {
// found a sequence that yields in available bus byte // found a sequence that yields in available bus byte
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("avail enhanced @%d+%d %2.2x %2.2x", m_bufPos, pos, m_buffer[(pos+m_bufPos)%m_bufSize], ch);
fprintf(stdout, "raw avail enhanced @%d+%d %2.2x %2.2x\n", m_bufPos, pos, m_buffer[(pos+m_bufPos)%m_bufSize], ch);
fflush(stdout);
#endif
return true; return true;
} }
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("avail enhanced skip cmd %d @%d+%d %2.2x", cmd, m_bufPos, pos, ch);
fprintf(stdout, "raw avail enhanced skip cmd %d @%d+%d %2.2x\n", cmd, m_bufPos, pos, ch);
fflush(stdout);
#endif
pos++; // skip enhanced sequence of 2 bytes pos++; // skip enhanced sequence of 2 bytes
continue; continue;
} }
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("avail enhanced bad @%d+%d %2.2x", m_bufPos, pos, ch);
fprintf(stdout, "raw avail enhanced bad @%d+%d %2.2x\n", m_bufPos, pos, ch);
fflush(stdout);
#endif
if (m_listener != nullptr) { if (m_listener != nullptr) {
m_listener->notifyStatus(true, "unexpected available enhanced byte 2"); m_listener->notifyStatus(true, "unexpected available enhanced byte 2");
} }
@@ -610,19 +594,13 @@ bool FileDevice::read(symbol_t* value, bool isAvailable, ArbitrationState* arbit
tail = (m_bufPos+m_bufLen) % m_bufSize; tail = (m_bufPos+m_bufLen) % m_bufSize;
size_t head = m_bufLen-tail; size_t head = m_bufLen-tail;
memmove(m_buffer+head, m_buffer, tail); memmove(m_buffer+head, m_buffer, tail);
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("move tail %d @0 to @%d", tail, head);
fprintf(stdout, "raw move tail %d @0 to @%d\n", tail, head);
fflush(stdout);
#endif
} else { } else {
tail = 0; tail = 0;
} }
// move head to first position // move head to first position
memmove(m_buffer, m_buffer + m_bufPos, m_bufLen - tail); memmove(m_buffer, m_buffer + m_bufPos, m_bufLen - tail);
#ifdef DEBUG_RAW_TRAFFIC DEBUG_RAW_TRAFFIC("move head %d @%d to 0", m_bufLen - tail, m_bufPos);
fprintf(stdout, "raw move head %d @%d to 0\n", m_bufLen - tail, m_bufPos);
fflush(stdout);
#endif
} }
} }
m_bufPos = 0; m_bufPos = 0;
@@ -631,13 +609,12 @@ bool FileDevice::read(symbol_t* value, bool isAvailable, ArbitrationState* arbit
if (size <= 0) { if (size <= 0) {
return false; return false;
} }
#ifdef DEBUG_RAW_TRAFFIC #ifdef DEBUG_RAW_TRAFFIC_ITEM
fprintf(stdout, "raw %ld+%ld <", m_bufLen, size); DEBUG_RAW_TRAFFIC_HEAD("%d+%d <", m_bufLen, size);
for (int pos=0; pos < size; pos++) { for (int pos=0; pos < size; pos++) {
fprintf(stdout, " %2.2x", m_buffer[(m_bufLen+pos)%m_bufSize]); DEBUG_RAW_TRAFFIC_ITEM(" %2.2x", m_buffer[(m_bufLen+pos)%m_bufSize]);
} }
fprintf(stdout, "\n"); DEBUG_RAW_TRAFFIC_FINAL();
fflush(stdout);
#endif #endif
m_bufLen += size; m_bufLen += size;
} }
@@ -953,7 +930,6 @@ result_t SerialDevice::open() {
newSettings.c_cc[VMIN] = 1; newSettings.c_cc[VMIN] = 1;
newSettings.c_cc[VTIME] = 0; newSettings.c_cc[VTIME] = 0;
// int flag = TIOCM_RTS|TIOCM_DTR;
// empty device buffer // empty device buffer
tcflush(m_fd, TCIFLUSH); tcflush(m_fd, TCIFLUSH);