ymodem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <atomic>
  34. #include <m_loger.h>
  35. #define SOH QChar(0x01)
  36. #define STX QChar(0x02)
  37. #define EOT 0x04
  38. #define ACK 0x06
  39. #define NAK 0x15
  40. #define CAN 0x18
  41. #define CTRLZ QChar(0x00)
  42. #define DLY_1S 1000
  43. #define MAXRETRANS 25
  44. class YMODEM : public QObject
  45. {
  46. Q_OBJECT
  47. private:
  48. //协议等待超时(毫秒)
  49. //主板 bootloader 在等头包时是"每 3s 发一次 'C'",所以任何等待都必须放宽到 3s 以上,
  50. //原实现的 (DLY_1S<<1)=2000ms 比该周期还短,必然超时。
  51. //用 enum 而不是 static constexpr(C++11 下 constexpr 成员被 odr-use 时要额外定义,会链不上)
  52. enum {
  53. kStartTimeoutMs = 30000, //等接收端首个 'C'
  54. kHeaderAckTimeoutMs = 8000, //头包 ACK(接收端可能先擦除 Flash)
  55. kAckTimeoutMs = 5000, //数据块 ACK
  56. kEotAckTimeoutMs = 5000, //EOT / 结束空块 ACK
  57. kClosingWaitMs = 4000, //等结束空块的 'C'
  58. kStartCChars = 2, //必须"连续"收到几个 'C' 才认为接收端真的在等数据
  59. kDrainQuietMs = 200 //清空串口残留时要求静默的时间(ms)
  60. };
  61. bool m_cancel;
  62. //【防重入】升级指令可能被重复下发(主板重复上报 "system will update"/服务器补发),
  63. // 两个 yTransmit 同时跑会互相 clear 串口、抢 ACK,主板收到的包头会错位 -> 升级失败复位
  64. std::atomic<bool> m_running{false};
  65. QSerialPort *m_serialPort;
  66. QByteArray dataBuf; //协议阶段收到的非协议字节(主板调试文本),仅用于日志排查
  67. QString m_fp;
  68. double percent;
  69. int yTransmitRead(unsigned short timeout);
  70. int yTransmitReadProtocol(unsigned int timeoutMs); //只返回协议字节,自动跳过主板调试文本
  71. void drainInput(int quietMs = kDrainQuietMs); //把串口里已堆积的字节全部读掉丢弃
  72. bool waitForStart(unsigned int timeoutMs); //等接收端"连续"发来 'C'
  73. void sendCancel();
  74. int yTransmitPacket(const QFileInfo &fileInfo, QIODevice &dev, const quint32 &payloadSize, quint8 sequenceNum = 0, bool firstPacket = true);
  75. void yTransmitWrite(const QByteArray &ba );
  76. void yTransmitWrite( int c );
  77. void yTransmitFlush();
  78. unsigned short crc16_ccitt(const unsigned char *buf, unsigned int len);
  79. void configureForProtocolMode(bool enable);
  80. public:
  81. enum YMODEMResponce {
  82. MODEM_TIMEOUT = 1,
  83. MODEM_SUCCESS = 0,
  84. MODEM_PACKET_FAILED = -1,
  85. MODEM_NO_SYNC = -2,
  86. MODEM_REMOTE_CANCEL = -3,
  87. MODEM_EOT_ERROR = -4,
  88. MODEM_LOCAL_CANCEL = -5,
  89. MODEM_SIZE_ERROR = -6,
  90. MODEM_RESTART = -7,
  91. MODEM_FILE_NOT_FOUND_ERROR = -8,
  92. MODEM_FILE_NOT_OPEN_ERROR = -9
  93. };
  94. explicit YMODEM(QSerialPort *port, QObject *parent = nullptr);
  95. int yTransmit();
  96. int yTransmitClose();
  97. bool isConnected();
  98. void cancel();
  99. void start_ymodem(QString filePath){
  100. m_fp = filePath;
  101. LOG("YMODEM: Starting transfer for {}", m_fp.toStdString());
  102. yTransmit();
  103. }
  104. protected:
  105. // void run() override {
  106. // yTransmit();
  107. // }
  108. signals:
  109. void downloadStart(QString mtype);
  110. void packet(int,int);
  111. void progress(int val,int tval);
  112. void message(QString);
  113. void success();
  114. void modem_result(int r);
  115. };
  116. #endif // _YMODEM_H