@@ -33,6 +33,8 @@ enum MqttQos: uint8_t {
33
33
constexpr MqttQos QosDefault = MqttQos0;
34
34
constexpr size_t MqttClientIdMaxLength = 256 ;
35
35
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?
36
38
class MqttClientInterface : public arduino ::ClientConnect{
37
39
public:
38
40
// virtual ~MqttClientInterface() = default; // not needed if deriving from ClientConnect
@@ -49,18 +51,16 @@ class MqttClientInterface: public arduino::ClientConnect{
49
51
50
52
// nullptr means generate it randomly
51
53
// 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;
58
56
}
59
57
60
58
// TODO Will stuff
61
59
// TODO auth stuff, also related to MQTT 5.0
62
60
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];
64
64
65
65
// TODO single callback for every incoming message or a callback for everything?
66
66
MqttReceiveCallback _cbk;
@@ -70,6 +70,7 @@ class MqttClientInterface: public arduino::ClientConnect{
70
70
// this could be generally available, outside of namespaces
71
71
class MqttClient : public MqttClientInterface {
72
72
public:
73
+ MqttClient ();
73
74
74
75
int connect (IPAddress ip, uint16_t port) override ;
75
76
int connect (const char *host, uint16_t port) override ;
@@ -86,12 +87,18 @@ class MqttClient: public MqttClientInterface {
86
87
error_t ping () override ;
87
88
88
89
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;
90
93
}
94
+
95
+ void setClientId (char * client_id = nullptr ) override ;
91
96
protected:
92
- static std::function<std::unique_ptr<MqttClientInterface>()> _factory;
97
+ static std::function<std::unique_ptr<MqttClientInterface>()> * _factory;
93
98
94
99
std::unique_ptr<MqttClientInterface> impl;
100
+ private:
101
+ inline void checkInstance ();
95
102
};
96
103
97
104
// } // }}
0 commit comments