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 <string_view>
19
20#ifdef CONFIG_OVE_NET_MQTT
21
22namespace ove {
23
32namespace mqtt {
33
38enum class Qos : uint8_t {
39 AtMostOnce = 0,
40 AtLeastOnce = 1,
41};
42
49struct Config {
50 const char *host{};
51 uint16_t port{1883};
52 const char *client_id{};
53 const char *username{};
54 const char *password{};
55 uint16_t keep_alive_s{30};
56 bool use_tls{false};
57};
58
70class Client {
71public:
78 using MsgFn = void (*)(std::string_view topic,
79 std::string_view payload);
80
87 Client() {
88#ifdef CONFIG_OVE_ZERO_HEAP
89 int err = ove_mqtt_client_init(&handle_, &storage_);
90#else
91 int err = ove_mqtt_client_create(&handle_);
92#endif
93 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
94 }
95
101 ~Client() {
102 if (!handle_) return;
103#ifdef CONFIG_OVE_ZERO_HEAP
104 ove_mqtt_client_deinit(handle_);
105#else
106 ove_mqtt_client_destroy(handle_);
107#endif
108 }
109
110 Client(const Client &) = delete;
111 Client &operator=(const Client &) = delete;
112 Client(Client &&) = delete;
113 Client &operator=(Client &&) = delete;
114
126 [[nodiscard]] int connect(const Config &cfg,
127 MsgFn on_message = nullptr) {
128 s_msg_fn_ = on_message;
129 ove_mqtt_config_t c{};
130 c.host = cfg.host;
131 c.port = cfg.port;
132 c.client_id = cfg.client_id;
133 c.username = cfg.username;
134 c.password = cfg.password;
135 c.keep_alive_s = cfg.keep_alive_s;
136 c.use_tls = cfg.use_tls ? 1 : 0;
137 c.on_message = on_message ? trampoline_ : nullptr;
138 c.user_data = nullptr;
139 return ove_mqtt_connect(handle_, &c);
140 }
141
145 void disconnect() { ove_mqtt_disconnect(handle_); }
146
155 [[nodiscard]] int publish(const char *topic, const void *payload,
156 size_t len, Qos qos = Qos::AtMostOnce) {
157 return ove_mqtt_publish(handle_, topic, payload, len,
158 static_cast<ove_mqtt_qos_t>(qos));
159 }
160
168 [[nodiscard]] int publish(const char *topic,
169 std::string_view payload,
170 Qos qos = Qos::AtMostOnce) {
171 return publish(topic, payload.data(), payload.size(), qos);
172 }
173
180 [[nodiscard]] int subscribe(const char *topic,
181 Qos qos = Qos::AtMostOnce) {
182 return ove_mqtt_subscribe(handle_, topic,
183 static_cast<ove_mqtt_qos_t>(qos));
184 }
185
191 [[nodiscard]] int unsubscribe(const char *topic) {
192 return ove_mqtt_unsubscribe(handle_, topic);
193 }
194
203 [[nodiscard]] int loop(uint32_t timeout_ms = 500) {
204 return ove_mqtt_loop(handle_, timeout_ms);
205 }
206
211 bool valid() const { return handle_ != nullptr; }
212
217 ove_mqtt_client_t handle() const { return handle_; }
218
219private:
224 static void trampoline_(const char *topic, size_t topic_len,
225 const void *payload, size_t payload_len,
226 void * /*user_data*/) {
227 if (s_msg_fn_) {
228 s_msg_fn_(
229 std::string_view(topic, topic_len),
230 std::string_view(
231 static_cast<const char *>(payload),
232 payload_len));
233 }
234 }
235
236 static inline MsgFn s_msg_fn_{};
237
238 ove_mqtt_client_t handle_ = nullptr;
239#ifdef CONFIG_OVE_ZERO_HEAP
240 ove_mqtt_client_storage_t storage_ = {};
241#endif
242};
243
244} /* namespace mqtt */
245
246} // namespace ove
247
248#endif /* CONFIG_OVE_NET_MQTT */
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.