Skip to content

Commit baf27b9

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

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

src/MqttInterface.h

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

0 commit comments

Comments
 (0)