ymodem.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <mserial.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 QThread
  44. {
  45. Q_OBJECT
  46. private:
  47. bool m_cancel;
  48. QSerialPort *m_serialPort;
  49. QString m_fp;
  50. double percent;
  51. int yTransmitRead(unsigned short timeout);
  52. void yTransmitWrite(const QByteArray &ba );
  53. void yTransmitWrite( int c );
  54. void yTransmitFlush();
  55. unsigned short crc16_ccitt(const unsigned char *buf, unsigned int len);
  56. int yTransmitPacket(const QFileInfo &fileInfo, QIODevice &dev, const quint32 &payloadSize, quint8 sequenceNum = 0,bool firstPacket = true);
  57. public:
  58. enum YMODEMResponce {
  59. MODEM_TIMEOUT = 1,
  60. MODEM_SUCCESS = 0,
  61. MODEM_PACKET_FAILED = -1,
  62. MODEM_NO_SYNC = -2,
  63. MODEM_REMOTE_CANCEL = -3,
  64. MODEM_EOT_ERROR = -4,
  65. MODEM_LOCAL_CANCEL = -5,
  66. MODEM_SIZE_ERROR = -6,
  67. MODEM_RESTART = -7,
  68. MODEM_FILE_NOT_FOUND_ERROR = -8,
  69. MODEM_FILE_NOT_OPEN_ERROR = -9
  70. };
  71. explicit YMODEM(SerialThread *sp, QObject *parent = 0);
  72. int yTransmit();
  73. int yTransmitClose();
  74. bool isConnected();
  75. void cancel();
  76. void start_ymodem(QString filePath){
  77. m_fp = filePath;
  78. start();
  79. }
  80. protected:
  81. void run() override {
  82. yTransmit();
  83. wait();
  84. }
  85. signals:
  86. void downloadStart(QString mtype);
  87. void packet(int,int);
  88. void progress(int val,int tval);
  89. void message(QString);
  90. void success();
  91. void modem_result(int r);
  92. };
  93. #endif // _YMODEM_H