From c6bb3caf570030c7c163d2da02be0291293628c0 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 7 Feb 2014 20:57:12 +0000 Subject: [PATCH] Fix publish_P and add test coverage --- PubSubClient/PubSubClient.cpp | 9 +++++---- tests/src/lib/Arduino.h | 5 +++-- tests/src/lib/ShimClient.cpp | 21 +++++++++++++++++---- tests/src/publish_spec.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/PubSubClient/PubSubClient.cpp b/PubSubClient/PubSubClient.cpp index 54f3900..f26ac4c 100755 --- a/PubSubClient/PubSubClient.cpp +++ b/PubSubClient/PubSubClient.cpp @@ -273,10 +273,10 @@ boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plengt boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned int plength, boolean retained) { uint8_t llen = 0; uint8_t digit; - int rc; + unsigned int rc = 0; uint16_t tlen; - int pos = 0; - int i; + unsigned int pos = 0; + unsigned int i; uint8_t header; unsigned int len; @@ -311,7 +311,8 @@ boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned } lastOutActivity = millis(); - return rc == len + 1 + plength; + + return rc == tlen + 4 + plength; } boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { diff --git a/tests/src/lib/Arduino.h b/tests/src/lib/Arduino.h index ee72ead..c675280 100644 --- a/tests/src/lib/Arduino.h +++ b/tests/src/lib/Arduino.h @@ -15,8 +15,9 @@ extern "C"{ extern void setup( void ) ; extern void loop( void ) ; uint32_t millis( void ); - uint8_t pgm_read_byte_near(uint8_t*); -#define PROGMEM } +#define PROGMEM +#define pgm_read_byte_near(x) *(x) + #endif // Arduino_h diff --git a/tests/src/lib/ShimClient.cpp b/tests/src/lib/ShimClient.cpp index f951052..280c2c7 100644 --- a/tests/src/lib/ShimClient.cpp +++ b/tests/src/lib/ShimClient.cpp @@ -8,9 +8,6 @@ extern "C" { uint32_t millis(void) { return time(0)*1000; } - uint8_t pgm_read_byte_near(uint8_t*) { - return 0; - } } ShimClient::ShimClient() { @@ -57,7 +54,23 @@ int ShimClient::connect(const char *host, uint16_t port) { } return this->_connected; } -size_t ShimClient::write(uint8_t b) { std::cout << "!!not implemented!! " << b; return 1; } +size_t ShimClient::write(uint8_t b) { + this->_received += 1; + TRACE(std::hex << (unsigned int)b); + if (!this->expectAnything) { + if (this->expectBuffer->available()) { + uint8_t expected = this->expectBuffer->next(); + if (expected != b) { + this->_error = true; + TRACE("!=" << (unsigned int)expected); + } + } else { + this->_error = true; + } + } + TRACE("\n"<< std::dec); + return 1; +} size_t ShimClient::write(const uint8_t *buf, size_t size) { this->_received += size; TRACE( "[" << std::dec << (unsigned int)(size) << "] "); diff --git a/tests/src/publish_spec.cpp b/tests/src/publish_spec.cpp index fcf943a..7689f77 100644 --- a/tests/src/publish_spec.cpp +++ b/tests/src/publish_spec.cpp @@ -102,12 +102,41 @@ int test_publish_not_connected() { END_IT } + +int test_publish_P() { + IT("publishes using PROGMEM"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; + int length = 5; + + 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,0x1,0x2,0x3,0x0,0x5}; + shimClient.expect(publish,14); + + rc = client.publish_P((char*)"topic",payload,length,true); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + + int main() { test_publish(); test_publish_bytes(); test_publish_retained(); test_publish_not_connected(); + test_publish_P(); FINISH }