Skip to content

Commit e655ab4

Browse files
added Mqtt client Interface definition
1 parent d2f5935 commit e655ab4

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

src/MqttInterface.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#pragma once
2+
#include <functional>
3+
#include <memory>
4+
5+
// https://github.com/arduino-libraries/ArduinoMqttClient/
6+
7+
// The Idea for this section of the library is to allow the usage of different implementation for Mqtt Clients
8+
// while preserving the possibility of having an Arduino standardized interface for Mqtt protocol
9+
// One should implement MqttClientInterface and provide a way to instantiate the implementation
10+
11+
// namespace arduino { // namespace net { namespace mqtt {
12+
13+
class IStream {
14+
public:
15+
virtual ~IStream() = default;
16+
17+
virtual int available() = 0;
18+
virtual int read() = 0;
19+
virtual int peek() = 0;
20+
virtual size_t readBytes(uint8_t *buffer, size_t length) = 0; // read chars from stream into buffer
21+
};
22+
23+
class OStream {
24+
public:
25+
virtual ~OStream() = default;
26+
27+
virtual size_t write(uint8_t) = 0;
28+
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
29+
virtual int availableForWrite() = 0;
30+
};
31+
32+
using Topic = const char* const;
33+
34+
// for incoming published messages
35+
using MqttReceiveCallback = std::function<void(Topic, IStream&)>;
36+
37+
// TODO MQTT 5.0 stuff
38+
39+
// TODO define callback for mqtt events. one should be the default, but the user can always change it
40+
41+
typedef int error_t; // TODO move this to be generally available
42+
43+
// copied from zephyr
44+
enum mqtt_conn_return_code {
45+
/** Connection accepted. */
46+
MQTT_CONNECTION_ACCEPTED = 0x00,
47+
48+
/** The Server does not support the level of the MQTT protocol
49+
* requested by the Client.
50+
*/
51+
MQTT_UNACCEPTABLE_PROTOCOL_VERSION = 0x01,
52+
53+
/** The Client identifier is correct UTF-8 but not allowed by the
54+
* Server.
55+
*/
56+
MQTT_IDENTIFIER_REJECTED = 0x02,
57+
58+
/** The Network Connection has been made but the MQTT service is
59+
* unavailable.
60+
*/
61+
MQTT_SERVER_UNAVAILABLE = 0x03,
62+
63+
/** The data in the user name or password is malformed. */
64+
MQTT_BAD_USER_NAME_OR_PASSWORD = 0x04,
65+
66+
/** The Client is not authorized to connect. */
67+
MQTT_NOT_AUTHORIZED = 0x05
68+
};
69+
70+
enum MqttQos: uint8_t {
71+
MqttQos0 = 0, // At Most once
72+
MqttQos1 = 1, // At least once
73+
MqttQos2 = 2, // Exactly once
74+
};
75+
76+
typedef uint8_t MqttPublishFlag;
77+
enum class MqttPublishFlags: MqttPublishFlag {
78+
None = 0;
79+
RetainEnabled = 1,
80+
DupEnabled = 2,
81+
};
82+
83+
// TODO define mqtt version
84+
85+
constexpr MqttQos QosDefault = MqttQos0;
86+
// constexpr size_t MqttClientIdMaxLength = 256;
87+
constexpr size_t MqttClientIdMaxLength = 40;
88+
89+
// TODO it shouldn't be called client, since it is not an arduino client
90+
class MqttClientInterface {
91+
public:
92+
virtual ~MqttClientInterface() = default;
93+
94+
virtual error_t connect(IPAddress ip, uint16_t port) = 0;
95+
virtual error_t connect(const char *host, uint16_t port) = 0; // TODO should host be string instead of c-string?
96+
97+
virtual error_t subscribe(Topic t, MqttQos qos = QosDefault) = 0;
98+
99+
virtual error_t publish(
100+
Topic t, uint8_t payload[],
101+
size_t size, MqttQos qos = QosDefault,
102+
MqttPublishFlag flags = MqttPublishFlags::None) = 0;
103+
104+
/**
105+
* Publish method for publishing messages in "streaming mode", meaning that
106+
* they can outsize the available ram. The OStream returned handles the lifecycle of
107+
* an mqtt Message, from the opening header to its termaination.
108+
* The concrete OStream is defined by the implementation
109+
*/
110+
virtual OStream& publish( // TODO should OStream keep publish error information?
111+
Topic t, MqttQos qos = QosDefault,
112+
MqttPublishFlag flags = MqttPublishFlags::None) = 0;
113+
114+
virtual error_t unsubscribe(Topic t) = 0;
115+
virtual void poll() = 0;
116+
virtual error_t ping() = 0;
117+
118+
virtual void setReceiveCallback(MqttReceiveCallback cbk) = 0;
119+
120+
// nullptr means generate it randomly
121+
virtual void setId(const char* client_id = nullptr) = 0;
122+
123+
// password may be null, if username is null password won't be used
124+
virtual void setAuth(const char* username, const char* password=nullptr) = 0;
125+
126+
virtual void setWill(
127+
Topic willTopic, const uint8_t* will_message,
128+
size_t will_size, MqttQos qos=QosDefault,
129+
MqttPublishFlag flags = MqttPublishFlags::None) = 0;
130+
131+
virtual void setClient(Client*) = 0;
132+
133+
// may cause errors since one can easily pass a context dependent object
134+
// virtual void setClient(Client&) = 0;
135+
// Could this be a better solution?
136+
// virtual void setClient(Client&&) = 0;
137+
};

0 commit comments

Comments
 (0)