Fix publish_P and add test coverage
This commit is contained in:
		| @@ -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) { | boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned int plength, boolean retained) { | ||||||
|    uint8_t llen = 0; |    uint8_t llen = 0; | ||||||
|    uint8_t digit; |    uint8_t digit; | ||||||
|    int rc; |    unsigned int rc = 0; | ||||||
|    uint16_t tlen; |    uint16_t tlen; | ||||||
|    int pos = 0; |    unsigned int pos = 0; | ||||||
|    int i; |    unsigned int i; | ||||||
|    uint8_t header; |    uint8_t header; | ||||||
|    unsigned int len; |    unsigned int len; | ||||||
|     |     | ||||||
| @@ -311,7 +311,8 @@ boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned | |||||||
|    } |    } | ||||||
|     |     | ||||||
|    lastOutActivity = millis(); |    lastOutActivity = millis(); | ||||||
|    return rc == len + 1 + plength; |     | ||||||
|  |    return rc == tlen + 4 + plength; | ||||||
| } | } | ||||||
|  |  | ||||||
| boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { | boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { | ||||||
|   | |||||||
| @@ -15,8 +15,9 @@ extern "C"{ | |||||||
|     extern void setup( void ) ; |     extern void setup( void ) ; | ||||||
|     extern void loop( void ) ; |     extern void loop( void ) ; | ||||||
|     uint32_t millis( 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 | #endif // Arduino_h | ||||||
|   | |||||||
| @@ -8,9 +8,6 @@ extern "C" { | |||||||
|     uint32_t millis(void) { |     uint32_t millis(void) { | ||||||
|        return time(0)*1000; |        return time(0)*1000; | ||||||
|     } |     } | ||||||
|     uint8_t pgm_read_byte_near(uint8_t*) { |  | ||||||
|        return 0; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| ShimClient::ShimClient() { | ShimClient::ShimClient() { | ||||||
| @@ -57,7 +54,23 @@ int ShimClient::connect(const char *host, uint16_t port)  { | |||||||
|     } |     } | ||||||
|     return this->_connected; |     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)  { | size_t ShimClient::write(const uint8_t *buf, size_t size)  { | ||||||
|     this->_received += size; |     this->_received += size; | ||||||
|     TRACE( "[" << std::dec << (unsigned int)(size) << "] "); |     TRACE( "[" << std::dec << (unsigned int)(size) << "] "); | ||||||
|   | |||||||
| @@ -102,12 +102,41 @@ int test_publish_not_connected() { | |||||||
|     END_IT |     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() | int main() | ||||||
| { | { | ||||||
|     test_publish(); |     test_publish(); | ||||||
|     test_publish_bytes(); |     test_publish_bytes(); | ||||||
|     test_publish_retained(); |     test_publish_retained(); | ||||||
|     test_publish_not_connected(); |     test_publish_not_connected(); | ||||||
|  |     test_publish_P(); | ||||||
|      |      | ||||||
|     FINISH |     FINISH | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Nick O'Leary
					Nick O'Leary