ESPectre SDK
2.8.0-280-gac7af68
Wi-Fi CSI motion sensing for ESP32 firmware
Toggle main menu visibility
Loading...
Searching...
No Matches
mqtt_transport.h
Go to the documentation of this file.
1
/*
2
* ESPectre - MQTT Transport Boundary
3
*
4
* Abstract MQTT transport used by native frontends and shared helpers.
5
*
6
* Author: Francesco Pace <francesco.pace@gmail.com>
7
* SPDX-License-Identifier: GPL-3.0-only
8
* Commercial licensing available under separate agreement; see LICENSING.md.
9
*/
10
#pragma once
11
12
#include <cstddef>
13
#include <cstdint>
14
#include <functional>
15
#include <string>
16
17
#include "
espectre_protocol.h
"
18
19
namespace
espectre
{
20
21
struct
MqttTransportDiagnostics
{
22
size_t
queue_capacity
{0U};
23
size_t
outbox_capacity_bytes
{0U};
24
size_t
queued_publishes
{0U};
25
uint32_t
dropped_publishes
{0U};
26
uint32_t
publish_failures
{0U};
27
uint32_t
reconnects
{0U};
28
};
29
30
/**
31
* The MQTT client seam.
32
*
33
* Implement it to carry ESPectre Protocol messages over an MQTT stack you
34
* already own, then hand the instance to a frontend. `EspIdfMqttTransport`
35
* (`mqtt_transport_esp_idf.h`) is the shipped implementation over `esp-mqtt`,
36
* and `test/cpp/support/mqtt_transport_mock.h` is the host double.
37
*
38
* Topic layout and payload schemas live in `docs/ESPECTRE_PROTOCOL.md`, and
39
* `espectre_protocol.h` builds the payloads, so an implementation only has to
40
* move bytes.
41
*
42
* @par Contract for implementers
43
* - The transport is driven from the frontend's task: `loop()` is where you
44
* pump your client and deliver queued callbacks.
45
* - Publishing while disconnected must fail cleanly rather than block.
46
* - Registered subscriptions must survive a reconnect. Callers subscribe once
47
* and expect the broker subscription to be reissued on the next connect.
48
*/
49
class
IMqttTransport
{
50
public
:
51
/** Payload of a message on the device command topic. */
52
using
CommandCallback
= std::function<void(
const
std::string &)>;
53
/** Broker connection state changed; the argument is the new state. */
54
using
ConnectionCallback
= std::function<void(
bool
connected
)>;
55
/** Message on a topic registered through `subscribe()`: `(topic, payload)`. */
56
using
MessageCallback
= std::function<void(
const
std::string &,
const
std::string &)>;
57
58
virtual
~IMqttTransport
() =
default
;
59
60
/**
61
* Configure and start connecting.
62
*
63
* Asynchronous: true means the client started, not that it reached the
64
* broker. Wait for the connection callback before expecting publishes to
65
* land. Calling it again reconfigures and tears down the previous client.
66
*
67
* @return false when the configuration cannot produce a client, such as an
68
* empty `EspectreDeviceConfig::mqtt_host`.
69
*/
70
virtual
bool
setup
(
const
EspectreDeviceConfig
&config) = 0;
71
/** Pump the client and dispatch callbacks. Called from the frontend loop. */
72
virtual
void
loop
() = 0;
73
/** Disconnect and release resources. Safe to repeat. */
74
virtual
void
shutdown
() = 0;
75
/** True while the broker connection is established. */
76
virtual
bool
connected
()
const
= 0;
77
/**
78
* Publish to an absolute topic.
79
*
80
* @param topic Full topic name, not a suffix.
81
* @param payload Message body, copied before returning.
82
* @param retain Ask the broker to retain the message, for state a late
83
* subscriber must still see, such as availability.
84
* @return false when disconnected or the bounded publish queue rejects the
85
* message. Published at QoS 0, so true means queued locally, not
86
* delivered to the broker.
87
*/
88
virtual
bool
publish
(
const
std::string &topic,
const
std::string &payload,
bool
retain) = 0;
89
/**
90
* Publish under this device's protocol topic prefix.
91
*
92
* The prefix comes from the `EspectreDeviceConfig` passed to `setup()`, so
93
* callers pass only the trailing segment, for example `"/telemetry"`.
94
*/
95
virtual
bool
publish_suffix
(
const
char
*suffix,
const
std::string &payload,
bool
retain) = 0;
96
/**
97
* Register a topic and its handler.
98
*
99
* Idempotent per topic: subscribing again replaces the handler. May be
100
* called before the connection is up; the subscription is issued on connect.
101
*
102
* @return false for an empty topic or an empty callback.
103
*/
104
virtual
bool
subscribe
(
const
std::string &topic,
MessageCallback
callback) = 0;
105
/** Handler for the device command topic, which the transport subscribes itself. */
106
virtual
void
set_command_callback
(
CommandCallback
callback) = 0;
107
/** Handler for connection state changes, including reconnects. */
108
virtual
void
set_connection_callback
(
ConnectionCallback
callback) = 0;
109
/** Bounded outbound queue, drop, failure, and reconnect counters. */
110
virtual
MqttTransportDiagnostics
diagnostics
()
const
{
return
{}; }
111
};
112
113
}
// namespace espectre
espectre::IMqttTransport
The MQTT client seam.
Definition
mqtt_transport.h:49
espectre::IMqttTransport::loop
virtual void loop()=0
Pump the client and dispatch callbacks.
espectre::IMqttTransport::subscribe
virtual bool subscribe(const std::string &topic, MessageCallback callback)=0
Register a topic and its handler.
espectre::IMqttTransport::shutdown
virtual void shutdown()=0
Disconnect and release resources.
espectre::IMqttTransport::set_connection_callback
virtual void set_connection_callback(ConnectionCallback callback)=0
Handler for connection state changes, including reconnects.
espectre::IMqttTransport::set_command_callback
virtual void set_command_callback(CommandCallback callback)=0
Handler for the device command topic, which the transport subscribes itself.
espectre::IMqttTransport::publish
virtual bool publish(const std::string &topic, const std::string &payload, bool retain)=0
Publish to an absolute topic.
espectre::IMqttTransport::diagnostics
virtual MqttTransportDiagnostics diagnostics() const
Bounded outbound queue, drop, failure, and reconnect counters.
Definition
mqtt_transport.h:110
espectre::IMqttTransport::ConnectionCallback
std::function< void(bool connected)> ConnectionCallback
Broker connection state changed; the argument is the new state.
Definition
mqtt_transport.h:54
espectre::IMqttTransport::setup
virtual bool setup(const EspectreDeviceConfig &config)=0
Configure and start connecting.
espectre::IMqttTransport::CommandCallback
std::function< void(const std::string &)> CommandCallback
Payload of a message on the device command topic.
Definition
mqtt_transport.h:52
espectre::IMqttTransport::~IMqttTransport
virtual ~IMqttTransport()=default
espectre::IMqttTransport::publish_suffix
virtual bool publish_suffix(const char *suffix, const std::string &payload, bool retain)=0
Publish under this device's protocol topic prefix.
espectre::IMqttTransport::connected
virtual bool connected() const =0
True while the broker connection is established.
espectre::IMqttTransport::MessageCallback
std::function< void(const std::string &, const std::string &)> MessageCallback
Message on a topic registered through subscribe(): (topic, payload).
Definition
mqtt_transport.h:56
espectre_protocol.h
Wire types and payload builders for the ESPectre Protocol.
espectre
Definition
espectre_sdk_version.h:62
espectre::EspectreDeviceConfig
Device identity and broker settings.
Definition
espectre_protocol.h:68
espectre::MqttTransportDiagnostics
Definition
mqtt_transport.h:21
espectre::MqttTransportDiagnostics::queued_publishes
size_t queued_publishes
Definition
mqtt_transport.h:24
espectre::MqttTransportDiagnostics::outbox_capacity_bytes
size_t outbox_capacity_bytes
Definition
mqtt_transport.h:23
espectre::MqttTransportDiagnostics::dropped_publishes
uint32_t dropped_publishes
Definition
mqtt_transport.h:25
espectre::MqttTransportDiagnostics::queue_capacity
size_t queue_capacity
Definition
mqtt_transport.h:22
espectre::MqttTransportDiagnostics::reconnects
uint32_t reconnects
Definition
mqtt_transport.h:27
espectre::MqttTransportDiagnostics::publish_failures
uint32_t publish_failures
Definition
mqtt_transport.h:26
src
cpp
runtime
mqtt_transport.h
Generated by
1.17.0