@@ -33,6 +33,8 @@ enum MqttQos: uint8_t {
3333constexpr MqttQos QosDefault = MqttQos0;
3434constexpr 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?
3638class MqttClientInterface : public arduino ::ClientConnect{
3739public:
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
6260protected:
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
7171class MqttClient : public MqttClientInterface {
7272public:
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 ;
9196protected:
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