1
1
#pragma once
2
2
#include < functional>
3
- #include < memory>
4
-
5
- // https://github.com/arduino-libraries/ArduinoMqttClient/
3
+ #include < Client.h>
6
4
7
5
// The Idea for this section of the library is to allow the usage of different implementation for Mqtt Clients
8
6
// while preserving the possibility of having an Arduino standardized interface for Mqtt protocol
9
7
// One should implement MqttClientInterface and provide a way to instantiate the implementation
10
8
11
9
// namespace arduino { // namespace net { namespace mqtt {
10
+ typedef int error_t ; // TODO move this to be generally available
12
11
13
12
class IStream {
14
13
public:
15
14
virtual ~IStream () = default ;
16
15
17
16
virtual int available () = 0;
18
17
virtual int read () = 0;
19
- virtual int peek () = 0;
18
+ // virtual int peek() = 0;
20
19
virtual size_t readBytes (uint8_t *buffer, size_t length) = 0; // read chars from stream into buffer
21
20
};
22
21
23
- class OStream {
22
+ class MqttOStream { // if OStream becomes available in arduino core api, we still need to extend its definition
24
23
public:
25
- virtual ~OStream () = default ;
24
+ MqttOStream (error_t rc=0 ): rc(rc) {}
25
+ virtual ~MqttOStream () = default ;
26
26
27
27
virtual size_t write (uint8_t ) = 0;
28
28
virtual size_t write (const uint8_t *buffer, size_t size) = 0;
29
29
virtual int availableForWrite () = 0;
30
+
31
+ const error_t rc;
30
32
};
31
33
32
34
using Topic = const char * const ;
@@ -38,34 +40,34 @@ using MqttReceiveCallback = std::function<void(Topic, IStream&)>;
38
40
39
41
// TODO define callback for mqtt events. one should be the default, but the user can always change it
40
42
41
- typedef int error_t ; // TODO move this to be generally available
42
-
43
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
- };
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
69
71
70
72
enum MqttQos: uint8_t {
71
73
MqttQos0 = 0 , // At Most once
@@ -74,8 +76,8 @@ enum MqttQos: uint8_t {
74
76
};
75
77
76
78
typedef uint8_t MqttPublishFlag;
77
- enum class MqttPublishFlags : MqttPublishFlag {
78
- None = 0 ;
79
+ enum MqttPublishFlags: MqttPublishFlag {
80
+ None = 0 ,
79
81
RetainEnabled = 1 ,
80
82
DupEnabled = 2 ,
81
83
};
@@ -93,6 +95,7 @@ class MqttClientInterface {
93
95
94
96
virtual error_t connect (IPAddress ip, uint16_t port) = 0;
95
97
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;
96
99
97
100
virtual error_t subscribe (Topic t, MqttQos qos = QosDefault) = 0;
98
101
@@ -107,7 +110,7 @@ class MqttClientInterface {
107
110
* an mqtt Message, from the opening header to its termaination.
108
111
* The concrete OStream is defined by the implementation
109
112
*/
110
- virtual OStream& publish ( // TODO should OStream keep publish error information?
113
+ virtual MqttOStream&& publish(
111
114
Topic t, MqttQos qos = QosDefault,
112
115
MqttPublishFlag flags = MqttPublishFlags::None) = 0 ;
113
116
@@ -121,16 +124,16 @@ class MqttClientInterface {
121
124
virtual void setId (const char * client_id = nullptr ) = 0;
122
125
123
126
// 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;
125
128
126
129
virtual void setWill (
127
130
Topic willTopic, const uint8_t * will_message,
128
131
size_t will_size, MqttQos qos=QosDefault,
129
132
MqttPublishFlag flags = MqttPublishFlags::None) = 0;
130
133
131
- virtual void setClient (Client*) = 0;
134
+ virtual void setClient (arduino:: Client*) = 0;
132
135
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
134
137
// virtual void setClient(Client&) = 0;
135
138
// Could this be a better solution?
136
139
// virtual void setClient(Client&&) = 0;
0 commit comments