aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Loading...
Searching...
No Matches
MqttClient.h
Go to the documentation of this file.
1#pragma once
6#include <aws/crt/Config.h>
7#include <aws/crt/Exports.h>
10#include <aws/iot/MqttCommon.h>
11
12#if !BYO_CRYPTO
13
14namespace Aws
15{
16 namespace Iot
17 {
18 class MqttClient;
19
26 {
27 public:
28 static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
29
40 const Crt::String &endpoint,
41 uint32_t port,
42 const Crt::Io::SocketOptions &socketOptions,
43 Crt::Io::TlsContext &&tlsContext,
44 bool enableMetrics = true);
45
65 const Crt::String &endpoint,
66 uint32_t port,
67 const Crt::Io::SocketOptions &socketOptions,
68 Crt::Io::TlsContext &&tlsContext,
71 bool enableMetrics = true);
72
76 explicit operator bool() const noexcept { return m_context ? true : false; }
77
81 int LastError() const noexcept { return m_lastError; }
82
83 private:
84 MqttClientConnectionConfig(int lastError) noexcept;
85
87 const Crt::String &endpoint,
88 uint32_t port,
89 const Crt::Io::SocketOptions &socketOptions,
90 Crt::Io::TlsContext &&tlsContext,
92 bool enableMetrics = true);
93
94 Crt::String m_endpoint;
95 uint32_t m_port;
96 Crt::Io::TlsContext m_context;
97 Crt::Io::SocketOptions m_socketOptions;
98 Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
99 Crt::String m_username;
100 Crt::String m_password;
102 bool m_enableMetricsCollection;
103 int m_lastError;
104
105 friend class MqttClient;
107 };
108
115 {
116 public:
118
128 const char *certPath,
129 const char *pkeyPath,
130 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
131
141 const Crt::ByteCursor &cert,
142 const Crt::ByteCursor &pkey,
143 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
144
154 const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
155 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
156
167 const struct Pkcs12Options &options,
168 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
169
181 const char *windowsCertStorePath,
182 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
183
191 const WebsocketConfig &config,
192 Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
193
199 static MqttClientConnectionConfigBuilder NewDefaultBuilder() noexcept;
200
208 MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
209
217 MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
218
228 MqttClientConnectionConfigBuilder &WithPortOverride(uint32_t port) noexcept;
229
238 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
239
248 MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
249
255 MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
256
264 MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
265
274 MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
275
285 MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
286
295 MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
296
304 MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
305
316 MqttClientConnectionConfigBuilder &WithTlsCipherPreference(aws_tls_cipher_pref cipherPref) noexcept;
317
325 MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
326 const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
327
336 MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
337
345 MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
346
354 MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
355
375 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
376 const Crt::String &username,
377 const Crt::String &authorizerName,
378 const Crt::String &authorizerSignature,
379 const Crt::String &password) noexcept;
380
403 MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
404 const Crt::String &username,
405 const Crt::String &authorizerName,
406 const Crt::String &authorizerSignature,
407 const Crt::String &password,
408 const Crt::String &tokenKeyName,
409 const Crt::String &tokenValue) noexcept;
410
418 MqttClientConnectionConfigBuilder &WithUsername(const Crt::String &username) noexcept;
419
427 MqttClientConnectionConfigBuilder &WithPassword(const Crt::String &password) noexcept;
428
434 MqttClientConnectionConfig Build() noexcept;
435
439 explicit operator bool() const noexcept { return m_lastError == 0; }
440
444 int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
445
446 private:
447 // Common setup shared by all valid constructors
449
450 // Helper function to add parameters to the username in the WithCustomAuthorizer function
451 Crt::String AddToUsernameParameter(
452 Crt::String currentUsername,
453 Crt::String parameterValue,
454 Crt::String parameterPreText);
455
456 Crt::Allocator *m_allocator;
457 Crt::String m_endpoint;
458 uint32_t m_portOverride;
459 Crt::Io::SocketOptions m_socketOptions;
460 Crt::Io::TlsContextOptions m_contextOptions;
461 Crt::Optional<WebsocketConfig> m_websocketConfig;
463 bool m_enableMetricsCollection = true;
464 Crt::String m_sdkName = "IoTDeviceSDK/CPP";
465 Crt::String m_sdkVersion;
466 Crt::String m_username = "";
467 Crt::String m_password = "";
468 bool m_isUsingCustomAuthorizer = false;
469
470 int m_lastError;
471 };
472
479 {
480 public:
481 MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
482
489 MqttClient(Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
490
496 std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
497
501 int LastError() const noexcept { return m_client.LastError(); }
502
506 explicit operator bool() const noexcept { return m_client ? true : false; }
507
508 private:
509 Crt::Mqtt::MqttClient m_client;
510 int m_lastError;
511 };
512 } // namespace Iot
513} // namespace Aws
514
515#endif // !BYO_CRYPTO
#define AWS_CRT_CPP_API
Definition Exports.h:36
Definition Bootstrap.h:35
Definition SocketOptions.h:48
Definition TlsOptions.h:350
Definition TlsOptions.h:36
Definition TlsOptions.h:217
Definition MqttClient.h:48
Definition Optional.h:19
int LastError() const noexcept
Definition MqttClient.h:444
Definition MqttClient.h:26
int LastError() const noexcept
Definition MqttClient.h:81
Definition MqttClient.h:479
int LastError() const noexcept
Definition MqttClient.h:501
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition MqttConnection.h:146
aws_byte_cursor ByteCursor
Definition Types.h:31
aws_allocator Allocator
Definition Allocator.h:14
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition Types.h:45
Definition Allocator.h:11
Definition MqttCommon.h:105
Definition MqttCommon.h:24