From 41666458d9eb0154c141033f46321ea1417247ed Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Wed, 1 Nov 2023 18:18:56 +0100 Subject: [PATCH] merge mqttClinet PR 115 from BertMelis --- lib/espMqttClient/src/MqttClient.cpp | 22 ++++++------------- .../src/Transport/ClientPosix.cpp | 3 +-- lib/espMqttClient/src/Transport/ClientPosix.h | 2 +- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp index 18de7ec74..51d67426d 100644 --- a/lib/espMqttClient/src/MqttClient.cpp +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -14,12 +14,8 @@ using espMqttClientTypes::DisconnectReason; using espMqttClientTypes::Error; MqttClient::MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority, uint8_t core) -#if defined(ARDUINO_ARCH_ESP32) : _useInternalTask(useInternalTask) , _transport(nullptr) -#else - : _transport(nullptr) -#endif , _onConnectCallback(nullptr) , _onDisconnectCallback(nullptr) , _onSubscribeCallback(nullptr) @@ -153,8 +149,8 @@ uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, const #endif return 0; } - uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; EMC_SEMAPHORE_TAKE(); + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; if (!_addPacket(packetId, topic, payload, length, qos, retain)) { emc_log_e("Could not create PUBLISH packet"); _onError(packetId, Error::OUT_OF_MEMORY); @@ -177,8 +173,8 @@ uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, espMq #endif return 0; } - uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; EMC_SEMAPHORE_TAKE(); + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; if (!_addPacket(packetId, topic, callback, length, qos, retain)) { emc_log_e("Could not create PUBLISH packet"); _onError(packetId, Error::OUT_OF_MEMORY); @@ -320,12 +316,9 @@ void MqttClient::_loop(MqttClient * c) { #endif uint16_t MqttClient::_getNextPacketId() { - uint16_t packetId = 0; - EMC_SEMAPHORE_TAKE(); - // cppcheck-suppress knownConditionTrueFalse - packetId = (++_packetId == 0) ? ++_packetId : _packetId; - EMC_SEMAPHORE_GIVE(); - return packetId; + ++_packetId; + if (_packetId == 0) ++_packetId; + return _packetId; } void MqttClient::_checkOutbox() { @@ -340,10 +333,9 @@ int MqttClient::_sendPacket() { EMC_SEMAPHORE_TAKE(); OutgoingPacket * packet = _outbox.getCurrent(); - size_t wantToWrite = 0; size_t written = 0; - if (packet && (wantToWrite == written)) { - wantToWrite = packet->packet.available(_bytesSent); + if (packet) { + size_t wantToWrite = packet->packet.available(_bytesSent); if (wantToWrite == 0) { EMC_SEMAPHORE_GIVE(); return 0; diff --git a/lib/espMqttClient/src/Transport/ClientPosix.cpp b/lib/espMqttClient/src/Transport/ClientPosix.cpp index edccad065..82f16b449 100644 --- a/lib/espMqttClient/src/Transport/ClientPosix.cpp +++ b/lib/espMqttClient/src/Transport/ClientPosix.cpp @@ -40,8 +40,7 @@ bool ClientPosix::connect(IPAddress ip, uint16_t port) { _host.sin_addr.s_addr = htonl(uint32_t(ip)); _host.sin_port = htons(port); // modified by proddy for EMS-ESP compiling standalone - int ret = ::connect(_sockfd, (struct sockaddr *)&_host, sizeof(_host)); - + int ret = ::connect(_sockfd, reinterpret_cast(&_host), sizeof(_host)); if (ret < 0) { emc_log_e("Error connecting: %d - (%d) %s", ret, errno, strerror(errno)); return false; diff --git a/lib/espMqttClient/src/Transport/ClientPosix.h b/lib/espMqttClient/src/Transport/ClientPosix.h index a2f9c9fae..af0dd4bff 100644 --- a/lib/espMqttClient/src/Transport/ClientPosix.h +++ b/lib/espMqttClient/src/Transport/ClientPosix.h @@ -43,7 +43,7 @@ class ClientPosix : public Transport { protected: int _sockfd; - struct sockaddr_in _host; + sockaddr_in _host; }; } // namespace espMqttClientInternals