oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
ove::mqtt::Client Class Reference

RAII wrapper around an oveRTOS MQTT client handle. More...

#include <net_mqtt.hpp>

Public Types

using MsgFn = void(*)(std::string_view topic, std::string_view payload)
 Stateless message callback type.
 

Public Member Functions

 Client ()
 Constructs and initialises the MQTT client.
 
 ~Client () noexcept
 Destroys the MQTT client, releasing the underlying resource.
 
 Client (const Client &)=delete
 
Clientoperator= (const Client &)=delete
 
 Client (Client &&)=delete
 
Clientoperator= (Client &&)=delete
 
Result< void > connect (const Config &cfg, MsgFn on_message=nullptr) noexcept
 Connects to an MQTT broker.
 
void disconnect ()
 Disconnects from the MQTT broker.
 
Result< void > publish (const char *topic, const void *payload, size_t len, Qos qos=Qos::AtMostOnce) noexcept
 Publishes a message (raw pointer + length).
 
Result< void > publish (const char *topic, std::string_view payload, Qos qos=Qos::AtMostOnce) noexcept
 Publishes a message from a string_view.
 
Result< void > subscribe (const char *topic, Qos qos=Qos::AtMostOnce) noexcept
 Subscribes to a topic.
 
Result< void > unsubscribe (const char *topic) noexcept
 Unsubscribes from a topic.
 
Result< void > loop (std::chrono::nanoseconds timeout=std::chrono::milliseconds{500}) noexcept
 Processes incoming packets and sends keep-alive pings.
 
bool valid () const
 Returns true if the underlying client handle is non-null.
 
ove_mqtt_client_t handle () const
 Returns the raw oveRTOS MQTT client handle.
 

Detailed Description

RAII wrapper around an oveRTOS MQTT client handle.

Constructs the underlying MQTT client on creation and destroys it on destruction. The message callback uses a static trampoline so that no heap-allocated closure is required; this makes the Client inherently per-instance and therefore non-movable even without CONFIG_OVE_ZERO_HEAP.

Note
Non-copyable and non-movable (static callback is per-instance).

Member Typedef Documentation

◆ MsgFn

using ove::mqtt::Client::MsgFn = void (*)(std::string_view topic, std::string_view payload)

Stateless message callback type.

Must be a plain function pointer (or a stateless lambda convertible to one). Receives the topic and payload as std::string_view.

Constructor & Destructor Documentation

◆ Client()

ove::mqtt::Client::Client ( )
inline

Constructs and initialises the MQTT client.

Calls ove_mqtt_client_init (zero-heap) or ove_mqtt_client_create (heap). Asserts at startup if initialisation fails.

◆ ~Client()

ove::mqtt::Client::~Client ( )
inlinenoexcept

Destroys the MQTT client, releasing the underlying resource.

If the handle is null the destructor is a no-op.

Member Function Documentation

◆ connect()

Result< void > ove::mqtt::Client::connect ( const Config cfg,
MsgFn  on_message = nullptr 
)
inlinenoexcept

Connects to an MQTT broker.

Optionally registers a stateless message callback that is invoked for every incoming PUBLISH. The callback is stored in a static variable and dispatched via an internal trampoline.

Parameters
[in]cfgConnection configuration.
[in]on_messageMessage callback (nullptr to disable).
Returns
Empty Result<void> on success; unexpected Error on failure.

◆ publish() [1/2]

Result< void > ove::mqtt::Client::publish ( const char *  topic,
const void *  payload,
size_t  len,
Qos  qos = Qos::AtMostOnce 
)
inlinenoexcept

Publishes a message (raw pointer + length).

Parameters
[in]topicTopic string (NUL-terminated).
[in]payloadMessage payload.
[in]lenPayload length in bytes.
[in]qosQoS level (default: AtMostOnce).
Returns
Empty Result<void> on success; unexpected Error on failure.

◆ publish() [2/2]

Result< void > ove::mqtt::Client::publish ( const char *  topic,
std::string_view  payload,
Qos  qos = Qos::AtMostOnce 
)
inlinenoexcept

Publishes a message from a string_view.

Parameters
[in]topicTopic string (NUL-terminated).
[in]payloadMessage payload as a string_view.
[in]qosQoS level (default: AtMostOnce).
Returns
As publish(const char*, const void*, size_t, Qos).

◆ subscribe()

Result< void > ove::mqtt::Client::subscribe ( const char *  topic,
Qos  qos = Qos::AtMostOnce 
)
inlinenoexcept

Subscribes to a topic.

Parameters
[in]topicTopic filter (NUL-terminated).
[in]qosMaximum QoS level (default: AtMostOnce).
Returns
Empty Result<void> on success; unexpected Error on failure.

◆ unsubscribe()

Result< void > ove::mqtt::Client::unsubscribe ( const char *  topic)
inlinenoexcept

Unsubscribes from a topic.

Parameters
[in]topicTopic filter (NUL-terminated).
Returns
Empty Result<void> on success; unexpected Error on failure.

◆ loop()

Result< void > ove::mqtt::Client::loop ( std::chrono::nanoseconds  timeout = std::chrono::milliseconds{500})
inlinenoexcept

Processes incoming packets and sends keep-alive pings.

Must be called periodically (typically in a loop or timer).

Parameters
[in]timeoutMaximum time to wait for incoming data (any std::chrono::duration unit; defaults to 500 ms).
Returns
Empty Result<void> on success; unexpected Error on failure.

◆ valid()

bool ove::mqtt::Client::valid ( ) const
inline

Returns true if the underlying client handle is non-null.

Returns
true when the client was successfully initialised.

◆ handle()

ove_mqtt_client_t ove::mqtt::Client::handle ( ) const
inline

Returns the raw oveRTOS MQTT client handle.

Returns
The opaque ove_mqtt_client_t handle.

The documentation for this class was generated from the following file: