forked from aws/aws-iot-device-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect.hpp
426 lines (393 loc) · 21.1 KB
/
Connect.hpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/**
* @file Connect.hpp
* @brief
*
*/
#pragma once
#include "mqtt/ClientState.hpp"
#include "mqtt/Packet.hpp"
namespace awsiotsdk {
namespace mqtt {
/**
* @brief Define strongly typed enum for Connack Return Codes
*/
enum class ConnackReturnCode {
CONNECTION_ACCEPTED = 0,
UNACCEPTABLE_PROTOCOL_VERSION_ERROR = 1,
IDENTIFIER_REJECTED_ERROR = 2,
SERVER_UNAVAILABLE_ERROR = 3,
BAD_USERDATA_ERROR = 4,
NOT_AUTHORIZED_ERROR = 5
};
/**
* @brief Connect Packet Type
*
* Defines a type for MQTT Connect message
*/
class ConnectPacket : public Packet {
protected:
bool
is_clean_session_; ///< MQTT clean session. True = this session is to be treated as clean. Previous server state is cleared and no information is retained from any previous connection
unsigned char connect_flags_; ///< MQTT Connect flags byte
std::chrono::seconds keep_alive_timeout_; ///< MQTT Keepalive timeout in seconds
mqtt::Version mqtt_version_; ///< Desired MQTT version used during connection
std::unique_ptr<Utf8String> p_protocol_id_; ///< The protocol ID for this connection
std::unique_ptr<Utf8String> p_client_id_; ///< Pointer to a string defining the MQTT client ID (this needs to be unique \b per \b device across your AWS account)
std::unique_ptr<WillOptions> p_will_msg_; ///< MQTT LWT parameters
// Currently not supported by AWS IoT. Kept for future use
std::unique_ptr<Utf8String> p_username_; ///< MQTT Username
//std::unique_ptr<Utf8String> p_password_; ///< MQTT Password
public:
// Ensure Default and Copy Constructors and Copy assignment operator are deleted
// Use default move constructors and assignment operators
// Default virtual destructor
// Delete Default constructor
ConnectPacket() = delete;
// Delete Copy constructor
ConnectPacket(const ConnectPacket &) = delete;
// Default Move constructor
ConnectPacket(ConnectPacket &&) = default;
// Delete Copy assignment operator
ConnectPacket &operator=(const ConnectPacket &) & = delete;
// Default Move assignment operator
ConnectPacket &operator=(ConnectPacket &&) & = default;
// Default destructor
virtual ~ConnectPacket() = default;
/**
* @brief Constructor
*
* @warning This constructor can throw exceptions, it is recommended to use Factory create method
* Constructor is kept public to not restrict usage possibilities (eg. make_shared)
*
* @param is_clean_session - Is this a clean session? Currently we do not support setting this to false
* @param mqtt_version - Which version of the MQTT protocol to use. Currently the only allowed value is 3.1.1
* @param p_client_id - Client ID to use to make the connection
* @param p_username - Username, currently unused in AWS IoT and will be ignored
* @param p_password - Password, currently unused in AWS IoT and will be ignored
* @param p_will_msg - MQTT Last Will and Testament message
* @param is_metrics_enabled - enable SDK metrics in username string
*/
ConnectPacket(bool is_clean_session,
mqtt::Version mqtt_version,
std::chrono::seconds keep_alive_timeout,
std::unique_ptr<Utf8String> p_client_id,
std::unique_ptr<Utf8String> p_username,
std::unique_ptr<Utf8String> p_password,
std::unique_ptr<mqtt::WillOptions> p_will_msg,
bool is_metrics_enabled);
/**
* @brief Constructor
*
* @warning This constructor can throw exceptions, it is recommended to use Factory create method
* Constructor is kept public to not restrict usage possibilities (eg. make_shared). SDK metrics are
* enabled by default
*
* @param is_clean_session - Is this a clean session? Currently we do not support setting this to false
* @param mqtt_version - Which version of the MQTT protocol to use. Currently the only allowed value is 3.1.1
* @param p_client_id - Client ID to use to make the connection
* @param p_username - Username, currently unused in AWS IoT and will be ignored
* @param p_password - Password, currently unused in AWS IoT and will be ignored
* @param p_will_msg - MQTT Last Will and Testament message
*/
ConnectPacket(bool is_clean_session,
mqtt::Version mqtt_version,
std::chrono::seconds keep_alive_timeout,
std::unique_ptr<Utf8String> p_client_id,
std::unique_ptr<Utf8String> p_username,
std::unique_ptr<Utf8String> p_password,
std::unique_ptr<mqtt::WillOptions> p_will_msg);
/**
* @brief Create Factory method
*
* @param is_clean_session - Is this a clean session? Currently we do not support setting this to false
* @param mqtt_version - Which version of the MQTT protocol to use. Currently the only allowed value is 3.1.1
* @param p_client_id - Client ID to use to make the connection
* @param p_username - Username, currently unused in AWS IoT
* @param p_password - Password, currently unused in AWS IoT
* @param p_will_msg - MQTT Last Will and Testament message
* @param is_metrics_enabled - enable SDK metrics in username string
* @return nullptr on error, shared_ptr pointing to a created ConnectPacket instance if successful
*/
static std::shared_ptr<ConnectPacket> Create(bool is_clean_session,
mqtt::Version mqtt_version,
std::chrono::seconds keep_alive_timeout,
std::unique_ptr<Utf8String> p_client_id,
std::unique_ptr<Utf8String> p_username,
std::unique_ptr<Utf8String> p_password,
std::unique_ptr<mqtt::WillOptions> p_will_msg,
bool is_metrics_enabled);
/**
* @brief Create Factory method, SDK metrics enabled by default
*
* @param is_clean_session - Is this a clean session? Currently we do not support setting this to false
* @param mqtt_version - Which version of the MQTT protocol to use. Currently the only allowed value is 3.1.1
* @param p_client_id - Client ID to use to make the connection
* @param p_username - Username, currently unused in AWS IoT
* @param p_password - Password, currently unused in AWS IoT
* @param p_will_msg - MQTT Last Will and Testament message
* @return nullptr on error, shared_ptr pointing to a created ConnectPacket instance if successful
*/
static std::shared_ptr<ConnectPacket> Create(bool is_clean_session,
mqtt::Version mqtt_version,
std::chrono::seconds keep_alive_timeout,
std::unique_ptr<Utf8String> p_client_id,
std::unique_ptr<Utf8String> p_username,
std::unique_ptr<Utf8String> p_password,
std::unique_ptr<mqtt::WillOptions> p_will_msg);
/**
* @brief Serialize this packet into a String
* @return String containing serialized packet
*/
util::String ToString();
/**
* @brief Get duration of Keep alive interval in seconds
* @return std::chrono::seconds Keep alive interval duration
*/
std::chrono::seconds GetKeepAliveTimeout() { return keep_alive_timeout_; }
/**
* @brief get the client ID from the connect packet
* @return String containing the client ID
*/
util::String GetClientID() { return p_client_id_ ? p_client_id_->ToStdString() : ""; }
};
/**
* @brief Disconnect Packet Type
*
* Defines a type for MQTT Disconnect message
*/
class DisconnectPacket : public Packet {
public:
// Ensure Default move and copy constructors and assignment operators are created
// Default virtual destructor
DisconnectPacket(const DisconnectPacket &) = default;
// Default Move constructor
DisconnectPacket(DisconnectPacket &&) = default;
// Default Copy assignment operator
DisconnectPacket &operator=(const DisconnectPacket &) & = default;
// Default Move assignment operator
DisconnectPacket &operator=(DisconnectPacket &&) & = default;
// Default destructor
virtual ~DisconnectPacket() = default;
/**
* @brief Constructor
*/
DisconnectPacket();
/**
* @brief Create Factory method
*
* @return nullptr on error, shared_ptr pointing to a created DisconnectPacket instance if successful
*/
static std::shared_ptr<DisconnectPacket> Create();
/**
* @brief Serialize this packet into a String
* @return String containing serialized packet
*/
util::String ToString();
};
class PingreqPacket : public Packet {
public:
// Ensure Default move and copy constructors and assignment operators are created
// Default virtual destructor
// Default Copy constructor
PingreqPacket(const PingreqPacket &) = default;
// Default Move constructor
PingreqPacket(PingreqPacket &&) = default;
// Default Copy assignment operator
PingreqPacket &operator=(const PingreqPacket &) & = default;
// Default Move assignment operator
PingreqPacket &operator=(PingreqPacket &&) & = default;
// Default destructor
virtual ~PingreqPacket() = default;
/**
* @brief Constructor
*/
PingreqPacket();
/**
* @brief Create Factory method
*
* @return nullptr on error, shared_ptr pointing to a created PingreqPacket instance if successful
*/
static std::shared_ptr<PingreqPacket> Create();
/**
* @brief Serialize this packet into a String
* @return String containing serialized packet
*/
util::String ToString();
};
/**
* @brief Define a class for ConnectActionAsync
*
* This class defines an Asynchronous action for performing a MQTT Connect operation
*/
class ConnectActionAsync : public Action {
protected:
std::shared_ptr<ClientState> p_client_state_; ///< Shared Client State instance
public:
// Disabling default, move and copy constructors to match Action parent
// Default virtual destructor
ConnectActionAsync() = delete;
// Default Copy constructor
ConnectActionAsync(const ConnectActionAsync &) = delete;
// Default Move constructor
ConnectActionAsync(ConnectActionAsync &&) = delete;
// Default Copy assignment operator
ConnectActionAsync &operator=(const ConnectActionAsync &) & = delete;
// Default Move assignment operator
ConnectActionAsync &operator=(ConnectActionAsync &&) & = delete;
// Default destructor
virtual ~ConnectActionAsync() = default;
/**
* @brief Constructor
*
* @warning This constructor can throw exceptions, it is recommended to use Factory create method
* Constructor is kept public to not restrict usage possibilities (eg. make_shared)
*
* @param p_client_state - Shared Client State instance
*/
ConnectActionAsync(std::shared_ptr<ClientState> p_client_state);
/**
* @brief Factory Create method
*
* @param p_client_state - Shared Client State instance
* @return nullptr on error, unique_ptr pointing to a created ConnectActionAsync instance if successful
*/
static std::unique_ptr<Action> Create(std::shared_ptr<ActionState> p_action_state);
/**
* @brief Perform MQTT Connect Action in Async mode
*
* Performs the MQTT Connect Operation in Async mode. This also calls Connect on the Network Connection
* provided with the Perform Action call. If the Network connect call fails, the action will return with
* the ResponseCode returned by the Network Connect call. This does not wait for CONNACK to be received.
* CONNACK is handled separately in the HandleConnack function of NetworkReadAction. If the MQTT connection
* is already active, will not attempt another Connect and return with appropriate ResponseCode.
*
* @param p_network_connection - Network connection instance to use for performing this action
* @param p_action_data - Action data specific to this execution of the Action
* @return - ResponseCode indicating status of the operation
*/
ResponseCode PerformAction(std::shared_ptr<NetworkConnection> p_network_connection,
std::shared_ptr<ActionData> p_action_data);
};
/**
* @brief Define a class for DisconnectActionAsync
*
* This class defines an Asynchronous action for performing a MQTT Puback operation
*/
class DisconnectActionAsync : public Action {
protected:
std::shared_ptr<ClientState> p_client_state_; ///< Shared Client State instance
public:
// Disabling default, move and copy constructors to match Action parent
// Default virtual destructor
DisconnectActionAsync() = delete;
// Default Copy constructor
DisconnectActionAsync(const DisconnectActionAsync &) = delete;
// Default Move constructor
DisconnectActionAsync(DisconnectActionAsync &&) = delete;
// Default Copy assignment operator
DisconnectActionAsync &operator=(const DisconnectActionAsync &) & = delete;
// Default Move assignment operator
DisconnectActionAsync &operator=(DisconnectActionAsync &&) & = delete;
// Default destructor
virtual ~DisconnectActionAsync() = default;
/**
* @brief Constructor
*
* @warning This constructor can throw exceptions, it is recommended to use Factory create method
* Constructor is kept public to not restrict usage possibilities (eg. make_shared)
*
* @param p_client_state - Shared Client State instance
*/
DisconnectActionAsync(std::shared_ptr<ClientState> p_client_state);
/**
* @brief Factory Create method
*
* @param p_client_state - Shared Client State instance
* @return nullptr on error, unique_ptr pointing to a created DisconnectActionAsync instance if successful
*/
static std::unique_ptr<Action> Create(std::shared_ptr<ActionState> p_action_state);
/**
* @brief Perform MQTT Disconnect Action in Async mode
*
* Performs the MQTT Disconnect Operation in Async mode. Also calls disconnect API of the provided
* network connection. If the client is already in disconnected state, will not attempt disconnect and
* return with appropriate ResponseCode.
*
* @param p_network_connection - Network connection instance to use for performing this action
* @param p_action_data - Action data specific to this execution of the Action
* @return - ResponseCode indicating status of the operation
*/
ResponseCode PerformAction(std::shared_ptr<NetworkConnection> p_network_connection,
std::shared_ptr<ActionData> p_action_data);
};
/**
* @brief Define a class for KeepaliveActionRunner
*
* This class defines an action for performing a MQTT Keep Alive operation. This is meant to be run in a separate
* thread using ClientCore and will not do anything if called for one single execution using Perform Action.
*/
class KeepaliveActionRunner : public Action {
protected:
std::shared_ptr<ClientState> p_client_state_; ///< Shared Client State instance
public:
// Disabling default, move and copy constructors to match Action parent
// Default virtual destructor
KeepaliveActionRunner() = delete;
// Default Copy constructor
KeepaliveActionRunner(const KeepaliveActionRunner &) = delete;
// Default Move constructor
KeepaliveActionRunner(KeepaliveActionRunner &&) = delete;
// Default Copy assignment operator
KeepaliveActionRunner &operator=(const KeepaliveActionRunner &) & = delete;
// Default Move assignment operator
KeepaliveActionRunner &operator=(KeepaliveActionRunner &&) & = delete;
// Default destructor
virtual ~KeepaliveActionRunner() = default;
/**
* @brief Constructor
*
* @warning This constructor can throw exceptions, it is recommended to use Factory create method
* Constructor is kept public to not restrict usage possibilities (eg. make_shared)
*
* @param p_client_state - Shared Client State instance
*/
KeepaliveActionRunner(std::shared_ptr<ClientState> p_client_state);
/**
* @brief Factory Create method
*
* @param p_client_state - Shared Client State instance
* @return nullptr on error, unique_ptr pointing to a created KeepaliveActionRunner instance if successful
*/
static std::unique_ptr<Action> Create(std::shared_ptr<ActionState> p_action_state);
/**
* @brief Perform MQTT Keep Alive Action. Expects to run in a separate thread using ClientCore
*
* Performs the MQTT Keep Alive operation. Will send out Ping requests at Half the specified Keepalive
* interval and expect a response to be received before that same period passes again. If a response is not
* received during that time, assumes connection has been lost and initiates and performs a reconnect. Also
* resubscribes to any existing subscribed topics. Uses exponential backoff using minimum and maximum values
* defined in Client state.
*
* @param p_network_connection - Network connection instance to use for performing this action
* @param p_action_data - Action data specific to this execution of the Action
* @return - ResponseCode indicating status of the operation
*/
ResponseCode PerformAction(std::shared_ptr<NetworkConnection> p_network_connection,
std::shared_ptr<ActionData> p_action_data);
};
}
}