AWS IoT Device SDK C++ v2  1.39.0
AWS IoT Device SDK C++ v2
Mqtt5Client.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  using namespace Crt::Mqtt5;
17 
18  namespace Io
19  {
20  class ClientBootstrap;
21  class SocketOptions;
22  class TlsContextOptions;
23  class WebsocketConfig;
24  } // namespace Io
25 
26  namespace Iot
27  {
28 
33  {
34  public:
39  virtual ~Mqtt5CustomAuthConfig();
40 
43 
44  Mqtt5CustomAuthConfig &operator=(const Mqtt5CustomAuthConfig &rhs);
46 
47  Mqtt5CustomAuthConfig &WithAuthorizerName(Crt::String authName);
48  Mqtt5CustomAuthConfig &WithUsername(Crt::String username);
49  Mqtt5CustomAuthConfig &WithPassword(Crt::ByteCursor password);
50  Mqtt5CustomAuthConfig &WithTokenKeyName(Crt::String tokenKeyName);
51  Mqtt5CustomAuthConfig &WithTokenValue(Crt::String tokenValue);
52  Mqtt5CustomAuthConfig &WithTokenSignature(Crt::String tokenSignature);
53 
54  const Crt::Optional<Crt::String> &GetAuthorizerName();
55  const Crt::Optional<Crt::String> &GetUsername();
56  const Crt::Optional<Crt::ByteCursor> &GetPassword();
57  const Crt::Optional<Crt::String> &GetTokenKeyName();
58  const Crt::Optional<Crt::String> &GetTokenValue();
59  const Crt::Optional<Crt::String> &GetTokenSignature();
60 
61  private:
68  Crt::Optional<Crt::String> m_authorizerName;
69 
82  Crt::Optional<Crt::String> m_username;
83 
89 
96  Crt::Optional<Crt::String> m_tokenKeyName;
97 
104  Crt::Optional<Crt::String> m_tokenValue;
105 
112  Crt::Optional<Crt::String> m_tokenSignature;
113 
114  Crt::ByteBuf m_passwordStorage;
115  Crt::Allocator *m_allocator;
116  };
117 
123  {
124  public:
136  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsFromPath(
137  const Crt::String &hostName,
138  const char *certPath,
139  const char *pkeyPath,
140  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
141 
153  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsFromMemory(
154  const Crt::String &hostName,
155  const Crt::ByteCursor &cert,
156  const Crt::ByteCursor &pkey,
157  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
158 
170  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsPkcs11(
171  const Crt::String &hostName,
172  const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
173  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
174 
186  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithMtlsPkcs12(
187  const Crt::String &hostName,
188  const struct Pkcs12Options &options,
189  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
190 
204  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithWindowsCertStorePath(
205  const Crt::String &hostName,
206  const char *windowsCertStorePath,
207  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
208 
218  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithWebsocket(
219  const Crt::String &hostName,
220  const WebsocketConfig &config,
221  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
222 
232  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithCustomAuthorizer(
233  const Crt::String &hostName,
234  const Mqtt5CustomAuthConfig &customAuthConfig,
235  Crt::Allocator *allocator) noexcept;
236 
247  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilderWithCustomAuthorizerWebsocket(
248  const Crt::String &hostName,
249  const Mqtt5CustomAuthConfig &customAuthConfig,
250  const WebsocketConfig &config,
251  Crt::Allocator *allocator) noexcept;
252 
260  Mqtt5ClientBuilder &WithHostName(Crt::String hostname);
261 
269  Mqtt5ClientBuilder &WithPort(uint32_t port) noexcept;
270 
279  Mqtt5ClientBuilder &WithBootstrap(Crt::Io::ClientBootstrap *bootStrap) noexcept;
280 
289  Mqtt5ClientBuilder &WithCertificateAuthority(const char *caPath) noexcept;
290 
299  Mqtt5ClientBuilder &WithCertificateAuthority(const Crt::ByteCursor &cert) noexcept;
300 
311  Mqtt5ClientBuilder &WithTlsCipherPreference(aws_tls_cipher_pref cipherPref) noexcept;
312 
320  Mqtt5ClientBuilder &WithSocketOptions(Crt::Io::SocketOptions socketOptions) noexcept;
321 
329  Mqtt5ClientBuilder &WithHttpProxyOptions(
330  const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
331 
337  Mqtt5ClientBuilder &WithCustomAuthorizer(const Iot::Mqtt5CustomAuthConfig &config) noexcept;
338 
346  Mqtt5ClientBuilder &WithConnectOptions(std::shared_ptr<ConnectPacket> packetConnect) noexcept;
347 
355  Mqtt5ClientBuilder &WithSessionBehavior(ClientSessionBehaviorType sessionBehavior) noexcept;
356 
366  Mqtt5ClientBuilder &WithClientExtendedValidationAndFlowControl(
367  ClientExtendedValidationAndFlowControl clientExtendedValidationAndFlowControl) noexcept;
368 
379  Mqtt5ClientBuilder &WithOfflineQueueBehavior(
380  ClientOperationQueueBehaviorType offlineQueueBehavior) noexcept;
381 
390  Mqtt5ClientBuilder &WithReconnectOptions(ReconnectOptions reconnectOptions) noexcept;
391 
398  Mqtt5ClientBuilder &WithTopicAliasingOptions(TopicAliasingOptions topicAliasingOptions) noexcept;
399 
410  uint64_t minConnectedTimeToResetReconnectDelayMs) noexcept;
411 
420  Mqtt5ClientBuilder &WithPingTimeoutMs(uint32_t pingTimeoutMs) noexcept;
421 
430  Mqtt5ClientBuilder &WithConnackTimeoutMs(uint32_t connackTimeoutMs) noexcept;
431 
440  Mqtt5ClientBuilder &WithAckTimeoutSec(uint32_t ackTimeoutSec) noexcept;
441 
452  Mqtt5ClientBuilder &WithAckTimeoutSeconds(uint32_t ackTimeoutSec) noexcept;
453 
461  Mqtt5ClientBuilder &WithSdkName(const Crt::String &sdkName);
462 
470  Mqtt5ClientBuilder &WithSdkVersion(const Crt::String &sdkVersion);
471 
477  std::shared_ptr<Mqtt5Client> Build() noexcept;
478 
482  explicit operator bool() const noexcept { return m_lastError == 0; }
483 
487  int LastError() const noexcept { return m_lastError ? m_lastError : AWS_ERROR_UNKNOWN; }
488 
490  {
491  if (m_options)
492  {
493  delete m_options;
494  }
495  };
500 
508  Mqtt5ClientBuilder &WithClientConnectionSuccessCallback(OnConnectionSuccessHandler callback) noexcept;
509 
517  Mqtt5ClientBuilder &WithClientConnectionFailureCallback(OnConnectionFailureHandler callback) noexcept;
518 
526  Mqtt5ClientBuilder &WithClientDisconnectionCallback(OnDisconnectionHandler callback) noexcept;
527 
535  Mqtt5ClientBuilder &WithClientStoppedCallback(OnStoppedHandler callback) noexcept;
536 
544  Mqtt5ClientBuilder &WithClientAttemptingConnectCallback(OnAttemptingConnectHandler callback) noexcept;
545 
553  Mqtt5ClientBuilder &WithPublishReceivedCallback(OnPublishReceivedHandler callback) noexcept;
554 
558  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsFromPath(
559  const Crt::String hostName,
560  const char *certPath,
561  const char *pkeyPath,
562  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
563 
567  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsFromMemory(
568  const Crt::String hostName,
569  const Crt::ByteCursor &cert,
570  const Crt::ByteCursor &pkey,
571  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
572 
576  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsPkcs11(
577  const Crt::String hostName,
578  const Crt::Io::TlsContextPkcs11Options &pkcs11Options,
579  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
580 
584  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithMtlsPkcs12(
585  const Crt::String hostName,
586  const struct Pkcs12Options &options,
587  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
588 
592  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithWindowsCertStorePath(
593  const Crt::String hostName,
594  const char *windowsCertStorePath,
595  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
596 
600  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithWebsocket(
601  const Crt::String hostName,
602  const WebsocketConfig &config,
603  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
604 
608  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithCustomAuthorizer(
609  const Crt::String hostName,
610  const Mqtt5CustomAuthConfig &customAuthConfig,
611  Crt::Allocator *allocator) noexcept;
612 
616  static Mqtt5ClientBuilder *NewMqtt5ClientBuilderWithCustomAuthorizerWebsocket(
617  const Crt::String hostName,
618  const Mqtt5CustomAuthConfig &customAuthConfig,
619  const WebsocketConfig &config,
620  Crt::Allocator *allocator) noexcept;
621 
622  private:
623  // Common setup shared by all valid constructors
624  Mqtt5ClientBuilder(Crt::Allocator *allocator) noexcept;
625  // Common setup shared by all valid constructors
626  Mqtt5ClientBuilder(int error, Crt::Allocator *allocator) noexcept;
627 
628  /*
629  * Creates a new Mqtt5ClientBuilder instance with default values.
630  */
631  static std::shared_ptr<Mqtt5ClientBuilder> CreateMqtt5ClientBuilder(
632  Crt::Allocator *allocator = Crt::ApiAllocator()) noexcept;
633 
634  Crt::Allocator *m_allocator;
635 
639  uint32_t m_port;
640 
645  Crt::Optional<Crt::Io::TlsContextOptions> m_tlsConnectionOptions;
646 
650  Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
651 
655  Crt::Optional<WebsocketConfig> m_websocketConfig;
656 
660  Crt::Optional<Mqtt5CustomAuthConfig> m_customAuthConfig;
661 
666  std::shared_ptr<ConnectPacket> m_connectOptions;
667 
668  Crt::Mqtt5::Mqtt5ClientOptions *m_options;
669 
670  /* Error */
671  int m_lastError;
672 
673  bool m_enableMetricsCollection;
674 
675  Crt::String m_sdkName = "CPPv2";
676  Crt::String m_sdkVersion = AWS_CRT_CPP_VERSION;
677  };
678 
679  } // namespace Iot
680 } // namespace Aws
681 
682 #endif // !BYO_CRYPTO
Aws::Iot::Mqtt5ClientBuilder
Definition: Mqtt5Client.h:123
MqttCommon.h
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Iot::Mqtt5CustomAuthConfig::Mqtt5CustomAuthConfig
Mqtt5CustomAuthConfig(Mqtt5CustomAuthConfig &&rhs)=delete
Aws::Iot::Mqtt5ClientBuilder::Mqtt5ClientBuilder
Mqtt5ClientBuilder(const Mqtt5ClientBuilder &)=delete
Aws::Iot::WebsocketConfig
Definition: MqttCommon.h:24
Aws::Iot::Mqtt5ClientBuilder::~Mqtt5ClientBuilder
~Mqtt5ClientBuilder()
Definition: Mqtt5Client.h:489
Aws::Crt::Mqtt5::Mqtt5ClientOptions
Definition: Mqtt5Client.h:453
Aws::Iot::Mqtt5ClientBuilder::operator=
Mqtt5ClientBuilder & operator=(Mqtt5ClientBuilder &&)=delete
Aws::Iot::Mqtt5CustomAuthConfig::operator=
Mqtt5CustomAuthConfig & operator=(Mqtt5CustomAuthConfig &&rhs)=delete
Aws::Crt::Mqtt5::ClientExtendedValidationAndFlowControl
aws_mqtt5_extended_validation_and_flow_control_options ClientExtendedValidationAndFlowControl
Definition: Mqtt5Types.h:200
Aws::Iot::Pkcs12Options
Definition: MqttCommon.h:105
Aws::Iot::Mqtt5ClientBuilder::WithMinConnectedTimeToResetReconnectDelayMs
Mqtt5ClientBuilder & WithMinConnectedTimeToResetReconnectDelayMs(uint64_t minConnectedTimeToResetReconnectDelayMs) noexcept
Aws::Crt::Mqtt5::OnPublishReceivedHandler
std::function< void(const PublishReceivedEventData &)> OnPublishReceivedHandler
Definition: Mqtt5Client.h:308
Mqtt5Client.h
Aws::Crt::Optional< Crt::String >
Aws::Iot::Mqtt5CustomAuthConfig
Definition: Mqtt5Client.h:33
Aws::Crt::Io::TlsContextPkcs11Options
Definition: TlsOptions.h:217
Aws::Iot::Mqtt5ClientBuilder::LastError
int LastError() const noexcept
Definition: Mqtt5Client.h:487
Aws::Crt::Mqtt5::OnAttemptingConnectHandler
std::function< void(const OnAttemptingConnectEventData &)> OnAttemptingConnectHandler
Definition: Mqtt5Client.h:282
Aws::Crt::Mqtt5::TopicAliasingOptions
Definition: Mqtt5Client.h:101
Aws::Iot::Mqtt5ClientBuilder::operator=
Mqtt5ClientBuilder & operator=(const Mqtt5ClientBuilder &)=delete
Aws::Crt::Mqtt5::ConnectPacket
Definition: Mqtt5Packets.h:672
Aws::Crt::Mqtt5::OnConnectionFailureHandler
std::function< void(const OnConnectionFailureEventData &)> OnConnectionFailureHandler
Definition: Mqtt5Client.h:271
Aws::Crt::Mqtt5::OnDisconnectionHandler
std::function< void(const OnDisconnectionEventData &)> OnDisconnectionHandler
Definition: Mqtt5Client.h:276
Aws::Crt::Http::HttpClientConnectionProxyOptions
Definition: HttpConnection.h:270
Aws::Iot::Mqtt5ClientBuilder::Mqtt5ClientBuilder
Mqtt5ClientBuilder(Mqtt5ClientBuilder &&)=delete
Aws::Crt::Io::SocketOptions
Definition: SocketOptions.h:48
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:30
std
Definition: StringView.h:862
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:36
Aws
Definition: Allocator.h:11
Aws::Crt::Mqtt5::ReconnectOptions
Definition: Mqtt5Client.h:145
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:31
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Exports.h
Sigv4Signing.h
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:35
Aws::Crt::Mqtt5::ClientSessionBehaviorType
aws_mqtt5_client_session_behavior_type ClientSessionBehaviorType
Definition: Mqtt5Types.h:186
Aws::Crt::Mqtt5::OnConnectionSuccessHandler
std::function< void(const OnConnectionSuccessEventData &)> OnConnectionSuccessHandler
Definition: Mqtt5Client.h:266
Aws::Crt::Mqtt5::OnStoppedHandler
std::function< void(const OnStoppedEventData &)> OnStoppedHandler
Definition: Mqtt5Client.h:288
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45
Aws::Crt::Mqtt5::ClientOperationQueueBehaviorType
aws_mqtt5_client_operation_queue_behavior_type ClientOperationQueueBehaviorType
Definition: Mqtt5Types.h:223