VLink  2.0.0
A high-performance communication middleware
mqtt_conf.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 by Thun Lu. All rights reserved.
3  * Author: Thun Lu <thun.lu@zohomail.cn>
4  * Repo: https://github.com/thun-res/vlink
5  * _ __ __ _ __
6  * | | / / / / (_) ____ / /__
7  * | | / / / / / / / __ \ / //_/
8  * | |/ / / /___ / / / / / / / ,<
9  * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 /**
25  * @file mqtt_conf.h
26  * @brief Transport configuration for the @c mqtt:// MQTT broker bridge.
27  *
28  * @details
29  * @c MqttConf binds the @c mqtt:// URL scheme to the Eclipse Paho MQTT C client.
30  * MQTT is a lightweight publish/subscribe protocol designed for constrained
31  * devices, intermittent links, and low-bandwidth wide-area networks; it always
32  * routes through a broker (no peer-to-peer mode). Use this transport to bridge
33  * VLink topics to a cloud / fleet MQTT broker, or to interoperate with existing
34  * MQTT-based telemetry pipelines.
35  *
36  * @par Supported Node Types
37  *
38  * | Publisher | Subscriber | Server | Client | Getter | Setter |
39  * | :-------: | :--------: | :----: | :----: | :----: | :----: |
40  * | yes | yes | yes | yes | yes | yes |
41  *
42  * @par URL Format
43  * @code
44  * mqtt://<address>[?event=<name>&domain=<N>&qos=<0|1|2>][#<broker_uri>]
45  * @endcode
46  *
47  * | Component | Description |
48  * | ---------- | -------------------------------------------------------------------------- |
49  * | @c address | MQTT topic path (URL host concatenated with path) |
50  * | @c event | Optional secondary event filter (@c ?event=) |
51  * | @c domain | Domain / namespace identifier (@c ?domain=); factory default applied |
52  * | @c qos | MQTT QoS level @c 0, @c 1 or @c 2 (@c ?qos=); factory default applied |
53  * | @c fragment| Optional broker URI override carried in the URL fragment |
54  *
55  * @par Broker Connection
56  *
57  * | Property | Source | Description |
58  * | ---------------- | ---------------------------- | ------------------------------------ |
59  * | Broker URI | @c VLINK_MQTT_BROKER env var | @c tcp:// / @c ssl:// / @c ws:// URI |
60  * | Domain ID | @c VLINK_MQTT_DOMAIN env var | Default @c ?domain= when absent |
61  * | QoS level | @c VLINK_MQTT_QOS env var | Default @c 0, @c 1 or @c 2 |
62  * | Keep-alive (s) | @c VLINK_MQTT_KEEPALIVE env | MQTT keep-alive interval |
63  * | Client ID prefix | @c VLINK_MQTT_CLIENT_ID env | Prefix used for generated client IDs |
64  *
65  * @par TLS Configuration
66  *
67  * | Property | Description |
68  * | --------------------- | ---------------------------------------------------------- |
69  * | @c ssl:// URI scheme | Selects TLS transport instead of plain TCP |
70  * | CA certificate file | Configured through @c set_property("mqtt.ca_file", path) |
71  * | Client certificate | Configured through @c set_property("mqtt.cert_file", path) |
72  * | Client private key | Configured through @c set_property("mqtt.key_file", path) |
73  * | Username / password | Set via @c mqtt.username and @c mqtt.password properties |
74  *
75  * @par Example
76  * @code
77  * // Defaults inherit broker URI and QoS from environment variables:
78  * auto pub = vlink::Publisher<MyMsg>::create_unique("mqtt://telemetry/state?qos=1");
79  *
80  * // Override broker URI via URL fragment:
81  * auto sub = vlink::Subscriber<MyMsg>::create_unique("mqtt://telemetry/state#tcp://10.0.0.5:1883");
82  * @endcode
83  *
84  * @note Compiled only when @c VLINK_SUPPORT_MQTT is defined.
85  * @note URL parsing uses @c MqttFactory::get_default_domain_id() and
86  * @c MqttFactory::get_default_qos() when @c domain or @c qos are omitted;
87  * direct construction defaults remain @c domain=0 and @c qos=1.
88  * @note @c is_valid() returns @c false when @c address is empty, @c domain is
89  * negative, or @c qos is outside @c [0, 2].
90  */
91 
92 #pragma once
93 
94 #ifdef VLINK_SUPPORT_MQTT
95 
96 #include <cstdint>
97 #include <map>
98 #include <shared_mutex>
99 #include <string>
100 
101 #include "../impl/conf.h"
102 
103 namespace vlink {
104 
105 /**
106  * @struct MqttConf
107  * @brief Concrete @c Conf describing an MQTT endpoint addressed by an @c mqtt:// URL.
108  *
109  * @details
110  * Stores the MQTT topic path, optional secondary event filter, domain identifier,
111  * MQTT QoS level, and an optional broker URI override carried in the URL fragment.
112  */
113 struct VLINK_EXPORT MqttConf final : public Conf {
114  std::string address; ///< MQTT topic path (URL host concatenated with path).
115  std::string event; ///< Optional secondary event filter string.
116  int32_t domain{0}; ///< Domain / namespace identifier (non-negative).
117  int32_t qos{1}; ///< MQTT QoS level; @c 0, @c 1 or @c 2.
118  std::string fragment; ///< Optional broker URI override carried in the URL fragment.
119 
120  /**
121  * @brief Builds an @c MqttConf from its five logical fields.
122  *
123  * @param _address MQTT topic path.
124  * @param _event Optional event filter; empty by default.
125  * @param _domain Domain identifier; defaults to @c 0.
126  * @param _qos MQTT QoS level; defaults to @c 1.
127  * @param _fragment Optional broker URI override; empty by default.
128  */
129  explicit MqttConf(const std::string& _address, const std::string& _event = "", int32_t _domain = 0, int32_t _qos = 1,
130  const std::string& _fragment = "");
131 
132  /**
133  * @brief Component-wise equality on all configuration fields.
134  *
135  * @param conf Configuration to compare with.
136  * @return @c true when every field of @c *this matches @p conf.
137  */
138  [[nodiscard]] bool operator==(const MqttConf& conf) const noexcept;
139 
140  /**
141  * @brief Logical negation of @c operator==.
142  *
143  * @param conf Configuration to compare with.
144  * @return @c true when any field differs from @p conf.
145  */
146  [[nodiscard]] bool operator!=(const MqttConf& conf) const noexcept;
147 
148  /**
149  * @brief Reports this object's transport tag.
150  *
151  * @return @c TransportType::kMqtt.
152  */
153  [[nodiscard]] TransportType get_transport_type() const override;
154 
155  static constexpr const char* kRespSuffix{"___resp"}; ///< Suffix appended to RPC reply topic names.
156 
157 #ifndef VLINK_ENABLE_C_INTERFACE
159 #endif
161  VLINK_CONF_IMPL(MqttConf)
162 };
163 
164 ////////////////////////////////////////////////////////////////
165 /// Details
166 ////////////////////////////////////////////////////////////////
167 
168 inline MqttConf::MqttConf(const std::string& _address, const std::string& _event, int32_t _domain, int32_t _qos,
169  const std::string& _fragment)
170  : address(_address), event(_event), domain(_domain), qos(_qos), fragment(_fragment) {}
171 
172 inline bool MqttConf::operator==(const MqttConf& conf) const noexcept {
173  return address == conf.address && event == conf.event && domain == conf.domain && qos == conf.qos &&
174  fragment == conf.fragment;
175 }
176 
177 inline bool MqttConf::operator!=(const MqttConf& conf) const noexcept { return !(*this == conf); }
178 
179 inline TransportType MqttConf::get_transport_type() const { return TransportType::kMqtt; }
180 
181 } // namespace vlink
182 
183 #endif
#define VLINK_CONF_IMPL(classname)
Convenience macro that emits the standard concrete conf boilerplate.
Definition: conf.h:251
#define VLINK_DECLARE_GLOBAL_PROPERTY()
Declares per-transport static configuration storage and access helpers.
Definition: conf.h:309
#define VLINK_ALLOW_IMPL_TYPE(type)
Records the bitmask of ImplType values supported by a conf.
Definition: conf.h:292
#define VLINK_EXPORT
Definition: macros.h:81