ymodem.h 4.4 KB

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