Tidy up PROGMEM and const char*s

This commit is contained in:
Nick O'Leary 2015-08-27 23:22:54 +01:00
parent 12a9d89ea2
commit 8e7e99cb8d
2 changed files with 40 additions and 42 deletions

View File

@ -59,42 +59,42 @@ PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE,
setStream(stream); setStream(stream);
} }
PubSubClient::PubSubClient(char* domain, uint16_t port, Client& client) { PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) {
setServer(domain,port); setServer(domain,port);
setClient(client); setClient(client);
this->stream = NULL; this->stream = NULL;
} }
PubSubClient::PubSubClient(char* domain, uint16_t port, Client& client, Stream& stream) { PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream) {
setServer(domain,port); setServer(domain,port);
setClient(client); setClient(client);
setStream(stream); setStream(stream);
} }
PubSubClient::PubSubClient(char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) { PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
setServer(domain,port); setServer(domain,port);
setCallback(callback); setCallback(callback);
setClient(client); setClient(client);
this->stream = NULL; this->stream = NULL;
} }
PubSubClient::PubSubClient(char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) { PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
setServer(domain,port); setServer(domain,port);
setCallback(callback); setCallback(callback);
setClient(client); setClient(client);
setStream(stream); setStream(stream);
} }
boolean PubSubClient::connect(char *id) { boolean PubSubClient::connect(const char *id) {
return connect(id,NULL,NULL,0,0,0,0); return connect(id,NULL,NULL,0,0,0,0);
} }
boolean PubSubClient::connect(char *id, char *user, char *pass) { boolean PubSubClient::connect(const char *id, const char *user, const 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(const char *id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage); return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage);
} }
boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) { boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
if (!connected()) { if (!connected()) {
int result = 0; int result = 0;
@ -296,15 +296,15 @@ boolean PubSubClient::loop() {
return false; return false;
} }
boolean PubSubClient::publish(char* topic, char* payload) { boolean PubSubClient::publish(const char* topic, const char* payload) {
return publish(topic,(uint8_t*)payload,strlen(payload),false); return publish(topic,(const uint8_t*)payload,strlen(payload),false);
} }
boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plength) { boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
return publish(topic, payload, plength, false); return publish(topic, payload, plength, false);
} }
boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plength, boolean retained) { boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
if (connected()) { if (connected()) {
// Leave room in the buffer for header and variable length field // Leave room in the buffer for header and variable length field
uint16_t length = 5; uint16_t length = 5;
@ -321,8 +321,8 @@ boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plengt
} }
return false; return false;
} }
#ifdef PROGMEM
boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned int plength, boolean retained) { boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
uint8_t llen = 0; uint8_t llen = 0;
uint8_t digit; uint8_t digit;
unsigned int rc = 0; unsigned int rc = 0;
@ -366,7 +366,6 @@ boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned
return rc == tlen + 4 + plength; return rc == tlen + 4 + plength;
} }
#endif
boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
uint8_t lenBuf[4]; uint8_t lenBuf[4];
@ -395,11 +394,11 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
return (rc == 1+llen+length); return (rc == 1+llen+length);
} }
boolean PubSubClient::subscribe(char* topic) { boolean PubSubClient::subscribe(const char* topic) {
return subscribe(topic, 0); return subscribe(topic, 0);
} }
boolean PubSubClient::subscribe(char* topic, uint8_t qos) { boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
if (qos < 0 || qos > 1) if (qos < 0 || qos > 1)
return false; return false;
@ -412,14 +411,14 @@ boolean PubSubClient::subscribe(char* topic, uint8_t qos) {
} }
buffer[length++] = (nextMsgId >> 8); buffer[length++] = (nextMsgId >> 8);
buffer[length++] = (nextMsgId & 0xFF); buffer[length++] = (nextMsgId & 0xFF);
length = writeString(topic, buffer,length); length = writeString((char*)topic, buffer,length);
buffer[length++] = qos; buffer[length++] = qos;
return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length-5); return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length-5);
} }
return false; return false;
} }
boolean PubSubClient::unsubscribe(char* topic) { boolean PubSubClient::unsubscribe(const char* topic) {
if (connected()) { if (connected()) {
uint16_t length = 5; uint16_t length = 5;
nextMsgId++; nextMsgId++;
@ -442,8 +441,8 @@ void PubSubClient::disconnect() {
lastInActivity = lastOutActivity = millis(); lastInActivity = lastOutActivity = millis();
} }
uint16_t PubSubClient::writeString(char* string, uint8_t* buf, uint16_t pos) { uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {
char* idp = string; const char* idp = string;
uint16_t i = 0; uint16_t i = 0;
pos += 2; pos += 2;
while (*idp) { while (*idp) {
@ -475,9 +474,10 @@ void PubSubClient::setServer(uint8_t * ip, uint16_t port) {
void PubSubClient::setServer(IPAddress ip, uint16_t port) { void PubSubClient::setServer(IPAddress ip, uint16_t port) {
this->ip = ip; this->ip = ip;
this->port = port; this->port = port;
this->domain = NULL;
} }
void PubSubClient::setServer(char * domain, uint16_t port) { void PubSubClient::setServer(const char * domain, uint16_t port) {
this->domain = domain; this->domain = domain;
this->port = port; this->port = port;
} }

View File

@ -58,9 +58,9 @@ private:
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(const char* string, uint8_t* buf, uint16_t pos);
IPAddress ip; IPAddress ip;
char* domain; const char* domain;
uint16_t port; uint16_t port;
Stream* stream; Stream* stream;
public: public:
@ -73,32 +73,30 @@ public:
PubSubClient(uint8_t *, uint16_t, Client& client, Stream&); 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);
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
PubSubClient(char*, uint16_t, Client& client); PubSubClient(const char*, uint16_t, Client& client);
PubSubClient(char*, uint16_t, Client& client, Stream&); PubSubClient(const char*, uint16_t, Client& client, Stream&);
PubSubClient(char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
PubSubClient(char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
void setServer(IPAddress ip, uint16_t port); 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(const char * domain, uint16_t port);
void setCallback(MQTT_CALLBACK_SIGNATURE); void setCallback(MQTT_CALLBACK_SIGNATURE);
void setClient(Client& client); void setClient(Client& client);
void setStream(Stream& stream); void setStream(Stream& stream);
boolean connect(char *); boolean connect(const char* id);
boolean connect(char *, char *, char *); boolean connect(const char* id, const char* user, const char* pass);
boolean connect(char *, char *, uint8_t, uint8_t, char *); boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
boolean connect(char *, char *, char *, char *, uint8_t, uint8_t, char*); boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
void disconnect(); void disconnect();
boolean publish(char *, char *); boolean publish(const char* topic, const char* payload);
boolean publish(char *, uint8_t *, unsigned int); boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
boolean publish(char *, uint8_t *, unsigned int, boolean); boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
#ifdef PROGMEM boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
boolean publish_P(char *, uint8_t PROGMEM *, unsigned int, boolean); boolean subscribe(const char* topic);
#endif boolean subscribe(const char* topic, uint8_t qos);
boolean subscribe(char *); boolean unsubscribe(const char* topic);
boolean subscribe(char *, uint8_t qos);
boolean unsubscribe(char *);
boolean loop(); boolean loop();
boolean connected(); boolean connected();
}; };