diff OSTC3Operations.cpp @ 8:21ce6187d32e

Minor changes done by automatic style checker
author Ideenmodellierer
date Mon, 12 Jan 2026 13:51:17 +0000
parents 115cfa4a3239
children
line wrap: on
line diff
--- a/OSTC3Operations.cpp	Mon Jan 12 13:49:16 2026 +0000
+++ b/OSTC3Operations.cpp	Mon Jan 12 13:51:17 2026 +0000
@@ -40,8 +40,8 @@
 #include "Utils/Log.h"
 #include "Utils/ProgressEvent.h"
 
+#include "HexFile.h"
 #include "SettingsDialog.h"
-#include "HexFile.h"
 
 #include <QApplication>
 #include <QDateTime>
@@ -49,30 +49,30 @@
 #include <QRegularExpression>
 
 // Byte extration, compatible littleendian or bigendian.
-#define LOW(x)      ((unsigned char)((x) % 256))
-#define HIGH(x)     ((unsigned char)((x / (1<<8)) % 256))
-#define UPPER(x)    ((unsigned char)((x / (1<<16)) % 256))
-#define UP32(x)     ((unsigned char)((x / (1<<24)) % 256))
+#define LOW(x) ((unsigned char) ((x) % 256))
+#define HIGH(x) ((unsigned char) ((x / (1 << 8)) % 256))
+#define UPPER(x) ((unsigned char) ((x / (1 << 16)) % 256))
+#define UP32(x) ((unsigned char) ((x / (1 << 24)) % 256))
 
-#define FIRMWARE_AREA   0x3E0000
-#define FIRMWARE_SIZE   0x01E000        // 120KB
-#define FIRMWARE_BLOCK    0x1000        //   4KB
+#define FIRMWARE_AREA 0x3E0000
+#define FIRMWARE_SIZE 0x01E000 // 120KB
+#define FIRMWARE_BLOCK 0x1000  //   4KB
 
 //////////////////////////////////////////////////////////////////////////////
 
 OSTC3Operations::OSTC3Operations()
-  : descriptionString(""),
-    emulatorName("OSTC3"),
-    _computerFirmware(0),
-    _computerSerial(0),
-    _connectMode(CLOSED_MODE)
+    : descriptionString("")
+    , emulatorName("OSTC3")
+    , _computerFirmware(0)
+    , _computerSerial(0)
+    , _connectMode(CLOSED_MODE)
 {
     memset(computerText, 0, sizeof computerText);
 }
 
 OSTC3Operations::~OSTC3Operations()
 {
-    if( _connectMode != CLOSED_MODE )
+    if (_connectMode != CLOSED_MODE)
         disconnect(true);
 }
 
@@ -87,51 +87,49 @@
 
 bool OSTC3Operations::connect()
 {
-    LOG_TRACE( "Enter download mode..." );
+    LOG_TRACE("Enter download mode...");
 
     try {
         _connectMode = CLOSED_MODE;
-        _serial.open( Settings::port, emulatorName);
-        _serial.sleep(333);    // Initial 1/3 sec. delay to first comm.
+        _serial.open(Settings::port, emulatorName);
+        _serial.sleep(333); // Initial 1/3 sec. delay to first comm.
 
-        for(int retry=0; retry < 10; ++retry)
-        {
+        for (int retry = 0; retry < 10; ++retry) {
             // Allow for 0.1sec extra delay
             try {
                 _serial.writeByte(0xBB);
-            } catch(const WriteTimeout& ) {
+            } catch (const WriteTimeout &) {
                 // Bluetooth not present: one can open the pseudo COM port,
                 // but we will have a timeout on first write byte...
-                if( retry < 9 ) {
-                    LOG_INFO("Cannot connect to " << Settings::port <<" (" << (retry+1) << "/10)...");
+                if (retry < 9) {
+                    LOG_INFO("Cannot connect to " << Settings::port << " (" << (retry + 1)
+                                                  << "/10)...");
                     _serial.sleep(1000);
                     continue;
                 }
-                LOG_THROW("Cannot connect to " << model() <<".");
+                LOG_THROW("Cannot connect to " << model() << ".");
                 return false;
             }
 
             _serial.sleep(100);
 
             //---- Check acknowledge, w/o fatal timeouts.
-            unsigned char ok   = 0;
+            unsigned char ok = 0;
             unsigned char echo = 0;
             try {
                 echo = _serial.readByte();
 
                 // Already in connect() mode ???
-                if( echo == 'M' )
+                if (echo == 'M')
                     break;
 
                 ok = _serial.readByte();
-            }
-            catch(const ReadTimeout&) {
-                LOG_INFO("Retry " << (retry+1) << "/10...");
+            } catch (const ReadTimeout &) {
+                LOG_INFO("Retry " << (retry + 1) << "/10...");
             }
 
-            if( echo != 0xBB || ok != 0x4D ) {  // DOWNLOAD modes only.
-                if( retry < 9 )
-                {
+            if (echo != 0xBB || ok != 0x4D) { // DOWNLOAD modes only.
+                if (retry < 9) {
                     _serial.purge();
                     _serial.sleep(400);
                     continue;
@@ -148,8 +146,7 @@
         LOG_TRACE("Connected.");
         _connectMode = DOWNLOAD_MODE;
         return true;
-    }
-    catch(const Exception& e) {
+    } catch (const Exception &e) {
         disconnect();
         LOG_THROW("Port " << Settings::port << ": " << e.what());
     }
@@ -160,13 +157,14 @@
 
 bool OSTC3Operations::disconnect(bool /*closing*/)
 {
-    if( _connectMode == CLOSED_MODE ) return false;
+    if (_connectMode == CLOSED_MODE)
+        return false;
 
-    descriptionString.clear();  // cleanup for interface updateStatus()
+    descriptionString.clear(); // cleanup for interface updateStatus()
     _connectMode = CLOSED_MODE;
 
     _serial.purge();
-    _serial.writeByte(0xFF);    // Exit communications, just in case...
+    _serial.writeByte(0xFF); // Exit communications, just in case...
     _serial.sleep(100);
 
     _serial.purge();
@@ -183,8 +181,8 @@
 
     LOG_TRACE("Getting model...");
     HardwareDescriptor hw = hardwareDescriptor();
- //   if( hw != HW_OSTC3 )
- //        LOG_THROW("Not an OSTC3.");
+    //   if( hw != HW_OSTC3 )
+    //        LOG_THROW("Not an OSTC3.");
 
     LOG_TRACE("Getting identity...");
     getCommonIdentity();
@@ -197,24 +195,25 @@
 void OSTC3Operations::getCommonIdentity()
 {
     unsigned char echo = retryCommand(_serial, 'i'); // 0x69
-    if( echo != 'i' )
+    if (echo != 'i')
         LOG_THROW("Bad identity reply (1)");
 
     unsigned char header[4 + 60 + 1] = {0};
     _serial.readBlock(header, sizeof header);
-    if( header[64] != 0x4D )    // DOWNLOAD modes only.
+    if (header[64] != 0x4D) // DOWNLOAD modes only.
         LOG_THROW("Bad identity reply (2)");
 
-    _computerSerial   = header[0] + header[1]*256;
+    _computerSerial = header[0] + header[1] * 256;
     _computerFirmware = header[2] * 100 + header[3];
-    memcpy(computerText, header+4, sizeof computerText);
+    memcpy(computerText, header + 4, sizeof computerText);
 
-    descriptionString = QString("%1 #%2, fw %3.%4, %5")
-            .arg(model())
-            .arg(serialNumber(), 4, 10, QChar('0'))
-            .arg(firmware() / 100).arg(firmware() % 100, 2, 10, QChar('0'))
-            .arg( QString::fromLatin1((char*)(computerText), 60)
-                .replace(QChar('\0'), " ") );
+    descriptionString
+        = QString("%1 #%2, fw %3.%4, %5")
+              .arg(model())
+              .arg(serialNumber(), 4, 10, QChar('0'))
+              .arg(firmware() / 100)
+              .arg(firmware() % 100, 2, 10, QChar('0'))
+              .arg(QString::fromLatin1((char *) (computerText), 60).replace(QChar('\0'), " "));
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -243,7 +242,7 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
-void OSTC3Operations::writeText(const QString& msg)
+void OSTC3Operations::writeText(const QString &msg)
 {
     QByteArray buffer = msg.leftJustified(16, ' ', true).toLatin1();
     LOG_TRACE("Echoing string '" << buffer << "'");
@@ -251,14 +250,14 @@
     // 2014-10-27 jDG: On OSTC3 v1.60, after an ERASE AREA, we do get
     //                 a spurious L here (instead of n)...
     unsigned char echo = retryCommand(_serial, 'n'); // 0x6E Echo string.
-    if( echo != 'n' )
+    if (echo != 'n')
         LOG_THROW("Bad message reply (1)");
 
-    _serial.writeBlock((const unsigned char*)buffer.data(), 16);
-    _serial.sleep(25);  // Allow 25msec to display the message...
+    _serial.writeBlock((const unsigned char *) buffer.data(), 16);
+    _serial.sleep(25); // Allow 25msec to display the message...
 
     unsigned char ok = _serial.readByte();
-    if( ok != 0x4C && ok != 0x4D )      // DOWNLOAD or SERVICE modes.
+    if (ok != 0x4C && ok != 0x4D) // DOWNLOAD or SERVICE modes.
         LOG_THROW("Bad message reply (2)");
 }
 
@@ -277,17 +276,17 @@
     buffer[5] = date.date().year() % 100;
 
     unsigned char echo = retryCommand(_serial, 'b'); // 0x62 Sync date
-    if( echo != 'b' )
+    if (echo != 'b')
         LOG_THROW("Bad clock reply (1)");
 
     _serial.writeBlock(buffer, sizeof buffer);
     _serial.sleep(5);
 
     unsigned char ok = _serial.readByte();
-    if( ok != 0x4D )    // DOWNLOAD mode only.
+    if (ok != 0x4D) // DOWNLOAD mode only.
         LOG_THROW("Bad clock reply (2)");
 
-    writeText( "Set " + date.toString("MM/dd hh:mm") );
+    writeText("Set " + date.toString("MM/dd hh:mm"));
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -301,14 +300,14 @@
     strncpy(buffer, newName.toLatin1().constData(), sizeof buffer);
 
     unsigned char echo = retryCommand(_serial, 'c'); // 0x63 Send custom text
-    if( echo != 'c' )
+    if (echo != 'c')
         LOG_THROW("Bad text reply (1)");
 
-    _serial.writeBlock((unsigned char*)buffer, sizeof buffer);
+    _serial.writeBlock((unsigned char *) buffer, sizeof buffer);
     _serial.sleep(5);
 
     unsigned char ok = _serial.readByte();
-    if( ok != 0x4D )    // DOWNLOAD modes only.
+    if (ok != 0x4D) // DOWNLOAD modes only.
         LOG_THROW("Bad text reply (2)");
 
     getIdentity();
@@ -331,12 +330,8 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
-static unsigned char ostc3SecretKey[16] = {
-    241,233, 176, 48,
-     69,111, 190, 85,
-    255,231, 248, 49,
-     19,108, 242,254
-};
+static unsigned char ostc3SecretKey[16]
+    = {241, 233, 176, 48, 69, 111, 190, 85, 255, 231, 248, 49, 19, 108, 242, 254};
 
 //////////////////////////////////////////////////////////////////////////////
 
@@ -352,8 +347,8 @@
     buffer[2] = LOW(addr);
     buffer[3] = LOW(size);
 
-    unsigned char reply = retryCommand(_serial, 'B');    // Command 'B'
-    if( reply != 0x42 )
+    unsigned char reply = retryCommand(_serial, 'B'); // Command 'B'
+    if (reply != 0x42)
         LOG_THROW("eraseRange (1)");
 
     _serial.writeBlock(buffer, 4);
@@ -362,23 +357,21 @@
     _serial.sleep(40 + size * 31);
 
     unsigned char ok = _serial.readByte();
-    if( ok != 0x4c )                    // SERVICE MODE acknowledge.
+    if (ok != 0x4c) // SERVICE MODE acknowledge.
         LOG_THROW("eraseRange (2)");
 }
 
 //////////////////////////////////////////////////////////////////////////////
 
-void OSTC3Operations::writeBlock(unsigned int addr,
-                                 const unsigned char *data,
-                                 unsigned int size)
+void OSTC3Operations::writeBlock(unsigned int addr, const unsigned char *data, unsigned int size)
 {
     unsigned char buffer[3];
     buffer[0] = UPPER(addr);
     buffer[1] = HIGH(addr);
     buffer[2] = LOW(addr);
 
-    unsigned char reply = retryCommand(_serial, '0');    // 0x30
-    if( reply != '0' )
+    unsigned char reply = retryCommand(_serial, '0'); // 0x30
+    if (reply != '0')
         LOG_THROW("startWrite");
 
     _serial.writeBlock(buffer, sizeof buffer);
@@ -391,14 +384,13 @@
     _serial.sleep(1100);
 
     unsigned char ok = _serial.readByte();
-    if( ok != 0x4c && ok != 0x4d )      // DOWNLOAD or SERVICE modes.
+    if (ok != 0x4c && ok != 0x4d) // DOWNLOAD or SERVICE modes.
         LOG_THROW("stopWrite");
 }
 
-
 //////////////////////////////////////////////////////////////////////////////
 
-void OSTC3Operations::readBlock(unsigned int addr, unsigned char* ptr, unsigned int size)
+void OSTC3Operations::readBlock(unsigned int addr, unsigned char *ptr, unsigned int size)
 {
     unsigned char buffer[6];
     buffer[0] = UPPER(addr);
@@ -408,17 +400,17 @@
     buffer[4] = HIGH(size);
     buffer[5] = LOW(size);
 
-    unsigned char reply = retryCommand(_serial, 0x20);   // Command ' '
-    if( reply != 0x20 )
+    unsigned char reply = retryCommand(_serial, 0x20); // Command ' '
+    if (reply != 0x20)
         LOG_THROW("readBytes");
 
     _serial.writeBlock(buffer, sizeof buffer);
-    _serial.sleep(500);         // Allow some time to start emitting...
+    _serial.sleep(500); // Allow some time to start emitting...
     _serial.readBlock(ptr, size);
 
     // Note: in that case, the OK byte is send AFTER the data block.
     unsigned char ok = _serial.readByte();
-    if( ok != 0x4C )                    // SERVICE modes only.
+    if (ok != 0x4C) // SERVICE modes only.
         LOG_THROW("readBytes (2)");
 }
 
@@ -434,18 +426,22 @@
 
     // Compute magic checksum's checksum.
     buffer[4] = 0x55;
-    buffer[4] ^= buffer[0]; buffer[4] =(buffer[4]<<1 | buffer[4]>>7);
-    buffer[4] ^= buffer[1]; buffer[4] =(buffer[4]<<1 | buffer[4]>>7);
-    buffer[4] ^= buffer[2]; buffer[4] =(buffer[4]<<1 | buffer[4]>>7);
-    buffer[4] ^= buffer[3]; buffer[4] =(buffer[4]<<1 | buffer[4]>>7);
+    buffer[4] ^= buffer[0];
+    buffer[4] = (buffer[4] << 1 | buffer[4] >> 7);
+    buffer[4] ^= buffer[1];
+    buffer[4] = (buffer[4] << 1 | buffer[4] >> 7);
+    buffer[4] ^= buffer[2];
+    buffer[4] = (buffer[4] << 1 | buffer[4] >> 7);
+    buffer[4] ^= buffer[3];
+    buffer[4] = (buffer[4] << 1 | buffer[4] >> 7);
 
-    unsigned char reply = retryCommand(_serial, 0x50);    // 'P' : send FW to bootloader
-    if( reply != 0x50 )
+    unsigned char reply = retryCommand(_serial, 0x50); // 'P' : send FW to bootloader
+    if (reply != 0x50)
         LOG_THROW("Flashing start (1)");
 
     _serial.writeBlock(buffer, sizeof buffer);
-   unsigned char ok = _serial.readByte();
-    if( ok != 0x4C )                    // SERVICE modes only.
+    unsigned char ok = _serial.readByte();
+    if (ok != 0x4C) // SERVICE modes only.
         LOG_THROW("Flashing start (2)");
 
     // NOTE: the device never return, because it always do a reset,
@@ -477,44 +473,40 @@
 QRegularExpression OSTC3Operations::portTemplate() const
 {
 #if defined(Q_OS_MAC)
-    return QRegularExpression("tty.usbserial-.*",
-                              QRegularExpression::CaseInsensitiveOption);
+    return QRegularExpression("tty.usbserial-.*", QRegularExpression::CaseInsensitiveOption);
 #elif defined(Q_OS_LINUX)
     // Debian, Ubuntu, RedHat, CentOS, SUSE
     return QRegularExpression("ttyUSB.*"); // default: case-sensitive
 #elif defined(Q_OS_WIN)
-    return QRegularExpression("COM.*");    // default: case-sensitive
+    return QRegularExpression("COM.*"); // default: case-sensitive
 #endif
 }
 //////////////////////////////////////////////////////////////////////////////
 
 void OSTC3Operations::connectServiceMode()
 {
-    LOG_TRACE( "Enter service mode..." );
+    LOG_TRACE("Enter service mode...");
 
     // NOTE: Service mode requires a special starting sequence, different from
     //       the usual download mode state.
     //       Also, the general acknowledge byte is changed to 0x4c 'L'.
 
-    assert( _connectMode != SERVICE_MODE );
+    assert(_connectMode != SERVICE_MODE);
 
-    if( _connectMode == CLOSED_MODE )
-    {
-        _serial.open( Settings::port, emulatorName);
+    if (_connectMode == CLOSED_MODE) {
+        _serial.open(Settings::port, emulatorName);
         _serial.sleep(333); // Initial 1/3 sec before trying service mode...
     }
-    for(int retry=0; retry < 10; ++retry)
-    {
-        unsigned char serviceMode[] = { 0xAA, 0xAB, 0xCD, 0xEF };
+    for (int retry = 0; retry < 10; ++retry) {
+        unsigned char serviceMode[] = {0xAA, 0xAB, 0xCD, 0xEF};
 
         try {
             _serial.writeBlock(serviceMode, sizeof serviceMode);
-        }
-        catch(const WriteTimeout&) {
+        } catch (const WriteTimeout &) {
             // Bluetooth not present: one can open the pseudo COM port,
             // but we will have a timeout on first write byte...
-            if( retry < 9 ) {
-                LOG_INFO("Cannot connect to " << Settings::port <<" (" << (retry+1) << "/10)...");
+            if (retry < 9) {
+                LOG_INFO("Cannot connect to " << Settings::port << " (" << (retry + 1) << "/10)...");
                 _serial.sleep(1000);
                 continue;
             }
@@ -529,31 +521,30 @@
         unsigned char echo = 0;
         try {
             echo = _serial.readByte();
+        } catch (...) {
         }
-        catch(...) {}
 
-        if( echo != 0x4b ) {
-serviceModeFailed:
-            if( retry < 9 )
-            {
+        if (echo != 0x4b) {
+        serviceModeFailed:
+            if (retry < 9) {
                 _serial.purge();
                 _serial.sleep(400);
                 continue;
             }
-            LOG_THROW("Unable to enter " << model() <<" service mode");
+            LOG_THROW("Unable to enter " << model() << " service mode");
         }
 
         echo = _serial.readByte();
-        if( echo != 0xAB )
+        if (echo != 0xAB)
             goto serviceModeFailed;
         echo = _serial.readByte();
-        if( echo != 0xCD )
+        if (echo != 0xCD)
             goto serviceModeFailed;
         echo = _serial.readByte();
-        if( echo != 0xEF )
+        if (echo != 0xEF)
             goto serviceModeFailed;
         echo = _serial.readByte();
-        if( echo != 0x4c )              // SERVICE modes only.
+        if (echo != 0x4c) // SERVICE modes only.
             goto serviceModeFailed;
         break;
     }
@@ -583,14 +574,12 @@
     LOG_INFO("Uploading firmware.");
     writeText(" Uploading...");
 
-    for(int len = 0x00000; len < FIRMWARE_SIZE; len += FIRMWARE_BLOCK)
-    {
+    for (int len = 0x00000; len < FIRMWARE_SIZE; len += FIRMWARE_BLOCK) {
         unsigned char percent = int(len * 100.0f / FIRMWARE_SIZE + 0.5f);
-        writeText( QString(" Uploading %1%")
-                       .arg(percent, 2) );
+        writeText(QString(" Uploading %1%").arg(percent, 2));
         PROGRESS(percent, 100);
 
-        writeBlock(FIRMWARE_AREA+len, hex.data()+len, FIRMWARE_BLOCK);
+        writeBlock(FIRMWARE_AREA + len, hex.data() + len, FIRMWARE_BLOCK);
     }
     PROGRESS(100, 100);
 
@@ -598,24 +587,21 @@
     LOG_INFO("Verify firmware.");
     writeText(" Verifying...");
     {
-        unsigned char* buffer = new unsigned char[FIRMWARE_SIZE];
+        unsigned char *buffer = new unsigned char[FIRMWARE_SIZE];
         Q_CHECK_PTR(buffer);
 
-        for(int len = 0x00000; len < FIRMWARE_SIZE; len += FIRMWARE_BLOCK)
-        {
+        for (int len = 0x00000; len < FIRMWARE_SIZE; len += FIRMWARE_BLOCK) {
             unsigned char percent = int(len * 100.0f / FIRMWARE_SIZE + 0.5f);
-            writeText( QString(" Verifying %1%")
-                        .arg(percent, 2) );
+            writeText(QString(" Verifying %1%").arg(percent, 2));
             PROGRESS(percent, 100);
 
-            readBlock(FIRMWARE_AREA+len, buffer+len, FIRMWARE_BLOCK);
+            readBlock(FIRMWARE_AREA + len, buffer + len, FIRMWARE_BLOCK);
         }
         PROGRESS(100, 100);
         qApp->processEvents(QEventLoop::ExcludeUserInputEvents, 10);
 
-        for(int i=0; i<FIRMWARE_SIZE; ++i)
-            if( buffer[i] != hex.data()[i] )
-            {
+        for (int i = 0; i < FIRMWARE_SIZE; ++i)
+            if (buffer[i] != hex.data()[i]) {
                 writeText(" Verify FAILED");
                 LOG_THROW("readback is different");
             }
@@ -627,7 +613,7 @@
     //---- Flashing firmware -------------------------------------------------
     LOG_INFO("Programming.");
     writeText(" Programming...");
-    upgradeFirmware( hex.checksum() );
+    upgradeFirmware(hex.checksum());
 
     //---- Done --------------------------------------------------------------
     // Low-level close, to avoid trying to send a 0xFF byte...
@@ -637,7 +623,7 @@
     PROGRESS_RESET();
 }
 
-void OSTC3Operations::loadFirmware(HexFile& hex, const QString& fileName) const
+void OSTC3Operations::loadFirmware(HexFile &hex, const QString &fileName) const
 {
     hex.allocate(FIRMWARE_SIZE);
     hex.loadEncrypted(fileName, ostc3SecretKey);
@@ -660,8 +646,7 @@
 HardwareOperations::CompanionFeatures OSTC3Operations::supported() const
 {
     // No ICON, no DUMPSCREEN, no VPM
-    return CompanionFeatures(PARAMETERS|DATE|NAME|FIRMWARE
-                     |HELIUM_DIVE|CCR_DIVE);
+    return CompanionFeatures(PARAMETERS | DATE | NAME | FIRMWARE | HELIUM_DIVE | CCR_DIVE);
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -669,20 +654,20 @@
 {
     return;
 }
-void OSTC3Operations::getAllHeader(unsigned char* pBuffer)
+void OSTC3Operations::getAllHeader(unsigned char *pBuffer)
 {
     return;
 }
-void OSTC3Operations::writeAllHeader(unsigned char* pBuffer)
+void OSTC3Operations::writeAllHeader(unsigned char *pBuffer)
 {
     return;
 }
 
-void OSTC3Operations::getAllSamples(unsigned char* pBuffer)
+void OSTC3Operations::getAllSamples(unsigned char *pBuffer)
 {
     return;
 }
-void OSTC3Operations::writeAllSamples(unsigned char* pBuffer)
+void OSTC3Operations::writeAllSamples(unsigned char *pBuffer)
 {
     return;
 }