Skip to content

Commit 572e4c1

Browse files
fixup! defining MqttClient classes
1 parent 01684d4 commit 572e4c1

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

api/net/MqttClient.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "MqttClient.h"
22
#include <Arduino.h>
33

4-
std::function<std::unique_ptr<MqttClientInterface>()>* MqttClient::_factory = nullptr;
4+
// std::function<std::unique_ptr<MqttClientInterface>()>* MqttClient::_factory = nullptr;
5+
// std::function<std::unique_ptr<MqttClientInterface>()> MqttClient::_factory;
6+
static std::function<std::unique_ptr<MqttClientInterface>()>* _factory=nullptr;
57

68

79
MqttClient::MqttClient()
@@ -11,6 +13,7 @@ MqttClient::MqttClient()
1113

1214
//TODO define constant value for pimpl not instantiated
1315
int MqttClient::connect(IPAddress ip, uint16_t port) {
16+
checkInstance();
1417
return impl != nullptr? impl->connect(ip, port) : -1;
1518
}
1619

@@ -24,6 +27,7 @@ void MqttClient::disconnect() {
2427
if(impl != nullptr) {
2528
impl->disconnect();
2629
}
30+
// TODO we may delete the internal client
2731
}
2832

2933
uint8_t MqttClient::connected() {
@@ -74,10 +78,19 @@ void MqttClient::setClientId(char* client_id) {
7478

7579
void MqttClient::checkInstance() {
7680
if(impl == nullptr && _factory != nullptr) {
81+
// impl = _factory();
7782
impl = _factory->operator()();
7883

7984
// if client id has been set before the implementation has been instantiated
8085
// set it in the implementation
8186
impl->setClientId(_clientid);
87+
impl->_cbk = _cbk;
8288
}
8389
}
90+
91+
void MqttClient::setFactory(std::function<std::unique_ptr<MqttClientInterface>()> factory) {
92+
// FIXME find a better way to solve constructor call order
93+
static std::function<std::unique_ptr<MqttClientInterface>()> f = factory;
94+
_factory = &f;
95+
// _factory = factory;
96+
}

api/net/MqttClient.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ using Topic = const char* const;
1616

1717
// for incoming published messages
1818
// TODO double check with typename
19-
using MqttReceiveCallback = std::function<void(Topic, const uint8_t[], size_t)>;
19+
// using MqttReceiveCallback = std::function<void(Topic, const uint8_t[], size_t)>;
20+
using MqttReceiveCallback = std::function<void(Topic, Stream&)>;
2021

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

@@ -31,7 +32,8 @@ enum MqttQos: uint8_t {
3132
// TODO define mqtt version
3233

3334
constexpr MqttQos QosDefault = MqttQos0;
34-
constexpr size_t MqttClientIdMaxLength = 256;
35+
// constexpr size_t MqttClientIdMaxLength = 256;
36+
constexpr size_t MqttClientIdMaxLength = 40;
3537

3638
// TODO make it possible to generate the client id if none is provided during connect
3739
// + should it be performed by the derived class or by the interface?
@@ -57,13 +59,15 @@ class MqttClientInterface: public arduino::ClientConnect{
5759

5860
// TODO Will stuff
5961
// TODO auth stuff, also related to MQTT 5.0
62+
63+
// TODO single callback for every incoming message or a callback for everything?
64+
// FIXME make this private
65+
MqttReceiveCallback _cbk;
66+
6067
protected:
6168
// TODO is it better to use the one provided from outside or copy it locally?
6269
char* _clientid;
6370
// char _clientid[MqttClientIdMaxLength+1];
64-
65-
// TODO single callback for every incoming message or a callback for everything?
66-
MqttReceiveCallback _cbk;
6771
};
6872

6973

@@ -86,15 +90,11 @@ class MqttClient: public MqttClientInterface {
8690
void poll() override;
8791
error_t ping() override;
8892

89-
static void setFactory(std::function<std::unique_ptr<MqttClientInterface>()> factory) {
90-
// FIXME find a better way to solve constructor call order
91-
static std::function<std::unique_ptr<MqttClientInterface>()> f = factory;
92-
_factory = &f;
93-
}
94-
93+
// FIXME use a & or && parameter
94+
static void setFactory(std::function<std::unique_ptr<MqttClientInterface>()> factory);
9595
void setClientId(char* client_id = nullptr) override;
9696
protected:
97-
static std::function<std::unique_ptr<MqttClientInterface>()> *_factory;
97+
// static std::function<std::unique_ptr<MqttClientInterface>()> _factory;
9898

9999
std::unique_ptr<MqttClientInterface> impl;
100100
private:

0 commit comments

Comments
 (0)