@@ -171,9 +171,9 @@ int MqttClient::messageRetain() const
171
171
return -1 ;
172
172
}
173
173
174
- int MqttClient::beginMessage (const char * topic, unsigned long size, bool retain, uint8_t qos, bool dup)
174
+ int MqttClient::beginMessage (const String& topic, unsigned long size, bool retain, uint8_t qos, bool dup)
175
175
{
176
- _txMessageTopic = topic;
176
+ _txMessageTopic = topic. c_str () ;
177
177
_txMessageRetain = retain;
178
178
_txMessageQoS = qos;
179
179
_txMessageDup = dup;
@@ -191,19 +191,9 @@ int MqttClient::beginMessage(const char* topic, unsigned long size, bool retain,
191
191
return 1 ;
192
192
}
193
193
194
- int MqttClient::beginMessage (const String& topic, unsigned long size, bool retain, uint8_t qos, bool dup)
195
- {
196
- return beginMessage (topic.c_str (), size, retain, qos, dup);
197
- }
198
-
199
- int MqttClient::beginMessage (const char * topic, bool retain, uint8_t qos, bool dup)
200
- {
201
- return beginMessage (topic, 0xffffffffL , retain, qos, dup);
202
- }
203
-
204
194
int MqttClient::beginMessage (const String& topic, bool retain, uint8_t qos, bool dup)
205
195
{
206
- return beginMessage (topic. c_str () , retain, qos, dup);
196
+ return beginMessage (topic, 0xffffffffL , retain, qos, dup);
207
197
}
208
198
209
199
int MqttClient::endMessage ()
@@ -286,17 +276,32 @@ int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, u
286
276
287
277
int MqttClient::beginWill (const String& topic, unsigned short size, bool retain, uint8_t qos)
288
278
{
289
- return beginWill ( topic.c_str (), size, retain, qos );
290
- }
279
+ int topicLength = topic.length ( );
280
+ size_t willLength = ( 2 + topicLength + 2 + size);
291
281
292
- int MqttClient::beginWill (const char * topic, bool retain, uint8_t qos)
293
- {
294
- return beginWill (topic, _tx_payload_buffer_size, retain, qos);
282
+ if (qos > 2 ) {
283
+ // invalid QoS
284
+ }
285
+
286
+ _willBuffer = (uint8_t *)realloc (_willBuffer, willLength);
287
+
288
+ _txBuffer = _willBuffer;
289
+ _txBufferIndex = 0 ;
290
+ writeString (topic.c_str (), topic.length ());
291
+ write16 (0 ); // dummy size for now
292
+ _willMessageIndex = _txBufferIndex;
293
+
294
+ _willFlags = (qos << 3 ) | 0x04 ;
295
+ if (retain) {
296
+ _willFlags |= 0x20 ;
297
+ }
298
+
299
+ return 0 ;
295
300
}
296
301
297
302
int MqttClient::beginWill (const String& topic, bool retain, uint8_t qos)
298
303
{
299
- return beginWill (topic. c_str () , retain, qos);
304
+ return beginWill (topic, _tx_payload_buffer_size , retain, qos);
300
305
}
301
306
302
307
int MqttClient::endWill ()
@@ -314,9 +319,10 @@ int MqttClient::endWill()
314
319
return 1 ;
315
320
}
316
321
317
- int MqttClient::subscribe (const char * topic, uint8_t qos)
322
+ int MqttClient::subscribe (const String& topic, uint8_t qos)
323
+
318
324
{
319
- int topicLength = strlen ( topic);
325
+ int topicLength = topic. length ( );
320
326
int remainingLength = topicLength + 5 ;
321
327
322
328
if (qos > 2 ) {
@@ -334,7 +340,7 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)
334
340
335
341
beginPacket (MQTT_SUBSCRIBE, 0x02 , remainingLength, packetBuffer);
336
342
write16 (_txPacketId);
337
- writeString (topic, topicLength);
343
+ writeString (topic. c_str () , topicLength);
338
344
write8 (qos);
339
345
340
346
if (!endPacket ()) {
@@ -362,14 +368,9 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)
362
368
return 0 ;
363
369
}
364
370
365
- int MqttClient::subscribe (const String& topic, uint8_t qos)
366
- {
367
- return subscribe (topic.c_str (), qos);
368
- }
369
-
370
- int MqttClient::unsubscribe (const char * topic)
371
+ int MqttClient::unsubscribe (const String& topic)
371
372
{
372
- int topicLength = strlen ( topic);
373
+ int topicLength = topic. length ( );
373
374
int remainingLength = topicLength + 4 ;
374
375
375
376
_txPacketId++;
@@ -382,7 +383,7 @@ int MqttClient::unsubscribe(const char* topic)
382
383
383
384
beginPacket (MQTT_UNSUBSCRIBE, 0x02 , remainingLength, packetBuffer);
384
385
write16 (_txPacketId);
385
- writeString (topic, topicLength);
386
+ writeString (topic. c_str () , topicLength);
386
387
387
388
if (!endPacket ()) {
388
389
stop ();
@@ -406,11 +407,6 @@ int MqttClient::unsubscribe(const char* topic)
406
407
return 0 ;
407
408
}
408
409
409
- int MqttClient::unsubscribe (const String& topic)
410
- {
411
- return unsubscribe (topic.c_str ());
412
- }
413
-
414
410
void MqttClient::poll ()
415
411
{
416
412
if (clientAvailable () == 0 && !clientConnected ()) {
@@ -775,22 +771,11 @@ MqttClient::operator bool()
775
771
return true ;
776
772
}
777
773
778
- void MqttClient::setId (const char * id)
779
- {
780
- _id = id;
781
- }
782
-
783
774
void MqttClient::setId (const String& id)
784
775
{
785
776
_id = id;
786
777
}
787
778
788
- void MqttClient::setUsernamePassword (const char * username, const char * password)
789
- {
790
- _username = username;
791
- _password = password;
792
- }
793
-
794
779
void MqttClient::setUsernamePassword (const String& username, const String& password)
795
780
{
796
781
_username = username;
0 commit comments