Skip to content

Commit

Permalink
Add publish(topic,payload,retained) function
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Sep 11, 2015
1 parent 15a0e41 commit 2f97e4a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
@@ -1,5 +1,9 @@
2.3
* Add publish(topic,payload,retained) function

2.2
* Change code layout to match Arduino Library reqs

2.1
* Add MAX_TRANSFER_SIZE def to chunk messages if needed
* Reject topic/payloads that exceed MQTT_MAX_PACKET_SIZE
Expand Down
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=PubSubClient
version=2.2
version=2.3
author=Nick O'Leary <nick.oleary@gmail.com>
maintainer=Nick O'Leary <nick.oleary@gmail.com>
sentence=A client library for MQTT messaging.
Expand Down
4 changes: 4 additions & 0 deletions src/PubSubClient.cpp
Expand Up @@ -329,6 +329,10 @@ boolean PubSubClient::publish(const char* topic, const char* payload) {
return publish(topic,(const uint8_t*)payload,strlen(payload),false);
}

boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
return publish(topic,(const uint8_t*)payload,strlen(payload),retained);
}

boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
return publish(topic, payload, plength, false);
}
Expand Down
1 change: 1 addition & 0 deletions src/PubSubClient.h
Expand Up @@ -111,6 +111,7 @@ class PubSubClient {
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
void disconnect();
boolean publish(const char* topic, const char* payload);
boolean publish(const char* topic, const char* payload, boolean retained);
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
Expand Down
26 changes: 25 additions & 1 deletion tests/src/publish_spec.cpp
Expand Up @@ -63,7 +63,7 @@ int test_publish_bytes() {


int test_publish_retained() {
IT("publishes retained");
IT("publishes retained - 1");
ShimClient shimClient;
shimClient.setAllowConnect(true);

Expand All @@ -88,6 +88,29 @@ int test_publish_retained() {
END_IT
}

int test_publish_retained_2() {
IT("publishes retained - 2");
ShimClient shimClient;
shimClient.setAllowConnect(true);

byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
shimClient.respond(connack,4);

PubSubClient client(server, 1883, callback, shimClient);
int rc = client.connect((char*)"client_test1");
IS_TRUE(rc);

byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'};
shimClient.expect(publish,14);

rc = client.publish((char*)"topic",(char*)"ABCDE",true);
IS_TRUE(rc);

IS_FALSE(shimClient.error());

END_IT
}

int test_publish_not_connected() {
IT("publish fails when not connected");
ShimClient shimClient;
Expand Down Expand Up @@ -158,6 +181,7 @@ int main()
test_publish();
test_publish_bytes();
test_publish_retained();
test_publish_retained_2();
test_publish_not_connected();
test_publish_too_long();
test_publish_P();
Expand Down

0 comments on commit 2f97e4a

Please sign in to comment.