MQTTSuite
Loading...
Searching...
No Matches
ConfigSections.cpp
Go to the documentation of this file.
1/*
2 * MQTTSuite - A lightweight MQTT Integration System
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "ConfigSections.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#endif
47
48namespace mqtt::mqttcli::lib {
49 ConfigSubscribe::ConfigSubscribe(utils::SubCommand* parent)
50 : utils::SubCommand(parent, this, "Applications (at least one required)")
51 , topicOpt( //
52 setConfigurable(addOption("--topic", "List of topics subscribing to", "string", CLI::TypeValidator<std::string>()), true)
53 ->take_all()) {
54 required(topicOpt);
55
56 required(false, true);
57 }
58
59 ConfigSubscribe::~ConfigSubscribe() = default;
60
61 std::list<std::string> ConfigSubscribe::getTopic() const {
62 std::list<std::string> topicList = topicOpt->as<std::list<std::string>>();
63
64 if (topicList.front().empty()) {
65 topicList.pop_front();
66 }
67
68 return topicList;
69 }
70
71 const ConfigSubscribe& ConfigSubscribe::setTopic(const std::string& topic) {
72 topicOpt->default_val(topic);
73
74 return *this;
75 }
76
77 ConfigPublish::ConfigPublish(utils::SubCommand* parent)
78 : utils::SubCommand(parent, this, "Applications (at least one required)")
79 , topicOpt( //
80 setConfigurable(addOption("--topic", "List of topics subscribing to", "string", CLI::TypeValidator<std::string>()), true))
81 , messageOpt( //
82 setConfigurable(addOption("--message", "Message to be published", "string", CLI::TypeValidator<std::string>()), true))
83 , retainOpt( //
84 setConfigurable(addFlag("--retain{true}", "Message retain", "bool", "false", CLI::IsMember({"true", "false"})), true)) {
85 required(messageOpt);
86 required(topicOpt);
87
88 required(false, true);
89 }
90
91 ConfigPublish::~ConfigPublish() = default;
92
93 std::string ConfigPublish::getTopic() const {
94 return topicOpt->as<std::string>();
95 }
96
97 const ConfigPublish& ConfigPublish::setTopic(const std::string& topic) {
98 topicOpt->default_val(topic);
99
100 return *this;
101 }
102
103 std::string ConfigPublish::getMessage() const {
104 return messageOpt->as<std::string>();
105 }
106
107 const ConfigPublish& ConfigPublish::setMessage(const std::string& message) {
108 messageOpt->default_val(message);
109
110 return *this;
111 }
112
113 bool ConfigPublish::getRetain() const {
114 return retainOpt->as<bool>();
115 }
116
117 const ConfigPublish& ConfigPublish::setRetain(bool retain) {
118 retainOpt->default_val(retain);
119
120 return *this;
121 }
122
123 ConfigSession::ConfigSession(utils::SubCommand* parent)
124 : utils::SubCommand(parent, this, "Applications")
125 , clientIdOpt( //
126 setConfigurable(addOption("--client-id", "MQTT Client-ID", "string", CLI::TypeValidator<std::string>()), true))
127 , qoSOpt( //
128 setConfigurable(addOption("--qos", "Quality of service", "uint8_t", "0", CLI::Range(0, 2)), true))
129 , retainSessionOpt( //
130 setConfigurable(
131 addFlag("--retain-session{true},-r{true}", "Clean session", "bool", "false", CLI::IsMember({"true", "false"})), true)
132 ->needs(clientIdOpt))
133 , keepAliveOpt( //
134 setConfigurable(addOption("--keep-alive", "Quality of service", "uint16_t", "60", CLI::TypeValidator<uint16_t>()), true))
135 , willTopicOpt( //
136 setConfigurable(addOption("--will-topic", "MQTT will topic", "string", CLI::TypeValidator<std::string>()), true))
137 , willMessageOpt( //
138 setConfigurable(addOption("--will-message", "MQTT will message", "string", CLI::TypeValidator<std::string>()), true))
139 , willQoSOpt( //
140 setConfigurable(addOption("--will-qos", "MQTT will quality of service", "uint8_t", "0", CLI::TypeValidator<uint8_t>()), true))
141 , willRetainOpt( //
142 setConfigurable(addFlag("--will-retain{true}", "MQTT will message retain", "bool", "false", CLI::IsMember({"true", "false"})),
143 true))
144 , usernameOpt( //
145 setConfigurable(addOption("--username", "MQTT username", "string", CLI::TypeValidator<std::string>()), true))
146 , passwordOpt( //
147 setConfigurable(addOption("--password", "MQTT password", "string", CLI::TypeValidator<std::string>()), true)) {
148 }
149
150 ConfigSession::~ConfigSession() = default;
151
152 std::string ConfigSession::getClientId() const {
153 return clientIdOpt->as<std::string>();
154 }
155
156 const ConfigSession& ConfigSession::setClientId(const std::string& clientId) const {
157 clientIdOpt->default_val(clientId)->clear();
158
159 return *this;
160 }
161
162 uint8_t ConfigSession::getQoS() const {
163 return qoSOpt->as<uint8_t>();
164 }
165
166 const ConfigSession& ConfigSession::setQos(uint8_t qoS) const {
167 qoSOpt->default_val(qoS)->clear();
168
169 return *this;
170 }
171
173 return retainSessionOpt->as<bool>();
174 }
175
176 const ConfigSession& ConfigSession::setRetainSession(bool retainSession) const {
177 retainSessionOpt->default_val(retainSession)->clear();
178
179 return *this;
180 }
181
182 uint16_t ConfigSession::getKeepAlive() const {
183 return keepAliveOpt->as<uint16_t>();
184 }
185
186 const ConfigSession& ConfigSession::setKeepAlive(uint16_t keepAlive) const {
187 keepAliveOpt->default_val(keepAlive)->clear();
188
189 return *this;
190 }
191
192 std::string ConfigSession::getWillTopic() const {
193 return willTopicOpt->as<std::string>();
194 }
195
196 const ConfigSession& ConfigSession::setWillTopic(const std::string& willTopic) const {
197 willTopicOpt->default_val(willTopic)->clear();
198
199 return *this;
200 }
201
202 std::string ConfigSession::getWillMessage() const {
203 return willMessageOpt->as<std::string>();
204 }
205
206 const ConfigSession& ConfigSession::setWillMessage(const std::string& willMessage) const {
207 willMessageOpt->default_val(willMessage)->clear();
208
209 return *this;
210 }
211
212 uint8_t ConfigSession::getWillQoS() const {
213 return willQoSOpt->as<uint8_t>();
214 }
215
216 const ConfigSession& ConfigSession::settWillQoS(uint8_t willQoS) const {
217 willQoSOpt->default_val(willQoS)->clear();
218
219 return *this;
220 }
221
223 return willRetainOpt->as<bool>();
224 }
225
226 const ConfigSession& ConfigSession::setWillRetain(bool willRetain) const {
227 willRetainOpt->default_val(willRetain)->clear();
228
229 return *this;
230 }
231
232 std::string ConfigSession::getUsername() const {
233 return usernameOpt->as<std::string>();
234 }
235
236 const ConfigSession& ConfigSession::settUsername(const std::string& username) const {
237 usernameOpt->default_val(username)->clear();
238
239 return *this;
240 }
241
242 std::string ConfigSession::getPassword() const {
243 return passwordOpt->as<std::string>();
244 }
245
246 const ConfigSession& ConfigSession::setPassword(const std::string& password) const {
247 passwordOpt->default_val(password)->clear();
248
249 return *this;
250 }
251
252} // namespace mqtt::mqttcli::lib
ConfigPublish(utils::SubCommand *parent)
const ConfigPublish & setRetain(bool retain)
const ConfigSession & setKeepAlive(uint16_t keepAlive) const
const ConfigSession & setWillRetain(bool willRetain) const
ConfigSession(utils::SubCommand *parent)
const ConfigSession & setQos(uint8_t qoS) const
const ConfigSession & setRetainSession(bool retainSession) const
const ConfigSession & settWillQoS(uint8_t willQoS) const
ConfigSubscribe(utils::SubCommand *parent)
std::list< std::string > getTopic() const