Astarte device API for C++ 0.8.1
Astarte device SDK for C++
Loading...
Searching...
No Matches
config.hpp
Go to the documentation of this file.
1// (C) Copyright 2025, SECO Mind Srl
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef ASTARTE_MQTT_CONFIG_H
6#define ASTARTE_MQTT_CONFIG_H
7
16
17#include <ada.h>
18#include <spdlog/spdlog.h>
19
20#include <chrono>
21#include <filesystem>
22#include <fstream>
23#include <memory>
24#include <optional>
25#include <string>
26#include <string_view>
27
30
31namespace astarte::device::mqtt {
32
34using namespace std::chrono_literals;
36
37// Forward declaration, needed to make Credential private.
38class Credential;
39
41constexpr uint32_t DEFAULT_KEEP_ALIVE = 30;
42
44constexpr uint32_t DEFAULT_CONNECTION_TIMEOUT = 5;
45
48
55class Config {
56 public:
61 Config(Config&& other) noexcept;
62
68 auto operator=(Config&& other) noexcept -> Config&;
69
71 Config(const Config&) = delete;
72
74 auto operator=(const Config&) -> Config& = delete;
75
78
89 [[nodiscard]] static auto with_credential_secret(std::string_view realm,
90 std::string_view device_id,
91 std::string_view credential,
92 std::string_view pairing_url,
93 std::string_view store_dir) -> Config;
94
99 [[nodiscard]] auto realm() -> std::string_view { return realm_; }
100
105 [[nodiscard]] auto device_id() -> std::string_view { return device_id_; }
106
111 [[nodiscard]] auto pairing_url() -> std::string_view { return pairing_url_; }
112
117 [[nodiscard]] auto cred_is_credential_secret() -> bool;
118
123 [[nodiscard]] auto credential_secret() -> std::optional<std::string>;
124
129 [[nodiscard]] auto store_dir() -> std::string_view { return store_dir_; }
130
137 auto keepalive(uint32_t duration) -> Config& {
138 this->keepalive_ = duration;
139 return *this;
140 }
141
147 this->ignore_ssl_ = true;
148 return *this;
149 }
150
157 auto connection_timeout(uint32_t duration) -> Config& {
158 this->conn_timeout_ = duration;
159 return *this;
160 }
161
168 auto disconnection_timeout(std::chrono::milliseconds duration) -> Config& {
169 this->disconn_timeout_ = duration;
170 return *this;
171 }
172
177 [[nodiscard]] auto keepalive() const -> uint32_t { return keepalive_; }
178
183 [[nodiscard]] auto connection_timeout() const -> uint32_t { return conn_timeout_; }
184
189 [[nodiscard]] auto disconnection_timeout() const -> std::chrono::milliseconds {
190 return disconn_timeout_;
191 }
192
193 private:
206 Config(std::string_view realm, std::string_view device_id, std::unique_ptr<Credential> credential,
207 std::string_view pairing_url, std::string_view store_dir);
208
209 std::string realm_;
210 std::string device_id_;
211 std::string pairing_url_;
212 std::unique_ptr<Credential> credential_;
213 std::string store_dir_;
214 bool ignore_ssl_{false};
215 uint32_t keepalive_;
216 uint32_t conn_timeout_;
217 std::chrono::milliseconds disconn_timeout_;
218};
219
220} // namespace astarte::device::mqtt
221
222#endif // ASTARTE_MQTT_CONFIG_H
auto ignore_ssl_errors() -> Config &
Configures the client to ignore TLS/SSL certificate validation errors.
Definition config.hpp:146
auto operator=(const Config &) -> Config &=delete
Config is non-copyable.
Config(const Config &)=delete
Config is non-copyable.
auto connection_timeout(uint32_t duration) -> Config &
Sets the MQTT connection timeout.
Definition config.hpp:157
auto disconnection_timeout(std::chrono::milliseconds duration) -> Config &
Sets the MQTT disconnection timeout.
Definition config.hpp:168
auto cred_is_credential_secret() -> bool
Checks if the credential is of type credential secret.
auto keepalive() const -> uint32_t
Gets the MQTT keep-alive interval.
Definition config.hpp:177
auto pairing_url() -> std::string_view
Gets the configured Pairing API URL.
Definition config.hpp:111
auto device_id() -> std::string_view
Gets the configured device ID.
Definition config.hpp:105
auto store_dir() -> std::string_view
Gets the configured store directory.
Definition config.hpp:129
auto operator=(Config &&other) noexcept -> Config &
Move assignment operator.
auto connection_timeout() const -> uint32_t
Gets the MQTT connection timeout.
Definition config.hpp:183
auto realm() -> std::string_view
Gets the configured realm.
Definition config.hpp:99
auto credential_secret() -> std::optional< std::string >
Retrieves the credential value.
static auto with_credential_secret(std::string_view realm, std::string_view device_id, std::string_view credential, std::string_view pairing_url, std::string_view store_dir) -> Config
Creates a new Config instance using a credential secret.
auto disconnection_timeout() const -> std::chrono::milliseconds
Gets the MQTT disconnection timeout.
Definition config.hpp:189
Config(Config &&other) noexcept
Move constructor.
auto keepalive(uint32_t duration) -> Config &
Sets the MQTT keep-alive interval.
Definition config.hpp:137
specific error definitions for the Astarte device SDK.
Namespace for Astarte device functionality using the MQTT transport protocol.
Definition errors.hpp:57
constexpr uint32_t DEFAULT_KEEP_ALIVE
Default keep alive interval in seconds for the MQTT connection.
Definition config.hpp:41
constexpr auto DEFAULT_DISCONNECTION_TIMEOUT
Default disconnection timeout in seconds for the MQTT connection.
Definition config.hpp:47
constexpr uint32_t DEFAULT_CONNECTION_TIMEOUT
Default connection timeout in seconds for the MQTT connection.
Definition config.hpp:44
Astarte pairing API and utilities.