/* * 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 #include #include #include #include #include #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' kStartCChars = 2, //必须"连续"收到几个 'C' 才认为接收端真的在等数据 kDrainQuietMs = 200 //清空串口残留时要求静默的时间(ms) }; bool m_cancel; //【防重入】升级指令可能被重复下发(主板重复上报 "system will update"/服务器补发), // 两个 yTransmit 同时跑会互相 clear 串口、抢 ACK,主板收到的包头会错位 -> 升级失败复位 std::atomic m_running{false}; QSerialPort *m_serialPort; QByteArray dataBuf; //协议阶段收到的非协议字节(主板调试文本),仅用于日志排查 QString m_fp; double percent; int yTransmitRead(unsigned short timeout); int yTransmitReadProtocol(unsigned int timeoutMs); //只返回协议字节,自动跳过主板调试文本 void drainInput(int quietMs = kDrainQuietMs); //把串口里已堆积的字节全部读掉丢弃 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