diff --git a/CMakeLists.txt b/CMakeLists.txt index 70400c4..1330eb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ add_compile_definitions(TLS_KEY_MAX_SIZE=1800) add_compile_definitions(TX_PAYLOAD_BUFFER_SIZE=6144) add_compile_definitions(ESP_PLATFORM) add_compile_definitions(ESP32) +add_compile_definitions(ARDUINO_ARCH_ESP32) include_directories(${PROJECT_NAME} PRIVATE @@ -31,10 +32,11 @@ include_directories(${PROJECT_NAME} lib/BleScanner/src lib/nuki_ble/src lib/WiFiManager - lib/ArduinoMqttClient/src lib/WebServer/src lib/Ethernet/src lib/MqttLogger/src + lib/espMqttClient/src + lib/AsyncTCP/src ) file(GLOB SRCFILES @@ -73,9 +75,8 @@ file(GLOB SRCFILES lib/nuki_ble/src/NukiOpenerUtils.cpp lib/BleScanner/src/BleInterfaces.h lib/BleScanner/src/BleScanner.cpp - lib/ArduinoMqttClient/src/MqttClient.cpp - lib/ArduinoMqttClient/src/ArduinoMqttClient.h lib/MqttLogger/src/MqttLogger.cpp + lib/AsyncTCP/src/AsyncTCP.cpp ) file(GLOB_RECURSE SRCFILESREC @@ -86,6 +87,12 @@ file(GLOB_RECURSE SRCFILESREC lib/WebServer/src/*.h lib/Ethernet/src/*.cpp lib/Ethernet/src/*.h + lib/espMqttClient/src/*.cpp + lib/espMqttClient/src/*.h + lib/espMqttClient/src/Packets/*.cpp + lib/espMqttClient/src/Packets/*.h + lib/espMqttClient/src/Transport/*.cpp + lib/espMqttClient/src/Transport/*.h ) add_executable(${PROJECT_NAME} diff --git a/Network.cpp b/Network.cpp index 6a1c4cc..1b5c1f7 100644 --- a/Network.cpp +++ b/Network.cpp @@ -133,8 +133,8 @@ void Network::initialize() Log->print(F(":")); Log->println(port); - _device->mqttClient()->setId(_preferences->getString(preference_hostname)); - _device->mqttClient()->setCleanSession(MQTT_CLEAN_SESSIONS); + // TODO +// _device->mqttClient()->setId(_preferences->getString(preference_hostname)); _networkTimeout = _preferences->getInt(preference_network_timeout); if(_networkTimeout == 0) @@ -231,7 +231,8 @@ int Network::update() _lastMaintenanceTs = ts; } - _device->mqttClient()->poll(); + // TODO +// _device->mqttClient()->poll(); return 0; } @@ -248,13 +249,15 @@ bool Network::reconnect() if(strlen(_mqttUser) == 0) { Log->println(F("MQTT: Connecting without credentials")); - success = _device->mqttClient()->connect(_mqttBrokerAddr, port); + _device->mqttClient()->setServer(_mqttBrokerAddr, port); + success = _device->mqttClient()->connect(); } else { Log->print(F("MQTT: Connecting with user: ")); Log->println(_mqttUser); - _device->mqttClient()->setUsernamePassword(_mqttUser, _mqttPass); - success = _device->mqttClient()->connect(_mqttBrokerAddr, port); + _device->mqttClient()->setCredentials(_mqttUser, _mqttPass); + _device->mqttClient()->setServer(_mqttBrokerAddr, port); + success = _device->mqttClient()->connect(); } if (success) @@ -262,6 +265,8 @@ bool Network::reconnect() Log->println(F("MQTT connected")); _mqttConnectionState = 1; delay(100); + + // TODO _device->mqttClient()->onMessage(Network::onMqttDataReceivedCallback); for(const String& topic : _subscribedTopics) { @@ -272,14 +277,12 @@ bool Network::reconnect() _firstConnect = false; for(const auto& it : _initTopics) { - _device->mqttClient()->beginMessage(it.first, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(it.second); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str()); } } for(int i=0; i<10; i++) { - _device->mqttClient()->poll(); +// _device->mqttClient()->poll(); delay(100); } _mqttConnectionState = 2; @@ -287,9 +290,9 @@ bool Network::reconnect() else { Log->print(F("MQTT connect failed, rc=")); - Log->println(_device->mqttClient()->connectError()); +// Log->println(_device->mqttClient()->connectError()); _device->printError(); - _device->mqttClient()->stop(); +// _device->mqttClient()->stop(); _mqttConnectionState = 0; _nextReconnect = millis() + 5000; } @@ -340,33 +343,20 @@ void Network::registerMqttReceiver(MqttReceiver* receiver) _mqttReceivers.push_back(receiver); } -void Network::onMqttDataReceivedCallback(int messageSize) +void Network::onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { - _inst->onMqttDataReceived(messageSize); + _inst->onMqttDataReceived(properties, topic, payload, len, index, total); } -void Network::onMqttDataReceived(int) +void Network::onMqttDataReceived(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { - MqttClient* mqttClient = _device->mqttClient(); - String topic = mqttClient->messageTopic(); - - byte payload[500]; - memset(payload, 0, sizeof(payload)); - - int index = 0; - while (mqttClient->available() && index < sizeof(payload)) - { - payload[index] = mqttClient->read(); - ++index; - } - for(auto receiver : _mqttReceivers) { - receiver->onMqttDataReceived(topic.c_str(), payload, index); + receiver->onMqttDataReceived(topic, (byte*)payload, index); } } -MqttClient *Network::mqttClient() +MqttClientSetup *Network::mqttClient() { return _device->mqttClient(); } @@ -400,9 +390,7 @@ void Network::publishFloat(const char* prefix, const char* topic, const float va dtostrf(value, 0, precision, str); char path[200] = {0}; buildMqttPath(prefix, topic, path); - _device->mqttClient()->beginMessage(path, true); - _device->mqttClient()->print(str); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str); } void Network::publishInt(const char* prefix, const char *topic, const int value) @@ -411,9 +399,7 @@ void Network::publishInt(const char* prefix, const char *topic, const int value) itoa(value, str, 10); char path[200] = {0}; buildMqttPath(prefix, topic, path); - _device->mqttClient()->beginMessage(path, true); - _device->mqttClient()->print(str); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str); } void Network::publishUInt(const char* prefix, const char *topic, const unsigned int value) @@ -422,9 +408,7 @@ void Network::publishUInt(const char* prefix, const char *topic, const unsigned utoa(value, str, 10); char path[200] = {0}; buildMqttPath(prefix, topic, path); - _device->mqttClient()->beginMessage(path, true); - _device->mqttClient()->print(str); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str); } void Network::publishULong(const char* prefix, const char *topic, const unsigned long value) @@ -433,9 +417,7 @@ void Network::publishULong(const char* prefix, const char *topic, const unsigned utoa(value, str, 10); char path[200] = {0}; buildMqttPath(prefix, topic, path); - _device->mqttClient()->beginMessage(path, true); - _device->mqttClient()->print(str); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str); } void Network::publishBool(const char* prefix, const char *topic, const bool value) @@ -444,19 +426,14 @@ void Network::publishBool(const char* prefix, const char *topic, const bool valu str[0] = value ? '1' : '0'; char path[200] = {0}; buildMqttPath(prefix, topic, path); - _device->mqttClient()->beginMessage(path, true); - _device->mqttClient()->print(str); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, str); } bool Network::publishString(const char* prefix, const char *topic, const char *value) { char path[200] = {0}; buildMqttPath(prefix, topic, path); - _device->mqttClient()->beginMessage(path, true); - _device->mqttClient()->print(value); - bool success = _device->mqttClient()->endMessage() > 0; - return success; + return _device->mqttClient()->publish(path, MQTT_QOS_LEVEL, true, value) > 0; } void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState) @@ -498,9 +475,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n path.concat(uidString); path.concat("/smartlock/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); // Battery critical configJSON = "{\"dev\":{\"ids\":[\"nuki_"; @@ -524,9 +499,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n path.concat(uidString); path.concat("/battery_low/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); // Keypad battery critical configJSON = "{\"dev\":{\"ids\":[\"nuki_"; @@ -550,9 +523,8 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n path.concat(uidString); path.concat("/keypad_battery_low/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); + // Battery voltage configJSON = "{\"dev\":{\"ids\":[\"nuki_"; configJSON.concat(uidString); @@ -577,9 +549,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n path.concat(uidString); path.concat("/battery_voltage/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); // Trigger configJSON = "{\"dev\":{\"ids\":[\"nuki_"; @@ -604,9 +574,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n path.concat(uidString); path.concat("/trigger/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); } } @@ -642,9 +610,7 @@ void Network::publishHASSConfigBatLevel(char *deviceType, const char *baseTopic, path.concat(uidString); path.concat("/battery_level/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); } } @@ -679,9 +645,7 @@ void Network::publishHASSConfigDoorSensor(char *deviceType, const char *baseTopi path.concat(uidString); path.concat("/door_sensor/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); } } @@ -715,9 +679,7 @@ void Network::publishHASSConfigRingDetect(char *deviceType, const char *baseTopi path.concat(uidString); path.concat("/ring/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); } } @@ -755,9 +717,7 @@ void Network::publishHASSWifiRssiConfig(char *deviceType, const char *baseTopic, path.concat(uidString); path.concat("/wifi_signal_strength/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); } } @@ -789,9 +749,7 @@ void Network::publishHASSBleRssiConfig(char *deviceType, const char *baseTopic, path.concat(uidString); path.concat("/bluetooth_signal_strength/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(configJSON); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, configJSON.c_str()); } } @@ -805,73 +763,55 @@ void Network::removeHASSConfig(char* uidString) path.concat("/lock/"); path.concat(uidString); path.concat("/smartlock/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/binary_sensor/"); path.concat(uidString); path.concat("/battery_low/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/sensor/"); path.concat(uidString); path.concat("/battery_voltage/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/sensor/"); path.concat(uidString); path.concat("/trigger/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/sensor/"); path.concat(uidString); path.concat("/battery_level/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/binary_sensor/"); path.concat(uidString); path.concat("/door_sensor/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/binary_sensor/"); path.concat(uidString); path.concat("/ring/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/sensor/"); path.concat(uidString); path.concat("/wifi_signal_strength/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); path = discoveryTopic; path.concat("/sensor/"); path.concat(uidString); path.concat("/bluetooth_signal_strength/config"); - _device->mqttClient()->beginMessage(path, true, MQTT_QOS_LEVEL); - _device->mqttClient()->print(""); - _device->mqttClient()->endMessage(); + _device->mqttClient()->publish(path.c_str(), MQTT_QOS_LEVEL, true, ""); } } diff --git a/Network.h b/Network.h index 953094b..437f4ac 100644 --- a/Network.h +++ b/Network.h @@ -43,14 +43,14 @@ public: void publishPresenceDetection(char* csv); - MqttClient* mqttClient(); + MqttClientSetup* mqttClient(); int mqttConnectionState(); // 0 = not connected; 1 = connected; 2 = connected and mqtt processed const NetworkDeviceType networkDeviceType(); private: - static void onMqttDataReceivedCallback(int); - void onMqttDataReceived(int messageSize); + static void onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total); + void onMqttDataReceived(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total); void setupDevice(); bool reconnect(); diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index 534fc71..32a61cc 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -1,10 +1,9 @@ #include "NetworkOpener.h" -#include // https://github.com/tzapu/WiFiManager #include "Arduino.h" #include "MqttTopics.h" #include "PreferencesKeys.h" -#include "Pins.h" #include "Logger.h" +#include "Config.h" NetworkOpener::NetworkOpener(Network* network, Preferences* preferences) : _preferences(preferences), @@ -483,7 +482,7 @@ void NetworkOpener::subscribe(const char *path) { char prefixedPath[500]; buildMqttPath(path, prefixedPath); - _network->mqttClient()->subscribe(prefixedPath); + _network->mqttClient()->subscribe(prefixedPath, MQTT_QOS_LEVEL); } bool NetworkOpener::comparePrefixedPath(const char *fullPath, const char *subPath) diff --git a/lib/ArduinoMqttClient/.codespellrc b/lib/ArduinoMqttClient/.codespellrc deleted file mode 100644 index 101edae..0000000 --- a/lib/ArduinoMqttClient/.codespellrc +++ /dev/null @@ -1,7 +0,0 @@ -# See: https://github.com/codespell-project/codespell#using-a-config-file -[codespell] -# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: -ignore-words-list = , -check-filenames = -check-hidden = -skip = ./.git diff --git a/lib/ArduinoMqttClient/LICENSE.txt b/lib/ArduinoMqttClient/LICENSE.txt deleted file mode 100644 index 8000a6f..0000000 --- a/lib/ArduinoMqttClient/LICENSE.txt +++ /dev/null @@ -1,504 +0,0 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 2.1, February 1999 - - Copyright (C) 1991, 1999 Free Software Foundation, Inc. - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - -[This is the first released version of the Lesser GPL. It also counts - as the successor of the GNU Library Public License, version 2, hence - the version number 2.1.] - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -Licenses are intended to guarantee your freedom to share and change -free software--to make sure the software is free for all its users. - - This license, the Lesser General Public License, applies to some -specially designated software packages--typically libraries--of the -Free Software Foundation and other authors who decide to use it. You -can use it too, but we suggest you first think carefully about whether -this license or the ordinary General Public License is the better -strategy to use in any particular case, based on the explanations below. - - When we speak of free software, we are referring to freedom of use, -not price. Our General Public Licenses are designed to make sure that -you have the freedom to distribute copies of free software (and charge -for this service if you wish); that you receive source code or can get -it if you want it; that you can change the software and use pieces of -it in new free programs; and that you are informed that you can do -these things. - - To protect your rights, we need to make restrictions that forbid -distributors to deny you these rights or to ask you to surrender these -rights. These restrictions translate to certain responsibilities for -you if you distribute copies of the library or if you modify it. - - For example, if you distribute copies of the library, whether gratis -or for a fee, you must give the recipients all the rights that we gave -you. You must make sure that they, too, receive or can get the source -code. If you link other code with the library, you must provide -complete object files to the recipients, so that they can relink them -with the library after making changes to the library and recompiling -it. And you must show them these terms so they know their rights. - - We protect your rights with a two-step method: (1) we copyright the -library, and (2) we offer you this license, which gives you legal -permission to copy, distribute and/or modify the library. - - To protect each distributor, we want to make it very clear that -there is no warranty for the free library. Also, if the library is -modified by someone else and passed on, the recipients should know -that what they have is not the original version, so that the original -author's reputation will not be affected by problems that might be -introduced by others. - - Finally, software patents pose a constant threat to the existence of -any free program. We wish to make sure that a company cannot -effectively restrict the users of a free program by obtaining a -restrictive license from a patent holder. Therefore, we insist that -any patent license obtained for a version of the library must be -consistent with the full freedom of use specified in this license. - - Most GNU software, including some libraries, is covered by the -ordinary GNU General Public License. This license, the GNU Lesser -General Public License, applies to certain designated libraries, and -is quite different from the ordinary General Public License. We use -this license for certain libraries in order to permit linking those -libraries into non-free programs. - - When a program is linked with a library, whether statically or using -a shared library, the combination of the two is legally speaking a -combined work, a derivative of the original library. The ordinary -General Public License therefore permits such linking only if the -entire combination fits its criteria of freedom. The Lesser General -Public License permits more lax criteria for linking other code with -the library. - - We call this license the "Lesser" General Public License because it -does Less to protect the user's freedom than the ordinary General -Public License. It also provides other free software developers Less -of an advantage over competing non-free programs. These disadvantages -are the reason we use the ordinary General Public License for many -libraries. However, the Lesser license provides advantages in certain -special circumstances. - - For example, on rare occasions, there may be a special need to -encourage the widest possible use of a certain library, so that it becomes -a de-facto standard. To achieve this, non-free programs must be -allowed to use the library. A more frequent case is that a free -library does the same job as widely used non-free libraries. In this -case, there is little to gain by limiting the free library to free -software only, so we use the Lesser General Public License. - - In other cases, permission to use a particular library in non-free -programs enables a greater number of people to use a large body of -free software. For example, permission to use the GNU C Library in -non-free programs enables many more people to use the whole GNU -operating system, as well as its variant, the GNU/Linux operating -system. - - Although the Lesser General Public License is Less protective of the -users' freedom, it does ensure that the user of a program that is -linked with the Library has the freedom and the wherewithal to run -that program using a modified version of the Library. - - The precise terms and conditions for copying, distribution and -modification follow. Pay close attention to the difference between a -"work based on the library" and a "work that uses the library". The -former contains code derived from the library, whereas the latter must -be combined with the library in order to run. - - GNU LESSER GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License Agreement applies to any software library or other -program which contains a notice placed by the copyright holder or -other authorized party saying it may be distributed under the terms of -this Lesser General Public License (also called "this License"). -Each licensee is addressed as "you". - - A "library" means a collection of software functions and/or data -prepared so as to be conveniently linked with application programs -(which use some of those functions and data) to form executables. - - The "Library", below, refers to any such software library or work -which has been distributed under these terms. A "work based on the -Library" means either the Library or any derivative work under -copyright law: that is to say, a work containing the Library or a -portion of it, either verbatim or with modifications and/or translated -straightforwardly into another language. (Hereinafter, translation is -included without limitation in the term "modification".) - - "Source code" for a work means the preferred form of the work for -making modifications to it. For a library, complete source code means -all the source code for all modules it contains, plus any associated -interface definition files, plus the scripts used to control compilation -and installation of the library. - - Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running a program using the Library is not restricted, and output from -such a program is covered only if its contents constitute a work based -on the Library (independent of the use of the Library in a tool for -writing it). Whether that is true depends on what the Library does -and what the program that uses the Library does. - - 1. You may copy and distribute verbatim copies of the Library's -complete source code as you receive it, in any medium, provided that -you conspicuously and appropriately publish on each copy an -appropriate copyright notice and disclaimer of warranty; keep intact -all the notices that refer to this License and to the absence of any -warranty; and distribute a copy of this License along with the -Library. - - You may charge a fee for the physical act of transferring a copy, -and you may at your option offer warranty protection in exchange for a -fee. - - 2. You may modify your copy or copies of the Library or any portion -of it, thus forming a work based on the Library, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) The modified work must itself be a software library. - - b) You must cause the files modified to carry prominent notices - stating that you changed the files and the date of any change. - - c) You must cause the whole of the work to be licensed at no - charge to all third parties under the terms of this License. - - d) If a facility in the modified Library refers to a function or a - table of data to be supplied by an application program that uses - the facility, other than as an argument passed when the facility - is invoked, then you must make a good faith effort to ensure that, - in the event an application does not supply such function or - table, the facility still operates, and performs whatever part of - its purpose remains meaningful. - - (For example, a function in a library to compute square roots has - a purpose that is entirely well-defined independent of the - application. Therefore, Subsection 2d requires that any - application-supplied function or table used by this function must - be optional: if the application does not supply it, the square - root function must still compute square roots.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Library, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Library, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote -it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Library. - -In addition, mere aggregation of another work not based on the Library -with the Library (or with a work based on the Library) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may opt to apply the terms of the ordinary GNU General Public -License instead of this License to a given copy of the Library. To do -this, you must alter all the notices that refer to this License, so -that they refer to the ordinary GNU General Public License, version 2, -instead of to this License. (If a newer version than version 2 of the -ordinary GNU General Public License has appeared, then you can specify -that version instead if you wish.) Do not make any other change in -these notices. - - Once this change is made in a given copy, it is irreversible for -that copy, so the ordinary GNU General Public License applies to all -subsequent copies and derivative works made from that copy. - - This option is useful when you wish to copy part of the code of -the Library into a program that is not a library. - - 4. You may copy and distribute the Library (or a portion or -derivative of it, under Section 2) in object code or executable form -under the terms of Sections 1 and 2 above provided that you accompany -it with the complete corresponding machine-readable source code, which -must be distributed under the terms of Sections 1 and 2 above on a -medium customarily used for software interchange. - - If distribution of object code is made by offering access to copy -from a designated place, then offering equivalent access to copy the -source code from the same place satisfies the requirement to -distribute the source code, even though third parties are not -compelled to copy the source along with the object code. - - 5. A program that contains no derivative of any portion of the -Library, but is designed to work with the Library by being compiled or -linked with it, is called a "work that uses the Library". Such a -work, in isolation, is not a derivative work of the Library, and -therefore falls outside the scope of this License. - - However, linking a "work that uses the Library" with the Library -creates an executable that is a derivative of the Library (because it -contains portions of the Library), rather than a "work that uses the -library". The executable is therefore covered by this License. -Section 6 states terms for distribution of such executables. - - When a "work that uses the Library" uses material from a header file -that is part of the Library, the object code for the work may be a -derivative work of the Library even though the source code is not. -Whether this is true is especially significant if the work can be -linked without the Library, or if the work is itself a library. The -threshold for this to be true is not precisely defined by law. - - If such an object file uses only numerical parameters, data -structure layouts and accessors, and small macros and small inline -functions (ten lines or less in length), then the use of the object -file is unrestricted, regardless of whether it is legally a derivative -work. (Executables containing this object code plus portions of the -Library will still fall under Section 6.) - - Otherwise, if the work is a derivative of the Library, you may -distribute the object code for the work under the terms of Section 6. -Any executables containing that work also fall under Section 6, -whether or not they are linked directly with the Library itself. - - 6. As an exception to the Sections above, you may also combine or -link a "work that uses the Library" with the Library to produce a -work containing portions of the Library, and distribute that work -under terms of your choice, provided that the terms permit -modification of the work for the customer's own use and reverse -engineering for debugging such modifications. - - You must give prominent notice with each copy of the work that the -Library is used in it and that the Library and its use are covered by -this License. You must supply a copy of this License. If the work -during execution displays copyright notices, you must include the -copyright notice for the Library among them, as well as a reference -directing the user to the copy of this License. Also, you must do one -of these things: - - a) Accompany the work with the complete corresponding - machine-readable source code for the Library including whatever - changes were used in the work (which must be distributed under - Sections 1 and 2 above); and, if the work is an executable linked - with the Library, with the complete machine-readable "work that - uses the Library", as object code and/or source code, so that the - user can modify the Library and then relink to produce a modified - executable containing the modified Library. (It is understood - that the user who changes the contents of definitions files in the - Library will not necessarily be able to recompile the application - to use the modified definitions.) - - b) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (1) uses at run time a - copy of the library already present on the user's computer system, - rather than copying library functions into the executable, and (2) - will operate properly with a modified version of the library, if - the user installs one, as long as the modified version is - interface-compatible with the version that the work was made with. - - c) Accompany the work with a written offer, valid for at - least three years, to give the same user the materials - specified in Subsection 6a, above, for a charge no more - than the cost of performing this distribution. - - d) If distribution of the work is made by offering access to copy - from a designated place, offer equivalent access to copy the above - specified materials from the same place. - - e) Verify that the user has already received a copy of these - materials or that you have already sent this user a copy. - - For an executable, the required form of the "work that uses the -Library" must include any data and utility programs needed for -reproducing the executable from it. However, as a special exception, -the materials to be distributed need not include anything that is -normally distributed (in either source or binary form) with the major -components (compiler, kernel, and so on) of the operating system on -which the executable runs, unless that component itself accompanies -the executable. - - It may happen that this requirement contradicts the license -restrictions of other proprietary libraries that do not normally -accompany the operating system. Such a contradiction means you cannot -use both them and the Library together in an executable that you -distribute. - - 7. You may place library facilities that are a work based on the -Library side-by-side in a single library together with other library -facilities not covered by this License, and distribute such a combined -library, provided that the separate distribution of the work based on -the Library and of the other library facilities is otherwise -permitted, and provided that you do these two things: - - a) Accompany the combined library with a copy of the same work - based on the Library, uncombined with any other library - facilities. This must be distributed under the terms of the - Sections above. - - b) Give prominent notice with the combined library of the fact - that part of it is a work based on the Library, and explaining - where to find the accompanying uncombined form of the same work. - - 8. You may not copy, modify, sublicense, link with, or distribute -the Library except as expressly provided under this License. Any -attempt otherwise to copy, modify, sublicense, link with, or -distribute the Library is void, and will automatically terminate your -rights under this License. However, parties who have received copies, -or rights, from you under this License will not have their licenses -terminated so long as such parties remain in full compliance. - - 9. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Library or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Library (or any work based on the -Library), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Library or works based on it. - - 10. Each time you redistribute the Library (or any work based on the -Library), the recipient automatically receives a license from the -original licensor to copy, distribute, link with or modify the Library -subject to these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties with -this License. - - 11. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Library at all. For example, if a patent -license would not permit royalty-free redistribution of the Library by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Library. - -If any portion of this section is held invalid or unenforceable under any -particular circumstance, the balance of the section is intended to apply, -and the section as a whole is intended to apply in other circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 12. If the distribution and/or use of the Library is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Library under this License may add -an explicit geographical distribution limitation excluding those countries, -so that distribution is permitted only in or among countries not thus -excluded. In such case, this License incorporates the limitation as if -written in the body of this License. - - 13. The Free Software Foundation may publish revised and/or new -versions of the Lesser General Public License from time to time. -Such new versions will be similar in spirit to the present version, -but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library -specifies a version number of this License which applies to it and -"any later version", you have the option of following the terms and -conditions either of that version or of any later version published by -the Free Software Foundation. If the Library does not specify a -license version number, you may choose any version ever published by -the Free Software Foundation. - - 14. If you wish to incorporate parts of the Library into other free -programs whose distribution conditions are incompatible with these, -write to the author to ask for permission. For software which is -copyrighted by the Free Software Foundation, write to the Free -Software Foundation; we sometimes make exceptions for this. Our -decision will be guided by the two goals of preserving the free status -of all derivatives of our free software and of promoting the sharing -and reuse of software generally. - - NO WARRANTY - - 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO -WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR -OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY -KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE -LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME -THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN -WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY -AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU -FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR -CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE -LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING -RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A -FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH -DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Libraries - - If you develop a new library, and you want it to be of the greatest -possible use to the public, we recommend making it free software that -everyone can redistribute and change. You can do so by permitting -redistribution under these terms (or, alternatively, under the terms of the -ordinary General Public License). - - To apply these terms, attach the following notices to the library. It is -safest to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least the -"copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 - USA - -Also add information on how to contact you by electronic and paper mail. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the library, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the - library `Frob' (a library for tweaking knobs) written by James Random - Hacker. - - , 1 April 1990 - Ty Coon, President of Vice - -That's all there is to it! diff --git a/lib/ArduinoMqttClient/README.adoc b/lib/ArduinoMqttClient/README.adoc deleted file mode 100644 index 050baca..0000000 --- a/lib/ArduinoMqttClient/README.adoc +++ /dev/null @@ -1,10 +0,0 @@ -:repository-owner: arduino-libraries -:repository-name: ArduinoMqttClient - -= {repository-name} Library for Arduino = - -image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-arduino.yml/badge.svg["Check Arduino status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-arduino.yml"] -image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/compile-examples.yml/badge.svg["Compile Examples status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/compile-examples.yml"] -image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/spell-check.yml/badge.svg["Spell Check status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/spell-check.yml"] - -Allows you to send and receive MQTT messages using Arduino. diff --git a/lib/ArduinoMqttClient/examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino b/lib/ArduinoMqttClient/examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino deleted file mode 100644 index 3aed2c6..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino +++ /dev/null @@ -1,190 +0,0 @@ -/* - ArduinoMqttClient - WiFi Advanced Callback - - This example connects to a MQTT broker and subscribes to a single topic, - it also publishes a message to another topic every 10 seconds. - When a message is received it prints the message to the Serial Monitor, - it uses the callback functionality of the library. - - It also demonstrates how to set the will message, get/set QoS, - duplicate and retain values of messages. - - The circuit: - - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board - - This example code is in the public domain. -*/ - -#include -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#elif defined(ARDUINO_SAMD_MKR1000) - #include -#elif defined(ARDUINO_ARCH_ESP8266) - #include -#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) - #include -#endif - -#include "arduino_secrets.h" -///////please enter your sensitive data in the Secret tab/arduino_secrets.h -char ssid[] = SECRET_SSID; // your network SSID (name) -char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) - -// To connect with SSL/TLS: -// 1) Change WiFiClient to WiFiSSLClient. -// 2) Change port value from 1883 to 8883. -// 3) Change broker value to a server with a known SSL/TLS root certificate -// flashed in the WiFi module. - -WiFiClient wifiClient; -MqttClient mqttClient(wifiClient); - -const char broker[] = "test.mosquitto.org"; -int port = 1883; -const char willTopic[] = "arduino/will"; -const char inTopic[] = "arduino/in"; -const char outTopic[] = "arduino/out"; - -const long interval = 10000; -unsigned long previousMillis = 0; - -int count = 0; - -void setup() { - //Initialize serial and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // attempt to connect to WiFi network: - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - while (WiFi.begin(ssid, pass) != WL_CONNECTED) { - // failed, retry - Serial.print("."); - delay(5000); - } - - Serial.println("You're connected to the network"); - Serial.println(); - - // You can provide a unique client ID, if not set the library uses Arduino-millis() - // Each client must have a unique client ID - // mqttClient.setId("clientId"); - - // You can provide a username and password for authentication - // mqttClient.setUsernamePassword("username", "password"); - - // By default the library connects with the "clean session" flag set, - // you can disable this behaviour by using - // mqttClient.setCleanSession(false); - - // set a will message, used by the broker when the connection dies unexpectedly - // you must know the size of the message beforehand, and it must be set before connecting - String willPayload = "oh no!"; - bool willRetain = true; - int willQos = 1; - - mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos); - mqttClient.print(willPayload); - mqttClient.endWill(); - - Serial.print("Attempting to connect to the MQTT broker: "); - Serial.println(broker); - - if (!mqttClient.connect(broker, port)) { - Serial.print("MQTT connection failed! Error code = "); - Serial.println(mqttClient.connectError()); - - while (1); - } - - Serial.println("You're connected to the MQTT broker!"); - Serial.println(); - - // set the message receive callback - mqttClient.onMessage(onMqttMessage); - - Serial.print("Subscribing to topic: "); - Serial.println(inTopic); - Serial.println(); - - // subscribe to a topic - // the second parameter sets the QoS of the subscription, - // the the library supports subscribing at QoS 0, 1, or 2 - int subscribeQos = 1; - - mqttClient.subscribe(inTopic, subscribeQos); - - // topics can be unsubscribed using: - // mqttClient.unsubscribe(inTopic); - - Serial.print("Waiting for messages on topic: "); - Serial.println(inTopic); - Serial.println(); -} - -void loop() { - // call poll() regularly to allow the library to receive MQTT messages and - // send MQTT keep alives which avoids being disconnected by the broker - mqttClient.poll(); - - // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay - // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info - unsigned long currentMillis = millis(); - - if (currentMillis - previousMillis >= interval) { - // save the last time a message was sent - previousMillis = currentMillis; - - String payload; - - payload += "hello world!"; - payload += " "; - payload += count; - - Serial.print("Sending message to topic: "); - Serial.println(outTopic); - Serial.println(payload); - - // send message, the Print interface can be used to set the message contents - // in this case we know the size ahead of time, so the message payload can be streamed - - bool retained = false; - int qos = 1; - bool dup = false; - - mqttClient.beginMessage(outTopic, payload.length(), retained, qos, dup); - mqttClient.print(payload); - mqttClient.endMessage(); - - Serial.println(); - - count++; - } -} - -void onMqttMessage(int messageSize) { - // we received a message, print out the topic and contents - Serial.print("Received a message with topic '"); - Serial.print(mqttClient.messageTopic()); - Serial.print("', duplicate = "); - Serial.print(mqttClient.messageDup() ? "true" : "false"); - Serial.print(", QoS = "); - Serial.print(mqttClient.messageQoS()); - Serial.print(", retained = "); - Serial.print(mqttClient.messageRetain() ? "true" : "false"); - Serial.print("', length "); - Serial.print(messageSize); - Serial.println(" bytes:"); - - // use the Stream interface to print the contents - while (mqttClient.available()) { - Serial.print((char)mqttClient.read()); - } - Serial.println(); - - Serial.println(); -} diff --git a/lib/ArduinoMqttClient/examples/WiFiAdvancedCallback/arduino_secrets.h b/lib/ArduinoMqttClient/examples/WiFiAdvancedCallback/arduino_secrets.h deleted file mode 100644 index 0c9fdd5..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiAdvancedCallback/arduino_secrets.h +++ /dev/null @@ -1,2 +0,0 @@ -#define SECRET_SSID "" -#define SECRET_PASS "" diff --git a/lib/ArduinoMqttClient/examples/WiFiEcho/WiFiEcho.ino b/lib/ArduinoMqttClient/examples/WiFiEcho/WiFiEcho.ino deleted file mode 100644 index 840f088..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiEcho/WiFiEcho.ino +++ /dev/null @@ -1,146 +0,0 @@ -/* - ArduinoMqttClient - WiFi Echo - - This example connects to a MQTT broker and subscribes to a single topic, - it also publishes a message to the same topic once a second. - When a message is received it prints the message to the Serial Monitor. - - The circuit: - - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board - - This example code is in the public domain. -*/ - -#include -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#elif defined(ARDUINO_SAMD_MKR1000) - #include -#elif defined(ARDUINO_ARCH_ESP8266) - #include -#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) - #include -#endif - -#include "arduino_secrets.h" -///////please enter your sensitive data in the Secret tab/arduino_secrets.h -char ssid[] = SECRET_SSID; // your network SSID (name) -char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) - -// To connect with SSL/TLS: -// 1) Change WiFiClient to WiFiSSLClient. -// 2) Change port value from 1883 to 8883. -// 3) Change broker value to a server with a known SSL/TLS root certificate -// flashed in the WiFi module. - -WiFiClient wifiClient; -MqttClient mqttClient(wifiClient); - -const char broker[] = "test.mosquitto.org"; -int port = 1883; - -const char topic[] = "arduino/echo"; - -const long interval = 1000; -unsigned long previousMillis = 0; - -int count = 0; - -void setup() { - //Initialize serial and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // attempt to connect to WiFi network: - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - while (WiFi.begin(ssid, pass) != WL_CONNECTED) { - // failed, retry - Serial.print("."); - delay(5000); - } - - Serial.println("You're connected to the network"); - Serial.println(); - - // You can provide a unique client ID, if not set the library uses Arduino-millis() - // Each client must have a unique client ID - // mqttClient.setId("clientId"); - - // You can provide a username and password for authentication - // mqttClient.setUsernamePassword("username", "password"); - - Serial.print("Attempting to connect to the MQTT broker: "); - Serial.println(broker); - - if (!mqttClient.connect(broker, port)) { - Serial.print("MQTT connection failed! Error code = "); - Serial.println(mqttClient.connectError()); - - while (1); - } - - Serial.println("You're connected to the MQTT broker!"); - Serial.println(); - - Serial.print("Subscribing to topic: "); - Serial.println(topic); - Serial.println(); - - // subscribe to a topic - mqttClient.subscribe(topic); - - // topics can be unsubscribed using: - // mqttClient.unsubscribe(topic); - - Serial.print("Waiting for messages on topic: "); - Serial.println(topic); - Serial.println(); -} - -void loop() { - // check for incoming messages - int messageSize = mqttClient.parseMessage(); - if (messageSize) { - // we received a message, print out the topic and contents - Serial.print("Received a message with topic '"); - Serial.print(mqttClient.messageTopic()); - Serial.print("', length "); - Serial.print(messageSize); - Serial.println(" bytes:"); - - // use the Stream interface to print the contents - while (mqttClient.available()) { - Serial.print((char)mqttClient.read()); - } - Serial.println(); - - Serial.println(); - } - - // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay - // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info - unsigned long currentMillis = millis(); - - if (currentMillis - previousMillis >= interval) { - // save the last time a message was sent - previousMillis = currentMillis; - - Serial.print("Sending message to topic: "); - Serial.println(topic); - Serial.print("echo "); - Serial.println(count); - - // send message, the Print interface can be used to set the message contents - mqttClient.beginMessage(topic); - mqttClient.print("echo "); - mqttClient.print(count); - mqttClient.endMessage(); - - Serial.println(); - - count++; - } -} diff --git a/lib/ArduinoMqttClient/examples/WiFiEcho/arduino_secrets.h b/lib/ArduinoMqttClient/examples/WiFiEcho/arduino_secrets.h deleted file mode 100644 index 0c9fdd5..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiEcho/arduino_secrets.h +++ /dev/null @@ -1,2 +0,0 @@ -#define SECRET_SSID "" -#define SECRET_PASS "" diff --git a/lib/ArduinoMqttClient/examples/WiFiEchoCallback/WiFiEchoCallback.ino b/lib/ArduinoMqttClient/examples/WiFiEchoCallback/WiFiEchoCallback.ino deleted file mode 100644 index 8438be3..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiEchoCallback/WiFiEchoCallback.ino +++ /dev/null @@ -1,151 +0,0 @@ -/* - ArduinoMqttClient - WiFi Echo - - This example connects to a MQTT broker and subscribes to a single topic, - it also publishes a message to the same topic once a second. - When a message is received it prints the message to the Serial Monitor, - it uses the callback functionality of the library. - - The circuit: - - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board - - This example code is in the public domain. -*/ - -#include -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#elif defined(ARDUINO_SAMD_MKR1000) - #include -#elif defined(ARDUINO_ARCH_ESP8266) - #include -#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) - #include -#endif - -#include "arduino_secrets.h" -///////please enter your sensitive data in the Secret tab/arduino_secrets.h -char ssid[] = SECRET_SSID; // your network SSID (name) -char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) - -// To connect with SSL/TLS: -// 1) Change WiFiClient to WiFiSSLClient. -// 2) Change port value from 1883 to 8883. -// 3) Change broker value to a server with a known SSL/TLS root certificate -// flashed in the WiFi module. - -WiFiClient wifiClient; -MqttClient mqttClient(wifiClient); - -const char broker[] = "test.mosquitto.org"; -int port = 1883; -const char topic[] = "arduino/echo"; - -const long interval = 1000; -unsigned long previousMillis = 0; - -int count = 0; - -void setup() { - //Initialize serial and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // attempt to connect to WiFi network: - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - while (WiFi.begin(ssid, pass) != WL_CONNECTED) { - // failed, retry - Serial.print("."); - delay(5000); - } - - Serial.println("You're connected to the network"); - Serial.println(); - - // You can provide a unique client ID, if not set the library uses Arduino-millis() - // Each client must have a unique client ID - // mqttClient.setId("clientId"); - - // You can provide a username and password for authentication - // mqttClient.setUsernamePassword("username", "password"); - - Serial.print("Attempting to connect to the MQTT broker: "); - Serial.println(broker); - - if (!mqttClient.connect(broker, port)) { - Serial.print("MQTT connection failed! Error code = "); - Serial.println(mqttClient.connectError()); - - while (1); - } - - Serial.println("You're connected to the MQTT broker!"); - Serial.println(); - - // set the message receive callback - mqttClient.onMessage(onMqttMessage); - - Serial.print("Subscribing to topic: "); - Serial.println(topic); - Serial.println(); - - // subscribe to a topic - mqttClient.subscribe(topic); - - // topics can be unsubscribed using: - // mqttClient.unsubscribe(topic); - - Serial.print("Waiting for messages on topic: "); - Serial.println(topic); - Serial.println(); -} - -void loop() { - // call poll() regularly to allow the library to receive MQTT messages and - // send MQTT keep alives which avoids being disconnected by the broker - mqttClient.poll(); - - // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay - // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info - unsigned long currentMillis = millis(); - - if (currentMillis - previousMillis >= interval) { - // save the last time a message was sent - previousMillis = currentMillis; - - Serial.print("Sending message to topic: "); - Serial.println(topic); - Serial.print("echo "); - Serial.println(count); - - // send message, the Print interface can be used to set the message contents - mqttClient.beginMessage(topic); - mqttClient.print("echo "); - mqttClient.print(count); - mqttClient.endMessage(); - - Serial.println(); - - count++; - } -} - -void onMqttMessage(int messageSize) { - // we received a message, print out the topic and contents - Serial.print("Received a message with topic '"); - Serial.print(mqttClient.messageTopic()); - Serial.print("', length "); - Serial.print(messageSize); - Serial.println(" bytes:"); - - // use the Stream interface to print the contents - while (mqttClient.available()) { - Serial.print((char)mqttClient.read()); - } - Serial.println(); - - Serial.println(); -} diff --git a/lib/ArduinoMqttClient/examples/WiFiEchoCallback/arduino_secrets.h b/lib/ArduinoMqttClient/examples/WiFiEchoCallback/arduino_secrets.h deleted file mode 100644 index 0c9fdd5..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiEchoCallback/arduino_secrets.h +++ /dev/null @@ -1,2 +0,0 @@ -#define SECRET_SSID "" -#define SECRET_PASS "" diff --git a/lib/ArduinoMqttClient/examples/WiFiSimpleReceive/WiFiSimpleReceive.ino b/lib/ArduinoMqttClient/examples/WiFiSimpleReceive/WiFiSimpleReceive.ino deleted file mode 100644 index 4cb406a..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiSimpleReceive/WiFiSimpleReceive.ino +++ /dev/null @@ -1,114 +0,0 @@ -/* - ArduinoMqttClient - WiFi Simple Receive - - This example connects to a MQTT broker and subscribes to a single topic. - When a message is received it prints the message to the Serial Monitor. - - The circuit: - - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board - - This example code is in the public domain. -*/ - -#include -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#elif defined(ARDUINO_SAMD_MKR1000) - #include -#elif defined(ARDUINO_ARCH_ESP8266) - #include -#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) - #include -#endif - -#include "arduino_secrets.h" -///////please enter your sensitive data in the Secret tab/arduino_secrets.h -char ssid[] = SECRET_SSID; // your network SSID (name) -char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) - -// To connect with SSL/TLS: -// 1) Change WiFiClient to WiFiSSLClient. -// 2) Change port value from 1883 to 8883. -// 3) Change broker value to a server with a known SSL/TLS root certificate -// flashed in the WiFi module. - -WiFiClient wifiClient; -MqttClient mqttClient(wifiClient); - -const char broker[] = "test.mosquitto.org"; -int port = 1883; -const char topic[] = "arduino/simple"; - -void setup() { - //Initialize serial and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // attempt to connect to WiFi network: - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - while (WiFi.begin(ssid, pass) != WL_CONNECTED) { - // failed, retry - Serial.print("."); - delay(5000); - } - - Serial.println("You're connected to the network"); - Serial.println(); - - // You can provide a unique client ID, if not set the library uses Arduino-millis() - // Each client must have a unique client ID - // mqttClient.setId("clientId"); - - // You can provide a username and password for authentication - // mqttClient.setUsernamePassword("username", "password"); - - Serial.print("Attempting to connect to the MQTT broker: "); - Serial.println(broker); - - if (!mqttClient.connect(broker, port)) { - Serial.print("MQTT connection failed! Error code = "); - Serial.println(mqttClient.connectError()); - - while (1); - } - - Serial.println("You're connected to the MQTT broker!"); - Serial.println(); - - Serial.print("Subscribing to topic: "); - Serial.println(topic); - Serial.println(); - - // subscribe to a topic - mqttClient.subscribe(topic); - - // topics can be unsubscribed using: - // mqttClient.unsubscribe(topic); - - Serial.print("Waiting for messages on topic: "); - Serial.println(topic); - Serial.println(); -} - -void loop() { - int messageSize = mqttClient.parseMessage(); - if (messageSize) { - // we received a message, print out the topic and contents - Serial.print("Received a message with topic '"); - Serial.print(mqttClient.messageTopic()); - Serial.print("', length "); - Serial.print(messageSize); - Serial.println(" bytes:"); - - // use the Stream interface to print the contents - while (mqttClient.available()) { - Serial.print((char)mqttClient.read()); - } - Serial.println(); - - Serial.println(); - } -} diff --git a/lib/ArduinoMqttClient/examples/WiFiSimpleReceive/arduino_secrets.h b/lib/ArduinoMqttClient/examples/WiFiSimpleReceive/arduino_secrets.h deleted file mode 100644 index 0c9fdd5..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiSimpleReceive/arduino_secrets.h +++ /dev/null @@ -1,2 +0,0 @@ -#define SECRET_SSID "" -#define SECRET_PASS "" diff --git a/lib/ArduinoMqttClient/examples/WiFiSimpleReceiveCallback/WiFiSimpleReceiveCallback.ino b/lib/ArduinoMqttClient/examples/WiFiSimpleReceiveCallback/WiFiSimpleReceiveCallback.ino deleted file mode 100644 index 77a4d93..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiSimpleReceiveCallback/WiFiSimpleReceiveCallback.ino +++ /dev/null @@ -1,121 +0,0 @@ -/* - ArduinoMqttClient - WiFi Simple Receive Callback - - This example connects to a MQTT broker and subscribes to a single topic. - When a message is received it prints the message to the Serial Monitor, - it uses the callback functionality of the library. - - The circuit: - - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board - - This example code is in the public domain. -*/ - -#include -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#elif defined(ARDUINO_SAMD_MKR1000) - #include -#elif defined(ARDUINO_ARCH_ESP8266) - #include -#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) - #include -#endif - -#include "arduino_secrets.h" -///////please enter your sensitive data in the Secret tab/arduino_secrets.h -char ssid[] = SECRET_SSID; // your network SSID (name) -char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) - -// To connect with SSL/TLS: -// 1) Change WiFiClient to WiFiSSLClient. -// 2) Change port value from 1883 to 8883. -// 3) Change broker value to a server with a known SSL/TLS root certificate -// flashed in the WiFi module. - -WiFiClient wifiClient; -MqttClient mqttClient(wifiClient); - -const char broker[] = "test.mosquitto.org"; -int port = 1883; -const char topic[] = "arduino/simple"; - -void setup() { - //Initialize serial and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // attempt to connect to WiFi network: - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - while (WiFi.begin(ssid, pass) != WL_CONNECTED) { - // failed, retry - Serial.print("."); - delay(5000); - } - - Serial.println("You're connected to the network"); - Serial.println(); - - // You can provide a unique client ID, if not set the library uses Arduino-millis() - // Each client must have a unique client ID - // mqttClient.setId("clientId"); - - // You can provide a username and password for authentication - // mqttClient.setUsernamePassword("username", "password"); - - Serial.print("Attempting to connect to the MQTT broker: "); - Serial.println(broker); - - if (!mqttClient.connect(broker, port)) { - Serial.print("MQTT connection failed! Error code = "); - Serial.println(mqttClient.connectError()); - - while (1); - } - - Serial.println("You're connected to the MQTT broker!"); - Serial.println(); - - // set the message receive callback - mqttClient.onMessage(onMqttMessage); - - Serial.print("Subscribing to topic: "); - Serial.println(topic); - Serial.println(); - - // subscribe to a topic - mqttClient.subscribe(topic); - - // topics can be unsubscribed using: - // mqttClient.unsubscribe(topic); - - Serial.print("Waiting for messages on topic: "); - Serial.println(topic); - Serial.println(); -} - -void loop() { - // call poll() regularly to allow the library to receive MQTT messages and - // send MQTT keep alives which avoids being disconnected by the broker - mqttClient.poll(); -} - -void onMqttMessage(int messageSize) { - // we received a message, print out the topic and contents - Serial.println("Received a message with topic '"); - Serial.print(mqttClient.messageTopic()); - Serial.print("', length "); - Serial.print(messageSize); - Serial.println(" bytes:"); - - // use the Stream interface to print the contents - while (mqttClient.available()) { - Serial.print((char)mqttClient.read()); - } - Serial.println(); - - Serial.println(); -} diff --git a/lib/ArduinoMqttClient/examples/WiFiSimpleReceiveCallback/arduino_secrets.h b/lib/ArduinoMqttClient/examples/WiFiSimpleReceiveCallback/arduino_secrets.h deleted file mode 100644 index 0c9fdd5..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiSimpleReceiveCallback/arduino_secrets.h +++ /dev/null @@ -1,2 +0,0 @@ -#define SECRET_SSID "" -#define SECRET_PASS "" diff --git a/lib/ArduinoMqttClient/examples/WiFiSimpleSender/WiFiSimpleSender.ino b/lib/ArduinoMqttClient/examples/WiFiSimpleSender/WiFiSimpleSender.ino deleted file mode 100644 index 2513609..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiSimpleSender/WiFiSimpleSender.ino +++ /dev/null @@ -1,115 +0,0 @@ -/* - ArduinoMqttClient - WiFi Simple Sender - - This example connects to a MQTT broker and publishes a message to - a topic once a second. - - The circuit: - - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board - - This example code is in the public domain. -*/ - -#include -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) - #include -#elif defined(ARDUINO_SAMD_MKR1000) - #include -#elif defined(ARDUINO_ARCH_ESP8266) - #include -#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) - #include -#endif - -#include "arduino_secrets.h" -///////please enter your sensitive data in the Secret tab/arduino_secrets.h -char ssid[] = SECRET_SSID; // your network SSID (name) -char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) - -// To connect with SSL/TLS: -// 1) Change WiFiClient to WiFiSSLClient. -// 2) Change port value from 1883 to 8883. -// 3) Change broker value to a server with a known SSL/TLS root certificate -// flashed in the WiFi module. - -WiFiClient wifiClient; -MqttClient mqttClient(wifiClient); - -const char broker[] = "test.mosquitto.org"; -int port = 1883; -const char topic[] = "arduino/simple"; - -const long interval = 1000; -unsigned long previousMillis = 0; - -int count = 0; - -void setup() { - //Initialize serial and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // attempt to connect to WiFi network: - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - while (WiFi.begin(ssid, pass) != WL_CONNECTED) { - // failed, retry - Serial.print("."); - delay(5000); - } - - Serial.println("You're connected to the network"); - Serial.println(); - - // You can provide a unique client ID, if not set the library uses Arduino-millis() - // Each client must have a unique client ID - // mqttClient.setId("clientId"); - - // You can provide a username and password for authentication - // mqttClient.setUsernamePassword("username", "password"); - - Serial.print("Attempting to connect to the MQTT broker: "); - Serial.println(broker); - - if (!mqttClient.connect(broker, port)) { - Serial.print("MQTT connection failed! Error code = "); - Serial.println(mqttClient.connectError()); - - while (1); - } - - Serial.println("You're connected to the MQTT broker!"); - Serial.println(); -} - -void loop() { - // call poll() regularly to allow the library to send MQTT keep alives which - // avoids being disconnected by the broker - mqttClient.poll(); - - // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay - // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info - unsigned long currentMillis = millis(); - - if (currentMillis - previousMillis >= interval) { - // save the last time a message was sent - previousMillis = currentMillis; - - Serial.print("Sending message to topic: "); - Serial.println(topic); - Serial.print("hello "); - Serial.println(count); - - // send message, the Print interface can be used to set the message contents - mqttClient.beginMessage(topic); - mqttClient.print("hello "); - mqttClient.print(count); - mqttClient.endMessage(); - - Serial.println(); - - count++; - } -} diff --git a/lib/ArduinoMqttClient/examples/WiFiSimpleSender/arduino_secrets.h b/lib/ArduinoMqttClient/examples/WiFiSimpleSender/arduino_secrets.h deleted file mode 100644 index 0c9fdd5..0000000 --- a/lib/ArduinoMqttClient/examples/WiFiSimpleSender/arduino_secrets.h +++ /dev/null @@ -1,2 +0,0 @@ -#define SECRET_SSID "" -#define SECRET_PASS "" diff --git a/lib/ArduinoMqttClient/keywords.txt b/lib/ArduinoMqttClient/keywords.txt deleted file mode 100644 index 6c414b6..0000000 --- a/lib/ArduinoMqttClient/keywords.txt +++ /dev/null @@ -1,52 +0,0 @@ -############################################ -# Syntax Coloring Map For ArduinoMqttClient -############################################ -# Class -############################################ - -ArduinoMqttClient KEYWORD1 -MqttClient KEYWORD1 - -############################################ -# Methods and Functions -############################################ - -onMessage KEYWORD2 - -parseMessage KEYWORD2 -messageTopic KEYWORD2 -messageDup KEYWORD2 -messageQoS KEYWORD2 -messageRetain KEYWORD2 - -beginMessage KEYWORD2 -endMessage KEYWORD2 -beginWill KEYWORD2 -endWill KEYWORD2 - -subscribe KEYWORD2 -unsubscribe KEYWORD2 - -poll KEYWORD2 - -connect KEYWORD2 -write KEYWORD2 -available KEYWORD2 -read KEYWORD2 -peek KEYWORD2 -flush KEYWORD2 -stop KEYWORD2 -connected KEYWORD2 - -setId KEYWORD2 -setUsernamePassword KEYWORD2 -setCleanSession KEYWORD2 -setKeepAliveInterval KEYWORD2 -setConnectionTimeout KEYWORD2 - -connectError KEYWORD2 -subscribeQoS KEYWORD2 - -############################################ -# Constants -############################################ diff --git a/lib/ArduinoMqttClient/library.properties b/lib/ArduinoMqttClient/library.properties deleted file mode 100644 index 79e24de..0000000 --- a/lib/ArduinoMqttClient/library.properties +++ /dev/null @@ -1,10 +0,0 @@ -name=ArduinoMqttClient -version=0.1.6 -author=Arduino -maintainer=Arduino -sentence=[BETA] Allows you to send and receive MQTT messages using Arduino. -paragraph= -category=Communication -url=https://github.com/arduino-libraries/ArduinoMqttClient -architectures=* -includes=ArduinoMqttClient.h diff --git a/lib/ArduinoMqttClient/src/ArduinoMqttClient.h b/lib/ArduinoMqttClient/src/ArduinoMqttClient.h deleted file mode 100644 index ff0a8cc..0000000 --- a/lib/ArduinoMqttClient/src/ArduinoMqttClient.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - This file is part of the ArduinoMqttClient library. - Copyright (c) 2019 Arduino SA. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef _ARDUINO_MQTT_CLIENT_H_ -#define _ARDUINO_MQTT_CLIENT_H_ - -#include "MqttClient.h" - -#endif diff --git a/lib/ArduinoMqttClient/src/MqttClient.cpp b/lib/ArduinoMqttClient/src/MqttClient.cpp deleted file mode 100644 index 221b230..0000000 --- a/lib/ArduinoMqttClient/src/MqttClient.cpp +++ /dev/null @@ -1,1200 +0,0 @@ -/* - This file is part of the ArduinoMqttClient library. - Copyright (c) 2019 Arduino SA. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include "MqttClient.h" - -// #define MQTT_CLIENT_DEBUG - -#ifndef htons - #ifdef __ARM__ - #define htons __REV16 - #else - #define htons(s) ((s<<8) | (s>>8)) - #endif -#endif - -#ifndef TX_PAYLOAD_BUFFER_SIZE - #ifdef __AVR__ - #define TX_PAYLOAD_BUFFER_SIZE 128 - #else - #define TX_PAYLOAD_BUFFER_SIZE 256 - #endif -#endif - -#define MQTT_CONNECT 1 -#define MQTT_CONNACK 2 -#define MQTT_PUBLISH 3 -#define MQTT_PUBACK 4 -#define MQTT_PUBREC 5 -#define MQTT_PUBREL 6 -#define MQTT_PUBCOMP 7 -#define MQTT_SUBSCRIBE 8 -#define MQTT_SUBACK 9 -#define MQTT_UNSUBSCRIBE 10 -#define MQTT_UNSUBACK 11 -#define MQTT_PINGREQ 12 -#define MQTT_PINGRESP 13 -#define MQTT_DISCONNECT 14 - -enum { - MQTT_CLIENT_RX_STATE_READ_TYPE, - MQTT_CLIENT_RX_STATE_READ_REMAINING_LENGTH, - MQTT_CLIENT_RX_STATE_READ_VARIABLE_HEADER, - MQTT_CLIENT_RX_STATE_READ_PUBLISH_TOPIC_LENGTH, - MQTT_CLIENT_RX_STATE_READ_PUBLISH_TOPIC, - MQTT_CLIENT_RX_STATE_READ_PUBLISH_PACKET_ID, - MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD, - MQTT_CLIENT_RX_STATE_DISCARD_PUBLISH_PAYLOAD -}; - -MqttClient::MqttClient(Client* client) : - _client(client), - _onMessage(NULL), - _cleanSession(true), - _keepAliveInterval(60 * 1000L), - _connectionTimeout(30 * 1000L), - _tx_payload_buffer_size(TX_PAYLOAD_BUFFER_SIZE), - _connectError(MQTT_SUCCESS), - _connected(false), - _subscribeQos(0x00), - _rxState(MQTT_CLIENT_RX_STATE_READ_TYPE), - _txBufferIndex(0), - _txPayloadBuffer(NULL), - _txPayloadBufferIndex(0), - _willBuffer(NULL), - _willBufferIndex(0), - _willMessageIndex(0), - _willFlags(0x00) -{ - setTimeout(0); -} - -MqttClient::MqttClient(Client& client) : MqttClient(&client) -{ - -} - -MqttClient::~MqttClient() -{ - if (_willBuffer) { - free(_willBuffer); - - _willBuffer = NULL; - } - - if (_txPayloadBuffer) { - free(_txPayloadBuffer); - - _txPayloadBuffer = NULL; - } -} - -#ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK -void MqttClient::onMessage(MessageCallback callback) -#else -void MqttClient::onMessage(void(*callback)(int)) -#endif -{ - _onMessage = callback; -} - -int MqttClient::parseMessage() -{ - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - // already had a message, but only partially read, discard the data - _rxState = MQTT_CLIENT_RX_STATE_DISCARD_PUBLISH_PAYLOAD; - } - - poll(); - - if (_rxState != MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - // message not received or not ready - return 0; - } - - return _rxLength; -} - -String MqttClient::messageTopic() const -{ - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - // message received and ready for reading - return _rxMessageTopic; - } - - return ""; -} - -int MqttClient::messageDup() const -{ - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - // message received and ready for reading - return _rxMessageDup; - } - - return -1; -} - -int MqttClient::messageQoS() const -{ - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - // message received and ready for reading - return _rxMessageQoS; - } - - return -1; -} - -int MqttClient::messageRetain() const -{ - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - // message received and ready for reading - return _rxMessageRetain; - } - - return -1; -} - -int MqttClient::beginMessage(const char* topic, unsigned long size, bool retain, uint8_t qos, bool dup) -{ - _txMessageTopic = topic; - _txMessageRetain = retain; - _txMessageQoS = qos; - _txMessageDup = dup; - _txPayloadBufferIndex = 0; - _txStreamPayload = (size != 0xffffffffL); - - if (_txStreamPayload) { - if (!publishHeader(size)) { - stop(); - - return 0; - } - } - - return 1; -} - -int MqttClient::beginMessage(const String& topic, unsigned long size, bool retain, uint8_t qos, bool dup) -{ - return beginMessage(topic.c_str(), size, retain, qos, dup); -} - -int MqttClient::beginMessage(const char* topic, bool retain, uint8_t qos, bool dup) -{ - return beginMessage(topic, 0xffffffffL, retain, qos, dup); -} - -int MqttClient::beginMessage(const String& topic, bool retain, uint8_t qos, bool dup) -{ - return beginMessage(topic.c_str(), retain, qos, dup); -} - -int MqttClient::endMessage() -{ - if (!_txStreamPayload) { - if (!publishHeader(_txPayloadBufferIndex) || - (clientWrite(_txPayloadBuffer, _txPayloadBufferIndex) != _txPayloadBufferIndex)) { - stop(); - - return 0; - } - } - - _txStreamPayload = false; - - if (_txMessageQoS) { - if (_txMessageQoS == 2) { - // wait for PUBREC - _returnCode = -1; - - for (unsigned long start = millis(); ((millis() - start) < _connectionTimeout) && clientConnected();) { - poll(); - - if (_returnCode != -1) { - if (_returnCode == 0) { - break; - } else { - return 0; - } - } - yield(); - } - - // reply with PUBREL - pubrel(_txPacketId); - } - - // wait for PUBACK or PUBCOMP - _returnCode = -1; - - for (unsigned long start = millis(); ((millis() - start) < _connectionTimeout) && clientConnected();) { - poll(); - - if (_returnCode != -1) { - return (_returnCode == 0); - } - yield(); - } - - return 0; - } - - return 1; -} - -int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos) -{ - int topicLength = strlen(topic); - size_t willLength = (2 + topicLength + 2 + size); - - if (qos > 2) { - // invalid QoS - } - - _willBuffer = (uint8_t*)realloc(_willBuffer, willLength); - - _txBuffer = _willBuffer; - _txBufferIndex = 0; - writeString(topic, topicLength); - write16(0); // dummy size for now - _willMessageIndex = _txBufferIndex; - - _willFlags = (qos << 3) | 0x04; - if (retain) { - _willFlags |= 0x20; - } - - return 0; -} - -int MqttClient::beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos) -{ - return beginWill(topic.c_str(), size, retain, qos); -} - -int MqttClient::beginWill(const char* topic, bool retain, uint8_t qos) -{ - return beginWill(topic, _tx_payload_buffer_size, retain, qos); -} - -int MqttClient::beginWill(const String& topic, bool retain, uint8_t qos) -{ - return beginWill(topic.c_str(), retain, qos); -} - -int MqttClient::endWill() -{ - // update the index - _willBufferIndex = _txBufferIndex; - - // update the will message size - _txBufferIndex = (_willMessageIndex - 2); - write16(_willBufferIndex - _willMessageIndex); - - _txBuffer = NULL; - _willMessageIndex = 0; - - return 1; -} - -int MqttClient::subscribe(const char* topic, uint8_t qos) -{ - int topicLength = strlen(topic); - int remainingLength = topicLength + 5; - - if (qos > 2) { - // invalid QoS - return 0; - } - - _txPacketId++; - - if (_txPacketId == 0) { - _txPacketId = 1; - } - - uint8_t packetBuffer[5 + remainingLength]; - - beginPacket(MQTT_SUBSCRIBE, 0x02, remainingLength, packetBuffer); - write16(_txPacketId); - writeString(topic, topicLength); - write8(qos); - - if (!endPacket()) { - stop(); - - return 0; - } - - _returnCode = -1; - _subscribeQos = 0x80; - - for (unsigned long start = millis(); ((millis() - start) < _connectionTimeout) && clientConnected();) { - poll(); - - if (_returnCode != -1) { - _subscribeQos = _returnCode; - - return (_returnCode >= 0 && _returnCode <= 2); - } - yield(); - } - - stop(); - - return 0; -} - -int MqttClient::subscribe(const String& topic, uint8_t qos) -{ - return subscribe(topic.c_str(), qos); -} - -int MqttClient::unsubscribe(const char* topic) -{ - int topicLength = strlen(topic); - int remainingLength = topicLength + 4; - - _txPacketId++; - - if (_txPacketId == 0) { - _txPacketId = 1; - } - - uint8_t packetBuffer[5 + remainingLength]; - - beginPacket(MQTT_UNSUBSCRIBE, 0x02, remainingLength, packetBuffer); - write16(_txPacketId); - writeString(topic, topicLength); - - if (!endPacket()) { - stop(); - - return 0; - } - - _returnCode = -1; - - for (unsigned long start = millis(); ((millis() - start) < _connectionTimeout) && clientConnected();) { - poll(); - - if (_returnCode != -1) { - return (_returnCode == 0); - } - yield(); - } - - stop(); - - return 0; -} - -int MqttClient::unsubscribe(const String& topic) -{ - return unsubscribe(topic.c_str()); -} - -void MqttClient::poll() -{ - if (clientAvailable() == 0 && !clientConnected()) { - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - _connected = false; - } - - while (clientAvailable()) { - byte b = clientRead(); - _lastRx = millis(); - - switch (_rxState) { - case MQTT_CLIENT_RX_STATE_READ_TYPE: { - _rxType = (b >> 4); - _rxFlags = (b & 0x0f); - _rxLength = 0; - _rxLengthMultiplier = 1; - - _rxState = MQTT_CLIENT_RX_STATE_READ_REMAINING_LENGTH; - break; - } - - case MQTT_CLIENT_RX_STATE_READ_REMAINING_LENGTH: { - _rxLength += (b & 0x7f) * _rxLengthMultiplier; - - _rxLengthMultiplier *= 128; - - if (_rxLengthMultiplier > (128 * 128 * 128L)) { - // malformed - stop(); - - return; - } - - if ((b & 0x80) == 0) { // length done - bool malformedResponse = false; - - if (_rxType == MQTT_CONNACK || - _rxType == MQTT_PUBACK || - _rxType == MQTT_PUBREC || - _rxType == MQTT_PUBCOMP || - _rxType == MQTT_UNSUBACK) { - malformedResponse = (_rxFlags != 0x00 || _rxLength != 2); - } else if (_rxType == MQTT_PUBLISH) { - malformedResponse = ((_rxFlags & 0x06) == 0x06); - } else if (_rxType == MQTT_PUBREL) { - malformedResponse = (_rxFlags != 0x02 || _rxLength != 2); - } else if (_rxType == MQTT_SUBACK) { - malformedResponse = (_rxFlags != 0x00 || _rxLength != 3); - } else if (_rxType == MQTT_PINGRESP) { - malformedResponse = (_rxFlags != 0x00 || _rxLength != 0); - } else { - // unexpected type - malformedResponse = true; - } - - if (malformedResponse) { - stop(); - return; - } - - if (_rxType == MQTT_PUBLISH) { - _rxMessageDup = (_rxFlags & 0x80) != 0; - _rxMessageQoS = (_rxFlags >> 1) & 0x03; - _rxMessageRetain = (_rxFlags & 0x01); - - _rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_TOPIC_LENGTH; - } else if (_rxLength == 0) { - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - } else { - _rxState = MQTT_CLIENT_RX_STATE_READ_VARIABLE_HEADER; - } - - _rxMessageIndex = 0; - } - break; - } - - case MQTT_CLIENT_RX_STATE_READ_VARIABLE_HEADER: { - _rxMessageBuffer[_rxMessageIndex++] = b; - - if (_rxMessageIndex == _rxLength) { - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - - if (_rxType == MQTT_CONNACK) { - _returnCode = _rxMessageBuffer[1]; - } else if (_rxType == MQTT_PUBACK || - _rxType == MQTT_PUBREC || - _rxType == MQTT_PUBCOMP || - _rxType == MQTT_UNSUBACK) { - uint16_t packetId = (_rxMessageBuffer[0] << 8) | _rxMessageBuffer[1]; - - if (packetId == _txPacketId) { - _returnCode = 0; - } - } else if (_rxType == MQTT_PUBREL) { - uint16_t packetId = (_rxMessageBuffer[0] << 8) | _rxMessageBuffer[1]; - - if (_txStreamPayload) { - // ignore, can't send as in the middle of a publish - } else { - pubcomp(packetId); - } - } else if (_rxType == MQTT_SUBACK) { - uint16_t packetId = (_rxMessageBuffer[0] << 8) | _rxMessageBuffer[1]; - - if (packetId == _txPacketId) { - _returnCode = _rxMessageBuffer[2]; - } - } - } - break; - } - - case MQTT_CLIENT_RX_STATE_READ_PUBLISH_TOPIC_LENGTH: { - _rxMessageBuffer[_rxMessageIndex++] = b; - - if (_rxMessageIndex == 2) { - _rxMessageTopicLength = (_rxMessageBuffer[0] << 8) | _rxMessageBuffer[1]; - _rxLength -= 2; - - _rxMessageTopic = ""; - _rxMessageTopic.reserve(_rxMessageTopicLength); - - if (_rxMessageQoS) { - if (_rxLength < (_rxMessageTopicLength + 2)) { - stop(); - return; - } - } else { - if (_rxLength < _rxMessageTopicLength) { - stop(); - return; - } - } - - _rxMessageIndex = 0; - _rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_TOPIC; - } - - break; - } - - case MQTT_CLIENT_RX_STATE_READ_PUBLISH_TOPIC: { - _rxMessageTopic += (char)b; - - if (_rxMessageTopicLength == _rxMessageTopic.length()) { - _rxLength -= _rxMessageTopicLength; - - if (_rxMessageQoS) { - _rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PACKET_ID; - } else { - _rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD; - - if (_onMessage) { -#ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK - _onMessage(this,_rxLength); -#else - _onMessage(_rxLength); -#endif - - if (_rxLength == 0) { - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - } - } - } - } - - break; - } - - case MQTT_CLIENT_RX_STATE_READ_PUBLISH_PACKET_ID: { - _rxMessageBuffer[_rxMessageIndex++] = b; - - if (_rxMessageIndex == 2) { - _rxLength -= 2; - - _rxPacketId = (_rxMessageBuffer[0] << 8) | _rxMessageBuffer[1]; - - _rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD; - - if (_onMessage) { -#ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK - _onMessage(this,_rxLength); -#else - _onMessage(_rxLength); -#endif - } - - if (_rxLength == 0) { - // no payload to read, ack zero length message - ackRxMessage(); - - if (_onMessage) { - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - } - } - } - - break; - } - - case MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD: - case MQTT_CLIENT_RX_STATE_DISCARD_PUBLISH_PAYLOAD: { - if (_rxLength > 0) { - _rxLength--; - } - - if (_rxLength == 0) { - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - } else { - _rxState = MQTT_CLIENT_RX_STATE_DISCARD_PUBLISH_PAYLOAD; - } - - break; - } - } - - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - break; - } - } - - if (_connected) { - unsigned long now = millis(); - - if ((now - _lastPingTx) >= _keepAliveInterval) { - _lastPingTx = now; - - ping(); - } else if ((now - _lastRx) >= (_keepAliveInterval * 2)) { - stop(); - } - } -} - -int MqttClient::connect(IPAddress ip, uint16_t port) -{ - return connect(ip, NULL, port); -} - -int MqttClient::connect(const char *host, uint16_t port) -{ - return connect((uint32_t)0, host, port); -} - -size_t MqttClient::write(uint8_t b) -{ - return write(&b, sizeof(b)); -} - -size_t MqttClient::write(const uint8_t *buf, size_t size) -{ - if (_willMessageIndex) { - return writeData(buf, size); - } - - if (_txStreamPayload) { - return clientWrite(buf, size); - } - - if ((_txPayloadBufferIndex + size) >= _tx_payload_buffer_size) { - size = (_tx_payload_buffer_size - _txPayloadBufferIndex); - } - - if (_txPayloadBuffer == NULL) { - _txPayloadBuffer = (uint8_t*)malloc(_tx_payload_buffer_size); - } - - memcpy(&_txPayloadBuffer[_txPayloadBufferIndex], buf, size); - _txPayloadBufferIndex += size; - - return size; -} - -int MqttClient::available() -{ - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - return _rxLength; - } - - return 0; -} - -int MqttClient::read() -{ - byte b; - - if (read(&b, sizeof(b)) != sizeof(b)) { - return -1; - } - - return b; -} - -int MqttClient::read(uint8_t *buf, size_t size) -{ - size_t result = 0; - - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - size_t avail = available(); - - if (size > avail) { - size = avail; - } - - while (result < size) { - int b = clientTimedRead(); - - if (b == -1) { - break; - } - - result++; - *buf++ = b; - } - - if (result > 0) { - _rxLength -= result; - - if (_rxLength == 0) { - ackRxMessage(); - - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - } - } - } - - return result; -} - -int MqttClient::peek() -{ - if (_rxState == MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD) { - return clientPeek(); - } - - return -1; -} - -void MqttClient::flush() -{ -} - -void MqttClient::stop() -{ - if (connected()) { - disconnect(); - } - - _connected = false; - _client->stop(); -} - -uint8_t MqttClient::connected() -{ - return clientConnected() && _connected; -} - -MqttClient::operator bool() -{ - return true; -} - -void MqttClient::setId(const char* id) -{ - _id = id; -} - -void MqttClient::setId(const String& id) -{ - _id = id; -} - -void MqttClient::setUsernamePassword(const char* username, const char* password) -{ - _username = username; - _password = password; -} - -void MqttClient::setUsernamePassword(const String& username, const String& password) -{ - _username = username; - _password = password; -} - -void MqttClient::setCleanSession(bool cleanSession) -{ - _cleanSession = cleanSession; -} - -void MqttClient::setKeepAliveInterval(unsigned long interval) -{ - _keepAliveInterval = interval; -} - -void MqttClient::setConnectionTimeout(unsigned long timeout) -{ - _connectionTimeout = timeout; -} - -void MqttClient::setTxPayloadSize(unsigned short size) -{ - if (_txPayloadBuffer) { - free(_txPayloadBuffer); - _txPayloadBuffer = NULL; - _txPayloadBufferIndex = 0; - } - - _tx_payload_buffer_size = size; -} - -int MqttClient::connectError() const -{ - return _connectError; -} - -int MqttClient::subscribeQoS() const -{ - return _subscribeQos; -} - -int MqttClient::connect(IPAddress ip, const char* host, uint16_t port) -{ - if (clientConnected()) { - _client->stop(); - } - _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; - _connected = false; - _txPacketId = 0x0000; - - if (host) { - if (!_client->connect(host, port)) { - _connectError = MQTT_CONNECTION_REFUSED; - return 0; - } - } else { - if (!_client->connect(ip, port)) { - _connectError = MQTT_CONNECTION_REFUSED; - return 0; - } - } - - _lastRx = millis(); - - String id = _id; - int idLength = id.length(); - int usernameLength = _username.length(); - int passwordLength = _password.length(); - uint8_t flags = 0; - - if (idLength == 0) { - char tempId[17]; - - snprintf(tempId, sizeof(tempId), "Arduino-%.8lx", millis()); - - id = tempId; - idLength = sizeof(tempId) - 1; - } - - struct __attribute__ ((packed)) { - struct { - uint16_t length; - char value[4]; - } protocolName; - uint8_t level; - uint8_t flags; - uint16_t keepAlive; - } connectVariableHeader; - - size_t remainingLength = sizeof(connectVariableHeader) + (2 + idLength) + _willBufferIndex; - - if (usernameLength) { - flags |= 0x80; - - remainingLength += (2 + usernameLength); - } - - if (passwordLength) { - flags |= 0x40; - - remainingLength += (2 + passwordLength); - } - - flags |= _willFlags; - - if (_cleanSession) { - flags |= 0x02; // clean session - } - - connectVariableHeader.protocolName.length = htons(sizeof(connectVariableHeader.protocolName.value)); - memcpy(connectVariableHeader.protocolName.value, "MQTT", sizeof(connectVariableHeader.protocolName.value)); - connectVariableHeader.level = 0x04; - connectVariableHeader.flags = flags; - connectVariableHeader.keepAlive = htons(_keepAliveInterval / 1000); - - uint8_t packetBuffer[5 + remainingLength]; - - beginPacket(MQTT_CONNECT, 0x00, remainingLength, packetBuffer); - writeData(&connectVariableHeader, sizeof(connectVariableHeader)); - writeString(id.c_str(), idLength); - - if (_willBufferIndex) { - writeData(_willBuffer, _willBufferIndex); - } - - if (usernameLength) { - writeString(_username.c_str(), usernameLength); - } - - if (passwordLength) { - writeString(_password.c_str(), passwordLength); - } - - if (!endPacket()) { - _client->stop(); - - _connectError = MQTT_SERVER_UNAVAILABLE; - - return 0; - } - - _returnCode = MQTT_CONNECTION_TIMEOUT; - - for (unsigned long start = millis(); ((millis() - start) < _connectionTimeout) && clientConnected();) { - poll(); - - if (_returnCode != MQTT_CONNECTION_TIMEOUT) { - break; - } - yield(); - } - - _connectError = _returnCode; - - if (_returnCode == MQTT_SUCCESS) { - _connected = true; - - return 1; - } - - _client->stop(); - - return 0; -} - -int MqttClient::publishHeader(size_t length) -{ - int topicLength = _txMessageTopic.length(); - int headerLength = topicLength + 2; - - if (_txMessageQoS > 2) { - // invalid QoS - return 0; - } - - if (_txMessageQoS) { - // add two for packet id - headerLength += 2; - - _txPacketId++; - - if (_txPacketId == 0) { - _txPacketId = 1; - } - } - - // only for packet header - uint8_t packetHeaderBuffer[5 + headerLength]; - - uint8_t flags = 0; - - if (_txMessageRetain) { - flags |= 0x01; - } - - if (_txMessageQoS) { - flags |= (_txMessageQoS << 1); - } - - if (_txMessageDup) { - flags |= 0x08; - } - - beginPacket(MQTT_PUBLISH, flags, headerLength + length, packetHeaderBuffer); - writeString(_txMessageTopic.c_str(), topicLength); - if (_txMessageQoS) { - write16(_txPacketId); - } - - // send packet header - return endPacket(); -} - -void MqttClient::puback(uint16_t id) -{ - uint8_t packetBuffer[4]; - - beginPacket(MQTT_PUBACK, 0x00, 2, packetBuffer); - write16(id); - endPacket(); -} - -void MqttClient::pubrec(uint16_t id) -{ - uint8_t packetBuffer[4]; - - beginPacket(MQTT_PUBREC, 0x00, 2, packetBuffer); - write16(id); - endPacket(); -} - -void MqttClient::pubrel(uint16_t id) -{ - uint8_t packetBuffer[4]; - - beginPacket(MQTT_PUBREL, 0x02, 2, packetBuffer); - write16(id); - endPacket(); -} - -void MqttClient::pubcomp(uint16_t id) -{ - uint8_t packetBuffer[4]; - - beginPacket(MQTT_PUBCOMP, 0x00, 2, packetBuffer); - write16(id); - endPacket(); -} - -void MqttClient::ping() -{ - uint8_t packetBuffer[2]; - - beginPacket(MQTT_PINGREQ, 0, 0, packetBuffer); - endPacket(); -} - -void MqttClient::disconnect() -{ - uint8_t packetBuffer[2]; - - beginPacket(MQTT_DISCONNECT, 0, 0, packetBuffer); - endPacket(); -} - -int MqttClient::beginPacket(uint8_t type, uint8_t flags, size_t length, uint8_t* buffer) -{ - _txBuffer = buffer; - _txBufferIndex = 0; - - write8((type << 4) | flags); - - do { - uint8_t b = length % 128; - length /= 128; - - if(length > 0) { - b |= 0x80; - } - - _txBuffer[_txBufferIndex++] = b; - } while (length > 0); - - return _txBufferIndex; -} - -int MqttClient::writeString(const char* s, uint16_t length) -{ - int result = 0; - - result += write16(length); - result += writeData(s, length); - - return result; -} - -int MqttClient::write8(uint8_t val) -{ - return writeData(&val, sizeof(val)); -} - -int MqttClient::write16(uint16_t val) -{ - val = htons(val); - - return writeData(&val, sizeof(val)); -} - -int MqttClient::writeData(const void* data, size_t length) -{ - memcpy(&_txBuffer[_txBufferIndex], data, length); - _txBufferIndex += length; - - return length; -} - -int MqttClient::endPacket() -{ - int result = (clientWrite(_txBuffer, _txBufferIndex) == _txBufferIndex); - - _txBufferIndex = 0; - - return result; -} - -void MqttClient::ackRxMessage() -{ - if (_rxMessageQoS == 1) { - puback(_rxPacketId); - } else if (_rxMessageQoS == 2) { - pubrec(_rxPacketId); - } -} - -int MqttClient::clientRead() -{ - int result = _client->read(); - -#ifdef MQTT_CLIENT_DEBUG - if (result != -1) { - Serial.print("RX: "); - - if (result < 16) { - Serial.print('0'); - } - - Serial.println(result, HEX); - } -#endif - - return result; -} - -uint8_t MqttClient::clientConnected() -{ - return _client->connected(); -} - -int MqttClient::clientAvailable() -{ - return _client->available(); -} - -int MqttClient::clientTimedRead() -{ - unsigned long startMillis = millis(); - - do { - if (clientAvailable()) { - return clientRead(); - } else if (!clientConnected()) { - return -1; - } - - yield(); - } while((millis() - startMillis) < 1000); - - return -1; -} - -int MqttClient::clientPeek() -{ - return _client->peek(); -} - -size_t MqttClient::clientWrite(const uint8_t *buf, size_t size) -{ -#ifdef MQTT_CLIENT_DEBUG - Serial.print("TX["); - Serial.print(size); - Serial.print("]: "); - for (size_t i = 0; i < size; i++) { - uint8_t b = buf[i]; - - if (b < 16) { - Serial.print('0'); - } - - Serial.print(b, HEX); - Serial.print(' '); - } - Serial.println(); -#endif - - return _client->write(buf, size); -} diff --git a/lib/ArduinoMqttClient/src/MqttClient.h b/lib/ArduinoMqttClient/src/MqttClient.h deleted file mode 100644 index 522f023..0000000 --- a/lib/ArduinoMqttClient/src/MqttClient.h +++ /dev/null @@ -1,200 +0,0 @@ -/* - This file is part of the ArduinoMqttClient library. - Copyright (c) 2019 Arduino SA. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef _MQTT_CLIENT_H_ -#define _MQTT_CLIENT_H_ - -#include -#include - -#define MQTT_CONNECTION_REFUSED -2 -#define MQTT_CONNECTION_TIMEOUT -1 -#define MQTT_SUCCESS 0 -#define MQTT_UNACCEPTABLE_PROTOCOL_VERSION 1 -#define MQTT_IDENTIFIER_REJECTED 2 -#define MQTT_SERVER_UNAVAILABLE 3 -#define MQTT_BAD_USER_NAME_OR_PASSWORD 4 -#define MQTT_NOT_AUTHORIZED 5 - -// Make this definition in your application code to use std::functions for onMessage callbacks instead of C-pointers: -// #define MQTT_CLIENT_STD_FUNCTION_CALLBACK - -#ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK -#include -#endif - -class MqttClient : public Client { -public: - MqttClient(Client* client); - MqttClient(Client& client); - virtual ~MqttClient(); - -#ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK - typedef std::function MessageCallback; - void onMessage(MessageCallback callback); -#else - inline void setClient(Client& client) { _client = &client; } - void onMessage(void(*)(int)); -#endif - - int parseMessage(); - String messageTopic() const; - int messageDup() const; - int messageQoS() const; - int messageRetain() const; - - int beginMessage(const char* topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false); - int beginMessage(const String& topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false); - int beginMessage(const char* topic, bool retain = false, uint8_t qos = 0, bool dup = false); - int beginMessage(const String& topic, bool retain = false, uint8_t qos = 0, bool dup = false); - int endMessage(); - - int beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos); - int beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos); - int beginWill(const char* topic, bool retain, uint8_t qos); - int beginWill(const String& topic, bool retain, uint8_t qos); - int endWill(); - - int subscribe(const char* topic, uint8_t qos = 0); - int subscribe(const String& topic, uint8_t qos = 0); - int unsubscribe(const char* topic); - int unsubscribe(const String& topic); - - void poll(); - - // from Client - virtual int connect(IPAddress ip, uint16_t port = 1883); - virtual int connect(const char *host, uint16_t port = 1883); -#ifdef ESP8266 - virtual int connect(const IPAddress& ip, uint16_t port) { return 0; }; /* ESP8266 core defines this pure virtual in Client.h */ -#endif - virtual size_t write(uint8_t); - virtual size_t write(const uint8_t *buf, size_t size); - virtual int available(); - virtual int read(); - virtual int read(uint8_t *buf, size_t size); - virtual int peek(); - virtual void flush(); - virtual void stop(); - virtual uint8_t connected(); - virtual operator bool(); - - void setId(const char* id); - void setId(const String& id); - - void setUsernamePassword(const char* username, const char* password); - void setUsernamePassword(const String& username, const String& password); - - void setCleanSession(bool cleanSession); - - void setKeepAliveInterval(unsigned long interval); - void setConnectionTimeout(unsigned long timeout); - void setTxPayloadSize(unsigned short size); - - int connectError() const; - int subscribeQoS() const; -#ifdef ESP8266 - virtual bool flush(unsigned int /*maxWaitMs*/) { flush(); return true; } /* ESP8266 core defines this pure virtual in Client.h */ - virtual bool stop(unsigned int /*maxWaitMs*/) { stop(); return true; } /* ESP8266 core defines this pure virtual in Client.h */ -#endif - -private: - int connect(IPAddress ip, const char* host, uint16_t port); - int publishHeader(size_t length); - void puback(uint16_t id); - void pubrec(uint16_t id); - void pubrel(uint16_t id); - void pubcomp(uint16_t id); - void ping(); - void disconnect(); - - int beginPacket(uint8_t type, uint8_t flags, size_t length, uint8_t* buffer); - int writeString(const char* s, uint16_t length); - int write8(uint8_t val); - int write16(uint16_t val); - int writeData(const void* data, size_t length); - int endPacket(); - - void ackRxMessage(); - - uint8_t clientConnected(); - int clientAvailable(); - int clientRead(); - int clientTimedRead(); - int clientPeek(); - size_t clientWrite(const uint8_t *buf, size_t size); - -private: - Client* _client; - -#ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK - MessageCallback _onMessage; -#else - void (*_onMessage)(int); -#endif - - String _id; - String _username; - String _password; - bool _cleanSession; - - unsigned long _keepAliveInterval; - unsigned long _connectionTimeout; - unsigned short _tx_payload_buffer_size; - - int _connectError; - bool _connected; - int _subscribeQos; - - int _rxState; - uint8_t _rxType; - uint8_t _rxFlags; - size_t _rxLength; - uint32_t _rxLengthMultiplier; - int _returnCode; - - String _rxMessageTopic; - size_t _rxMessageTopicLength; - bool _rxMessageDup; - uint8_t _rxMessageQoS; - bool _rxMessageRetain; - uint16_t _rxPacketId; - uint8_t _rxMessageBuffer[3]; - size_t _rxMessageIndex; - unsigned long _lastRx; - - String _txMessageTopic; - bool _txMessageRetain; - uint8_t _txMessageQoS; - bool _txMessageDup; - uint16_t _txPacketId; - uint8_t* _txBuffer; - size_t _txBufferIndex; - bool _txStreamPayload; - uint8_t* _txPayloadBuffer; - size_t _txPayloadBufferIndex; - unsigned long _lastPingTx; - - uint8_t* _willBuffer; - uint16_t _willBufferIndex; - size_t _willMessageIndex; - uint8_t _willFlags; -}; - -#endif diff --git a/lib/AsyncTCP/.github/scripts/install-arduino-core-esp32.sh b/lib/AsyncTCP/.github/scripts/install-arduino-core-esp32.sh new file mode 100755 index 0000000..cf1026d --- /dev/null +++ b/lib/AsyncTCP/.github/scripts/install-arduino-core-esp32.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +export ARDUINO_ESP32_PATH="$ARDUINO_USR_PATH/hardware/espressif/esp32" +if [ ! -d "$ARDUINO_ESP32_PATH" ]; then + echo "Installing ESP32 Arduino Core ..." + script_init_path="$PWD" + mkdir -p "$ARDUINO_USR_PATH/hardware/espressif" + cd "$ARDUINO_USR_PATH/hardware/espressif" + + echo "Installing Python Serial ..." + pip install pyserial > /dev/null + + if [ "$OS_IS_WINDOWS" == "1" ]; then + echo "Installing Python Requests ..." + pip install requests > /dev/null + fi + + if [ "$GITHUB_REPOSITORY" == "espressif/arduino-esp32" ]; then + echo "Linking Core..." + ln -s $GITHUB_WORKSPACE esp32 + else + echo "Cloning Core Repository..." + git clone https://github.com/espressif/arduino-esp32.git esp32 > /dev/null 2>&1 + fi + + echo "Updating Submodules ..." + cd esp32 + git submodule update --init --recursive > /dev/null 2>&1 + + echo "Installing Platform Tools ..." + cd tools && python get.py + cd $script_init_path + + echo "ESP32 Arduino has been installed in '$ARDUINO_ESP32_PATH'" + echo "" +fi diff --git a/lib/AsyncTCP/.github/scripts/install-arduino-ide.sh b/lib/AsyncTCP/.github/scripts/install-arduino-ide.sh new file mode 100755 index 0000000..7e268b1 --- /dev/null +++ b/lib/AsyncTCP/.github/scripts/install-arduino-ide.sh @@ -0,0 +1,220 @@ +#!/bin/bash + +#OSTYPE: 'linux-gnu', ARCH: 'x86_64' => linux64 +#OSTYPE: 'msys', ARCH: 'x86_64' => win32 +#OSTYPE: 'darwin18', ARCH: 'i386' => macos + +OSBITS=`arch` +if [[ "$OSTYPE" == "linux"* ]]; then + export OS_IS_LINUX="1" + ARCHIVE_FORMAT="tar.xz" + if [[ "$OSBITS" == "i686" ]]; then + OS_NAME="linux32" + elif [[ "$OSBITS" == "x86_64" ]]; then + OS_NAME="linux64" + elif [[ "$OSBITS" == "armv7l" || "$OSBITS" == "aarch64" ]]; then + OS_NAME="linuxarm" + else + OS_NAME="$OSTYPE-$OSBITS" + echo "Unknown OS '$OS_NAME'" + exit 1 + fi +elif [[ "$OSTYPE" == "darwin"* ]]; then + export OS_IS_MACOS="1" + ARCHIVE_FORMAT="zip" + OS_NAME="macosx" +elif [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then + export OS_IS_WINDOWS="1" + ARCHIVE_FORMAT="zip" + OS_NAME="windows" +else + OS_NAME="$OSTYPE-$OSBITS" + echo "Unknown OS '$OS_NAME'" + exit 1 +fi +export OS_NAME + +ARDUINO_BUILD_DIR="$HOME/.arduino/build.tmp" +ARDUINO_CACHE_DIR="$HOME/.arduino/cache.tmp" + +if [ "$OS_IS_MACOS" == "1" ]; then + export ARDUINO_IDE_PATH="/Applications/Arduino.app/Contents/Java" + export ARDUINO_USR_PATH="$HOME/Documents/Arduino" +elif [ "$OS_IS_WINDOWS" == "1" ]; then + export ARDUINO_IDE_PATH="$HOME/arduino_ide" + export ARDUINO_USR_PATH="$HOME/Documents/Arduino" +else + export ARDUINO_IDE_PATH="$HOME/arduino_ide" + export ARDUINO_USR_PATH="$HOME/Arduino" +fi + +if [ ! -d "$ARDUINO_IDE_PATH" ]; then + echo "Installing Arduino IDE on $OS_NAME ..." + echo "Downloading 'arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT' to 'arduino.$ARCHIVE_FORMAT' ..." + if [ "$OS_IS_LINUX" == "1" ]; then + wget -O "arduino.$ARCHIVE_FORMAT" "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1 + echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..." + tar xf "arduino.$ARCHIVE_FORMAT" > /dev/null + mv arduino-nightly "$ARDUINO_IDE_PATH" + else + curl -o "arduino.$ARCHIVE_FORMAT" -L "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1 + echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..." + unzip "arduino.$ARCHIVE_FORMAT" > /dev/null + if [ "$OS_IS_MACOS" == "1" ]; then + mv "Arduino.app" "/Applications/Arduino.app" + else + mv arduino-nightly "$ARDUINO_IDE_PATH" + fi + fi + rm -rf "arduino.$ARCHIVE_FORMAT" + + mkdir -p "$ARDUINO_USR_PATH/libraries" + mkdir -p "$ARDUINO_USR_PATH/hardware" + + echo "Arduino IDE Installed in '$ARDUINO_IDE_PATH'" + echo "" +fi + +function build_sketch(){ # build_sketch [extra-options] + if [ "$#" -lt 2 ]; then + echo "ERROR: Illegal number of parameters" + echo "USAGE: build_sketch [extra-options]" + return 1 + fi + + local fqbn="$1" + local sketch="$2" + local xtra_opts="$3" + local win_opts="" + if [ "$OS_IS_WINDOWS" == "1" ]; then + local ctags_version=`ls "$ARDUINO_IDE_PATH/tools-builder/ctags/"` + local preprocessor_version=`ls "$ARDUINO_IDE_PATH/tools-builder/arduino-preprocessor/"` + win_opts="-prefs=runtime.tools.ctags.path=$ARDUINO_IDE_PATH/tools-builder/ctags/$ctags_version -prefs=runtime.tools.arduino-preprocessor.path=$ARDUINO_IDE_PATH/tools-builder/arduino-preprocessor/$preprocessor_version" + fi + + echo "" + echo "Compiling '"$(basename "$sketch")"' ..." + mkdir -p "$ARDUINO_BUILD_DIR" + mkdir -p "$ARDUINO_CACHE_DIR" + $ARDUINO_IDE_PATH/arduino-builder -compile -logger=human -core-api-version=10810 \ + -fqbn=$fqbn \ + -warnings="all" \ + -tools "$ARDUINO_IDE_PATH/tools-builder" \ + -tools "$ARDUINO_IDE_PATH/tools" \ + -built-in-libraries "$ARDUINO_IDE_PATH/libraries" \ + -hardware "$ARDUINO_IDE_PATH/hardware" \ + -hardware "$ARDUINO_USR_PATH/hardware" \ + -libraries "$ARDUINO_USR_PATH/libraries" \ + -build-cache "$ARDUINO_CACHE_DIR" \ + -build-path "$ARDUINO_BUILD_DIR" \ + $win_opts $xtra_opts "$sketch" +} + +function count_sketches() # count_sketches +{ + local examples="$1" + rm -rf sketches.txt + if [ ! -d "$examples" ]; then + touch sketches.txt + return 0 + fi + local sketches=$(find $examples -name *.ino) + local sketchnum=0 + for sketch in $sketches; do + local sketchdir=$(dirname $sketch) + local sketchdirname=$(basename $sketchdir) + local sketchname=$(basename $sketch) + if [[ "${sketchdirname}.ino" != "$sketchname" ]]; then + continue + fi; + if [[ -f "$sketchdir/.test.skip" ]]; then + continue + fi + echo $sketch >> sketches.txt + sketchnum=$(($sketchnum + 1)) + done + return $sketchnum +} + +function build_sketches() # build_sketches [extra-options] +{ + local fqbn=$1 + local examples=$2 + local chunk_idex=$3 + local chunks_num=$4 + local xtra_opts=$5 + + if [ "$#" -lt 2 ]; then + echo "ERROR: Illegal number of parameters" + echo "USAGE: build_sketches [ ] [extra-options]" + return 1 + fi + + if [ "$#" -lt 4 ]; then + chunk_idex="0" + chunks_num="1" + xtra_opts=$3 + fi + + if [ "$chunks_num" -le 0 ]; then + echo "ERROR: Chunks count must be positive number" + return 1 + fi + if [ "$chunk_idex" -ge "$chunks_num" ]; then + echo "ERROR: Chunk index must be less than chunks count" + return 1 + fi + + set +e + count_sketches "$examples" + local sketchcount=$? + set -e + local sketches=$(cat sketches.txt) + rm -rf sketches.txt + + local chunk_size=$(( $sketchcount / $chunks_num )) + local all_chunks=$(( $chunks_num * $chunk_size )) + if [ "$all_chunks" -lt "$sketchcount" ]; then + chunk_size=$(( $chunk_size + 1 )) + fi + + local start_index=$(( $chunk_idex * $chunk_size )) + if [ "$sketchcount" -le "$start_index" ]; then + echo "Skipping job" + return 0 + fi + + local end_index=$(( $(( $chunk_idex + 1 )) * $chunk_size )) + if [ "$end_index" -gt "$sketchcount" ]; then + end_index=$sketchcount + fi + + local start_num=$(( $start_index + 1 )) + echo "Found $sketchcount Sketches"; + echo "Chunk Count : $chunks_num" + echo "Chunk Size : $chunk_size" + echo "Start Sketch: $start_num" + echo "End Sketch : $end_index" + + local sketchnum=0 + for sketch in $sketches; do + local sketchdir=$(dirname $sketch) + local sketchdirname=$(basename $sketchdir) + local sketchname=$(basename $sketch) + if [ "${sketchdirname}.ino" != "$sketchname" ] \ + || [ -f "$sketchdir/.test.skip" ]; then + continue + fi + sketchnum=$(($sketchnum + 1)) + if [ "$sketchnum" -le "$start_index" ] \ + || [ "$sketchnum" -gt "$end_index" ]; then + continue + fi + build_sketch "$fqbn" "$sketch" "$xtra_opts" + local result=$? + if [ $result -ne 0 ]; then + return $result + fi + done + return 0 +} diff --git a/lib/AsyncTCP/.github/scripts/install-platformio.sh b/lib/AsyncTCP/.github/scripts/install-platformio.sh new file mode 100644 index 0000000..61c94fe --- /dev/null +++ b/lib/AsyncTCP/.github/scripts/install-platformio.sh @@ -0,0 +1,133 @@ +#!/bin/bash + +echo "Installing Python Wheel ..." +pip install wheel > /dev/null 2>&1 + +echo "Installing PlatformIO ..." +pip install -U platformio > /dev/null 2>&1 + +echo "PlatformIO has been installed" +echo "" + + +function build_pio_sketch(){ # build_pio_sketch + if [ "$#" -lt 2 ]; then + echo "ERROR: Illegal number of parameters" + echo "USAGE: build_pio_sketch " + return 1 + fi + + local board="$1" + local sketch="$2" + local sketch_dir=$(dirname "$sketch") + echo "" + echo "Compiling '"$(basename "$sketch")"' ..." + python -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv" +} + +function count_sketches() # count_sketches +{ + local examples="$1" + rm -rf sketches.txt + if [ ! -d "$examples" ]; then + touch sketches.txt + return 0 + fi + local sketches=$(find $examples -name *.ino) + local sketchnum=0 + for sketch in $sketches; do + local sketchdir=$(dirname $sketch) + local sketchdirname=$(basename $sketchdir) + local sketchname=$(basename $sketch) + if [[ "${sketchdirname}.ino" != "$sketchname" ]]; then + continue + fi; + if [[ -f "$sketchdir/.test.skip" ]]; then + continue + fi + echo $sketch >> sketches.txt + sketchnum=$(($sketchnum + 1)) + done + return $sketchnum +} + +function build_pio_sketches() # build_pio_sketches +{ + if [ "$#" -lt 2 ]; then + echo "ERROR: Illegal number of parameters" + echo "USAGE: build_pio_sketches [ ]" + return 1 + fi + + local board=$1 + local examples=$2 + local chunk_idex=$3 + local chunks_num=$4 + + if [ "$#" -lt 4 ]; then + chunk_idex="0" + chunks_num="1" + fi + + if [ "$chunks_num" -le 0 ]; then + echo "ERROR: Chunks count must be positive number" + return 1 + fi + if [ "$chunk_idex" -ge "$chunks_num" ]; then + echo "ERROR: Chunk index must be less than chunks count" + return 1 + fi + + set +e + count_sketches "$examples" + local sketchcount=$? + set -e + local sketches=$(cat sketches.txt) + rm -rf sketches.txt + + local chunk_size=$(( $sketchcount / $chunks_num )) + local all_chunks=$(( $chunks_num * $chunk_size )) + if [ "$all_chunks" -lt "$sketchcount" ]; then + chunk_size=$(( $chunk_size + 1 )) + fi + + local start_index=$(( $chunk_idex * $chunk_size )) + if [ "$sketchcount" -le "$start_index" ]; then + echo "Skipping job" + return 0 + fi + + local end_index=$(( $(( $chunk_idex + 1 )) * $chunk_size )) + if [ "$end_index" -gt "$sketchcount" ]; then + end_index=$sketchcount + fi + + local start_num=$(( $start_index + 1 )) + echo "Found $sketchcount Sketches"; + echo "Chunk Count : $chunks_num" + echo "Chunk Size : $chunk_size" + echo "Start Sketch: $start_num" + echo "End Sketch : $end_index" + + local sketchnum=0 + for sketch in $sketches; do + local sketchdir=$(dirname $sketch) + local sketchdirname=$(basename $sketchdir) + local sketchname=$(basename $sketch) + if [ "${sketchdirname}.ino" != "$sketchname" ] \ + || [ -f "$sketchdir/.test.skip" ]; then + continue + fi + sketchnum=$(($sketchnum + 1)) + if [ "$sketchnum" -le "$start_index" ] \ + || [ "$sketchnum" -gt "$end_index" ]; then + continue + fi + build_pio_sketch "$board" "$sketch" + local result=$? + if [ $result -ne 0 ]; then + return $result + fi + done + return 0 +} diff --git a/lib/AsyncTCP/.github/scripts/on-push.sh b/lib/AsyncTCP/.github/scripts/on-push.sh new file mode 100755 index 0000000..ece5d7a --- /dev/null +++ b/lib/AsyncTCP/.github/scripts/on-push.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +set -e + +if [ ! -z "$TRAVIS_BUILD_DIR" ]; then + export GITHUB_WORKSPACE="$TRAVIS_BUILD_DIR" + export GITHUB_REPOSITORY="$TRAVIS_REPO_SLUG" +elif [ -z "$GITHUB_WORKSPACE" ]; then + export GITHUB_WORKSPACE="$PWD" + export GITHUB_REPOSITORY="me-no-dev/AsyncTCP" +fi + +CHUNK_INDEX=$1 +CHUNKS_CNT=$2 +BUILD_PIO=0 +if [ "$#" -lt 2 ] || [ "$CHUNKS_CNT" -le 0 ]; then + CHUNK_INDEX=0 + CHUNKS_CNT=1 +elif [ "$CHUNK_INDEX" -gt "$CHUNKS_CNT" ]; then + CHUNK_INDEX=$CHUNKS_CNT +elif [ "$CHUNK_INDEX" -eq "$CHUNKS_CNT" ]; then + BUILD_PIO=1 +fi + +if [ "$BUILD_PIO" -eq 0 ]; then + # ArduinoIDE Test + source ./.github/scripts/install-arduino-ide.sh + source ./.github/scripts/install-arduino-core-esp32.sh + + echo "Installing AsyncTCP ..." + cp -rf "$GITHUB_WORKSPACE" "$ARDUINO_USR_PATH/libraries/AsyncTCP" + + FQBN="espressif:esp32:esp32:PSRAM=enabled,PartitionScheme=huge_app" + build_sketches "$FQBN" "$GITHUB_WORKSPACE/examples" + if [ ! "$OS_IS_WINDOWS" == "1" ]; then + echo "Installing ESPAsyncWebServer ..." + git clone https://github.com/me-no-dev/ESPAsyncWebServer "$ARDUINO_USR_PATH/libraries/ESPAsyncWebServer" > /dev/null 2>&1 + + echo "Installing ArduinoJson ..." + git clone https://github.com/bblanchon/ArduinoJson "$ARDUINO_USR_PATH/libraries/ArduinoJson" > /dev/null 2>&1 + + build_sketches "$FQBN" "$ARDUINO_USR_PATH/libraries/ESPAsyncWebServer/examples" + fi +else + # PlatformIO Test + source ./.github/scripts/install-platformio.sh + + echo "Installing AsyncTCP ..." + python -m platformio lib --storage-dir "$GITHUB_WORKSPACE" install + + BOARD="esp32dev" + build_pio_sketches "$BOARD" "$GITHUB_WORKSPACE/examples" + + if [[ "$OSTYPE" != "cygwin" ]] && [[ "$OSTYPE" != "msys" ]] && [[ "$OSTYPE" != "win32" ]]; then + echo "Installing ESPAsyncWebServer ..." + python -m platformio lib -g install https://github.com/me-no-dev/ESPAsyncWebServer.git > /dev/null 2>&1 + git clone https://github.com/me-no-dev/ESPAsyncWebServer "$HOME/ESPAsyncWebServer" > /dev/null 2>&1 + + echo "Installing ArduinoJson ..." + python -m platformio lib -g install https://github.com/bblanchon/ArduinoJson.git > /dev/null 2>&1 + + build_pio_sketches "$BOARD" "$HOME/ESPAsyncWebServer/examples" + fi +fi diff --git a/lib/AsyncTCP/.github/stale.yml b/lib/AsyncTCP/.github/stale.yml new file mode 100644 index 0000000..ce7a8e3 --- /dev/null +++ b/lib/AsyncTCP/.github/stale.yml @@ -0,0 +1,31 @@ +# Configuration for probot-stale - https://github.com/probot/stale + +daysUntilStale: 60 +daysUntilClose: 14 +limitPerRun: 30 +staleLabel: stale +exemptLabels: + - pinned + - security + - "to be implemented" + - "for reference" + - "move to PR" + - "enhancement" + +only: issues +onlyLabels: [] +exemptProjects: false +exemptMilestones: false +exemptAssignees: false + +markComment: > + [STALE_SET] This issue has been automatically marked as stale because it has not had + recent activity. It will be closed in 14 days if no further activity occurs. Thank you + for your contributions. + +unmarkComment: > + [STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future. + +closeComment: > + [STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions. + diff --git a/lib/AsyncTCP/.github/workflows/push.yml b/lib/AsyncTCP/.github/workflows/push.yml new file mode 100644 index 0000000..15cd441 --- /dev/null +++ b/lib/AsyncTCP/.github/workflows/push.yml @@ -0,0 +1,32 @@ +name: Async TCP CI + +on: + push: + branches: + - master + - release/* + pull_request: + +jobs: + + build-arduino: + name: Arduino on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macOS-latest] + steps: + - uses: actions/checkout@v1 + - name: Build Tests + run: bash ./.github/scripts/on-push.sh 0 1 + + build-pio: + name: PlatformIO on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macOS-latest] + steps: + - uses: actions/checkout@v1 + - name: Build Tests + run: bash ./.github/scripts/on-push.sh 1 1 diff --git a/lib/AsyncTCP/.gitignore b/lib/AsyncTCP/.gitignore new file mode 100644 index 0000000..9bea433 --- /dev/null +++ b/lib/AsyncTCP/.gitignore @@ -0,0 +1,2 @@ + +.DS_Store diff --git a/lib/AsyncTCP/.travis.yml b/lib/AsyncTCP/.travis.yml new file mode 100644 index 0000000..dbfc064 --- /dev/null +++ b/lib/AsyncTCP/.travis.yml @@ -0,0 +1,34 @@ +sudo: false +language: python +os: + - linux + +git: + depth: false + +stages: + - build + +jobs: + include: + + - name: "Arduino Build" + if: tag IS blank AND (type = pull_request OR (type = push AND branch = master)) + stage: build + script: bash $TRAVIS_BUILD_DIR/.github/scripts/on-push.sh + + - name: "PlatformIO Build" + if: tag IS blank AND (type = pull_request OR (type = push AND branch = master)) + stage: build + script: bash $TRAVIS_BUILD_DIR/.github/scripts/on-push.sh 1 1 + +notifications: + email: + on_success: change + on_failure: change + webhooks: + urls: + - https://webhooks.gitter.im/e/60e65d0c78ea0a920347 + on_success: change # options: [always|never|change] default: always + on_failure: always # options: [always|never|change] default: always + on_start: false # default: false diff --git a/lib/AsyncTCP/CMakeLists.txt b/lib/AsyncTCP/CMakeLists.txt new file mode 100644 index 0000000..f52e1c9 --- /dev/null +++ b/lib/AsyncTCP/CMakeLists.txt @@ -0,0 +1,15 @@ +set(COMPONENT_SRCDIRS + "src" +) + +set(COMPONENT_ADD_INCLUDEDIRS + "src" +) + +set(COMPONENT_REQUIRES + "arduino-esp32" +) + +register_component() + +target_compile_options(${COMPONENT_TARGET} PRIVATE -fno-rtti) diff --git a/lib/AsyncTCP/Kconfig.projbuild b/lib/AsyncTCP/Kconfig.projbuild new file mode 100644 index 0000000..1774926 --- /dev/null +++ b/lib/AsyncTCP/Kconfig.projbuild @@ -0,0 +1,30 @@ +menu "AsyncTCP Configuration" + +choice ASYNC_TCP_RUNNING_CORE + bool "Core on which AsyncTCP's thread is running" + default ASYNC_TCP_RUN_CORE1 + help + Select on which core AsyncTCP is running + + config ASYNC_TCP_RUN_CORE0 + bool "CORE 0" + config ASYNC_TCP_RUN_CORE1 + bool "CORE 1" + config ASYNC_TCP_RUN_NO_AFFINITY + bool "BOTH" + +endchoice + +config ASYNC_TCP_RUNNING_CORE + int + default 0 if ASYNC_TCP_RUN_CORE0 + default 1 if ASYNC_TCP_RUN_CORE1 + default -1 if ASYNC_TCP_RUN_NO_AFFINITY + +config ASYNC_TCP_USE_WDT + bool "Enable WDT for the AsyncTCP task" + default "y" + help + Enable WDT for the AsyncTCP task, so it will trigger if a handler is locking the thread. + +endmenu diff --git a/lib/AsyncTCP/LICENSE b/lib/AsyncTCP/LICENSE new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/lib/AsyncTCP/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/lib/AsyncTCP/README.md b/lib/AsyncTCP/README.md new file mode 100644 index 0000000..983aabd --- /dev/null +++ b/lib/AsyncTCP/README.md @@ -0,0 +1,13 @@ +# AsyncTCP +[![Build Status](https://travis-ci.org/me-no-dev/AsyncTCP.svg?branch=master)](https://travis-ci.org/me-no-dev/AsyncTCP) ![](https://github.com/me-no-dev/AsyncTCP/workflows/Async%20TCP%20CI/badge.svg) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2f7e4d1df8b446d192cbfec6dc174d2d)](https://www.codacy.com/manual/me-no-dev/AsyncTCP?utm_source=github.com&utm_medium=referral&utm_content=me-no-dev/AsyncTCP&utm_campaign=Badge_Grade) + +### Async TCP Library for ESP32 Arduino + +[![Join the chat at https://gitter.im/me-no-dev/ESPAsyncWebServer](https://badges.gitter.im/me-no-dev/ESPAsyncWebServer.svg)](https://gitter.im/me-no-dev/ESPAsyncWebServer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +This is a fully asynchronous TCP library, aimed at enabling trouble-free, multi-connection network environment for Espressif's ESP32 MCUs. + +This library is the base for [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) + +## AsyncClient and AsyncServer +The base classes on which everything else is built. They expose all possible scenarios, but are really raw and require more skills to use. diff --git a/lib/AsyncTCP/component.mk b/lib/AsyncTCP/component.mk new file mode 100644 index 0000000..bb5bb16 --- /dev/null +++ b/lib/AsyncTCP/component.mk @@ -0,0 +1,3 @@ +COMPONENT_ADD_INCLUDEDIRS := src +COMPONENT_SRCDIRS := src +CXXFLAGS += -fno-rtti diff --git a/lib/AsyncTCP/library.json b/lib/AsyncTCP/library.json new file mode 100644 index 0000000..89f90e4 --- /dev/null +++ b/lib/AsyncTCP/library.json @@ -0,0 +1,22 @@ +{ + "name":"AsyncTCP", + "description":"Asynchronous TCP Library for ESP32", + "keywords":"async,tcp", + "authors": + { + "name": "Hristo Gochkov", + "maintainer": true + }, + "repository": + { + "type": "git", + "url": "https://github.com/me-no-dev/AsyncTCP.git" + }, + "version": "1.1.1", + "license": "LGPL-3.0", + "frameworks": "arduino", + "platforms": "espressif32", + "build": { + "libCompatMode": 2 + } +} diff --git a/lib/AsyncTCP/library.properties b/lib/AsyncTCP/library.properties new file mode 100644 index 0000000..eb4e26e --- /dev/null +++ b/lib/AsyncTCP/library.properties @@ -0,0 +1,9 @@ +name=AsyncTCP +version=1.1.1 +author=Me-No-Dev +maintainer=Me-No-Dev +sentence=Async TCP Library for ESP32 +paragraph=Async TCP Library for ESP32 +category=Other +url=https://github.com/me-no-dev/AsyncTCP +architectures=* diff --git a/lib/AsyncTCP/src/AsyncTCP.cpp b/lib/AsyncTCP/src/AsyncTCP.cpp new file mode 100644 index 0000000..89ff6ee --- /dev/null +++ b/lib/AsyncTCP/src/AsyncTCP.cpp @@ -0,0 +1,1357 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "Arduino.h" + +#include "AsyncTCP.h" +extern "C"{ +#include "lwip/opt.h" +#include "lwip/tcp.h" +#include "lwip/inet.h" +#include "lwip/dns.h" +#include "lwip/err.h" +} +#include "esp_task_wdt.h" + +/* + * TCP/IP Event Task + * */ + +typedef enum { + LWIP_TCP_SENT, LWIP_TCP_RECV, LWIP_TCP_FIN, LWIP_TCP_ERROR, LWIP_TCP_POLL, LWIP_TCP_CLEAR, LWIP_TCP_ACCEPT, LWIP_TCP_CONNECTED, LWIP_TCP_DNS +} lwip_event_t; + +typedef struct { + lwip_event_t event; + void *arg; + union { + struct { + void * pcb; + int8_t err; + } connected; + struct { + int8_t err; + } error; + struct { + tcp_pcb * pcb; + uint16_t len; + } sent; + struct { + tcp_pcb * pcb; + pbuf * pb; + int8_t err; + } recv; + struct { + tcp_pcb * pcb; + int8_t err; + } fin; + struct { + tcp_pcb * pcb; + } poll; + struct { + AsyncClient * client; + } accept; + struct { + const char * name; + ip_addr_t addr; + } dns; + }; +} lwip_event_packet_t; + +static xQueueHandle _async_queue; +static TaskHandle_t _async_service_task_handle = NULL; + + +SemaphoreHandle_t _slots_lock; +const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP; +static uint32_t _closed_slots[_number_of_closed_slots]; +static uint32_t _closed_index = []() { + _slots_lock = xSemaphoreCreateBinary(); + xSemaphoreGive(_slots_lock); + for (int i = 0; i < _number_of_closed_slots; ++ i) { + _closed_slots[i] = 1; + } + return 1; +}(); + + +static inline bool _init_async_event_queue(){ + if(!_async_queue){ + _async_queue = xQueueCreate(32, sizeof(lwip_event_packet_t *)); + if(!_async_queue){ + return false; + } + } + return true; +} + +static inline bool _send_async_event(lwip_event_packet_t ** e){ + return _async_queue && xQueueSend(_async_queue, e, portMAX_DELAY) == pdPASS; +} + +static inline bool _prepend_async_event(lwip_event_packet_t ** e){ + return _async_queue && xQueueSendToFront(_async_queue, e, portMAX_DELAY) == pdPASS; +} + +static inline bool _get_async_event(lwip_event_packet_t ** e){ + return _async_queue && xQueueReceive(_async_queue, e, portMAX_DELAY) == pdPASS; +} + +static bool _remove_events_with_arg(void * arg){ + lwip_event_packet_t * first_packet = NULL; + lwip_event_packet_t * packet = NULL; + + if(!_async_queue){ + return false; + } + //figure out which is the first packet so we can keep the order + while(!first_packet){ + if(xQueueReceive(_async_queue, &first_packet, 0) != pdPASS){ + return false; + } + //discard packet if matching + if((int)first_packet->arg == (int)arg){ + free(first_packet); + first_packet = NULL; + //return first packet to the back of the queue + } else if(xQueueSend(_async_queue, &first_packet, portMAX_DELAY) != pdPASS){ + return false; + } + } + + while(xQueuePeek(_async_queue, &packet, 0) == pdPASS && packet != first_packet){ + if(xQueueReceive(_async_queue, &packet, 0) != pdPASS){ + return false; + } + if((int)packet->arg == (int)arg){ + free(packet); + packet = NULL; + } else if(xQueueSend(_async_queue, &packet, portMAX_DELAY) != pdPASS){ + return false; + } + } + return true; +} + +static void _handle_async_event(lwip_event_packet_t * e){ + if(e->arg == NULL){ + // do nothing when arg is NULL + //ets_printf("event arg == NULL: 0x%08x\n", e->recv.pcb); + } else if(e->event == LWIP_TCP_CLEAR){ + _remove_events_with_arg(e->arg); + } else if(e->event == LWIP_TCP_RECV){ + //ets_printf("-R: 0x%08x\n", e->recv.pcb); + AsyncClient::_s_recv(e->arg, e->recv.pcb, e->recv.pb, e->recv.err); + } else if(e->event == LWIP_TCP_FIN){ + //ets_printf("-F: 0x%08x\n", e->fin.pcb); + AsyncClient::_s_fin(e->arg, e->fin.pcb, e->fin.err); + } else if(e->event == LWIP_TCP_SENT){ + //ets_printf("-S: 0x%08x\n", e->sent.pcb); + AsyncClient::_s_sent(e->arg, e->sent.pcb, e->sent.len); + } else if(e->event == LWIP_TCP_POLL){ + //ets_printf("-P: 0x%08x\n", e->poll.pcb); + AsyncClient::_s_poll(e->arg, e->poll.pcb); + } else if(e->event == LWIP_TCP_ERROR){ + //ets_printf("-E: 0x%08x %d\n", e->arg, e->error.err); + AsyncClient::_s_error(e->arg, e->error.err); + } else if(e->event == LWIP_TCP_CONNECTED){ + //ets_printf("C: 0x%08x 0x%08x %d\n", e->arg, e->connected.pcb, e->connected.err); + AsyncClient::_s_connected(e->arg, e->connected.pcb, e->connected.err); + } else if(e->event == LWIP_TCP_ACCEPT){ + //ets_printf("A: 0x%08x 0x%08x\n", e->arg, e->accept.client); + AsyncServer::_s_accepted(e->arg, e->accept.client); + } else if(e->event == LWIP_TCP_DNS){ + //ets_printf("D: 0x%08x %s = %s\n", e->arg, e->dns.name, ipaddr_ntoa(&e->dns.addr)); + AsyncClient::_s_dns_found(e->dns.name, &e->dns.addr, e->arg); + } + free((void*)(e)); +} + +static void _async_service_task(void *pvParameters){ + lwip_event_packet_t * packet = NULL; + for (;;) { + if(_get_async_event(&packet)){ +#if CONFIG_ASYNC_TCP_USE_WDT + if(esp_task_wdt_add(NULL) != ESP_OK){ + log_e("Failed to add async task to WDT"); + } +#endif + _handle_async_event(packet); +#if CONFIG_ASYNC_TCP_USE_WDT + if(esp_task_wdt_delete(NULL) != ESP_OK){ + log_e("Failed to remove loop task from WDT"); + } +#endif + } + } + vTaskDelete(NULL); + _async_service_task_handle = NULL; +} +/* +static void _stop_async_task(){ + if(_async_service_task_handle){ + vTaskDelete(_async_service_task_handle); + _async_service_task_handle = NULL; + } +} +*/ +static bool _start_async_task(){ + if(!_init_async_event_queue()){ + return false; + } + if(!_async_service_task_handle){ + xTaskCreateUniversal(_async_service_task, "async_tcp", 8192 * 2, NULL, 3, &_async_service_task_handle, CONFIG_ASYNC_TCP_RUNNING_CORE); + if(!_async_service_task_handle){ + return false; + } + } + return true; +} + +/* + * LwIP Callbacks + * */ + +static int8_t _tcp_clear_events(void * arg) { + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + e->event = LWIP_TCP_CLEAR; + e->arg = arg; + if (!_prepend_async_event(&e)) { + free((void*)(e)); + } + return ERR_OK; +} + +static int8_t _tcp_connected(void * arg, tcp_pcb * pcb, int8_t err) { + //ets_printf("+C: 0x%08x\n", pcb); + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + e->event = LWIP_TCP_CONNECTED; + e->arg = arg; + e->connected.pcb = pcb; + e->connected.err = err; + if (!_prepend_async_event(&e)) { + free((void*)(e)); + } + return ERR_OK; +} + +static int8_t _tcp_poll(void * arg, struct tcp_pcb * pcb) { + //ets_printf("+P: 0x%08x\n", pcb); + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + e->event = LWIP_TCP_POLL; + e->arg = arg; + e->poll.pcb = pcb; + if (!_send_async_event(&e)) { + free((void*)(e)); + } + return ERR_OK; +} + +static int8_t _tcp_recv(void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_t err) { + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + e->arg = arg; + if(pb){ + //ets_printf("+R: 0x%08x\n", pcb); + e->event = LWIP_TCP_RECV; + e->recv.pcb = pcb; + e->recv.pb = pb; + e->recv.err = err; + } else { + //ets_printf("+F: 0x%08x\n", pcb); + e->event = LWIP_TCP_FIN; + e->fin.pcb = pcb; + e->fin.err = err; + //close the PCB in LwIP thread + AsyncClient::_s_lwip_fin(e->arg, e->fin.pcb, e->fin.err); + } + if (!_send_async_event(&e)) { + free((void*)(e)); + } + return ERR_OK; +} + +static int8_t _tcp_sent(void * arg, struct tcp_pcb * pcb, uint16_t len) { + //ets_printf("+S: 0x%08x\n", pcb); + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + e->event = LWIP_TCP_SENT; + e->arg = arg; + e->sent.pcb = pcb; + e->sent.len = len; + if (!_send_async_event(&e)) { + free((void*)(e)); + } + return ERR_OK; +} + +static void _tcp_error(void * arg, int8_t err) { + //ets_printf("+E: 0x%08x\n", arg); + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + e->event = LWIP_TCP_ERROR; + e->arg = arg; + e->error.err = err; + if (!_send_async_event(&e)) { + free((void*)(e)); + } +} + +static void _tcp_dns_found(const char * name, struct ip_addr * ipaddr, void * arg) { + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + //ets_printf("+DNS: name=%s ipaddr=0x%08x arg=%x\n", name, ipaddr, arg); + e->event = LWIP_TCP_DNS; + e->arg = arg; + e->dns.name = name; + if (ipaddr) { + memcpy(&e->dns.addr, ipaddr, sizeof(struct ip_addr)); + } else { + memset(&e->dns.addr, 0, sizeof(e->dns.addr)); + } + if (!_send_async_event(&e)) { + free((void*)(e)); + } +} + +//Used to switch out from LwIP thread +static int8_t _tcp_accept(void * arg, AsyncClient * client) { + lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t)); + e->event = LWIP_TCP_ACCEPT; + e->arg = arg; + e->accept.client = client; + if (!_prepend_async_event(&e)) { + free((void*)(e)); + } + return ERR_OK; +} + +/* + * TCP/IP API Calls + * */ + +#include "lwip/priv/tcpip_priv.h" + +typedef struct { + struct tcpip_api_call_data call; + tcp_pcb * pcb; + int8_t closed_slot; + int8_t err; + union { + struct { + const char* data; + size_t size; + uint8_t apiflags; + } write; + size_t received; + struct { + ip_addr_t * addr; + uint16_t port; + tcp_connected_fn cb; + } connect; + struct { + ip_addr_t * addr; + uint16_t port; + } bind; + uint8_t backlog; + }; +} tcp_api_call_t; + +static err_t _tcp_output_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = ERR_CONN; + if(msg->closed_slot == -1 || !_closed_slots[msg->closed_slot]) { + msg->err = tcp_output(msg->pcb); + } + return msg->err; +} + +static esp_err_t _tcp_output(tcp_pcb * pcb, int8_t closed_slot) { + if(!pcb){ + return ERR_CONN; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = closed_slot; + tcpip_api_call(_tcp_output_api, (struct tcpip_api_call_data*)&msg); + return msg.err; +} + +static err_t _tcp_write_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = ERR_CONN; + if(msg->closed_slot == -1 || !_closed_slots[msg->closed_slot]) { + msg->err = tcp_write(msg->pcb, msg->write.data, msg->write.size, msg->write.apiflags); + } + return msg->err; +} + +static esp_err_t _tcp_write(tcp_pcb * pcb, int8_t closed_slot, const char* data, size_t size, uint8_t apiflags) { + if(!pcb){ + return ERR_CONN; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = closed_slot; + msg.write.data = data; + msg.write.size = size; + msg.write.apiflags = apiflags; + tcpip_api_call(_tcp_write_api, (struct tcpip_api_call_data*)&msg); + return msg.err; +} + +static err_t _tcp_recved_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = ERR_CONN; + if(msg->closed_slot == -1 || !_closed_slots[msg->closed_slot]) { + msg->err = 0; + tcp_recved(msg->pcb, msg->received); + } + return msg->err; +} + +static esp_err_t _tcp_recved(tcp_pcb * pcb, int8_t closed_slot, size_t len) { + if(!pcb){ + return ERR_CONN; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = closed_slot; + msg.received = len; + tcpip_api_call(_tcp_recved_api, (struct tcpip_api_call_data*)&msg); + return msg.err; +} + +static err_t _tcp_close_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = ERR_CONN; + if(msg->closed_slot == -1 || !_closed_slots[msg->closed_slot]) { + msg->err = tcp_close(msg->pcb); + } + return msg->err; +} + +static esp_err_t _tcp_close(tcp_pcb * pcb, int8_t closed_slot) { + if(!pcb){ + return ERR_CONN; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = closed_slot; + tcpip_api_call(_tcp_close_api, (struct tcpip_api_call_data*)&msg); + return msg.err; +} + +static err_t _tcp_abort_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = ERR_CONN; + if(msg->closed_slot == -1 || !_closed_slots[msg->closed_slot]) { + tcp_abort(msg->pcb); + } + return msg->err; +} + +static esp_err_t _tcp_abort(tcp_pcb * pcb, int8_t closed_slot) { + if(!pcb){ + return ERR_CONN; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = closed_slot; + tcpip_api_call(_tcp_abort_api, (struct tcpip_api_call_data*)&msg); + return msg.err; +} + +static err_t _tcp_connect_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = tcp_connect(msg->pcb, msg->connect.addr, msg->connect.port, msg->connect.cb); + return msg->err; +} + +static esp_err_t _tcp_connect(tcp_pcb * pcb, int8_t closed_slot, ip_addr_t * addr, uint16_t port, tcp_connected_fn cb) { + if(!pcb){ + return ESP_FAIL; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = closed_slot; + msg.connect.addr = addr; + msg.connect.port = port; + msg.connect.cb = cb; + tcpip_api_call(_tcp_connect_api, (struct tcpip_api_call_data*)&msg); + return msg.err; +} + +static err_t _tcp_bind_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = tcp_bind(msg->pcb, msg->bind.addr, msg->bind.port); + return msg->err; +} + +static esp_err_t _tcp_bind(tcp_pcb * pcb, ip_addr_t * addr, uint16_t port) { + if(!pcb){ + return ESP_FAIL; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = -1; + msg.bind.addr = addr; + msg.bind.port = port; + tcpip_api_call(_tcp_bind_api, (struct tcpip_api_call_data*)&msg); + return msg.err; +} + +static err_t _tcp_listen_api(struct tcpip_api_call_data *api_call_msg){ + tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg; + msg->err = 0; + msg->pcb = tcp_listen_with_backlog(msg->pcb, msg->backlog); + return msg->err; +} + +static tcp_pcb * _tcp_listen_with_backlog(tcp_pcb * pcb, uint8_t backlog) { + if(!pcb){ + return NULL; + } + tcp_api_call_t msg; + msg.pcb = pcb; + msg.closed_slot = -1; + msg.backlog = backlog?backlog:0xFF; + tcpip_api_call(_tcp_listen_api, (struct tcpip_api_call_data*)&msg); + return msg.pcb; +} + + + +/* + Async TCP Client + */ + +AsyncClient::AsyncClient(tcp_pcb* pcb) +: _connect_cb(0) +, _connect_cb_arg(0) +, _discard_cb(0) +, _discard_cb_arg(0) +, _sent_cb(0) +, _sent_cb_arg(0) +, _error_cb(0) +, _error_cb_arg(0) +, _recv_cb(0) +, _recv_cb_arg(0) +, _pb_cb(0) +, _pb_cb_arg(0) +, _timeout_cb(0) +, _timeout_cb_arg(0) +, _pcb_busy(false) +, _pcb_sent_at(0) +, _ack_pcb(true) +, _rx_last_packet(0) +, _rx_since_timeout(0) +, _ack_timeout(ASYNC_MAX_ACK_TIME) +, _connect_port(0) +, prev(NULL) +, next(NULL) +{ + _pcb = pcb; + _closed_slot = -1; + if(_pcb){ + _allocate_closed_slot(); + _rx_last_packet = millis(); + tcp_arg(_pcb, this); + tcp_recv(_pcb, &_tcp_recv); + tcp_sent(_pcb, &_tcp_sent); + tcp_err(_pcb, &_tcp_error); + tcp_poll(_pcb, &_tcp_poll, 1); + } +} + +AsyncClient::~AsyncClient(){ + if(_pcb) { + _close(); + } + _free_closed_slot(); +} + +/* + * Operators + * */ + +AsyncClient& AsyncClient::operator=(const AsyncClient& other){ + if (_pcb) { + _close(); + } + + _pcb = other._pcb; + _closed_slot = other._closed_slot; + if (_pcb) { + _rx_last_packet = millis(); + tcp_arg(_pcb, this); + tcp_recv(_pcb, &_tcp_recv); + tcp_sent(_pcb, &_tcp_sent); + tcp_err(_pcb, &_tcp_error); + tcp_poll(_pcb, &_tcp_poll, 1); + } + return *this; +} + +bool AsyncClient::operator==(const AsyncClient &other) { + return _pcb == other._pcb; +} + +AsyncClient & AsyncClient::operator+=(const AsyncClient &other) { + if(next == NULL){ + next = (AsyncClient*)(&other); + next->prev = this; + } else { + AsyncClient *c = next; + while(c->next != NULL) { + c = c->next; + } + c->next =(AsyncClient*)(&other); + c->next->prev = c; + } + return *this; +} + +/* + * Callback Setters + * */ + +void AsyncClient::onConnect(AcConnectHandler cb, void* arg){ + _connect_cb = cb; + _connect_cb_arg = arg; +} + +void AsyncClient::onDisconnect(AcConnectHandler cb, void* arg){ + _discard_cb = cb; + _discard_cb_arg = arg; +} + +void AsyncClient::onAck(AcAckHandler cb, void* arg){ + _sent_cb = cb; + _sent_cb_arg = arg; +} + +void AsyncClient::onError(AcErrorHandler cb, void* arg){ + _error_cb = cb; + _error_cb_arg = arg; +} + +void AsyncClient::onData(AcDataHandler cb, void* arg){ + _recv_cb = cb; + _recv_cb_arg = arg; +} + +void AsyncClient::onPacket(AcPacketHandler cb, void* arg){ + _pb_cb = cb; + _pb_cb_arg = arg; +} + +void AsyncClient::onTimeout(AcTimeoutHandler cb, void* arg){ + _timeout_cb = cb; + _timeout_cb_arg = arg; +} + +void AsyncClient::onPoll(AcConnectHandler cb, void* arg){ + _poll_cb = cb; + _poll_cb_arg = arg; +} + +/* + * Main Public Methods + * */ + +bool AsyncClient::connect(IPAddress ip, uint16_t port){ + if (_pcb){ + log_w("already connected, state %d", _pcb->state); + return false; + } + if(!_start_async_task()){ + log_e("failed to start task"); + return false; + } + + ip_addr_t addr; + addr.type = IPADDR_TYPE_V4; + addr.u_addr.ip4.addr = ip; + + tcp_pcb* pcb = tcp_new_ip_type(IPADDR_TYPE_V4); + if (!pcb){ + log_e("pcb == NULL"); + return false; + } + + tcp_arg(pcb, this); + tcp_err(pcb, &_tcp_error); + tcp_recv(pcb, &_tcp_recv); + tcp_sent(pcb, &_tcp_sent); + tcp_poll(pcb, &_tcp_poll, 1); + //_tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected); + _tcp_connect(pcb, _closed_slot, &addr, port,(tcp_connected_fn)&_tcp_connected); + return true; +} + +bool AsyncClient::connect(const char* host, uint16_t port){ + ip_addr_t addr; + + if(!_start_async_task()){ + log_e("failed to start task"); + return false; + } + + err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_tcp_dns_found, this); + if(err == ERR_OK) { + return connect(IPAddress(addr.u_addr.ip4.addr), port); + } else if(err == ERR_INPROGRESS) { + _connect_port = port; + return true; + } + log_e("error: %d", err); + return false; +} + +void AsyncClient::close(bool now){ + if(_pcb){ + _tcp_recved(_pcb, _closed_slot, _rx_ack_len); + } + _close(); +} + +int8_t AsyncClient::abort(){ + if(_pcb) { + _tcp_abort(_pcb, _closed_slot ); + _pcb = NULL; + } + return ERR_ABRT; +} + +size_t AsyncClient::space(){ + if((_pcb != NULL) && (_pcb->state == 4)){ + return tcp_sndbuf(_pcb); + } + return 0; +} + +size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) { + if(!_pcb || size == 0 || data == NULL) { + return 0; + } + size_t room = space(); + if(!room) { + return 0; + } + size_t will_send = (room < size) ? room : size; + int8_t err = ERR_OK; + err = _tcp_write(_pcb, _closed_slot, data, will_send, apiflags); + if(err != ERR_OK) { + return 0; + } + return will_send; +} + +bool AsyncClient::send(){ + int8_t err = ERR_OK; + err = _tcp_output(_pcb, _closed_slot); + if(err == ERR_OK){ + _pcb_busy = true; + _pcb_sent_at = millis(); + return true; + } + return false; +} + +size_t AsyncClient::ack(size_t len){ + if(len > _rx_ack_len) + len = _rx_ack_len; + if(len){ + _tcp_recved(_pcb, _closed_slot, len); + } + _rx_ack_len -= len; + return len; +} + +void AsyncClient::ackPacket(struct pbuf * pb){ + if(!pb){ + return; + } + _tcp_recved(_pcb, _closed_slot, pb->len); + pbuf_free(pb); +} + +/* + * Main Private Methods + * */ + +int8_t AsyncClient::_close(){ + //ets_printf("X: 0x%08x\n", (uint32_t)this); + int8_t err = ERR_OK; + if(_pcb) { + //log_i(""); + tcp_arg(_pcb, NULL); + tcp_sent(_pcb, NULL); + tcp_recv(_pcb, NULL); + tcp_err(_pcb, NULL); + tcp_poll(_pcb, NULL, 0); + _tcp_clear_events(this); + err = _tcp_close(_pcb, _closed_slot); + if(err != ERR_OK) { + err = abort(); + } + _pcb = NULL; + if(_discard_cb) { + _discard_cb(_discard_cb_arg, this); + } + } + return err; +} + +void AsyncClient::_allocate_closed_slot(){ + xSemaphoreTake(_slots_lock, portMAX_DELAY); + uint32_t closed_slot_min_index = 0; + for (int i = 0; i < _number_of_closed_slots; ++ i) { + if ((_closed_slot == -1 || _closed_slots[i] <= closed_slot_min_index) && _closed_slots[i] != 0) { + closed_slot_min_index = _closed_slots[i]; + _closed_slot = i; + } + } + if (_closed_slot != -1) { + _closed_slots[_closed_slot] = 0; + } + xSemaphoreGive(_slots_lock); +} + +void AsyncClient::_free_closed_slot(){ + if (_closed_slot != -1) { + _closed_slots[_closed_slot] = _closed_index; + _closed_slot = -1; + ++ _closed_index; + } +} + +/* + * Private Callbacks + * */ + +int8_t AsyncClient::_connected(void* pcb, int8_t err){ + _pcb = reinterpret_cast(pcb); + if(_pcb){ + _rx_last_packet = millis(); + _pcb_busy = false; +// tcp_recv(_pcb, &_tcp_recv); +// tcp_sent(_pcb, &_tcp_sent); +// tcp_poll(_pcb, &_tcp_poll, 1); + } + if(_connect_cb) { + _connect_cb(_connect_cb_arg, this); + } + return ERR_OK; +} + +void AsyncClient::_error(int8_t err) { + if(_pcb){ + tcp_arg(_pcb, NULL); + if(_pcb->state == LISTEN) { + tcp_sent(_pcb, NULL); + tcp_recv(_pcb, NULL); + tcp_err(_pcb, NULL); + tcp_poll(_pcb, NULL, 0); + } + _pcb = NULL; + } + if(_error_cb) { + _error_cb(_error_cb_arg, this, err); + } + if(_discard_cb) { + _discard_cb(_discard_cb_arg, this); + } +} + +//In LwIP Thread +int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) { + if(!_pcb || pcb != _pcb){ + log_e("0x%08x != 0x%08x", (uint32_t)pcb, (uint32_t)_pcb); + return ERR_OK; + } + tcp_arg(_pcb, NULL); + if(_pcb->state == LISTEN) { + tcp_sent(_pcb, NULL); + tcp_recv(_pcb, NULL); + tcp_err(_pcb, NULL); + tcp_poll(_pcb, NULL, 0); + } + if(tcp_close(_pcb) != ERR_OK) { + tcp_abort(_pcb); + } + _free_closed_slot(); + _pcb = NULL; + return ERR_OK; +} + +//In Async Thread +int8_t AsyncClient::_fin(tcp_pcb* pcb, int8_t err) { + _tcp_clear_events(this); + if(_discard_cb) { + _discard_cb(_discard_cb_arg, this); + } + return ERR_OK; +} + +int8_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) { + _rx_last_packet = millis(); + //log_i("%u", len); + _pcb_busy = false; + if(_sent_cb) { + _sent_cb(_sent_cb_arg, this, len, (millis() - _pcb_sent_at)); + } + return ERR_OK; +} + +int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) { + while(pb != NULL) { + _rx_last_packet = millis(); + //we should not ack before we assimilate the data + _ack_pcb = true; + pbuf *b = pb; + pb = b->next; + b->next = NULL; + if(_pb_cb){ + _pb_cb(_pb_cb_arg, this, b); + } else { + if(_recv_cb) { + _recv_cb(_recv_cb_arg, this, b->payload, b->len); + } + if(!_ack_pcb) { + _rx_ack_len += b->len; + } else if(_pcb) { + _tcp_recved(_pcb, _closed_slot, b->len); + } + pbuf_free(b); + } + } + return ERR_OK; +} + +int8_t AsyncClient::_poll(tcp_pcb* pcb){ + if(!_pcb){ + log_w("pcb is NULL"); + return ERR_OK; + } + if(pcb != _pcb){ + log_e("0x%08x != 0x%08x", (uint32_t)pcb, (uint32_t)_pcb); + return ERR_OK; + } + + uint32_t now = millis(); + + // ACK Timeout + if(_pcb_busy && _ack_timeout && (now - _pcb_sent_at) >= _ack_timeout){ + _pcb_busy = false; + log_w("ack timeout %d", pcb->state); + if(_timeout_cb) + _timeout_cb(_timeout_cb_arg, this, (now - _pcb_sent_at)); + return ERR_OK; + } + // RX Timeout + if(_rx_since_timeout && (now - _rx_last_packet) >= (_rx_since_timeout * 1000)){ + log_w("rx timeout %d", pcb->state); + _close(); + return ERR_OK; + } + // Everything is fine + if(_poll_cb) { + _poll_cb(_poll_cb_arg, this); + } + return ERR_OK; +} + +void AsyncClient::_dns_found(struct ip_addr *ipaddr){ + if(ipaddr && ipaddr->u_addr.ip4.addr){ + connect(IPAddress(ipaddr->u_addr.ip4.addr), _connect_port); + } else { + if(_error_cb) { + _error_cb(_error_cb_arg, this, -55); + } + if(_discard_cb) { + _discard_cb(_discard_cb_arg, this); + } + } +} + +/* + * Public Helper Methods + * */ + +void AsyncClient::stop() { + close(false); +} + +bool AsyncClient::free(){ + if(!_pcb) { + return true; + } + if(_pcb->state == 0 || _pcb->state > 4) { + return true; + } + return false; +} + +size_t AsyncClient::write(const char* data) { + if(data == NULL) { + return 0; + } + return write(data, strlen(data)); +} + +size_t AsyncClient::write(const char* data, size_t size, uint8_t apiflags) { + size_t will_send = add(data, size, apiflags); + if(!will_send || !send()) { + return 0; + } + return will_send; +} + +void AsyncClient::setRxTimeout(uint32_t timeout){ + _rx_since_timeout = timeout; +} + +uint32_t AsyncClient::getRxTimeout(){ + return _rx_since_timeout; +} + +uint32_t AsyncClient::getAckTimeout(){ + return _ack_timeout; +} + +void AsyncClient::setAckTimeout(uint32_t timeout){ + _ack_timeout = timeout; +} + +void AsyncClient::setNoDelay(bool nodelay){ + if(!_pcb) { + return; + } + if(nodelay) { + tcp_nagle_disable(_pcb); + } else { + tcp_nagle_enable(_pcb); + } +} + +bool AsyncClient::getNoDelay(){ + if(!_pcb) { + return false; + } + return tcp_nagle_disabled(_pcb); +} + +uint16_t AsyncClient::getMss(){ + if(!_pcb) { + return 0; + } + return tcp_mss(_pcb); +} + +uint32_t AsyncClient::getRemoteAddress() { + if(!_pcb) { + return 0; + } + return _pcb->remote_ip.u_addr.ip4.addr; +} + +uint16_t AsyncClient::getRemotePort() { + if(!_pcb) { + return 0; + } + return _pcb->remote_port; +} + +uint32_t AsyncClient::getLocalAddress() { + if(!_pcb) { + return 0; + } + return _pcb->local_ip.u_addr.ip4.addr; +} + +uint16_t AsyncClient::getLocalPort() { + if(!_pcb) { + return 0; + } + return _pcb->local_port; +} + +IPAddress AsyncClient::remoteIP() { + return IPAddress(getRemoteAddress()); +} + +uint16_t AsyncClient::remotePort() { + return getRemotePort(); +} + +IPAddress AsyncClient::localIP() { + return IPAddress(getLocalAddress()); +} + +uint16_t AsyncClient::localPort() { + return getLocalPort(); +} + +uint8_t AsyncClient::state() { + if(!_pcb) { + return 0; + } + return _pcb->state; +} + +bool AsyncClient::connected(){ + if (!_pcb) { + return false; + } + return _pcb->state == 4; +} + +bool AsyncClient::connecting(){ + if (!_pcb) { + return false; + } + return _pcb->state > 0 && _pcb->state < 4; +} + +bool AsyncClient::disconnecting(){ + if (!_pcb) { + return false; + } + return _pcb->state > 4 && _pcb->state < 10; +} + +bool AsyncClient::disconnected(){ + if (!_pcb) { + return true; + } + return _pcb->state == 0 || _pcb->state == 10; +} + +bool AsyncClient::freeable(){ + if (!_pcb) { + return true; + } + return _pcb->state == 0 || _pcb->state > 4; +} + +bool AsyncClient::canSend(){ + return space() > 0; +} + +const char * AsyncClient::errorToString(int8_t error){ + switch(error){ + case ERR_OK: return "OK"; + case ERR_MEM: return "Out of memory error"; + case ERR_BUF: return "Buffer error"; + case ERR_TIMEOUT: return "Timeout"; + case ERR_RTE: return "Routing problem"; + case ERR_INPROGRESS: return "Operation in progress"; + case ERR_VAL: return "Illegal value"; + case ERR_WOULDBLOCK: return "Operation would block"; + case ERR_USE: return "Address in use"; + case ERR_ALREADY: return "Already connected"; + case ERR_CONN: return "Not connected"; + case ERR_IF: return "Low-level netif error"; + case ERR_ABRT: return "Connection aborted"; + case ERR_RST: return "Connection reset"; + case ERR_CLSD: return "Connection closed"; + case ERR_ARG: return "Illegal argument"; + case -55: return "DNS failed"; + default: return "UNKNOWN"; + } +} + +const char * AsyncClient::stateToString(){ + switch(state()){ + case 0: return "Closed"; + case 1: return "Listen"; + case 2: return "SYN Sent"; + case 3: return "SYN Received"; + case 4: return "Established"; + case 5: return "FIN Wait 1"; + case 6: return "FIN Wait 2"; + case 7: return "Close Wait"; + case 8: return "Closing"; + case 9: return "Last ACK"; + case 10: return "Time Wait"; + default: return "UNKNOWN"; + } +} + +/* + * Static Callbacks (LwIP C2C++ interconnect) + * */ + +void AsyncClient::_s_dns_found(const char * name, struct ip_addr * ipaddr, void * arg){ + reinterpret_cast(arg)->_dns_found(ipaddr); +} + +int8_t AsyncClient::_s_poll(void * arg, struct tcp_pcb * pcb) { + return reinterpret_cast(arg)->_poll(pcb); +} + +int8_t AsyncClient::_s_recv(void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_t err) { + return reinterpret_cast(arg)->_recv(pcb, pb, err); +} + +int8_t AsyncClient::_s_fin(void * arg, struct tcp_pcb * pcb, int8_t err) { + return reinterpret_cast(arg)->_fin(pcb, err); +} + +int8_t AsyncClient::_s_lwip_fin(void * arg, struct tcp_pcb * pcb, int8_t err) { + return reinterpret_cast(arg)->_lwip_fin(pcb, err); +} + +int8_t AsyncClient::_s_sent(void * arg, struct tcp_pcb * pcb, uint16_t len) { + return reinterpret_cast(arg)->_sent(pcb, len); +} + +void AsyncClient::_s_error(void * arg, int8_t err) { + reinterpret_cast(arg)->_error(err); +} + +int8_t AsyncClient::_s_connected(void * arg, void * pcb, int8_t err){ + return reinterpret_cast(arg)->_connected(pcb, err); +} + +/* + Async TCP Server + */ + +AsyncServer::AsyncServer(IPAddress addr, uint16_t port) +: _port(port) +, _addr(addr) +, _noDelay(false) +, _pcb(0) +, _connect_cb(0) +, _connect_cb_arg(0) +{} + +AsyncServer::AsyncServer(uint16_t port) +: _port(port) +, _addr((uint32_t) IPADDR_ANY) +, _noDelay(false) +, _pcb(0) +, _connect_cb(0) +, _connect_cb_arg(0) +{} + +AsyncServer::~AsyncServer(){ + end(); +} + +void AsyncServer::onClient(AcConnectHandler cb, void* arg){ + _connect_cb = cb; + _connect_cb_arg = arg; +} + +void AsyncServer::begin(){ + if(_pcb) { + return; + } + + if(!_start_async_task()){ + log_e("failed to start task"); + return; + } + int8_t err; + _pcb = tcp_new_ip_type(IPADDR_TYPE_V4); + if (!_pcb){ + log_e("_pcb == NULL"); + return; + } + + ip_addr_t local_addr; + local_addr.type = IPADDR_TYPE_V4; + local_addr.u_addr.ip4.addr = (uint32_t) _addr; + err = _tcp_bind(_pcb, &local_addr, _port); + + if (err != ERR_OK) { + _tcp_close(_pcb, -1); + log_e("bind error: %d", err); + return; + } + + static uint8_t backlog = 5; + _pcb = _tcp_listen_with_backlog(_pcb, backlog); + if (!_pcb) { + log_e("listen_pcb == NULL"); + return; + } + tcp_arg(_pcb, (void*) this); + tcp_accept(_pcb, &_s_accept); +} + +void AsyncServer::end(){ + if(_pcb){ + tcp_arg(_pcb, NULL); + tcp_accept(_pcb, NULL); + if(tcp_close(_pcb) != ERR_OK){ + _tcp_abort(_pcb, -1); + } + _pcb = NULL; + } +} + +//runs on LwIP thread +int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){ + //ets_printf("+A: 0x%08x\n", pcb); + if(_connect_cb){ + AsyncClient *c = new AsyncClient(pcb); + if(c){ + c->setNoDelay(_noDelay); + return _tcp_accept(this, c); + } + } + if(tcp_close(pcb) != ERR_OK){ + tcp_abort(pcb); + } + log_e("FAIL"); + return ERR_OK; +} + +int8_t AsyncServer::_accepted(AsyncClient* client){ + if(_connect_cb){ + _connect_cb(_connect_cb_arg, client); + } + return ERR_OK; +} + +void AsyncServer::setNoDelay(bool nodelay){ + _noDelay = nodelay; +} + +bool AsyncServer::getNoDelay(){ + return _noDelay; +} + +uint8_t AsyncServer::status(){ + if (!_pcb) { + return 0; + } + return _pcb->state; +} + +int8_t AsyncServer::_s_accept(void * arg, tcp_pcb * pcb, int8_t err){ + return reinterpret_cast(arg)->_accept(pcb, err); +} + +int8_t AsyncServer::_s_accepted(void *arg, AsyncClient* client){ + return reinterpret_cast(arg)->_accepted(client); +} diff --git a/lib/AsyncTCP/src/AsyncTCP.h b/lib/AsyncTCP/src/AsyncTCP.h new file mode 100644 index 0000000..ac87ded --- /dev/null +++ b/lib/AsyncTCP/src/AsyncTCP.h @@ -0,0 +1,217 @@ +/* + Asynchronous TCP library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef ASYNCTCP_H_ +#define ASYNCTCP_H_ + +#include "IPAddress.h" +#include "sdkconfig.h" +#include +extern "C" { + #include "freertos/semphr.h" + #include "lwip/pbuf.h" +} + +//If core is not defined, then we are running in Arduino or PIO +#ifndef CONFIG_ASYNC_TCP_RUNNING_CORE +#define CONFIG_ASYNC_TCP_RUNNING_CORE -1 //any available core +#define CONFIG_ASYNC_TCP_USE_WDT 1 //if enabled, adds between 33us and 200us per event +#endif + +class AsyncClient; + +#define ASYNC_MAX_ACK_TIME 5000 +#define ASYNC_WRITE_FLAG_COPY 0x01 //will allocate new buffer to hold the data while sending (else will hold reference to the data given) +#define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react. + +typedef std::function AcConnectHandler; +typedef std::function AcAckHandler; +typedef std::function AcErrorHandler; +typedef std::function AcDataHandler; +typedef std::function AcPacketHandler; +typedef std::function AcTimeoutHandler; + +struct tcp_pcb; +struct ip_addr; + +class AsyncClient { + public: + AsyncClient(tcp_pcb* pcb = 0); + ~AsyncClient(); + + AsyncClient & operator=(const AsyncClient &other); + AsyncClient & operator+=(const AsyncClient &other); + + bool operator==(const AsyncClient &other); + + bool operator!=(const AsyncClient &other) { + return !(*this == other); + } + bool connect(IPAddress ip, uint16_t port); + bool connect(const char* host, uint16_t port); + void close(bool now = false); + void stop(); + int8_t abort(); + bool free(); + + bool canSend();//ack is not pending + size_t space();//space available in the TCP window + size_t add(const char* data, size_t size, uint8_t apiflags=ASYNC_WRITE_FLAG_COPY);//add for sending + bool send();//send all data added with the method above + + //write equals add()+send() + size_t write(const char* data); + size_t write(const char* data, size_t size, uint8_t apiflags=ASYNC_WRITE_FLAG_COPY); //only when canSend() == true + + uint8_t state(); + bool connecting(); + bool connected(); + bool disconnecting(); + bool disconnected(); + bool freeable();//disconnected or disconnecting + + uint16_t getMss(); + + uint32_t getRxTimeout(); + void setRxTimeout(uint32_t timeout);//no RX data timeout for the connection in seconds + + uint32_t getAckTimeout(); + void setAckTimeout(uint32_t timeout);//no ACK timeout for the last sent packet in milliseconds + + void setNoDelay(bool nodelay); + bool getNoDelay(); + + uint32_t getRemoteAddress(); + uint16_t getRemotePort(); + uint32_t getLocalAddress(); + uint16_t getLocalPort(); + + //compatibility + IPAddress remoteIP(); + uint16_t remotePort(); + IPAddress localIP(); + uint16_t localPort(); + + void onConnect(AcConnectHandler cb, void* arg = 0); //on successful connect + void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected + void onAck(AcAckHandler cb, void* arg = 0); //ack received + void onError(AcErrorHandler cb, void* arg = 0); //unsuccessful connect or error + void onData(AcDataHandler cb, void* arg = 0); //data received (called if onPacket is not used) + void onPacket(AcPacketHandler cb, void* arg = 0); //data received + void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout + void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected + + void ackPacket(struct pbuf * pb);//ack pbuf from onPacket + size_t ack(size_t len); //ack data that you have not acked using the method below + void ackLater(){ _ack_pcb = false; } //will not ack the current packet. Call from onData + + const char * errorToString(int8_t error); + const char * stateToString(); + + //Do not use any of the functions below! + static int8_t _s_poll(void *arg, struct tcp_pcb *tpcb); + static int8_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, int8_t err); + static int8_t _s_fin(void *arg, struct tcp_pcb *tpcb, int8_t err); + static int8_t _s_lwip_fin(void *arg, struct tcp_pcb *tpcb, int8_t err); + static void _s_error(void *arg, int8_t err); + static int8_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len); + static int8_t _s_connected(void* arg, void* tpcb, int8_t err); + static void _s_dns_found(const char *name, struct ip_addr *ipaddr, void *arg); + + int8_t _recv(tcp_pcb* pcb, pbuf* pb, int8_t err); + tcp_pcb * pcb(){ return _pcb; } + + protected: + tcp_pcb* _pcb; + int8_t _closed_slot; + + AcConnectHandler _connect_cb; + void* _connect_cb_arg; + AcConnectHandler _discard_cb; + void* _discard_cb_arg; + AcAckHandler _sent_cb; + void* _sent_cb_arg; + AcErrorHandler _error_cb; + void* _error_cb_arg; + AcDataHandler _recv_cb; + void* _recv_cb_arg; + AcPacketHandler _pb_cb; + void* _pb_cb_arg; + AcTimeoutHandler _timeout_cb; + void* _timeout_cb_arg; + AcConnectHandler _poll_cb; + void* _poll_cb_arg; + + bool _pcb_busy; + uint32_t _pcb_sent_at; + bool _ack_pcb; + uint32_t _rx_ack_len; + uint32_t _rx_last_packet; + uint32_t _rx_since_timeout; + uint32_t _ack_timeout; + uint16_t _connect_port; + + int8_t _close(); + void _free_closed_slot(); + void _allocate_closed_slot(); + int8_t _connected(void* pcb, int8_t err); + void _error(int8_t err); + int8_t _poll(tcp_pcb* pcb); + int8_t _sent(tcp_pcb* pcb, uint16_t len); + int8_t _fin(tcp_pcb* pcb, int8_t err); + int8_t _lwip_fin(tcp_pcb* pcb, int8_t err); + void _dns_found(struct ip_addr *ipaddr); + + public: + AsyncClient* prev; + AsyncClient* next; +}; + +class AsyncServer { + public: + AsyncServer(IPAddress addr, uint16_t port); + AsyncServer(uint16_t port); + ~AsyncServer(); + void onClient(AcConnectHandler cb, void* arg); + void begin(); + void end(); + void setNoDelay(bool nodelay); + bool getNoDelay(); + uint8_t status(); + + //Do not use any of the functions below! + static int8_t _s_accept(void *arg, tcp_pcb* newpcb, int8_t err); + static int8_t _s_accepted(void *arg, AsyncClient* client); + + protected: + uint16_t _port; + IPAddress _addr; + bool _noDelay; + tcp_pcb* _pcb; + AcConnectHandler _connect_cb; + void* _connect_cb_arg; + + int8_t _accept(tcp_pcb* newpcb, int8_t err); + int8_t _accepted(AsyncClient* client); +}; + + +#endif /* ASYNCTCP_H_ */ diff --git a/lib/MqttLogger/src/MqttLogger.cpp b/lib/MqttLogger/src/MqttLogger.cpp index 2b3c12b..4877290 100644 --- a/lib/MqttLogger/src/MqttLogger.cpp +++ b/lib/MqttLogger/src/MqttLogger.cpp @@ -7,7 +7,7 @@ MqttLogger::MqttLogger(MqttLoggerMode mode) this->setBufferSize(MQTT_MAX_PACKET_SIZE); } -MqttLogger::MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode) +MqttLogger::MqttLogger(MqttClientSetup& client, const char* topic, MqttLoggerMode mode) { this->setClient(client); this->setTopic(topic); @@ -19,7 +19,7 @@ MqttLogger::~MqttLogger() { } -void MqttLogger::setClient(MqttClient& client) +void MqttLogger::setClient(MqttClientSetup& client) { this->client = &client; } @@ -76,9 +76,7 @@ void MqttLogger::sendBuffer() bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial; if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->connected()) { - this->client->beginMessage(topic); - this->client->write((byte *)this->buffer, this->bufferCnt); - this->client->endMessage(); + this->client->publish(topic, 0, true, (uint8_t*)this->buffer, this->bufferCnt); } else if (this->mode == MqttLoggerMode::MqttAndSerialFallback) { doSerial = true; diff --git a/lib/MqttLogger/src/MqttLogger.h b/lib/MqttLogger/src/MqttLogger.h index 9a570e4..a85e084 100644 --- a/lib/MqttLogger/src/MqttLogger.h +++ b/lib/MqttLogger/src/MqttLogger.h @@ -12,6 +12,7 @@ #include #include #include "MqttClient.h" +#include "MqttClientSetup.h" #define MQTT_MAX_PACKET_SIZE 1024 @@ -29,16 +30,16 @@ private: uint8_t* buffer; uint8_t* bufferEnd; uint16_t bufferCnt = 0, bufferSize = 0; - MqttClient* client; + MqttClientSetup* client; MqttLoggerMode mode; void sendBuffer(); public: MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); - MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); + MqttLogger(MqttClientSetup& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); ~MqttLogger(); - void setClient(MqttClient& client); + void setClient(MqttClientSetup& client); void setTopic(const char* topic); void setMode(MqttLoggerMode mode); void setRetained(boolean retained); diff --git a/lib/espMqttClient/LICENSE b/lib/espMqttClient/LICENSE new file mode 100644 index 0000000..1cc5546 --- /dev/null +++ b/lib/espMqttClient/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Bert Melis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/espMqttClient/README.md b/lib/espMqttClient/README.md new file mode 100644 index 0000000..586d333 --- /dev/null +++ b/lib/espMqttClient/README.md @@ -0,0 +1,54 @@ +# espMqttClient + +MQTT client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework. +Aims to be a non-blocking, fully compliant MQTT 3.1.1 client. + +![platformio](https://github.com/bertmelis/espMqttClient/actions/workflows/build_platformio.yml/badge.svg) +![cpplint](https://github.com/bertmelis/espMqttClient/actions/workflows/cpplint.yml/badge.svg) +![cppcheck](https://github.com/bertmelis/espMqttClient/actions/workflows/cppcheck.yml/badge.svg) +[![PlatformIO Registry](https://badges.registry.platformio.org/packages/bertmelis/library/espMqttClient.svg)](https://registry.platformio.org/libraries/bertmelis/espMqttClient) + +# Features + +- MQTT 3.1.1 compliant library +- Sending and receiving at all QoS levels +- TCP and TCP/TLS using standard WiFiClient and WiFiClientSecure connections +- Virtually unlimited incoming and outgoing payload sizes +- Readable and understandable code +- Fully async clients available via [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) or [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP) (no TLS supported) +- Supported platforms: + - Espressif ESP8266 and ESP32 using the Arduino framework +- Basic Linux compatibility*. This includes WSL on Windows + + > Linux compatibility is mainly for automatic testing. It relies on a quick and dirty Arduino-style `Client` with a POSIX TCP client underneath and Arduino-style `IPAddress` class. These are lacking many features needed for proper Linux support. + +# Documentation + +See [documentation](https://www.emelis.net/espMqttClient/) and the [examples](examples/). + +## Limitations + +### MQTT 3.1.1 Compliancy + +Outgoing messages and session data are not stored in non-volatile memory. Any events like loss of power or sudden resets result in loss of data. Despite this limitation, one could still consider this library as fully complaint based on the non normative remark in point 4.1.1 of the specification. + +### Non-blocking + +This library aims to be fully non-blocking. It is however limited by the underlying `WiFiClient` library which is part of the Arduino framework and has a blocking `connect` method. This is not an issue on ESP32 because the call is offloaded to a separate task. On ESP8266 however, connecting will block until succesful or until the connection timeouts. + +If you need a fully asynchronous MQTT client, you can use `espMqttClientAsync` which uses AsyncTCP/ESPAsyncTCP under the hood. These underlying libraries do not support TLS (anymore). I will not provide support TLS for the async client. + +# Bugs and feature requests + +Please use Github's facilities to get in touch. + +# About this library + +This client wouldn't exist without [Async-mqtt-client](https://github.com/marvinroger/async-mqtt-client). It has been my go-to MQTT client for many years. It was fast, reliable and had features that were non-existing in alternative libraries. However, the underlying async TCP libraries are lacking updates, especially updates related to secure connections. Adapting this library to use up-to-date TCP clients would not be trivial. I eventually decided to write my own MQTT library, from scratch. + +The result is an almost non-blocking library with no external dependencies. The library is almost a drop-in replacement for the async-mqtt-client except a few parameter type changes (eg. `uint8_t*` instead of `char*` for payloads). + +# License + +This library is released under the MIT Licence. A copy is included in the repo. +Parts of this library, most notably the API, are based on [Async MQTT client for ESP8266 and ESP32](https://github.com/marvinroger/async-mqtt-client). diff --git a/lib/espMqttClient/docs/_config.yml b/lib/espMqttClient/docs/_config.yml new file mode 100644 index 0000000..6975b20 --- /dev/null +++ b/lib/espMqttClient/docs/_config.yml @@ -0,0 +1,6 @@ +theme: jekyll-theme-cayman +title: espMqttClient +description: | + MQTT client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework. + Aims to be a non-blocking fully compliant MQTT 3.1.1 client. +show_downloads: false diff --git a/lib/espMqttClient/docs/index.md b/lib/espMqttClient/docs/index.md new file mode 100644 index 0000000..8b8efb9 --- /dev/null +++ b/lib/espMqttClient/docs/index.md @@ -0,0 +1,487 @@ +![platformio](https://github.com/bertmelis/espMqttClient/actions/workflows/build_platformio.yml/badge.svg) +![cpplint](https://github.com/bertmelis/espMqttClient/actions/workflows/cpplint.yml/badge.svg) +![cppcheck](https://github.com/bertmelis/espMqttClient/actions/workflows/cppcheck.yml/badge.svg) +[![PlatformIO Registry](https://badges.registry.platformio.org/packages/bertmelis/library/espMqttClient.svg)](https://registry.platformio.org/libraries/bertmelis/espMqttClient) + +# Features + +- MQTT 3.1.1 compliant library +- Sending and receiving at all QoS levels +- TCP and TCP/TLS using standard WiFiClient and WiFiClientSecure connections +- Virtually unlimited incoming and outgoing payload sizes +- Readable and understandable code +- Fully async clients available via [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) or [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP) (no TLS supported) +- Supported platforms: + - Espressif ESP8266 and ESP32 using the Arduino framework +- Basic Linux compatibility*. This includes WSL on Windows + + > Linux compatibility is mainly for automatic testing. It relies on a quick and dirty Arduino-style `Client` with a POSIX TCP client underneath and Arduino-style `IPAddress` class. These are lacking many features needed for proper Linux support. + +# Contents + +1. [Runtime behaviour](#runtime-behaviour) +2. [API Reference](#api-reference) +3. [Compile-time configuration](#compile-time-configuration) +4. [Code samples](#code-samples) + +# Runtime behaviour + +A normal operation cycle of an MQTT client goes like this: + +1. setup the client +2. connect to the broker +3. subscribe/publish/receive +4. disconnect/reconnect when disconnected +5. Cleanly disconnect + +### Setup + +Setting up the client means to tell which host and port to connect to, possible credentials to use and so on. espMqttClient has a set of methods to configure the client. Setup is generally done in the `setup()` function of the Arduino framework. +One important thing to remember is that there are a number of settings that are not stored inside the library: `username`, `password`, `willTopic`, `willPayload`, `clientId` and `host`. Make sure these variables stay available during the lifetime of the `espMqttClient`. + +For TLS secured connections, the relevant methods from `WiFiClientSecure` have been made available to setup the TLS mechanisms. + +### Connecting + +After setting up the client, you are ready to connect. A simple call to `connect()` does the job. If you set an `OnConnectCallback`, you will be notified when the connection has been made. On failure, `OnDisconnectCallback` will be called. Although good code structure can avoid this, you can call `connect()` multiple times. + +### Subscribing, publishing and receiving + +Once connected, you can subscribe, publish and receive. The methods to do this return the packetId of the generated packet or `1` for packets without packetId. In case of an error, the method returns `0`. When the client is not connected, you cannot subscribe, unsubscribe or publish (configurable, see [EMC_ALLOW_NOT_CONNECTED_PUBLISH](#EMC_ALLOW_NOT_CONNECTED_PUBLISH)). + +Receiving packets is done via the `onMessage`-callback. This callback gives you the topic, properties (qos, dup, retain, packetId) and payload. For the payload, you get a pointer to the data, the index, length and total length. On long payloads it is normal that you get multiple callbacks for the same packet. This way, you can receive payloads longer than what could fit in the microcontroller's memory. + + > Beware that MQTT payloads are binary. MQTT payloads are **not** c-strings unless explicitely constructed like that. You therefore can **not** print the payload to your Serial monitor without supporting code. + +### Disconnecting + +You can disconnect from the broker by calling `disconnect()`. If you do not force-disconnect, the client will first send the remaining messages that are in the queue and disconnect afterwards. During this period however, no new incoming PUBLISH messages will be processed. + +# API Reference + +```cpp +espMqttClient() +espMqttClientSecure() +espMqttClientAsync() +``` + +Instantiate a new espMqttClient or espMqttSecure object. +On ESP32, two optional parameters are available: `espMqttClient(uint8_t priority = 1, uint8_t core = 1)`. This will change the priority of the MQTT client task and the core on which it runs (higher priority = more cpu-time). + +For the asynchronous version, use `espMqttClientAsync`. + +### Configuration + +```cpp +espMqttClient& setKeepAlive(uint16_t keepAlive) +``` + +Set the keep alive. Defaults to 15 seconds. + +* **`keepAlive`**: Keep alive in seconds + +```cpp +espMqttClient& setClientId(const char* clientId) +``` + +Set the client ID. Defaults to `esp8266123456` or `esp32123456` where `123456` is the chip ID. +The library only stores a pointer to the client ID. Make sure the variable pointed to stays available throughout the lifetime of espMqttClient. + +- **`clientId`**: Client ID, expects a null-terminated char array (c-string) + +```cpp +espMqttClient& setCleanSession(bool cleanSession) +``` + +Set the CleanSession flag. Defaults to `true`. + +- **`cleanSession`**: clean session wanted or not + +```cpp +espMqttClient& setCredentials(const char* username, const char* password) +``` + +Set the username/password. Defaults to non-auth. +The library only stores a pointer to the username and password. Make sure the variable to pointed stays available throughout the lifetime of espMqttClient. + +- **`username`**: Username, expects a null-terminated char array (c-string) +- **`password`**: Password, expects a null-terminated char array (c-string) + +```cpp +espMqttClient& setWill(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) +``` + +Set the Last Will. Defaults to none. +The library only stores a pointer to the topic and payload. Make sure the variable pointed to stays available throughout the lifetime of espMqttClient. + +- **`topic`**: Topic of the LWT, expects a null-terminated char array (c-string) +- **`qos`**: QoS of the LWT +- **`retain`**: Retain flag of the LWT +- **`payload`**: Payload of the LWT. +- **`length`**: Payload length + +```cpp +espMqttClient& setWill(const char* topic, uint8_t qos, bool retain, const char* payload) +``` + +Set the Last Will. Defaults to none. +The library only stores a pointer to the topic and payload. Make sure the variable pointed to stays available throughout the lifetime of espMqttClient. + +- **`topic`**: Topic of the LWT, expects a null-terminated char array (c-string) +- **`qos`**: QoS of the LWT +- **`retain`**: Retain flag of the LWT +- **`payload`**: Payload of the LWT, expects a null-terminated char array (c-string). Its lenght will be calculated using `strlen(payload)` + +```cpp +espMqttClient& setServer(IPAddress ip, uint16_t port) +``` + +Set the server. Mind that when using `espMqttClientSecure` with a certificate, the hostname will be chacked against the certificate. Often IP-addresses are not valid and the connection will fail. + +- **`ip`**: IP of the server +- **`port`**: Port of the server + +```cpp +espMqttClient& setServer(const char* host, uint16_t port) +``` + +Set the server. + +- **`host`**: Host of the server, expects a null-terminated char array (c-string) +- **`port`**: Port of the server + +#### Options for TLS connections + +All common options from WiFiClientSecure to setup an encrypted connection are made available. These include: + +- `espMqttClientSecure& setInsecure()` +- `espMqttClientSecure& setCACert(const char* rootCA)` (ESP32 only) +- `espMqttClientSecure& setCertificate(const char* clientCa)` (ESP32 only) +- `espMqttClientSecure& setPrivateKey(const char* privateKey)` (ESP32 only) +- `espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey)` (ESP32 only) +- `espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20])` (ESP8266 only) +- `espMqttClientSecure& setTrustAnchors(const X509List *ta)` (ESP8266 only) +- `espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk)` (ESP8266 only) +- `espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type)` (ESP8266 only) +- `espMqttClientSecure& setCertStore(CertStoreBase *certStore)` (ESP8266 only) + +For documenation, please visit [ESP8266's documentation](https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html#bearssl-client-secure-and-server-secure) or [ESP32's documentation](https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFiClientSecure). + +### Events handlers + +```cpp +espMqttClient& onConnect(espMqttClientTypes::OnConnectCallback callback) +``` + +Add a connect event handler. Function signature: `void(bool sessionPresent)` + +- **`callback`**: Function to call + +```cpp +espMqttClient& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) +``` + +Add a disconnect event handler. Function signature: `void(espMqttClientTypes::DisconnectReason reason)` + +- **`callback`**: Function to call + +```cpp +espMqttClient& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) +``` + +Add a subscribe acknowledged event handler. Function signature: `void(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* returncodes, size_t len)` + +- **`callback`**: Function to call + +```cpp +espMqttClient& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) +``` + +Add an unsubscribe acknowledged event handler. Function signature: `void(uint16_t packetId)` + +- **`callback`**: Function to call + +```cpp +espMqttClient& onMessage(espMqttClientTypes::OnMessageCallback callback) +``` + +Add a publish received event handler. Function signature: `void(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total)` + +- **`callback`**: Function to call + +```cpp +espMqttClient& onPublish(espMqttClientTypes::OnPublishCallback callback) +``` + +Add a publish acknowledged event handler. Function signature: `void(uint16_t packetId)` + +- **`callback`**: Function to call + +### Operational functions + +```cpp +bool connected() +``` + +Returns if the client is currently fully connected to the broker or not. During connecting or disconnecting, it will return false. + +```cpp +bool disconnected() +``` + +Returns if the client is currently disconnected to the broker or not. During disconnecting or connecting, it will return false. + +```cpp +void connect() +``` + +Connect to the server. + +```cpp +void disconnect(bool force = false) +``` + +Disconnect from the server. +When disconnecting with `force` false, the client first tries to handle all the outgoing messages in the queue and disconnect cleanly afterwards. During this time, no incoming PUBLISH messages are handled. + +- **`force`**: Whether to force the disconnection. Defaults to `false` (clean disconnection). + +```cpp +uint16_t subscribe(const char* topic, uint8_t qos) +``` + +Subscribe to the given topic at the given QoS. Return the packet ID or 0 if failed. + +- **`topic`**: Topic, expects a null-terminated char array (c-string) +- **`qos`**: QoS + +It is also possible to subscribe to multiple topics at once. Just add the topic/qos pairs to the parameters: + +```cpp +uint16_t packetId = yourclient.subscribe(topic1, qos1, topic2, qos2, topic3, qos3); // add as many topics as you like* +``` + +```cpp +uint16_t unsubscribe(const char* topic) +``` + +Unsubscribe from the given topic. Return the packet ID or 0 if failed. + +- **`topic`**: Topic, expects a null-terminated char array (c-string) + +It is also possible to unsubscribe to multiple topics at once. Just add the topics to the parameters: + +```cpp +uint16_t packetId = yourclient.unsubscribe(topic1, topic2, topic3); // add as many topics as you like* +``` + +```cpp +uint16_t publish(const char* topic, uint8_t qos, bool retain, const uint8* payload, size_t length) +``` + +Publish a packet. Return the packet ID (or 1 if QoS 0) or 0 if failed. The topic and payload will be buffered by the library. + +- **`topic`**: Topic, expects a null-terminated char array (c-string) +- **`qos`**: QoS +- **`retain`**: Retain flag +- **`payload`**: Payload +- **`length`**: Payload length + +```cpp +uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload) +``` + +Publish a packet. Return the packet ID (or 1 if QoS 0) or 0 if failed. The topic and payload will be buffered by the library. + +- **`topic`**: Topic, expects a null-terminated char array (c-string) +- **`qos`**: QoS +- **`retain`**: Retain flag +- **`payload`**: Payload, expects a null-terminated char array (c-string). Its lenght will be calculated using `strlen(payload)` + +```cpp +uint16_t publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length) +``` + +Publish a packet with a callback for payload handling. Return the packet ID (or 1 if QoS 0) or 0 if failed. The topic will be buffered by the library. + +- **`topic`**: Topic, expects a null-terminated char array (c-string) +- **`qos`**: QoS +- **`retain`**: Retain flag +- **`callback`**: callback to fetch the payload. + +The callback has the following signature: `size_t callback(uint8_t* data, size_t maxSize, size_t index)`. When the library needs payload data, the callback will be invoked. It is the callback's job to write data indo `data` with a maximum of `maxSize` bytes, according the `index` and return the amount of bytes written. + +```cpp +void clearQueue() +``` + +When disconnected, clears all queued messages. +Keep in mind that this also deletes any session data and therefore is no MQTT compliant. + +```cpp +void loop() +``` + +This is the worker function of the MQTT client. For ESP8266 you must call this function in the Arduino loop. For ESP32 this function is only used internally and is not available in the API. + +```cpp +const char* getClientId() const +``` + +Retuns the client ID. + +# Compile time configuration + +A number of constants which influence the behaviour of the client can be set at compile time. You can set these options in the `Config.h` file or pass the values as compiler flags. Because these options are compile-time constants, they are used for all instances of `espMqttClient` you create in your program. + +### EMC_RX_BUFFER_SIZE 1440 + +The client copies incoming data into a buffer before parsing. This sets the buffer size. + +### EMC_TX_BUFFER_SIZE 1440 + +When publishing using the callback, the client fetches data in chunks of EMC_TX_BUFFER_SIZE size. This is not necessarily the same as the actual outging TCP packets. + +### EMC_MAX_TOPIC_LENGTH 128 + +For **incoming** messages, a maximum topic length is set. Topics longer than this will be truncated. + +### EMC_PAYLOAD_BUFFER_SIZE 32 + +Set the incoming payload buffer size for SUBACK messages. When subscribing to multiple topics at once, the acknowledgement contains all the return codes in its payload. The detault of 32 means you can theoretically subscribe to 32 topics at once. + +### EMC_MIN_FREE_MEMORY 4096 + +The client keeps all outgoing packets in a queue which stores its data in heap memory. With this option, you can set the minimum available (contiguous) heap memory that needs to be available for adding a message to the queue. + +### EMC_ESP8266_MULTITHREADING 0 + +Set this to 1 if you use the async version on ESP8266. For the regular client this setting can be kept disabled because the ESP8266 doesn't use multithreading and is only single-core. + +### EMC_ALLOW_NOT_CONNECTED_PUBLISH 1 + +By default, you can publish when the client is not connected. If you don't want this, set this to 0. + +### EMC_CLIENTID_LENGTH 18 + 1 + +The (maximum) length of the client ID. (Keep in mind that this is a c-string. You need to have 1 position available for the null-termination.) + +### EMC_TASK_STACK_SIZE 5000 + +Only used on ESP32. Sets the stack size (in words) of the MQTT client worker task. + +### EMC_USE_WATCHDOG 0 + +(ESP32 only) + +**Experimental** + +You can enable a watchdog on the MQTT task. This is experimental and will probably result in resets because some (framework) function calls block without feeding the dog. + +### Logging + +If needed, you have to enable logging at compile time. This is done differently on ESP32 and ESP8266. + +ESP8266: + +- Enable logging for Arduino [see docs](https://arduino-esp8266.readthedocs.io/en/latest/Troubleshooting/debugging.html) +- Pass the `DEBUG_ESP_MQTT_CLIENT` flag to the compiler + +ESP32 + +- Enable logging for Arduino [see docs](https://docs.espressif.com/projects/arduino-esp32/en/latest/guides/tools_menu.html?#core-debug-level) + +# Code samples + +A number of examples are in the [examples](/examples) directory. These include basic operation on ESP8266 and ESP32. Please examine these to understand the basic operation of the MQTT client. + +Below are examples on specific points for working with this library. + +### Printing payloads + +MQTT 3.1.1 defines no special format for the payload so it is treated as binary. If you want to print a payload to the Arduino serial console, you have to make sure that the payload is null-terminated (c-string). + +```cpp +// option one: print the payload char by char +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received:"); + Serial.printf(" topic: %s\n payload:", topic); + const char* p = reinterpret_cast(payload); + for (size_t i = 0; i < len; ++i) { + Serial.print(p[i]); + } + Serial.print("\n"); +} +``` + +```cpp +// option two: copy the payload into a c-string +// you cannot just do payload[len] = 0 because you do not own this memory location! +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received:"); + Serial.printf(" topic: %s\n payload:", topic); + char* strval = new char[len + 1]; + memcpy(strval, payload, len); + strval[len] = "\0"; + Serial.println(strval); + delete[] strval; +} +``` + +### Assembling chunked messages + +The `onMessage`-callback is called as data comes in. So if the data comes in partially, the callback will be called on every receipt of a chunk, with the proper `index`, (chunk)`size` and `total` set. With little code, you can reassemble chunked messages yourself. + +```cpp +const size_t maxPayloadSize = 8192; +uint8_t* payloadbuffer = nullptr; +size_t payloadbufferSize = 0; +size_t payloadbufferIndex = 0; + +void onOversizedMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + // handle oversized messages +} + +void onCompleteMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + // handle assembled messages +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + // payload is bigger then max: return chunked + if (total > maxPayloadSize) { + onOversizedMqttMessage(properties, topic, payload, len, index, total); + return; + } + + // start new packet, increase buffer size if neccesary + if (index == 0) { + if (total > payloadbufferSize) { + delete[] payloadbuffer; + payloadbufferSize = total; + payloadbuffer = new (std::nothrow) uint8_t[payloadbufferSize]; + if (!payloadbuffer) { + // no buffer could be created. you might want to log this somewhere + return; + } + } + payloadbufferIndex = 0; + } + + // add data and dispatch when done + if (payloadBuffer) { + memcpy(&payloadbuffer[payloadbufferIndex], payload, len); + payloadbufferIndex += len; + if (payloadbufferIndex == total) { + // message is complete here + onCompleteMqttMessage(properties, topic, payloadBuffer, total, 0, total); + // optionally: + delete[] payloadBuffer; + payloadBuffer = nullptr; + payloadbufferSize = 0; + } + } +} + +// attach callback to MQTT client +mqttClient.onMessage(onMqttMessage); +``` diff --git a/lib/espMqttClient/docs/mqtt-v3.1.1.pdf b/lib/espMqttClient/docs/mqtt-v3.1.1.pdf new file mode 100644 index 0000000..e4095f1 Binary files /dev/null and b/lib/espMqttClient/docs/mqtt-v3.1.1.pdf differ diff --git a/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino b/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino new file mode 100644 index 0000000..f719080 --- /dev/null +++ b/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino @@ -0,0 +1,91 @@ +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +espMqttClient mqttClient; +Ticker reconnectTimer; + +size_t fetchPayload(uint8_t* dest, size_t len, size_t index) { + Serial.printf("filling buffer at index %zu\n", index); + // fill the buffer with random bytes + // but maybe don't fill the entire buffer + size_t i = 0; + for (; i < len; ++i) { + dest[i] = random(0xFF); + if (dest[i] > 0xFC) { + ++i; // extra increment to compensate 'break' + break; + } + } + return i; +} + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + Serial.println("Disconnected from Wi-Fi."); + reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + reconnectTimer.once(5, connectToWiFi); +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + mqttClient.publish("topic/largepayload", 1, false, fetchPayload, 6000); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + + connectToWiFi(); +} + +void loop() { + mqttClient.loop(); +} \ No newline at end of file diff --git a/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino b/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino new file mode 100644 index 0000000..07bb1aa --- /dev/null +++ b/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino @@ -0,0 +1,142 @@ +#include +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 130, 10) +#define MQTT_PORT 1883 + +#define UPDATE_TOPIC "device/firmware/set" + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +espMqttClient mqttClient; +Ticker reconnectTimer; +bool disconnectFlag = false; +bool restartFlag = false; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + Serial.println("Disconnected from Wi-Fi."); + reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + reconnectTimer.once(5, connectToWiFi); +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + uint16_t packetIdSub = mqttClient.subscribe(UPDATE_TOPIC, 2); + Serial.print("Subscribing at QoS 2, packetId: "); + Serial.println(packetIdSub); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (disconnectFlag) { + restartFlag = true; + return; + } + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + Serial.println("Subscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); + for (size_t i = 0; i < len; ++i) { + Serial.print(" qos: "); + Serial.println(static_cast(codes[i])); + } +} + +void handleUpdate(const uint8_t* payload, size_t length, size_t index, size_t total) { + // The Updater class takes a non-const pointer to write data although it doesn't change the data + uint8_t* data = const_cast(payload); + static size_t written = 0; + Update.runAsync(true); + if (index == 0) { + if (Update.isRunning()) { + Update.end(); + Update.clearError(); + } + Update.begin(total); + written = Update.write(data, length); + Serial.printf("Updating %u/%u\n", written, Update.size()); + } else { + if (!Update.isRunning()) return; + written += Update.write(data, length); + Serial.printf("Updating %u/%u\n", written, Update.size()); + } + if (Update.isFinished()) { + if (Update.end()) { + Serial.println("Update succes"); + disconnectFlag = true; + } else { + Serial.printf("Update error: %u\n", Update.getError()); + Update.printError(Serial); + Update.clearError(); + } + } +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + if (strcmp(UPDATE_TOPIC, topic) != 0) { + Serial.println("Topic mismatch"); + return; + } + handleUpdate(payload, len, index, total); +} + +void setup() { + Serial.begin(74880); + Serial.println(); + Serial.println(); + + wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + + connectToWiFi(); +} + +void loop() { + mqttClient.loop(); + + if (disconnectFlag) { + // it's safe to call this multiple times + mqttClient.disconnect(); + } + + if (restartFlag) { + Serial.println("Rebooting... See you next time!"); + Serial.flush(); + ESP.reset(); + } +} \ No newline at end of file diff --git a/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino b/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino new file mode 100644 index 0000000..581db1f --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino @@ -0,0 +1,127 @@ +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +espMqttClient mqttClient; +Ticker reconnectTimer; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case SYSTEM_EVENT_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + reconnectTimer.once(5, connectToWiFi); + break; + default: + break; + } +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + uint16_t packetIdSub = mqttClient.subscribe("foo/bar", 2); + Serial.print("Subscribing at QoS 2, packetId: "); + Serial.println(packetIdSub); + mqttClient.publish("foo/bar", 0, true, "test 1"); + Serial.println("Publishing at QoS 0"); + uint16_t packetIdPub1 = mqttClient.publish("foo/bar", 1, true, "test 2"); + Serial.print("Publishing at QoS 1, packetId: "); + Serial.println(packetIdPub1); + uint16_t packetIdPub2 = mqttClient.publish("foo/bar", 2, true, "test 3"); + Serial.print("Publishing at QoS 2, packetId: "); + Serial.println(packetIdPub2); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + Serial.println("Subscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); + for (size_t i = 0; i < len; ++i) { + Serial.print(" qos: "); + Serial.println(static_cast(codes[i])); + } +} + +void onMqttUnsubscribe(uint16_t packetId) { + Serial.println("Unsubscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received."); + Serial.print(" topic: "); + Serial.println(topic); + Serial.print(" qos: "); + Serial.println(properties.qos); + Serial.print(" dup: "); + Serial.println(properties.dup); + Serial.print(" retain: "); + Serial.println(properties.retain); + Serial.print(" len: "); + Serial.println(len); + Serial.print(" index: "); + Serial.println(index); + Serial.print(" total: "); + Serial.println(total); +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + WiFi.onEvent(WiFiEvent); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onUnsubscribe(onMqttUnsubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + + connectToWiFi(); +} + +void loop() { + // nothing to do here +} diff --git a/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino b/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino new file mode 100644 index 0000000..0747ec6 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino @@ -0,0 +1,123 @@ +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +espMqttClient mqttClient; +Ticker reconnectTimer; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + Serial.println("Disconnected from Wi-Fi."); + reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + reconnectTimer.once(5, connectToWiFi); +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2); + Serial.print("Subscribing at QoS 2, packetId: "); + Serial.println(packetIdSub); + mqttClient.publish("test/lol", 0, true, "test 1"); + Serial.println("Publishing at QoS 0"); + uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2"); + Serial.print("Publishing at QoS 1, packetId: "); + Serial.println(packetIdPub1); + uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3"); + Serial.print("Publishing at QoS 2, packetId: "); + Serial.println(packetIdPub2); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + Serial.println("Subscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); + for (size_t i = 0; i < len; ++i) { + Serial.print(" qos: "); + Serial.println(static_cast(codes[i])); + } +} + +void onMqttUnsubscribe(uint16_t packetId) { + Serial.println("Unsubscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received."); + Serial.print(" topic: "); + Serial.println(topic); + Serial.print(" qos: "); + Serial.println(properties.qos); + Serial.print(" dup: "); + Serial.println(properties.dup); + Serial.print(" retain: "); + Serial.println(properties.retain); + Serial.print(" len: "); + Serial.println(len); + Serial.print(" index: "); + Serial.println(index); + Serial.print(" total: "); + Serial.println(total); +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onUnsubscribe(onMqttUnsubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + + connectToWiFi(); +} + +void loop() { + mqttClient.loop(); +} \ No newline at end of file diff --git a/lib/espMqttClient/examples/simple-linux/main.cpp b/lib/espMqttClient/examples/simple-linux/main.cpp new file mode 100644 index 0000000..54daa86 --- /dev/null +++ b/lib/espMqttClient/examples/simple-linux/main.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +#define MQTT_HOST IPAddress(192,168,1,10) +#define MQTT_PORT 1883 + +espMqttClient mqttClient; +std::atomic_bool exitProgram(false); + +void connectToMqtt() { + std::cout << "Connecting to MQTT..." << std::endl; + mqttClient.connect(); +} + +void onMqttConnect(bool sessionPresent) { + std::cout << "Connected to MQTT." << std::endl; + std::cout << "Session present: " << sessionPresent << std::endl; + uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2); + std::cout << "Subscribing at QoS 2, packetId: " << packetIdSub << std::endl; + mqttClient.publish("test/lol", 0, true, "test 1"); + std::cout << "Publishing at QoS 0" << std::endl; + uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2"); + std::cout << "Publishing at QoS 1, packetId: " << packetIdPub1 << std::endl; + uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3"); + std::cout << "Publishing at QoS 2, packetId: " << packetIdPub2 << std::endl; +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + std::cout << "Disconnected from MQTT: %u.\n" << unsigned(static_cast(reason)) << std::endl; + exitProgram = true; +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + std::cout << "Subscribe acknowledged." << std::endl; + std::cout << " packetId: " << packetId << std::endl; + for (size_t i = 0; i < len; ++i) { + std::cout << " qos: " << unsigned(static_cast(codes[i])) << std::endl; + } +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + (void) payload; + std::cout << "Publish received." << std::endl; + std::cout << " topic: " << topic << std::endl; + std::cout << " qos: " << unsigned(properties.qos) << std::endl; + std::cout << " dup: " << properties.dup << std::endl; + std::cout << " retain: " << properties.retain << std::endl; + std::cout << " len: " << len << std::endl; + std::cout << " index: " << index << std::endl; + std::cout << " total: " << total << std::endl; +} + +void onMqttPublish(uint16_t packetId) { + std::cout << "Publish acknowledged." << std::endl; + std::cout << " packetId: " << packetId << std::endl; +} + +void ClientLoop(void* arg) { + (void) arg; + for(;;) { + mqttClient.loop(); // includes a yield + if (exitProgram) break; + } +} + +int main() { + std::cout << "Setting up sample MQTT client" << std::endl; + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + + std::cout << "Starting sample MQTT client" << std::endl; + std::thread t = std::thread(ClientLoop, nullptr); + + connectToMqtt(); + + while(1) { + if (exitProgram) break; + std::this_thread::yield(); + } + + t.join(); + return EXIT_SUCCESS; +} diff --git a/lib/espMqttClient/examples/simple-linux/platformio.ini b/lib/espMqttClient/examples/simple-linux/platformio.ini new file mode 100644 index 0000000..54a9443 --- /dev/null +++ b/lib/espMqttClient/examples/simple-linux/platformio.ini @@ -0,0 +1,28 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +;[platformio] +;default_envs = esp8266 + +[common] +build_flags = + -D DEBUG_ESP_MQTT_CLIENT=1 + -std=c++11 + -pthread + -Wall + -Wextra + +[env:native] +platform = native +build_flags = + ${common.build_flags} + -D EMC_RX_BUFFER_SIZE=1500 +build_type = debug +lib_compat_mode = off diff --git a/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino b/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino new file mode 100644 index 0000000..c4d9aa0 --- /dev/null +++ b/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino @@ -0,0 +1,127 @@ +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +espMqttClientAsync mqttClient; +Ticker reconnectTimer; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case SYSTEM_EVENT_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + reconnectTimer.once(5, connectToWiFi); + break; + default: + break; + } +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + uint16_t packetIdSub = mqttClient.subscribe("foo/bar", 2); + Serial.print("Subscribing at QoS 2, packetId: "); + Serial.println(packetIdSub); + mqttClient.publish("foo/bar", 0, true, "test 1"); + Serial.println("Publishing at QoS 0"); + uint16_t packetIdPub1 = mqttClient.publish("foo/bar", 1, true, "test 2"); + Serial.print("Publishing at QoS 1, packetId: "); + Serial.println(packetIdPub1); + uint16_t packetIdPub2 = mqttClient.publish("foo/bar", 2, true, "test 3"); + Serial.print("Publishing at QoS 2, packetId: "); + Serial.println(packetIdPub2); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + Serial.println("Subscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); + for (size_t i = 0; i < len; ++i) { + Serial.print(" qos: "); + Serial.println(static_cast(codes[i])); + } +} + +void onMqttUnsubscribe(uint16_t packetId) { + Serial.println("Unsubscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received."); + Serial.print(" topic: "); + Serial.println(topic); + Serial.print(" qos: "); + Serial.println(properties.qos); + Serial.print(" dup: "); + Serial.println(properties.dup); + Serial.print(" retain: "); + Serial.println(properties.retain); + Serial.print(" len: "); + Serial.println(len); + Serial.print(" index: "); + Serial.println(index); + Serial.print(" total: "); + Serial.println(total); +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + WiFi.onEvent(WiFiEvent); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onUnsubscribe(onMqttUnsubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + + connectToWiFi(); +} + +void loop() { + // nothing to do here +} diff --git a/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino b/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino new file mode 100644 index 0000000..2ed6b53 --- /dev/null +++ b/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino @@ -0,0 +1,123 @@ +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +espMqttClientAsync mqttClient; +Ticker reconnectTimer; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + Serial.println("Disconnected from Wi-Fi."); + reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + reconnectTimer.once(5, connectToWiFi); +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2); + Serial.print("Subscribing at QoS 2, packetId: "); + Serial.println(packetIdSub); + mqttClient.publish("test/lol", 0, true, "test 1"); + Serial.println("Publishing at QoS 0"); + uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2"); + Serial.print("Publishing at QoS 1, packetId: "); + Serial.println(packetIdPub1); + uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3"); + Serial.print("Publishing at QoS 2, packetId: "); + Serial.println(packetIdPub2); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + Serial.println("Subscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); + for (size_t i = 0; i < len; ++i) { + Serial.print(" qos: "); + Serial.println(static_cast(codes[i])); + } +} + +void onMqttUnsubscribe(uint16_t packetId) { + Serial.println("Unsubscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received."); + Serial.print(" topic: "); + Serial.println(topic); + Serial.print(" qos: "); + Serial.println(properties.qos); + Serial.print(" dup: "); + Serial.println(properties.dup); + Serial.print(" retain: "); + Serial.println(properties.retain); + Serial.print(" len: "); + Serial.println(len); + Serial.print(" index: "); + Serial.println(index); + Serial.print(" total: "); + Serial.println(total); +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onUnsubscribe(onMqttUnsubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + + connectToWiFi(); +} + +void loop() { + // nothing to do here +} \ No newline at end of file diff --git a/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino b/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino new file mode 100644 index 0000000..73a6db4 --- /dev/null +++ b/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino @@ -0,0 +1,145 @@ +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST "mqtt.yourhost.com" +#define MQTT_PORT 8883 +#define MQTT_USER "username" +#define MQTT_PASS "password" + +const char rootCA[] = \ + "-----BEGIN CERTIFICATE-----\n" \ + " add your certificate here \n" \ + "-----END CERTIFICATE-----\n"; + +espMqttClientSecure mqttClient; +Ticker reconnectTimer; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case SYSTEM_EVENT_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + reconnectTimer.once(5, connectToWiFi); + break; + default: + break; + } +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + + uint16_t packetIdSub0 = mqttClient.subscribe("foo/bar/0", 0); + Serial.print("Subscribing at QoS 0, packetId: "); + Serial.println(packetIdSub0); + + uint16_t packetIdPub0 = mqttClient.publish("foo/bar/0", 0, false, "test"); + Serial.println("Publishing at QoS 0, packetId: "); + Serial.println(packetIdPub0); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + Serial.println("Subscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); + for (size_t i = 0; i < len; ++i) { + Serial.print(" qos: "); + Serial.println(static_cast(codes[i])); + } +} + +void onMqttUnsubscribe(uint16_t packetId) { + Serial.println("Unsubscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received."); + Serial.print(" topic: "); + Serial.println(topic); + Serial.print(" qos: "); + Serial.println(properties.qos); + Serial.print(" dup: "); + Serial.println(properties.dup); + Serial.print(" retain: "); + Serial.println(properties.retain); + Serial.print(" len: "); + Serial.println(len); + Serial.print(" index: "); + Serial.println(index); + Serial.print(" total: "); + Serial.println(total); +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + WiFi.onEvent(WiFiEvent); + + //mqttClient.setInsecure(); + mqttClient.setCACert(rootCA); + mqttClient.setCredentials(MQTT_USER, MQTT_PASS); + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onUnsubscribe(onMqttUnsubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + mqttClient.setCleanSession(true); + + connectToWiFi(); +} + +void loop() { + static uint32_t lastMillis = 0; + if (millis() - lastMillis > 5000) { + lastMillis = millis(); + Serial.printf("heap: %u\n", ESP.getFreeHeap()); + } + + static uint32_t millisDisconnect = 0; + if (millis() - millisDisconnect > 60000) { + millisDisconnect = millis(); + mqttClient.disconnect(); + } +} diff --git a/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino b/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino new file mode 100644 index 0000000..b5b6567 --- /dev/null +++ b/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino @@ -0,0 +1,127 @@ +#include +#include +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST "test.mosquitto.org" +#define MQTT_PORT 1883 + +// test.mosquitto.org +const uint8_t fingerprint[] = {0xee, 0xbc, 0x4b, 0xf8, 0x57, 0xe3, 0xd3, 0xe4, 0x07, 0x54, 0x23, 0x1e, 0xf0, 0xc8, 0xa1, 0x56, 0xe0, 0xd3, 0x1a, 0x1c}; + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +espMqttClientSecure mqttClient; +Ticker reconnectTimer; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + Serial.println("Disconnected from Wi-Fi."); + reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + reconnectTimer.once(5, connectToWiFi); +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2); + Serial.print("Subscribing at QoS 2, packetId: "); + Serial.println(packetIdSub); + mqttClient.publish("test/lol", 0, true, "test 1"); + Serial.println("Publishing at QoS 0"); + uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2"); + Serial.print("Publishing at QoS 1, packetId: "); + Serial.println(packetIdPub1); + uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3"); + Serial.print("Publishing at QoS 2, packetId: "); + Serial.println(packetIdPub2); +} + +void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { + Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); + + if (WiFi.isConnected()) { + reconnectTimer.once(5, connectToMqtt); + } +} + +void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { + Serial.println("Subscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); + for (size_t i = 0; i < len; ++i) { + Serial.print(" qos: "); + Serial.println(static_cast(codes[i])); + } +} + +void onMqttUnsubscribe(uint16_t packetId) { + Serial.println("Unsubscribe acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + Serial.println("Publish received."); + Serial.print(" topic: "); + Serial.println(topic); + Serial.print(" qos: "); + Serial.println(properties.qos); + Serial.print(" dup: "); + Serial.println(properties.dup); + Serial.print(" retain: "); + Serial.println(properties.retain); + Serial.print(" len: "); + Serial.println(len); + Serial.print(" index: "); + Serial.println(index); + Serial.print(" total: "); + Serial.println(total); +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onUnsubscribe(onMqttUnsubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + mqttClient.setFingerprint(fingerprint); + + connectToWiFi(); +} + +void loop() { + mqttClient.loop(); +} \ No newline at end of file diff --git a/lib/espMqttClient/keywords.txt b/lib/espMqttClient/keywords.txt new file mode 100644 index 0000000..136da0a --- /dev/null +++ b/lib/espMqttClient/keywords.txt @@ -0,0 +1,60 @@ +# Datatypes (KEYWORD1) +espMqttClient KEYWORD1 +espMqttClientSecure KEYWORD1 + +OnConnectCallback KEYWORD1 +OnDisconnectCallback KEYWORD1 +OnSubscribeCallback KEYWORD1 +OnUnsubscribeCallback KEYWORD1 +OnMessageCallback KEYWORD1 +OnPublishCallback KEYWORD1 + +# Methods and Functions (KEYWORD2) +setKeepAlive KEYWORD2 +setClientId KEYWORD2 +setCleanSession KEYWORD2 +setCredentials KEYWORD2 +setWill KEYWORD2 +setServer KEYWORD2 + +setInsecure KEYWORD2 +setCACert KEYWORD2 +setCertificate KEYWORD2 +setPrivateKey KEYWORD2 +setPreSharedKey KEYWORD2 +setFingerprint KEYWORD2 +setTrustAnchors KEYWORD2 +setClientRSACert KEYWORD2 +setClientECCert KEYWORD2 +setCertStore KEYWORD2 + +onConnect KEYWORD2 +onDisconnect KEYWORD2 +onSubscribe KEYWORD2 +onUnsubscribe KEYWORD2 +onMessage KEYWORD2 +onPublish KEYWORD2 + +connected KEYWORD2 +connect KEYWORD2 +disconnect KEYWORD2 +subscribe KEYWORD2 +unsubscribe KEYWORD2 +publish KEYWORD2 +clearQueue KEYWORD2 +loop KEYWORD2 +getClientId KEYWORD2 + +# Structures (KEYWORD3) +espMqttClientTypes KEYWORD3 +MessageProperties KEYWORD3 +DisconnectReason KEYWORD3 + +# Constants (LITERAL1) +TCP_DISCONNECTED LITERAL1 +MQTT_UNACCEPTABLE_PROTOCOL_VERSION LITERAL1 +MQTT_IDENTIFIER_REJECTED LITERAL1 +MQTT_SERVER_UNAVAILABLE LITERAL1 +MQTT_MALFORMED_CREDENTIALS LITERAL1 +MQTT_NOT_AUTHORIZED LITERAL1 +TLS_BAD_FINGERPRINT LITERAL1 diff --git a/lib/espMqttClient/library.json b/lib/espMqttClient/library.json new file mode 100644 index 0000000..24ffbbb --- /dev/null +++ b/lib/espMqttClient/library.json @@ -0,0 +1,37 @@ +{ + "name": "espMqttClient", + "keywords": "iot, home, automation, mqtt, client, esp8266, esp32", + "description": "an MQTT client for the Arduino framework for ESP8266 / ESP32", + "authors": + { + "name": "Bert Melis", + "url": "https://github.com/bertmelis" + }, + "license": "MIT", + "homepage": "https://github.com/bertmelis/espMqttClient", + "repository": + { + "type": "git", + "url": "https://github.com/bertmelis/espMqttClient.git" + }, + "version": "1.3.3", + "frameworks": "arduino", + "platforms": ["espressif8266", "espressif32"], + "headers": ["espMqttClient.h", "espMqttClientAsync.h"], + "dependencies": [ + { + "name": "ESPAsyncTCP", + "version": ">=1.2.2", + "platforms": "espressif8266" + }, + { + "name": "AsyncTCP", + "version": ">=1.1.1", + "platforms": "espressif32" + } + ], + "build": + { + "libLDFMode": "deep+" + } +} \ No newline at end of file diff --git a/lib/espMqttClient/library.properties b/lib/espMqttClient/library.properties new file mode 100644 index 0000000..ea14fcf --- /dev/null +++ b/lib/espMqttClient/library.properties @@ -0,0 +1,9 @@ +name=espMqttClient +version=1.3.3 +author=Bert Melis +maintainer=Bert Melis +sentence=an MQTT client for the Arduino framework for ESP8266 / ESP32 +paragraph= +category=Communication +url=https://github.com/bertmelis/espMqttClient +architectures=esp8266,esp32 \ No newline at end of file diff --git a/lib/espMqttClient/platformio.ini b/lib/espMqttClient/platformio.ini new file mode 100644 index 0000000..43e3953 --- /dev/null +++ b/lib/espMqttClient/platformio.ini @@ -0,0 +1,33 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +;[platformio] +;default_envs = esp8266 + +[common] +build_flags = + -D DEBUG_ESP_MQTT_CLIENT=1 + -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE + -Wall + -Wextra + -std=c++11 + -pthread + +[env:native] +platform = native +test_build_src = yes +build_flags = + ${common.build_flags} + -lgcov + --coverage + -D EMC_RX_BUFFER_SIZE=100 + -D EMC_TX_BUFFER_SIZE=10 +;extra_scripts = test-coverage.py +build_type = debug diff --git a/lib/espMqttClient/scripts/CI/build_examples_pio.sh b/lib/espMqttClient/scripts/CI/build_examples_pio.sh new file mode 100644 index 0000000..4ef860c --- /dev/null +++ b/lib/espMqttClient/scripts/CI/build_examples_pio.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# already done by workflow +#pip install -U platformio +#platformio update +#pio pkg install --global --library me-no-dev/AsyncTCP +#pio pkg install --global --library EspAsyncTCP + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +NC='\033[0m' + +lines=$(find ./examples/ -maxdepth 1 -mindepth 1 -type d) +retval=0 +retvalpart=0 +while read line; do + if [[ "$line" != *esp8266 && "$line" != *esp32 && "$line" != *linux ]]; then + echo -e "========================== BUILDING $line ==========================" + echo -e "${YELLOW}SKIPPING${NC}" + continue + fi + echo -e "========================== BUILDING $line ==========================" + if [[ -e "$line/platformio.ini" ]]; then + output=$(platformio ci --lib="." --project-conf="$line/platformio.ini" $line 2>&1) + retvalpart=$? + else + if [[ "$line" == *esp8266 ]]; then + output=$(platformio ci --lib="." --project-conf="scripts/CI/platformio_esp8266.ini" $line 2>&1) + retvalpart=$? + else + output=$(platformio ci --lib="." --project-conf="scripts/CI/platformio_esp32.ini" $line 2>&1) + retvalpart=$? + fi + : + fi + if [ $retvalpart -ne 0 ]; then + echo "$output" + echo -e "Building $line ${RED}FAILED${NC}" + retval=1 + else + echo -e "${GREEN}SUCCESS${NC}" + fi +done <<< "$lines" + +# will be deleted together with container +#pio pkg uninstall --global --library me-no-dev/AsyncTCP +#pio pkg uninstall --global --library EspAsyncTCP + +exit "$retval" diff --git a/lib/espMqttClient/scripts/CI/platformio_esp32.ini b/lib/espMqttClient/scripts/CI/platformio_esp32.ini new file mode 100644 index 0000000..29f9ab1 --- /dev/null +++ b/lib/espMqttClient/scripts/CI/platformio_esp32.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32] +platform = espressif32 +board = lolin32 +framework = arduino +build_flags = + ;-Werror + -Wall + -Wextra diff --git a/lib/espMqttClient/scripts/CI/platformio_esp8266.ini b/lib/espMqttClient/scripts/CI/platformio_esp8266.ini new file mode 100644 index 0000000..919df73 --- /dev/null +++ b/lib/espMqttClient/scripts/CI/platformio_esp8266.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp8266] +platform = espressif8266 +board = d1_mini +framework = arduino +build_flags = + ;-Werror + -Wall + -Wextra diff --git a/lib/espMqttClient/scripts/get-fingerprint.py b/lib/espMqttClient/scripts/get-fingerprint.py new file mode 100644 index 0000000..22c078b --- /dev/null +++ b/lib/espMqttClient/scripts/get-fingerprint.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +# https://github.com/marvinroger/async-mqtt-client/blob/develop/scripts/get-fingerprint/get-fingerprint.py + +import argparse +import ssl +import hashlib + +parser = argparse.ArgumentParser(description='Compute SSL/TLS fingerprints.') +parser.add_argument('--host', required=True) +parser.add_argument('--port', default=8883) + +args = parser.parse_args() +print(args.host) + +cert_pem = ssl.get_server_certificate((args.host, args.port)) +cert_der = ssl.PEM_cert_to_DER_cert(cert_pem) + +md5 = hashlib.md5(cert_der).hexdigest() +sha1 = hashlib.sha1(cert_der).hexdigest() +sha256 = hashlib.sha256(cert_der).hexdigest() +print("MD5: " + md5) +print("SHA1: " + sha1) +print("SHA256: " + sha256) + +print("\nSHA1 as array initializer:") +print("const uint8_t fingerprint[] = {0x" + ", 0x".join([sha1[i:i+2] for i in range(0, len(sha1), 2)]) + "};") + +print("\nSHA1 as function call:") +print("mqttClient.addServerFingerprint((const uint8_t[]){0x" + ", 0x".join([sha1[i:i+2] for i in range(0, len(sha1), 2)]) + "});") \ No newline at end of file diff --git a/lib/espMqttClient/src/Config.h b/lib/espMqttClient/src/Config.h new file mode 100644 index 0000000..540bb2f --- /dev/null +++ b/lib/espMqttClient/src/Config.h @@ -0,0 +1,54 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#ifndef EMC_TX_TIMEOUT +#define EMC_TX_TIMEOUT 5000 +#endif + +#ifndef EMC_RX_BUFFER_SIZE +#define EMC_RX_BUFFER_SIZE 1440 +#endif + +#ifndef EMC_TX_BUFFER_SIZE +#define EMC_TX_BUFFER_SIZE 1440 +#endif + +#ifndef EMC_MAX_TOPIC_LENGTH +#define EMC_MAX_TOPIC_LENGTH 128 +#endif + +#ifndef EMC_PAYLOAD_BUFFER_SIZE +#define EMC_PAYLOAD_BUFFER_SIZE 32 +#endif + +#ifndef EMC_MIN_FREE_MEMORY +#define EMC_MIN_FREE_MEMORY 4096 +#endif + +#ifndef EMC_ESP8266_MULTITHREADING +#define EMC_ESP8266_MULTITHREADING 0 +#endif + +#ifndef EMC_ALLOW_NOT_CONNECTED_PUBLISH +#define EMC_ALLOW_NOT_CONNECTED_PUBLISH 1 +#endif + +#ifndef EMC_CLIENTID_LENGTH +// esp8266abc123 and esp32abcdef123456 +#define EMC_CLIENTID_LENGTH 23 + 1 +#endif + +#ifndef EMC_TASK_STACK_SIZE +#define EMC_TASK_STACK_SIZE 5000 +#endif + +#ifndef EMC_USE_WATCHDOG +#define EMC_USE_WATCHDOG 0 +#endif diff --git a/lib/espMqttClient/src/Helpers.h b/lib/espMqttClient/src/Helpers.h new file mode 100644 index 0000000..4a19224 --- /dev/null +++ b/lib/espMqttClient/src/Helpers.h @@ -0,0 +1,49 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP32) + #include // millis(), ESP.getFreeHeap(); + #include "freertos/FreeRTOS.h" + #include "freertos/task.h" + #include "esp_task_wdt.h" + #define EMC_SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY) + #define EMC_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore) + #define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram()) + #define EMC_YIELD() taskYIELD() + #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp32%06llx", ESP.getEfuseMac()); +#elif defined(ARDUINO_ARCH_ESP8266) + #include // millis(), ESP.getFreeHeap(); + #if EMC_ESP8266_MULTITHREADING + // This lib doesn't run use multithreading on ESP8266 + // _xSemaphore defined as std::atomic + #define EMC_SEMAPHORE_TAKE() while (_xSemaphore) { /*ESP.wdtFeed();*/ } _xSemaphore = true + #define EMC_SEMAPHORE_GIVE() _xSemaphore = false + #else + #define EMC_SEMAPHORE_TAKE() + #define EMC_SEMAPHORE_GIVE() + #endif + #define EMC_GET_FREE_MEMORY() ESP.getMaxFreeBlockSize() + // no need to yield for ESP8266, the Arduino framework does this internally + // yielding in async is forbidden (will crash) + #define EMC_YIELD() + #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp8266%06x", ESP.getChipId()); +#elif defined(__linux__) + #include // NOLINT [build/c++11] + #include // NOLINT [build/c++11] for yield() + #define millis() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() + #define EMC_GET_FREE_MEMORY() 1000000000 + #define EMC_YIELD() std::this_thread::yield() + #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "Client%04d%04d%04d", rand()%10000, rand()%10000, rand()%10000) + #include // NOLINT [build/c++11] + #define EMC_SEMAPHORE_TAKE() mtx.lock(); + #define EMC_SEMAPHORE_GIVE() mtx.unlock(); +#else + #error Target platform not supported +#endif diff --git a/lib/espMqttClient/src/Logging.h b/lib/espMqttClient/src/Logging.h new file mode 100644 index 0000000..d2e7ea4 --- /dev/null +++ b/lib/espMqttClient/src/Logging.h @@ -0,0 +1,34 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP32) + // Logging is en/disabled by Arduino framework macros + #include + #define emc_log_i(...) log_i(__VA_ARGS__) + #define emc_log_e(...) log_e(__VA_ARGS__) + #define emc_log_w(...) log_w(__VA_ARGS__) +#elif defined(ARDUINO_ARCH_ESP8266) + #if defined(DEBUG_ESP_PORT) && defined(DEBUG_ESP_MQTT_CLIENT) + #include + #define emc_log_i(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n") + #define emc_log_e(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n") + #define emc_log_w(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n") + #else + #define emc_log_i(...) + #define emc_log_e(...) + #define emc_log_w(...) + #endif +#else + // when building for PC, always show debug statements as part of testing suite + #include + #define emc_log_i(...) std::cout << "[I] " << __FILE__ ":" << __LINE__ << ": "; printf(__VA_ARGS__); std::cout << std::endl + #define emc_log_e(...) std::cout << "[E] " << __FILE__ ":" << __LINE__ << ": "; printf(__VA_ARGS__); std::cout << std::endl + #define emc_log_w(...) std::cout << "[W] " << __FILE__ ":" << __LINE__ << ": "; printf(__VA_ARGS__); std::cout << std::endl +#endif diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp new file mode 100644 index 0000000..6b3758c --- /dev/null +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -0,0 +1,669 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "MqttClient.h" + +using espMqttClientInternals::Packet; +using espMqttClientInternals::PacketType; +using espMqttClientTypes::DisconnectReason; +using espMqttClientTypes::Error; + +#if defined(ARDUINO_ARCH_ESP32) +MqttClient::MqttClient(bool useTask, uint8_t priority, uint8_t core) +: _useTask(useTask) +, _transport(nullptr) +#else +MqttClient::MqttClient() +: _transport(nullptr) +#endif +, _onConnectCallback(nullptr) +, _onDisconnectCallback(nullptr) +, _onSubscribeCallback(nullptr) +, _onUnsubscribeCallback(nullptr) +, _onMessageCallback(nullptr) +, _onPublishCallback(nullptr) +, _onErrorCallback(nullptr) +, _clientId(nullptr) +, _ip() +, _host(nullptr) +, _port(1183) +, _useIp(false) +, _keepAlive(15000) +, _cleanSession(true) +, _username(nullptr) +, _password(nullptr) +, _willTopic(nullptr) +, _willPayload(nullptr) +, _willPayloadLength(0) +, _willQos(0) +, _willRetain(false) +, _state(State::disconnected) +, _generatedClientId{0} +, _packetId(0) +#if defined(ARDUINO_ARCH_ESP32) +, _xSemaphore(nullptr) +, _taskHandle(nullptr) +#endif +, _rxBuffer{0} +, _outbox() +, _bytesSent(0) +, _parser() +, _lastClientActivity(0) +, _lastServerActivity(0) +, _pingSent(false) +, _disconnectReason(DisconnectReason::TCP_DISCONNECTED) +#if defined(ARDUINO_ARCH_ESP32) +#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO +, _highWaterMark(4294967295) +#endif +#endif + { + EMC_GENERATE_CLIENTID(_generatedClientId); +#if defined(ARDUINO_ARCH_ESP32) + _xSemaphore = xSemaphoreCreateMutex(); + EMC_SEMAPHORE_GIVE(); // release before first use + if (useTask) { + xTaskCreatePinnedToCore((TaskFunction_t)_loop, "mqttclient", EMC_TASK_STACK_SIZE, this, priority, &_taskHandle, core); + } +#endif + _clientId = _generatedClientId; +} + +MqttClient::~MqttClient() { + disconnect(true); + _clearQueue(true); +#if defined(ARDUINO_ARCH_ESP32) + vSemaphoreDelete(_xSemaphore); + if (_useTask) { + #if EMC_USE_WATCHDOG + esp_task_wdt_delete(_taskHandle); // not sure if this is really needed + #endif + vTaskDelete(_taskHandle); + } +#endif +} + +bool MqttClient::connected() const { + if (_state == State::connected) return true; + return false; +} + +bool MqttClient::disconnected() const { + if (_state == State::disconnected) return true; + return false; +} + +bool MqttClient::connect() { + bool result = true; + if (_state == State::disconnected) { + EMC_SEMAPHORE_TAKE(); + if (_addPacketFront(_cleanSession, + _username, + _password, + _willTopic, + _willRetain, + _willQos, + _willPayload, + _willPayloadLength, + (uint16_t)(_keepAlive / 1000), // 32b to 16b doesn't overflow because it comes from 16b orignally + _clientId)) { + #if defined(ARDUINO_ARCH_ESP32) + if (_useTask) { + vTaskResume(_taskHandle); + } + #endif + _state = State::connectingTcp1; + } else { + EMC_SEMAPHORE_GIVE(); + emc_log_e("Could not create CONNECT packet"); + _onError(0, Error::OUT_OF_MEMORY); + result = false; + } + EMC_SEMAPHORE_GIVE(); + } + return result; +} + +bool MqttClient::disconnect(bool force) { + if (force && _state != State::disconnected && _state != State::disconnectingTcp1 && _state != State::disconnectingTcp2) { + _state = State::disconnectingTcp1; + return true; + } + if (!force && _state == State::connected) { + _state = State::disconnectingMqtt1; + return true; + } + return false; +} + +uint16_t MqttClient::publish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) { + #if !EMC_ALLOW_NOT_CONNECTED_PUBLISH + if (_state != State::connected) { + return 0; + } + #endif + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, payload, length, qos, retain)) { + emc_log_e("Could not create PUBLISH packet"); + _onError(packetId, Error::OUT_OF_MEMORY); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + return packetId; +} + +uint16_t MqttClient::publish(const char* topic, uint8_t qos, bool retain, const char* payload) { + size_t len = strlen(payload); + return publish(topic, qos, retain, reinterpret_cast(payload), len); +} + +uint16_t MqttClient::publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length) { + #if !EMC_ALLOW_NOT_CONNECTED_PUBLISH + if (_state != State::connected) { + return 0; + } + #endif + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, callback, length, qos, retain)) { + emc_log_e("Could not create PUBLISH packet"); + _onError(packetId, Error::OUT_OF_MEMORY); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + return packetId; +} + +void MqttClient::clearQueue(bool all) { + _clearQueue(all); +} + +const char* MqttClient::getClientId() const { + return _clientId; +} + +void MqttClient::loop() { + switch (_state) { + case State::disconnected: + #if defined(ARDUINO_ARCH_ESP32) + if (_useTask) { + vTaskSuspend(_taskHandle); + } + #endif + break; + case State::connectingTcp1: + if (_useIp ? _transport->connect(_ip, _port) : _transport->connect(_host, _port)) { + _state = State::connectingTcp2; + } else { + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + break; + } + // Falling through to speed up connecting on blocking transport 'connect' implementations + [[fallthrough]]; + case State::connectingTcp2: + if (_transport->connected()) { + _parser.reset(); + _lastClientActivity = _lastServerActivity = millis(); + _state = State::connectingMqtt; + } + break; + case State::disconnectingMqtt1: + EMC_SEMAPHORE_TAKE(); + if (_outbox.empty()) { + if (!_addPacket(PacketType.DISCONNECT)) { + EMC_SEMAPHORE_GIVE(); + emc_log_e("Could not create DISCONNECT packet"); + _onError(0, Error::OUT_OF_MEMORY); + } else { + _state = State::disconnectingMqtt2; + } + } + EMC_SEMAPHORE_GIVE(); + // fall through to 'connected' to send out DISCONN packet + [[fallthrough]]; + case State::disconnectingMqtt2: + [[fallthrough]]; + case State::connectingMqtt: + // receipt of CONNACK packet will set state to CONNECTED + // client however is allowed to send packets before CONNACK is received + // so we fall through to 'connected' + [[fallthrough]]; + case State::connected: + if (_transport->connected()) { + // CONNECT packet is first in the queue + _checkOutgoing(); + _checkIncoming(); + _checkPing(); + } else { + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + } + break; + case State::disconnectingTcp1: + _transport->stop(); + _state = State::disconnectingTcp2; + break; + case State::disconnectingTcp2: + if (_transport->disconnected()) { + _clearQueue(false); + _state = State::disconnected; + if (_onDisconnectCallback) _onDisconnectCallback(_disconnectReason); + } + break; + // all cases covered, no default case + } + EMC_YIELD(); + #if defined(ARDUINO_ARCH_ESP32) && ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO + size_t waterMark = uxTaskGetStackHighWaterMark(NULL); + if (waterMark < _highWaterMark) { + _highWaterMark = waterMark; + emc_log_i("Stack usage: %zu/%i", EMC_TASK_STACK_SIZE - _highWaterMark, EMC_TASK_STACK_SIZE); + } + #endif +} + +#if defined(ARDUINO_ARCH_ESP32) +void MqttClient::_loop(MqttClient* c) { + #if EMC_USE_WATCHDOG + if (esp_task_wdt_add(NULL) != ESP_OK) { + emc_log_e("Failed to add async task to WDT"); + } + #endif + for (;;) { + c->loop(); + #if EMC_USE_WATCHDOG + esp_task_wdt_reset(); + #endif + } +} +#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; +} + +void MqttClient::_checkOutgoing() { + EMC_SEMAPHORE_TAKE(); + Packet* packet = _outbox.getCurrent(); + + int32_t wantToWrite = 0; + int32_t written = 0; + while (packet && (wantToWrite == written)) { + // mixing signed with unsigned here but safe because of MQTT packet size limits + wantToWrite = packet->available(_bytesSent); + written = _transport->write(packet->data(_bytesSent), wantToWrite); + if (written < 0) { + emc_log_w("Write error, check connection"); + break; + } + _lastClientActivity = millis(); + _bytesSent += written; + emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->size(), packet->packetType()); + if (_bytesSent == packet->size()) { + if ((packet->packetType()) == PacketType.DISCONNECT) { + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::USER_OK; + } + if (packet->removable()) { + _outbox.removeCurrent(); + } else { + // handle with care! millis() returns unsigned 32 bit, token is void* + packet->token = reinterpret_cast(millis()); + if ((packet->packetType()) == PacketType.PUBLISH) packet->setDup(); + _outbox.next(); + } + packet = _outbox.getCurrent(); + _bytesSent = 0; + } + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_checkIncoming() { + int32_t remainingBufferLength = _transport->read(_rxBuffer, EMC_RX_BUFFER_SIZE); + if (remainingBufferLength > 0) { + _lastServerActivity = millis(); + emc_log_i("rx len %i", remainingBufferLength); + size_t bytesParsed = 0; + size_t index = 0; + while (remainingBufferLength > 0) { + espMqttClientInternals::ParserResult result = _parser.parse(&_rxBuffer[index], remainingBufferLength, &bytesParsed); + if (result == espMqttClientInternals::ParserResult::packet) { + 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; + return; + } + switch (packetType & 0xF0) { + case PacketType.CONNACK: + _onConnack(); + if (_state != State::connected) { + return; + } + break; + case PacketType.PUBLISH: + if (_state == State::disconnectingMqtt1 || _state == State::disconnectingMqtt2) break; // stop processing incoming once user has called disconnect + _onPublish(); + break; + case PacketType.PUBACK: + _onPuback(); + break; + case PacketType.PUBREC: + _onPubrec(); + break; + case PacketType.PUBREL: + _onPubrel(); + break; + case PacketType.PUBCOMP: + _onPubcomp(); + break; + case PacketType.SUBACK: + _onSuback(); + break; + case PacketType.UNSUBACK: + _onUnsuback(); + break; + case PacketType.PINGRESP: + _pingSent = false; + break; + } + } else if (result == espMqttClientInternals::ParserResult::protocolError) { + emc_log_w("Disconnecting, protocol error"); + _state = State::disconnectingTcp1; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + return; + } + remainingBufferLength -= bytesParsed; + index += bytesParsed; + emc_log_i("Parsed %zu - remaining %i", bytesParsed, remainingBufferLength); + bytesParsed = 0; + } + } +} + +void MqttClient::_checkPing() { + if (_keepAlive == 0) return; // keepalive is disabled + + uint32_t currentMillis = millis(); + + // 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; + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + return; + } + + // send ping when client was inactive during the keepalive time + // or when server hasn't responded within keepalive time (typically due to QOS 0) + if (!_pingSent && + ((currentMillis - _lastClientActivity > _keepAlive) || + (currentMillis - _lastServerActivity > _keepAlive))) { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(PacketType.PINGREQ)) { + EMC_SEMAPHORE_GIVE(); + emc_log_e("Could not create PING packet"); + return; + } + EMC_SEMAPHORE_GIVE(); + _pingSent = true; + } +} + +void MqttClient::_onConnack() { + if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) { + _pingSent = false; // reset after keepalive timeout disconnect + _state = State::connected; + if (_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent == 0) { + _clearQueue(true); + } + if (_onConnectCallback) { + _onConnectCallback(_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent); + } + } else { + _state = State::disconnectingTcp1; + // cast is safe because the parser already checked for a valid return code + _disconnectReason = static_cast(_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode); + } +} + +void MqttClient::_onPublish() { + espMqttClientInternals::IncomingPacket p = _parser.getPacket(); + uint8_t qos = p.qos(); + bool retain = p.retain(); + bool dup = p.dup(); + uint16_t packetId = p.variableHeader.fixed.packetId; + bool callback = true; + if (qos == 1) { + if (p.payload.index + p.payload.length == p.payload.total) { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(PacketType.PUBACK, packetId)) { + emc_log_e("Could not create PUBACK packet"); + } + EMC_SEMAPHORE_GIVE(); + } + } else if (qos == 2) { + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + if ((it.get()->packetType()) == PacketType.PUBREC && it.get()->packetId() == packetId) { + callback = false; + emc_log_e("QoS2 packet previously delivered"); + break; + } + ++it; + } + if (p.payload.index + p.payload.length == p.payload.total) { + if (!_addPacket(PacketType.PUBREC, packetId)) { + emc_log_e("Could not create PUBREC packet"); + } + } + EMC_SEMAPHORE_GIVE(); + } + if (callback && _onMessageCallback) _onMessageCallback({qos, dup, retain, packetId}, + p.variableHeader.topic, + p.payload.data, + p.payload.length, + p.payload.index, + p.payload.total); +} + +void MqttClient::_onPuback() { + bool callback = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + // PUBACKs come in the order PUBs are sent. So we only check the first PUB packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packetType()) == PacketType.PUBLISH) { + if (it.get()->packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + emc_log_w("Received out of order PUBACK"); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onPublishCallback) _onPublishCallback(idToMatch); + } else { + emc_log_w("No matching PUBLISH packet found"); + } +} + +void MqttClient::_onPubrec() { + bool success = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + // PUBRECs come in the order PUBs are sent. So we only check the first PUB packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packetType()) == PacketType.PUBLISH) { + if (it.get()->packetId() == idToMatch) { + if (!_addPacket(PacketType.PUBREL, idToMatch)) { + emc_log_e("Could not create PUBREL packet"); + } + _outbox.remove(it); + success = true; + break; + } + emc_log_w("Received out of order PUBREC"); + break; + } + ++it; + } + if (!success) { + emc_log_w("No matching PUBLISH packet found"); + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_onPubrel() { + bool success = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + // PUBRELs come in the order PUBRECs are sent. So we only check the first PUBREC packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packetType()) == PacketType.PUBREC) { + if (it.get()->packetId() == idToMatch) { + if (!_addPacket(PacketType.PUBCOMP, idToMatch)) { + emc_log_e("Could not create PUBCOMP packet"); + } + _outbox.remove(it); + success = true; + break; + } + emc_log_w("Received out of order PUBREL"); + break; + } + ++it; + } + if (!success) { + emc_log_w("No matching PUBREC packet found"); + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_onPubcomp() { + bool callback = false; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + while (it) { + // PUBCOMPs come in the order PUBRELs are sent. So we only check the first PUBREL packet in outbox + // if it doesn't match the ID, return + if ((it.get()->packetType()) == PacketType.PUBREL) { + if (it.get()->packetId() == idToMatch) { + if (!_addPacket(PacketType.PUBCOMP, idToMatch)) { + emc_log_e("Could not create PUBCOMP packet"); + } + callback = true; + _outbox.remove(it); + break; + } + emc_log_w("Received out of order PUBCOMP"); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onPublishCallback) _onPublishCallback(idToMatch); + } else { + emc_log_w("No matching PUBREL packet found"); + } +} + +void MqttClient::_onSuback() { + bool callback = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + if (((it.get()->packetType()) == PacketType.SUBSCRIBE) && it.get()->packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onSubscribeCallback) _onSubscribeCallback(idToMatch, reinterpret_cast(_parser.getPacket().payload.data), _parser.getPacket().payload.total); + } else { + emc_log_w("received SUBACK without SUB"); + } +} + +void MqttClient::_onUnsuback() { + bool callback = false; + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + while (it) { + if (it.get()->packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + ++it; + } + EMC_SEMAPHORE_GIVE(); + if (callback) { + if (_onUnsubscribeCallback) _onUnsubscribeCallback(idToMatch); + } else { + emc_log_w("received UNSUBACK without UNSUB"); + } +} + +void MqttClient::_clearQueue(bool clearSession) { + emc_log_i("clearing queue (clear session: %s)", clearSession ? "true" : "false"); + EMC_SEMAPHORE_TAKE(); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + if (clearSession) { + while (it) { + _outbox.remove(it); + } + } else { + // keep PUB (qos > 0, aka packetID != 0), PUBREC and PUBREL + // Spec only mentions PUB and PUBREL but this lib implements method B from point 4.3.3 (Fig. 4.3) + // and stores the packet id in the PUBREC packet. So we also must keep PUBREC. + while (it) { + espMqttClientInternals::MQTTPacketType type = it.get()->packetType(); + if (type == PacketType.PUBREC || + type == PacketType.PUBREL || + (type == PacketType.PUBLISH && it.get()->packetId() != 0)) { + ++it; + } else { + _outbox.remove(it); + } + } + } + EMC_SEMAPHORE_GIVE(); +} + +void MqttClient::_onError(uint16_t packetId, espMqttClientTypes::Error error) { + if (_onErrorCallback) { + _onErrorCallback(packetId, error); + } +} diff --git a/lib/espMqttClient/src/MqttClient.h b/lib/espMqttClient/src/MqttClient.h new file mode 100644 index 0000000..db7b254 --- /dev/null +++ b/lib/espMqttClient/src/MqttClient.h @@ -0,0 +1,185 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include + +#include "Helpers.h" +#include "Config.h" +#include "TypeDefs.h" +#include "Logging.h" +#include "Outbox.h" +#include "Packets/Packet.h" +#include "Packets/Parser.h" +#include "Transport/Transport.h" + +class MqttClient { + public: + virtual ~MqttClient(); + bool connected() const; + bool disconnected() const; + bool connect(); + bool disconnect(bool force = false); + template + uint16_t subscribe(const char* topic, uint8_t qos, Args&&... args) { + uint16_t packetId = _getNextPacketId(); + if (_state != State::connected) { + packetId = 0; + } else { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, qos, std::forward(args) ...)) { + emc_log_e("Could not create SUBSCRIBE packet"); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + } + return packetId; + } + template + uint16_t unsubscribe(const char* topic, Args&&... args) { + uint16_t packetId = _getNextPacketId(); + if (_state != State::connected) { + packetId = 0; + } else { + EMC_SEMAPHORE_TAKE(); + if (!_addPacket(packetId, topic, std::forward(args) ...)) { + emc_log_e("Could not create UNSUBSCRIBE packet"); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + } + return packetId; + } + uint16_t publish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length); + uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload); + uint16_t publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length); + void clearQueue(bool all = false); // Not MQTT compliant and may cause unpredictable results when `all` = true! + const char* getClientId() const; + #if defined(ARDUINO_ARCH_ESP32) + + protected: + #endif + void loop(); + #if defined(ARDUINO_ARCH_ESP32) + explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1); + bool _useTask; + #else + + protected: + MqttClient(); + #endif + espMqttClientInternals::Transport* _transport; + + espMqttClientTypes::OnConnectCallback _onConnectCallback; + espMqttClientTypes::OnDisconnectCallback _onDisconnectCallback; + espMqttClientTypes::OnSubscribeCallback _onSubscribeCallback; + espMqttClientTypes::OnUnsubscribeCallback _onUnsubscribeCallback; + espMqttClientTypes::OnMessageCallback _onMessageCallback; + espMqttClientTypes::OnPublishCallback _onPublishCallback; + espMqttClientTypes::OnErrorCallback _onErrorCallback; + typedef void(*mqttClientHook)(void*); + const char* _clientId; + IPAddress _ip; + const char* _host; + uint16_t _port; + bool _useIp; + uint32_t _keepAlive; + bool _cleanSession; + const char* _username; + const char* _password; + const char* _willTopic; + const uint8_t* _willPayload; + uint16_t _willPayloadLength; + uint8_t _willQos; + bool _willRetain; + + // state is protected to allow state changes by the transport system, defined in child classes + // eg. to allow AsyncTCP + enum class State { + disconnected, + connectingTcp1, + connectingTcp2, + connectingMqtt, + connected, + disconnectingMqtt1, + disconnectingMqtt2, + disconnectingTcp1, + disconnectingTcp2 + }; + std::atomic _state; + + private: + char _generatedClientId[EMC_CLIENTID_LENGTH]; + uint16_t _packetId; + +#if defined(ARDUINO_ARCH_ESP32) + SemaphoreHandle_t _xSemaphore; + TaskHandle_t _taskHandle; + static void _loop(MqttClient* c); +#elif defined(ARDUINO_ARCH_ESP8266) && EMC_ESP8266_MULTITHREADING + std::atomic _xSemaphore = false; +#elif defined(__linux__) + std::mutex mtx; +#endif + + uint8_t _rxBuffer[EMC_RX_BUFFER_SIZE]; + espMqttClientInternals::Outbox _outbox; + size_t _bytesSent; + espMqttClientInternals::Parser _parser; + uint32_t _lastClientActivity; + uint32_t _lastServerActivity; + bool _pingSent; + espMqttClientTypes::DisconnectReason _disconnectReason; + + uint16_t _getNextPacketId(); + + template + bool _addPacket(Args&&... args) { + espMqttClientTypes::Error error; + espMqttClientInternals::Outbox::Iterator it = _outbox.emplace(error, std::forward(args) ...); + if (it && error == espMqttClientTypes::Error::SUCCESS) return true; + _outbox.remove(it); + return false; + } + + template + bool _addPacketFront(Args&&... args) { + espMqttClientTypes::Error error; + espMqttClientInternals::Outbox::Iterator it = _outbox.emplaceFront(error, std::forward(args) ...); + if (it && error == espMqttClientTypes::Error::SUCCESS) return true; + _outbox.remove(it); + return false; + } + + void _checkOutgoing(); + void _checkIncoming(); + void _checkPing(); + + void _onConnack(); + void _onPublish(); + void _onPuback(); + void _onPubrec(); + void _onPubrel(); + void _onPubcomp(); + void _onSuback(); + void _onUnsuback(); + + void _clearQueue(bool clearSession); + void _onError(uint16_t packetId, espMqttClientTypes::Error error); + + #if defined(ARDUINO_ARCH_ESP32) + #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO + size_t _highWaterMark; + #endif + #endif +}; diff --git a/lib/espMqttClient/src/MqttClientSetup.h b/lib/espMqttClient/src/MqttClientSetup.h new file mode 100644 index 0000000..f7a28c4 --- /dev/null +++ b/lib/espMqttClient/src/MqttClientSetup.h @@ -0,0 +1,115 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include "MqttClient.h" + +class MqttClientSetup : public MqttClient { +public: + void setKeepAlive(uint16_t keepAlive) { + _keepAlive = keepAlive * 1000; // s to ms conversion, will also do 16 to 32 bit conversion + + } + + void setClientId(const char* clientId) { + _clientId = clientId; + + } + + void setCleanSession(bool cleanSession) { + _cleanSession = cleanSession; + + } + + void setCredentials(const char* username, const char* password) { + _username = username; + _password = password; + + } + + void setWill(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) { + _willTopic = topic; + _willQos = qos; + _willRetain = retain; + _willPayload = payload; + if (!_willPayload) { + _willPayloadLength = 0; + } else { + _willPayloadLength = length; + } + + } + + void setWill(const char* topic, uint8_t qos, bool retain, const char* payload) { + return setWill(topic, qos, retain, reinterpret_cast(payload), strlen(payload)); + } + + void setServer(IPAddress ip, uint16_t port) { + _ip = ip; + _port = port; + _useIp = true; + + } + + void setServer(const char* host, uint16_t port) { + _host = host; + _port = port; + _useIp = false; + + } + + void onConnect(espMqttClientTypes::OnConnectCallback callback) { + _onConnectCallback = callback; + + } + + void onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) { + _onDisconnectCallback = callback; + + } + + void onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) { + _onSubscribeCallback = callback; + + } + + void onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) { + _onUnsubscribeCallback = callback; + + } + + void onMessage(espMqttClientTypes::OnMessageCallback callback) { + _onMessageCallback = callback; + + } + + void onPublish(espMqttClientTypes::OnPublishCallback callback) { + _onPublishCallback = callback; + + } + + /* + void onError(espMqttClientTypes::OnErrorCallback callback) { + _onErrorCallback = callback; + + } + */ + +protected: +#if defined(ESP32) + explicit MqttClientSetup(bool useTask, uint8_t priority = 1, uint8_t core = 1) + : MqttClient(useTask, priority, core) {} +#else + MqttClientSetup() + : MqttClient() {} +#endif +}; diff --git a/lib/espMqttClient/src/Outbox.h b/lib/espMqttClient/src/Outbox.h new file mode 100644 index 0000000..5e1edcf --- /dev/null +++ b/lib/espMqttClient/src/Outbox.h @@ -0,0 +1,202 @@ + +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include // new (std::nothrow) +#include // std::forward + +namespace espMqttClientInternals { + +/** + * @brief Singly linked queue with builtin non-invalidating forward iterator + * + * Queue items can only be emplaced, at front and back of the queue. + * Remove items using an iterator or the builtin iterator. + */ + +template +class Outbox { + public: + Outbox() + : _first(nullptr) + , _last(nullptr) + , _current(nullptr) + , _prev(nullptr) {} + ~Outbox() { + while (_first) { + Node* n = _first->next; + delete _first; + _first = n; + } + } + + struct Node { + public: + template + explicit Node(Args&&... args) + : data(std::forward(args) ...) + , next(nullptr) { + // empty + } + + T data; + Node* next; + }; + + class Iterator { + friend class Outbox; + public: + void operator++() { + if (_node) { + _prev = _node; + _node = _node->next; + } + } + + explicit operator bool() const { + if (_node) return true; + return false; + } + + T* get() const { + if (_node) return &(_node->data); + return nullptr; + } + + private: + Node* _node = nullptr; + Node* _prev = nullptr; + }; + + // add node to back, advance current to new if applicable + template + Iterator emplace(Args&&... args) { + Iterator it; + Node* node = new (std::nothrow) Node(std::forward(args) ...); + if (node != nullptr) { + if (!_first) { + // queue is empty + _first = _current = node; + } else { + // queue has at least one item + _last->next = node; + it._prev = _last; + } + _last = node; + it._node = node; + // point current to newly created if applicable + if (!_current) { + _current = _last; + } + } + return it; + } + + // add item to front, current points to newly created front. + template + Iterator emplaceFront(Args&&... args) { + Iterator it; + Node* node = new (std::nothrow) Node(std::forward(args) ...); + if (node != nullptr) { + if (!_first) { + // queue is empty + _last = node; + } else { + // queue has at least one item + node->next = _first; + } + _current = _first = node; + _prev = nullptr; + it._node = node; + } + return it; + } + + // remove node at iterator, iterator points to next + void remove(Iterator& it) { // NOLINT(runtime/references) + Node* node = it._node; + Node* prev = it._prev; + ++it; + _remove(prev, node); + } + + // remove current node, current points to next + void removeCurrent() { + _remove(_prev, _current); + } + + // Get current item or return nullptr + T* getCurrent() const { + if (_current) return &(_current->data); + return nullptr; + } + + Iterator front() const { + Iterator it; + it._node = _first; + return it; + } + + // Advance current item + void next() { + if (_current) { + _prev = _current; + _current = _current->next; + } + } + + // Outbox is empty + bool empty() { + if (!_first) return true; + return false; + } + + private: + Node* _first; + Node* _last; + Node* _current; + Node* _prev; // element just before _current + + void _remove(Node* prev, Node* node) { + if (!node) return; + + // set current to next, node->next may be nullptr + if (_current == node) { + _current = node->next; + } + + if (_prev == node) { + _prev = prev; + } + + // only one element in outbox + if (_first == _last) { + _first = _last = nullptr; + + // delete first el in longer outbox + } else if (_first == node) { + _first = node->next; + + // delete last in longer outbox + } else if (_last == node) { + _last = prev; + _last->next = nullptr; + + // delete somewhere in the middle + } else { + prev->next = node->next; + } + + // finally, delete the node + delete node; + } +}; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Constants.h b/lib/espMqttClient/src/Packets/Constants.h new file mode 100644 index 0000000..ee92e31 --- /dev/null +++ b/lib/espMqttClient/src/Packets/Constants.h @@ -0,0 +1,77 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +Parts are based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include + +namespace espMqttClientInternals { + +constexpr const char PROTOCOL[] = "MQTT"; +constexpr const uint8_t PROTOCOL_LEVEL = 0b00000100; + +typedef uint8_t MQTTPacketType; + +constexpr struct { + const uint8_t RESERVED1 = 0; + const uint8_t CONNECT = 1 << 4; + const uint8_t CONNACK = 2 << 4; + const uint8_t PUBLISH = 3 << 4; + const uint8_t PUBACK = 4 << 4; + const uint8_t PUBREC = 5 << 4; + const uint8_t PUBREL = 6 << 4; + const uint8_t PUBCOMP = 7 << 4; + const uint8_t SUBSCRIBE = 8 << 4; + const uint8_t SUBACK = 9 << 4; + const uint8_t UNSUBSCRIBE = 10 << 4; + const uint8_t UNSUBACK = 11 << 4; + const uint8_t PINGREQ = 12 << 4; + const uint8_t PINGRESP = 13 << 4; + const uint8_t DISCONNECT = 14 << 4; + const uint8_t RESERVED2 = 1 << 4; +} PacketType; + +constexpr struct { + const uint8_t CONNECT_RESERVED = 0x00; + const uint8_t CONNACK_RESERVED = 0x00; + const uint8_t PUBLISH_DUP = 0x08; + const uint8_t PUBLISH_QOS0 = 0x00; + const uint8_t PUBLISH_QOS1 = 0x02; + const uint8_t PUBLISH_QOS2 = 0x04; + const uint8_t PUBLISH_QOSRESERVED = 0x06; + const uint8_t PUBLISH_RETAIN = 0x01; + const uint8_t PUBACK_RESERVED = 0x00; + const uint8_t PUBREC_RESERVED = 0x00; + const uint8_t PUBREL_RESERVED = 0x02; + const uint8_t PUBCOMP_RESERVED = 0x00; + const uint8_t SUBSCRIBE_RESERVED = 0x02; + const uint8_t SUBACK_RESERVED = 0x00; + const uint8_t UNSUBSCRIBE_RESERVED = 0x02; + const uint8_t UNSUBACK_RESERVED = 0x00; + const uint8_t PINGREQ_RESERVED = 0x00; + const uint8_t PINGRESP_RESERVED = 0x00; + const uint8_t DISCONNECT_RESERVED = 0x00; + const uint8_t RESERVED2_RESERVED = 0x00; +} HeaderFlag; + +constexpr struct { + const uint8_t USERNAME = 0x80; + const uint8_t PASSWORD = 0x40; + const uint8_t WILL_RETAIN = 0x20; + const uint8_t WILL_QOS0 = 0x00; + const uint8_t WILL_QOS1 = 0x08; + const uint8_t WILL_QOS2 = 0x10; + const uint8_t WILL = 0x04; + const uint8_t CLEAN_SESSION = 0x02; + const uint8_t RESERVED = 0x00; +} ConnectFlag; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Packet.cpp b/lib/espMqttClient/src/Packets/Packet.cpp new file mode 100644 index 0000000..39816df --- /dev/null +++ b/lib/espMqttClient/src/Packets/Packet.cpp @@ -0,0 +1,445 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "Packet.h" + +namespace espMqttClientInternals { + +Packet::~Packet() { + free(_data); +} + +size_t Packet::available(size_t index) { + if (index >= _size) return 0; + if (!_getPayload) return _size - index; + return _chunkedAvailable(index); +} + +const uint8_t* Packet::data(size_t index) const { + if (!_getPayload) { + if (!_data) return nullptr; + if (index >= _size) return nullptr; + return &_data[index]; + } + return _chunkedData(index); +} + +size_t Packet::size() const { + return _size; +} + +void Packet::setDup() { + if (!_data) return; + if (packetType() != PacketType.PUBLISH) return; + if (_packetId == 0) return; + _data[0] |= 0x08; +} + +uint16_t Packet::packetId() const { + return _packetId; +} + +MQTTPacketType Packet::packetType() const { + if (_data) return static_cast(_data[0] & 0xF0); + return static_cast(0); +} + +bool Packet::removable() const { + if (_packetId == 0) return true; + if ((packetType() == PacketType.PUBACK) || (packetType() == PacketType.PUBCOMP)) return true; + return false; +} + +Packet::Packet(espMqttClientTypes::Error& error, + bool cleanSession, + const char* username, + const char* password, + const char* willTopic, + bool willRetain, + uint8_t willQos, + const uint8_t* willPayload, + uint16_t willPayloadLength, + uint16_t keepAlive, + const char* clientId) +: token(nullptr) +, _packetId(0) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (willPayload && willPayloadLength == 0) { + size_t length = strlen(reinterpret_cast(willPayload)); + if (length > UINT16_MAX) { + emc_log_w("Payload length truncated (l:%zu)", length); + willPayloadLength = UINT16_MAX; + } else { + willPayloadLength = length; + } + } + if (!clientId || strlen(clientId) == 0) { + emc_log_w("clientId not set error"); + error = espMqttClientTypes::Error::MALFORMED_PARAMETER; + return; + } + + // Calculate size + size_t remainingLength = + 6 + // protocol + 1 + // protocol level + 1 + // connect flags + 2 + // keepalive + 2 + strlen(clientId) + + (willTopic ? 2 + strlen(willTopic) + 2 + willPayloadLength : 0) + + (username ? 2 + strlen(username) : 0) + + (password ? 2 + strlen(password) : 0); + + // allocate memory + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + // serialize + size_t pos = 0; + + // FIXED HEADER + _data[pos++] = PacketType.CONNECT | HeaderFlag.CONNECT_RESERVED; + pos += encodeRemainingLength(remainingLength, &_data[pos]); + pos += encodeString(PROTOCOL, &_data[pos]); + _data[pos++] = PROTOCOL_LEVEL; + uint8_t connectFlags = 0; + if (cleanSession) connectFlags |= espMqttClientInternals::ConnectFlag.CLEAN_SESSION; + if (username != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.USERNAME; + if (password != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.PASSWORD; + if (willTopic != nullptr) { + connectFlags |= espMqttClientInternals::ConnectFlag.WILL; + if (willRetain) connectFlags |= espMqttClientInternals::ConnectFlag.WILL_RETAIN; + switch (willQos) { + case 0: + connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS0; + break; + case 1: + connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS1; + break; + case 2: + connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS2; + break; + } + } + _data[pos++] = connectFlags; + _data[pos++] = keepAlive >> 8; + _data[pos++] = keepAlive & 0xFF; + + // PAYLOAD + // client ID + pos += encodeString(clientId, &_data[pos]); + // will + if (willTopic != nullptr && willPayload != nullptr) { + pos += encodeString(willTopic, &_data[pos]); + _data[pos++] = willPayloadLength >> 8; + _data[pos++] = willPayloadLength & 0xFF; + memcpy(&_data[pos], willPayload, willPayloadLength); + pos += willPayloadLength; + } + // credentials + if (username != nullptr) pos += encodeString(username, &_data[pos]); + if (password != nullptr) encodeString(password, &_data[pos]); + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, + uint16_t packetId, + const char* topic, + const uint8_t* payload, + size_t payloadLength, + uint8_t qos, + bool retain) +: token(nullptr) +, _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + size_t remainingLength = + 2 + strlen(topic) + // topic length + topic + 2 + // packet ID + payloadLength; + + if (qos == 0) { + remainingLength -= 2; + _packetId = 0; + } + + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + size_t pos = _fillPublishHeader(packetId, topic, remainingLength, qos, retain); + + // PAYLOAD + memcpy(&_data[pos], payload, payloadLength); + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, + uint16_t packetId, + const char* topic, + espMqttClientTypes::PayloadCallback payloadCallback, + size_t payloadLength, + uint8_t qos, + bool retain) +: token(nullptr) +, _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(payloadCallback) { + size_t remainingLength = + 2 + strlen(topic) + // topic length + topic + 2 + // packet ID + payloadLength; + + if (qos == 0) { + remainingLength -= 2; + _packetId = 0; + } + + if (!_allocate(remainingLength - payloadLength + std::min(payloadLength, static_cast(EMC_RX_BUFFER_SIZE)))) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + size_t pos = _fillPublishHeader(packetId, topic, remainingLength, qos, retain); + + // payload will be added by 'Packet::available' + _size = pos + payloadLength; + _payloadIndex = pos; + _payloadStartIndex = _payloadIndex; + _payloadEndIndex = _payloadIndex; + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, uint16_t packetId, const char* topic, uint8_t qos) +: token(nullptr) +, _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + SubscribeItem list[1] = {topic, qos}; + _createSubscribe(error, list, 1); +} + +Packet::Packet(espMqttClientTypes::Error& error, MQTTPacketType type, uint16_t packetId) +: token(nullptr) +, _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (!_allocate(2)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + size_t pos = 0; + _data[pos] = type; + if (type == PacketType.PUBREL) { + _data[pos++] |= HeaderFlag.PUBREL_RESERVED; + } else { + pos++; + } + pos += encodeRemainingLength(2, &_data[pos]); + _data[pos++] = packetId >> 8; + _data[pos] = packetId & 0xFF; + + error = espMqttClientTypes::Error::SUCCESS; +} + +Packet::Packet(espMqttClientTypes::Error& error, uint16_t packetId, const char* topic) +: token(nullptr) +, _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + const char* list[1] = {topic}; + _createUnsubscribe(error, list, 1); +} + +Packet::Packet(espMqttClientTypes::Error& error, MQTTPacketType type) +: token(nullptr) +, _packetId(0) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (!_allocate(0)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + _data[0] |= type; + + error = espMqttClientTypes::Error::SUCCESS; +} + + +bool Packet::_allocate(size_t remainingLength) { + if (EMC_GET_FREE_MEMORY() < EMC_MIN_FREE_MEMORY) { + emc_log_w("Packet buffer not allocated: low memory"); + return false; + } + _size = 1 + remainingLengthLength(remainingLength) + remainingLength; + _data = reinterpret_cast(malloc(_size)); + if (!_data) { + _size = 0; + emc_log_w("Alloc failed (l:%zu)", _size); + return false; + } + emc_log_i("Alloc (l:%zu)", _size); + memset(_data, 0, _size); + return true; +} + +size_t Packet::_fillPublishHeader(uint16_t packetId, + const char* topic, + size_t remainingLength, + uint8_t qos, + bool retain) { + size_t index = 0; + + // FIXED HEADER + _data[index] = PacketType.PUBLISH; + if (retain) _data[index] |= HeaderFlag.PUBLISH_RETAIN; + if (qos == 0) { + _data[index++] |= HeaderFlag.PUBLISH_QOS0; + } else if (qos == 1) { + _data[index++] |= HeaderFlag.PUBLISH_QOS1; + } else if (qos == 2) { + _data[index++] |= HeaderFlag.PUBLISH_QOS2; + } + index += encodeRemainingLength(remainingLength, &_data[index]); + + // VARIABLE HEADER + index += encodeString(topic, &_data[index]); + if (qos > 0) { + _data[index++] = packetId >> 8; + _data[index++] = packetId & 0xFF; + } + + return index; +} + +void Packet::_createSubscribe(espMqttClientTypes::Error& error, + SubscribeItem* list, + size_t numberTopics) { + // Calculate size + size_t payload = 0; + for (size_t i = 0; i < numberTopics; ++i) { + payload += 2 + strlen(list[i].topic) + 1; // length bytes, string, qos + } + size_t remainingLength = 2 + payload; // packetId + payload + + // allocate memory + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + // serialize + size_t pos = 0; + _data[pos++] = PacketType.SUBSCRIBE | HeaderFlag.SUBSCRIBE_RESERVED; + pos += encodeRemainingLength(remainingLength, &_data[pos]); + _data[pos++] = _packetId >> 8; + _data[pos++] = _packetId & 0xFF; + for (size_t i = 0; i < numberTopics; ++i) { + pos += encodeString(list[i].topic, &_data[pos]); + _data[pos++] = list[i].qos; + } + + error = espMqttClientTypes::Error::SUCCESS; +} + +void Packet::_createUnsubscribe(espMqttClientTypes::Error& error, + const char** list, + size_t numberTopics) { + // Calculate size + size_t payload = 0; + for (size_t i = 0; i < numberTopics; ++i) { + payload += 2 + strlen(list[i]); // length bytes, string + } + size_t remainingLength = 2 + payload; // packetId + payload + + // allocate memory + if (!_allocate(remainingLength)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + + // serialize + size_t pos = 0; + _data[pos++] = PacketType.UNSUBSCRIBE | HeaderFlag.UNSUBSCRIBE_RESERVED; + pos += encodeRemainingLength(remainingLength, &_data[pos]); + _data[pos++] = _packetId >> 8; + _data[pos++] = _packetId & 0xFF; + for (size_t i = 0; i < numberTopics; ++i) { + pos += encodeString(list[i], &_data[pos]); + } + + error = espMqttClientTypes::Error::SUCCESS; +} + +size_t Packet::_chunkedAvailable(size_t index) { + // index vs size check done in 'available(index)' + + // index points to header or first payload byte + if (index < _payloadIndex) { + if (_size > _payloadIndex && _payloadEndIndex != 0) { + size_t copied = _getPayload(&_data[_payloadIndex], std::min(static_cast(EMC_TX_BUFFER_SIZE), _size - _payloadStartIndex), index); + _payloadStartIndex = _payloadIndex; + _payloadEndIndex = _payloadStartIndex + copied - 1; + } + + // index points to payload unavailable + } else if (index > _payloadEndIndex || _payloadStartIndex > index) { + _payloadStartIndex = index; + size_t copied = _getPayload(&_data[_payloadIndex], std::min(static_cast(EMC_TX_BUFFER_SIZE), _size - _payloadStartIndex), index); + _payloadEndIndex = _payloadStartIndex + copied - 1; + } + + // now index points to header or payload available + return _payloadEndIndex - index + 1; +} + +const uint8_t* Packet::_chunkedData(size_t index) const { + // CAUTION!! available(index) has to be called first to check available data and possibly fill payloadbuffer + if (index < _payloadIndex) { + return &_data[index]; + } + return &_data[index - _payloadStartIndex + _payloadIndex]; +} + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Packet.h b/lib/espMqttClient/src/Packets/Packet.h new file mode 100644 index 0000000..b76aada --- /dev/null +++ b/lib/espMqttClient/src/Packets/Packet.h @@ -0,0 +1,159 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include + +#include "Constants.h" +#include "Config.h" +#include "../TypeDefs.h" +#include "../Helpers.h" +#include "../Logging.h" +#include "RemainingLength.h" +#include "String.h" + +namespace espMqttClientInternals { + +class Packet { + public: + ~Packet(); + size_t available(size_t index); + const uint8_t* data(size_t index) const; + + size_t size() const; + void setDup(); + uint16_t packetId() const; + MQTTPacketType packetType() const; + bool removable() const; + + void* token; // native typeless variable to store any additional data + + protected: + uint16_t _packetId; // save as separate variable: will be accessed frequently + uint8_t* _data; + size_t _size; + + // variables for chunked payload handling + size_t _payloadIndex; + size_t _payloadStartIndex; + size_t _payloadEndIndex; + espMqttClientTypes::PayloadCallback _getPayload; + + struct SubscribeItem { + const char* topic; + uint8_t qos; + }; + + public: + // CONNECT + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + bool cleanSession, + const char* username, + const char* password, + const char* willTopic, + bool willRetain, + uint8_t willQos, + const uint8_t* willPayload, + uint16_t willPayloadLength, + uint16_t keepAlive, + const char* clientId); + // PUBLISH + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic, + const uint8_t* payload, + size_t payloadLength, + uint8_t qos, + bool retain); + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic, + espMqttClientTypes::PayloadCallback payloadCallback, + size_t payloadLength, + uint8_t qos, + bool retain); + // SUBSCRIBE + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic, + uint8_t qos); + template + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic1, + uint8_t qos1, + const char* topic2, + uint8_t qos2, + Args&& ... args) + : token(nullptr) + , _packetId(packetId) + , _data(nullptr) + , _size(0) + , _payloadIndex(0) + , _payloadStartIndex(0) + , _payloadEndIndex(0) + , _getPayload(nullptr) { + static_assert(sizeof...(Args) % 2 == 0); + size_t numberTopics = 2 + (sizeof...(Args) / 2); + SubscribeItem list[numberTopics] = {topic1, qos1, topic2, qos2, args...}; + _createSubscribe(error, list, numberTopics); + } + // UNSUBSCRIBE + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic); + template + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + uint16_t packetId, + const char* topic1, + const char* topic2, + Args&& ... args) + : token(nullptr) + , _packetId(packetId) + , _data(nullptr) + , _size(0) + , _payloadIndex(0) + , _payloadStartIndex(0) + , _payloadEndIndex(0) + , _getPayload(nullptr) { + size_t numberTopics = 2 + sizeof...(Args); + const char* list[numberTopics] = {topic1, topic2, args...}; + _createUnsubscribe(error, list, numberTopics); + } + // PUBACK, PUBREC, PUBREL, PUBCOMP + Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + MQTTPacketType type, + uint16_t packetId); + // PING, DISCONN + explicit Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + MQTTPacketType type); + + private: + // pass remainingLength = total size - header - remainingLengthLength! + bool _allocate(size_t remainingLength); + + // fills header and returns index of next available byte in buffer + size_t _fillPublishHeader(uint16_t packetId, + const char* topic, + size_t remainingLength, + uint8_t qos, + bool retain); + void _createSubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + SubscribeItem* list, + size_t numberTopics); + void _createUnsubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references) + const char** list, + size_t numberTopics); + + size_t _chunkedAvailable(size_t index); + const uint8_t* _chunkedData(size_t index) const; +}; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Parser.cpp b/lib/espMqttClient/src/Packets/Parser.cpp new file mode 100644 index 0000000..07998a3 --- /dev/null +++ b/lib/espMqttClient/src/Packets/Parser.cpp @@ -0,0 +1,316 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "Parser.h" + +namespace espMqttClientInternals { + +uint8_t IncomingPacket::qos() const { + if ((fixedHeader.packetType & 0xF0) != PacketType.PUBLISH) return 0; + return (fixedHeader.packetType & 0x06) >> 1; // mask 0x00000110 +} + +bool IncomingPacket::retain() const { + if ((fixedHeader.packetType & 0xF0) != PacketType.PUBLISH) return 0; + return fixedHeader.packetType & 0x01; // mask 0x00000001 +} + +bool IncomingPacket::dup() const { + if ((fixedHeader.packetType & 0xF0) != PacketType.PUBLISH) return 0; + return fixedHeader.packetType & 0x08; // mask 0x00001000 +} + +void IncomingPacket::reset() { + fixedHeader.packetType = 0; + variableHeader.topicLength = 0; + variableHeader.fixed.packetId = 0; + payload.index = 0; + payload.length = 0; +} + +Parser::Parser() +: _data(nullptr) +, _len(0) +, _bytesRead(0) +, _bytePos(0) +, _parse(_fixedHeader) +, _packet() +, _payloadBuffer{0} { + // empty +} + +ParserResult Parser::parse(const uint8_t* data, size_t len, size_t* bytesRead) { + _data = data; + _len = len; + _bytesRead = 0; + ParserResult result = ParserResult::awaitData; + while (result == ParserResult::awaitData && _bytesRead < _len) { + result = _parse(this); + ++_bytesRead; + } + (*bytesRead) += _bytesRead; + return result; +} + +const IncomingPacket& Parser::getPacket() const { + return _packet; +} + +void Parser::reset() { + _parse = _fixedHeader; + _bytesRead = 0; + _bytePos = 0; + _packet.reset(); +} + +ParserResult Parser::_fixedHeader(Parser* p) { + p->_packet.reset(); + p->_packet.fixedHeader.packetType = p->_data[p->_bytesRead]; + + // keep PUBLISH out of the switch and handle in separate if/else + if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.PUBLISH) { + uint8_t headerFlags = p->_packet.fixedHeader.packetType & 0x0F; + /* flags can be: 0b0000 --> no dup, qos 0, no retain + 0x0001 --> no dup, qos 0, retain + 0x0010 --> no dup, qos 1, no retain + 0x0011 --> no dup, qos 1, retain + 0x0100 --> no dup, qos 2, no retain + 0x0101 --> no dup, qos 2, retain + 0x1010 --> dup, qos 1, no retain + 0x1011 --> dup, qos 1, retain + 0x1100 --> dup, qos 2, no retain + 0x1101 --> dup, qos 2, retain + */ + if (headerFlags <= 0x05 || headerFlags >= 0x0A) { + p->_parse = _remainingLengthVariable; + p->_bytePos = 0; + } else { + emc_log_w("Invalid packet header: 0x%02x", p->_packet.fixedHeader.packetType); + return ParserResult::protocolError; + } + } else { + switch (p->_packet.fixedHeader.packetType) { + case PacketType.CONNACK | HeaderFlag.CONNACK_RESERVED: + case PacketType.PUBACK | HeaderFlag.PUBACK_RESERVED: + case PacketType.PUBREC | HeaderFlag.PUBREC_RESERVED: + case PacketType.PUBREL | HeaderFlag.PUBREL_RESERVED: + case PacketType.PUBCOMP | HeaderFlag.PUBCOMP_RESERVED: + case PacketType.UNSUBACK | HeaderFlag.UNSUBACK_RESERVED: + p->_parse = _remainingLengthFixed; + break; + case PacketType.SUBACK | HeaderFlag.SUBACK_RESERVED: + p->_parse = _remainingLengthVariable; + p->_bytePos = 0; + break; + case PacketType.PINGRESP | HeaderFlag.PINGRESP_RESERVED: + p->_parse = _remainingLengthNone; + break; + default: + emc_log_w("Invalid packet header: 0x%02x", p->_packet.fixedHeader.packetType); + return ParserResult::protocolError; + } + } + emc_log_i("Packet type: 0x%02x", p->_packet.fixedHeader.packetType); + return ParserResult::awaitData; +} + +ParserResult Parser::_remainingLengthFixed(Parser* p) { + p->_packet.fixedHeader.remainingLength.remainingLength = p->_data[p->_bytesRead]; + + if (p->_packet.fixedHeader.remainingLength.remainingLength == 2) { // variable header is 2 bytes long + if ((p->_packet.fixedHeader.packetType & 0xF0) != PacketType.CONNACK) { + p->_parse = _varHeaderPacketId1; + } else { + p->_parse = _varHeaderConnack1; + } + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::awaitData; + } + p->_parse = _fixedHeader; + emc_log_w("Invalid remaining length (fixed): %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::protocolError; +} + +ParserResult Parser::_remainingLengthVariable(Parser* p) { + p->_packet.fixedHeader.remainingLength.remainingLengthRaw[p->_bytePos] = p->_data[p->_bytesRead]; + if (p->_packet.fixedHeader.remainingLength.remainingLengthRaw[p->_bytePos] & 0x80) { + p->_bytePos++; + if (p->_bytePos == 4) { + emc_log_w("Invalid remaining length (variable)"); + return ParserResult::protocolError; + } else { + return ParserResult::awaitData; + } + } + + // no need to check for negative decoded length, check is already done + p->_packet.fixedHeader.remainingLength.remainingLength = decodeRemainingLength(p->_packet.fixedHeader.remainingLength.remainingLengthRaw); + + if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.PUBLISH) { + p->_parse = _varHeaderTopicLength1; + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::awaitData; + } else { + int32_t payloadSize = p->_packet.fixedHeader.remainingLength.remainingLength - 2; // total - packet ID + if (0 < payloadSize && payloadSize < EMC_PAYLOAD_BUFFER_SIZE) { + p->_bytePos = 0; + p->_packet.payload.data = p->_payloadBuffer; + p->_packet.payload.index = 0; + p->_packet.payload.length = payloadSize; + p->_packet.payload.total = payloadSize; + p->_parse = _varHeaderPacketId1; + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::awaitData; + } else { + emc_log_w("Invalid payload length"); + } + } + p->_parse = _fixedHeader; + return ParserResult::protocolError; +} + +ParserResult Parser::_remainingLengthNone(Parser* p) { + p->_packet.fixedHeader.remainingLength.remainingLength = p->_data[p->_bytesRead]; + p->_parse = _fixedHeader; + if (p->_packet.fixedHeader.remainingLength.remainingLength == 0) { + emc_log_i("Remaining length: %zu", p->_packet.fixedHeader.remainingLength.remainingLength); + return ParserResult::packet; + } + emc_log_w("Invalid remaining length (none)"); + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderConnack1(Parser* p) { + uint8_t data = p->_data[p->_bytesRead]; + if (data < 2) { // session present flag: equal to 0 or 1 + p->_packet.variableHeader.fixed.connackVarHeader.sessionPresent = data; + p->_parse = _varHeaderConnack2; + return ParserResult::awaitData; + } + p->_parse = _fixedHeader; + emc_log_w("Invalid session flags"); + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderConnack2(Parser* p) { + uint8_t data = p->_data[p->_bytesRead]; + p->_parse = _fixedHeader; + if (data <= 5) { // connect return code max is 5 + p->_packet.variableHeader.fixed.connackVarHeader.returnCode = data; + emc_log_i("Packet complete"); + return ParserResult::packet; + } + emc_log_w("Invalid connack return code"); + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderPacketId1(Parser* p) { + p->_packet.variableHeader.fixed.packetId |= p->_data[p->_bytesRead] << 8; + p->_parse = _varHeaderPacketId2; + return ParserResult::awaitData; +} + +ParserResult Parser::_varHeaderPacketId2(Parser* p) { + p->_packet.variableHeader.fixed.packetId |= p->_data[p->_bytesRead]; + p->_parse = _fixedHeader; + if (p->_packet.variableHeader.fixed.packetId != 0) { + emc_log_i("Packet variable header complete"); + if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.SUBACK) { + p->_parse = _payloadSuback; + return ParserResult::awaitData; + } else if ((p->_packet.fixedHeader.packetType & 0xF0) == PacketType.PUBLISH) { + p->_packet.payload.total -= 2; // substract packet id length from payload + if (p->_packet.payload.total == 0) { + p->_parse = _fixedHeader; + return ParserResult::packet; + } else { + p->_parse = _payloadPublish; + } + return ParserResult::awaitData; + } else { + return ParserResult::packet; + } + } else { + emc_log_w("Invalid packet id"); + return ParserResult::protocolError; + } +} + +ParserResult Parser::_varHeaderTopicLength1(Parser* p) { + p->_packet.variableHeader.topicLength = p->_data[p->_bytesRead] << 8; + p->_parse = _varHeaderTopicLength2; + return ParserResult::awaitData; +} + +ParserResult Parser::_varHeaderTopicLength2(Parser* p) { + p->_packet.variableHeader.topicLength |= p->_data[p->_bytesRead]; + size_t maxTopicLength = + p->_packet.fixedHeader.remainingLength.remainingLength + - 2 // topic length bytes + - ((p->_packet.fixedHeader.packetType & (HeaderFlag.PUBLISH_QOS1 | HeaderFlag.PUBLISH_QOS2)) ? 2 : 0); + if (p->_packet.variableHeader.topicLength <= maxTopicLength) { + p->_parse = _varHeaderTopic; + p->_bytePos = 0; + p->_packet.payload.total = p->_packet.fixedHeader.remainingLength.remainingLength - 2 - p->_packet.variableHeader.topicLength; + return ParserResult::awaitData; + } + emc_log_w("Invalid topic length: %u > %zu", p->_packet.variableHeader.topicLength, maxTopicLength); + p->_parse = _fixedHeader; + return ParserResult::protocolError; +} + +ParserResult Parser::_varHeaderTopic(Parser* p) { + // no checking for character [MQTT-3.3.2-1] [MQTT-3.3.2-2] + p->_packet.variableHeader.topic[p->_bytePos] = static_cast(p->_data[p->_bytesRead]); + p->_bytePos++; + if (p->_bytePos == p->_packet.variableHeader.topicLength || p->_bytePos == EMC_MAX_TOPIC_LENGTH) { + p->_packet.variableHeader.topic[p->_bytePos] = 0x00; // add c-string delimiter + emc_log_i("Packet variable header topic complete"); + if (p->_packet.fixedHeader.packetType & (HeaderFlag.PUBLISH_QOS1 | HeaderFlag.PUBLISH_QOS2)) { + p->_parse = _varHeaderPacketId1; + } else if (p->_packet.payload.total == 0) { + p->_parse = _fixedHeader; + return ParserResult::packet; + } else { + p->_parse = _payloadPublish; + } + } + return ParserResult::awaitData; +} + +ParserResult Parser::_payloadSuback(Parser* p) { + uint8_t data = p->_data[p->_bytesRead]; + if (data < 0x03 || data == 0x80) { + p->_payloadBuffer[p->_bytePos] = data; + p->_bytePos++; + } else { + p->_parse = _fixedHeader; + emc_log_w("Invalid suback return code"); + return ParserResult::protocolError; + } + if (p->_bytePos == p->_packet.payload.total) { + p->_parse = _fixedHeader; + emc_log_i("Packet complete"); + return ParserResult::packet; + } + return ParserResult::awaitData; +} + +ParserResult Parser::_payloadPublish(Parser* p) { + p->_packet.payload.index += p->_packet.payload.length; + p->_packet.payload.data = &p->_data[p->_bytesRead]; + emc_log_i("payload: index %zu, total %zu, avail %zu/%zu", p->_packet.payload.index, p->_packet.payload.total, p->_len - p->_bytesRead, p->_len); + p->_packet.payload.length = std::min(p->_len - p->_bytesRead, p->_packet.payload.total - p->_packet.payload.index); + p->_bytesRead += p->_packet.payload.length - 1; // compensate for increment in _parse-loop + if (p->_packet.payload.index + p->_packet.payload.length == p->_packet.payload.total) { + p->_parse = _fixedHeader; + } + return ParserResult::packet; +} + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/Parser.h b/lib/espMqttClient/src/Packets/Parser.h new file mode 100644 index 0000000..2f6334e --- /dev/null +++ b/lib/espMqttClient/src/Packets/Parser.h @@ -0,0 +1,100 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include +#include + +#include "../Config.h" +#include "Constants.h" +#include "../Logging.h" +#include "RemainingLength.h" + +namespace espMqttClientInternals { + +struct IncomingPacket { + struct __attribute__((__packed__)) { + MQTTPacketType packetType; + union { + size_t remainingLength; + uint8_t remainingLengthRaw[4]; + } remainingLength; + } fixedHeader; + struct __attribute__((__packed__)) { + uint16_t topicLength; + char topic[EMC_MAX_TOPIC_LENGTH + 1]; // + 1 for c-string delimiter + union { + struct { + uint8_t sessionPresent; + uint8_t returnCode; + } connackVarHeader; + uint16_t packetId; + } fixed; + } variableHeader; + struct { + const uint8_t* data; + size_t length; + size_t index; + size_t total; + } payload; + + uint8_t qos() const; + bool retain() const; + bool dup() const; + void reset(); +}; + +enum class ParserResult : uint8_t { + awaitData, + packet, + protocolError +}; + +class Parser; +typedef ParserResult(*ParserFunc)(Parser*); + +class Parser { + public: + Parser(); + ParserResult parse(const uint8_t* data, size_t len, size_t* bytesRead); + const IncomingPacket& getPacket() const; + void reset(); + + private: + // keep data variables in class to avoid copying on every iteration of the parser + const uint8_t* _data; + size_t _len; + size_t _bytesRead; + size_t _bytePos; + ParserFunc _parse; + IncomingPacket _packet; + uint8_t _payloadBuffer[EMC_PAYLOAD_BUFFER_SIZE]; + + static ParserResult _fixedHeader(Parser* p); + static ParserResult _remainingLengthFixed(Parser* p); + static ParserResult _remainingLengthNone(Parser* p); + static ParserResult _remainingLengthVariable(Parser* p); + + + static ParserResult _varHeaderConnack1(Parser* p); + static ParserResult _varHeaderConnack2(Parser* p); + + static ParserResult _varHeaderPacketId1(Parser* p); + static ParserResult _varHeaderPacketId2(Parser* p); + + static ParserResult _varHeaderTopicLength1(Parser* p); + static ParserResult _varHeaderTopicLength2(Parser* p); + static ParserResult _varHeaderTopic(Parser* p); + + static ParserResult _payloadSuback(Parser* p); + static ParserResult _payloadPublish(Parser* p); +}; + +} // end namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/RemainingLength.cpp b/lib/espMqttClient/src/Packets/RemainingLength.cpp new file mode 100644 index 0000000..d8644a3 --- /dev/null +++ b/lib/espMqttClient/src/Packets/RemainingLength.cpp @@ -0,0 +1,57 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "RemainingLength.h" + +namespace espMqttClientInternals { + +int32_t decodeRemainingLength(const uint8_t* stream) { + uint32_t multiplier = 1; + int32_t remainingLength = 0; + uint8_t currentByte = 0; + uint8_t encodedByte; + + do { + encodedByte = stream[currentByte++]; + remainingLength += (encodedByte & 127) * multiplier; + if (multiplier > 128 * 128 * 128) { + emc_log_e("Malformed Remaining Length"); + return -1; + } + multiplier *= 128; + } while ((encodedByte & 128) != 0); + + return remainingLength; +} + +uint8_t remainingLengthLength(uint32_t remainingLength) { + if (remainingLength < 128) return 1; + if (remainingLength < 16384) return 2; + if (remainingLength < 2097152) return 3; + if (remainingLength > 268435455) return 0; + return 4; +} + +uint8_t encodeRemainingLength(uint32_t remainingLength, uint8_t* destination) { + uint8_t currentByte = 0; + uint8_t bytesNeeded = 0; + + do { + uint8_t encodedByte = remainingLength % 128; + remainingLength /= 128; + if (remainingLength > 0) { + encodedByte = encodedByte | 128; + } + destination[currentByte++] = encodedByte; + bytesNeeded++; + } while (remainingLength > 0); + + return bytesNeeded; +} + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/RemainingLength.h b/lib/espMqttClient/src/Packets/RemainingLength.h new file mode 100644 index 0000000..0b84e23 --- /dev/null +++ b/lib/espMqttClient/src/Packets/RemainingLength.h @@ -0,0 +1,32 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include + +#include "../Logging.h" + +namespace espMqttClientInternals { + +// Calculations are based on non normative comment in section 2.2.3 Remaining Length of the MQTT specification + +// returns decoded length based on input stream +// stream is expected to contain full encoded remaining length +// return -1 on error. +int32_t decodeRemainingLength(const uint8_t* stream); + + +// returns the number of bytes needed to encode the remaining length +uint8_t remainingLengthLength(uint32_t remainingLength); + +// encodes the given remaining length to destination and returns number of bytes used +// destination is expected to be large enough to hold the number of bytes needed +uint8_t encodeRemainingLength(uint32_t remainingLength, uint8_t* destination); + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/String.cpp b/lib/espMqttClient/src/Packets/String.cpp new file mode 100644 index 0000000..c3fe23f --- /dev/null +++ b/lib/espMqttClient/src/Packets/String.cpp @@ -0,0 +1,26 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "String.h" + +namespace espMqttClientInternals { + +size_t encodeString(const char* source, uint8_t* dest) { + size_t length = strlen(source); + if (length > 65535) { + emc_log_e("String length error"); + return 0; + } + + dest[0] = static_cast(length) >> 8; + dest[1] = static_cast(length) & 0xFF; + memcpy(&dest[2], source, length); + return 2 + length; +} + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Packets/String.h b/lib/espMqttClient/src/Packets/String.h new file mode 100644 index 0000000..7f1e1e8 --- /dev/null +++ b/lib/espMqttClient/src/Packets/String.h @@ -0,0 +1,22 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include // memcpy + +#include "../Logging.h" + +namespace espMqttClientInternals { + +// encodes the given source string into destination and returns number of bytes used +// destination is expected to be large enough to hold the number of bytes needed +size_t encodeString(const char* source, uint8_t* dest); + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/Transport/ClientAsync.cpp b/lib/espMqttClient/src/Transport/ClientAsync.cpp new file mode 100644 index 0000000..35e2276 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientAsync.cpp @@ -0,0 +1,62 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "ClientAsync.h" + +namespace espMqttClientInternals { + +ClientAsync::ClientAsync() +: client() +, availableData(0) +, bufData(nullptr) { + // empty +} + +bool ClientAsync::connect(IPAddress ip, uint16_t port) { + return client.connect(ip, port); +} + +bool ClientAsync::connect(const char* host, uint16_t port) { + return client.connect(host, port); +} + +size_t ClientAsync::write(const uint8_t* buf, size_t size) { + return client.write(reinterpret_cast(buf), size); +} + +int ClientAsync::available() { + return static_cast(availableData); // availableData will never be large enough to cause an overflow +} + +int ClientAsync::read(uint8_t* buf, size_t size) { + size_t willRead = std::min(size, availableData); + memcpy(buf, bufData, std::min(size, availableData)); + if (availableData > size) { + emc_log_w("Buffer is smaller than available data: %zu - %zu", size, availableData); + } + availableData = 0; + return willRead; +} + +void ClientAsync::stop() { + client.close(false); +} + +bool ClientAsync::connected() { + return client.connected(); +} + +bool ClientAsync::disconnected() { + return client.disconnected(); +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientAsync.h b/lib/espMqttClient/src/Transport/ClientAsync.h new file mode 100644 index 0000000..1d24c32 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientAsync.h @@ -0,0 +1,45 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#pragma once + +#if defined(ARDUINO_ARCH_ESP32) + #include "freertos/FreeRTOS.h" + #include +#elif defined(ARDUINO_ARCH_ESP8266) + #include +#endif + +#include "Transport.h" +#include "../Config.h" +#include "../Logging.h" + +namespace espMqttClientInternals { + +class ClientAsync : public Transport { + public: + ClientAsync(); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int available() override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + + AsyncClient client; + size_t availableData; + uint8_t* bufData; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientPosix.cpp b/lib/espMqttClient/src/Transport/ClientPosix.cpp new file mode 100644 index 0000000..511eb9a --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosix.cpp @@ -0,0 +1,98 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "ClientPosix.h" + +#if defined(__linux__) + +namespace espMqttClientInternals { + +ClientPosix::ClientPosix() +: _sockfd(-1) +, _host() { + // empty +} + +ClientPosix::~ClientPosix() { + ClientPosix::stop(); +} + +bool ClientPosix::connect(IPAddress ip, uint16_t port) { + if (connected()) stop(); + + _sockfd = ::socket(AF_INET, SOCK_STREAM, 0); + if (_sockfd < 0) { + emc_log_e("Error %d opening socket", errno); + } + + int flag = 1; + if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) { + emc_log_e("Error %d disabling nagle", errno); + } + + memset(&_host, 0, sizeof(_host)); + _host.sin_family = AF_INET; + _host.sin_addr.s_addr = htonl(uint32_t(ip)); + _host.sin_port = ::htons(port); + + int ret = ::connect(_sockfd, (struct sockaddr *)&_host, sizeof(_host)); + + if (ret < 0) { + emc_log_e("Error connecting: %d - (%d) %s", ret, errno, strerror(errno)); + return false; + } + + emc_log_i("Connected"); + return true; +} + +bool ClientPosix::connect(const char* host, uint16_t port) { + // tbi + (void) host; + (void) port; + return false; +} + +size_t ClientPosix::write(const uint8_t* buf, size_t size) { + return ::send(_sockfd, buf, size, 0); +} + +int ClientPosix::available() { + uint8_t buf[EMC_POSIX_PEEK_SIZE]; + int ret = ::recv(_sockfd, &buf, EMC_POSIX_PEEK_SIZE, MSG_DONTWAIT|MSG_PEEK); + return ret; +} + +int ClientPosix::read(uint8_t* buf, size_t size) { + int ret = ::recv(_sockfd, buf, size, MSG_DONTWAIT); + /* + if (ret < 0) { + emc_log_e("Error reading: %s", strerror(errno)); + } + */ + return ret; +} + +void ClientPosix::stop() { + if (_sockfd >= 0) { + ::close(_sockfd); + _sockfd = -1; + } +} + +bool ClientPosix::connected() { + return _sockfd >= 0; +} + +bool ClientPosix::disconnected() { + return _sockfd < 0; +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientPosix.h b/lib/espMqttClient/src/Transport/ClientPosix.h new file mode 100644 index 0000000..616940b --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosix.h @@ -0,0 +1,52 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(__linux__) + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Transport.h" // includes IPAddress +#include "../Logging.h" + +#ifndef EMC_POSIX_PEEK_SIZE +#define EMC_POSIX_PEEK_SIZE 1500 +#endif + +namespace espMqttClientInternals { + +class ClientPosix : public Transport { + public: + ClientPosix(); + ~ClientPosix(); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int available() override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + + protected: + int _sockfd; + struct sockaddr_in _host; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSecureSync.cpp b/lib/espMqttClient/src/Transport/ClientSecureSync.cpp new file mode 100644 index 0000000..295cfc5 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSecureSync.cpp @@ -0,0 +1,75 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "ClientSecureSync.h" +#include // socket options + +namespace espMqttClientInternals { + +ClientSecureSync::ClientSecureSync() +: client() { + // empty +} + +bool ClientSecureSync::connect(IPAddress ip, uint16_t port) { + bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client.setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure + int val = true; + client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +bool ClientSecureSync::connect(const char* host, uint16_t port) { + bool ret = client.connect(host, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client.setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure + int val = true; + client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +size_t ClientSecureSync::write(const uint8_t* buf, size_t size) { + return client.write(buf, size); +} + +int ClientSecureSync::available() { + return client.available(); +} + +int ClientSecureSync::read(uint8_t* buf, size_t size) { + return client.read(buf, size); +} + +void ClientSecureSync::stop() { + client.stop(); +} + +bool ClientSecureSync::connected() { + return client.connected(); +} + +bool ClientSecureSync::disconnected() { + return !client.connected(); +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSecureSync.h b/lib/espMqttClient/src/Transport/ClientSecureSync.h new file mode 100644 index 0000000..8f8cfa1 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSecureSync.h @@ -0,0 +1,35 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include // includes IPAddress + +#include "Transport.h" + +namespace espMqttClientInternals { + +class ClientSecureSync : public Transport { + public: + ClientSecureSync(); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int available() override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + WiFiClientSecure client; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSync.cpp b/lib/espMqttClient/src/Transport/ClientSync.cpp new file mode 100644 index 0000000..f1078bb --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSync.cpp @@ -0,0 +1,75 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "ClientSync.h" +#include // socket options + +namespace espMqttClientInternals { + +ClientSync::ClientSync(WiFiClient* wiFiClient) +: client(wiFiClient) { + // empty +} + +bool ClientSync::connect(IPAddress ip, uint16_t port) { + bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client->setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here) + int val = true; + client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +bool ClientSync::connect(const char* host, uint16_t port) { + bool ret = client->connect(host, port); // implicit conversion of return code int --> bool + if (ret) { + #if defined(ARDUINO_ARCH_ESP8266) + client->setNoDelay(true); + #elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here) + int val = true; + client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); + #endif + } + return ret; +} + +size_t ClientSync::write(const uint8_t* buf, size_t size) { + return client->write(buf, size); +} + +int ClientSync::available() { + return client->available(); +} + +int ClientSync::read(uint8_t* buf, size_t size) { + return client->read(buf, size); +} + +void ClientSync::stop() { + client->stop(); +} + +bool ClientSync::connected() { + return client->connected(); +} + +bool ClientSync::disconnected() { + return !client->connected(); +} + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSync.h b/lib/espMqttClient/src/Transport/ClientSync.h new file mode 100644 index 0000000..aeb076b --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSync.h @@ -0,0 +1,35 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include // includes IPAddress + +#include "Transport.h" + +namespace espMqttClientInternals { + +class ClientSync : public Transport { + public: + ClientSync(WiFiClient* wiFiClient); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int available() override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + WiFiClient* client; +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSyncEthernet.cpp b/lib/espMqttClient/src/Transport/ClientSyncEthernet.cpp new file mode 100644 index 0000000..b4ab686 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSyncEthernet.cpp @@ -0,0 +1,79 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "ClientSyncEthernet.h" +#include // socket options + +namespace espMqttClientInternals { + + ClientSyncEthernet::ClientSyncEthernet(EthernetClient* ethernetClient) + : client(ethernetClient) { + // empty + } + + bool ClientSyncEthernet::connect(IPAddress ip, uint16_t port) { + bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool + if (ret) { +#if defined(ARDUINO_ARCH_ESP8266) + client->setNoDelay(true); +#elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here) + int val = true; + + // TODO +// client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); +#endif + } + return ret; + } + + bool ClientSyncEthernet::connect(const char* host, uint16_t port) { + bool ret = client->connect(host, port); // implicit conversion of return code int --> bool + if (ret) { +#if defined(ARDUINO_ARCH_ESP8266) + client->setNoDelay(true); +#elif defined(ARDUINO_ARCH_ESP32) + // Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here) + int val = true; + + // TODO +// client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int)); +#endif + } + return ret; + } + + size_t ClientSyncEthernet::write(const uint8_t* buf, size_t size) { + return client->write(buf, size); + } + + int ClientSyncEthernet::available() { + return client->available(); + } + + int ClientSyncEthernet::read(uint8_t* buf, size_t size) { + return client->read(buf, size); + } + + void ClientSyncEthernet::stop() { + client->stop(); + } + + bool ClientSyncEthernet::connected() { + return client->connected(); + } + + bool ClientSyncEthernet::disconnected() { + return !client->connected(); + } + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSyncEthernet.h b/lib/espMqttClient/src/Transport/ClientSyncEthernet.h new file mode 100644 index 0000000..f010be7 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSyncEthernet.h @@ -0,0 +1,26 @@ +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "Transport.h" +#include "EthernetClient.h" + +namespace espMqttClientInternals { + + class ClientSyncEthernet : public Transport { + public: + ClientSyncEthernet(EthernetClient* ethernetClient); + bool connect(IPAddress ip, uint16_t port) override; + bool connect(const char* host, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int available() override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + EthernetClient* client; + }; + +} // namespace espMqttClientInternals + +#endif \ No newline at end of file diff --git a/lib/espMqttClient/src/Transport/IPAddress.cpp b/lib/espMqttClient/src/Transport/IPAddress.cpp new file mode 100644 index 0000000..b198429 --- /dev/null +++ b/lib/espMqttClient/src/Transport/IPAddress.cpp @@ -0,0 +1,32 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(__linux__) + +#include "IPAddress.h" + +IPAddress::IPAddress() +: _address(0) { + // empty +} + +IPAddress::IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3) +: _address(0) { + _address = (uint32_t)p0 << 24 | (uint32_t)p1 << 16 | (uint32_t)p2 << 8 | p3; +} + +IPAddress::IPAddress(uint32_t address) +: _address(address) { + // empty +} + +IPAddress::operator uint32_t() { + return _address; +} + +#endif diff --git a/lib/espMqttClient/src/Transport/IPAddress.h b/lib/espMqttClient/src/Transport/IPAddress.h new file mode 100644 index 0000000..279a195 --- /dev/null +++ b/lib/espMqttClient/src/Transport/IPAddress.h @@ -0,0 +1,28 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO) + #include +#else + +#include + +class IPAddress { + public: + IPAddress(); + IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3); + explicit IPAddress(uint32_t address); + operator uint32_t(); + + protected: + uint32_t _address; +}; + +#endif diff --git a/lib/espMqttClient/src/Transport/Transport.h b/lib/espMqttClient/src/Transport/Transport.h new file mode 100644 index 0000000..b249e86 --- /dev/null +++ b/lib/espMqttClient/src/Transport/Transport.h @@ -0,0 +1,29 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include // size_t + +#include "IPAddress.h" + +namespace espMqttClientInternals { + +class Transport { + public: + virtual bool connect(IPAddress ip, uint16_t port) = 0; + virtual bool connect(const char* host, uint16_t port) = 0; + virtual size_t write(const uint8_t* buf, size_t size) = 0; + virtual int available() = 0; + virtual int read(uint8_t* buf, size_t size) = 0; + virtual void stop() = 0; + virtual bool connected() = 0; + virtual bool disconnected() = 0; +}; + +} // namespace espMqttClientInternals diff --git a/lib/espMqttClient/src/TypeDefs.cpp b/lib/espMqttClient/src/TypeDefs.cpp new file mode 100644 index 0000000..4f92c1f --- /dev/null +++ b/lib/espMqttClient/src/TypeDefs.cpp @@ -0,0 +1,51 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +Parts are based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "TypeDefs.h" + +namespace espMqttClientTypes { + +const char* disconnectReasonToString(DisconnectReason reason) { + switch (reason) { + case DisconnectReason::USER_OK: return "No error"; + case DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION: return "Unacceptable protocol version"; + case DisconnectReason::MQTT_IDENTIFIER_REJECTED: return "Identified rejected"; + case DisconnectReason::MQTT_SERVER_UNAVAILABLE: return "Server unavailable"; + case DisconnectReason::MQTT_MALFORMED_CREDENTIALS: return "Malformed credentials"; + case DisconnectReason::MQTT_NOT_AUTHORIZED: return "Not authorized"; + case DisconnectReason::TLS_BAD_FINGERPRINT: return "Bad fingerprint"; + case DisconnectReason::TCP_DISCONNECTED: return "TCP disconnected"; + default: return ""; + } +} + +const char* subscribeReturncodeToString(SubscribeReturncode returnCode) { + switch (returnCode) { + case SubscribeReturncode::QOS0: return "QoS 0"; + case SubscribeReturncode::QOS1: return "QoS 1"; + case SubscribeReturncode::QOS2: return "QoS 2"; + case SubscribeReturncode::FAIL: return "Failed"; + default: return ""; + } +} + +const char* errorToString(Error error) { + switch (error) { + case Error::SUCCESS: return "Success"; + case Error::OUT_OF_MEMORY: return "Out of memory"; + case Error::MAX_RETRIES: return "Maximum retries exceeded"; + case Error::MALFORMED_PARAMETER: return "Malformed parameters"; + case Error::MISC_ERROR: return "Misc error"; + default: return ""; + } +} + +} // end namespace espMqttClientTypes diff --git a/lib/espMqttClient/src/TypeDefs.h b/lib/espMqttClient/src/TypeDefs.h new file mode 100644 index 0000000..2e765ad --- /dev/null +++ b/lib/espMqttClient/src/TypeDefs.h @@ -0,0 +1,68 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +Parts are based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#include +#include +#include + +namespace espMqttClientTypes { + +enum class DisconnectReason : uint8_t { + USER_OK = 0, + MQTT_UNACCEPTABLE_PROTOCOL_VERSION = 1, + MQTT_IDENTIFIER_REJECTED = 2, + MQTT_SERVER_UNAVAILABLE = 3, + MQTT_MALFORMED_CREDENTIALS = 4, + MQTT_NOT_AUTHORIZED = 5, + TLS_BAD_FINGERPRINT = 6, + TCP_DISCONNECTED = 7 +}; + +const char* disconnectReasonToString(DisconnectReason reason); + +enum class SubscribeReturncode : uint8_t { + QOS0 = 0x00, + QOS1 = 0x01, + QOS2 = 0x02, + FAIL = 0X80 +}; + +const char* subscribeReturncodeToString(SubscribeReturncode returnCode); + +enum class Error : uint8_t { + SUCCESS = 0, + OUT_OF_MEMORY = 1, + MAX_RETRIES = 2, + MALFORMED_PARAMETER = 3, + MISC_ERROR = 4 +}; + +const char* errorToString(Error error); + +struct MessageProperties { + uint8_t qos; + bool dup; + bool retain; + uint16_t packetId; +}; + +typedef std::function OnConnectCallback; +typedef std::function OnDisconnectCallback; +typedef std::function OnSubscribeCallback; +typedef std::function OnUnsubscribeCallback; +typedef std::function OnMessageCallback; +typedef std::function OnPublishCallback; +typedef std::function PayloadCallback; +typedef std::function OnErrorCallback; + +} // end namespace espMqttClientTypes diff --git a/lib/espMqttClient/src/espMqttClient.cpp b/lib/espMqttClient/src/espMqttClient.cpp new file mode 100644 index 0000000..1a1e0f6 --- /dev/null +++ b/lib/espMqttClient/src/espMqttClient.cpp @@ -0,0 +1,97 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#include "espMqttClient.h" + +#if defined(ARDUINO_ARCH_ESP32) +espMqttClient::espMqttClient(WiFiClient* wiFiClient, uint8_t priority, uint8_t core) +: MqttClientSetup(true, priority, core) +, _client(wiFiClient) { +#else +espMqttClient::espMqttClient() +: _client() { +#endif + _transport = &_client; +} + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP32) +espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core) +: MqttClientSetup(priority, core) +, _client() { +#else +espMqttClientSecure::espMqttClientSecure() +: _client() { +#endif + _transport = &_client; +} + +espMqttClientSecure& espMqttClientSecure::setInsecure() { + _client.client.setInsecure(); + return *this; +} + +#if defined(ARDUINO_ARCH_ESP32) +espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) { + _client.client.setCACert(rootCA); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) { + _client.client.setCertificate(clientCa); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) { + _client.client.setPrivateKey(privateKey); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) { + _client.client.setPreSharedKey(pskIdent, psKey); + return *this; +} +#elif defined(ARDUINO_ARCH_ESP8266) +espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) { + _client.client.setFingerprint(fingerprint); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) { + _client.client.setTrustAnchors(ta); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) { + _client.client.setClientRSACert(cert, sk); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) { + _client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) { + _client.client.setCertStore(certStore); + return *this; +} +#endif + +#endif + +#if defined(ARDUINO_ARCH_ESP32) +espMqttClientEthernet::espMqttClientEthernet(EthernetClient* ethernetClient, uint8_t priority, uint8_t core) + : MqttClientSetup(true, priority, core) + , _client(ethernetClient) { +#else + espMqttClientEthernet::espMqttClientEthernet() +: _client() { +#endif + _transport = &_client; +} diff --git a/lib/espMqttClient/src/espMqttClient.h b/lib/espMqttClient/src/espMqttClient.h new file mode 100644 index 0000000..08affc1 --- /dev/null +++ b/lib/espMqttClient/src/espMqttClient.h @@ -0,0 +1,82 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#include "Transport/ClientSync.h" +#include "Transport/ClientSecureSync.h" +#elif defined(__linux__) +#include "Transport/ClientPosix.h" +#endif + +#include "MqttClientSetup.h" +#include "Transport/ClientSyncEthernet.h" + +class espMqttClient : public MqttClientSetup { +public: +#if defined(ARDUINO_ARCH_ESP32) + explicit espMqttClient(WiFiClient* wiFiClient, uint8_t priority = 1, uint8_t core = 1); +#else + espMqttClient(); +#endif + +protected: +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + espMqttClientInternals::ClientSync _client; +#elif defined(__linux__) + espMqttClientInternals::ClientPosix _client; +#endif +}; + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +class espMqttClientSecure : public MqttClientSetup { +public: +#if defined(ARDUINO_ARCH_ESP32) + explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1); +#else + espMqttClientSecure(); +#endif + espMqttClientSecure& setInsecure(); +#if defined(ARDUINO_ARCH_ESP32) + espMqttClientSecure& setCACert(const char* rootCA); + espMqttClientSecure& setCertificate(const char* clientCa); + espMqttClientSecure& setPrivateKey(const char* privateKey); + espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey); +#else + espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]); + espMqttClientSecure& setTrustAnchors(const X509List *ta); + espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk); + espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type); + espMqttClientSecure& setCertStore(CertStoreBase *certStore); +#endif + +protected: + espMqttClientInternals::ClientSecureSync _client; +}; + +class espMqttClientEthernet : public MqttClientSetup { +public: +#if defined(ARDUINO_ARCH_ESP32) + explicit espMqttClientEthernet(EthernetClient* ethernetClient, uint8_t priority = 1, uint8_t core = 1); +#else + espMqttClient(); +#endif + +protected: +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + espMqttClientInternals::ClientSyncEthernet _client; +#elif defined(__linux__) + espMqttClientInternals::ClientPosix _client; +#endif +}; + +#endif diff --git a/lib/espMqttClient/src/espMqttClientAsync.cpp b/lib/espMqttClient/src/espMqttClientAsync.cpp new file mode 100644 index 0000000..bc53878 --- /dev/null +++ b/lib/espMqttClient/src/espMqttClientAsync.cpp @@ -0,0 +1,68 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "espMqttClientAsync.h" + +#if defined(ARDUINO_ARCH_ESP32) +espMqttClientAsync::espMqttClientAsync(uint8_t priority, uint8_t core) +: MqttClientSetup(false, priority, core) +, _clientAsync() { +#else +espMqttClientAsync::espMqttClientAsync() +: _clientAsync() { +#endif + _transport = &_clientAsync; + // _onConnectHook = reinterpret_cast(_setupClient); + // _onConnectHookArg = this; + _clientAsync.client.onConnect(onConnectCb, this); + _clientAsync.client.onDisconnect(onDisconnectCb, this); + _clientAsync.client.onData(onDataCb, this); + _clientAsync.client.onPoll(onPollCb, this); +} + +bool espMqttClientAsync::connect() { + bool ret = MqttClient::connect(); + loop(); + return ret; +} + +void espMqttClientAsync::_setupClient(espMqttClientAsync* c) { + (void)c; +} + +void espMqttClientAsync::onConnectCb(void* a, AsyncClient* c) { + c->setNoDelay(true); + espMqttClientAsync* client = reinterpret_cast(a); + client->_state = MqttClient::State::connectingTcp2; + client->loop(); +} + +void espMqttClientAsync::onDataCb(void* a, AsyncClient* c, void* data, size_t len) { + (void)c; + espMqttClientAsync* client = reinterpret_cast(a); + client->_clientAsync.bufData = reinterpret_cast(data); + client->_clientAsync.availableData = len; + client->loop(); +} + +void espMqttClientAsync::onDisconnectCb(void* a, AsyncClient* c) { + (void)c; + espMqttClientAsync* client = reinterpret_cast(a); + client->_state = MqttClient::State::disconnectingTcp2; + client->loop(); +} + +void espMqttClientAsync::onPollCb(void* a, AsyncClient* c) { + (void)c; + espMqttClientAsync* client = reinterpret_cast(a); + client->loop(); +} + +#endif diff --git a/lib/espMqttClient/src/espMqttClientAsync.h b/lib/espMqttClient/src/espMqttClientAsync.h new file mode 100644 index 0000000..352e0fd --- /dev/null +++ b/lib/espMqttClient/src/espMqttClientAsync.h @@ -0,0 +1,40 @@ +/* +Copyright (c) 2022 Bert Melis. All rights reserved. + +API is based on the original work of Marvin Roger: +https://github.com/marvinroger/async-mqtt-client + +This work is licensed under the terms of the MIT license. +For a copy, see or +the LICENSE file. +*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include "Transport/ClientAsync.h" + +#include "MqttClientSetup.h" + +class espMqttClientAsync : public MqttClientSetup { + public: +#if defined(ARDUINO_ARCH_ESP32) + explicit espMqttClientAsync(uint8_t priority = 1, uint8_t core = 1); +#else + espMqttClientAsync(); +#endif + bool connect(); + + protected: + espMqttClientInternals::ClientAsync _clientAsync; + static void _setupClient(espMqttClientAsync* c); + static void _disconnectClient(espMqttClientAsync* c); + + static void onConnectCb(void* a, AsyncClient* c); + static void onDataCb(void* a, AsyncClient* c, void* data, size_t len); + static void onDisconnectCb(void* a, AsyncClient* c); + static void onPollCb(void* a, AsyncClient* c); +}; + +#endif diff --git a/lib/espMqttClient/test-coverage.py b/lib/espMqttClient/test-coverage.py new file mode 100644 index 0000000..0d83301 --- /dev/null +++ b/lib/espMqttClient/test-coverage.py @@ -0,0 +1,22 @@ +import os + +Import("env", "projenv") + +# Dump build environment (for debug purpose) +print(env.Dump()) + +# access to global build environment +print(env) + +# access to the project build environment +# (used for source files located in the "src" folder) +print(projenv) + +def generateCoverageInfo(source, target, env): + for file in os.listdir("test"): + os.system(".pio/build/native/program test/"+file) + os.system("lcov -d .pio/build/native/ -c -o lcov.info") + os.system("lcov --remove lcov.info '*Unity*' '*unity*' '/usr/include/*' '*/test/*' -o filtered_lcov.info") + os.system("genhtml -o cov/ --demangle-cpp filtered_lcov.info") + +env.AddPostAction(".pio/build/native/program", generateCoverageInfo) \ No newline at end of file diff --git a/lib/espMqttClient/test/test_client_native/test_client_native.cpp b/lib/espMqttClient/test/test_client_native/test_client_native.cpp new file mode 100644 index 0000000..01a3896 --- /dev/null +++ b/lib/espMqttClient/test/test_client_native/test_client_native.cpp @@ -0,0 +1,313 @@ +#include +#include +#include +#include // espMqttClient for Linux also defines millis() + +void setUp() {} +void tearDown() {} + +espMqttClient mqttClient; +std::atomic_bool exitProgram(false); +std::thread t; + +const IPAddress broker(127,0,0,1); +//const char* broker = "localhost"; +const uint16_t broker_port = 1883; + +/* + +- setup the client with basic settings +- connect to the broker +- successfully connect + +*/ +void test_connect() { + std::atomic onConnectCalledTest(false); + bool sessionPresentTest = true; + mqttClient.setServer(broker, broker_port) + .setCleanSession(true) + .setKeepAlive(5) + .onConnect([&](bool sessionPresent) mutable { + sessionPresentTest = sessionPresent; + onConnectCalledTest = true; + }); + mqttClient.connect(); + uint32_t start = millis(); + while (millis() - start < 2000) { + if (onConnectCalledTest) { + break; + } + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_TRUE(onConnectCalledTest); + TEST_ASSERT_FALSE(sessionPresentTest); +} + +/* + +- keepalive is set at 5 seconds in previous test +- client should stay connected during 2x keepalive period + +*/ + +void test_ping() { + bool pingTest = true; + uint32_t start = millis(); + while (millis() - start < 11000) { + if (mqttClient.disconnected()) { + pingTest = false; + break; + } + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_TRUE(pingTest); +} + +/* + +- client subscribes to topic +- ack is received from broker + +*/ + +void test_subscribe() { + std::atomic subscribeTest(false); + mqttClient.onSubscribe([&](uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* returncodes, size_t len) mutable { + (void) packetId; + if (len == 1 && returncodes[0] == espMqttClientTypes::SubscribeReturncode::QOS0) { + subscribeTest = true; + } + }); + mqttClient.subscribe("test/test", 0); + uint32_t start = millis(); + while (millis() - start < 2000) { + if (subscribeTest) { + break; + } + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_TRUE(subscribeTest); +} + +/* + +- client publishes using all three qos levels +- all publish get packetID returned > 0 (equal to 1 for qos 0) +- 2 pubacks are received + +*/ + +void test_publish() { + std::atomic publishSendTest(0); + mqttClient.onPublish([&](uint16_t packetId) mutable { + (void) packetId; + publishSendTest++; + }); + std::atomic publishReceiveTest(0); + mqttClient.onMessage([&](const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) mutable { + (void) properties; + (void) topic; + (void) payload; + (void) len; + (void) index; + (void) total; + publishReceiveTest++; + }); + uint16_t sendQos0Test = mqttClient.publish("test/test", 0, false, "test0"); + uint16_t sendQos1Test = mqttClient.publish("test/test", 1, false, "test1"); + uint16_t sendQos2Test = mqttClient.publish("test/test", 2, false, "test2"); + uint32_t start = millis(); + while (millis() - start < 6000) { + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_EQUAL_UINT16(1, sendQos0Test); + TEST_ASSERT_GREATER_THAN_UINT16(0, sendQos1Test); + TEST_ASSERT_GREATER_THAN_UINT16(0, sendQos2Test); + TEST_ASSERT_EQUAL_INT(2, publishSendTest); + TEST_ASSERT_EQUAL_INT(3, publishReceiveTest); +} + +void test_publish_empty() { + std::atomic publishSendEmptyTest(0); + mqttClient.onPublish([&](uint16_t packetId) mutable { + (void) packetId; + publishSendEmptyTest++; + }); + std::atomic publishReceiveEmptyTest(0); + mqttClient.onMessage([&](const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) mutable { + (void) properties; + (void) topic; + (void) payload; + (void) len; + (void) index; + (void) total; + publishReceiveEmptyTest++; + }); + uint16_t sendQos0Test = mqttClient.publish("test/test", 0, false, nullptr, 0); + uint16_t sendQos1Test = mqttClient.publish("test/test", 1, false, nullptr, 0); + uint16_t sendQos2Test = mqttClient.publish("test/test", 2, false, nullptr, 0); + uint32_t start = millis(); + while (millis() - start < 6000) { + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_EQUAL_UINT16(1, sendQos0Test); + TEST_ASSERT_GREATER_THAN_UINT16(0, sendQos1Test); + TEST_ASSERT_GREATER_THAN_UINT16(0, sendQos2Test); + TEST_ASSERT_EQUAL_INT(2, publishSendEmptyTest); + TEST_ASSERT_EQUAL_INT(3, publishReceiveEmptyTest); +} + +/* + +- subscribe to test/test, qos 1 +- send to test/test, qos 1 +- check if message is received at least once. + +*/ + +void test_receive1() { + std::atomic publishReceive1Test(0); + mqttClient.onMessage([&](const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) mutable { + (void) properties; + (void) topic; + (void) payload; + (void) len; + (void) index; + (void) total; + publishReceive1Test++; + }); + mqttClient.onSubscribe([&](uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* returncodes, size_t len) mutable { + (void) packetId; + if (len == 1 && returncodes[0] == espMqttClientTypes::SubscribeReturncode::QOS1) { + mqttClient.publish("test/test", 1, false, ""); + } + }); + mqttClient.subscribe("test/test", 1); + uint32_t start = millis(); + while (millis() - start < 6000) { + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_GREATER_THAN_INT(0, publishReceive1Test); +} + +/* + +- subscribe to test/test, qos 2 +- send to test/test, qos 2 +- check if message is received exactly once. + +*/ + +void test_receive2() { + std::atomic publishReceive2Test(0); + mqttClient.onMessage([&](const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) mutable { + (void) properties; + (void) topic; + (void) payload; + (void) len; + (void) index; + (void) total; + publishReceive2Test++; + }); + mqttClient.onSubscribe([&](uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* returncodes, size_t len) mutable { + (void) packetId; + if (len == 1 && returncodes[0] == espMqttClientTypes::SubscribeReturncode::QOS2) { + mqttClient.publish("test/test", 2, false, ""); + } + }); + mqttClient.subscribe("test/test", 2); + uint32_t start = millis(); + while (millis() - start < 6000) { + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_EQUAL_INT(1, publishReceive2Test); +} + + +/* + +- client unsibscribes from topic + +*/ + +void test_unsubscribe() { + std::atomic unsubscribeTest(false); + mqttClient.onUnsubscribe([&](uint16_t packetId) mutable { + (void) packetId; + unsubscribeTest = true; + }); + mqttClient.unsubscribe("test/test"); + uint32_t start = millis(); + while (millis() - start < 2000) { + if (unsubscribeTest) { + break; + } + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(mqttClient.connected()); + TEST_ASSERT_TRUE(unsubscribeTest); +} + +/* + +- client disconnects cleanly + +*/ + +void test_disconnect() { + std::atomic onDisconnectCalled(false); + espMqttClientTypes::DisconnectReason reasonTest = espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED; + mqttClient.onDisconnect([&](espMqttClientTypes::DisconnectReason reason) mutable { + reasonTest = reason; + onDisconnectCalled = true; + }); + mqttClient.disconnect(); + uint32_t start = millis(); + while (millis() - start < 2000) { + if (onDisconnectCalled) { + break; + } + std::this_thread::yield(); + } + + TEST_ASSERT_TRUE(onDisconnectCalled); + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::DisconnectReason::USER_OK, reasonTest); + TEST_ASSERT_TRUE(mqttClient.disconnected()); +} + +int main() { + UNITY_BEGIN(); + t = std::thread([] { + while (1) { + mqttClient.loop(); + if (exitProgram) break; + } + }); + RUN_TEST(test_connect); + RUN_TEST(test_ping); + RUN_TEST(test_subscribe); + RUN_TEST(test_publish); + RUN_TEST(test_publish_empty); + RUN_TEST(test_receive1); + RUN_TEST(test_receive2); + RUN_TEST(test_unsubscribe); + RUN_TEST(test_disconnect); + exitProgram = true; + t.join(); + return UNITY_END(); +} diff --git a/lib/espMqttClient/test/test_outbox/test_outbox.cpp b/lib/espMqttClient/test/test_outbox/test_outbox.cpp new file mode 100644 index 0000000..0d6c337 --- /dev/null +++ b/lib/espMqttClient/test/test_outbox/test_outbox.cpp @@ -0,0 +1,171 @@ +#include + +#include + +using espMqttClientInternals::Outbox; + +void setUp() {} +void tearDown() {} + +void test_outbox_create() { + Outbox outbox; + Outbox::Iterator it = outbox.front(); + TEST_ASSERT_NULL(outbox.getCurrent()); + TEST_ASSERT_NULL(it.get()); + TEST_ASSERT_TRUE(outbox.empty()); +} + +void test_outbox_emplace() { + Outbox outbox; + outbox.emplace(1); + // 1, current points to 1 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(1, *(outbox.getCurrent())); + TEST_ASSERT_FALSE(outbox.empty()); + + outbox.next(); + // 1, current points to nullptr + TEST_ASSERT_NULL(outbox.getCurrent()); + + outbox.emplace(2); + // 1 2, current points to 2 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(2, *(outbox.getCurrent())); + + outbox.emplace(3); + // 1 2 3, current points to 2 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(2, *(outbox.getCurrent())); +} + +void test_outbox_emplaceFront() { + Outbox outbox; + outbox.emplaceFront(1); + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(1, *(outbox.getCurrent())); + + outbox.emplaceFront(2); + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(2, *(outbox.getCurrent())); +} + +void test_outbox_remove1() { + Outbox outbox; + Outbox::Iterator it; + outbox.emplace(1); + outbox.emplace(2); + outbox.emplace(3); + outbox.emplace(4); + outbox.next(); + outbox.next(); + it = outbox.front(); + ++it; + ++it; + ++it; + ++it; + outbox.remove(it); + // 1 2 3 4, it points to nullptr, current points to 3 + TEST_ASSERT_NULL(it.get()); + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(3, *(outbox.getCurrent())); + + it = outbox.front(); + ++it; + ++it; + ++it; + outbox.remove(it); + // 1 2 3, it points to nullptr, current points to 3 + TEST_ASSERT_NULL(it.get()); + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(3, *(outbox.getCurrent())); + + + it = outbox.front(); + outbox.remove(it); + // 2 3, it points to 2, current points to 3 + TEST_ASSERT_NOT_NULL(it.get()); + TEST_ASSERT_EQUAL_UINT32(2, *(it.get())); + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(3, *(outbox.getCurrent())); + + it = outbox.front(); + outbox.remove(it); + // 3, it points to 3, current points to 3 + TEST_ASSERT_NOT_NULL(it.get()); + TEST_ASSERT_EQUAL_UINT32(3, *(it.get())); + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(3, *(outbox.getCurrent())); + + it = outbox.front(); + outbox.remove(it); + TEST_ASSERT_NULL(it.get()); + TEST_ASSERT_NULL(outbox.getCurrent()); +} + +void test_outbox_remove2() { + Outbox outbox; + Outbox::Iterator it; + outbox.emplace(1); + outbox.emplace(2); + outbox.next(); + outbox.next(); + it = outbox.front(); + // 1 2, current points to nullptr + TEST_ASSERT_NULL(outbox.getCurrent()); + TEST_ASSERT_NOT_NULL(it.get()); + TEST_ASSERT_EQUAL_UINT32(1, *(it.get())); + + ++it; + // 1 2, current points to nullptr + TEST_ASSERT_NOT_NULL(it.get()); + TEST_ASSERT_EQUAL_UINT32(2, *(it.get())); + + outbox.remove(it); + // 1, current points to nullptr + TEST_ASSERT_NULL(outbox.getCurrent()); + TEST_ASSERT_NULL(it.get()); + + it = outbox.front(); + TEST_ASSERT_NOT_NULL(it.get()); + TEST_ASSERT_EQUAL_UINT32(1, *(it.get())); + + outbox.remove(it); + TEST_ASSERT_NULL(it.get()); + TEST_ASSERT_TRUE(outbox.empty()); +} + +void test_outbox_removeCurrent() { + Outbox outbox; + outbox.emplace(1); + outbox.emplace(2); + outbox.emplace(3); + outbox.emplace(4); + outbox.removeCurrent(); + // 2 3 4, current points to 2 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(2, *(outbox.getCurrent())); + + outbox.next(); + outbox.removeCurrent(); + // 2 4, current points to 4 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(4, *(outbox.getCurrent())); + + outbox.removeCurrent(); + // 4, current points to nullptr + TEST_ASSERT_NULL(outbox.getCurrent()); + + // outbox will go out of scope and destructor will be called + // Valgrind should not detect a leak here +} + +int main() { + UNITY_BEGIN(); + RUN_TEST(test_outbox_create); + RUN_TEST(test_outbox_emplace); + RUN_TEST(test_outbox_emplaceFront); + RUN_TEST(test_outbox_remove1); + RUN_TEST(test_outbox_remove2); + RUN_TEST(test_outbox_removeCurrent); + return UNITY_END(); +} diff --git a/lib/espMqttClient/test/test_packets/test_packets.cpp b/lib/espMqttClient/test/test_packets/test_packets.cpp new file mode 100644 index 0000000..3e4c108 --- /dev/null +++ b/lib/espMqttClient/test/test_packets/test_packets.cpp @@ -0,0 +1,714 @@ +#include + +#include + +using espMqttClientInternals::Packet; +using espMqttClientInternals::PacketType; + +void setUp() {} +void tearDown() {} + +void test_encodeConnect0() { + const uint8_t check[] = { + 0b00010000, // header + 0x0F, // remaining length + 0x00,0x04,'M','Q','T','T', // protocol + 0b00000100, // protocol level + 0b00000010, // connect flags + 0x00,0x10, // keepalive (16) + 0x00,0x03,'c','l','i' // client id + }; + const uint32_t length = 17; + + bool cleanSession = true; + const char* username = nullptr; + const char* password = nullptr; + const char* willTopic = nullptr; + bool willRemain = false; + uint8_t willQoS = 0; + const uint8_t* willPayload = nullptr; + uint16_t willPayloadLength = 0; + uint16_t keepalive = 16; + const char* clientId = "cli"; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, + cleanSession, + username, + password, + willTopic, + willRemain, + willQoS, + willPayload, + willPayloadLength, + keepalive, + clientId); + + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.CONNECT, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(0, packet.packetId()); +} + +void test_encodeConnect1() { + const uint8_t check[] = { + 0b00010000, // header + 0x20, // remaining length + 0x00,0x04,'M','Q','T','T', // protocol + 0b00000100, // protocol level + 0b11101110, // connect flags + 0x00,0x10, // keepalive (16) + 0x00,0x03,'c','l','i', // client id + 0x00,0x03,'t','o','p', // will topic + 0x00,0x02,'p','l', // will payload + 0x00,0x02,'u','n', // username + 0x00,0x02,'p','a' // password + }; + const uint32_t length = 34; + + bool cleanSession = true; + const char* username = "un"; + const char* password = "pa"; + const char* willTopic = "top"; + bool willRemain = true; + uint8_t willQoS = 1; + const uint8_t willPayload[] = {'p', 'l'}; + uint16_t willPayloadLength = 2; + uint16_t keepalive = 16; + const char* clientId = "cli"; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, + cleanSession, + username, + password, + willTopic, + willRemain, + willQoS, + willPayload, + willPayloadLength, + keepalive, + clientId); + + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.CONNECT, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(0, packet.packetId()); +} + +void test_encodeConnect2() { + const uint8_t check[] = { + 0b00010000, // header + 0x20, // remaining length + 0x00,0x04,'M','Q','T','T', // protocol + 0b00000100, // protocol level + 0b11110110, // connect flags + 0x00,0x10, // keepalive (16) + 0x00,0x03,'c','l','i', // client id + 0x00,0x03,'t','o','p', // will topic + 0x00,0x02,'p','l', // will payload + 0x00,0x02,'u','n', // username + 0x00,0x02,'p','a' // password + }; + const uint32_t length = 34; + + bool cleanSession = true; + const char* username = "un"; + const char* password = "pa"; + const char* willTopic = "top"; + bool willRemain = true; + uint8_t willQoS = 2; + const uint8_t willPayload[] = {'p', 'l', '\0'}; + uint16_t willPayloadLength = 0; + uint16_t keepalive = 16; + const char* clientId = "cli"; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, + cleanSession, + username, + password, + willTopic, + willRemain, + willQoS, + willPayload, + willPayloadLength, + keepalive, + clientId); + + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.CONNECT, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(0, packet.packetId()); +} + +void test_encodeConnectFail0() { + bool cleanSession = true; + const char* username = nullptr; + const char* password = nullptr; + const char* willTopic = nullptr; + bool willRemain = false; + uint8_t willQoS = 0; + const uint8_t* willPayload = nullptr; + uint16_t willPayloadLength = 0; + uint16_t keepalive = 16; + const char* clientId = ""; + espMqttClientTypes::Error error = espMqttClientTypes::Error::SUCCESS; + + Packet packet(error, + cleanSession, + username, + password, + willTopic, + willRemain, + willQoS, + willPayload, + willPayloadLength, + keepalive, + clientId); + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::MALFORMED_PARAMETER, error); +} + +void test_encodePublish0() { + const uint8_t check[] = { + 0b00110000, // header, dup, qos, retain + 0x09, + 0x00,0x03,'t','o','p', // topic + 0x01,0x02,0x03,0x04 // payload + }; + const uint32_t length = 11; + + const char* topic = "top"; + uint8_t qos = 0; + bool retain = false; + const uint8_t payload[] = {0x01, 0x02, 0x03, 0x04}; + uint16_t payloadLength = 4; + uint16_t packetId = 22; // any value except 0 for testing + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, + packetId, + topic, + payload, + payloadLength, + qos, + retain); + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PUBLISH, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(0, packet.packetId()); + + packet.setDup(); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); +} + +void test_encodePublish1() { + const uint8_t check[] = { + 0b00110011, // header, dup, qos, retain + 0x0B, + 0x00,0x03,'t','o','p', // topic + 0x00,0x16, // packet Id + 0x01,0x02,0x03,0x04 // payload + }; + const uint32_t length = 13; + + const char* topic = "top"; + uint8_t qos = 1; + bool retain = true; + const uint8_t payload[] = {0x01, 0x02, 0x03, 0x04}; + uint16_t payloadLength = 4; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, + packetId, + topic, + payload, + payloadLength, + qos, + retain); + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PUBLISH, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); + + const uint8_t checkDup[] = { + 0b00111011, // header, dup, qos, retain + 0x0B, + 0x00,0x03,'t','o','p', // topic + 0x00,0x16, // packet Id + 0x01,0x02,0x03,0x04 // payload + }; + + packet.setDup(); + TEST_ASSERT_EQUAL_UINT8_ARRAY(checkDup, packet.data(0), length); +} + +void test_encodePublish2() { + const uint8_t check[] = { + 0b00110101, // header, dup, qos, retain + 0x0B, + 0x00,0x03,'t','o','p', // topic + 0x00,0x16, // packet Id + 0x01,0x02,0x03,0x04 // payload + }; + const uint32_t length = 13; + + const char* topic = "top"; + uint8_t qos = 2; + bool retain = true; + const uint8_t payload[] = {0x01, 0x02, 0x03, 0x04}; + uint16_t payloadLength = 4; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, + packetId, + topic, + payload, + payloadLength, + qos, + retain); + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PUBLISH, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); + + const uint8_t checkDup[] = { + 0b00111101, // header, dup, qos, retain + 0x0B, + 0x00,0x03,'t','o','p', // topic + 0x00,0x16, // packet Id + 0x01,0x02,0x03,0x04 // payload + }; + + packet.setDup(); + TEST_ASSERT_EQUAL_UINT8_ARRAY(checkDup, packet.data(0), length); +} + +void test_encodePubAck() { + const uint8_t check[] = { + 0b01000000, // header + 0x02, + 0x00,0x16, // packet Id + }; + const uint32_t length = 4; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, PacketType.PUBACK, packetId); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PUBACK, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodePubRec() { + const uint8_t check[] = { + 0b01010000, // header + 0x02, + 0x00,0x16, // packet Id + }; + const uint32_t length = 4; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, PacketType.PUBREC, packetId); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PUBREC, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodePubRel() { + const uint8_t check[] = { + 0b01100010, // header + 0x02, + 0x00,0x16, // packet Id + }; + const uint32_t length = 4; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, PacketType.PUBREL, packetId); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PUBREL, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodePubComp() { + const uint8_t check[] = { + 0b01110000, // header + 0x02, // remaining length + 0x00,0x16, // packet Id + }; + const uint32_t length = 4; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, PacketType.PUBCOMP, packetId); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PUBCOMP, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodeSubscribe() { + const uint8_t check[] = { + 0b10000010, // header + 0x08, // remaining length + 0x00,0x16, // packet Id + 0x00, 0x03, 'a', '/', 'b', // topic + 0x02 // qos + }; + const uint32_t length = 10; + const char* topic = "a/b"; + uint8_t qos = 2; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, packetId, topic, qos); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.SUBSCRIBE, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodeMultiSubscribe2() { + const uint8_t check[] = { + 0b10000010, // header + 0x0E, // remaining length + 0x00,0x16, // packet Id + 0x00, 0x03, 'a', '/', 'b', // topic1 + 0x01, // qos1 + 0x00, 0x03, 'c', '/', 'd', // topic2 + 0x02 // qos2 + }; + const uint32_t length = 16; + const char* topic1 = "a/b"; + const char* topic2 = "c/d"; + uint8_t qos1 = 1; + uint8_t qos2 = 2; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, packetId, topic1, qos1, topic2, qos2); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.SUBSCRIBE, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodeMultiSubscribe3() { + const uint8_t check[] = { + 0b10000010, // header + 0x14, // remaining length + 0x00,0x16, // packet Id + 0x00, 0x03, 'a', '/', 'b', // topic1 + 0x01, // qos1 + 0x00, 0x03, 'c', '/', 'd', // topic2 + 0x02, // qos2 + 0x00, 0x03, 'e', '/', 'f', // topic3 + 0x00 // qos3 + }; + const uint32_t length = 22; + const char* topic1 = "a/b"; + const char* topic2 = "c/d"; + const char* topic3 = "e/f"; + uint8_t qos1 = 1; + uint8_t qos2 = 2; + uint8_t qos3 = 0; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, packetId, topic1, qos1, topic2, qos2, topic3, qos3); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.SUBSCRIBE, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodeUnsubscribe() { + const uint8_t check[] = { + 0b10100010, // header + 0x07, // remaining length + 0x00,0x16, // packet Id + 0x00, 0x03, 'a', '/', 'b', // topic + }; + const uint32_t length = 9; + const char* topic = "a/b"; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, packetId, topic); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.UNSUBSCRIBE, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodeMultiUnsubscribe2() { + const uint8_t check[] = { + 0b10100010, // header + 0x0C, // remaining length + 0x00,0x16, // packet Id + 0x00, 0x03, 'a', '/', 'b', // topic1 + 0x00, 0x03, 'c', '/', 'd' // topic2 + }; + const uint32_t length = 14; + const char* topic1 = "a/b"; + const char* topic2 = "c/d"; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, packetId, topic1, topic2); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.UNSUBSCRIBE, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodeMultiUnsubscribe3() { + const uint8_t check[] = { + 0b10100010, // header + 0x11, // remaining length + 0x00,0x16, // packet Id + 0x00, 0x03, 'a', '/', 'b', // topic1 + 0x00, 0x03, 'c', '/', 'd', // topic2 + 0x00, 0x03, 'e', '/', 'f', // topic3 + }; + const uint32_t length = 19; + const char* topic1 = "a/b"; + const char* topic2 = "c/d"; + const char* topic3 = "e/f"; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, packetId, topic1, topic2, topic3); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.UNSUBSCRIBE, packet.packetType()); + TEST_ASSERT_FALSE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); +} + +void test_encodePingReq() { + const uint8_t check[] = { + 0b11000000, // header + 0x00 + }; + const uint32_t length = 2; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, PacketType.PINGREQ); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.PINGREQ, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(0, packet.packetId()); +} + +void test_encodeDisconnect() { + const uint8_t check[] = { + 0b11100000, // header + 0x00 + }; + const uint32_t length = 2; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, PacketType.DISCONNECT); + packet.setDup(); // no effect + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(length, packet.size()); + TEST_ASSERT_EQUAL_UINT8(PacketType.DISCONNECT, packet.packetType()); + TEST_ASSERT_TRUE(packet.removable()); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(0), length); + TEST_ASSERT_EQUAL_UINT16(0, packet.packetId()); +} + +size_t getData(uint8_t* dest, size_t len, size_t index) { + (void) index; + static uint8_t i = 1; + memset(dest, i, len); + ++i; + return len; +} + +void test_encodeChunkedPublish() { + const uint8_t check[] = { + 0b00110011, // header, dup, qos, retain + 0xCF, 0x01, // 7 + 200 = (0x4F * 1) & 0x40 + (0x01 * 128) + 0x00,0x03,'t','o','p', // topic + 0x00,0x16 // packet Id + }; + uint8_t payloadChunk[EMC_TX_BUFFER_SIZE] = {}; + memset(payloadChunk, 0x01, EMC_TX_BUFFER_SIZE); + const char* topic = "top"; + uint8_t qos = 1; + bool retain = true; + size_t headerLength = 10; + size_t payloadLength = 200; + size_t size = headerLength + payloadLength; + uint16_t packetId = 22; + espMqttClientTypes::Error error = espMqttClientTypes::Error::MISC_ERROR; + + Packet packet(error, + packetId, + topic, + getData, + payloadLength, + qos, + retain); + + TEST_ASSERT_EQUAL_UINT8(espMqttClientTypes::Error::SUCCESS, error); + TEST_ASSERT_EQUAL_UINT32(size, packet.size()); + TEST_ASSERT_EQUAL_UINT16(packetId, packet.packetId()); + + size_t available = 0; + size_t index = 0; + + // call 'available' before 'data' + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(headerLength + EMC_TX_BUFFER_SIZE, available); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, packet.data(index), headerLength); + + // index == first payload byte + index = headerLength; + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(EMC_TX_BUFFER_SIZE, available); + TEST_ASSERT_EQUAL_UINT8_ARRAY(payloadChunk, packet.data(index), available); + + // index == first payload byte + index = headerLength + 4; + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(EMC_TX_BUFFER_SIZE - 4, available); + TEST_ASSERT_EQUAL_UINT8_ARRAY(payloadChunk, packet.data(index), available); + + // index == last payload byte in first chunk + index = headerLength + EMC_TX_BUFFER_SIZE - 1; + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(1, available); + + // index == first payloadbyte in second chunk + memset(payloadChunk, 0x02, EMC_TX_BUFFER_SIZE); + index = headerLength + EMC_TX_BUFFER_SIZE; + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(EMC_TX_BUFFER_SIZE, available); + TEST_ASSERT_EQUAL_UINT8_ARRAY(payloadChunk, packet.data(index), available); + + memset(payloadChunk, 0x03, EMC_TX_BUFFER_SIZE); + index = headerLength + EMC_TX_BUFFER_SIZE + EMC_TX_BUFFER_SIZE + 10; + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(EMC_TX_BUFFER_SIZE, available); + TEST_ASSERT_EQUAL_UINT8_ARRAY(payloadChunk, packet.data(index), available); + + const uint8_t checkDup[] = { + 0b00111011, // header, dup, qos, retain + 0xCF, 0x01, // 7 + 200 = (0x4F * 0) + (0x01 * 128) + 0x00,0x03,'t','o','p', // topic + 0x00,0x16, // packet Id + }; + + index = 0; + packet.setDup(); + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(headerLength + EMC_TX_BUFFER_SIZE, available); + TEST_ASSERT_EQUAL_UINT8_ARRAY(checkDup, packet.data(index), headerLength); + + memset(payloadChunk, 0x04, EMC_TX_BUFFER_SIZE); + index = headerLength; + available = packet.available(index); + TEST_ASSERT_EQUAL_UINT32(EMC_TX_BUFFER_SIZE, available); + TEST_ASSERT_EQUAL_UINT8_ARRAY(payloadChunk, packet.data(index), available); +} + +int main() { + UNITY_BEGIN(); + RUN_TEST(test_encodeConnect0); + RUN_TEST(test_encodeConnect1); + RUN_TEST(test_encodeConnect2); + RUN_TEST(test_encodeConnectFail0); + RUN_TEST(test_encodePublish0); + RUN_TEST(test_encodePublish1); + RUN_TEST(test_encodePublish2); + RUN_TEST(test_encodePubAck); + RUN_TEST(test_encodePubRec); + RUN_TEST(test_encodePubRel); + RUN_TEST(test_encodePubComp); + RUN_TEST(test_encodeSubscribe); + RUN_TEST(test_encodeMultiSubscribe2); + RUN_TEST(test_encodeMultiSubscribe3); + RUN_TEST(test_encodeUnsubscribe); + RUN_TEST(test_encodeMultiUnsubscribe2); + RUN_TEST(test_encodeMultiUnsubscribe3); + RUN_TEST(test_encodePingReq); + RUN_TEST(test_encodeDisconnect); + RUN_TEST(test_encodeChunkedPublish); + return UNITY_END(); +} diff --git a/lib/espMqttClient/test/test_parser/test_parser.cpp b/lib/espMqttClient/test/test_parser/test_parser.cpp new file mode 100644 index 0000000..ed51f92 --- /dev/null +++ b/lib/espMqttClient/test/test_parser/test_parser.cpp @@ -0,0 +1,355 @@ +#include + +#include + +using espMqttClientInternals::Parser; +using espMqttClientInternals::ParserResult; +using espMqttClientInternals::IncomingPacket; + +void setUp() {} +void tearDown() {} + +Parser parser; + +void test_Connack() { + const uint8_t stream[] = { + 0b00100000, // header + 0b00000010, // flags + 0b00000001, // session present + 0b00000000 // reserved + }; + const size_t length = 4; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(4, bytesRead); + TEST_ASSERT_EQUAL_UINT8(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT8(1, parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_Empty() { + const uint8_t stream[] = { + 0x00 + }; + const size_t length = 0; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_UINT8(ParserResult::awaitData, result); + TEST_ASSERT_EQUAL_INT32(0, bytesRead); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_Header() { + const uint8_t stream[] = { + 0x12, + 0x13, + 0x14 + }; + const size_t length = 3; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::protocolError, result); + TEST_ASSERT_EQUAL_UINT32(1, bytesRead); +} + +void test_Publish() { + uint8_t stream[] = { + 0b00110010, // header + 0x0B, // remaining length + 0x00, 0x03, 'a', '/', 'b', // topic + 0x00, 0x0A, // packet id + 0x01, 0x02 // payload + }; + size_t length = 11; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PUBLISH, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_STRING("a/b", parser.getPacket().variableHeader.topic); + TEST_ASSERT_EQUAL_UINT16(10, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT32(0, parser.getPacket().payload.index); + TEST_ASSERT_EQUAL_UINT32(2, parser.getPacket().payload.length); + TEST_ASSERT_EQUAL_UINT32(4, parser.getPacket().payload.total); + TEST_ASSERT_EQUAL_UINT8(1, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); + + stream[0] = 0x03; + stream[1] = 0x04; + length = 2; + + bytesRead = 0; + result = parser.parse(stream, length, &bytesRead); + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_STRING("a/b", parser.getPacket().variableHeader.topic); + TEST_ASSERT_EQUAL_UINT16(10, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT32(2, parser.getPacket().payload.index); + TEST_ASSERT_EQUAL_UINT32(2, parser.getPacket().payload.length); + TEST_ASSERT_EQUAL_UINT32(4, parser.getPacket().payload.total); + TEST_ASSERT_EQUAL_UINT8(1, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_Publish_empty() { + uint8_t stream0[] = { + 0b00110000, // header + 0x05, // remaining length + 0x00, 0x03, 'a', '/', 'b', // topic + }; + size_t length0 = 7; + + size_t bytesRead0 = 0; + ParserResult result0 = parser.parse(stream0, length0, &bytesRead0); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result0); + TEST_ASSERT_EQUAL_UINT32(length0, bytesRead0); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PUBLISH, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_STRING("a/b", parser.getPacket().variableHeader.topic); + TEST_ASSERT_EQUAL_UINT32(0, parser.getPacket().payload.index); + TEST_ASSERT_EQUAL_UINT32(0, parser.getPacket().payload.length); + TEST_ASSERT_EQUAL_UINT32(0, parser.getPacket().payload.total); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); + + uint8_t stream1[] = { + 0b00110000, // header + 0x05, // remaining length + 0x00, 0x03, 'a', '/', 'b', // topic + }; + size_t length1 = 7; + + size_t bytesRead1 = 0; + ParserResult result1 = parser.parse(stream1, length1, &bytesRead1); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result1); + TEST_ASSERT_EQUAL_UINT32(length1, bytesRead1); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PUBLISH, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_STRING("a/b", parser.getPacket().variableHeader.topic); + TEST_ASSERT_EQUAL_UINT32(0, parser.getPacket().payload.index); + TEST_ASSERT_EQUAL_UINT32(0, parser.getPacket().payload.length); + TEST_ASSERT_EQUAL_UINT32(0, parser.getPacket().payload.total); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); + +} + +void test_PubAck() { + const uint8_t stream[] = { + 0b01000000, + 0b00000010, + 0x12, + 0x34 + }; + const size_t length = 4; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PUBACK, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT16(4660, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_PubRec() { + const uint8_t stream[] = { + 0b01010000, + 0b00000010, + 0x56, + 0x78 + }; + const size_t length = 4; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_BITS(0xF0, espMqttClientInternals::PacketType.PUBREC, parser.getPacket().fixedHeader.packetType); + TEST_ASSERT_EQUAL_UINT16(22136, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_PubRel() { + const uint8_t stream[] = { + 0b01100010, + 0b00000010, + 0x9A, + 0xBC + }; + const size_t length = 4; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PUBREL, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT16(0x9ABC, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_PubComp() { + const uint8_t stream[] = { + 0b01110000, + 0b00000010, + 0xDE, + 0xF0 + }; + const size_t length = 4; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PUBCOMP, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT16(0xDEF0, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_SubAck() { + const uint8_t stream[] = { + 0b10010000, + 0b00000100, + 0x00, + 0x0A, + 0x02, + 0x01 + }; + const size_t length = 6; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.SUBACK, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT16(10, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT8_ARRAY(&stream[4], parser.getPacket().payload.data,2); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_UnsubAck() { + const uint8_t stream[] = { + 0b10110000, + 0b00000010, + 0x00, + 0x0A + }; + const size_t length = 4; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.UNSUBACK, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT16(10, parser.getPacket().variableHeader.fixed.packetId); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + + +void test_PingResp() { + const uint8_t stream[] = { + 0b11010000, + 0x00 + }; + const size_t length = 2; + + size_t bytesRead = 0; + ParserResult result = parser.parse(stream, length, &bytesRead); + + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT32(length, bytesRead); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PINGRESP, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +void test_longStream() { + const uint8_t stream[] = { + 0x90, 0x03, 0x00, 0x01, 0x00, 0x31, 0x0F, 0x00, 0x09, 0x66, 0x6F, 0x6F, 0x2F, 0x62, 0x61, 0x72, + 0x2F, 0x30, 0x74, 0x65, 0x73, 0x74, 0x90, 0x03, 0x00, 0x02, 0x01, 0x33, 0x11, 0x00, 0x09, 0x66, + 0x6F, 0x6F, 0x2F, 0x62, 0x61, 0x72, 0x2F, 0x31, 0x00, 0x01, 0x74, 0x65, 0x73, 0x74, 0x90, 0x03, + 0x00, 0x03, 0x02, 0x30, 0x0F, 0x00, 0x09, 0x66, 0x6F, 0x6F, 0x2F, 0x62, 0x61, 0x72, 0x2F, 0x30, + 0x74, 0x65, 0x73, 0x74, 0x32, 0x11, 0x00, 0x09, 0x66, 0x6F, 0x6F, 0x2F, 0x62, 0x61, 0x72, 0x2F, + 0x31, 0x00, 0x02, 0x74, 0x65, 0x73, 0x74, 0x40, 0x02, 0x00, 0x04, 0x50, 0x02, 0x00, 0x05 + }; + const size_t length = 94; + + size_t bytesRead = 0; + ParserResult result = parser.parse(&stream[bytesRead], length - bytesRead, &bytesRead); + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.SUBACK, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT32(5, bytesRead); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); + + result = parser.parse(&stream[bytesRead], length - bytesRead, &bytesRead); + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.PUBLISH, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT32(5 + 17, bytesRead); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_TRUE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); + + result = parser.parse(&stream[bytesRead], length - bytesRead, &bytesRead); + TEST_ASSERT_EQUAL_INT32(ParserResult::packet, result); + TEST_ASSERT_EQUAL_UINT8(espMqttClientInternals::PacketType.SUBACK, parser.getPacket().fixedHeader.packetType & 0xF0); + TEST_ASSERT_EQUAL_UINT32(5 + 17 + 5, bytesRead); + TEST_ASSERT_EQUAL_UINT8(0, parser.getPacket().qos()); + TEST_ASSERT_FALSE(parser.getPacket().retain()); + TEST_ASSERT_FALSE(parser.getPacket().dup()); +} + +int main() { + UNITY_BEGIN(); + RUN_TEST(test_Connack); + RUN_TEST(test_Empty); + RUN_TEST(test_Header); + RUN_TEST(test_Publish); + RUN_TEST(test_Publish_empty); + RUN_TEST(test_PubAck); + RUN_TEST(test_PubRec); + RUN_TEST(test_PubRel); + RUN_TEST(test_PubComp); + RUN_TEST(test_SubAck); + RUN_TEST(test_UnsubAck); + RUN_TEST(test_PingResp); + RUN_TEST(test_longStream); + return UNITY_END(); +} diff --git a/lib/espMqttClient/test/test_remainingLength/test_remainingLength.cpp b/lib/espMqttClient/test/test_remainingLength/test_remainingLength.cpp new file mode 100644 index 0000000..d422b25 --- /dev/null +++ b/lib/espMqttClient/test/test_remainingLength/test_remainingLength.cpp @@ -0,0 +1,63 @@ +#include + +#include + +#include + +void setUp() {} +void tearDown() {} + +// Examples takes from MQTT specification +uint8_t bytes1[] = {0x40}; +uint8_t size1 = 1; +uint32_t length1 = 64; + +uint8_t bytes2[] = {193, 2}; +uint8_t size2 = 2; +uint32_t length2 = 321; + +uint8_t bytes3[] = {0xff, 0xff, 0xff, 0x7f}; +uint8_t size3 = 4; +uint32_t length3 = 268435455; + +void test_remainingLengthDecode() { + TEST_ASSERT_EQUAL_INT32(length1, espMqttClientInternals::decodeRemainingLength(bytes1)); + TEST_ASSERT_EQUAL_INT32(length2, espMqttClientInternals::decodeRemainingLength(bytes2)); + + uint8_t stream[] = {0x80, 0x80, 0x80, 0x01}; + TEST_ASSERT_EQUAL_INT32(2097152 , espMqttClientInternals::decodeRemainingLength(stream)); + + TEST_ASSERT_EQUAL_INT32(length3, espMqttClientInternals::decodeRemainingLength(bytes3)); +} + +void test_remainingLengthEncode() { + uint8_t bytes[4]; + + TEST_ASSERT_EQUAL_UINT8(1, espMqttClientInternals::remainingLengthLength(0)); + + TEST_ASSERT_EQUAL_UINT8(size1, espMqttClientInternals::remainingLengthLength(length1)); + TEST_ASSERT_EQUAL_UINT8(size1, espMqttClientInternals::encodeRemainingLength(length1, bytes)); + TEST_ASSERT_EQUAL_UINT8_ARRAY(bytes1, bytes, size1); + TEST_ASSERT_EQUAL_UINT8(size2, espMqttClientInternals::remainingLengthLength(length2)); + TEST_ASSERT_EQUAL_UINT8(size2, espMqttClientInternals::encodeRemainingLength(length2, bytes)); + TEST_ASSERT_EQUAL_UINT8_ARRAY(bytes2, bytes, size2); + TEST_ASSERT_EQUAL_UINT8(size3, espMqttClientInternals::remainingLengthLength(length3)); + TEST_ASSERT_EQUAL_UINT8(size3, espMqttClientInternals::encodeRemainingLength(length3, bytes)); + TEST_ASSERT_EQUAL_UINT8_ARRAY(bytes3, bytes, size3); +} + +void test_remainingLengthError() { + uint8_t bytes[] = {0xff, 0xff, 0xff, 0x80}; // high bit of last byte is 1 + // this indicates a next byte is coming + // which is a violation of the spec + TEST_ASSERT_EQUAL_UINT8(0, espMqttClientInternals::remainingLengthLength(268435456)); + TEST_ASSERT_EQUAL_INT32(-1, espMqttClientInternals::decodeRemainingLength(bytes)); +} + +int main() { + UNITY_BEGIN(); + RUN_TEST(test_remainingLengthDecode); + RUN_TEST(test_remainingLengthEncode); + RUN_TEST(test_remainingLengthError); + return UNITY_END(); +} diff --git a/lib/espMqttClient/test/test_string/test_string.cpp b/lib/espMqttClient/test/test_string/test_string.cpp new file mode 100644 index 0000000..a1d67e5 --- /dev/null +++ b/lib/espMqttClient/test/test_string/test_string.cpp @@ -0,0 +1,64 @@ +#include + +#include + +#include + +void setUp() {} +void tearDown() {} + +void test_encodeString() { + const char test[] = "abcd"; + uint8_t buffer[6]; + const uint8_t check[] = {0x00, 0x04, 'a', 'b', 'c', 'd'}; + const uint32_t length = 6; + + TEST_ASSERT_EQUAL_UINT32(length, espMqttClientInternals::encodeString(test, buffer)); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, buffer, length); +} + +void test_emtpyString() { + const char test[] = ""; + uint8_t buffer[2]; + const uint8_t check[] = {0x00, 0x00}; + const uint32_t length = 2; + + TEST_ASSERT_EQUAL_UINT32(length, espMqttClientInternals::encodeString(test, buffer)); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, buffer, length); +} + +void test_longString() { + const size_t maxSize = 65535; + char test[maxSize + 1]; + test[maxSize] = '\0'; + memset(test, 'a', maxSize); + uint8_t buffer[maxSize + 3]; + uint8_t check[maxSize + 2]; + check[0] = 0xFF; + check[1] = 0xFF; + memset(&check[2], 'a', maxSize); + const uint32_t length = 2 + maxSize; + + TEST_ASSERT_EQUAL_UINT32(length, espMqttClientInternals::encodeString(test, buffer)); + TEST_ASSERT_EQUAL_UINT8_ARRAY(check, buffer, length); +} + +void test_tooLongString() { + const size_t maxSize = 65535; + char test[maxSize + 2]; + test[maxSize + 1] = '\0'; + memset(test, 'a', maxSize + 1); + uint8_t buffer[maxSize + 4]; // extra 4 bytes for headroom: test progam, don't test test + const uint32_t length = 0; + + TEST_ASSERT_EQUAL_UINT32(length, espMqttClientInternals::encodeString(test, buffer)); +} + +int main() { + UNITY_BEGIN(); + RUN_TEST(test_encodeString); + RUN_TEST(test_emtpyString); + RUN_TEST(test_longString); + RUN_TEST(test_tooLongString); + return UNITY_END(); +} diff --git a/networkDevices/NetworkDevice.h b/networkDevices/NetworkDevice.h index b23637b..7e79da1 100644 --- a/networkDevices/NetworkDevice.h +++ b/networkDevices/NetworkDevice.h @@ -1,6 +1,7 @@ #pragma once #include "MqttClient.h" +#include "MqttClientSetup.h" enum class ReconnectStatus { @@ -16,7 +17,7 @@ public: : _hostname(hostname) {} - virtual MqttClient* mqttClient() = 0; + virtual MqttClientSetup* mqttClient() = 0; virtual void initialize() = 0; virtual ReconnectStatus reconnect() = 0; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index 2970420..cf6f485 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -39,7 +39,7 @@ void W5500Device::initialize() Ethernet.init(ETHERNET_CS_PIN); _ethClient = new EthernetClient(); - _mqttClient = new MqttClient(*_ethClient); + _mqttClient = new espMqttClientEthernet(_ethClient); if(_preferences->getBool(preference_mqtt_log_enabled)) { @@ -142,10 +142,6 @@ void W5500Device::printError() Log->println(ESP.getFreeHeap()); } -MqttClient *W5500Device::mqttClient() -{ - return _mqttClient; -} bool W5500Device::isConnected() { @@ -188,3 +184,9 @@ int8_t W5500Device::signalStrength() { return 127; } + +MqttClientSetup *W5500Device::mqttClient() +{ + return _mqttClient; +} + diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index b17816e..7181ad3 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -1,6 +1,7 @@ #pragma once #include "NetworkDevice.h" +#include "espMqttClient.h" #include #include @@ -21,14 +22,14 @@ public: int8_t signalStrength() override; - virtual MqttClient *mqttClient(); + virtual MqttClientSetup* mqttClient(); private: void resetDevice(); void initializeMacAddress(byte* mac); EthernetClient* _ethClient = nullptr; - MqttClient* _mqttClient = nullptr; + MqttClientSetup* _mqttClient = nullptr; Preferences* _preferences = nullptr; int _maintainResult = 0; diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 844b7f5..bc80adb 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -3,6 +3,7 @@ #include "../PreferencesKeys.h" #include "../Logger.h" #include "../MqttTopics.h" +#include "espMqttClient.h" RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17]; @@ -31,12 +32,13 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) _wifiClientSecure->setCertificate(_cert); _wifiClientSecure->setPrivateKey(_key); } - _mqttClient = new MqttClient(*_wifiClientSecure); + // TODO +// _mqttClient = new espMqttClient(*_wifiClientSecure); } else { Log->println(F("MQTT without TLS.")); _wifiClient = new WiFiClient(); - _mqttClient = new MqttClient(*_wifiClient); + _mqttClient = new espMqttClient(_wifiClient); } if(_preferences->getBool(preference_mqtt_log_enabled)) @@ -51,11 +53,6 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) } } -MqttClient *WifiDevice::mqttClient() -{ - return _mqttClient; -} - void WifiDevice::initialize() { std::vector wm_menu; @@ -152,3 +149,8 @@ void WifiDevice::clearRtcInitVar(WiFiManager *) { memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect); } + +MqttClientSetup *WifiDevice::mqttClient() +{ + return _mqttClient; +} diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 36a4891..3eb5ff0 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -22,7 +22,7 @@ public: int8_t signalStrength() override; - virtual MqttClient* mqttClient(); + MqttClientSetup *mqttClient() override; private: static void clearRtcInitVar(WiFiManager*); @@ -32,7 +32,7 @@ private: WiFiManager _wm; WiFiClient* _wifiClient = nullptr; WiFiClientSecure* _wifiClientSecure = nullptr; - MqttClient* _mqttClient = nullptr; + MqttClientSetup* _mqttClient = nullptr; // SpiffsCookie _cookie; bool _restartOnDisconnect = false; bool _startAp = false;