@@ -171,9 +171,9 @@ int MqttClient::messageRetain() const
171171 return -1 ;
172172}
173173
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)
175175{
176- _txMessageTopic = topic;
176+ _txMessageTopic = topic. c_str () ;
177177 _txMessageRetain = retain;
178178 _txMessageQoS = qos;
179179 _txMessageDup = dup;
@@ -191,19 +191,9 @@ int MqttClient::beginMessage(const char* topic, unsigned long size, bool retain,
191191 return 1 ;
192192}
193193
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-
204194int MqttClient::beginMessage (const String& topic, bool retain, uint8_t qos, bool dup)
205195{
206- return beginMessage (topic. c_str () , retain, qos, dup);
196+ return beginMessage (topic, 0xffffffffL , retain, qos, dup);
207197}
208198
209199int MqttClient::endMessage ()
@@ -286,17 +276,32 @@ int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, u
286276
287277int MqttClient::beginWill (const String& topic, unsigned short size, bool retain, uint8_t qos)
288278{
289- return beginWill ( topic.c_str (), size, retain, qos );
290- }
279+ int topicLength = topic.length ( );
280+ size_t willLength = ( 2 + topicLength + 2 + size);
291281
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 ;
295300}
296301
297302int MqttClient::beginWill (const String& topic, bool retain, uint8_t qos)
298303{
299- return beginWill (topic. c_str () , retain, qos);
304+ return beginWill (topic, _tx_payload_buffer_size , retain, qos);
300305}
301306
302307int MqttClient::endWill ()
@@ -314,9 +319,10 @@ int MqttClient::endWill()
314319 return 1 ;
315320}
316321
317- int MqttClient::subscribe (const char * topic, uint8_t qos)
322+ int MqttClient::subscribe (const String& topic, uint8_t qos)
323+
318324{
319- int topicLength = strlen ( topic);
325+ int topicLength = topic. length ( );
320326 int remainingLength = topicLength + 5 ;
321327
322328 if (qos > 2 ) {
@@ -334,7 +340,7 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)
334340
335341 beginPacket (MQTT_SUBSCRIBE, 0x02 , remainingLength, packetBuffer);
336342 write16 (_txPacketId);
337- writeString (topic, topicLength);
343+ writeString (topic. c_str () , topicLength);
338344 write8 (qos);
339345
340346 if (!endPacket ()) {
@@ -362,14 +368,9 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)
362368 return 0 ;
363369}
364370
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)
371372{
372- int topicLength = strlen ( topic);
373+ int topicLength = topic. length ( );
373374 int remainingLength = topicLength + 4 ;
374375
375376 _txPacketId++;
@@ -382,7 +383,7 @@ int MqttClient::unsubscribe(const char* topic)
382383
383384 beginPacket (MQTT_UNSUBSCRIBE, 0x02 , remainingLength, packetBuffer);
384385 write16 (_txPacketId);
385- writeString (topic, topicLength);
386+ writeString (topic. c_str () , topicLength);
386387
387388 if (!endPacket ()) {
388389 stop ();
@@ -406,11 +407,6 @@ int MqttClient::unsubscribe(const char* topic)
406407 return 0 ;
407408}
408409
409- int MqttClient::unsubscribe (const String& topic)
410- {
411- return unsubscribe (topic.c_str ());
412- }
413-
414410void MqttClient::poll ()
415411{
416412 if (clientAvailable () == 0 && !clientConnected ()) {
@@ -775,22 +771,11 @@ MqttClient::operator bool()
775771 return true ;
776772}
777773
778- void MqttClient::setId (const char * id)
779- {
780- _id = id;
781- }
782-
783774void MqttClient::setId (const String& id)
784775{
785776 _id = id;
786777}
787778
788- void MqttClient::setUsernamePassword (const char * username, const char * password)
789- {
790- _username = username;
791- _password = password;
792- }
793-
794779void MqttClient::setUsernamePassword (const String& username, const String& password)
795780{
796781 _username = username;
0 commit comments