Skip to content

Store strings inside library, instead using external char * #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions src/AsyncMqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ AsyncMqttClient::AsyncMqttClient()
, _lastClientActivity(0)
, _lastServerActivity(0)
, _lastPingRequestTime(0)
, _host(nullptr)
, _useIp(false)
#if ASYNC_TCP_SSL_ENABLED
, _secure(false)
#endif
, _port(0)
, _keepAlive(15)
, _cleanSession(true)
, _clientId(nullptr)
, _username(nullptr)
, _password(nullptr)
, _willTopic(nullptr)
, _willPayload(nullptr)
, _willPayloadLength(0)
, _willQos(0)
, _willRetain(false)
, _parsingInformation { .bufferState = AsyncMqttClientInternals::BufferState::NONE }
Expand All @@ -36,11 +29,13 @@ AsyncMqttClient::AsyncMqttClient()
_client.onData([](void* obj, AsyncClient* c, void* data, size_t len) { (static_cast<AsyncMqttClient*>(obj))->_onData(c, static_cast<char*>(data), len); }, this);
_client.onPoll([](void* obj, AsyncClient* c) { (static_cast<AsyncMqttClient*>(obj))->_onPoll(c); }, this);

char _generatedClientId[13 + 1]; // esp8266abc123
#ifdef ESP32
sprintf(_generatedClientId, "esp32%06x", ESP.getEfuseMac());
#elif defined(ESP8266)
sprintf(_generatedClientId, "esp8266%06x", ESP.getChipId());
#endif

_clientId = _generatedClientId;

setMaxTopicLength(128);
Expand Down Expand Up @@ -83,8 +78,15 @@ AsyncMqttClient& AsyncMqttClient::setWill(const char* topic, uint8_t qos, bool r
_willTopic = topic;
_willQos = qos;
_willRetain = retain;
_willPayload = payload;
_willPayloadLength = length;

if (length == 0) {
_willPayload = payload;
} else {
_willPayload = "";
while (length--) {
_willPayload += *payload;
}
}
return *this;
}

Expand Down Expand Up @@ -193,6 +195,9 @@ void AsyncMqttClient::_onConnect(AsyncClient* client) {
}
#endif

uint16_t usernameLength = _username.length();
uint16_t passwordLength = _password.length();

char fixedHeader[5];
fixedHeader[0] = AsyncMqttClientInternals::PacketType.CONNECT;
fixedHeader[0] = fixedHeader[0] << 4;
Expand All @@ -208,10 +213,14 @@ void AsyncMqttClient::_onConnect(AsyncClient* client) {

char connectFlags[1];
connectFlags[0] = 0;

uint16_t willTopicLength = _willTopic.length();

if (_cleanSession) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.CLEAN_SESSION;
if (_username != nullptr) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.USERNAME;
if (_password != nullptr) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.PASSWORD;
if (_willTopic != nullptr) {
if (usernameLength) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.USERNAME;
if (passwordLength) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.PASSWORD;

if (willTopicLength) {
connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.WILL;
if (_willRetain) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.WILL_RETAIN;
switch (_willQos) {
Expand All @@ -231,47 +240,39 @@ void AsyncMqttClient::_onConnect(AsyncClient* client) {
keepAliveBytes[0] = _keepAlive >> 8;
keepAliveBytes[1] = _keepAlive & 0xFF;

uint16_t clientIdLength = strlen(_clientId);
uint16_t clientIdLength = _clientId.length();
char clientIdLengthBytes[2];
clientIdLengthBytes[0] = clientIdLength >> 8;
clientIdLengthBytes[1] = clientIdLength & 0xFF;

// Optional fields
uint16_t willTopicLength = 0;
char willTopicLengthBytes[2];
uint16_t willPayloadLength = _willPayloadLength;
uint16_t willPayloadLength = _willPayload.length();
char willPayloadLengthBytes[2];
if (_willTopic != nullptr) {
willTopicLength = strlen(_willTopic);
if (willTopicLength) {
willTopicLengthBytes[0] = willTopicLength >> 8;
willTopicLengthBytes[1] = willTopicLength & 0xFF;

if (_willPayload != nullptr && willPayloadLength == 0) willPayloadLength = strlen(_willPayload);

willPayloadLengthBytes[0] = willPayloadLength >> 8;
willPayloadLengthBytes[1] = willPayloadLength & 0xFF;
}

uint16_t usernameLength = 0;
char usernameLengthBytes[2];
if (_username != nullptr) {
usernameLength = strlen(_username);
if ( usernameLength ) {
usernameLengthBytes[0] = usernameLength >> 8;
usernameLengthBytes[1] = usernameLength & 0xFF;
}

uint16_t passwordLength = 0;
char passwordLengthBytes[2];
if (_password != nullptr) {
passwordLength = strlen(_password);
if ( passwordLength ) {
passwordLengthBytes[0] = passwordLength >> 8;
passwordLengthBytes[1] = passwordLength & 0xFF;
}

uint32_t remainingLength = 2 + protocolNameLength + 1 + 1 + 2 + 2 + clientIdLength; // always present
if (_willTopic != nullptr) remainingLength += 2 + willTopicLength + 2 + willPayloadLength;
if (_username != nullptr) remainingLength += 2 + usernameLength;
if (_password != nullptr) remainingLength += 2 + passwordLength;
if (willTopicLength) remainingLength += 2 + willTopicLength + 2 + willPayloadLength;
if (usernameLength) remainingLength += 2 + usernameLength;
if (passwordLength) remainingLength += 2 + passwordLength;
uint8_t remainingLengthLength = AsyncMqttClientInternals::Helpers::encodeRemainingLength(remainingLength, fixedHeader + 1);

uint32_t neededSpace = 1 + remainingLengthLength;
Expand All @@ -282,18 +283,18 @@ void AsyncMqttClient::_onConnect(AsyncClient* client) {
neededSpace += 2;
neededSpace += 2;
neededSpace += clientIdLength;
if (_willTopic != nullptr) {
if (willTopicLength) {
neededSpace += 2;
neededSpace += willTopicLength;

neededSpace += 2;
if (_willPayload != nullptr) neededSpace += willPayloadLength;
if (willPayloadLength) neededSpace += willPayloadLength;
}
if (_username != nullptr) {
if (usernameLength) {
neededSpace += 2;
neededSpace += usernameLength;
}
if (_password != nullptr) {
if (passwordLength) {
neededSpace += 2;
neededSpace += passwordLength;
}
Expand All @@ -311,21 +312,21 @@ void AsyncMqttClient::_onConnect(AsyncClient* client) {
_client.add(connectFlags, 1);
_client.add(keepAliveBytes, 2);
_client.add(clientIdLengthBytes, 2);
_client.add(_clientId, clientIdLength);
if (_willTopic != nullptr) {
_client.add(_clientId.c_str(), clientIdLength);
if (willTopicLength) {
_client.add(willTopicLengthBytes, 2);
_client.add(_willTopic, willTopicLength);
_client.add(_willTopic.c_str(), willTopicLength);

_client.add(willPayloadLengthBytes, 2);
if (_willPayload != nullptr) _client.add(_willPayload, willPayloadLength);
if (willPayloadLength) _client.add(_willPayload.c_str(), willPayloadLength);
}
if (_username != nullptr) {
if (usernameLength) {
_client.add(usernameLengthBytes, 2);
_client.add(_username, usernameLength);
_client.add(_username.c_str(), usernameLength);
}
if (_password != nullptr) {
if (passwordLength) {
_client.add(passwordLengthBytes, 2);
_client.add(_password, passwordLength);
_client.add(_password.c_str(), passwordLength);
}
_client.send();
_lastClientActivity = millis();
Expand Down Expand Up @@ -441,6 +442,7 @@ void AsyncMqttClient::_onData(AsyncClient* client, char* data, size_t len) {
}

void AsyncMqttClient::_onPoll(AsyncClient* client) {
(void)client;
if (!_connected) return;

// if there is too much time the client has sent a ping request without a response, disconnect client to avoid half open connections
Expand Down Expand Up @@ -685,13 +687,13 @@ void AsyncMqttClient::connect() {
if (_useIp) {
_client.connect(_ip, _port, _secure);
} else {
_client.connect(_host, _port, _secure);
_client.connect(_host.c_str(), _port, _secure);
}
#else
if (_useIp) {
_client.connect(_ip, _port);
} else {
_client.connect(_host, _port);
_client.connect(_host.c_str(), _port);
}
#endif
}
Expand Down
14 changes: 6 additions & 8 deletions src/AsyncMqttClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,20 @@ class AsyncMqttClient {
uint32_t _lastServerActivity;
uint32_t _lastPingRequestTime;

char _generatedClientId[13 + 1]; // esp8266abc123
IPAddress _ip;
const char* _host;
String _host;
bool _useIp;
#if ASYNC_TCP_SSL_ENABLED
bool _secure;
#endif
uint16_t _port;
uint16_t _keepAlive;
bool _cleanSession;
const char* _clientId;
const char* _username;
const char* _password;
const char* _willTopic;
const char* _willPayload;
uint16_t _willPayloadLength;
String _clientId;
String _username;
String _password;
String _willTopic;
String _willPayload;
uint8_t _willQos;
bool _willRetain;

Expand Down