Skip to content

Commit dc768c0

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 dc768c0

File tree

2 files changed

+31
-54
lines changed

2 files changed

+31
-54
lines changed

src/MqttClient.cpp

Lines changed: 31 additions & 46 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()
@@ -286,17 +276,32 @@ int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, u
286276

287277
int 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

297302
int 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

302307
int 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-
414410
void 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-
783774
void 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-
794779
void MqttClient::setUsernamePassword(const String& username, const String& password)
795780
{
796781
_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)