* @brief Reacts to a byte received while waiting for ACK/NAK of a data block. * Returns false when processing must stop because the transfer ended. */
| 121 | * Returns false when processing must stop because the transfer ended. |
| 122 | */ |
| 123 | bool IO::Protocols::YMODEM::handleDataAckByte(quint8 ch) |
| 124 | { |
| 125 | if (ch == kACK) { |
| 126 | m_timeoutTimer.stop(); |
| 127 | m_retryCount = 0; |
| 128 | m_blockNumber = static_cast<quint8>((m_blockNumber + 1) & 0xFF); |
| 129 | |
| 130 | if (m_file.atEnd()) { |
| 131 | m_yState = YState::WaitingForFirstEOTResponse; |
| 132 | Q_EMIT writeRequested(QByteArray(1, static_cast<char>(kEOT))); |
| 133 | Q_EMIT statusMessage(tr("Sending first EOT…")); |
| 134 | m_timeoutTimer.start(m_timeoutMs); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | m_yState = YState::SendingData; |
| 139 | sendDataBlock(); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | if (ch == kNAK) { |
| 144 | m_timeoutTimer.stop(); |
| 145 | ++m_retryCount; |
| 146 | if (m_retryCount >= m_maxRetries) { |
| 147 | QByteArray cancel(5, static_cast<char>(kCAN)); |
| 148 | Q_EMIT writeRequested(cancel); |
| 149 | resetState(); |
| 150 | m_yState = YState::Idle; |
| 151 | Q_EMIT statusMessage(tr("Too many retries, transfer aborted")); |
| 152 | Q_EMIT finished(false, tr("Maximum retries exceeded")); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | Q_EMIT statusMessage(tr("NAK received, retrying block %1").arg(m_blockNumber)); |
| 157 | |
| 158 | m_bytesSent = qMax<qint64>(0, m_bytesSent - m_lastBlockBytes); |
| 159 | if (!m_file.seek(m_lastBlockStart)) [[unlikely]] { |
| 160 | m_file.close(); |
| 161 | Q_EMIT finished(false, tr("Failed to seek in file")); |
| 162 | return false; |
| 163 | } |
| 164 | m_yState = YState::SendingData; |
| 165 | sendDataBlock(); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | if (ch == kCAN) { |
| 170 | m_timeoutTimer.stop(); |
| 171 | resetState(); |
| 172 | m_yState = YState::Idle; |
| 173 | Q_EMIT statusMessage(tr("Transfer cancelled by receiver")); |
| 174 | Q_EMIT finished(false, tr("Receiver cancelled the transfer")); |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | /** |
nothing calls this directly
no test coverage detected