-
Notifications
You must be signed in to change notification settings - Fork 6
/
filetransfer.h
145 lines (123 loc) · 3.75 KB
/
filetransfer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* \file
* <!--
* Copyright 2015 Develer S.r.l. (http://www.develer.com/)
* -->
*
* \brief FileTransfer class header
*
* \author Aurelien Rainone <aurelien@develer.com>
*/
#ifndef FILETRANSFER_H
#define FILETRANSFER_H
#include <QObject>
class QSerialPort;
/**
* \brief serial port file transfer abstract class
*
* child classes implementing a specific file transfer protocol must
* implement this pure virtual method :
*
* - TransferError performTransfer() : this is where the actual file
* transfer must take place. At this point 'buffer' and
* 'total_size' have been set
*
* Furthermore, child class implementation must emit transferProgressed()
* signal frequently enough, so that the application can inform the user
* about the current state of the transfer. the integer parameter of
* transferProgressed() should represents the percentage of current
* transfer that has already been performed
*
* \see FileTransfer::quit_requested
*/
class FileTransfer : public QObject
{
Q_OBJECT
public:
/**
* \brief The TransferError enum
*/
enum TransferError
{
/// file successfully transferred
NoError = 0,
/// no synchronization between local and remote
NoSyncError = 1,
/// transmission error
TransmissionError = 2,
/// transfer timed out
TimeoutError = 3,
/// transfer cancelled by remote
RemoteCancelledError = 4,
/// transfer cancelled locally
LocalCancelledError = 5,
/// local file error (size, permission)
InputFileError = 6,
/// unknown error
UnknownError = 7
};
/**
* \brief quit_requested flag indicating, if true, that the file transfer
* should quit as soon as possible
* \warning this flag is read from the worker thread
*/
volatile bool quit_requested;
protected:
/// file to transfer
QString filename;
/// serial port instance used for the transfer
QSerialPort *serial;
/// full content of file to transfer
QByteArray buffer;
/// total size in bytes of buffer
qint64 total_size;
/// thread in which the transfer is performed
QThread *thread;
public:
/**
* \brief start the file transfer
* \note this method returns immediately, the actual file transfer
* is performed in another thread
* \see performTransfer
* \return boolean indicating wether transfer has started or not
*/
bool startTransfer();
/**
* \brief return a string corresponding to given TransferError
* \param error error code
* \return error error string
*/
static QString errorString(TransferError error);
protected:
/**
* \brief FileTransfer constructor
* \param parent object taking ownership
* \param serial opened instance of QSerialPort
* \param filename file to transfer
*/
FileTransfer(QObject *parent, QSerialPort *serial, const QString &filename);
private:
/**
* \brief perform the actual file transfer
* \return transfer end code
*/
virtual void performTransfer() = 0;
/**
* \brief handle transferEnded signal
* \param error transfer end error code
*/
void handleTransferEnded(TransferError error);
signals:
/**
* \brief signal emitted when file transfer has ended
* \param error transfer end error code
*/
void transferEnded(TransferError error);
/**
* \brief signal emitted each time the file transfer progresses
* (minimum amount to emit is 1% progress)
* \percent percentage of file transfered
*/
void transferProgressed(int percent);
};
#endif // FILETRANSFER_H