From 94b9f33137933b8672099ebdd55dec0d09d70f94 Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Tue, 8 Aug 2023 13:53:55 +0200 Subject: [PATCH] state change debug logging --- src/MqttClient.cpp | 41 ++++++++++++++++++++++---------------- src/MqttClient.h | 1 + src/espMqttClientAsync.cpp | 4 ++-- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index c19997b..b78cedf 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -115,7 +115,7 @@ bool MqttClient::connect() { (uint16_t)(_keepAlive / 1000), // 32b to 16b doesn't overflow because it comes from 16b orignally _clientId)) { result = true; - _state = State::connectingTcp1; + _setState(State::connectingTcp1); #if defined(ARDUINO_ARCH_ESP32) if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { vTaskResume(_taskHandle); @@ -133,11 +133,11 @@ bool MqttClient::connect() { bool MqttClient::disconnect(bool force) { if (force && _state != State::disconnected && _state != State::disconnectingTcp1 && _state != State::disconnectingTcp2) { - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); return true; } if (!force && _state == State::connected) { - _state = State::disconnectingMqtt1; + _setState(State::disconnectingMqtt1); return true; } return false; @@ -205,9 +205,9 @@ void MqttClient::loop() { break; case State::connectingTcp1: if (_useIp ? _transport->connect(_ip, _port) : _transport->connect(_host, _port)) { - _state = State::connectingTcp2; + _setState(State::connectingTcp2); } else { - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); _disconnectReason = DisconnectReason::TCP_DISCONNECTED; break; } @@ -217,7 +217,7 @@ void MqttClient::loop() { if (_transport->connected()) { _parser.reset(); _lastClientActivity = _lastServerActivity = millis(); - _state = State::connectingMqtt; + _setState(State::connectingMqtt); } break; case State::connectingMqtt: @@ -227,7 +227,7 @@ void MqttClient::loop() { _checkIncoming(); _checkPing(); } else { - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); _disconnectReason = DisconnectReason::TCP_DISCONNECTED; } break; @@ -247,7 +247,7 @@ void MqttClient::loop() { _checkPing(); _checkTimeout(); } else { - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); _disconnectReason = DisconnectReason::TCP_DISCONNECTED; } break; @@ -259,7 +259,7 @@ void MqttClient::loop() { emc_log_e("Could not create DISCONNECT packet"); _onError(0, Error::OUT_OF_MEMORY); } else { - _state = State::disconnectingMqtt2; + _setState(State::disconnectingMqtt2); } } EMC_SEMAPHORE_GIVE(); @@ -270,13 +270,15 @@ void MqttClient::loop() { break; case State::disconnectingTcp1: _transport->stop(); - _state = State::disconnectingTcp2; + _setState(State::disconnectingTcp2); + // Test Issue #106 //break; // keep break to accomodate async clients + [[fallthrough]]; case State::disconnectingTcp2: if (_transport->disconnected()) { _clearQueue(0); _bytesSent = 0; - _state = State::disconnected; + _setState(State::disconnected); if (_onDisconnectCallback) _onDisconnectCallback(_disconnectReason); } break; @@ -308,6 +310,11 @@ void MqttClient::_loop(MqttClient* c) { } #endif +inline void MqttClient::_setState(State state) { + emc_log_i("new state: %u", static_cast(state)); + _state = state; +} + uint16_t MqttClient::_getNextPacketId() { uint16_t packetId = 0; EMC_SEMAPHORE_TAKE(); @@ -352,7 +359,7 @@ bool MqttClient::_advanceOutbox() { OutgoingPacket* packet = _outbox.getCurrent(); if (packet && _bytesSent == packet->packet.size()) { if ((packet->packet.packetType()) == PacketType.DISCONNECT) { - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); _disconnectReason = DisconnectReason::USER_OK; } if (packet->packet.removable()) { @@ -382,7 +389,7 @@ void MqttClient::_checkIncoming() { espMqttClientInternals::MQTTPacketType packetType = _parser.getPacket().fixedHeader.packetType & 0xF0; if (_state == State::connectingMqtt && packetType != PacketType.CONNACK) { emc_log_w("Disconnecting, expected CONNACK - protocol error"); - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); return; } switch (packetType & 0xF0) { @@ -420,7 +427,7 @@ void MqttClient::_checkIncoming() { } } else if (result == espMqttClientInternals::ParserResult::protocolError) { emc_log_w("Disconnecting, protocol error"); - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); _disconnectReason = DisconnectReason::TCP_DISCONNECTED; return; } @@ -440,7 +447,7 @@ void MqttClient::_checkPing() { // disconnect when server was inactive for twice the keepalive time if (currentMillis - _lastServerActivity > 2 * _keepAlive) { emc_log_w("Disconnecting, server exceeded keepalive"); - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); _disconnectReason = DisconnectReason::TCP_DISCONNECTED; return; } @@ -478,7 +485,7 @@ void MqttClient::_checkTimeout() { void MqttClient::_onConnack() { if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) { _pingSent = false; // reset after keepalive timeout disconnect - _state = State::connected; + _setState(State::connected); _advanceOutbox(); if (_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent == 0) { _clearQueue(1); @@ -487,7 +494,7 @@ void MqttClient::_onConnack() { _onConnectCallback(_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent); } } else { - _state = State::disconnectingTcp1; + _setState(State::disconnectingTcp1); // cast is safe because the parser already checked for a valid return code _disconnectReason = static_cast(_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode); } diff --git a/src/MqttClient.h b/src/MqttClient.h index 8d89aba..de41b1c 100644 --- a/src/MqttClient.h +++ b/src/MqttClient.h @@ -110,6 +110,7 @@ class MqttClient { disconnectingTcp2 = 8 }; std::atomic _state; + inline void _setState(State state); private: char _generatedClientId[EMC_CLIENTID_LENGTH]; diff --git a/src/espMqttClientAsync.cpp b/src/espMqttClientAsync.cpp index 98b7f15..722e983 100644 --- a/src/espMqttClientAsync.cpp +++ b/src/espMqttClientAsync.cpp @@ -33,7 +33,7 @@ void espMqttClientAsync::_setupClient(espMqttClientAsync* c) { void espMqttClientAsync::onConnectCb(void* a, AsyncClient* c) { c->setNoDelay(true); espMqttClientAsync* client = reinterpret_cast(a); - client->_state = MqttClient::State::connectingTcp2; + client->_setState(MqttClient::State::connectingTcp2); client->loop(); } @@ -48,7 +48,7 @@ void espMqttClientAsync::onDataCb(void* a, AsyncClient* c, void* data, size_t le void espMqttClientAsync::onDisconnectCb(void* a, AsyncClient* c) { (void)c; espMqttClientAsync* client = reinterpret_cast(a); - client->_state = MqttClient::State::disconnectingTcp2; + client->_setState(MqttClient::State::disconnectingTcp2); client->loop(); }