Skip to content

Commit e6846ec

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

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

api/net/MqttClient.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,38 @@ error_t MqttClient::ping() {
6868
return impl != nullptr? impl->ping() : -1;
6969
}
7070

71-
// TODO call impl set client id when instantiating the implementation
72-
void MqttClient::setClientId(char* client_id) {
73-
MqttClientInterface::setClientId(client_id);
71+
void MqttClient::setClientId(const char* client_id) {
72+
checkInstance();
7473
if(impl != nullptr) {
7574
impl->setClientId(client_id);
7675
}
7776
}
7877

78+
void MqttClient::setAuth(const char* username, const char* password) {
79+
checkInstance();
80+
if(impl != nullptr) {
81+
impl->setAuth(username, password);
82+
}
83+
}
84+
85+
void MqttClient::setWill(Topic willTopic, const uint8_t* will_message, size_t will_size) {
86+
checkInstance();
87+
if(impl != nullptr) {
88+
impl->setWill(willTopic, will_message, will_size);
89+
}
90+
}
91+
92+
void MqttClient::setReceiveCallback(MqttReceiveCallback cbk) {
93+
checkInstance();
94+
if(impl != nullptr) {
95+
impl->setReceiveCallback(cbk);
96+
}
97+
}
98+
7999
void MqttClient::checkInstance() {
80100
if(impl == nullptr && _factory != nullptr) {
81101
// impl = _factory();
82102
impl = _factory->operator()();
83-
84-
// if client id has been set before the implementation has been instantiated
85-
// set it in the implementation
86-
impl->setClientId(_clientid);
87-
impl->_cbk = _cbk;
88103
}
89104
}
90105

api/net/MqttClient.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,18 @@ class MqttClientInterface: public arduino::ClientConnect{
4949
virtual void poll() = 0;
5050
virtual error_t ping() = 0;
5151

52-
virtual void setReceiveCallback(MqttReceiveCallback cbk) { _cbk = cbk; }
52+
// TODO make this pure virtual?
53+
virtual void setReceiveCallback(MqttReceiveCallback cbk) = 0;
5354

5455
// nullptr means generate it randomly
55-
// TODO should this be pure virtual?
56-
virtual void setClientId(char* client_id = nullptr) { // TODO put this in .cpp file
57-
_clientid = client_id;
58-
}
56+
virtual void setClientId(const char* client_id = nullptr) = 0;
5957

60-
// TODO Will stuff
61-
// TODO auth stuff, also related to MQTT 5.0
58+
// password may be null, if username is null password won't be used
59+
virtual void setAuth(const char* username, const char* password=nullptr) = 0;
6260

63-
// TODO single callback for every incoming message or a callback for everything?
64-
// FIXME make this private
65-
MqttReceiveCallback _cbk;
61+
virtual void setWill(Topic willTopic, const uint8_t* will_message, size_t will_size) = 0;
6662

67-
protected:
68-
// TODO is it better to use the one provided from outside or copy it locally?
69-
char* _clientid;
70-
// char _clientid[MqttClientIdMaxLength+1];
63+
// TODO MQTT 5.0 stuff
7164
};
7265

7366

@@ -90,9 +83,14 @@ class MqttClient: public MqttClientInterface {
9083
void poll() override;
9184
error_t ping() override;
9285

86+
void setReceiveCallback(MqttReceiveCallback cbk) override;
87+
9388
// FIXME use a & or && parameter
9489
static void setFactory(std::function<std::unique_ptr<MqttClientInterface>()> factory);
95-
void setClientId(char* client_id = nullptr) override;
90+
void setClientId(const char* client_id = nullptr) override;
91+
92+
void setAuth(const char* username, const char* password=nullptr) override;
93+
void setWill(Topic willTopic, const uint8_t* will_message, size_t will_size) override;
9694
protected:
9795
// static std::function<std::unique_ptr<MqttClientInterface>()> _factory;
9896

0 commit comments

Comments
 (0)