Skip to content

Commit 2f97e4a

Browse files
committed
Add publish(topic,payload,retained) function
1 parent 15a0e41 commit 2f97e4a

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
2.3
2+
* Add publish(topic,payload,retained) function
3+
14
2.2
25
* Change code layout to match Arduino Library reqs
6+
37
2.1
48
* Add MAX_TRANSFER_SIZE def to chunk messages if needed
59
* Reject topic/payloads that exceed MQTT_MAX_PACKET_SIZE

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=PubSubClient
2-
version=2.2
2+
version=2.3
33
author=Nick O'Leary <nick.oleary@gmail.com>
44
maintainer=Nick O'Leary <nick.oleary@gmail.com>
55
sentence=A client library for MQTT messaging.

src/PubSubClient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ boolean PubSubClient::publish(const char* topic, const char* payload) {
329329
return publish(topic,(const uint8_t*)payload,strlen(payload),false);
330330
}
331331

332+
boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
333+
return publish(topic,(const uint8_t*)payload,strlen(payload),retained);
334+
}
335+
332336
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
333337
return publish(topic, payload, plength, false);
334338
}

src/PubSubClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class PubSubClient {
111111
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
112112
void disconnect();
113113
boolean publish(const char* topic, const char* payload);
114+
boolean publish(const char* topic, const char* payload, boolean retained);
114115
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
115116
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
116117
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);

tests/src/publish_spec.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int test_publish_bytes() {
6363

6464

6565
int test_publish_retained() {
66-
IT("publishes retained");
66+
IT("publishes retained - 1");
6767
ShimClient shimClient;
6868
shimClient.setAllowConnect(true);
6969

@@ -88,6 +88,29 @@ int test_publish_retained() {
8888
END_IT
8989
}
9090

91+
int test_publish_retained_2() {
92+
IT("publishes retained - 2");
93+
ShimClient shimClient;
94+
shimClient.setAllowConnect(true);
95+
96+
byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
97+
shimClient.respond(connack,4);
98+
99+
PubSubClient client(server, 1883, callback, shimClient);
100+
int rc = client.connect((char*)"client_test1");
101+
IS_TRUE(rc);
102+
103+
byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'};
104+
shimClient.expect(publish,14);
105+
106+
rc = client.publish((char*)"topic",(char*)"ABCDE",true);
107+
IS_TRUE(rc);
108+
109+
IS_FALSE(shimClient.error());
110+
111+
END_IT
112+
}
113+
91114
int test_publish_not_connected() {
92115
IT("publish fails when not connected");
93116
ShimClient shimClient;
@@ -158,6 +181,7 @@ int main()
158181
test_publish();
159182
test_publish_bytes();
160183
test_publish_retained();
184+
test_publish_retained_2();
161185
test_publish_not_connected();
162186
test_publish_too_long();
163187
test_publish_P();

0 commit comments

Comments
 (0)