16#include <ove/net_mqtt.h>
20#ifdef CONFIG_OVE_NET_MQTT
38enum class Qos : uint8_t {
52 const char *client_id{};
53 const char *username{};
54 const char *password{};
55 uint16_t keep_alive_s{30};
78 using MsgFn = void (*)(std::string_view topic,
79 std::string_view payload);
88#ifdef CONFIG_OVE_ZERO_HEAP
89 int err = ove_mqtt_client_init(&handle_, &storage_);
91 int err = ove_mqtt_client_create(&handle_);
93 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
102 if (!handle_)
return;
103#ifdef CONFIG_OVE_ZERO_HEAP
104 ove_mqtt_client_deinit(handle_);
106 ove_mqtt_client_destroy(handle_);
110 Client(
const Client &) =
delete;
111 Client &operator=(
const Client &) =
delete;
112 Client(Client &&) =
delete;
113 Client &operator=(Client &&) =
delete;
126 [[nodiscard]]
int connect(
const Config &cfg,
127 MsgFn on_message =
nullptr) {
128 s_msg_fn_ = on_message;
129 ove_mqtt_config_t c{};
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);
145 void disconnect() { ove_mqtt_disconnect(handle_); }
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));
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);
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));
191 [[nodiscard]]
int unsubscribe(
const char *topic) {
192 return ove_mqtt_unsubscribe(handle_, topic);
203 [[nodiscard]]
int loop(uint32_t timeout_ms = 500) {
204 return ove_mqtt_loop(handle_, timeout_ms);
211 bool valid()
const {
return handle_ !=
nullptr; }
217 ove_mqtt_client_t handle()
const {
return handle_; }
224 static void trampoline_(
const char *topic,
size_t topic_len,
225 const void *payload,
size_t payload_len,
229 std::string_view(topic, topic_len),
231 static_cast<const char *
>(payload),
236 static inline MsgFn s_msg_fn_{};
238 ove_mqtt_client_t handle_ =
nullptr;
239#ifdef CONFIG_OVE_ZERO_HEAP
240 ove_mqtt_client_storage_t storage_ = {};
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.