Skip to content

Commit eacc434

Browse files
fixup! added Mqtt client Interface definition
1 parent c49fa3d commit eacc434

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

src/MqttInterface.h

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
#pragma once
22
#include <functional>
3-
#include <memory>
4-
5-
// https://github.com/arduino-libraries/ArduinoMqttClient/
3+
#include <Client.h>
64

75
// The Idea for this section of the library is to allow the usage of different implementation for Mqtt Clients
86
// while preserving the possibility of having an Arduino standardized interface for Mqtt protocol
97
// One should implement MqttClientInterface and provide a way to instantiate the implementation
108

119
// namespace arduino { // namespace net { namespace mqtt {
10+
typedef int error_t; // TODO move this to be generally available
1211

1312
class IStream {
1413
public:
1514
virtual ~IStream() = default;
1615

1716
virtual int available() = 0;
1817
virtual int read() = 0;
19-
virtual int peek() = 0;
18+
// virtual int peek() = 0;
2019
virtual size_t readBytes(uint8_t *buffer, size_t length) = 0; // read chars from stream into buffer
2120
};
2221

23-
class OStream {
22+
class MqttOStream { // if OStream becomes available in arduino core api, we still need to extend its definition
2423
public:
25-
virtual ~OStream() = default;
24+
MqttOStream(error_t rc=0): rc(rc) {}
25+
virtual ~MqttOStream() = default;
2626

2727
virtual size_t write(uint8_t) = 0;
2828
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
2929
virtual int availableForWrite() = 0;
30+
31+
const error_t rc;
3032
};
3133

3234
using Topic = const char* const;
@@ -38,34 +40,34 @@ using MqttReceiveCallback = std::function<void(Topic, IStream&)>;
3840

3941
// TODO define callback for mqtt events. one should be the default, but the user can always change it
4042

41-
typedef int error_t; // TODO move this to be generally available
42-
4343
// 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-
};
44+
// enum mqtt_conn_return_code: error_t {
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+
constexpr error_t NotImplementedError= -0x100; // TODO define a proper value
6971

7072
enum MqttQos: uint8_t {
7173
MqttQos0 = 0, // At Most once
@@ -74,8 +76,8 @@ enum MqttQos: uint8_t {
7476
};
7577

7678
typedef uint8_t MqttPublishFlag;
77-
enum class MqttPublishFlags: MqttPublishFlag {
78-
None = 0;
79+
enum MqttPublishFlags: MqttPublishFlag {
80+
None = 0,
7981
RetainEnabled = 1,
8082
DupEnabled = 2,
8183
};
@@ -93,6 +95,7 @@ class MqttClientInterface {
9395

9496
virtual error_t connect(IPAddress ip, uint16_t port) = 0;
9597
virtual error_t connect(const char *host, uint16_t port) = 0; // TODO should host be string instead of c-string?
98+
virtual void disconnect() = 0;
9699

97100
virtual error_t subscribe(Topic t, MqttQos qos = QosDefault) = 0;
98101

@@ -107,7 +110,7 @@ class MqttClientInterface {
107110
* an mqtt Message, from the opening header to its termaination.
108111
* The concrete OStream is defined by the implementation
109112
*/
110-
virtual OStream& publish( // TODO should OStream keep publish error information?
113+
virtual MqttOStream&& publish(
111114
Topic t, MqttQos qos = QosDefault,
112115
MqttPublishFlag flags = MqttPublishFlags::None) = 0;
113116

@@ -121,16 +124,16 @@ class MqttClientInterface {
121124
virtual void setId(const char* client_id = nullptr) = 0;
122125

123126
// 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;
127+
virtual void setUsernamePassword(const char* username, const char* password=nullptr) = 0;
125128

126129
virtual void setWill(
127130
Topic willTopic, const uint8_t* will_message,
128131
size_t will_size, MqttQos qos=QosDefault,
129132
MqttPublishFlag flags = MqttPublishFlags::None) = 0;
130133

131-
virtual void setClient(Client*) = 0;
134+
virtual void setClient(arduino::Client*) = 0;
132135

133-
// may cause errors since one can easily pass a context dependent object
136+
// FIXME the following definition may cause errors since one can easily pass a context dependent object
134137
// virtual void setClient(Client&) = 0;
135138
// Could this be a better solution?
136139
// virtual void setClient(Client&&) = 0;

0 commit comments

Comments
 (0)