| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /*
- * Copyright 2015 Omid Sakhi (www.omidsakhi.com)
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #ifndef _YMODEM_H
- #define _YMODEM_H
- #include <QObject>
- #include <QSerialPort>
- #include <QBuffer>
- #include <QFileInfo>
- #include <m_loger.h>
- #define SOH QChar(0x01)
- #define STX QChar(0x02)
- #define EOT 0x04
- #define ACK 0x06
- #define NAK 0x15
- #define CAN 0x18
- #define CTRLZ QChar(0x00)
- #define DLY_1S 1000
- #define MAXRETRANS 25
- class YMODEM : public QObject
- {
- Q_OBJECT
- private:
- //协议等待超时(毫秒)
- //主板 bootloader 在等头包时是"每 3s 发一次 'C'",所以任何等待都必须放宽到 3s 以上,
- //原实现的 (DLY_1S<<1)=2000ms 比该周期还短,必然超时。
- //用 enum 而不是 static constexpr(C++11 下 constexpr 成员被 odr-use 时要额外定义,会链不上)
- enum {
- kStartTimeoutMs = 30000, //等接收端首个 'C'
- kHeaderAckTimeoutMs = 8000, //头包 ACK(接收端可能先擦除 Flash)
- kAckTimeoutMs = 5000, //数据块 ACK
- kEotAckTimeoutMs = 5000, //EOT / 结束空块 ACK
- kClosingWaitMs = 4000 //等结束空块的 'C'
- };
- bool m_cancel;
- QSerialPort *m_serialPort;
- QByteArray dataBuf; //协议阶段收到的非协议字节(主板调试文本),仅用于日志排查
- QString m_fp;
- double percent;
- int yTransmitRead(unsigned short timeout);
- int yTransmitReadProtocol(unsigned int timeoutMs); //只返回协议字节,自动跳过主板调试文本
- bool waitForStart(unsigned int timeoutMs); //等接收端 'C'
- void sendCancel();
- int yTransmitPacket(const QFileInfo &fileInfo, QIODevice &dev, const quint32 &payloadSize, quint8 sequenceNum = 0, bool firstPacket = true);
- void yTransmitWrite(const QByteArray &ba );
- void yTransmitWrite( int c );
- void yTransmitFlush();
- unsigned short crc16_ccitt(const unsigned char *buf, unsigned int len);
- void configureForProtocolMode(bool enable);
- public:
- enum YMODEMResponce {
- MODEM_TIMEOUT = 1,
- MODEM_SUCCESS = 0,
- MODEM_PACKET_FAILED = -1,
- MODEM_NO_SYNC = -2,
- MODEM_REMOTE_CANCEL = -3,
- MODEM_EOT_ERROR = -4,
- MODEM_LOCAL_CANCEL = -5,
- MODEM_SIZE_ERROR = -6,
- MODEM_RESTART = -7,
- MODEM_FILE_NOT_FOUND_ERROR = -8,
- MODEM_FILE_NOT_OPEN_ERROR = -9
- };
- explicit YMODEM(QSerialPort *port, QObject *parent = nullptr);
- int yTransmit();
- int yTransmitClose();
- bool isConnected();
- void cancel();
- void start_ymodem(QString filePath){
- m_fp = filePath;
- LOG("YMODEM: Starting transfer for {}", m_fp.toStdString());
- yTransmit();
- }
- protected:
- // void run() override {
- // yTransmit();
- // }
- signals:
- void downloadStart(QString mtype);
- void packet(int,int);
- void progress(int val,int tval);
- void message(QString);
- void success();
- void modem_result(int r);
- };
- #endif // _YMODEM_H
|