Skip to content

Commit 2aded0e

Browse files
jeroenstmarvinroger
authored andcommitted
Fix #130, #147, #160 and #166 (#165)
* Solved setWill() cause mqtt could not connect with SSL enable #107 #107 * Fixed #160 * Fixed bug #147 * Fixed #166 - ESP32 now also uses a unique client id - getClientId() fuction added * Solved Minor Flaw #147 (comment)
1 parent 8bd918b commit 2aded0e

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

src/AsyncMqttClient.cpp

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
AsyncMqttClient::AsyncMqttClient()
44
: _connected(false)
55
, _connectPacketNotEnoughSpace(false)
6-
, _disconnectFlagged(false)
6+
, _disconnectOnPoll(false)
77
, _tlsBadFingerprint(false)
88
, _lastClientActivity(0)
99
, _lastServerActivity(0)
@@ -37,10 +37,10 @@ AsyncMqttClient::AsyncMqttClient()
3737
_client.onPoll([](void* obj, AsyncClient* c) { (static_cast<AsyncMqttClient*>(obj))->_onPoll(c); }, this);
3838

3939
#ifdef ESP32
40-
sprintf(_generatedClientId, "esp32%06x", ESP.getEfuseMac());
40+
sprintf(_generatedClientId, "esp32-%06llx", ESP.getEfuseMac());
4141
_xSemaphore = xSemaphoreCreateMutex();
4242
#elif defined(ESP8266)
43-
sprintf(_generatedClientId, "esp8266%06x", ESP.getChipId());
43+
sprintf(_generatedClientId, "esp8266-%06x", ESP.getChipId());
4444
#endif
4545
_clientId = _generatedClientId;
4646

@@ -158,7 +158,7 @@ void AsyncMqttClient::_freeCurrentParsedPacket() {
158158
void AsyncMqttClient::_clear() {
159159
_lastPingRequestTime = 0;
160160
_connected = false;
161-
_disconnectFlagged = false;
161+
_disconnectOnPoll = false;
162162
_connectPacketNotEnoughSpace = false;
163163
_tlsBadFingerprint = false;
164164
_freeCurrentParsedPacket();
@@ -311,12 +311,26 @@ void AsyncMqttClient::_onConnect(AsyncClient* client) {
311311
}
312312

313313
_client.add(fixedHeader, 1 + remainingLengthLength);
314-
_client.add(protocolNameLengthBytes, 2);
315-
_client.add("MQTT", protocolNameLength);
316-
_client.add(protocolLevel, 1);
317-
_client.add(connectFlags, 1);
318-
_client.add(keepAliveBytes, 2);
319-
_client.add(clientIdLengthBytes, 2);
314+
315+
// Using a sendbuffer to fix bug setwill on SSL not working
316+
char sendbuffer[12];
317+
sendbuffer[0] = protocolNameLengthBytes[0];
318+
sendbuffer[1] = protocolNameLengthBytes[1];
319+
320+
sendbuffer[2] = 'M';
321+
sendbuffer[3] = 'Q';
322+
sendbuffer[4] = 'T';
323+
sendbuffer[5] = 'T';
324+
325+
sendbuffer[6] = protocolLevel[0];
326+
sendbuffer[7] = connectFlags[0];
327+
sendbuffer[8] = keepAliveBytes[0];
328+
sendbuffer[9] = keepAliveBytes[1];
329+
sendbuffer[10] = clientIdLengthBytes[0];
330+
sendbuffer[11] = clientIdLengthBytes[1];
331+
332+
_client.add(sendbuffer, 12);
333+
320334
_client.add(_clientId, clientIdLength);
321335
if (_willTopic != nullptr) {
322336
_client.add(willTopicLengthBytes, 2);
@@ -340,19 +354,19 @@ void AsyncMqttClient::_onConnect(AsyncClient* client) {
340354

341355
void AsyncMqttClient::_onDisconnect(AsyncClient* client) {
342356
(void)client;
343-
if (!_disconnectFlagged) {
344-
AsyncMqttClientDisconnectReason reason;
357+
AsyncMqttClientDisconnectReason reason;
345358

346-
if (_connectPacketNotEnoughSpace) {
347-
reason = AsyncMqttClientDisconnectReason::ESP8266_NOT_ENOUGH_SPACE;
348-
} else if (_tlsBadFingerprint) {
349-
reason = AsyncMqttClientDisconnectReason::TLS_BAD_FINGERPRINT;
350-
} else {
351-
reason = AsyncMqttClientDisconnectReason::TCP_DISCONNECTED;
352-
}
353-
for (auto callback : _onDisconnectUserCallbacks) callback(reason);
359+
if (_connectPacketNotEnoughSpace) {
360+
reason = AsyncMqttClientDisconnectReason::ESP8266_NOT_ENOUGH_SPACE;
361+
} else if (_tlsBadFingerprint) {
362+
reason = AsyncMqttClientDisconnectReason::TLS_BAD_FINGERPRINT;
363+
} else {
364+
reason = AsyncMqttClientDisconnectReason::TCP_DISCONNECTED;
354365
}
366+
355367
_clear();
368+
369+
for (auto callback : _onDisconnectUserCallbacks) callback(reason);
356370
}
357371

358372
void AsyncMqttClient::_onError(AsyncClient* client, int8_t error) {
@@ -460,13 +474,14 @@ void AsyncMqttClient::_onPoll(AsyncClient* client) {
460474
_sendPing();
461475
}
462476

477+
463478
// handle to send ack packets
464479

465480
_sendAcks();
466481

467482
// handle disconnect
468483

469-
if (_disconnectFlagged) {
484+
if (_disconnectOnPoll) {
470485
_sendDisconnect();
471486
}
472487
}
@@ -485,8 +500,7 @@ void AsyncMqttClient::_onConnAck(bool sessionPresent, uint8_t connectReturnCode)
485500
_connected = true;
486501
for (auto callback : _onConnectUserCallbacks) callback(sessionPresent);
487502
} else {
488-
for (auto callback : _onDisconnectUserCallbacks) callback(static_cast<AsyncMqttClientDisconnectReason>(connectReturnCode));
489-
_disconnectFlagged = true;
503+
// Callbacks are handled by the ondisconnect function which is called from the AsyncTcp lib
490504
}
491505
}
492506

@@ -672,7 +686,7 @@ bool AsyncMqttClient::_sendDisconnect() {
672686
_client.send();
673687
_client.close(true);
674688

675-
_disconnectFlagged = false;
689+
_disconnectOnPoll = false;
676690

677691
SEMAPHORE_GIVE();
678692
return true;
@@ -715,8 +729,8 @@ void AsyncMqttClient::disconnect(bool force) {
715729
if (force) {
716730
_client.close(true);
717731
} else {
718-
_disconnectFlagged = true;
719732
_sendDisconnect();
733+
_disconnectOnPoll = false;
720734
}
721735
}
722736

@@ -875,3 +889,7 @@ uint16_t AsyncMqttClient::publish(const char* topic, uint8_t qos, bool retain, c
875889
return 1;
876890
}
877891
}
892+
893+
const char* AsyncMqttClient::getClientId() {
894+
return _clientId;
895+
}

src/AsyncMqttClient.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,20 @@ class AsyncMqttClient {
7878
uint16_t unsubscribe(const char* topic);
7979
uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload = nullptr, size_t length = 0, bool dup = false, uint16_t message_id = 0);
8080

81+
const char* getClientId();
82+
8183
private:
8284
AsyncClient _client;
8385

8486
bool _connected;
8587
bool _connectPacketNotEnoughSpace;
86-
bool _disconnectFlagged;
88+
bool _disconnectOnPoll;
8789
bool _tlsBadFingerprint;
8890
uint32_t _lastClientActivity;
8991
uint32_t _lastServerActivity;
9092
uint32_t _lastPingRequestTime;
9193

92-
char _generatedClientId[13 + 1]; // esp8266abc123
94+
char _generatedClientId[18 + 1]; // esp8266-abc123 and esp32-abcdef123456
9395
IPAddress _ip;
9496
const char* _host;
9597
bool _useIp;

0 commit comments

Comments
 (0)