Skip to content

Commit a797658

Browse files
Removing redundant methods that use char* instrad of String
This will simplify the definition of the class since String class is automatically built by providinc a C-string
1 parent 81ce835 commit a797658

File tree

2 files changed

+14
-62
lines changed

2 files changed

+14
-62
lines changed

src/MqttClient.cpp

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
204194
int 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

209199
int MqttClient::endMessage()
@@ -259,9 +249,9 @@ int MqttClient::endMessage()
259249
return 1;
260250
}
261251

262-
int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos)
252+
int MqttClient::beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos)
263253
{
264-
int topicLength = strlen(topic);
254+
int topicLength = topic.length();
265255
size_t willLength = (2 + topicLength + 2 + size);
266256

267257
if (qos > 2) {
@@ -272,7 +262,7 @@ int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, u
272262

273263
_txBuffer = _willBuffer;
274264
_txBufferIndex = 0;
275-
writeString(topic, topicLength);
265+
writeString(topic.c_str(), topic.length());
276266
write16(0); // dummy size for now
277267
_willMessageIndex = _txBufferIndex;
278268

@@ -284,19 +274,9 @@ int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, u
284274
return 0;
285275
}
286276

287-
int MqttClient::beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos)
288-
{
289-
return beginWill(topic.c_str(), size, retain, qos);
290-
}
291-
292-
int MqttClient::beginWill(const char* topic, bool retain, uint8_t qos)
293-
{
294-
return beginWill(topic, _tx_payload_buffer_size, retain, qos);
295-
}
296-
297277
int MqttClient::beginWill(const String& topic, bool retain, uint8_t qos)
298278
{
299-
return beginWill(topic.c_str(), retain, qos);
279+
return beginWill(topic, _tx_payload_buffer_size, retain, qos);
300280
}
301281

302282
int MqttClient::endWill()
@@ -314,9 +294,10 @@ int MqttClient::endWill()
314294
return 1;
315295
}
316296

317-
int MqttClient::subscribe(const char* topic, uint8_t qos)
297+
int MqttClient::subscribe(const String& topic, uint8_t qos)
298+
318299
{
319-
int topicLength = strlen(topic);
300+
int topicLength = topic.length();
320301
int remainingLength = topicLength + 5;
321302

322303
if (qos > 2) {
@@ -334,7 +315,7 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)
334315

335316
beginPacket(MQTT_SUBSCRIBE, 0x02, remainingLength, packetBuffer);
336317
write16(_txPacketId);
337-
writeString(topic, topicLength);
318+
writeString(topic.c_str(), topicLength);
338319
write8(qos);
339320

340321
if (!endPacket()) {
@@ -362,14 +343,9 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)
362343
return 0;
363344
}
364345

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)
346+
int MqttClient::unsubscribe(const String& topic)
371347
{
372-
int topicLength = strlen(topic);
348+
int topicLength = topic.length();
373349
int remainingLength = topicLength + 4;
374350

375351
_txPacketId++;
@@ -382,7 +358,7 @@ int MqttClient::unsubscribe(const char* topic)
382358

383359
beginPacket(MQTT_UNSUBSCRIBE, 0x02, remainingLength, packetBuffer);
384360
write16(_txPacketId);
385-
writeString(topic, topicLength);
361+
writeString(topic.c_str(), topicLength);
386362

387363
if (!endPacket()) {
388364
stop();
@@ -406,11 +382,6 @@ int MqttClient::unsubscribe(const char* topic)
406382
return 0;
407383
}
408384

409-
int MqttClient::unsubscribe(const String& topic)
410-
{
411-
return unsubscribe(topic.c_str());
412-
}
413-
414385
void MqttClient::poll()
415386
{
416387
if (clientAvailable() == 0 && !clientConnected()) {
@@ -775,22 +746,11 @@ MqttClient::operator bool()
775746
return true;
776747
}
777748

778-
void MqttClient::setId(const char* id)
779-
{
780-
_id = id;
781-
}
782-
783749
void MqttClient::setId(const String& id)
784750
{
785751
_id = id;
786752
}
787753

788-
void MqttClient::setUsernamePassword(const char* username, const char* password)
789-
{
790-
_username = username;
791-
_password = password;
792-
}
793-
794754
void MqttClient::setUsernamePassword(const String& username, const String& password)
795755
{
796756
_username = username;

src/MqttClient.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,15 @@ class MqttClient : public Client {
5959
int messageQoS() const;
6060
int messageRetain() const;
6161

62-
int beginMessage(const char* topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
6362
int beginMessage(const String& topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
64-
int beginMessage(const char* topic, bool retain = false, uint8_t qos = 0, bool dup = false);
6563
int beginMessage(const String& topic, bool retain = false, uint8_t qos = 0, bool dup = false);
6664
int endMessage();
6765

68-
int beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos);
6966
int beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos);
70-
int beginWill(const char* topic, bool retain, uint8_t qos);
7167
int beginWill(const String& topic, bool retain, uint8_t qos);
7268
int endWill();
7369

74-
int subscribe(const char* topic, uint8_t qos = 0);
7570
int subscribe(const String& topic, uint8_t qos = 0);
76-
int unsubscribe(const char* topic);
7771
int unsubscribe(const String& topic);
7872

7973
void poll();
@@ -95,10 +89,8 @@ class MqttClient : public Client {
9589
virtual uint8_t connected();
9690
virtual operator bool();
9791

98-
void setId(const char* id);
9992
void setId(const String& id);
10093

101-
void setUsernamePassword(const char* username, const char* password);
10294
void setUsernamePassword(const String& username, const String& password);
10395

10496
void setCleanSession(bool cleanSession);

0 commit comments

Comments
 (0)