From 009729cbdfe020268eaf6b1b144e95694122971e Mon Sep 17 00:00:00 2001 From: John Date: Fri, 10 Nov 2023 07:22:00 +0100 Subject: [PATCH] extract method --- src/lib/ebus/device.cpp | 191 +++++++++++++++++++++------------------- src/lib/ebus/device.h | 5 ++ 2 files changed, 105 insertions(+), 91 deletions(-) diff --git a/src/lib/ebus/device.cpp b/src/lib/ebus/device.cpp index 4be30ef6..4b565d2d 100755 --- a/src/lib/ebus/device.cpp +++ b/src/lib/ebus/device.cpp @@ -730,97 +730,7 @@ bool FileDevice::handleEnhancedBufferedData(symbol_t* value, ArbitrationState* a } else if (m_infoPos < m_infoLen && m_infoPos < sizeof(m_infoBuf)) { m_infoBuf[m_infoPos++] = data; if (m_infoPos >= m_infoLen) { - unsigned int val; - ostringstream stream; - switch ((m_infoLen << 8) | m_infoId) { - case 0x0200: - case 0x0500: // with firmware version and jumper info - case 0x0800: // with firmware version, jumper info, and bootloader version - stream << hex << static_cast(m_infoBuf[1]) // features mask - << "." << static_cast(m_infoBuf[0]); // version minor - if (m_infoLen >= 5) { - stream << "[" << setfill('0') << setw(2) << hex << static_cast(m_infoBuf[2]) - << setw(2) << static_cast(m_infoBuf[3]) << "]"; - } - if (m_infoLen >= 8) { - stream << "." << dec << static_cast(m_infoBuf[5]); - stream << "[" << setfill('0') << setw(2) << hex << static_cast(m_infoBuf[6]) - << setw(2) << static_cast(m_infoBuf[7]) << "]"; - } - m_enhInfoVersion = stream.str(); - stream.str(" "); - stream << "firmware " << m_enhInfoVersion; - if (m_infoLen >= 5) { - stream << ", jumpers 0x" << setw(2) << static_cast(m_infoBuf[4]); - } - stream << setfill(' '); // reset - break; - case 0x0901: - case 0x0802: - case 0x0302: - stream << (m_infoId == 1 ? "ID" : "config"); - stream << hex << setfill('0'); - for (uint8_t pos = 0; pos < m_infoPos; pos++) { - stream << " " << setw(2) << static_cast(m_infoBuf[pos]); - } - if (m_infoId == 2 && (m_infoBuf[2]&0x3f) != 0x3f) { - // non-default arbitration delay - val = (m_infoBuf[2]&0x3f)*10; // steps of 10us - stream << ", arbitration delay " << dec << static_cast(val) << " us"; - } - break; - case 0x0203: - val = (static_cast(m_infoBuf[0]) << 8) | static_cast(m_infoBuf[1]); - stream << "temperature " << static_cast(val) << " °C"; - m_enhInfoTemperature = stream.str(); - break; - case 0x0204: - stream << "supply voltage "; - if (m_infoBuf[0] | m_infoBuf[1]) { - val = (static_cast(m_infoBuf[0]) << 8) | static_cast(m_infoBuf[1]); - stream << static_cast(val) << " mV"; - } else { - stream << "unknown"; - } - m_enhInfoSupplyVoltage = stream.str(); - break; - case 0x0205: - stream << "bus voltage "; - if (m_infoBuf[0] | m_infoBuf[1]) { - stream << fixed << setprecision(1) - << static_cast(m_infoBuf[1] / 10.0) << " V - " - << static_cast(m_infoBuf[0] / 10.0) << " V"; - } else { - stream << "unknown"; - } - m_enhInfoBusVoltage = stream.str(); - break; - case 0x0206: - stream << "reset cause "; - if (m_infoBuf[0]) { - stream << static_cast(m_infoBuf[0]) << "="; - switch (m_infoBuf[0]) { - case 1: stream << "power-on"; break; - case 2: stream << "brown-out"; break; - case 3: stream << "watchdog"; break; - case 4: stream << "clear"; break; - case 5: stream << "reset"; break; - case 6: stream << "stack"; break; - case 7: stream << "memory"; break; - default: stream << "other"; break; - } - stream << ", restart count " << static_cast(m_infoBuf[1]); - } else { - stream << "unknown"; - } - break; - default: - stream << "unknown 0x" << hex << setfill('0') << setw(2) - << static_cast(m_infoId) << ", len " << dec << setw(0) - << static_cast(m_infoPos); - break; - } - m_listener->notifyDeviceStatus(false, ("extra info: "+stream.str()).c_str()); + notifyInfoRetrieved(); m_infoLen = 0; m_infoId = 0xff; } @@ -864,6 +774,105 @@ bool FileDevice::handleEnhancedBufferedData(symbol_t* value, ArbitrationState* a return false; } +void FileDevice::notifyInfoRetrieved() { + symbol_t* data = m_infoBuf; + size_t len = m_infoLen; + symbol_t id = m_infoId; + unsigned int val; + ostringstream stream; + switch ((len << 8) | id) { + case 0x0200: + case 0x0500: // with firmware version and jumper info + case 0x0800: // with firmware version, jumper info, and bootloader version + stream << hex << static_cast(data[1]) // features mask + << "." << static_cast(data[0]); // version minor + if (len >= 5) { + stream << "[" << setfill('0') << setw(2) << hex << static_cast(data[2]) + << setw(2) << static_cast(data[3]) << "]"; + } + if (len >= 8) { + stream << "." << dec << static_cast(data[5]); + stream << "[" << setfill('0') << setw(2) << hex << static_cast(data[6]) + << setw(2) << static_cast(data[7]) << "]"; + } + m_enhInfoVersion = stream.str(); + stream.str(" "); + stream << "firmware " << m_enhInfoVersion; + if (len >= 5) { + stream << ", jumpers 0x" << setw(2) << static_cast(data[4]); + } + stream << setfill(' '); // reset + break; + case 0x0901: + case 0x0802: + case 0x0302: + stream << (id == 1 ? "ID" : "config"); + stream << hex << setfill('0'); + for (uint8_t pos = 0; pos < len; pos++) { + stream << " " << setw(2) << static_cast(data[pos]); + } + if (id == 2 && (data[2]&0x3f) != 0x3f) { + // non-default arbitration delay + val = (data[2]&0x3f)*10; // steps of 10us + stream << ", arbitration delay " << dec << static_cast(val) << " us"; + } + break; + case 0x0203: + val = (static_cast(data[0]) << 8) | static_cast(data[1]); + stream << "temperature " << static_cast(val) << " °C"; + m_enhInfoTemperature = stream.str(); + break; + case 0x0204: + stream << "supply voltage "; + if (data[0] | data[1]) { + val = (static_cast(data[0]) << 8) | static_cast(data[1]); + stream << static_cast(val) << " mV"; + } else { + stream << "unknown"; + } + m_enhInfoSupplyVoltage = stream.str(); + break; + case 0x0205: + stream << "bus voltage "; + if (data[0] | data[1]) { + stream << fixed << setprecision(1) + << static_cast(data[1] / 10.0) << " V - " + << static_cast(data[0] / 10.0) << " V"; + } else { + stream << "unknown"; + } + m_enhInfoBusVoltage = stream.str(); + break; + case 0x0206: + stream << "reset cause "; + if (data[0]) { + stream << static_cast(data[0]) << "="; + switch (data[0]) { + case 1: stream << "power-on"; break; + case 2: stream << "brown-out"; break; + case 3: stream << "watchdog"; break; + case 4: stream << "clear"; break; + case 5: stream << "reset"; break; + case 6: stream << "stack"; break; + case 7: stream << "memory"; break; + default: stream << "other"; break; + } + stream << ", restart count " << static_cast(data[1]); + } else { + stream << "unknown"; + } + break; + default: + stream << "unknown 0x" << hex << setfill('0') << setw(2) + << static_cast(id) << ", len " << dec << setw(0) + << static_cast(len); + break; + } + if (m_listener != nullptr) { + m_listener->notifyDeviceStatus(false, ("extra info: "+stream.str()).c_str()); + } +} + result_t SerialDevice::open() { result_t result = FileDevice::open(); diff --git a/src/lib/ebus/device.h b/src/lib/ebus/device.h index 00d913eb..27ba4c66 100755 --- a/src/lib/ebus/device.h +++ b/src/lib/ebus/device.h @@ -378,6 +378,11 @@ class FileDevice : public Device { */ bool handleEnhancedBufferedData(symbol_t* value, ArbitrationState* arbitrationState); + /** + * Called when reception of an info ID was completed. + */ + void notifyInfoRetrieved(); + /** the arbitration master address to send when in arbitration, or @a SYN. */ symbol_t m_arbitrationMaster;