VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
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 protocol backend.
27 *
28 * @details
29 * @c MqttConf configures the Eclipse Paho MQTT C transport, a lightweight
30 * publish/subscribe messaging protocol designed for constrained devices and
31 * low-bandwidth, high-latency networks.
32 *
33 * @par Supported Node Types
34 * @c mqtt:// supports all six node types: @c kPublisher, @c kSubscriber, @c kServer,
35 * @c kClient, @c kSetter, and @c kGetter.
36 *
37 * @par URL Format
38 * @code
39 * mqtt://<address>[?event=<name>&domain=<N>&qos=<0|1|2>][#<fragment>]
40 * @endcode
41 *
42 * | Component | Description |
43 * | ------------ | ------------------------------------------------------------------------- |
44 * | @c address | MQTT topic path; formed from @c host + @c "/" + @c path |
45 * | @c event | Optional secondary event filter (@c ?event=) |
46 * | @c domain | Domain/namespace identifier (@c ?domain=, default 0) |
47 * | @c qos | MQTT QoS level: 0 (at most once), 1 (at least once), 2 (exactly once) |
48 * | @c fragment | Optional broker URI override (e.g. @c tcp://192.168.1.1:1883) |
49 *
50 * @par Environment Variables
51 *
52 * | Variable | Description | Default |
53 * | ---------------------- | ------------------------------------- | ------------------------ |
54 * | VLINK_MQTT_BROKER | MQTT broker URI | tcp://localhost:1883 |
55 * | VLINK_MQTT_DOMAIN | Default domain ID | 0 |
56 * | VLINK_MQTT_QOS | Default QoS level (0, 1, 2) | 1 |
57 * | VLINK_MQTT_KEEPALIVE | Keep-alive interval in seconds | 60 |
58 * | VLINK_MQTT_CLIENT_ID | Client ID prefix | vlink_mqtt |
59 *
60 * @note This header is compiled only when @c VLINK_SUPPORT_MQTT is defined.
61 */
62
63#pragma once
64
65#ifdef VLINK_SUPPORT_MQTT
66
67#include <cstdint>
68#include <map>
69#include <shared_mutex>
70#include <string>
71
72#include "../impl/conf.h"
73
74namespace vlink {
75
76/**
77 * @struct MqttConf
78 * @brief Configuration for the @c mqtt:// MQTT transport.
79 *
80 * @details
81 * Can be constructed directly or parsed from a URL string via @c Url.
82 */
83struct VLINK_EXPORT MqttConf final : public Conf {
84 std::string address; ///< MQTT topic path (host + "/" + path from URL).
85 std::string event; ///< Optional secondary event filter string.
86 int32_t domain{0}; ///< Domain/namespace identifier (non-negative).
87 int32_t qos{1}; ///< MQTT QoS level: 0, 1, or 2.
88 std::string fragment; ///< Optional broker URI override (URL fragment).
89
90 /**
91 * @brief Constructs a @c MqttConf with explicit parameters.
92 *
93 * @param _address MQTT topic path.
94 * @param _event Optional event filter; empty by default.
95 * @param _domain Domain identifier; default 0.
96 * @param _qos MQTT QoS level; default 1.
97 * @param _fragment Optional broker URI override; empty by default.
98 */
99 explicit MqttConf(const std::string& _address, const std::string& _event = "", int32_t _domain = 0, int32_t _qos = 1,
100 const std::string& _fragment = "");
101
102 /**
103 * @brief Returns @c true if all fields equal those of @p conf.
104 *
105 * @param conf Configuration to compare.
106 * @return @c true if all fields match.
107 */
108 [[nodiscard]] bool operator==(const MqttConf& conf) const noexcept;
109
110 /**
111 * @brief Returns @c true if any field differs from @p conf.
112 *
113 * @param conf Configuration to compare.
114 * @return Logical negation of @c operator==.
115 */
116 [[nodiscard]] bool operator!=(const MqttConf& conf) const noexcept;
117
118 /**
119 * @brief Returns @c TransportType::kMqtt identifying this transport.
120 *
121 * @return @c TransportType::kMqtt.
122 */
123 [[nodiscard]] TransportType get_transport_type() const override;
124
125 static constexpr const char* kRespSuffix{"___resp"};
126
127#ifndef VLINK_ENABLE_C_INTERFACE
129#endif
130 VLINK_ALLOW_IMPL_TYPE(kServer | kClient | kPublisher | kSubscriber | kSetter | kGetter)
131 VLINK_CONF_IMPL(MqttConf)
132};
133
134////////////////////////////////////////////////////////////////
135/// Details
136////////////////////////////////////////////////////////////////
137
138inline MqttConf::MqttConf(const std::string& _address, const std::string& _event, int32_t _domain, int32_t _qos,
139 const std::string& _fragment)
140 : address(_address), event(_event), domain(_domain), qos(_qos), fragment(_fragment) {}
141
142inline bool MqttConf::operator==(const MqttConf& conf) const noexcept {
143 return address == conf.address && event == conf.event && domain == conf.domain && qos == conf.qos &&
144 fragment == conf.fragment;
145}
146
147inline bool MqttConf::operator!=(const MqttConf& conf) const noexcept { return !(*this == conf); }
148
149inline TransportType MqttConf::get_transport_type() const { return TransportType::kMqtt; }
150
151} // namespace vlink
152
153#endif
Abstract transport configuration base class and associated helper macros.
#define VLINK_CONF_IMPL(classname)
Standard boilerplate for concrete Conf subclass declarations.
Definition conf.h:227
#define VLINK_DECLARE_GLOBAL_PROPERTY()
Declares per-transport global state: thread count and property storage.
Definition conf.h:292
#define VLINK_ALLOW_IMPL_TYPE(type)
Declares a static constexpr bitmask of supported ImplType values.
Definition conf.h:268
#define VLINK_EXPORT
Definition macros.h:85