Mercurial > public > ostc_companion
changeset 12:ac837fe1d590 default tip
Switch implementation for reqex class and added RFCOMM as label for Bluetooth based connection by Linux
| author | Ideenmodellierer |
|---|---|
| date | Mon, 12 Jan 2026 13:58:41 +0000 |
| parents | 6fba58c4964b |
| children | |
| files | HardwareOperations.cpp OSTC4Operations.cpp |
| diffstat | 2 files changed, 353 insertions(+), 335 deletions(-) [+] |
line wrap: on
line diff
--- a/HardwareOperations.cpp Mon Jan 12 13:57:24 2026 +0000 +++ b/HardwareOperations.cpp Mon Jan 12 13:58:41 2026 +0000 @@ -36,38 +36,36 @@ ////////////////////////////////////////////////////////////////////////////// #include "HardwareOperations.h" +#include <QSerialPortInfo> #include "MainWindow.h" #include "Utils/Log.h" -#include <QSerialPortInfo> + +#include <QRegularExpression> #ifdef WIN32 -# include <winreg.h> +#include <winreg.h> #endif #include <QDir> ////////////////////////////////////////////////////////////////////////////// -unsigned char HardwareOperations::retryCommand(Serial& serial, - unsigned char cmd, - int retries) +unsigned char HardwareOperations::retryCommand(Serial &serial, unsigned char cmd, int retries) { - for(int retry=0; retry<retries; ++retry) - { - serial.writeByte( cmd ); // Send command - serial.sleep(25); // Allow 25msec lag. + for (int retry = 0; retry < retries; ++retry) { + serial.writeByte(cmd); // Send command + serial.sleep(25); // Allow 25msec lag. try { unsigned char echo = serial.readByte(); - if( echo == cmd || echo == 'M' || echo == 'L' ) - return echo; // Got it, or unknown command... - } - catch(const ReadTimeout&) { + if (echo == cmd || echo == 'M' || echo == 'L') + return echo; // Got it, or unknown command... + } catch (const ReadTimeout &) { continue; } - serial.sleep(100); // Cleanup any pending stuff, - serial.purge(); // and retry... + serial.sleep(100); // Cleanup any pending stuff, + serial.purge(); // and retry... } return 0xFF; @@ -75,87 +73,100 @@ ////////////////////////////////////////////////////////////////////////////// -HardwareOperations::HardwareDescriptor -HardwareOperations::hardwareDescriptor() +HardwareOperations::HardwareDescriptor HardwareOperations::hardwareDescriptor() { - unsigned char echo = 0; - unsigned int hardFeatures = 0xFF; // timeout response... - unsigned int softFeatures = 0; - unsigned int model = 0; - unsigned char ok = 0; + unsigned char echo = 0; + unsigned int hardFeatures = 0xFF; // timeout response... + unsigned int softFeatures = 0; + unsigned int model = 0; + unsigned char ok = 0; try { //---- First: try the new extended hardware query -------------------- echo = retryCommand(_serial, 0x60, 1); // BACKQUOTE char - if( echo == 0x60 ) { + if (echo == 0x60) { uchar extendedDescriptor[6]; _serial.readBlock(extendedDescriptor, sizeof extendedDescriptor); - hardFeatures = extendedDescriptor[0] * 256 + extendedDescriptor[1]; softFeatures = extendedDescriptor[2] * 256 + extendedDescriptor[3]; - model = extendedDescriptor[4]; - ok = extendedDescriptor[5]; - } - else - { + model = extendedDescriptor[4]; + ok = extendedDescriptor[5]; + } else { // Did we have a timeout ? // In that case, some hwOS versions fails and need a reset of // the connection mode... - if( echo == 0xFF ) - { - echo = retryCommand(_serial, 0xBB); // Try to reconnect - if( echo == 0xBB ) - echo = _serial.readByte(); // Eat 4d prompt + if (echo == 0xFF) { + echo = retryCommand(_serial, 0xBB); // Try to reconnect + if (echo == 0xBB) + echo = _serial.readByte(); // Eat 4d prompt } // Then try the OLD hardware descriptor command... - echo = retryCommand(_serial, 'j'); // 0x6A - if( echo == 'j' ) { + echo = retryCommand(_serial, 'j'); // 0x6A + if (echo == 'j') { hardFeatures = _serial.readByte(); - ok = _serial.readByte(); + ok = _serial.readByte(); } } + } catch (const ReadTimeout &) { } - catch(const ReadTimeout&) {} - if( (echo != 0x60 && echo != 'j') - || (ok != 'M' && ok != 'L') - ) { + if ((echo != 0x60 && echo != 'j') || (ok != 'M' && ok != 'L')) { LOG_TRACE("Old OSTC not responding..."); return HW_UNKNOWN_OSTC; } - switch(hardFeatures) { - case HW_Frog : LOG_TRACE("Frog found"); break; - case HW_OSTCSport_a : LOG_TRACE("OSTC Sport found"); break; - case HW_OSTC2c : LOG_TRACE("OSTC 2c found"); break; + switch (hardFeatures) { + case HW_Frog: + LOG_TRACE("Frog found"); + break; + case HW_OSTCSport_a: + LOG_TRACE("OSTC Sport found"); + break; + case HW_OSTC2c: + LOG_TRACE("OSTC 2c found"); + break; case HW_OSTC2_a: - case HW_OSTC2_c : LOG_TRACE("OSTC 2 found"); break; + case HW_OSTC2_c: + LOG_TRACE("OSTC 2 found"); + break; case HW_OSTCcR_a: - case HW_OSTCcR_b : LOG_TRACE("OSTC cR found"); break; - case HW_OSTC3 : LOG_TRACE("OSTC 3 found"); break; - case HW_OSTC3p_a : LOG_TRACE("OSTC 3+ found"); break; - case HW_OSTC4 : LOG_TRACE("OSTC 4/5 found"); break; + case HW_OSTCcR_b: + LOG_TRACE("OSTC cR found"); + break; + case HW_OSTC3: + LOG_TRACE("OSTC 3 found"); + break; + case HW_OSTC3p_a: + LOG_TRACE("OSTC 3+ found"); + break; + case HW_OSTC4: + LOG_TRACE("OSTC 4/5 found"); + break; - case HW_OSTCSport_b : LOG_TRACE("OSTC Sport, OSTC 2 or OSTC 3 found."); break; + case HW_OSTCSport_b: + LOG_TRACE("OSTC Sport, OSTC 2 or OSTC 3 found."); + break; - case 0xFF: case 0x4C: case 0x4D: + case 0xFF: + case 0x4C: + case 0x4D: LOG_TRACE("old OSTC not responding..."); return HW_UNKNOWN_OSTC; default: - // LOG_TRACE("Unknown hardware feature =" << QString().sprintf("0x%04x", hardFeatures)); + // LOG_TRACE("Unknown hardware feature =" << QString().sprintf("0x%04x", hardFeatures)); LOG_TRACE("Unknown hardware feature =" << QString::asprintf("0x%04x", hardFeatures)); break; } - if( echo == 0x60 ) { - LOG_TRACE(" software feature = " << QString::asprintf("0x%04x", softFeatures)); - LOG_TRACE(" model = " << QString::asprintf("0x%02x", model)); + if (echo == 0x60) { + LOG_TRACE(" software feature = " << QString::asprintf("0x%04x", softFeatures)); + LOG_TRACE(" model = " << QString::asprintf("0x%02x", model)); } - return (HardwareDescriptor)hardFeatures; + return (HardwareDescriptor) hardFeatures; } ////////////////////////////////////////////////////////////////////////////// @@ -169,43 +180,44 @@ #if defined(Q_OS_MAC) || defined(Q_OS_LINUX) // TODO: Linux USB search... - QRegExp pTemplate = portTemplate(); + QRegularExpression pTemplate = portTemplate(); QDir dev("/dev"); - QStringList all = dev.entryList(QStringList() << "tty.*", - QDir::NoDotAndDotDot|QDir::System|QDir::Writable, + QStringList all = dev.entryList(QStringList() << "rfcomm*" << "tty.*", + QDir::NoDotAndDotDot | QDir::Files | QDir::System | QDir::Writable, QDir::Name); - for(int i=0; i<(int)all.count(); ++i) { - if( pTemplate.indexIn(all[i]) >= 0 ) { + for (int i = 0; i < all.count(); ++i) { + QRegularExpressionMatch match = pTemplate.match(all[i]); + + if (match.hasMatch()) { LOG_TRACE("Port " << all[i]); list += all[i]; + } else { + LOG_DEBUG("... " << all[i]); } - else - LOG_DEBUG("... " << all[i]); } #else /* Check the descriptors of the available COMs for Bluetooth tag */ for (const QSerialPortInfo &serialPortInfo : serialPortInfos) { - PortDesc = serialPortInfo.description(); - if( PortDesc.contains("Bluetooth")) - list += serialPortInfo.portName(); + PortDesc = serialPortInfo.description(); + if (PortDesc.contains("Bluetooth")) + list += serialPortInfo.portName(); } - if( list.isEmpty() ) /* no port identified => fallback to old detection function */ + if (list.isEmpty()) /* no port identified => fallback to old detection function */ { - for(int i=1; i<300; ++i) - { + for (int i = 1; i < 300; ++i) { QString port = QString("COM%1").arg(i); // First: try to read default configuration... COMMCONFIG config = {0}; - config.dwSize = sizeof config; + config.dwSize = sizeof config; config.wVersion = 1; DWORD len = sizeof config; QByteArray fixed = "\\\\.\\" + port.toLocal8Bit(); - if( GetDefaultCommConfigA(fixed.constData(), &config, &len) ) { - if( config.dwProviderSubType == PST_RS232 ) + if (GetDefaultCommConfigA(fixed.constData(), &config, &len)) { + if (config.dwProviderSubType == PST_RS232) list += port; } } @@ -216,33 +228,36 @@ { HKEY key; const char registryPath[] = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; - if( RegOpenKeyExA(HKEY_LOCAL_MACHINE, // PWD - registryPath, // "SOFTWARE\Intel\PSIB" - 0, // Options - KEY_READ, // Desired SAM: See 32bits view. - &key) == ERROR_SUCCESS - ) { - for(DWORD i = 0; ++i;) { + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, // PWD + registryPath, // "SOFTWARE\Intel\PSIB" + 0, // Options + KEY_READ, // Desired SAM: See 32bits view. + &key) + == ERROR_SUCCESS) { + for (DWORD i = 0; ++i;) { char nameBuffer[128] = {0}; DWORD nameLen = sizeof nameBuffer; unsigned char dataBuffer[128] = {0}; DWORD dataLen = sizeof dataBuffer; - long rc = RegEnumValueA(key, i, - nameBuffer, &nameLen, - nullptr, - nullptr, - dataBuffer, &dataLen); - if( rc != ERROR_SUCCESS ) + long rc = RegEnumValueA(key, + i, + nameBuffer, + &nameLen, + nullptr, + nullptr, + dataBuffer, + &dataLen); + if (rc != ERROR_SUCCESS) break; QString name = QString(nameBuffer); - QString port = QString((char*)dataBuffer); + QString port = QString((char *) dataBuffer); LOG_TRACE("Resource " << i << ": " << name << ", " << port); - if( name.contains("\\BtModem") || name.contains("\\BthModem") || name.contains("\\BtPort") ) { + if (name.contains("\\BtModem") || name.contains("\\BthModem") + || name.contains("\\BtPort")) { list += port + " (Bluetooth)"; LOG_TRACE("Port " << name); - } - else + } else LOG_DEBUG("... " << name); } RegCloseKey(key); @@ -258,48 +273,50 @@ QStringList HardwareOperations::listUSBPorts() const { - assert( !(supported() & BLUETOOTH) ); + assert(!(supported() & BLUETOOTH)); QStringList list; #if defined(Q_OS_MAC) || defined(Q_OS_LINUX) // TODO: Linux USB search... QDir dev("/dev"); - QRegExp pTemplate = portTemplate(); + QRegularExpression pTemplate = portTemplate(); // Hier bleibt alles gleich QStringList all = dev.entryList(QStringList() << "tty.*", - QDir::System|QDir::Writable|QDir::NoDotAndDotDot, + QDir::System | QDir::Writable | QDir::NoDotAndDotDot, QDir::Name); - for(int i=0; i<(int)all.count(); ++i) { - if( pTemplate.indexIn(all[i]) >= 0 ) { + + for (int i = 0; i < all.count(); ++i) { + // Verwende QRegularExpression::match anstelle von indexIn + QRegularExpressionMatch match = pTemplate.match(all[i]); + + if (match.hasMatch()) { // Überprüfe, ob eine Übereinstimmung gefunden wurde LOG_TRACE("Port " << all[i]); list += all[i]; + } else { + LOG_TRACE("... " << all[i]); } - else - LOG_TRACE("... " << all[i]); } #else //---- First chance: Try the normal port list: - for(int i=1; i<300; ++i) - { + for (int i = 1; i < 300; ++i) { QString port = QString("COM%1").arg(i); // First: try to read default configuration... COMMCONFIG config; memset(&config, 0, sizeof config); - config.dwSize = sizeof config; + config.dwSize = sizeof config; config.wVersion = 1; DWORD len = sizeof config; QByteArray fixed = "\\\\.\\" + port.toLocal8Bit(); - if( GetDefaultCommConfigA(fixed.constData(), &config, &len) ) { - LOG_TRACE("Port " << port << " subtype=" << int(config.dwProviderSubType) ); - if( config.dwProviderSubType == PST_RS232 ) + if (GetDefaultCommConfigA(fixed.constData(), &config, &len)) { + LOG_TRACE("Port " << port << " subtype=" << int(config.dwProviderSubType)); + if (config.dwProviderSubType == PST_RS232) list += port; - } else if( len != sizeof config ) + } else if (len != sizeof config) LOG_THROW("Required " << len << " bytes."); - else if( HRESULT rc = GetLastError() ) - if( rc != 87 ) - LOG_TRACE("Port " << port << " error=" << rc ); - + else if (HRESULT rc = GetLastError()) + if (rc != 87) + LOG_TRACE("Port " << port << " error=" << rc); } //---- Second chance // overide usual MS bug, by looking into the registry for more @@ -307,34 +324,37 @@ { HKEY key; const char registryPath[] = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; - if( RegOpenKeyExA(HKEY_LOCAL_MACHINE, // PWD - registryPath, // path - 0, // Options - KEY_READ, // Desired SAM: See 32bits view. - &key) == ERROR_SUCCESS - ) { - for(DWORD i = 0;; ++i) { + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, // PWD + registryPath, // path + 0, // Options + KEY_READ, // Desired SAM: See 32bits view. + &key) + == ERROR_SUCCESS) { + for (DWORD i = 0;; ++i) { char nameBuffer[128] = {0}; DWORD nameLen = sizeof nameBuffer; unsigned char dataBuffer[128] = {0}; DWORD dataLen = sizeof dataBuffer; - long rc = RegEnumValueA(key, i, - nameBuffer, &nameLen, - nullptr, - nullptr, - dataBuffer, &dataLen); + long rc = RegEnumValueA(key, + i, + nameBuffer, + &nameLen, + nullptr, + nullptr, + dataBuffer, + &dataLen); - if( rc == ERROR_NO_MORE_ITEMS ) + if (rc == ERROR_NO_MORE_ITEMS) break; - if( rc != ERROR_SUCCESS ) - LOG_THROW( "Enumeration error" ); + if (rc != ERROR_SUCCESS) + LOG_THROW("Enumeration error"); QString name = QString(nameBuffer); - QString port = QString((char*)dataBuffer); + QString port = QString((char *) dataBuffer); LOG_TRACE("Resource " << i << ": " << name << ", " << port); - if( name.contains("\\VCP") ) + if (name.contains("\\VCP")) list += port + " (USB)"; } RegCloseKey(key);
--- a/OSTC4Operations.cpp Mon Jan 12 13:57:24 2026 +0000 +++ b/OSTC4Operations.cpp Mon Jan 12 13:58:41 2026 +0000 @@ -34,20 +34,19 @@ // THE POSSIBILITY OF SUCH DAMAGE. // ////////////////////////////////////////////////////////////////////////////// -#include <QSettings> +#include "OSTC4Operations.h" #include <QRegularExpression> -#include "OSTC4Operations.h" +#include <QSettings> - +#include "Utils/Exception.h" #include "Utils/Log.h" -#include "Utils/Exception.h" #include "Utils/ProgressEvent.h" -extern QSettings* settings; +extern QSettings *settings; -#define FIRMWARE_BLOCK 0x40 // 64 bytes -#define FIRMWARE_BLOCK_DELAY 15 // 15 msec. -#define FIRMWARE_BLOCK_FAST 0x300 // 4096 bytes +#define FIRMWARE_BLOCK 0x40 // 64 bytes +#define FIRMWARE_BLOCK_DELAY 15 // 15 msec. +#define FIRMWARE_BLOCK_FAST 0x300 // 4096 bytes ////////////////////////////////////////////////////////////////////////////// @@ -79,7 +78,7 @@ features |= SIGNAL_CHECK; features |= ICON; - return features; + return features; } QString OSTC4Operations::firmwareTemplate() const @@ -105,17 +104,15 @@ } #endif - QRegularExpression OSTC4Operations::portTemplate() const { #if defined(Q_OS_MAC) - return QRegularExpression("tty[.]OSTC4-.*", - QRegularExpression::CaseInsensitiveOption); + return QRegularExpression("tty[.]OSTC4-.*", QRegularExpression::CaseInsensitiveOption); #elif defined(Q_OS_LINUX) // Debian, Ubuntu, RedHat, CentOS, SUSE - return QRegularExpression("ttyUSB.*"); // default: case-sensitive + return QRegularExpression("^(rfcomm.*|ttyUSB.*)$"); #elif defined(Q_OS_WIN) - return QRegularExpression("COM.*"); // default: case-sensitive + return QRegularExpression("COM.*"); // default: case-sensitive #endif } ////////////////////////////////////////////////////////////////////////////// @@ -126,7 +123,7 @@ LOG_TRACE("Getting model..."); HardwareDescriptor hw = hardwareDescriptor(); - if( hw != HW_OSTC4 ) + if (hw != HW_OSTC4) LOG_THROW("Not an OSTC4/5."); LOG_TRACE("Getting identity..."); @@ -136,61 +133,73 @@ QString mainFW; { unsigned char echo = retryCommand(_serial, 'k'); // 0x69 OSTC4 FW Details. - if( echo != 'k' ) + if (echo != 'k') LOG_THROW("Bad firmware details."); - unsigned char reply[4+1]; - _serial.writeByte(0xFF); // Main firmware + unsigned char reply[4 + 1]; + _serial.writeByte(0xFF); // Main firmware _serial.readBlock(reply, sizeof reply); - if( reply[4] != 0x4D ) + if (reply[4] != 0x4D) LOG_THROW("Bad main firmware reply."); - mainFW = QString::asprintf("%d.%02d.%02d%s", reply[0], reply[1], reply[2], reply[3] ? "beta" : ""); + mainFW = QString::asprintf("%d.%02d.%02d%s", + reply[0], + reply[1], + reply[2], + reply[3] ? "beta" : ""); } //---- RTE firmware ------------------------------------------------------ QString rteFW; { unsigned char echo = retryCommand(_serial, 'k'); // 0x69 OSTC4 FW Details. - if( echo != 'k' ) + if (echo != 'k') LOG_THROW("Bad firmware details."); - unsigned char reply[4+1]; - _serial.writeByte(0xFE); // RTE firmware + unsigned char reply[4 + 1]; + _serial.writeByte(0xFE); // RTE firmware _serial.readBlock(reply, sizeof reply); - if( reply[4] != 0x4D ) + if (reply[4] != 0x4D) LOG_THROW("Bad RTE firmware reply."); - rteFW = QString::asprintf("%d.%02d.%02d%s", reply[0], reply[1], reply[2], reply[3] ? "beta" : ""); + rteFW = QString::asprintf("%d.%02d.%02d%s", + reply[0], + reply[1], + reply[2], + reply[3] ? "beta" : ""); } //---- Font Package ------------------------------------------------------ QString fontPKG; { unsigned char echo = retryCommand(_serial, 'k'); // 0x69 OSTC4 FW Details. - if( echo != 'k' ) + if (echo != 'k') LOG_THROW("Bad firmware details."); - unsigned char reply[4+1]; - _serial.writeByte(0x10); // Font Package + unsigned char reply[4 + 1]; + _serial.writeByte(0x10); // Font Package _serial.readBlock(reply, sizeof reply); - if( reply[4] != 0x4D ) + if (reply[4] != 0x4D) LOG_THROW("Bad Font package reply."); - fontPKG = QString::asprintf("%d.%02d.%02d%s", reply[0], reply[1], reply[2], reply[3] ? "beta" : ""); + fontPKG = QString::asprintf("%d.%02d.%02d%s", + reply[0], + reply[1], + reply[2], + reply[3] ? "beta" : ""); } //---- OSTC4 specific description ---------------------------------------- descriptionString = QString("%1 #%2, fw %3 (main %4/RTE %5/Font %6), %7") - .arg(model()) - .arg(serialNumber(), 4, 10, QChar('0')) - .arg( QString::asprintf("%d.%02d", firmware() / 100, firmware() % 100)) - .arg( mainFW ) - .arg( rteFW ) - .arg( fontPKG ) - .arg( QString::fromLatin1((char*)(computerText), 60) - .replace(QChar('\0'), "") - .trimmed() ); + .arg(model()) + .arg(serialNumber(), 4, 10, QChar('0')) + .arg(QString::asprintf("%d.%02d", firmware() / 100, firmware() % 100)) + .arg(mainFW) + .arg(rteFW) + .arg(fontPKG) + .arg(QString::fromLatin1((char *) (computerText), 60) + .replace(QChar('\0'), "") + .trimmed()); LOG_INFO("Found " << descriptionString); } @@ -198,7 +207,7 @@ ////////////////////////////////////////////////////////////////////////////// OSTC4Operations::OSTC4Operations() - : fileChecksum(0,0) + : fileChecksum(0, 0) { emulatorName = "OSTC4/5"; m_crcWrapper = new CrcWrapper(nullptr); @@ -216,11 +225,9 @@ LOG_INFO("Please wait for OSTC4/5 to complete installation..."); } -#define SWAP4BYTES(x) \ -uint( (((x) & 0x000000FF) << 24) \ - | (((x) & 0x0000FF00) << 8) \ - | (((x) & 0x00FF0000) >> 8) \ - | (((x) & 0xFF000000) >> 24)) +#define SWAP4BYTES(x) \ + uint((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) \ + | (((x) & 0xFF000000) >> 24)) void OSTC4Operations::setIcons(const QString &fileName) { @@ -240,19 +247,16 @@ blockSize = FIRMWARE_BLOCK; transferDelay = FIRMWARE_BLOCK_DELAY; - try - { + try { bmp = std::make_unique<BmpToArray>(fileName); - } - catch (const std::exception& e) - { + } catch (const std::exception &e) { std::cerr << "Fehler beim Laden: " << e.what() << '\n'; } QByteArray bin = bmp->getTransferBytes(); bmp->getImageXY(&width, &height); - if(bin.size() < 0x20000 - 12) /* max bin size - header */ + if (bin.size() < 0x20000 - 12) /* max bin size - header */ { buf3offset[0] = 0x20; buf3offset[1] = 0x00; @@ -265,76 +269,72 @@ header.type = 0x20; /* original header shallnot be modifieded => split image size information into 5 bytes */ - header.dummy5 = width; /* low byte */ - header.dummy6 = height; /* low byte */ - header.dummy7 = (width >> 8) & 0x0000000F; /* shifted high nibble */ + header.dummy5 = width; /* low byte */ + header.dummy6 = height; /* low byte */ + header.dummy7 = (width >> 8) & 0x0000000F; /* shifted high nibble */ header.dummy7 |= (height >> 4) & 0x000000F0; /* shifted high nibble */ header.length = SWAP4BYTES(bin.size()); - header.checksum = bin.size() + (256*256*256*header.type) + (256*256* header.dummy5) + (256*header.dummy6) + header.dummy7; + header.checksum = bin.size() + (256 * 256 * 256 * header.type) + (256 * 256 * header.dummy5) + + (256 * header.dummy6) + header.dummy7; - checksum = SWAP4BYTES(m_crcWrapper->CRC_CalcBlockCRC(reinterpret_cast<uint32_t*>(bin.data()), bin.size() / 4)); + checksum = SWAP4BYTES( + m_crcWrapper->CRC_CalcBlockCRC(reinterpret_cast<uint32_t *>(bin.data()), + bin.size() / 4)); //---- Upload icon ----------------------------------------------- - const int steps = 3 + (bin.size()+blockSize-1) / blockSize; + const int steps = 3 + (bin.size() + blockSize - 1) / blockSize; int step = 0; PROGRESS(step++, steps); LOG_INFO(QString(" uploading icon size %1 bytes").arg(bin.size())); - writeText( QString("Upload Icon") ); + writeText(QString("Upload Icon")); //---- Command PROGRESS(step++, steps); echo = retryCommand(_serial, 'o'); // 0x6F upload icon - if( echo != 'o' ) + if (echo != 'o') LOG_THROW("Bad OSTC4/5 icon upload."); //---- Header PROGRESS(step++, steps); - _serial.writeBlock((uchar*)&header, sizeof header); + _serial.writeBlock((uchar *) &header, sizeof header); _serial.sleep(500); //---- Data - for(int len = 0x00000; len < bin.size(); len += blockSize) - { - if(transferDelay) - { - _serial.sleep(transferDelay); // 15 msec between 64B blocks... + for (int len = 0x00000; len < bin.size(); len += blockSize) { + if (transferDelay) { + _serial.sleep(transferDelay); // 15 msec between 64B blocks... } PROGRESS(step++, steps); - size = qMin(blockSize, - bin.size() - len); + size = qMin(blockSize, bin.size() - len); LOG_TRACE("Fill " << size); - _serial.writeBlock((uchar*)bin.data()+len, (unsigned int)size); + _serial.writeBlock((uchar *) bin.data() + len, (unsigned int) size); } - _serial.writeBlock((uchar*)&checksum, 4); + _serial.writeBlock((uchar *) &checksum, 4); PROGRESS(steps, steps); // 100% //---- Wait acknowledge ---------------------------------------------- PROGRESS_THROTTLE(); - for(int wait=0; wait<2*60; ++wait) // Up to 2 min... - { - try { - unsigned char reply = _serial.readByte(); - if( reply == 0x4D ) - break; - } - catch( const ReadTimeout&) {} + for (int wait = 0; wait < 2 * 60; ++wait) // Up to 2 min... + { + try { + unsigned char reply = _serial.readByte(); + if (reply == 0x4D) + break; + } catch (const ReadTimeout &) { } + } LOG_INFO(" icon uploaded."); - } - else - { + } else { LOG_THROW("Image to large. Must be < 128k."); } PROGRESS_RESET(); } - - ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// /// FIRMWARE UPGRADE /// @@ -357,12 +357,12 @@ int transferDelay; // Previous trial failed ? - if( _file.isOpen() ) _file.close(); + if (_file.isOpen()) + _file.close(); _file.setFileName(fileName); - if( ! _file.open(QIODevice::ReadOnly) ) - LOG_THROW( "Cannot open BIN file " << fileName ); - + if (!_file.open(QIODevice::ReadOnly)) + LOG_THROW("Cannot open BIN file " << fileName); // Get settings to overwrite version check forceFirmwareUpdate = settings->value("OSTC/forceFirmwareUpdate", false).toBool(); @@ -373,125 +373,118 @@ //---- Check consistency ------------------------------------------------- fileChecksum.reset(0, 0); - for(int part=1; part<4; ++part) - { + for (int part = 1; part < 4; ++part) { // Less than 3 parts ? - if( (_file.size() - _file.pos()) < 16 ) + if ((_file.size() - _file.pos()) < 16) break; - if( dryRun ) - LOG_TRACE("Checking part " << part <<"..."); + if (dryRun) + LOG_TRACE("Checking part " << part << "..."); FirmwareOSTC4 header; QByteArray bin; loadFirmwarePart(header, bin, part, dryRun); QString name = QString::asprintf("%s v%d.%02d.%02d%s", - header.type==0x10 ? "Font" - : header.type==0xFF ? "FW " - : header.type==0xFE ? "RTE " - : "??? ", - header.version.x, header.version.y, header.version.z, - header.version.beta ? " beta" : ""); + header.type == 0x10 ? "Font" + : header.type == 0xFF ? "FW " + : header.type == 0xFE ? "RTE " + : "??? ", + header.version.x, + header.version.y, + header.version.z, + header.version.beta ? " beta" : ""); //---- On first pass, just check file structure ---------------------- - if( dryRun ) + if (dryRun) continue; //---- On second pass, do upload new stuff --------------------------- - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) connectServiceMode(); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) LOG_THROW("Cannot connect OSTC 4/5 service mode..."); //---- Check if needed ? --------------------------------------------- LOG_TRACE("Check part " << part << " is needed ?"); unsigned char echo = retryCommand(_serial, 'k'); // 0x6B OSTC4 FW Details. - if( echo != 'k' ) + if (echo != 'k') LOG_THROW("Bad firmware details."); - unsigned char reply[4+1]; - _serial.writeByte(header.type); // type of firmware part + unsigned char reply[4 + 1]; + _serial.writeByte(header.type); // type of firmware part _serial.readBlock(reply, sizeof reply); - if( reply[4] != 0x4C ) // SERVICE MODE only. + if (reply[4] != 0x4C) // SERVICE MODE only. LOG_THROW("Bad firmware reply."); - if(((reply[0] *100 + reply[1] * 10 + reply[2]) > 151 ) && (header.type==0xFF)) /* suitable firmware version? First supported in 1.5.2 */ + if (((reply[0] * 100 + reply[1] * 10 + reply[2]) > 151) + && (header.type == 0xFF)) /* suitable firmware version? First supported in 1.5.2 */ { firmwareSupportFastMode = true; } - if(( header.version.x == reply[0] - && header.version.y == reply[1] - && header.version.z == reply[2] ) - && (!(forceFirmwareUpdate && header.type==0xFF)) - && (!(forceRTEUpdate && header.type==0xFE)) - && (!(forceFontlibUpdate && header.type==0x10)) - ) - { + if ((header.version.x == reply[0] && header.version.y == reply[1] + && header.version.z == reply[2]) + && (!(forceFirmwareUpdate && header.type == 0xFF)) + && (!(forceRTEUpdate && header.type == 0xFE)) + && (!(forceFontlibUpdate && header.type == 0x10))) { LOG_INFO(" part " << part << ": " << name << " already up-to-date."); continue; } - if((useFastMode) && (firmwareSupportFastMode)) /* is fast mode supported ? */ + if ((useFastMode) && (firmwareSupportFastMode)) /* is fast mode supported ? */ { blockSize = FIRMWARE_BLOCK_FAST; transferDelay = 1; - } - else { + } else { blockSize = FIRMWARE_BLOCK; transferDelay = FIRMWARE_BLOCK_DELAY; } //---- Upload firwmare ----------------------------------------------- - const int steps = 3 + (bin.size()+blockSize-1) / blockSize; + const int steps = 3 + (bin.size() + blockSize - 1) / blockSize; int step = 0; PROGRESS(step++, steps); - LOG_INFO(" uploading part " << part - << ": " << name - << "..."); - writeText( QString("Upload Part %1").arg(part) ); + LOG_INFO(" uploading part " << part << ": " << name << "..."); + writeText(QString("Upload Part %1").arg(part)); //---- Command PROGRESS(step++, steps); echo = retryCommand(_serial, 's'); // 0x73 OSTC4 FW Upgrade - if( echo != 's' ) + if (echo != 's') LOG_THROW("Bad OSTC4/5 FW upgrade commande."); //---- Header PROGRESS(step++, steps); - _serial.writeBlock((uchar*)&header, sizeof header); - _serial.sleep(500); + _serial.writeBlock((uchar *) &header, sizeof header); + _serial.sleep(500); //---- Data - for(int len = 0x00000; len < bin.size(); len += blockSize) - { - if(transferDelay) - { - _serial.sleep(transferDelay); // 15 msec between 64B blocks... + for (int len = 0x00000; len < bin.size(); len += blockSize) { + if (transferDelay) { + _serial.sleep(transferDelay); // 15 msec between 64B blocks... } PROGRESS(step++, steps); - size = qMin(blockSize, - bin.size() - len); + size = qMin(blockSize, bin.size() - len); LOG_TRACE("Fill " << size); - _serial.writeBlock((uchar*)bin.data()+len, (unsigned int)size); + _serial.writeBlock((uchar *) bin.data() + len, (unsigned int) size); } PROGRESS(steps, steps); // 100% //---- Wait acknowledge ---------------------------------------------- // RTE seems to miss the acknowledge byte ... - if( header.type != 0xFE ) { + if (header.type != 0xFE) { PROGRESS_THROTTLE(); - for(int wait=0; wait<2*60; ++wait) // Up to 2 min... + for (int wait = 0; wait < 2 * 60; ++wait) // Up to 2 min... { try { unsigned char reply = _serial.readByte(); - if( reply == 0x4C ) + if (reply == 0x4C) break; + } catch (const ReadTimeout &) { } - catch( const ReadTimeout&) {} } } @@ -500,26 +493,28 @@ //---- Done -------------------------------------------------------------- // Low-level close, to avoid trying to send a 0xFF byte... - if( !dryRun ) { + if (!dryRun) { disconnect(true); } PROGRESS_RESET(); //---- Check FILE checksum on first pass --------------------------------- - if( dryRun ) { + if (dryRun) { uint expected; - if( 4 != _file.read((char*)&expected, sizeof expected) ) + if (4 != _file.read((char *) &expected, sizeof expected)) LOG_THROW("Missing file checksum."); uint checksum = fileChecksum.a() | (fileChecksum.b() << 16); - if( checksum != expected ) - LOG_ERROR( QString::asprintf("File checksum ERROR: expected = %04X, actual = %04X", expected, checksum) ); - else if( dryRun ) - LOG_TRACE( QString::asprintf("File checksum = %04X (OK)", checksum) ); + if (checksum != expected) + LOG_ERROR(QString::asprintf("File checksum ERROR: expected = %04X, actual = %04X", + expected, + checksum)); + else if (dryRun) + LOG_TRACE(QString::asprintf("File checksum = %04X (OK)", checksum)); - if( ! _file.atEnd() ) + if (!_file.atEnd()) LOG_THROW("Extra data after last block."); } @@ -528,54 +523,63 @@ ////////////////////////////////////////////////////////////////////////////// -void OSTC4Operations::loadFirmwarePart(FirmwareOSTC4& header, - QByteArray& bin, - int part, - bool dryRun) +void OSTC4Operations::loadFirmwarePart(FirmwareOSTC4 &header, QByteArray &bin, int part, bool dryRun) { memset(&header, 0, sizeof header); //---- Read part header and check consistency ---------------------------- - if( sizeof header != _file.read((char*)&header, sizeof header) ) + if (sizeof header != _file.read((char *) &header, sizeof header)) LOG_THROW("EOF in header."); fileChecksum.add(&header, sizeof header); - uint checksum = SWAP4BYTES(header.length) - + SWAP4BYTES(*(uint*)&header.type); - if( checksum != header.checksum ) - LOG_THROW( QString::asprintf("Inconsistent header (%04X != %04X).", checksum, header.checksum) ); + uint checksum = SWAP4BYTES(header.length) + SWAP4BYTES(*(uint *) &header.type); + if (checksum != header.checksum) + LOG_THROW( + QString::asprintf("Inconsistent header (%04X != %04X).", checksum, header.checksum)); - if( SWAP4BYTES(header.length) > (1024 * 1024) || (SWAP4BYTES(header.length) % 4) ) - LOG_THROW("Inconsistent header (part=" << part << ", size=" << SWAP4BYTES(header.length) << ")."); + if (SWAP4BYTES(header.length) > (1024 * 1024) || (SWAP4BYTES(header.length) % 4)) + LOG_THROW("Inconsistent header (part=" << part << ", size=" << SWAP4BYTES(header.length) + << ")."); - if( dryRun ) - switch( header.type ) { - case 0x10: LOG_TRACE("... font"); break; - case 0xFF: LOG_TRACE("... firmware"); break; - case 0xFE: LOG_TRACE("... RTE"); break; + if (dryRun) + switch (header.type) { + case 0x10: + LOG_TRACE("... font"); + break; + case 0xFF: + LOG_TRACE("... firmware"); + break; + case 0xFE: + LOG_TRACE("... RTE"); + break; default: - // LOG_THROW("Inconsistent header (part=" << part << ", type=0x" << QString::asprintf(int(header.type), 16) << ")."); - LOG_THROW("Inconsistent header (part=" << part << ", type=0x" << QString::asprintf("Value = %d", header.type) << ")."); + // LOG_THROW("Inconsistent header (part=" << part << ", type=0x" << QString::asprintf(int(header.type), 16) << ")."); + LOG_THROW("Inconsistent header (part=" << part << ", type=0x" + << QString::asprintf("Value = %d", header.type) + << ")."); break; } - if( dryRun ) + if (dryRun) LOG_TRACE("... version " << QString::asprintf("%d.%02d.%02d.%s", - header.version.x, header.version.y, header.version.z, - header.version.beta ? " beta" : "")); + header.version.x, + header.version.y, + header.version.z, + header.version.beta ? " beta" : "")); //---- Read Binary Data -------------------------------------------------- - if( dryRun ) + if (dryRun) LOG_TRACE("... size " << SWAP4BYTES(header.length) << " bytes."); bin.resize(SWAP4BYTES(header.length) + 4); - assert((uint)bin.size() == SWAP4BYTES(header.length) + 4); + assert((uint) bin.size() == SWAP4BYTES(header.length) + 4); - if( bin.size() != _file.read(bin.data(), bin.size()) ) + if (bin.size() != _file.read(bin.data(), bin.size())) LOG_THROW("EOF in data."); - if( dryRun ) + if (dryRun) LOG_TRACE("... checksum " << QString::asprintf("%08x", - *(uint*)(bin.data() + SWAP4BYTES(header.length)))); + *(uint *) (bin.data() + + SWAP4BYTES(header.length)))); fileChecksum.add(bin); } @@ -584,133 +588,127 @@ char buffer[60]; memset(buffer, 0, sizeof buffer); unsigned char echo = retryCommand(_serial, 'l'); // 0x6c request bluetooth signal - if( echo != 0x6C ) + if (echo != 0x6C) LOG_THROW("Bad text reply (1)" << echo); _serial.sleep(1000); unsigned char ok = _serial.readByte(); - if( ok != 0x4D ) // DOWNLOAD mode only. + if (ok != 0x4D) // DOWNLOAD mode only. LOG_THROW("Bad clock reply (2)"); } - -void OSTC4Operations::getAllHeader(unsigned char* pBuffer) +void OSTC4Operations::getAllHeader(unsigned char *pBuffer) { unsigned char index = 0; char buffer[60]; memset(buffer, 0, sizeof buffer); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) connectServiceMode(); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) LOG_THROW("Cannot connect OSTC4 service mode..."); - unsigned char echo = retryCommand(_serial, 0x85); // 0x85 request all headers (dump) - if( echo != 0x85 ) + if (echo != 0x85) LOG_THROW("Bad text reply (1)" << echo); _serial.sleep(1000); - for(index = 0; index < 8; index++) // Expect 8 blocks a 0x8000 + for (index = 0; index < 8; index++) // Expect 8 blocks a 0x8000 { _serial.readBlock(pBuffer + (index * 0x8000), 0x8000); LOG_INFO((".")); } - *(pBuffer + (index * 0x8000)) = _serial.readByte(); // get lastdiveID - LOG_INFO(QString::asprintf("%d",*(pBuffer + (index * 0x8000)))); + *(pBuffer + (index * 0x8000)) = _serial.readByte(); // get lastdiveID + LOG_INFO(QString::asprintf("%d", *(pBuffer + (index * 0x8000)))); unsigned char ok = _serial.readByte(); - if( ok != 0x4C ) //Service mode only. + if (ok != 0x4C) //Service mode only. LOG_THROW("Bad clock reply (2)"); } -void OSTC4Operations::writeAllHeader(unsigned char* pBuffer) +void OSTC4Operations::writeAllHeader(unsigned char *pBuffer) { unsigned char index = 0; char buffer[60]; memset(buffer, 0, sizeof buffer); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) connectServiceMode(); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) LOG_THROW("Cannot connect OSTC4 service mode..."); - unsigned char echo = retryCommand(_serial, 0x86); // 0x86 request all headers (dump) - if( echo != 0x86 ) + if (echo != 0x86) LOG_THROW("Bad text reply (1)" << echo); _serial.sleep(1000); - for(index = 0; index < 8; index++) // Expect 8 blocks a 0x8000 + for (index = 0; index < 8; index++) // Expect 8 blocks a 0x8000 { _serial.writeBlock(pBuffer + (index * 0x8000), 0x8000); LOG_INFO((".")); } - _serial.writeByte(*(pBuffer + (index * 0x8000))); // write lastdiveID + _serial.writeByte(*(pBuffer + (index * 0x8000))); // write lastdiveID unsigned char ok = _serial.readByte(); - if( ok != 0x4C ) //Service mode only. + if (ok != 0x4C) //Service mode only. LOG_THROW("Bad clock reply (2)"); } -void OSTC4Operations::getAllSamples(unsigned char* pBuffer) +void OSTC4Operations::getAllSamples(unsigned char *pBuffer) { unsigned short index = 0; char buffer[60]; memset(buffer, 0, sizeof buffer); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) connectServiceMode(); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) LOG_THROW("Cannot connect OSTC4 service mode..."); - unsigned char echo = retryCommand(_serial, 0x88); // 0x88 request all samples (dump) - if( echo != 0x88 ) + if (echo != 0x88) LOG_THROW("Bad text reply (1)" << echo); _serial.sleep(500); - for(index = 0; index < 384; index++) // Expect 8 blocks a 0x8000 + for (index = 0; index < 384; index++) // Expect 8 blocks a 0x8000 { _serial.readBlock(pBuffer + (index * 0x8000), 0x8000); - LOG_INFO(QString::asprintf("%d",index)); + LOG_INFO(QString::asprintf("%d", index)); } - _serial.readBlock(pBuffer + (index * 0x8000), 4); // get lastdiveID + _serial.readBlock(pBuffer + (index * 0x8000), 4); // get lastdiveID unsigned char ok = _serial.readByte(); - if( ok != 0x4C ) //Service mode only. + if (ok != 0x4C) //Service mode only. LOG_THROW("Bad clock reply (2)"); } - -void OSTC4Operations::writeAllSamples(unsigned char* pBuffer) +void OSTC4Operations::writeAllSamples(unsigned char *pBuffer) { unsigned short index = 0; char buffer[60]; memset(buffer, 0, sizeof buffer); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) connectServiceMode(); - if( _connectMode != SERVICE_MODE ) + if (_connectMode != SERVICE_MODE) LOG_THROW("Cannot connect OSTC4 service mode..."); - unsigned char echo = retryCommand(_serial, 0x89); // 0x88 request all samples (dump) - if( echo != 0x89 ) + if (echo != 0x89) LOG_THROW("Bad text reply (1)" << echo); _serial.sleep(500); - for(index = 0; index < 384; index++) // Expect 8 blocks a 0x8000 384 + for (index = 0; index < 384; index++) // Expect 8 blocks a 0x8000 384 { - _serial.writeBlock((const unsigned char*)(pBuffer + (index * 0x8000)), 0x8000); - LOG_INFO(QString::asprintf("%d",index)); + _serial.writeBlock((const unsigned char *) (pBuffer + (index * 0x8000)), 0x8000); + LOG_INFO(QString::asprintf("%d", index)); } - _serial.writeBlock(pBuffer + (index * 0x8000), 4); // set lastdiveID + _serial.writeBlock(pBuffer + (index * 0x8000), 4); // set lastdiveID unsigned char ok = _serial.readByte(); - if( ok != 0x4C ) //Service mode only. + if (ok != 0x4C) //Service mode only. LOG_THROW("Bad clock reply (2)"); }
