Skip to content

Commit d28f08b

Browse files
committed
retain will topic and payload
1 parent 4d9e708 commit d28f08b

File tree

1 file changed

+61
-18
lines changed

1 file changed

+61
-18
lines changed

src/MQTTClient.h

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,39 @@ class MQTTClient {
7070
Client *netClient = nullptr;
7171
const char *hostname = nullptr;
7272
int port = 0;
73-
lwmqtt_will_t will = lwmqtt_default_will;
74-
bool hasWill = false;
73+
lwmqtt_will_t *will = nullptr;
7574
MQTTClientCallback callback;
7675

7776
lwmqtt_arduino_network_t network = {nullptr};
7877
lwmqtt_arduino_timer_t timer1 = {0};
7978
lwmqtt_arduino_timer_t timer2 = {0};
80-
lwmqtt_client_t client;
79+
lwmqtt_client_t client = {0};
8180

8281
bool _connected = false;
8382
lwmqtt_return_code_t _returnCode = (lwmqtt_return_code_t)0;
8483
lwmqtt_err_t _lastError = (lwmqtt_err_t)0;
8584

8685
public:
8786
explicit MQTTClient(int bufSize = 128) {
88-
memset(&client, 0, sizeof(client));
87+
// reset client
88+
memset(&this->client, 0, sizeof(lwmqtt_client_t));
89+
90+
// allocate buffers
8991
this->bufSize = (size_t)bufSize;
9092
this->readBuf = (uint8_t *)malloc((size_t)bufSize + 1);
9193
this->writeBuf = (uint8_t *)malloc((size_t)bufSize);
9294
}
9395

9496
~MQTTClient() {
97+
// free will
98+
this->clearWill();
99+
100+
// free hostname
101+
if (this->hostname != nullptr) {
102+
free((void *)this->hostname);
103+
}
104+
105+
// free buffers
95106
free(this->readBuf);
96107
free(this->writeBuf);
97108
}
@@ -136,7 +147,7 @@ class MQTTClient {
136147

137148
void setHost(const char hostname[], int port) {
138149
// free hostname if set
139-
if(this->hostname != nullptr) {
150+
if (this->hostname != nullptr) {
140151
free((void *)this->hostname);
141152
}
142153

@@ -150,16 +161,54 @@ class MQTTClient {
150161
void setWill(const char topic[], const char payload[]) { this->setWill(topic, payload, false, 0); }
151162

152163
void setWill(const char topic[], const char payload[], bool retained, int qos) {
153-
this->hasWill = true;
154-
this->will.topic = lwmqtt_string(topic);
155-
this->will.payload = lwmqtt_string(payload);
156-
this->will.retained = retained;
157-
this->will.qos = (lwmqtt_qos_t)qos;
164+
// return if topic is missing
165+
if (topic == nullptr || strlen(topic) == 0) {
166+
return;
167+
}
168+
169+
// clear existing will
170+
this->clearWill();
171+
172+
// allocate will
173+
this->will = (lwmqtt_will_t *)malloc(sizeof(lwmqtt_will_t));
174+
memset(this->will, 0, sizeof(lwmqtt_will_t));
175+
176+
// set topic
177+
this->will->topic = lwmqtt_string(strdup(topic));
178+
179+
// set payload if available
180+
if (payload != nullptr && strlen(payload) > 0) {
181+
this->will->payload = lwmqtt_string(strdup(payload));
182+
}
183+
184+
// set flags
185+
this->will->retained = retained;
186+
this->will->qos = (lwmqtt_qos_t)qos;
158187
}
159188

160-
void clearWill() { this->hasWill = false; }
189+
void clearWill() {
190+
// return if not set
191+
if (this->will == nullptr) {
192+
return;
193+
}
194+
195+
// free payload if set
196+
if (this->will->payload.len > 0) {
197+
free(this->will->payload.data);
198+
}
199+
200+
// free topic if set
201+
if (this->will->topic.len > 0) {
202+
free(this->will->topic.data);
203+
}
204+
205+
// free will
206+
free(this->will);
207+
this->will = nullptr;
208+
}
161209

162210
void setOptions(int keepAlive, bool cleanSession, int timeout) {
211+
// set new options
163212
this->keepAlive = (uint16_t)keepAlive;
164213
this->cleanSession = cleanSession;
165214
this->timeout = (uint32_t)timeout;
@@ -198,14 +247,8 @@ class MQTTClient {
198247
}
199248
}
200249

201-
// prepare will reference
202-
lwmqtt_will_t *will = nullptr;
203-
if (this->hasWill) {
204-
will = &this->will;
205-
}
206-
207250
// connect to broker
208-
this->_lastError = lwmqtt_connect(&this->client, options, will, &this->_returnCode, this->timeout);
251+
this->_lastError = lwmqtt_connect(&this->client, options, this->will, &this->_returnCode, this->timeout);
209252
if (this->_lastError != LWMQTT_SUCCESS) {
210253
// close connection
211254
this->close();

0 commit comments

Comments
 (0)