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
12#include "ConfigSections.h"
13
14namespace mqtt::mqttstore::lib {
15
16 ConfigSession::ConfigSession(utils::SubCommand* parent)
17 : utils::SubCommand(parent, this, "MQTT session")
18 , clientIdOpt(setConfigurable(addOption("--client-id", "MQTT Client-ID", "string", CLI::TypeValidator<std::string>()), true))
19 , qoSOpt(setConfigurable(addOption("--qos", "Default subscription quality of service", "uint8_t", "0", CLI::Range(0, 2)), true))
20 , retainSessionOpt(setConfigurable(addFlag("--retain-session{true},-r{true}",
21 "Use a persistent MQTT session (sets clean_session=false)",
22 "bool",
23 "false",
24 CLI::IsMember({"true", "false"})),
25 true)
26 ->needs(clientIdOpt))
27 , keepAliveOpt(setConfigurable(
28 addOption("--keep-alive", "MQTT keep alive in seconds", "uint16_t", "60", CLI::TypeValidator<std::uint16_t>()), true))
29 , willTopicOpt(setConfigurable(addOption("--will-topic", "MQTT will topic", "string", CLI::TypeValidator<std::string>()), true))
31 setConfigurable(addOption("--will-message", "MQTT will message", "string", CLI::TypeValidator<std::string>()), true))
32 , willQoSOpt(setConfigurable(addOption("--will-qos", "MQTT will quality of service", "uint8_t", "0", CLI::Range(0, 2)), true))
33 , willRetainOpt(setConfigurable(
34 addFlag("--will-retain{true}", "MQTT will message retain", "bool", "false", CLI::IsMember({"true", "false"})), true))
35 , usernameOpt(setConfigurable(addOption("--username", "MQTT username", "string", CLI::TypeValidator<std::string>()), true))
36 , passwordOpt(setConfigurable(addOption("--password", "MQTT password", "string", CLI::TypeValidator<std::string>()), true))
37 , sessionStoreOpt(setConfigurable(
38 addOption("--session-store", "Path to persistent MQTT session store", "filename", !CLI::ExistingDirectory), true)) {
39 }
40
41 ConfigSession::~ConfigSession() = default;
42
43 std::string ConfigSession::getClientId() const {
44 return clientIdOpt->as<std::string>();
45 }
46
48 return qoSOpt->as<std::uint8_t>();
49 }
50
52 return retainSessionOpt->as<bool>();
53 }
54
56 return keepAliveOpt->as<std::uint16_t>();
57 }
58
59 std::string ConfigSession::getWillTopic() const {
60 return willTopicOpt->as<std::string>();
61 }
62
63 std::string ConfigSession::getWillMessage() const {
64 return willMessageOpt->as<std::string>();
65 }
66
68 return willQoSOpt->as<std::uint8_t>();
69 }
70
72 return willRetainOpt->as<bool>();
73 }
74
75 std::string ConfigSession::getUsername() const {
76 return usernameOpt->as<std::string>();
77 }
78
79 std::string ConfigSession::getPassword() const {
80 return passwordOpt->as<std::string>();
81 }
82
83 std::string ConfigSession::getSessionStore() const {
84 return sessionStoreOpt->as<std::string>();
85 }
86
87 ConfigSubscribe::ConfigSubscribe(utils::SubCommand* parent)
88 : utils::SubCommand(parent, this, "MQTT subscriptions")
89 , topicOpt(setConfigurable(addOption("--topic",
90 "Topic filter to persist; append ##<qos> to override QoS",
91 "string",
92 CLI::TypeValidator<std::string>()),
93 true)
94 ->take_all()) {
95 required(topicOpt);
96 }
97
98 ConfigSubscribe::~ConfigSubscribe() = default;
99
100 std::list<std::string> ConfigSubscribe::getTopic() const {
101 std::list<std::string> topicList = topicOpt->as<std::list<std::string>>();
102
103 if (!topicList.empty() && topicList.front().empty()) {
104 topicList.pop_front();
105 }
106
107 return topicList;
108 }
109
110 ConfigDatabase::ConfigDatabase(utils::SubCommand* parent)
111 : utils::SubCommand(parent, this, "Database connection")
112 , hostOpt(setConfigurable(
113 addOption("--host", "Hostname or IP address of MariaDB server", "hostname|IPv4", CLI::TypeValidator<std::string>()), true))
114 , usernameOpt(setConfigurable(addOption("--username", "Database username", "string", CLI::TypeValidator<std::string>()), true))
115 , passwordOpt(setConfigurable(addOption("--password", "Database password", "string", CLI::TypeValidator<std::string>()), true))
116 , databaseOpt(setConfigurable(addOption("--database", "Database name", "string", CLI::TypeValidator<std::string>()), true))
117 , portOpt(setConfigurable(addOption("--port", "Database TCP port", "uint16_t", 3306, CLI::TypeValidator<std::uint16_t>()), true))
118 , socketOpt(setConfigurable(addOption("--socket",
119 "Database socket file (overrides host and port when set)",
120 "string",
121 "/run/mysqld/mysqld.sock",
122 CLI::TypeValidator<std::string>()),
123 true))
124 , flagsOpt(setConfigurable(addOption("--flags", "MariaDB connection flags", "uint32_t", "0", CLI::TypeValidator<std::uint32_t>()),
125 true)) {
126 }
127
128 ConfigDatabase::~ConfigDatabase() = default;
129
130 std::string ConfigDatabase::getHost() const {
131 return hostOpt->as<std::string>();
132 }
133
134 std::string ConfigDatabase::getUsername() const {
135 return usernameOpt->as<std::string>();
136 }
137
138 std::string ConfigDatabase::getPassword() const {
139 return passwordOpt->as<std::string>();
140 }
141
142 std::string ConfigDatabase::getDatabase() const {
143 return databaseOpt->as<std::string>();
144 }
145
147 return portOpt->as<std::uint16_t>();
148 }
149
150 std::string ConfigDatabase::getSocket() const {
151 return socketOpt->as<std::string>();
152 }
153
155 return flagsOpt->as<std::uint32_t>();
156 }
157
158 ConfigStorage::ConfigStorage(utils::SubCommand* parent)
159 : utils::SubCommand(parent, this, "Storage behavior")
160 , rawTableOpt(setConfigurable(
161 addOption("--raw-table", "Raw MQTT envelope table", "identifier", "mqtt_messages", CLI::TypeValidator<std::string>()), true))
162 , autoCreateRawTableOpt(setConfigurable(addFlag("--auto-create-raw-table{true}",
163 "Create the raw MQTT envelope table automatically",
164 "bool",
165 "true",
166 CLI::IsMember({"true", "false"})),
167 true))
168 , projectionFileOpt(setConfigurable(
169 addOption("--projection-file", "Optional JSON file describing typed projections", "filename", CLI::ExistingFile), true)) {
170 }
171
172 ConfigStorage::~ConfigStorage() = default;
173
174 std::string ConfigStorage::getRawTable() const {
175 return rawTableOpt->as<std::string>();
176 }
177
179 return autoCreateRawTableOpt->as<bool>();
180 }
181
182 std::string ConfigStorage::getProjectionFile() const {
183 return projectionFileOpt->as<std::string>();
184 }
185
186} // namespace mqtt::mqttstore::lib
ConfigDatabase(utils::SubCommand *parent)
ConfigSession(utils::SubCommand *parent)
ConfigStorage(utils::SubCommand *parent)
ConfigSubscribe(utils::SubCommand *parent)
std::list< std::string > getTopic() const