Skip to content

Commit 3d7de5d

Browse files
defining MqttClient classes
1 parent e960fda commit 3d7de5d

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

api/net/MqttClient.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "MqttClient.h"
2+
3+
//TODO define constant value for pimpl not instantiated
4+
int MqttClient::connect(IPAddress ip, uint16_t port) {
5+
return impl != nullptr? impl->connect(ip, port) : -1;
6+
}
7+
8+
int MqttClient::connect(const char *host, uint16_t port) {
9+
return impl != nullptr? impl->connect(host, port) : -1;
10+
}
11+
12+
void MqttClient::disconnect() {
13+
if(impl != nullptr) {
14+
return impl->disconnect();
15+
}
16+
}
17+
18+
uint8_t MqttClient::connected() {
19+
return impl != nullptr? impl->connected() : 0;
20+
}
21+
22+
MqttClient::operator bool() {
23+
// FIXME
24+
return impl != nullptr? impl->operator bool() : false;
25+
}
26+
27+
error_t MqttClient::subscribe(Topic t, MqttQos qos) {
28+
return impl != nullptr? impl->subscribe(t, qos) : -1;
29+
}
30+
31+
error_t MqttClient::publish(Topic t, uint8_t payload[], size_t size, MqttQos qos) {
32+
return impl != nullptr? impl->publish(t, payload, size, qos) : -1;
33+
}
34+
35+
error_t MqttClient::unsubscribe(Topic t) {
36+
return impl != nullptr? impl->unsubscribe(t) : -1;
37+
}
38+
39+
void MqttClient::poll() {
40+
if(impl != nullptr) {
41+
return impl->poll();
42+
}
43+
}
44+
45+
error_t MqttClient::ping() {
46+
return impl != nullptr? impl->ping() : -1;
47+
}
48+

api/net/MqttClient.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma once
2+
#include <functional>
3+
#include <memory>
4+
5+
#include <api/Client.h>
6+
7+
// https://github.com/arduino-libraries/ArduinoMqttClient/
8+
9+
// The Idea for this section of the library is to allow the usage of different implementation for Mqtt Clients
10+
// while preserving the possibility of having an Arduino standardized interface for Mqtt protocol
11+
// One should implement MqttClientInterface and provide a way to instantiate the implementation
12+
13+
// namespace arduino { // namespace net { namespace mqtt {
14+
15+
using Topic = const char* const;
16+
17+
// for incoming published messages
18+
// TODO double check with typename
19+
using MqttReceiveCallback = std::function<void(Topic, const uint8_t[], size_t)>;
20+
21+
// TODO define callback for mqtt events. one should be the default, but the user can always change it
22+
23+
typedef int error_t; // TODO move this to be generally available
24+
25+
enum MqttQos: uint8_t {
26+
MqttQos0 = 0, // At Most once
27+
MqttQos1 = 1, // At least once
28+
MqttQos2 = 2, // Exactly once
29+
};
30+
31+
// TODO define mqtt version
32+
33+
constexpr MqttQos QosDefault = MqttQos0;
34+
constexpr size_t MqttClientIdMaxLength = 256;
35+
36+
class MqttClientInterface: public arduino::ClientConnect{
37+
public:
38+
// virtual ~MqttClientInterface() = default; // not needed if deriving from ClientConnect
39+
40+
virtual error_t subscribe(Topic t, MqttQos qos = QosDefault) = 0;
41+
virtual error_t publish(Topic t, uint8_t payload[], size_t size, MqttQos qos = QosDefault) = 0;
42+
// TODO define a publish that can stream a message
43+
44+
virtual error_t unsubscribe(Topic t) = 0;
45+
virtual void poll() = 0;
46+
virtual error_t ping() = 0;
47+
48+
virtual void setReceiveCallback(MqttReceiveCallback cbk) { _cbk = cbk; }
49+
50+
// nullptr means generate it randomly
51+
// TODO should this be pure virtual?
52+
virtual void setClientId(const char* const clientid = nullptr) { // TODO put this in .cpp file
53+
if(clientid == nullptr) {
54+
// TODO generate it randomly
55+
} else {
56+
strncpy(_clientid, clientid, MqttClientIdMaxLength);
57+
}
58+
}
59+
60+
// TODO Will stuff
61+
// TODO auth stuff, also related to MQTT 5.0
62+
protected:
63+
char _clientid[MqttClientIdMaxLength+1];
64+
65+
// TODO single callback for every incoming message or a callback for everything?
66+
MqttReceiveCallback _cbk;
67+
};
68+
69+
70+
// this could be generally available, outside of namespaces
71+
class MqttClient: public MqttClientInterface {
72+
public:
73+
74+
int connect(IPAddress ip, uint16_t port) override;
75+
int connect(const char *host, uint16_t port) override;
76+
void disconnect() override;
77+
78+
uint8_t connected() override;
79+
operator bool() override;
80+
81+
//TODO wrap method
82+
error_t subscribe(Topic t, MqttQos qos = QosDefault) override;
83+
error_t publish(Topic t, uint8_t[], size_t, MqttQos qos = QosDefault) override;
84+
error_t unsubscribe(Topic t) override;
85+
void poll() override;
86+
error_t ping() override;
87+
88+
static void setFactory(std::function<std::unique_ptr<MqttClientInterface>()> factory) {
89+
_factory = factory;
90+
}
91+
protected:
92+
static std::function<std::unique_ptr<MqttClientInterface>()> _factory;
93+
94+
std::unique_ptr<MqttClientInterface> impl;
95+
};
96+
97+
// } // }}

0 commit comments

Comments
 (0)