Add wider IPAddress support in api

This commit is contained in:
Nick O'Leary 2015-08-27 14:18:16 +01:00
parent c753ecaebe
commit 12a9d89ea2
6 changed files with 98 additions and 46 deletions

View File

@ -5,7 +5,6 @@
*/ */
#include "PubSubClient.h" #include "PubSubClient.h"
#include <string.h>
PubSubClient::PubSubClient() { PubSubClient::PubSubClient() {
this->_client = NULL; this->_client = NULL;
@ -13,31 +12,73 @@ PubSubClient::PubSubClient() {
setCallback(NULL); setCallback(NULL);
} }
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) { PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) {
setServer(addr, port);
setClient(client); setClient(client);
setCallback(callback);
setServer(ip, port);
this->stream = NULL; this->stream = NULL;
} }
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) {
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) { setServer(addr,port);
setClient(client); setClient(client);
setStream(stream);
}
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
setServer(addr, port);
setCallback(callback); setCallback(callback);
setServer(domain,port); setClient(client);
this->stream = NULL; this->stream = NULL;
} }
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) { setServer(addr,port);
setClient(client);
setCallback(callback); setCallback(callback);
setServer(ip,port); setClient(client);
setStream(stream); setStream(stream);
} }
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) {
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) {
setServer(ip, port);
setClient(client); setClient(client);
this->stream = NULL;
}
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) {
setServer(ip,port);
setClient(client);
setStream(stream);
}
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
setServer(ip, port);
setCallback(callback); setCallback(callback);
setClient(client);
this->stream = NULL;
}
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
setServer(ip,port);
setCallback(callback);
setClient(client);
setStream(stream);
}
PubSubClient::PubSubClient(char* domain, uint16_t port, Client& client) {
setServer(domain,port); setServer(domain,port);
setClient(client);
this->stream = NULL;
}
PubSubClient::PubSubClient(char* domain, uint16_t port, Client& client, Stream& stream) {
setServer(domain,port);
setClient(client);
setStream(stream);
}
PubSubClient::PubSubClient(char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
setServer(domain,port);
setCallback(callback);
setClient(client);
this->stream = NULL;
}
PubSubClient::PubSubClient(char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
setServer(domain,port);
setCallback(callback);
setClient(client);
setStream(stream); setStream(stream);
} }
@ -49,8 +90,7 @@ boolean PubSubClient::connect(char *id, char *user, char *pass) {
return connect(id,user,pass,0,0,0,0); return connect(id,user,pass,0,0,0,0);
} }
boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
{
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage); return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage);
} }
@ -63,7 +103,6 @@ boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic,
} else { } else {
result = _client->connect(this->ip, this->port); result = _client->connect(this->ip, this->port);
} }
if (result) { if (result) {
nextMsgId = 1; nextMsgId = 1;
// Leave room in the buffer for header and variable length field // Leave room in the buffer for header and variable length field
@ -429,6 +468,11 @@ boolean PubSubClient::connected() {
} }
void PubSubClient::setServer(uint8_t * ip, uint16_t port) { void PubSubClient::setServer(uint8_t * ip, uint16_t port) {
IPAddress addr(ip[0],ip[1],ip[2],ip[3]);
setServer(addr,port);
}
void PubSubClient::setServer(IPAddress ip, uint16_t port) {
this->ip = ip; this->ip = ip;
this->port = port; this->port = port;
} }

View File

@ -8,6 +8,7 @@
#define PubSubClient_h #define PubSubClient_h
#include <Arduino.h> #include <Arduino.h>
#include "IPAddress.h"
#include "Client.h" #include "Client.h"
#include "Stream.h" #include "Stream.h"
@ -43,6 +44,8 @@
#define MQTTQOS1 (1 << 1) #define MQTTQOS1 (1 << 1)
#define MQTTQOS2 (2 << 1) #define MQTTQOS2 (2 << 1)
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*,uint8_t*,unsigned int)
class PubSubClient { class PubSubClient {
private: private:
Client* _client; Client* _client;
@ -51,25 +54,34 @@ private:
unsigned long lastOutActivity; unsigned long lastOutActivity;
unsigned long lastInActivity; unsigned long lastInActivity;
bool pingOutstanding; bool pingOutstanding;
void (*callback)(char*,uint8_t*,unsigned int); MQTT_CALLBACK_SIGNATURE;
uint16_t readPacket(uint8_t*); uint16_t readPacket(uint8_t*);
uint8_t readByte(); uint8_t readByte();
boolean write(uint8_t header, uint8_t* buf, uint16_t length); boolean write(uint8_t header, uint8_t* buf, uint16_t length);
uint16_t writeString(char* string, uint8_t* buf, uint16_t pos); uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
uint8_t *ip; IPAddress ip;
char* domain; char* domain;
uint16_t port; uint16_t port;
Stream* stream; Stream* stream;
public: public:
PubSubClient(); PubSubClient();
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client); PubSubClient(IPAddress, uint16_t, Client& client);
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&); PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client); PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&); PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
PubSubClient(uint8_t *, uint16_t, Client& client);
PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
PubSubClient(char*, uint16_t, Client& client);
PubSubClient(char*, uint16_t, Client& client, Stream&);
PubSubClient(char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
PubSubClient(char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
void setServer(IPAddress ip, uint16_t port);
void setServer(uint8_t * ip, uint16_t port); void setServer(uint8_t * ip, uint16_t port);
void setServer(char * domain, uint16_t port); void setServer(char * domain, uint16_t port);
void setCallback(void(*callback)(char*,uint8_t*,unsigned int)); void setCallback(MQTT_CALLBACK_SIGNATURE);
void setClient(Client& client); void setClient(Client& client);
void setStream(Stream& stream); void setStream(Stream& stream);

View File

@ -13,8 +13,8 @@
// Update these with values suitable for your network. // Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 172, 16, 0, 2 }; IPAddress ip(172, 16, 0, 100);
byte ip[] = { 172, 16, 0, 100 }; IPAddress server(172, 16, 0, 2);
void callback(char* topic, byte* payload, unsigned int length) { void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived // handle message arrived
@ -36,4 +36,3 @@ void loop()
{ {
client.loop(); client.loop();
} }

View File

@ -12,8 +12,8 @@
// Update these with values suitable for your network. // Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 172, 16, 0, 2 }; IPAddress ip(172, 16, 0, 100);
byte ip[] = { 172, 16, 0, 100 }; IPAddress server(172, 16, 0, 2);
void callback(char* topic, byte* payload, unsigned int length) { void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived // handle message arrived
@ -35,4 +35,3 @@ void loop()
{ {
client.loop(); client.loop();
} }

View File

@ -20,8 +20,8 @@
// Update these with values suitable for your network. // Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 172, 16, 0, 2 }; IPAddress ip(172, 16, 0, 100);
byte ip[] = { 172, 16, 0, 100 }; IPAddress server(172, 16, 0, 2);
// Callback function header // Callback function header
void callback(char* topic, byte* payload, unsigned int length); void callback(char* topic, byte* payload, unsigned int length);
@ -58,4 +58,3 @@ void loop()
{ {
client.loop(); client.loop();
} }

View File

@ -16,8 +16,8 @@
// Update these with values suitable for your network. // Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 172, 16, 0, 2 }; IPAddress ip(172, 16, 0, 100);
byte ip[] = { 172, 16, 0, 100 }; IPAddress server(172, 16, 0, 2);
SRAM sram(4, SRAM_1024); SRAM sram(4, SRAM_1024);
@ -55,4 +55,3 @@ void loop()
{ {
client.loop(); client.loop();
} }