Skip to content

Commit 01684d4

Browse files
fixup! defining MqttClient classes
1 parent 3d7de5d commit 01684d4

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

api/net/MqttClient.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,83 @@
11
#include "MqttClient.h"
2+
#include <Arduino.h>
3+
4+
std::function<std::unique_ptr<MqttClientInterface>()>* MqttClient::_factory = nullptr;
5+
6+
7+
MqttClient::MqttClient()
8+
: impl(nullptr) {
9+
10+
}
211

312
//TODO define constant value for pimpl not instantiated
413
int MqttClient::connect(IPAddress ip, uint16_t port) {
514
return impl != nullptr? impl->connect(ip, port) : -1;
615
}
716

817
int MqttClient::connect(const char *host, uint16_t port) {
18+
checkInstance();
919
return impl != nullptr? impl->connect(host, port) : -1;
1020
}
1121

1222
void MqttClient::disconnect() {
23+
checkInstance();
1324
if(impl != nullptr) {
14-
return impl->disconnect();
25+
impl->disconnect();
1526
}
1627
}
1728

1829
uint8_t MqttClient::connected() {
30+
checkInstance();
1931
return impl != nullptr? impl->connected() : 0;
2032
}
2133

2234
MqttClient::operator bool() {
23-
// FIXME
35+
checkInstance();
36+
// FIXME check operator bool call works
2437
return impl != nullptr? impl->operator bool() : false;
2538
}
2639

2740
error_t MqttClient::subscribe(Topic t, MqttQos qos) {
41+
checkInstance();
2842
return impl != nullptr? impl->subscribe(t, qos) : -1;
2943
}
3044

3145
error_t MqttClient::publish(Topic t, uint8_t payload[], size_t size, MqttQos qos) {
46+
checkInstance();
3247
return impl != nullptr? impl->publish(t, payload, size, qos) : -1;
3348
}
3449

3550
error_t MqttClient::unsubscribe(Topic t) {
51+
checkInstance();
3652
return impl != nullptr? impl->unsubscribe(t) : -1;
3753
}
3854

3955
void MqttClient::poll() {
56+
checkInstance();
4057
if(impl != nullptr) {
4158
return impl->poll();
4259
}
4360
}
4461

4562
error_t MqttClient::ping() {
63+
checkInstance();
4664
return impl != nullptr? impl->ping() : -1;
4765
}
4866

67+
// TODO call impl set client id when instantiating the implementation
68+
void MqttClient::setClientId(char* client_id) {
69+
MqttClientInterface::setClientId(client_id);
70+
if(impl != nullptr) {
71+
impl->setClientId(client_id);
72+
}
73+
}
74+
75+
void MqttClient::checkInstance() {
76+
if(impl == nullptr && _factory != nullptr) {
77+
impl = _factory->operator()();
78+
79+
// if client id has been set before the implementation has been instantiated
80+
// set it in the implementation
81+
impl->setClientId(_clientid);
82+
}
83+
}

api/net/MqttClient.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ enum MqttQos: uint8_t {
3333
constexpr MqttQos QosDefault = MqttQos0;
3434
constexpr size_t MqttClientIdMaxLength = 256;
3535

36+
// TODO make it possible to generate the client id if none is provided during connect
37+
// + should it be performed by the derived class or by the interface?
3638
class MqttClientInterface: public arduino::ClientConnect{
3739
public:
3840
// virtual ~MqttClientInterface() = default; // not needed if deriving from ClientConnect
@@ -49,18 +51,16 @@ class MqttClientInterface: public arduino::ClientConnect{
4951

5052
// nullptr means generate it randomly
5153
// 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-
}
54+
virtual void setClientId(char* client_id = nullptr) { // TODO put this in .cpp file
55+
_clientid = client_id;
5856
}
5957

6058
// TODO Will stuff
6159
// TODO auth stuff, also related to MQTT 5.0
6260
protected:
63-
char _clientid[MqttClientIdMaxLength+1];
61+
// TODO is it better to use the one provided from outside or copy it locally?
62+
char* _clientid;
63+
// char _clientid[MqttClientIdMaxLength+1];
6464

6565
// TODO single callback for every incoming message or a callback for everything?
6666
MqttReceiveCallback _cbk;
@@ -70,6 +70,7 @@ class MqttClientInterface: public arduino::ClientConnect{
7070
// this could be generally available, outside of namespaces
7171
class MqttClient: public MqttClientInterface {
7272
public:
73+
MqttClient();
7374

7475
int connect(IPAddress ip, uint16_t port) override;
7576
int connect(const char *host, uint16_t port) override;
@@ -86,12 +87,18 @@ class MqttClient: public MqttClientInterface {
8687
error_t ping() override;
8788

8889
static void setFactory(std::function<std::unique_ptr<MqttClientInterface>()> factory) {
89-
_factory = 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;
9093
}
94+
95+
void setClientId(char* client_id = nullptr) override;
9196
protected:
92-
static std::function<std::unique_ptr<MqttClientInterface>()> _factory;
97+
static std::function<std::unique_ptr<MqttClientInterface>()> *_factory;
9398

9499
std::unique_ptr<MqttClientInterface> impl;
100+
private:
101+
inline void checkInstance();
95102
};
96103

97104
// } // }}

0 commit comments

Comments
 (0)