|
| 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