oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
net_mqtt.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/net_mqtt.h>
17#include <ove/types.hpp>
18#include <ove/error.hpp>
19#include <string_view>
20
21#ifdef CONFIG_OVE_NET_MQTT
22
23namespace ove::mqtt
24{
25
39enum class Qos : uint8_t {
40 AtMostOnce = 0,
41 AtLeastOnce = 1,
42};
43
50struct Config {
51 const char *host{};
52 uint16_t port{1883};
53 const char *client_id{};
54 const char *username{};
55 const char *password{};
56 uint16_t keep_alive_s{30};
57 bool use_tls{false};
58};
59
71class Client
72{
73 public:
80 using MsgFn = void (*)(std::string_view topic, std::string_view payload);
81
89 {
90#ifdef CONFIG_OVE_ZERO_HEAP
91 int err = ove_mqtt_client_init(&handle_, &storage_);
92#else
93 int err = ove_mqtt_client_create(&handle_);
94#endif
95 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
96 }
97
103 ~Client() noexcept
104 {
105 if (!handle_)
106 return;
107#ifdef CONFIG_OVE_ZERO_HEAP
108 ove_mqtt_client_deinit(handle_);
109#else
110 ove_mqtt_client_destroy(handle_);
111#endif
112 }
113
114 Client(const Client &) = delete;
115 Client &operator=(const Client &) = delete;
116 Client(Client &&) = delete;
117 Client &operator=(Client &&) = delete;
118
131 [[nodiscard]] Result<void> connect(const Config &cfg, MsgFn on_message = nullptr) noexcept
132 {
133 s_msg_fn_ = on_message;
134 ove_mqtt_config_t c{};
135 c.host = cfg.host;
136 c.port = cfg.port;
137 c.client_id = cfg.client_id;
138 c.username = cfg.username;
139 c.password = cfg.password;
140 c.keep_alive_s = cfg.keep_alive_s;
141 c.use_tls = cfg.use_tls ? 1 : 0;
142 c.on_message = on_message ? trampoline_ : nullptr;
143 c.user_data = nullptr;
144 return from_rc(ove_mqtt_connect(handle_, &c));
145 }
146
151 {
152 ove_mqtt_disconnect(handle_);
153 }
154
164 [[nodiscard]] Result<void> publish(const char *topic, const void *payload, size_t len,
165 Qos qos = Qos::AtMostOnce) noexcept
166 {
167 return from_rc(ove_mqtt_publish(handle_, topic, payload, len,
168 static_cast<ove_mqtt_qos_t>(qos)));
169 }
170
178 [[nodiscard]] Result<void> publish(const char *topic, std::string_view payload,
179 Qos qos = Qos::AtMostOnce) noexcept
180 {
181 return publish(topic, payload.data(), payload.size(), qos);
182 }
183
191 [[nodiscard]] Result<void> subscribe(const char *topic, Qos qos = Qos::AtMostOnce) noexcept
192 {
193 return from_rc(
194 ove_mqtt_subscribe(handle_, topic, static_cast<ove_mqtt_qos_t>(qos)));
195 }
196
203 [[nodiscard]] Result<void> unsubscribe(const char *topic) noexcept
204 {
205 return from_rc(ove_mqtt_unsubscribe(handle_, topic));
206 }
207
219 [[nodiscard]] Result<void>
220 loop(std::chrono::nanoseconds timeout = std::chrono::milliseconds{500}) noexcept
221 {
222 return from_rc(ove_mqtt_loop(handle_, to_timeout_ns(timeout)));
223 }
224
229 bool valid() const
230 {
231 return handle_ != nullptr;
232 }
233
238 ove_mqtt_client_t handle() const
239 {
240 return handle_;
241 }
242
243 private:
248 static void trampoline_(const char *topic, size_t topic_len, const void *payload,
249 size_t payload_len, void * /*user_data*/)
250 {
251 if (s_msg_fn_) {
252 s_msg_fn_(std::string_view(topic, topic_len),
253 std::string_view(static_cast<const char *>(payload),
254 payload_len));
255 }
256 }
257
258 static inline MsgFn s_msg_fn_{};
259
260 ove_mqtt_client_t handle_ = nullptr;
261#ifdef CONFIG_OVE_ZERO_HEAP
262 ove_mqtt_client_storage_t storage_ = {};
263#endif
264};
265
266} /* namespace ove::mqtt */
267
268#endif /* CONFIG_OVE_NET_MQTT */
RAII wrapper around an oveRTOS MQTT client handle.
Definition net_mqtt.hpp:72
void disconnect()
Disconnects from the MQTT broker.
Definition net_mqtt.hpp:150
Result< void > publish(const char *topic, const void *payload, size_t len, Qos qos=Qos::AtMostOnce) noexcept
Publishes a message (raw pointer + length).
Definition net_mqtt.hpp:164
Result< void > loop(std::chrono::nanoseconds timeout=std::chrono::milliseconds{500}) noexcept
Processes incoming packets and sends keep-alive pings.
Definition net_mqtt.hpp:220
Result< void > subscribe(const char *topic, Qos qos=Qos::AtMostOnce) noexcept
Subscribes to a topic.
Definition net_mqtt.hpp:191
void(*)(std::string_view topic, std::string_view payload) MsgFn
Stateless message callback type.
Definition net_mqtt.hpp:80
Result< void > unsubscribe(const char *topic) noexcept
Unsubscribes from a topic.
Definition net_mqtt.hpp:203
Client()
Constructs and initialises the MQTT client.
Definition net_mqtt.hpp:88
Result< void > publish(const char *topic, std::string_view payload, Qos qos=Qos::AtMostOnce) noexcept
Publishes a message from a string_view.
Definition net_mqtt.hpp:178
~Client() noexcept
Destroys the MQTT client, releasing the underlying resource.
Definition net_mqtt.hpp:103
bool valid() const
Returns true if the underlying client handle is non-null.
Definition net_mqtt.hpp:229
ove_mqtt_client_t handle() const
Returns the raw oveRTOS MQTT client handle.
Definition net_mqtt.hpp:238
Result< void > connect(const Config &cfg, MsgFn on_message=nullptr) noexcept
Connects to an MQTT broker.
Definition net_mqtt.hpp:131
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
C++ wrappers around the oveRTOS MQTT client API.
Definition net_mqtt.hpp:24
Qos
MQTT Quality of Service level.
Definition net_mqtt.hpp:39
constexpr uint64_t to_timeout_ns(std::chrono::duration< Rep, Period > d) noexcept
Convert a chrono duration to uint64_t nanoseconds for the C API.
Definition types.hpp:129
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
MQTT connection configuration.
Definition net_mqtt.hpp:50
const char * host
Definition net_mqtt.hpp:51
const char * password
Definition net_mqtt.hpp:55
const char * username
Definition net_mqtt.hpp:54
const char * client_id
Definition net_mqtt.hpp:53
uint16_t port
Definition net_mqtt.hpp:52
bool use_tls
Definition net_mqtt.hpp:57
uint16_t keep_alive_s
Definition net_mqtt.hpp:56
Common type definitions and concepts for the C++ wrapper layer.