Skip to content

Commit

Permalink
state change debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bertmelis committed Aug 8, 2023
1 parent 7ce3524 commit 94b9f33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
41 changes: 24 additions & 17 deletions src/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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:
Expand All @@ -227,7 +227,7 @@ void MqttClient::loop() {
_checkIncoming();
_checkPing();
} else {
_state = State::disconnectingTcp1;
_setState(State::disconnectingTcp1);
_disconnectReason = DisconnectReason::TCP_DISCONNECTED;
}
break;
Expand All @@ -247,7 +247,7 @@ void MqttClient::loop() {
_checkPing();
_checkTimeout();
} else {
_state = State::disconnectingTcp1;
_setState(State::disconnectingTcp1);
_disconnectReason = DisconnectReason::TCP_DISCONNECTED;
}
break;
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -308,6 +310,11 @@ void MqttClient::_loop(MqttClient* c) {
}
#endif

inline void MqttClient::_setState(State state) {
emc_log_i("new state: %u", static_cast<int>(state));
_state = state;
}

uint16_t MqttClient::_getNextPacketId() {
uint16_t packetId = 0;
EMC_SEMAPHORE_TAKE();
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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<DisconnectReason>(_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode);
}
Expand Down
1 change: 1 addition & 0 deletions src/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class MqttClient {
disconnectingTcp2 = 8
};
std::atomic<State> _state;
inline void _setState(State state);

private:
char _generatedClientId[EMC_CLIENTID_LENGTH];
Expand Down
4 changes: 2 additions & 2 deletions src/espMqttClientAsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void espMqttClientAsync::_setupClient(espMqttClientAsync* c) {
void espMqttClientAsync::onConnectCb(void* a, AsyncClient* c) {
c->setNoDelay(true);
espMqttClientAsync* client = reinterpret_cast<espMqttClientAsync*>(a);
client->_state = MqttClient::State::connectingTcp2;
client->_setState(MqttClient::State::connectingTcp2);
client->loop();
}

Expand All @@ -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<espMqttClientAsync*>(a);
client->_state = MqttClient::State::disconnectingTcp2;
client->_setState(MqttClient::State::disconnectingTcp2);
client->loop();
}

Expand Down

0 comments on commit 94b9f33

Please sign in to comment.