-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket_client.h
More file actions
195 lines (162 loc) · 7.04 KB
/
socket_client.h
File metadata and controls
195 lines (162 loc) · 7.04 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/****************************************************************************
*
* Copyright (c) 2021 IMProject Development Team. All rights reserved.
* Authors: Igor Misic <igy1000mb@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name IMProject 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER OR 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 SOCKET_H_
#define SOCKET_H_
#include <QByteArray>
#include <QTcpSocket>
#include <QJsonObject>
#include <QJsonArray>
namespace socket {
namespace {
const QString kHeaderClientBoardInfo{"client_board_info"};
const QString kHeaderClientProductInfo{"client_product_info"};
const QString kHeaderServerProductInfo{"server_product_info"};
const QString kHeaderClientDownloadFile{"client_download_file"};
const QString kHeaderServerDownloadFile{"server_download_file"};
const QString kHeaderClientRequestData{"client_request_data"};
} // namespace
/*!
* \brief The SocketClient class, contains socket client information
*/
class SocketClient : public QTcpSocket {
Q_OBJECT
public:
/*!
* \brief SocketClient constructor
* \param servers_array - Json array with servers config
*/
explicit SocketClient(QJsonArray servers_array);
/*!
* \brief SocketClient destructor
*/
virtual ~SocketClient();
/*!
* \brief Sending information about board to the server
* \param board_info - Json object with board info for server
* \param bl_sw_info - Json object with bootloader software info about version, variant, etc
* \param fq_sw_info - Json object with firmware software info about version, variant, etc
* \return True if board info is successfully send, false otherwise
*/
virtual bool SendBoardInfo(QJsonObject board_info, QJsonObject bl_sw_info, QJsonObject fw_sw_info);
/*!
* \brief Receive product info for given board info from the server
* \param board_info - Json object with board info from server
* \param bl_sw_info - Json object with bootloader software info about version, variant, etc
* \param product_info - Json array with product info from server
* \param is_secure_communication - Flag indicating if the server uses secure communication
* \return
*/
virtual bool ReceiveProductInfo(QJsonObject board_info, QJsonObject bl_sw_info, QJsonArray& product_info, bool& is_secure_communication);
/*!
* \brief Download file from the server
* \param board_info - Json object with board info from server
* \param client_security_data - Json object to sending security data from the client
* \param file_version - File version to download
* \param server_security_data - Json object to getting security data from the server
* \param file_content - Reference to file_content to download
* \return
*/
virtual bool DownloadFile(const QJsonObject board_info, const QJsonObject client_security_data, const QString file_version, QJsonObject& server_security_data, QByteArray& file_content);
private:
/*!
* \brief Method use to perform connect action
* \return True if connect is performed successfully, false otherwise
*/
virtual bool Connect();
/*!
* \brief Method used to perform disconnect action
* \return True if disconnect is performed successfuly, false otherwise
*/
virtual bool Disconnect();
/*!
* \brief Method used to perform authentication
* \return True if authentication is done successfully, false otherwise
*/
virtual bool Authentication();
/*!
* \brief Method used to read all received data
* \param data_out - Byte array where received data will be stored
* \return True if all data is read successfully, false otherwise
*/
virtual bool ReadAll(QByteArray& data_out);
/*!
* \brief Method used to send QByteArray data
* \param in_data - Byte array input data
* \return True if data is successfully sent, false otherwise
*/
virtual bool SendDataWithAck(const QByteArray& in_data);
/*!
* \brief Method used to send QJsonObject
* \param json_objec - QJsonObject input data
* \return True if data is successfully sent, false otherwise
*/
virtual bool SendQJsonObject(const QJsonObject& json_objec);
/*!
* \brief Method used to request data from server
* \return True if data is successfully requested
*/
virtual bool RequestData();
/*!
* \brief Method used to check acknowledge
* \return True if ACK is received, false otherwise
*/
virtual bool CheckAck();
virtual bool WaitForReadyRead(int timeout);
virtual void ReadData(QByteArray& data_out);
public slots:
/*!
* \brief ReadyRead slot
*/
void ReadyRead();
signals:
/*!
* \brief Signal download progress
* \param bytes_received - Number of received bytes
* \param bytes_total - Total number of bytes
*/
void DownloadProgress(const qint64& bytes_received, const qint64& bytes_total);
private:
quint16 server_port_; //!< Server port
QString server_address_; //!< Server address
QByteArray preshared_key_; //!< Preshared key
QJsonArray servers_array_; //!< Server array
QByteArray socket_rx_data_; //!< Byte Array work as an Rx buffer
int previous_rx_data_size_{0}; //!< Previous Rx data size
int retry_number_{0}; //!< Data catch number retries
qint32 file_size_{0}; //!< File size
bool emit_progress{false}; //!< Flag for enabling/disabling emiting download prograss
const QByteArray kAck{"ACK"}; //!< Byte array constant that presents ACKNOWLEDGE
};
} // namespace socket
#endif // SOCKET_H_