AWS IoT Device SDK C++ v2  1.34.0
AWS IoT Device SDK C++ v2
MqttClient.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <aws/crt/Config.h>
7 #include <aws/crt/Exports.h>
10 #include <aws/iot/MqttCommon.h>
11 
12 #if !BYO_CRYPTO
13 
14 namespace Aws
15 {
16  namespace Iot
17  {
18  class MqttClient;
19 
26  {
27  public:
28  static MqttClientConnectionConfig CreateInvalid(int lastError) noexcept;
29 
39  const Crt::String &endpoint,
40  uint32_t port,
41  const Crt::Io::SocketOptions &socketOptions,
42  Crt::Io::TlsContext &&tlsContext);
43 
62  const Crt::String &endpoint,
63  uint32_t port,
64  const Crt::Io::SocketOptions &socketOptions,
65  Crt::Io::TlsContext &&tlsContext,
68 
72  explicit operator bool() const noexcept { return m_context ? true : false; }
73 
77  int LastError() const noexcept { return m_lastError; }
78 
79  private:
80  MqttClientConnectionConfig(int lastError) noexcept;
81 
83  const Crt::String &endpoint,
84  uint32_t port,
85  const Crt::Io::SocketOptions &socketOptions,
86  Crt::Io::TlsContext &&tlsContext,
88 
89  Crt::String m_endpoint;
90  uint32_t m_port;
91  Crt::Io::TlsContext m_context;
92  Crt::Io::SocketOptions m_socketOptions;
93  Crt::Mqtt::OnWebSocketHandshakeIntercept m_webSocketInterceptor;
94  Crt::String m_username;
95  Crt::String m_password;
97  int m_lastError;
98 
99  friend class MqttClient;
101  };
102 
109  {
110  public:
112 
122  const char *certPath,
123  const char *pkeyPath,
124  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
125 
135  const Crt::ByteCursor &cert,
136  const Crt::ByteCursor &pkey,
137  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
138 
148  const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
149  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
150 
161  const struct Pkcs12Options &options,
162  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
163 
175  const char *windowsCertStorePath,
176  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
177 
185  const WebsocketConfig &config,
186  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
187 
193  static MqttClientConnectionConfigBuilder NewDefaultBuilder() noexcept;
194 
202  MqttClientConnectionConfigBuilder &WithEndpoint(const Crt::String &endpoint);
203 
211  MqttClientConnectionConfigBuilder &WithEndpoint(Crt::String &&endpoint);
212 
222  MqttClientConnectionConfigBuilder &WithPortOverride(uint32_t port) noexcept;
223 
232  MqttClientConnectionConfigBuilder &WithCertificateAuthority(const char *caPath) noexcept;
233 
242  MqttClientConnectionConfigBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
243 
249  MqttClientConnectionConfigBuilder &WithTcpKeepAlive() noexcept;
250 
258  MqttClientConnectionConfigBuilder &WithTcpConnectTimeout(uint32_t connectTimeoutMs) noexcept;
259 
268  MqttClientConnectionConfigBuilder &WithTcpKeepAliveTimeout(uint16_t keepAliveTimeoutSecs) noexcept;
269 
279  MqttClientConnectionConfigBuilder &WithTcpKeepAliveInterval(uint16_t keepAliveIntervalSecs) noexcept;
280 
289  MqttClientConnectionConfigBuilder &WithTcpKeepAliveMaxProbes(uint16_t maxProbes) noexcept;
290 
298  MqttClientConnectionConfigBuilder &WithMinimumTlsVersion(aws_tls_versions minimumTlsVersion) noexcept;
299 
307  MqttClientConnectionConfigBuilder &WithHttpProxyOptions(
308  const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
309 
318  MqttClientConnectionConfigBuilder &WithMetricsCollection(bool enabled);
319 
327  MqttClientConnectionConfigBuilder &WithSdkName(const Crt::String &sdkName);
328 
336  MqttClientConnectionConfigBuilder &WithSdkVersion(const Crt::String &sdkVersion);
337 
357  MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
358  const Crt::String &username,
359  const Crt::String &authorizerName,
360  const Crt::String &authorizerSignature,
361  const Crt::String &password) noexcept;
362 
385  MqttClientConnectionConfigBuilder &WithCustomAuthorizer(
386  const Crt::String &username,
387  const Crt::String &authorizerName,
388  const Crt::String &authorizerSignature,
389  const Crt::String &password,
390  const Crt::String &tokenKeyName,
391  const Crt::String &tokenValue) noexcept;
392 
400  MqttClientConnectionConfigBuilder &WithUsername(const Crt::String &username) noexcept;
401 
409  MqttClientConnectionConfigBuilder &WithPassword(const Crt::String &password) noexcept;
410 
416  MqttClientConnectionConfig Build() noexcept;
417 
421  explicit operator bool() const noexcept { return m_lastError == 0; }
422 
426  int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
427 
428  private:
429  // Common setup shared by all valid constructors
431 
432  // Helper function to add parameters to the username in the WithCustomAuthorizer function
434  Crt::String currentUsername,
435  Crt::String parameterValue,
436  Crt::String parameterPreText);
437 
438  Crt::Allocator *m_allocator;
439  Crt::String m_endpoint;
440  uint32_t m_portOverride;
441  Crt::Io::SocketOptions m_socketOptions;
442  Crt::Io::TlsContextOptions m_contextOptions;
443  Crt::Optional<WebsocketConfig> m_websocketConfig;
445  bool m_enableMetricsCollection = true;
446  Crt::String m_sdkName = "CPPv2";
447  Crt::String m_sdkVersion;
448  Crt::String m_username = "";
449  Crt::String m_password = "";
450  bool m_isUsingCustomAuthorizer = false;
451 
452  int m_lastError;
453  };
454 
461  {
462  public:
463  MqttClient(Crt::Io::ClientBootstrap &bootstrap, Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
464 
471  MqttClient(Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
472 
478  std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnection(const MqttClientConnectionConfig &config) noexcept;
479 
483  int LastError() const noexcept { return m_client.LastError(); }
484 
488  explicit operator bool() const noexcept { return m_client ? true : false; }
489 
490  private:
491  Crt::Mqtt::MqttClient m_client;
492  int m_lastError;
493  };
494  } // namespace Iot
495 } // namespace Aws
496 
497 #endif // !BYO_CRYPTO
MqttCommon.h
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Iot::MqttClientConnectionConfigBuilder
Definition: MqttClient.h:109
Aws::Iot::MqttClient
Definition: MqttClient.h:461
Aws::Iot::WebsocketConfig
Definition: MqttCommon.h:24
Aws::Crt::Io::TlsContextOptions
Definition: TlsOptions.h:36
Aws::Iot::MqttClientConnectionConfig::LastError
int LastError() const noexcept
Definition: MqttClient.h:77
Aws::Iot::Pkcs12Options
Definition: MqttCommon.h:105
Aws::Crt::Optional
Definition: Optional.h:18
Aws::Crt::Io::TlsContext
Definition: TlsOptions.h:350
Aws::Crt::Io::TlsContextPkcs11Options
Definition: TlsOptions.h:217
Aws::Iot::MqttClientConnectionConfigBuilder::LastError
int LastError() const noexcept
Definition: MqttClient.h:426
Aws::Iot::MqttClientConnectionConfig
Definition: MqttClient.h:26
Aws::Iot::MqttClient::LastError
int LastError() const noexcept
Definition: MqttClient.h:483
Aws::Crt::Http::HttpClientConnectionProxyOptions
Definition: HttpConnection.h:270
Aws::Crt::Io::SocketOptions
Definition: SocketOptions.h:48
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws::Iot::AddToUsernameParameter
static Crt::String AddToUsernameParameter(Crt::String currentUsername, Crt::String parameterValue, Crt::String parameterPreText)
Definition: Mqtt5Client.cpp:21
Aws
Definition: Allocator.h:11
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:31
MqttConnection.h
Aws::Crt::Mqtt::OnWebSocketHandshakeIntercept
std::function< void(std::shared_ptr< Http::HttpRequest > req, const OnWebSocketHandshakeInterceptComplete &onComplete)> OnWebSocketHandshakeIntercept
Definition: MqttConnection.h:146
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Exports.h
Sigv4Signing.h
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:35
Aws::Crt::Mqtt::MqttClient
Definition: MqttClient.h:48
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45