16#include <ove/net_mqtt.h>
21#ifdef CONFIG_OVE_NET_MQTT
39enum class Qos : uint8_t {
80 using MsgFn = void (*)(std::string_view topic, std::string_view payload);
90#ifdef CONFIG_OVE_ZERO_HEAP
91 int err = ove_mqtt_client_init(&handle_, &storage_);
93 int err = ove_mqtt_client_create(&handle_);
95 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
107#ifdef CONFIG_OVE_ZERO_HEAP
108 ove_mqtt_client_deinit(handle_);
110 ove_mqtt_client_destroy(handle_);
133 s_msg_fn_ = on_message;
134 ove_mqtt_config_t c{};
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));
152 ove_mqtt_disconnect(handle_);
167 return from_rc(ove_mqtt_publish(handle_, topic, payload, len,
168 static_cast<ove_mqtt_qos_t
>(qos)));
181 return publish(topic, payload.data(), payload.size(), qos);
194 ove_mqtt_subscribe(handle_, topic,
static_cast<ove_mqtt_qos_t
>(qos)));
205 return from_rc(ove_mqtt_unsubscribe(handle_, topic));
220 loop(std::chrono::nanoseconds timeout = std::chrono::milliseconds{500})
noexcept
231 return handle_ !=
nullptr;
248 static void trampoline_(
const char *topic,
size_t topic_len,
const void *payload,
249 size_t payload_len,
void * )
252 s_msg_fn_(std::string_view(topic, topic_len),
253 std::string_view(
static_cast<const char *
>(payload),
258 static inline MsgFn s_msg_fn_{};
260 ove_mqtt_client_t handle_ =
nullptr;
261#ifdef CONFIG_OVE_ZERO_HEAP
262 ove_mqtt_client_storage_t storage_ = {};
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.