diff --git a/README.md b/README.md index 7886b0c..dedf3c8 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ After configuring Wi-Fi, the ESP should automatically connect to your network. To configure the connection to the MQTT broker, first connect your client device to the same Wi-Fi network the ESP32 is connected to.
In a browser navigate to the IP address assigned to the ESP32 via DHCP (often found in the web interface of your internet router).

-Next click on "Edit" below "MQTT and Network Configuration" and enter the address and port (usually 1883) of your MQTT broker and a username and a password if required by your MQTT broker.
+Next click on "Edit" below "MQTT Configuration" and enter the address and port (usually 1883) of your MQTT broker and a username and a password if required by your MQTT broker.

The firmware supports SSL encryption for MQTT, however most people and especially home users don't use this.
In that case leave all fields starting with "MQTT SSL" blank. Otherwise see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. @@ -175,38 +175,16 @@ If your ESP32-S3 device has PSRAM but it is not detected please flash the other In a browser navigate to the IP address assigned to the ESP32. -### MQTT and Network Configuration +### Network Configuration -#### Basic MQTT and Network Configuration +#### Network Configuration - Host name: Set the hostname for the Nuki Hub ESP -- MQTT Broker: Set to the IP address of the MQTT broker -- MQTT Broker port: Set to the Port of the MQTT broker (usually 1883) -- MQTT User: If using authentication on the MQTT broker set to a username with read/write rights on the MQTT broker, set to # to clear -- MQTT Password : If using authentication on the MQTT broker set to the password belonging to a username with read/write rights on the MQTT broker, set to # to clear - -#### Advanced MQTT and Network Configuration - -- Home Assistant discovery topic: Set to the Home Assistant auto discovery topic, leave empty to disable auto discovery. Usually "homeassistant" unless you manually changed this setting on the Home Assistant side. -- Home Assistant device configuration URL: When using Home Assistant discovery the link to the Nuki Hub Web Configuration will be published to Home Assistant. By default when this setting is left empty this will link to the current IP of the Nuki Hub. When using a reverse proxy to access the Web Configuration you can set a custom URL here. -- Set Nuki Opener Lock/Unlock action in Home Assistant to Continuous mode (Opener only): By default the lock entity in Home Assistant will enable Ring-to-Open (RTO) when unlocking and disable RTO when locking. By enabling this setting this behaviour will change and now unlocking will enable Continuous Mode and locking will disable Continuous Mode, for more information see the "[Home Assistant Discovery](#home-assistant-discovery-optional)" section of this README. -- MQTT SSL CA Certificate: Optionally set to the CA SSL certificate of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. -- MQTT SSL Client Certificate: Optionally set to the Client SSL certificate of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. -- MQTT SSL Client Key: Optionally set to the Client SSL key of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. - Network hardware: "Wi-Fi only" by default, set to one of the specified ethernet modules if available, see the "Supported Ethernet devices" and "[Connecting via Ethernet](#connecting-via-ethernet-optional)" section of this README. -- Connect to AP with the best signal in an environment with multiple APs with the same SSID: Enable to perform a scan for the Access Point with the best signal strenght for the specified SSID in a multi AP/Mesh environment. +- Home Assistant device configuration URL: When using Home Assistant discovery the link to the Nuki Hub Web Configuration will be published to Home Assistant. By default when this setting is left empty this will link to the current IP of the Nuki Hub. When using a reverse proxy to access the Web Configuration you can set a custom URL here. - RSSI Publish interval: Set to a positive integer to set the amount of seconds between updates to the maintenance/wifiRssi MQTT topic with the current Wi-Fi RSSI, set to -1 to disable, default 60. -- MQTT Timeout until restart: Set to a positive integer to restart the Nuki Hub after the set amount of seconds has passed without an active connection to the MQTT broker, set to -1 to disable, default 60. - Restart on disconnect: Enable to restart the Nuki Hub when disconnected from the network. -- Reconnect network on MQTT connection failure: Enable to force reconnection to the network when connection to the MQTT broker fails (after 15 tries). -- Enable MQTT logging: Enable to fill the maintenance/log MQTT topic with debug log information. -- Enable WebSerial logging : Enable to publish debug log information to `http://NUKIHUBIP:81/webserial`. - Check for Firmware Updates every 24h: Enable to allow the Nuki Hub to check the latest release of the Nuki Hub firmware on boot and every 24 hours. Requires the Nuki Hub to be able to connect to github.com. The latest version will be published to MQTT and will be visible on the main page of the Web Configurator. -- Allow updating using MQTT: Enable to allow starting the Nuki Hub update process using MQTT. Will also enable the Home Assistant update functionality if auto discovery is enabled. -- Disable some extraneous non-JSON topics: Enable to not publish non-JSON keypad and config MQTT topics. -- Enable hybrid official MQTT and Nuki Hub setup: Enable to combine the official MQTT over Thread/Wi-Fi with BLE. Improves speed of state changes. Needs the official MQTT to be setup first. Also requires Nuki Hub to be paired as app and unregistered as a bridge using the Nuki app. See [hybrid mode](/HYBRID.md) -- Enable sending actions through official MQTT: Enable to sent lock actions through the official MQTT topics (e.g. over Thread/Wi-Fi) instead of using BLE. Needs "Enable hybrid official MQTT and Nuki Hub setup" to be enabled. See [hybrid mode](/HYBRID.md) -- Time between status updates when official MQTT is offline (seconds): Set to a positive integer to set the maximum amount of seconds between actively querying the Nuki lock for the current lock state when the official MQTT is offline, default 600. #### IP Address assignment @@ -216,14 +194,37 @@ In a browser navigate to the IP address assigned to the ESP32. - Default gateway: When DHCP is disabled set to the preferred gateway IP address for the Nuki Hub to use - DNS Server: When DHCP is disabled set to the preferred DNS server IP address for the Nuki Hub to use +### MQTT Configuration + +#### Basic MQTT Configuration + +- MQTT Broker: Set to the IP address of the MQTT broker +- MQTT Broker port: Set to the Port of the MQTT broker (usually 1883) +- MQTT User: If using authentication on the MQTT broker set to a username with read/write rights on the MQTT broker, set to # to clear +- MQTT Password : If using authentication on the MQTT broker set to the password belonging to a username with read/write rights on the MQTT broker, set to # to clear +- MQTT NukiHub Path: Set to the preferred MQTT root topic for NukiHub, defaults to "nukihub". Make sure this topic is unique when using multiple ESP32 NukiHub devices + +#### Advanced MQTT Configuration + +- Home Assistant discovery topic: Set to the Home Assistant auto discovery topic, leave empty to disable auto discovery. Usually "homeassistant" unless you manually changed this setting on the Home Assistant side. +- Set Nuki Opener Lock/Unlock action in Home Assistant to Continuous mode (Opener only): By default the lock entity in Home Assistant will enable Ring-to-Open (RTO) when unlocking and disable RTO when locking. By enabling this setting this behaviour will change and now unlocking will enable Continuous Mode and locking will disable Continuous Mode, for more information see the "[Home Assistant Discovery](#home-assistant-discovery-optional)" section of this README. +- MQTT SSL CA Certificate: Optionally set to the CA SSL certificate of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. +- MQTT SSL Client Certificate: Optionally set to the Client SSL certificate of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. +- MQTT SSL Client Key: Optionally set to the Client SSL key of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. +- MQTT Timeout until restart: Set to a positive integer to restart the Nuki Hub after the set amount of seconds has passed without an active connection to the MQTT broker, set to -1 to disable, default 60. +- Enable MQTT logging: Enable to fill the maintenance/log MQTT topic with debug log information. +- Allow updating using MQTT: Enable to allow starting the Nuki Hub update process using MQTT. Will also enable the Home Assistant update functionality if auto discovery is enabled. +- Disable some extraneous non-JSON topics: Enable to not publish non-JSON keypad and config MQTT topics. +- Enable hybrid official MQTT and Nuki Hub setup: Enable to combine the official MQTT over Thread/Wi-Fi with BLE. Improves speed of state changes. Needs the official MQTT to be setup first. Also requires Nuki Hub to be paired as app and unregistered as a bridge using the Nuki app. See [hybrid mode](/HYBRID.md) +- Enable sending actions through official MQTT: Enable to sent lock actions through the official MQTT topics (e.g. over Thread/Wi-Fi) instead of using BLE. Needs "Enable hybrid official MQTT and Nuki Hub setup" to be enabled. See [hybrid mode](/HYBRID.md) +- Time between status updates when official MQTT is offline (seconds): Set to a positive integer to set the maximum amount of seconds between actively querying the Nuki lock for the current lock state when the official MQTT is offline, default 600. + ### Nuki Configuration #### Basic Nuki Configuration - Nuki Smartlock enabled: Enable if you want Nuki Hub to connect to a Nuki Lock (1.0-4.0) -- MQTT Nuki Smartlock Path (Lock only): Set to the preferred MQTT root topic for the Nuki Lock, defaults to "nuki". Make sure this topic is not the same as the setting for the opener and is unique when using multiple Nuki Hub devices (when using multiple Nuki Locks) - Nuki Opener enabled: Enable if you want Nuki Hub to connect to a Nuki Opener -- MQTT Nuki Opener Path (Opener only): Set to the preferred MQTT root topic for the Nuki Opener, defaults to "nukiopener". Make sure this topic is not the same as the setting for the lock and is unique when using multiple Nuki Hub devices (when using multiple Nuki Openers) #### Advanced Nuki Configuration @@ -321,60 +322,60 @@ After importing the device will reboot. ### Opener -- lock/action: Allows to execute lock actions. After receiving the action, the value is set to "ack". Possible actions: activateRTO, deactivateRTO, electricStrikeActuation, activateCM, deactivateCM, fobAction1, fobAction2, fobAction3. -- lock/state: Reports the current lock state as a string. Possible values are: locked, RTOactive, open, opening, uncalibrated. -- lock/hastate: Reports the current lock state as a string, specifically for use by Home Assistant. Possible values are: locking, locked, unlocking, unlocked, jammed. -- lock/json: Reports the lock state, trigger, ring to open timer, current time, time zone offset, last action trigger, last lock action, lock completion status, door sensor state, auth ID and auth name as JSON data. -- lock/binaryState: Reports the current lock state as a string, mostly for use by Home Assistant. Possible values are: locked, unlocked. -- lock/continuousMode: Enable or disable continuous mode on the opener (0 = disabled; 1 = enabled). -- lock/ring: The string "ring" is published to this topic when a doorbell ring is detected while RTO or continuous mode is active or "ringlocked" when both are inactive. -- lock/binaryRing: The string "ring" is published to this topic when a doorbell ring is detected, the state will revert to "standby" after 2 seconds. -- lock/trigger: The trigger of the last action: autoLock, automatic, button, manual, system. -- lock/lastLockAction: Reports the last lock action as a string. Possible values are: ActivateRTO, DeactivateRTO, ElectricStrikeActuation, ActivateCM, DeactivateCM, FobAction1, FobAction2, FobAction3, Unknown. -- lock/log: If "Publish auth data" is enabled in the web interface, this topic will be filled with the log of authorization data. -- lock/completionStatus: Status of the last action as reported by Nuki Opener: success, motorBlocked, canceled, tooRecent, busy, lowMotorVoltage, clutchFailure, motorPowerFailure, incompleteFailure, invalidCode, otherError, unknown. -- lock/authorizationId: If enabled in the web interface, this topic is set to the authorization id of the last lock action. -- lock/authorizationName: If enabled in the web interface, this topic is set to the authorization name of the last lock action. -- lock/commandResult: Result of the last action as reported by Nuki library: success, failed, timeOut, working, notPaired, error, undefined. -- lock/doorSensorState: State of the door sensor: unavailable, deactivated, doorClosed, doorOpened, doorStateUnknown, calibrating. -- lock/rssi: The bluetooth signal strength of the Nuki Lock as measured by the ESP32 and expressed by the RSSI Value in dBm. -- lock/address: The BLE address of the Nuki Lock. -- lock/retry: Reports the current number of retries for the current command. 0 when command is successful, "failed" if the number of retries is greater than the maximum configured number of retries. +- opener/action: Allows to execute lock actions. After receiving the action, the value is set to "ack". Possible actions: activateRTO, deactivateRTO, electricStrikeActuation, activateCM, deactivateCM, fobAction1, fobAction2, fobAction3. +- opener/state: Reports the current lock state as a string. Possible values are: locked, RTOactive, open, opening, uncalibrated. +- opener/hastate: Reports the current lock state as a string, specifically for use by Home Assistant. Possible values are: locking, locked, unlocking, unlocked, jammed. +- opener/json: Reports the lock state, trigger, ring to open timer, current time, time zone offset, last action trigger, last lock action, lock completion status, door sensor state, auth ID and auth name as JSON data. +- opener/binaryState: Reports the current lock state as a string, mostly for use by Home Assistant. Possible values are: locked, unlocked. +- opener/continuousMode: Enable or disable continuous mode on the opener (0 = disabled; 1 = enabled). +- opener/ring: The string "ring" is published to this topic when a doorbell ring is detected while RTO or continuous mode is active or "ringlocked" when both are inactive. +- opener/binaryRing: The string "ring" is published to this topic when a doorbell ring is detected, the state will revert to "standby" after 2 seconds. +- opener/trigger: The trigger of the last action: autoLock, automatic, button, manual, system. +- opener/lastLockAction: Reports the last lock action as a string. Possible values are: ActivateRTO, DeactivateRTO, ElectricStrikeActuation, ActivateCM, DeactivateCM, FobAction1, FobAction2, FobAction3, Unknown. +- opener/log: If "Publish auth data" is enabled in the web interface, this topic will be filled with the log of authorization data. +- opener/completionStatus: Status of the last action as reported by Nuki Opener: success, motorBlocked, canceled, tooRecent, busy, lowMotorVoltage, clutchFailure, motorPowerFailure, incompleteFailure, invalidCode, otherError, unknown. +- opener/authorizationId: If enabled in the web interface, this topic is set to the authorization id of the last lock action. +- opener/authorizationName: If enabled in the web interface, this topic is set to the authorization name of the last lock action. +- opener/commandResult: Result of the last action as reported by Nuki library: success, failed, timeOut, working, notPaired, error, undefined. +- opener/doorSensorState: State of the door sensor: unavailable, deactivated, doorClosed, doorOpened, doorStateUnknown, calibrating. +- opener/rssi: The bluetooth signal strength of the Nuki Lock as measured by the ESP32 and expressed by the RSSI Value in dBm. +- opener/address: The BLE address of the Nuki Lock. +- opener/retry: Reports the current number of retries for the current command. 0 when command is successful, "failed" if the number of retries is greater than the maximum configured number of retries. ### Configuration -- configuration/buttonEnabled: 1 if the Nuki Lock/Opener button is enabled, otherwise 0. -- configuration/ledEnabled: 1 if the Nuki Lock/Opener LED is enabled, otherwise 0. -- configuration/ledBrightness: Set to the brightness of the LED on the Nuki Lock (0=min; 5=max) (Lock only). -- configuration/singleLock: 0 if the Nuki Lock is set to double-lock the door, otherwise 1 (= single-lock) (Lock only). -- configuration/autoLock: 1 if the Nuki Lock is set to Auto Lock, otherwise 0 (Lock only). -- configuration/autoUnlock: 1 if the Nuki Lock is set to Auto Unlock, otherwise 0 (Lock only). -- configuration/soundLevel: Set to the volume for sounds the Nuki Opener plays (0 = min; 255 = max) (Opener only). -- configuration/action: Allows changing configuration settings of the Nuki Lock/Opener using a JSON formatted value. After receiving the action, the value is set to "--". See the "[Changing Nuki Lock/Opener Configuration](#changing-nuki-lockopener-configuration)" section of this README for possible actions/values -- configuration/commandResult: Result of the last configuration change action as JSON data. See the "[Changing Nuki Lock/Opener Configuration](#changing-nuki-lockopener-configuration)" section of this README for possible values -- configuration/basicJson: The current basic configuration of the Nuki Lock/Opener as JSON data. See [Nuki Smart Lock API](https://developer.nuki.io/page/nuki-smart-lock-api-2/2/#heading--set-config) and [Nuki Opener API](https://developer.nuki.io/page/nuki-opener-api-1/7/#heading--set-config) for available settings. Please note: Longitude and Latitude of the Lock/Opener are not published to MQTT by design. These values can still be changed though. -- configuration/advancedJson: The current advanced configuration of the Nuki Lock/Opener as JSON data. See [Nuki Smart Lock API](https://developer.nuki.io/page/nuki-smart-lock-api-2/2/#heading--advanced-config) and [Nuki Opener API](https://developer.nuki.io/page/nuki-opener-api-1/7/#heading--advanced-config) for available settings. +- [lock/opener]/configuration/buttonEnabled: 1 if the Nuki Lock/Opener button is enabled, otherwise 0. +- [lock/opener]/configuration/ledEnabled: 1 if the Nuki Lock/Opener LED is enabled, otherwise 0. +- [lock/opener]/configuration/ledBrightness: Set to the brightness of the LED on the Nuki Lock (0=min; 5=max) (Lock only). +- [lock/opener]/configuration/singleLock: 0 if the Nuki Lock is set to double-lock the door, otherwise 1 (= single-lock) (Lock only). +- [lock/opener]/configuration/autoLock: 1 if the Nuki Lock is set to Auto Lock, otherwise 0 (Lock only). +- [lock/opener]/configuration/autoUnlock: 1 if the Nuki Lock is set to Auto Unlock, otherwise 0 (Lock only). +- [lock/opener]/configuration/soundLevel: Set to the volume for sounds the Nuki Opener plays (0 = min; 255 = max) (Opener only). +- [lock/opener]/configuration/action: Allows changing configuration settings of the Nuki Lock/Opener using a JSON formatted value. After receiving the action, the value is set to "--". See the "[Changing Nuki Lock/Opener Configuration](#changing-nuki-lockopener-configuration)" section of this README for possible actions/values +- [lock/opener]/configuration/commandResult: Result of the last configuration change action as JSON data. See the "[Changing Nuki Lock/Opener Configuration](#changing-nuki-lockopener-configuration)" section of this README for possible values +- [lock/opener]/configuration/basicJson: The current basic configuration of the Nuki Lock/Opener as JSON data. See [Nuki Smart Lock API](https://developer.nuki.io/page/nuki-smart-lock-api-2/2/#heading--set-config) and [Nuki Opener API](https://developer.nuki.io/page/nuki-opener-api-1/7/#heading--set-config) for available settings. Please note: Longitude and Latitude of the Lock/Opener are not published to MQTT by design. These values can still be changed though. +- [lock/opener]/configuration/advancedJson: The current advanced configuration of the Nuki Lock/Opener as JSON data. See [Nuki Smart Lock API](https://developer.nuki.io/page/nuki-smart-lock-api-2/2/#heading--advanced-config) and [Nuki Opener API](https://developer.nuki.io/page/nuki-opener-api-1/7/#heading--advanced-config) for available settings. ### Query -- lock/query/lockstate: Set to 1 to trigger query lockstate. Auto-resets to 0. -- lock/query/config: Set to 1 to trigger query config. Auto-resets to 0. -- lock/query/keypad: Set to 1 to trigger query keypad. Auto-resets to 0. -- lock/query/battery: Set to 1 to trigger query battery. Auto-resets to 0. -- lock/query/lockstateCommandResult: Set to 1 to trigger query lockstate command result. Auto-resets to 0. +- [lock/opener]/query/lockstate: Set to 1 to trigger query lockstate. Auto-resets to 0. +- [lock/opener]/query/config: Set to 1 to trigger query config. Auto-resets to 0. +- [lock/opener]/query/keypad: Set to 1 to trigger query keypad. Auto-resets to 0. +- [lock/opener]/query/battery: Set to 1 to trigger query battery. Auto-resets to 0. +- [lock/opener]/query/lockstateCommandResult: Set to 1 to trigger query lockstate command result. Auto-resets to 0. ### Battery -- battery/level: Battery level in percent (Lock only). -- battery/critical: 1 if battery level is critical, otherwise 0. -- battery/charging: 1 if charging, otherwise 0 (Lock only). -- battery/voltage: Current Battery voltage (V). -- battery/drain: The drain of the last lock action in Milliwattseconds (mWs) (Lock only). -- battery/maxTurnCurrent: The highest current of the turn motor during the last lock action (A) (Lock only). -- battery/lockDistance: The total distance during the last lock action in centidegrees (Lock only). -- battery/keypadCritical: 1 if the battery level of a connected keypad is critical, otherwise 0. -- battery/doorSensorCritical (only available in hybdrid mode): 1 if the battery level of a connected doorsensor is critical, otherwise 0. -- battery/basicJson: The current battery state (critical, charging, level and keypad critical) of the Nuki Lock/Opener as JSON data. -- battery/advancedJson: : The current battery state (critical, batteryDrain, batteryVoltage, lockAction, startVoltage, lowestVoltage, lockDistance, startTemperature, maxTurnCurrent and batteryResistance) of the Nuki Lock/Opener as JSON data. +- [lock/opener]/battery/level: Battery level in percent (Lock only). +- [lock/opener]/battery/critical: 1 if battery level is critical, otherwise 0. +- [lock/opener]/battery/charging: 1 if charging, otherwise 0 (Lock only). +- [lock/opener]/battery/voltage: Current Battery voltage (V). +- [lock/opener]/battery/drain: The drain of the last lock action in Milliwattseconds (mWs) (Lock only). +- [lock/opener]/battery/maxTurnCurrent: The highest current of the turn motor during the last lock action (A) (Lock only). +- [lock/opener]/battery/lockDistance: The total distance during the last lock action in centidegrees (Lock only). +- [lock/opener]/battery/keypadCritical: 1 if the battery level of a connected keypad is critical, otherwise 0. +- [lock/opener]/battery/doorSensorCritical (only available in hybdrid mode): 1 if the battery level of a connected doorsensor is critical, otherwise 0. +- [lock/opener]/battery/basicJson: The current battery state (critical, charging, level and keypad critical) of the Nuki Lock/Opener as JSON data. +- [lock/opener]/battery/advancedJson: : The current battery state (critical, batteryDrain, batteryVoltage, lockAction, startVoltage, lowestVoltage, lockDistance, startTemperature, maxTurnCurrent and batteryResistance) of the Nuki Lock/Opener as JSON data. ### Keypad @@ -580,12 +581,12 @@ NOTE2: Home Assistant can be setup manually using the [MQTT Lock integration](ht If a keypad is connected to the lock, keypad codes can be added, updated and removed. This has to enabled first in the configuration portal. Check "Add, modify and delete keypad codes" under "Access Level Configuration" and save the configuration. -Information about current keypad codes is published as JSON data to the "keypad/json" MQTT topic.
+Information about current keypad codes is published as JSON data to the "[lock/opener]/keypad/json" MQTT topic.
This needs to be enabled separately by checking "Publish keypad codes information" under "Access Level Configuration" and saving the configuration. For security reasons, the code itself is not published, unless this is explicitly enabled in the Nuki Hub settings. By default a maximum of 10 entries are published. -To change Nuki Lock/Opener keypad settings set the `keypad/actionJson` topic to a JSON formatted value containing the following nodes. +To change Nuki Lock/Opener keypad settings set the `[lock/opener]/keypad/actionJson` topic to a JSON formatted value containing the following nodes. | Node | Delete | Add | Update | Usage | Possible values | |------------------|----------|----------|----------|------------------------------------------------------------------------------------------------------------------|----------------------------------------| @@ -608,7 +609,7 @@ Examples: ### Result of attempted keypad code changes -The result of the last keypad change action will be published to the `keypad/commandResultJson` MQTT topic.
+The result of the last keypad change action will be published to the `[lock/opener]/configuration/commandResultJson` MQTT topic.
Possible values are "noValidPinSet", "keypadControlDisabled", "keypadNotAvailable", "keypadDisabled", "invalidConfig", "invalidJson", "noActionSet", "invalidAction", "noExistingCodeIdSet", "noNameSet", "noValidCodeSet", "noCodeSet", "invalidAllowedFrom", "invalidAllowedUntil", "invalidAllowedFromTime", "invalidAllowedUntilTime", "success", "failed", "timeOut", "working", "notPaired", "error" and "undefined".
## Keypad control (alternative, optional) @@ -650,11 +651,11 @@ For example, to add a code: Timecontrol entries can be added, updated and removed. This has to enabled first in the configuration portal. Check "Add, modify and delete timecontrol entries" under "Access Level Configuration" and save the configuration. -Information about current timecontrol entries is published as JSON data to the "timecontrol/json" MQTT topic.
+Information about current timecontrol entries is published as JSON data to the "[lock/opener]/timecontrol/json" MQTT topic.
This needs to be enabled separately by checking "Publish timecontrol entries information" under "Access Level Configuration" and saving the configuration. By default a maximum of 10 entries are published. -To change Nuki Lock/Opener timecontrol settings set the `timecontrol/actionJson` topic to a JSON formatted value containing the following nodes. +To change Nuki Lock/Opener timecontrol settings set the `[lock/opener]/timecontrol/actionJson` topic to a JSON formatted value containing the following nodes. | Node | Delete | Add | Update | Usage | Possible values | |------------------|----------|----------|----------|------------------------------------------------------------------------------------------|----------------------------------------------------------------| @@ -675,12 +676,11 @@ Examples: Authorization entries can be updated and removed. This has to enabled first in the configuration portal. Check "Modify and delete authorization entries" under "Access Level Configuration" and save the configuration. It is currently not (yet) possible to add authorization entries this way. -Information about current authorization entries is published as JSON data to the "authorization/json" MQTT topic.
+Information about current authorization entries is published as JSON data to the "[lock/opener]/authorization/json" MQTT topic.
This needs to be enabled separately by checking "Publish authorization entries information" under "Access Level Configuration" and saving the configuration. By default a maximum of 10 entries are published. -To change Nuki Lock/Opener authorization settings set the `authorization/actionJson` topic to a JSON formatted value containing the following nodes. - +To change Nuki Lock/Opener authorization settings set the `[lock/opener]/authorization/action` topic to a JSON formatted value containing the following nodes. | Node | Delete | Add | Update | Usage | Possible values | |------------------|----------|----------|----------|------------------------------------------------------------------------------------------------------------------|----------------------------------------| diff --git a/clion/CMakeLists.txt b/clion/CMakeLists.txt index 508f2a0..741aea4 100644 --- a/clion/CMakeLists.txt +++ b/clion/CMakeLists.txt @@ -62,6 +62,12 @@ file(GLOB_RECURSE SRCFILESREC lib/ArduinoJson/src/*.hpp lib/PsychicHttp/src/*.cpp lib/PsychicHttp/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(dummy diff --git a/lib/MqttLogger/src/MqttLogger.cpp b/lib/MqttLogger/src/MqttLogger.cpp index 3ac5be3..335168b 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(esp_mqtt_client_handle_t client, const char* topic, MqttLoggerMode mode) +MqttLogger::MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode) { this->setClient(client); this->setTopic(topic); @@ -19,9 +19,9 @@ MqttLogger::~MqttLogger() { } -void MqttLogger::setClient(esp_mqtt_client_handle_t client) +void MqttLogger::setClient(MqttClient& client) { - this->client = client; + this->client = &client; } void MqttLogger::setTopic(const char* topic) @@ -75,10 +75,9 @@ void MqttLogger::sendBuffer() { bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial || this->mode==MqttLoggerMode::MqttAndSerialAndWeb || this->mode==MqttLoggerMode::SerialAndWeb; bool doWebSerial = this->mode==MqttLoggerMode::MqttAndSerialAndWeb || this->mode==MqttLoggerMode::SerialAndWeb; - - if (this->mode!=MqttLoggerMode::SerialOnly && this->mode!=MqttLoggerMode::SerialAndWeb) + if (this->mode!=MqttLoggerMode::SerialOnly && this->mode!=MqttLoggerMode::SerialAndWeb && this->client != NULL && this->client->connected()) { - esp_mqtt_client_publish(this->client, topic, (const char*)this->buffer, this->bufferCnt, 1, 1); + this->client->publish(topic, 0, true, this->buffer, this->bufferCnt); } else if (this->mode == MqttLoggerMode::MqttAndSerialFallback) { diff --git a/lib/MqttLogger/src/MqttLogger.h b/lib/MqttLogger/src/MqttLogger.h index 56dc300..4aa6920 100644 --- a/lib/MqttLogger/src/MqttLogger.h +++ b/lib/MqttLogger/src/MqttLogger.h @@ -11,7 +11,7 @@ #include #include -#include +#include //#include "MycilaWebSerial.h" #define MQTT_MAX_PACKET_SIZE 1024 @@ -33,16 +33,16 @@ private: uint8_t* bufferEnd; uint16_t bufferCnt = 0; uint16_t bufferSize = 0; - esp_mqtt_client_handle_t client; + MqttClient* client; MqttLoggerMode mode; void sendBuffer(); public: MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); - MqttLogger(esp_mqtt_client_handle_t client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); + MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); ~MqttLogger(); - void setClient(esp_mqtt_client_handle_t client); + void setClient(MqttClient& client); void setTopic(const char* topic); void setMode(MqttLoggerMode mode); void setRetained(boolean retained); diff --git a/lib/espMqttClient/.github/ISSUE_TEMPLATE/bug_report.md b/lib/espMqttClient/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..4eeb3bb --- /dev/null +++ b/lib/espMqttClient/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,34 @@ +--- +name: Bug report +about: Create a report to help us improve espMqttClient +title: "[BUG]" +labels: '' +assignees: '' + +--- + +#### Do not use to discuss topics! + +**Describe the bug** +A clear and concise description of what the bug is. + +Which platform, esp8266 or esp32? +Do you use TLS or not? +Do you use an IDE (Arduino, Platformio...)? +Which version of the Arduino framework? + +Please include any debug output and/or decoded stack trace if applicable. + +**Expected behaviour** +A clear and concise description of what you expected to happen. + +**To Reproduce** +Steps to reproduce the behaviour: + +**Example code** +```cpp +// Put code here to reproduce the bug, if possible +``` + +**Additional context** +Add any other context about the problem here. diff --git a/lib/espMqttClient/.github/mosquitto/mosquitto.conf b/lib/espMqttClient/.github/mosquitto/mosquitto.conf new file mode 100644 index 0000000..daa4137 --- /dev/null +++ b/lib/espMqttClient/.github/mosquitto/mosquitto.conf @@ -0,0 +1,2 @@ +listener 1883 +allow_anonymous true \ No newline at end of file diff --git a/lib/espMqttClient/.github/workflows/arduino-lint.yml b/lib/espMqttClient/.github/workflows/arduino-lint.yml new file mode 100644 index 0000000..9960cf4 --- /dev/null +++ b/lib/espMqttClient/.github/workflows/arduino-lint.yml @@ -0,0 +1,9 @@ +name: Arduino Lint + +on: [push, pull_request] +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: arduino/arduino-lint-action@v1 diff --git a/lib/espMqttClient/.github/workflows/build_arduino-ide.yml b/lib/espMqttClient/.github/workflows/build_arduino-ide.yml new file mode 100644 index 0000000..0c06d50 --- /dev/null +++ b/lib/espMqttClient/.github/workflows/build_arduino-ide.yml @@ -0,0 +1,62 @@ +name: Build with Arduino IDE +on: + - push + - pull_request + +jobs: + build-for-esp8266: + runs-on: ubuntu-latest + + strategy: + matrix: + fqbn: + - esp8266:esp8266:generic + + steps: + - uses: actions/checkout@v3 + - uses: arduino/compile-sketches@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + fqbn: ${{ matrix.fqbn }} + enable-deltas-report: true + platforms: | + - name: esp8266:esp8266 + source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json + sketch-paths: | + - examples/largepayload-esp8266 + - examples/simple-esp8266 + - examples/simpleAsync-esp8266 + - examples/tls-esp8266 + libraries: | + - name: espMqttClient + source-path: ./ + - name: ESPAsyncTCP + source-url: https://github.com/me-no-dev/ESPAsyncTCP.git + + build-for-esp32: + runs-on: ubuntu-latest + + strategy: + matrix: + fqbn: + - esp32:esp32:esp32 + + steps: + - uses: actions/checkout@v3 + - uses: arduino/compile-sketches@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + fqbn: ${{ matrix.fqbn }} + enable-deltas-report: true + platforms: | + - name: esp32:esp32 + source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json + sketch-paths: | + - examples/simple-esp32 + - examples/simpleAsync-esp32 + - examples/tls-esp32 + libraries: | + - name: espMqttClient + source-path: ./ + - name: AsyncTCP + source-url: https://github.com/me-no-dev/AsyncTCP.git diff --git a/lib/espMqttClient/.github/workflows/build_platformio.yml b/lib/espMqttClient/.github/workflows/build_platformio.yml new file mode 100644 index 0000000..8344198 --- /dev/null +++ b/lib/espMqttClient/.github/workflows/build_platformio.yml @@ -0,0 +1,78 @@ +name: Build with Platformio + +on: [push, pull_request] + +jobs: + build-for-esp8266: + runs-on: ubuntu-latest + container: ghcr.io/bertmelis/pio-test-container + strategy: + matrix: + example: [ + examples/largepayload-esp8266/largepayload-esp8266.ino, + examples/ota-esp8266/ota-esp8266.ino, + examples/simple-esp8266/simple-esp8266.ino, + examples/simpleAsync-esp8266/simpleAsync-esp8266.ino, + examples/tls-esp8266/tls-esp8266.ino + ] + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v4 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + - name: Download external libraries + run: pio pkg install --global --library me-no-dev/ESPAsyncTCP + - name: Build PlatformIO examples + run: pio ci --lib="." --board=d1_mini + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} + + build-for-esp32: + runs-on: ubuntu-latest + container: ghcr.io/bertmelis/pio-test-container + strategy: + matrix: + example: [ + examples/notask-esp32/notask-esp32.ino, + examples/simple-esp32/simple-esp32.ino, + examples/simpleAsync-esp32/simpleAsync-esp32.ino, + examples/tls-esp32/tls-esp32.ino + ] + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v4 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + - name: Download external libraries + run: pio pkg install --global --library me-no-dev/AsyncTCP + - name: Build PlatformIO examples + run: pio ci --lib="." --board=lolin32 + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} + + build-for-linux: + runs-on: ubuntu-latest + container: ghcr.io/bertmelis/pio-test-container + strategy: + matrix: + example: [ + examples/simple-linux/main.cpp + ] + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v4 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + - name: Build PlatformIO examples + run: pio ci --lib="." --project-conf="./examples/simple-linux/platformio.ini" + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} diff --git a/lib/espMqttClient/.github/workflows/cppcheck.yml b/lib/espMqttClient/.github/workflows/cppcheck.yml new file mode 100644 index 0000000..b9cd80c --- /dev/null +++ b/lib/espMqttClient/.github/workflows/cppcheck.yml @@ -0,0 +1,13 @@ +name: Cppcheck + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + container: ghcr.io/bertmelis/pio-test-container + steps: + - uses: actions/checkout@v4 + - name: Cppcheck + run: | + pio check --fail-on-defect=medium --fail-on-defect=high --flags "--inline-suppr --enable=warning --enable=style --enable=performance --suppress=unusedFunction --suppress=preprocessorErrorDirective" --skip-packages \ No newline at end of file diff --git a/lib/espMqttClient/.github/workflows/cpplint.yml b/lib/espMqttClient/.github/workflows/cpplint.yml new file mode 100644 index 0000000..6df403f --- /dev/null +++ b/lib/espMqttClient/.github/workflows/cpplint.yml @@ -0,0 +1,22 @@ +name: cpplint + +on: [push, pull_request] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install cpplint + - name: Linting + run: | + cpplint --repository=. --recursive --filter=-whitespace/line_length,-build/include ./src \ No newline at end of file diff --git a/lib/espMqttClient/.github/workflows/test_platformio.yml b/lib/espMqttClient/.github/workflows/test_platformio.yml new file mode 100644 index 0000000..eb41511 --- /dev/null +++ b/lib/espMqttClient/.github/workflows/test_platformio.yml @@ -0,0 +1,26 @@ +name: Test with Platformio + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + container: ghcr.io/bertmelis/pio-test-container + + services: + mqtt: + image: eclipse-mosquitto:2.0 + volumes: + - ${{ github.workspace }}/.github/mosquitto:/mosquitto/config + options: --name mqtt + + steps: + - uses: actions/checkout@v4 + - name: Restart MQTT + # Restart MQTT after volumes have been checked out + uses: docker://docker + with: + args: docker restart mqtt + - name: Test + run: | + pio test -e native -v \ No newline at end of file diff --git a/lib/espMqttClient/.gitignore b/lib/espMqttClient/.gitignore new file mode 100644 index 0000000..00649df --- /dev/null +++ b/lib/espMqttClient/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode +cov +*cov.info +.idea diff --git a/lib/espMqttClient/CMakeLists.txt b/lib/espMqttClient/CMakeLists.txt new file mode 100644 index 0000000..e1f4c03 --- /dev/null +++ b/lib/espMqttClient/CMakeLists.txt @@ -0,0 +1,8 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +SET(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(simple-esp32-idf) \ No newline at end of file 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..7b0bfdd --- /dev/null +++ b/lib/espMqttClient/README.md @@ -0,0 +1,61 @@ +# 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 + - Espressif ESP32 using the ESP IDF, see [esp idf component](https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html) +- 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 `ClientPosixIPAddress` class. These are lacking many features needed for proper Linux support. + +## Dependencies + +This libraries requires [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) and [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP). These libraries are not actively maintained and have some bugs. There are alternatives available on Github but make sure these alternatives fit in your project. + +Because of this, I have removed the explicit dependency. You will have to manually add the libraries so you can choose the version which best suites your code. + +# 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/component.mk b/lib/espMqttClient/component.mk new file mode 100644 index 0000000..bb5bb16 --- /dev/null +++ b/lib/espMqttClient/component.mk @@ -0,0 +1,3 @@ +COMPONENT_ADD_INCLUDEDIRS := src +COMPONENT_SRCDIRS := src +CXXFLAGS += -fno-rtti 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..4385ee9 --- /dev/null +++ b/lib/espMqttClient/docs/index.md @@ -0,0 +1,587 @@ +![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 + - Espressif ESP32 using the ESP IDF, see [esp idf component](https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html) +- 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. + +## Dependencies + +This libraries requires [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) and [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP). These libraries are not actively maintained and have some bugs. There are alternatives available on Github but make sure these alternatives fit in your project. + +Because of this, I have removed the explicit dependency. You will have to manually add the libraries so you can choose the version which best suites your code. + +# 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, three optional parameters are available: `espMqttClient(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1)`. By default, espMqttclient creates its own task to manage TCP. By setting `internalTask` to false, no task will be created and you will be responsible yourself to call `espMqttClient.loop()`. `priority` changes 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 + +```cpp +espMqttClient& setTimeout(uint16_t timeout) +``` + +Set the timeout for packets that need acknowledgement. Defaults to 10 seconds. +When no acknowledgement has been received from the broker after sending a packet, the client will retransmit **all** the packets in the queue. + +* **`timeout`**: Timeout in seconds + +#### 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 `true` if the client is currently fully connected to the broker. During connecting or disconnecting, it will return `false`. + +```cpp +bool disconnected() +``` + +Returns `true` if the client is currently disconnected from the broker. During disconnecting or connecting, it will return `false`. + +```cpp +bool connect() +``` + +Start the connect procedure. Returns `true` if successful. A positive return value doesn not mean the client is already connected. + +```cpp +bool disconnect(bool force = false) +``` + +Start the disconnect procedure, return `true` if successful. A positive return value doesn not mean the client is already disconnected. +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(bool deleteSessionData = false) +``` + +Clears all queued messages. +Keep in mind that this may also delete any session data and therefore is not MQTT compliant. + +- **`deleteSessionData`**: When true, delete all outgoing messages. Not 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 you have to call this function yourself **only if you have disabled the internal task** (see the constructors). + +```cpp +const char* getClientId() const +``` + +Retuns the client ID. + +```cpp +size_t queueSize(); +``` + +Returns the amount of elements, regardless of type, in the queue. + +# 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_TX_TIMEOUT 10000 + +Timeout in milliseconds before a (qos > 0) message will be retransmitted. + +### 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. +Regardless of this setting, after you called `disconnect()`, no messages can be published until fully disconnected. + +### EMC_WAIT_FOR_CONNACK 1 + +espMqttClient waits for the CONNACK (connection acknowledge) packet before starting to send other packets. +The MQTT specification allows to start sending before the broker acknowledges the connection but some brokers +don't allow this (AWS for example doesn't). + +### 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 5120 + +Only used on ESP32. Sets the stack size (in words) of the MQTT client worker task. + +### EMC_MULTIPLE_CALLBACKS + +This macro is by default not enabled so you can add a single callbacks to an event. Assigning a second will overwrite the existing callback. When enabling multiple callbacks, multiple callbacks (with uint32_t id) can be assigned. Removing is done by referencing the id. + +### 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. + +### EMC_USE_MEMPOOL 0 + +**Experimental** + +When set to `1`, (outgoing) MQTT packets and the outbox data is stored in a memory pool. The memory pool is part of the espMqttClient object and is thus allocated in the same memory type. There are two pools: one to hold the outgoing packets (dynamic size elements) and one for the outbox itself (fixed-size elements). + +#### EMC_NUM_POOL_ELEMENTS 32 + +This config variable is only used when enabling the memory pool. It defines +- the number of elements in the outbox-pool +- the number of blocks that will be allocated in the packet-pool + +#### EMC_SIZE_POOL_ELEMENTS 128 + +This defines the size of one packet-pool element. Together with `EMC_NUM_POOL_ELEMENTS`, you get the total packet-pool size. +The packet-pool can hold any size of element. The configuration only guarantees a minimum of `EMC_NUM_POOL_ELEMENTS` of size `EMC_SIZE_POOL_ELEMENTS` can fit in the pool. + +### 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); +``` + +### onMessage callbacks per topic + +espMqttClient allows only one callback for incoming messages. You might want to have specific ones per topic. This example shows one way on how to achieve this. + +Limitations of this code sample: only the first match is served and no wildcard topics allowed. + +```cpp +#include +#include + +// definitions of the std::map where we will store the topic/callback combinations +struct MatchTopic { + bool operator()(const char* a, const char* b) const { + return strcmp(a, b) < 0; + } +}; +std::map topicCallbacks; + +// callbacks per topic +void onTopic1(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + // received a packet on topic 1 +} +void onTopic2(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + // received a packet on topic 2 +} + +// general callback to dispatch to specific handlers +void onMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + auto it = topicCallbacks.find(topic); + if (it != topicCallbacks.end()) { + // if found, run specific callback + (it->second)(properties, topic, payload, len, index, total); + } else { + // or handle it here + } +} + +// in your Arduino setup() function: +topicCallbacks.emplace("base/topic1", onTopic1); +topicCallbacks.emplace("base/topic2", onTopic2); + +mqttClient.onMessage(onMessage); +``` 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..f64c9e7 --- /dev/null +++ b/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino @@ -0,0 +1,106 @@ +#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; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +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..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + (void) event; + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + (void) event; + Serial.println("Disconnected from Wi-Fi."); +} + +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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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.setAutoConnect(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + mqttClient.loop(); + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } +} \ No newline at end of file diff --git a/lib/espMqttClient/examples/notask-esp32/notask-esp32.ino b/lib/espMqttClient/examples/notask-esp32/notask-esp32.ino new file mode 100644 index 0000000..867d883 --- /dev/null +++ b/lib/espMqttClient/examples/notask-esp32/notask-esp32.ino @@ -0,0 +1,148 @@ +#include + +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +espMqttClient mqttClient(espMqttClientTypes::UseInternalTask::NO); +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + 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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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.persistent(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } + + // We used to option not to use the internal task + // so we need to call the loop-method ourselves. + // During connecting it may block. + // Creating a separate task yourself is obviously + // also a possibility. + mqttClient.loop(); +} 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..90f1326 --- /dev/null +++ b/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino @@ -0,0 +1,159 @@ +#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; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; +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..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + (void) event; + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + (void) event; + Serial.println("Disconnected from Wi-Fi."); +} + +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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) properties; + 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(); + + WiFi.setAutoConnect(false); + WiFi.setAutoReconnect(true); + 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() { + if (restartFlag) { + Serial.println("Rebooting... See you next time!"); + Serial.flush(); + ESP.reset(); + } + + static uint32_t currentMillis = millis(); + + mqttClient.loop(); + + if (!disconnectFlag && reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } + + if (disconnectFlag) { + // it's safe to call this multiple times + mqttClient.disconnect(); + } +} \ No newline at end of file diff --git a/lib/espMqttClient/examples/simple-esp32-idf/CMakeLists.txt b/lib/espMqttClient/examples/simple-esp32-idf/CMakeLists.txt new file mode 100644 index 0000000..e1f4c03 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp32-idf/CMakeLists.txt @@ -0,0 +1,8 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +SET(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(simple-esp32-idf) \ No newline at end of file diff --git a/lib/espMqttClient/examples/simple-esp32-idf/README.md b/lib/espMqttClient/examples/simple-esp32-idf/README.md new file mode 100644 index 0000000..fef6353 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp32-idf/README.md @@ -0,0 +1,3 @@ +This example is for use with [Arduino as a component](https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/esp-idf_component.html) in the ESP-IDF framework. + +Be sure to follow [this section](https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/esp-idf_component.html#adding-local-library) about adding libraries to your project. diff --git a/lib/espMqttClient/examples/simple-esp32-idf/main/CMakeLists.txt b/lib/espMqttClient/examples/simple-esp32-idf/main/CMakeLists.txt new file mode 100644 index 0000000..475d0f7 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp32-idf/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register( + SRCS "main.cpp" + INCLUDE_DIRS "") \ No newline at end of file diff --git a/lib/espMqttClient/examples/simple-esp32-idf/main/main.cpp b/lib/espMqttClient/examples/simple-esp32-idf/main/main.cpp new file mode 100644 index 0000000..77c8148 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp32-idf/main/main.cpp @@ -0,0 +1,142 @@ +#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; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + 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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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.persistent(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } +} diff --git a/lib/espMqttClient/examples/simple-esp32-idf/sdkconfig.defaults b/lib/espMqttClient/examples/simple-esp32-idf/sdkconfig.defaults new file mode 100644 index 0000000..4661ad2 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp32-idf/sdkconfig.defaults @@ -0,0 +1,39 @@ +# +# Bootloader config +# +CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y +CONFIG_BOOTLOADER_LOG_LEVEL_NONE=y +CONFIG_BOOTLOADER_LOG_LEVEL=0 + +# +# Serial flasher config +# +CONFIG_ESPTOOLPY_FLASHMODE_DIO=y +CONFIG_ESPTOOLPY_FLASHMODE="dio" +CONFIG_ESPTOOLPY_FLASHFREQ_40M=y +CONFIG_ESPTOOLPY_FLASHFREQ="40m" +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_ESPTOOLPY_FLASHSIZE="4MB" +# +# Partition Table +# +CONFIG_PARTITION_TABLE_CUSTOM=n + +# +# Arduino Configuration +# +CONFIG_ARDUINO_VARIANT="esp32" +CONFIG_ENABLE_ARDUINO_DEPENDS=y +CONFIG_AUTOSTART_ARDUINO=y + +# +# FreeRTOS +# +# 1000 require for Arduino +CONFIG_FREERTOS_HZ=1000 + +#ASYNC_TCP +CONFIG_ASYNC_TCP_RUN_NO_AFFINITY=y + +#MBEDTLS +CONFIG_MBEDTLS_PSK_MODES=y \ 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..1a5de37 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino @@ -0,0 +1,141 @@ +#include + +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +espMqttClient mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + 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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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.persistent(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } +} 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..2d54e12 --- /dev/null +++ b/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino @@ -0,0 +1,139 @@ +#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; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + (void) event; + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + (void) event; + Serial.println("Disconnected from Wi-Fi."); +} + +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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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.setAutoConnect(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + mqttClient.loop(); + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } +} \ 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..565336f --- /dev/null +++ b/lib/espMqttClient/examples/simple-linux/platformio.ini @@ -0,0 +1,29 @@ +; 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 + -Werror + +[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..109bcf0 --- /dev/null +++ b/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino @@ -0,0 +1,141 @@ +#include + +#include + +#define WIFI_SSID "yourSSID" +#define WIFI_PASSWORD "yourpass" + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 1883 + +espMqttClientAsync mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + 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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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.persistent(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } +} 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..804caa1 --- /dev/null +++ b/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino @@ -0,0 +1,138 @@ +#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; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + (void) event; + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + (void) event; + Serial.println("Disconnected from Wi-Fi."); +} + +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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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.setAutoConnect(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } +} \ 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..ce75a5a --- /dev/null +++ b/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino @@ -0,0 +1,170 @@ +#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(espMqttClientTypes::UseInternalTask::NO); +static TaskHandle_t taskHandle; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + 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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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 networkingTask() { + for (;;) { + mqttClient.loop(); + } +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + WiFi.persistent(false); + WiFi.setAutoReconnect(true); + 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); + + xTaskCreatePinnedToCore((TaskFunction_t)networkingTask, "mqttclienttask", 5120, nullptr, 1, &taskHandle, 0); + + connectToWiFi(); +} + +void loop() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } + + static uint32_t lastMillis = 0; + if (currentMillis - lastMillis > 5000) { + lastMillis = currentMillis; + Serial.printf("heap: %u\n", ESP.getFreeHeap()); + } + + static uint32_t millisDisconnect = 0; + if (currentMillis - millisDisconnect > 60000) { + millisDisconnect = currentMillis; + 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..f2cb209 --- /dev/null +++ b/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino @@ -0,0 +1,144 @@ +#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; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +void connectToWiFi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +void onWiFiConnect(const WiFiEventStationModeGotIP& event) { + (void) event; + Serial.println("Connected to Wi-Fi."); + connectToMqtt(); +} + +void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) { + (void) event; + Serial.println("Disconnected from Wi-Fi."); +} + +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()) { + reconnectMqtt = true; + lastReconnect = millis(); + } +} + +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) { + (void) payload; + 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.setAutoConnect(false); + WiFi.setAutoReconnect(true); + 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() { + static uint32_t currentMillis = millis(); + + mqttClient.loop(); + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } +} \ No newline at end of file diff --git a/lib/espMqttClient/keywords.txt b/lib/espMqttClient/keywords.txt new file mode 100644 index 0000000..3807482 --- /dev/null +++ b/lib/espMqttClient/keywords.txt @@ -0,0 +1,61 @@ +# 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 +queueSize 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..21c8429 --- /dev/null +++ b/lib/espMqttClient/library.json @@ -0,0 +1,25 @@ +{ + "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.7.0", + "frameworks": "arduino", + "platforms": ["espressif8266", "espressif32"], + "headers": ["espMqttClient.h", "espMqttClientAsync.h"], + "build": + { + "libLDFMode": "deep+" + } +} diff --git a/lib/espMqttClient/library.properties b/lib/espMqttClient/library.properties new file mode 100644 index 0000000..3b906a9 --- /dev/null +++ b/lib/espMqttClient/library.properties @@ -0,0 +1,9 @@ +name=espMqttClient +version=1.7.0 +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..219ed3b --- /dev/null +++ b/lib/espMqttClient/platformio.ini @@ -0,0 +1,43 @@ +; 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 + -ggdb3 + +[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 + -D EMC_MULTIPLE_CALLBACKS=1 + -D EMC_USE_MEMPOOL=1 +;extra_scripts = test-coverage.py +build_type = debug +test_testing_command = + valgrind + --leak-check=full + --show-leak-kinds=all + --track-origins=yes + --error-exitcode=1 + ${platformio.build_dir}/${this.__env__}/program 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..935f7e1 --- /dev/null +++ b/lib/espMqttClient/src/Config.h @@ -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. +*/ + +#pragma once + +#ifndef EMC_TX_TIMEOUT +#define EMC_TX_TIMEOUT 10000 +#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 16384 +#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_WAIT_FOR_CONNACK +#define EMC_WAIT_FOR_CONNACK 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 5120 +#endif + +#ifndef EMC_MULTIPLE_CALLBACKS +#define EMC_MULTIPLE_CALLBACKS 0 +#endif + +#ifndef EMC_USE_WATCHDOG +#define EMC_USE_WATCHDOG 0 +#endif + +#ifndef EMC_USE_MEMPOOL +#define EMC_USE_MEMPOOL 0 +#endif + +#if EMC_USE_MEMPOOL + #ifndef EMC_NUM_POOL_ELEMENTS + #define EMC_NUM_POOL_ELEMENTS 32 + #endif + #ifndef EMC_SIZE_POOL_ELEMENTS + #define EMC_SIZE_POOL_ELEMENTS 128 + #endif +#endif diff --git a/lib/espMqttClient/src/Helpers.h b/lib/espMqttClient/src/Helpers.h new file mode 100644 index 0000000..05ab136 --- /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() vTaskDelay(1) + #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..3ba096a --- /dev/null +++ b/lib/espMqttClient/src/Logging.h @@ -0,0 +1,43 @@ +/* +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 + #include "freertos/FreeRTOS.h" + #include "freertos/task.h" + #if defined(DEBUG_ESP_MQTT_CLIENT) + // Logging is en/disabled by Arduino framework macros + #define emc_log_i(...) log_i(__VA_ARGS__) + #define emc_log_e(...) log_e(__VA_ARGS__) + #define emc_log_w(...) log_w(__VA_ARGS__) + #else + // Logging is disabled + #define emc_log_i(...) + #define emc_log_e(...) + #define emc_log_w(...) + #endif +#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/MemoryPool/LICENSE b/lib/espMqttClient/src/MemoryPool/LICENSE new file mode 100644 index 0000000..526a0c7 --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 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/src/MemoryPool/README.md b/lib/espMqttClient/src/MemoryPool/README.md new file mode 100644 index 0000000..7363e77 --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/README.md @@ -0,0 +1,105 @@ +# Memory Pool + +EARLY VERSION. USE AT OWN RISK. + +### Description + +This is a simple memory pool that doesn't solve the fragmentation problem but contains it. Inside the pool you will still suffer memory fragmentation. The upside is that you're not restricted on memory size. As long as it fits in the pool, you can request any size! + +For applications where the (maximum) size to allocate is known, a simple fixed block size memory pool is available. There is no memory fragmentation happening in this case. The downside is wastage of memory if you need less then the specified blocksize. + +#### Features + +- pool memory is statically allocated +- pool size adjusts on architecture +- no size calculation required: input number of blocks and size of block +- header-only library +- Variable size pool: no restriction on allocated size +- Variable size pool: malloc and free are O(n); The number of allocated blocks affects lookup. +- Fixed size pool: malloc and free are O(1). + +[![Test with Platformio](https://github.com/bertmelis/MemoryPool/actions/workflows/test-platformio.yml/badge.svg)](https://github.com/bertmelis/MemoryPool/actions/workflows/test-platformio.yml) +[![cpplint](https://github.com/bertmelis/MemoryPool/actions/workflows/cpplint.yml/badge.svg)](https://github.com/bertmelis/MemoryPool/actions/workflows/cpplint.yml) + + +### Usage + +#### Variable size pool + +```cpp +#include + +Struct MyStruct { + unsigned int id; + std::size_t size; + unsigned char data[256]; +}; + +// pool will be able to hold 10 blocks the size of MyStruct +MemoryPool::Variable<10, sizeof(MyStruct)> pool; + +// you can allocate the specified blocksize +// allocation is done in number of 'unsigned char' +MyStruct* s = reinterpret_cast(pool.malloc(sizeof(MyStruct))); + +// you can allocate less than the specified blocksize +int* i = reinterpret_cast(pool.malloc(sizeof(int))); + +// you can allocate more than the specified blocksize +unsigned char* m = reinterpret_cast(pool.malloc(400)); + +pool.free(s); +pool.free(i); +pool.free(m); +``` + +#### Fixed size pool + +```cpp +#include + +Struct MyStruct { + unsigned int id; + std::size_t size; + unsigned char data[256]; +}; + +// pool will be able to hold 10 blocks the size of MyStruct +MemoryPool::Fixed<10, sizeof(MyStruct)> pool; + +// there is no size argument in the malloc function! +MyStruct* s = reinterpret_cast(pool.malloc()); + +// you can allocate less than the specified blocksize +int* i = reinterpret_cast(pool.malloc()); + +pool.free(s); +pool.free(i); +``` + +#### How it works + +##### Variable size pool + +Free blocks are organized as a linked list with their header (contains pointer to next and size). An allocated block also has this header with it's pointer set to `nullptr`. Therefore, each allocation wastes memory the size of the header (`sizeof(void*) + sizeof(std::size_t)`). On creation, the pool calculations the needed space to store the number of blocks wich each their header. + +However, memory allocation isn't restricted the the specified blocksize. So in reality, you can allocate more if you allocate larger chunks because less memory blocks means less headers. After all, memory needs to be contiguous. + +If you inspect the pool you'll see that a free pool only has one big block. + +Allocation is linear: the pool is iterated until a suitable spot is found. +Freeing is also linear as the pool is traversed to insert the chunk in the linked list of free blocks + +When freeing, free blocks which are adjacent are combined into one. + +##### Fixed size pool + +The fixed size pool is implemented as an array. Free blocks are saved as a linked list in this array. + +### Bugs and feature requests + +Please use Github's facilities to get in touch. + +### License + +This library is released under the MIT Licence. A copy is included in the repo. diff --git a/lib/espMqttClient/src/MemoryPool/keywords.txt b/lib/espMqttClient/src/MemoryPool/keywords.txt new file mode 100644 index 0000000..08db546 --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/keywords.txt @@ -0,0 +1,16 @@ +# Datatypes (KEYWORD1) +Fixed KEYWORD1 +Variable KEYWORD1 + +# Methods and Functions (KEYWORD2) +malloc KEYWORD2 +free KEYWORD2 +freeMemory KEYWORD2 +maxBlockSize KEYWORD2 +print KEYWORD2 + +# Structures (KEYWORD3) +# structure KEYWORD3 + +# Constants (LITERAL1) +MemoryPool LITERAL1 diff --git a/lib/espMqttClient/src/MemoryPool/library.json b/lib/espMqttClient/src/MemoryPool/library.json new file mode 100644 index 0000000..be2b63d --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/library.json @@ -0,0 +1,21 @@ +{ + "name": "MemoryPool", + "keywords": "memory", + "description": "A simple memory pool for fixed and variable sizes", + "authors": + { + "name": "Bert Melis", + "url": "https://github.com/bertmelis" + }, + "license": "MIT", + "homepage": "https://github.com/bertmelis/MemoryPool", + "repository": + { + "type": "git", + "url": "https://github.com/bertmelis/MemoryPool.git" + }, + "version": "0.1.0", + "frameworks": "*", + "platforms": "*", + "headers": ["MemoryPool.h"] + } \ No newline at end of file diff --git a/lib/espMqttClient/src/MemoryPool/library.properties b/lib/espMqttClient/src/MemoryPool/library.properties new file mode 100644 index 0000000..4c4a443 --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/library.properties @@ -0,0 +1,10 @@ +name=MemoryPool +version=0.1.0 +author=Bert Melis +maintainer=Bert Melis +sentence=A simple memory pool for fixed and variable sizes +paragraph= +category=Other +url=https://github.com/bertmelis/MemoryPool +architectures=* +includes=MemoryPool.h \ No newline at end of file diff --git a/lib/espMqttClient/src/MemoryPool/src/Fixed.h b/lib/espMqttClient/src/MemoryPool/src/Fixed.h new file mode 100644 index 0000000..d976b8f --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/src/Fixed.h @@ -0,0 +1,119 @@ +/* +Copyright (c) 2024 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 // std::size_t +#include // assert +#if _GLIBCXX_HAS_GTHREADS +#include // NOLINT [build/c++11] std::mutex, std::lock_guard +#else +#warning "The memory pool is not thread safe" +#endif + +#ifdef MEMPOL_DEBUG +#include +#endif + +namespace MemoryPool { + +template +class Fixed { + public: + Fixed() // cppcheck-suppress uninitMemberVar + : _buffer{0} + , _head(_buffer) { + unsigned char* b = _head; + std::size_t adjustedBlocksize = sizeof(std::size_t) > blocksize ? sizeof(std::size_t) : blocksize; + for (std::size_t i = 0; i < nrBlocks - 1; ++i) { + *reinterpret_cast(b) = b + adjustedBlocksize; + b += adjustedBlocksize; + } + *reinterpret_cast(b) = nullptr; + } + + // no copy nor move + Fixed (const Fixed&) = delete; + Fixed& operator= (const Fixed&) = delete; + + void* malloc() { + #if _GLIBCXX_HAS_GTHREADS + const std::lock_guard lockGuard(_mutex); + #endif + if (_head) { + void* retVal = _head; + _head = *reinterpret_cast(_head); + return retVal; + } + return nullptr; + } + + void free(void* ptr) { + if (!ptr) return; + #if _GLIBCXX_HAS_GTHREADS + const std::lock_guard lockGuard(_mutex); + #endif + *reinterpret_cast(ptr) = _head; + _head = reinterpret_cast(ptr); + } + + std::size_t freeMemory() { + #if _GLIBCXX_HAS_GTHREADS + const std::lock_guard lockGuard(_mutex); + #endif + unsigned char* i = _head; + std::size_t retVal = 0; + while (i) { + retVal += blocksize; + i = reinterpret_cast(i)[0]; + } + return retVal; + } + + #ifdef MEMPOL_DEBUG + void print() { + std::size_t adjustedBlocksize = sizeof(std::size_t) > blocksize ? sizeof(std::size_t) : blocksize; + std::cout << "+--------------------" << std::endl; + std::cout << "|start:" << reinterpret_cast(_buffer) << std::endl; + std::cout << "|blocks:" << nrBlocks << std::endl; + std::cout << "|blocksize:" << adjustedBlocksize << std::endl; + std::cout << "|head: " << reinterpret_cast(_head) << std::endl; + unsigned char* currentBlock = _buffer; + + for (std::size_t i = 0; i < nrBlocks; ++i) { + std::cout << "|" << i + 1 << ": " << reinterpret_cast(currentBlock) << std::endl; + if (_isFree(currentBlock)) { + std::cout << "| free" << std::endl; + std::cout << "| next: " << reinterpret_cast(*reinterpret_cast(currentBlock)) << std::endl; + } else { + std::cout << "| allocated" << std::endl; + } + currentBlock += adjustedBlocksize; + } + std::cout << "+--------------------" << std::endl; + } + + bool _isFree(const unsigned char* ptr) { + unsigned char* b = _head; + while (b) { + if (b == ptr) return true; + b = *reinterpret_cast(b); + } + return false; + } + #endif + + private: + unsigned char _buffer[nrBlocks * (sizeof(std::size_t) > blocksize ? sizeof(std::size_t) : blocksize)]; + unsigned char* _head; + #if _GLIBCXX_HAS_GTHREADS + std::mutex _mutex; + #endif +}; + +} // end namespace MemoryPool diff --git a/lib/espMqttClient/src/MemoryPool/src/MemoryPool.h b/lib/espMqttClient/src/MemoryPool/src/MemoryPool.h new file mode 100644 index 0000000..af036fc --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/src/MemoryPool.h @@ -0,0 +1,12 @@ +/* +Copyright (c) 2024 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 "Variable.h" +#include "Fixed.h" diff --git a/lib/espMqttClient/src/MemoryPool/src/Variable.h b/lib/espMqttClient/src/MemoryPool/src/Variable.h new file mode 100644 index 0000000..11483cc --- /dev/null +++ b/lib/espMqttClient/src/MemoryPool/src/Variable.h @@ -0,0 +1,242 @@ +/* +Copyright (c) 2024 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 // std::size_t +#include // assert +#if _GLIBCXX_HAS_GTHREADS +#include // NOLINT [build/c++11] std::mutex, std::lock_guard +#else +#warning "The memory pool is not thread safe" +#endif + +#ifdef MEMPOL_DEBUG +#include +#endif + +namespace MemoryPool { + +template +class Variable { + public: + Variable() + : _buffer{0} + , _head(nullptr) + #ifdef MEMPOL_DEBUG + , _bufferSize(0) + #endif + { + std::size_t _normBlocksize = blocksize / sizeof(BlockHeader) + ((blocksize % sizeof(BlockHeader)) ? 1 : 0); + size_t nrBlocksToAlloc = nrBlocks * (_normBlocksize + 1); + BlockHeader* h = reinterpret_cast(_buffer); + h->next = nullptr; + h->size = nrBlocksToAlloc; + _head = h; + + #ifdef MEMPOL_DEBUG + _bufferSize = nrBlocksToAlloc; + #endif + } + + // no copy nor move + Variable (const Variable&) = delete; + Variable& operator= (const Variable&) = delete; + + void* malloc(size_t size) { + #if _GLIBCXX_HAS_GTHREADS + const std::lock_guard lockGuard(_mutex); + #endif + if (size == 0) return nullptr; + + size = (size / sizeof(BlockHeader) + (size % sizeof(BlockHeader) != 0)) + 1; // count by BlockHeader size, add 1 for header + + #ifdef MEMPOL_DEBUG + std::cout << "malloc (raw) " << size << std::endl; + std::cout << "malloc (adj) " << size << " - "; + #endif + + BlockHeader* currentBlock = _head; + BlockHeader* previousBlock = nullptr; + void* retVal = nullptr; + + // iterate through linked free blocks + while (currentBlock) { + // consume whole block is size equals required size + if (currentBlock->size == size) { + if (previousBlock) previousBlock->next = currentBlock->next; + break; + + // split block if size is larger and add second part to list of free blocks + } else if (currentBlock->size > size) { + BlockHeader* newBlock = currentBlock + size; + if (previousBlock) previousBlock->next = newBlock; + newBlock->next = currentBlock->next; + newBlock->size = currentBlock->size - size; + currentBlock->next = newBlock; + break; + } + previousBlock = currentBlock; + currentBlock = currentBlock->next; + } + + if (currentBlock) { + if (currentBlock == _head) { + _head = currentBlock->next; + } + currentBlock->size = size; + currentBlock->next = nullptr; // used when freeing memory + retVal = currentBlock + 1; + #ifdef MEMPOL_DEBUG + std::cout << "ok" << std::endl; + #endif + } else { + #ifdef MEMPOL_DEBUG + std::cout << "nok" << std::endl; + #endif + (void)0; + } + + return retVal; + } + + void free(void* ptr) { + if (!ptr) return; + // check if ptr points to region in _buffer + + #ifdef MEMPOL_DEBUG + std::cout << "free " << static_cast(reinterpret_cast(ptr) - 1) << std::endl; + #endif + + #if _GLIBCXX_HAS_GTHREADS + const std::lock_guard lockGuard(_mutex); + #endif + + BlockHeader* toFree = reinterpret_cast(ptr) - 1; + BlockHeader* previous = reinterpret_cast(_buffer); + BlockHeader* next = _head; + + // toFree is the only free block + if (!next) { + _head = toFree; + return; + } + + while (previous) { + if (!next || toFree < next) { + // 1. add block to linked list of free blocks + if (toFree < _head) { + toFree->next = _head; + _head = toFree; + } else { + previous->next = toFree; + toFree->next = next; + } + + // 2. merge with previous if adjacent + if (toFree > _head && toFree == previous + previous->size) { + previous->size += toFree->size; + previous->next = toFree->next; + toFree = previous; // used in next check + } + + // 3. merge with next if adjacent + if (toFree + toFree->size == next) { + toFree->size += next->size; + toFree->next = next->next; + } + + // 4. done + return; + } + previous = next; + next = next->next; + } + } + + std::size_t freeMemory() { + #if _GLIBCXX_HAS_GTHREADS + const std::lock_guard lockGuard(_mutex); + #endif + size_t retVal = 0; + BlockHeader* currentBlock = reinterpret_cast(_head); + + while (currentBlock) { + retVal += currentBlock->size - 1; + currentBlock = currentBlock->next; + } + + return retVal * sizeof(BlockHeader); + } + + std::size_t maxBlockSize() { + #if _GLIBCXX_HAS_GTHREADS + const std::lock_guard lockGuard(_mutex); + #endif + size_t retVal = 0; + BlockHeader* currentBlock = reinterpret_cast(_head); + + while (currentBlock) { + retVal = (currentBlock->size - 1 > retVal) ? currentBlock->size - 1 : retVal; + currentBlock = currentBlock->next; + } + + return retVal * sizeof(BlockHeader); + } + + #ifdef MEMPOL_DEBUG + void print() { + std::cout << "+--------------------" << std::endl; + std::cout << "|start:" << static_cast(_buffer) << std::endl; + std::cout << "|size:" << _bufferSize << std::endl; + std::cout << "|headersize:" << sizeof(BlockHeader) << std::endl; + std::cout << "|head: " << static_cast(_head) << std::endl; + BlockHeader* nextFreeBlock = _head; + BlockHeader* currentBlock = reinterpret_cast(_buffer); + size_t blockNumber = 1; + while (currentBlock < reinterpret_cast(_buffer) + _bufferSize) { + std::cout << "|" << blockNumber << ": " << static_cast(currentBlock) << std::endl; + std::cout << "| " << static_cast(currentBlock->next) << std::endl; + std::cout << "| " << currentBlock->size << std::endl; + if (currentBlock == nextFreeBlock) { + std::cout << "| free" << std::endl; + nextFreeBlock = nextFreeBlock->next; + } else { + std::cout << "| allocated" << std::endl; + } + ++blockNumber; + currentBlock += currentBlock->size; + } + std::cout << "+--------------------" << std::endl; + } + #endif + + private: + struct BlockHeader { + BlockHeader* next; + std::size_t size; + }; + /* + pool size is aligned to sizeof(BlockHeader). + requested blocksize is therefore multiple of blockheader (rounded up) + total size = nr requested blocks * multiplier * blockheadersize + + see constructor for calculation + */ + unsigned char _buffer[(nrBlocks * ((blocksize / sizeof(BlockHeader) + ((blocksize % sizeof(BlockHeader)) ? 1 : 0)) + 1)) * sizeof(BlockHeader)]; + BlockHeader* _head; + #if _GLIBCXX_HAS_GTHREADS + std::mutex _mutex; + #endif + + #ifdef MEMPOL_DEBUG + std::size_t _bufferSize; + #endif +}; + +} // end namespace MemoryPool diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp new file mode 100644 index 0000000..dc21f74 --- /dev/null +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -0,0 +1,746 @@ +/* +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; + +MqttClient::MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority, uint8_t core) +: _useInternalTask(useInternalTask) +, _transport(nullptr) +, _onConnectCallback(nullptr) +, _onDisconnectCallback(nullptr) +, _onSubscribeCallback(nullptr) +, _onUnsubscribeCallback(nullptr) +, _onMessageCallback(nullptr) +, _onPublishCallback(nullptr) +, _onErrorCallback(nullptr) +, _clientId(nullptr) +, _ip() +, _host(nullptr) +, _port(1883) +, _useIp(false) +, _keepAlive(15000) +, _cleanSession(true) +, _username(nullptr) +, _password(nullptr) +, _willTopic(nullptr) +, _willPayload(nullptr) +, _willPayloadLength(0) +, _willQos(0) +, _willRetain(false) +, _timeout(EMC_TX_TIMEOUT) +, _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) && ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO +, _highWaterMark(4294967295) +#endif + { + EMC_GENERATE_CLIENTID(_generatedClientId); +#if defined(ARDUINO_ARCH_ESP32) + _xSemaphore = xSemaphoreCreateMutex(); + EMC_SEMAPHORE_GIVE(); // release before first use + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { + xTaskCreatePinnedToCore((TaskFunction_t)_loop, "mqttclient", EMC_TASK_STACK_SIZE, this, priority, &_taskHandle, core); + } +#else + (void) useInternalTask; + (void) priority; + (void) core; +#endif + _clientId = _generatedClientId; +} + +MqttClient::~MqttClient() { + disconnect(true); + _clearQueue(2); +#if defined(ARDUINO_ARCH_ESP32) + vSemaphoreDelete(_xSemaphore); + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { + #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 = false; + 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)) { + result = true; + _setState(State::connectingTcp1); + #if defined(ARDUINO_ARCH_ESP32) + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { + vTaskResume(_taskHandle); + } + #endif + } else { + emc_log_e("Could not create CONNECT packet"); + EMC_SEMAPHORE_GIVE(); + _onError(0, Error::OUT_OF_MEMORY); + EMC_SEMAPHORE_TAKE(); + } + EMC_SEMAPHORE_GIVE(); + } + return result; +} + +bool MqttClient::disconnect(bool force) { + if (force && _state != State::disconnected && _state != State::disconnectingTcp1 && _state != State::disconnectingTcp2) { + _setState(State::disconnectingTcp1); + return true; + } + if (!force && _state == State::connected) { + _setState(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) { + #else + if (_state > State::connected) { + #endif + return 0; + } + EMC_SEMAPHORE_TAKE(); + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; + if (!_addPacket(packetId, topic, payload, length, qos, retain)) { + emc_log_e("Could not create PUBLISH packet"); + EMC_SEMAPHORE_GIVE(); + _onError(packetId, Error::OUT_OF_MEMORY); + EMC_SEMAPHORE_TAKE(); + 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) { + #else + if (_state > State::connected) { + #endif + return 0; + } + EMC_SEMAPHORE_TAKE(); + uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1; + if (!_addPacket(packetId, topic, callback, length, qos, retain)) { + emc_log_e("Could not create PUBLISH packet"); + EMC_SEMAPHORE_GIVE(); + _onError(packetId, Error::OUT_OF_MEMORY); + EMC_SEMAPHORE_TAKE(); + packetId = 0; + } + EMC_SEMAPHORE_GIVE(); + return packetId; +} + +void MqttClient::clearQueue(bool deleteSessionData) { + EMC_SEMAPHORE_TAKE(); + _clearQueue(deleteSessionData ? 2 : 0); + EMC_SEMAPHORE_GIVE(); +} + +const char* MqttClient::getClientId() const { + return _clientId; +} + +size_t MqttClient::queueSize() { + size_t ret = 0; + EMC_SEMAPHORE_TAKE(); + ret = _outbox.size(); + EMC_SEMAPHORE_GIVE(); + return ret; +} + +void MqttClient::loop() { + switch (_state) { + case State::disconnected: + #if defined(ARDUINO_ARCH_ESP32) + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { + vTaskSuspend(_taskHandle); + } + #endif + break; + case State::connectingTcp1: + if (_useIp ? _transport->connect(_ip, _port) : _transport->connect(_host, _port)) { + _setState(State::connectingTcp2); + } else { + _setState(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(); + _setState(State::connectingMqtt); + } else if (_transport->disconnected()) { // sync: implemented as "not connected"; async: depending on state of pcb in underlying lib + _setState(State::disconnectingTcp1); + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + } + break; + case State::connectingMqtt: + #if EMC_WAIT_FOR_CONNACK + if (_transport->connected()) { + EMC_SEMAPHORE_TAKE(); + _sendPacket(); + _checkIncoming(); + _checkPing(); + EMC_SEMAPHORE_GIVE(); + } else { + _setState(State::disconnectingTcp1); + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + } + break; + #else + // 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]]; + #endif + case State::connected: + [[fallthrough]]; + case State::disconnectingMqtt2: + if (_transport->connected()) { + // CONNECT packet is first in the queue + EMC_SEMAPHORE_TAKE(); + _checkOutbox(); + _checkIncoming(); + _checkPing(); + _checkTimeout(); + EMC_SEMAPHORE_GIVE(); + } else { + _setState(State::disconnectingTcp1); + _disconnectReason = DisconnectReason::TCP_DISCONNECTED; + } + 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); + EMC_SEMAPHORE_TAKE(); + } else { + _setState(State::disconnectingMqtt2); + } + } + _checkOutbox(); + _checkIncoming(); + _checkPing(); + _checkTimeout(); + EMC_SEMAPHORE_GIVE(); + break; + case State::disconnectingTcp1: + _transport->stop(); + _setState(State::disconnectingTcp2); + break; // keep break to accomodate async clients + case State::disconnectingTcp2: + if (_transport->disconnected()) { + EMC_SEMAPHORE_TAKE(); + _clearQueue(0); + EMC_SEMAPHORE_GIVE(); + _bytesSent = 0; + _setState(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 + +inline void MqttClient::_setState(State newState) { + emc_log_i("state %i --> %i", static_cast::type>(_state.load()), static_cast::type>(newState)); + _state = newState; +} + +uint16_t MqttClient::_getNextPacketId() { + ++_packetId; + if (_packetId == 0) ++_packetId; + return _packetId; +} + +void MqttClient::_checkOutbox() { + while (_sendPacket() > 0) { + if (!_advanceOutbox()) { + break; + } + } +} + +int MqttClient::_sendPacket() { + OutgoingPacket* packet = _outbox.getCurrent(); + + size_t written = 0; + if (packet) { + size_t wantToWrite = packet->packet.available(_bytesSent); + if (wantToWrite == 0) { + return 0; + } + written = _transport->write(packet->packet.data(_bytesSent), wantToWrite); + packet->timeSent = millis(); + _lastClientActivity = millis(); + _bytesSent += written; + emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->packet.size(), packet->packet.packetType()); + } + return written; +} + +bool MqttClient::_advanceOutbox() { + OutgoingPacket* packet = _outbox.getCurrent(); + if (packet && _bytesSent == packet->packet.size()) { + if ((packet->packet.packetType()) == PacketType.DISCONNECT) { + _setState(State::disconnectingTcp1); + _disconnectReason = DisconnectReason::USER_OK; + } + if (packet->packet.removable()) { + _outbox.removeCurrent(); + } else { + // we already set 'dup' here, in case we have to retry + if ((packet->packet.packetType()) == PacketType.PUBLISH) packet->packet.setDup(); + _outbox.next(); + } + packet = _outbox.getCurrent(); + _bytesSent = 0; + } + return packet; +} + +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"); + _setState(State::disconnectingTcp1); + return; + } + switch (packetType) { + case PacketType.CONNACK: + _onConnack(); + if (_state != State::connected) { + return; + } + break; + case PacketType.PUBLISH: + if (_state >= State::disconnectingMqtt1) 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"); + _setState(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"); + _setState(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))) { + if (!_addPacket(PacketType.PINGREQ)) { + emc_log_e("Could not create PING packet"); + return; + } + _pingSent = true; + } +} + +void MqttClient::_checkTimeout() { + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + // check that we're not busy sending + // don't check when first item hasn't been sent yet + if (it && _bytesSent == 0 && it.get() != _outbox.getCurrent()) { + if (millis() - it.get()->timeSent > _timeout) { + emc_log_w("Packet ack timeout, retrying"); + _outbox.resetCurrent(); + } + } +} + +void MqttClient::_onConnack() { + if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) { + _pingSent = false; // reset after keepalive timeout disconnect + _setState(State::connected); + _advanceOutbox(); + if (_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent == 0) { + _clearQueue(1); + } + if (_onConnectCallback) { + EMC_SEMAPHORE_GIVE(); + _onConnectCallback(_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent); + EMC_SEMAPHORE_TAKE(); + } + } else { + _setState(State::disconnectingTcp1); + // cast is safe because the parser already checked for a valid return code + _disconnectReason = static_cast(_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode); + } +} + +void MqttClient::_onPublish() { + const 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) { + if (!_addPacket(PacketType.PUBACK, packetId)) { + emc_log_e("Could not create PUBACK packet"); + } + } + } else if (qos == 2) { + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + if ((it.get()->packet.packetType()) == PacketType.PUBREC && it.get()->packet.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"); + } + } + } + if (callback && _onMessageCallback) { + EMC_SEMAPHORE_GIVE(); + _onMessageCallback({qos, dup, retain, packetId}, + p.variableHeader.topic, + p.payload.data, + p.payload.length, + p.payload.index, + p.payload.total); + EMC_SEMAPHORE_TAKE(); + } +} + +void MqttClient::_onPuback() { + bool callback = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + 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()->packet.packetType()) == PacketType.PUBLISH) { + if (it.get()->packet.packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + emc_log_w("Received out of order PUBACK"); + break; + } + ++it; + } + if (callback) { + if (_onPublishCallback) { + EMC_SEMAPHORE_GIVE(); + _onPublishCallback(idToMatch); + EMC_SEMAPHORE_TAKE(); + } + } else { + emc_log_w("No matching PUBLISH packet found"); + } +} + +void MqttClient::_onPubrec() { + bool success = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + 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()->packet.packetType()) == PacketType.PUBLISH) { + if (it.get()->packet.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"); + } +} + +void MqttClient::_onPubrel() { + bool success = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + 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()->packet.packetType()) == PacketType.PUBREC) { + if (it.get()->packet.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"); + } +} + +void MqttClient::_onPubcomp() { + bool callback = false; + 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()->packet.packetType()) == PacketType.PUBREL) { + if (it.get()->packet.packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + emc_log_w("Received out of order PUBCOMP"); + break; + } + ++it; + } + if (callback) { + if (_onPublishCallback) { + EMC_SEMAPHORE_GIVE(); + _onPublishCallback(idToMatch); + EMC_SEMAPHORE_TAKE(); + } + } else { + emc_log_w("No matching PUBREL packet found"); + } +} + +void MqttClient::_onSuback() { + bool callback = false; + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + while (it) { + if (((it.get()->packet.packetType()) == PacketType.SUBSCRIBE) && it.get()->packet.packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + ++it; + } + if (callback) { + if (_onSubscribeCallback) { + EMC_SEMAPHORE_GIVE(); + _onSubscribeCallback(idToMatch, reinterpret_cast(_parser.getPacket().payload.data), _parser.getPacket().payload.total); + EMC_SEMAPHORE_TAKE(); + } + } else { + emc_log_w("received SUBACK without SUB"); + } +} + +void MqttClient::_onUnsuback() { + bool callback = false; + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId; + while (it) { + if (it.get()->packet.packetId() == idToMatch) { + callback = true; + _outbox.remove(it); + break; + } + ++it; + } + if (callback) { + if (_onUnsubscribeCallback) { + EMC_SEMAPHORE_GIVE(); + _onUnsubscribeCallback(idToMatch); + EMC_SEMAPHORE_TAKE(); + } + } else { + emc_log_w("received UNSUBACK without UNSUB"); + } +} + +void MqttClient::_clearQueue(int clearData) { + emc_log_i("clearing queue (clear session: %d)", clearData); + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + if (clearData == 0) { + // 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()->packet.packetType(); + if (type == PacketType.PUBREC || + type == PacketType.PUBREL || + (type == PacketType.PUBLISH && it.get()->packet.packetId() != 0)) { + ++it; + } else { + _outbox.remove(it); + } + } + } else if (clearData == 1) { + // keep PUB + while (it) { + if (it.get()->packet.packetType() == PacketType.PUBLISH) { + ++it; + } else { + _outbox.remove(it); + } + } + } else { // clearData == 2 + while (it) { + _outbox.remove(it); + } + } +} + +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..eaf9d2d --- /dev/null +++ b/lib/espMqttClient/src/MqttClient.h @@ -0,0 +1,201 @@ +/* +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 = 0; + if (_state != State::connected) { + return packetId; + } else { + EMC_SEMAPHORE_TAKE(); + packetId = _getNextPacketId(); + 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 = 0; + if (_state != State::connected) { + return packetId; + } else { + EMC_SEMAPHORE_TAKE(); + packetId = _getNextPacketId(); + 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 deleteSessionData = false); // Not MQTT compliant and may cause unpredictable results when `deleteSessionData` = true! + const char* getClientId() const; + size_t queueSize(); // No const because of mutex + void loop(); + + protected: + explicit MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1); + espMqttClientTypes::UseInternalTask _useInternalTask; + 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; + uint32_t _timeout; + + // state is protected to allow state changes by the transport system, defined in child classes + // eg. to allow AsyncTCP + enum class State { + disconnected = 0, + connectingTcp1 = 1, + connectingTcp2 = 2, + connectingMqtt = 3, + connected = 4, + disconnectingMqtt1 = 5, + disconnectingMqtt2 = 6, + disconnectingTcp1 = 7, + disconnectingTcp2 = 8 + }; + std::atomic _state; + inline void _setState(State newState); + + 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]; + struct OutgoingPacket { + uint32_t timeSent; + espMqttClientInternals::Packet packet; + template + OutgoingPacket(uint32_t t, espMqttClientTypes::Error& error, Args&&... args) : // NOLINT(runtime/references) + timeSent(t), + packet(error, std::forward(args) ...) {} + }; + 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(espMqttClientTypes::Error::SUCCESS); + espMqttClientInternals::Outbox::Iterator it = _outbox.emplace(0, error, std::forward(args) ...); + if (it && error == espMqttClientTypes::Error::SUCCESS) { + return true; + } else { + if (it) _outbox.remove(it); + return false; + } + } + + template + bool _addPacketFront(Args&&... args) { + espMqttClientTypes::Error error(espMqttClientTypes::Error::SUCCESS); + espMqttClientInternals::Outbox::Iterator it = _outbox.emplaceFront(0, error, std::forward(args) ...); + if (it && error == espMqttClientTypes::Error::SUCCESS) { + return true; + } else { + if (it) _outbox.remove(it); + return false; + } + } + + void _checkOutbox(); + int _sendPacket(); + bool _advanceOutbox(); + void _checkIncoming(); + void _checkPing(); + void _checkTimeout(); + + void _onConnack(); + void _onPublish(); + void _onPuback(); + void _onPubrec(); + void _onPubrel(); + void _onPubcomp(); + void _onSuback(); + void _onUnsuback(); + + void _clearQueue(int clearData); // 0: keep session, + // 1: keep only PUBLISH qos > 0 + // 2: delete all + 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..4ef7307 --- /dev/null +++ b/lib/espMqttClient/src/MqttClientSetup.h @@ -0,0 +1,245 @@ +/* +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" + +#if EMC_MULTIPLE_CALLBACKS +#include +#include +#endif + +template +class MqttClientSetup : public MqttClient { + public: + T& setKeepAlive(uint16_t keepAlive) { + _keepAlive = keepAlive * 1000; // s to ms conversion, will also do 16 to 32 bit conversion + return static_cast(*this); + } + + T& setClientId(const char* clientId) { + _clientId = clientId; + return static_cast(*this); + } + + T& setCleanSession(bool cleanSession) { + _cleanSession = cleanSession; + return static_cast(*this); + } + + T& setCredentials(const char* username, const char* password) { + _username = username; + _password = password; + return static_cast(*this); + } + + T& 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; + } + return static_cast(*this); + } + + T& setWill(const char* topic, uint8_t qos, bool retain, const char* payload) { + return setWill(topic, qos, retain, reinterpret_cast(payload), strlen(payload)); + } + + T& setServer(IPAddress ip, uint16_t port) { + _ip = ip; + _port = port; + _useIp = true; + return static_cast(*this); + } + + T& setServer(const char* host, uint16_t port) { + _host = host; + _port = port; + _useIp = false; + return static_cast(*this); + } + + T& setTimeout(uint16_t timeout) { + _timeout = timeout * 1000; // s to ms conversion, will also do 16 to 32 bit conversion + return static_cast(*this); + } + + T& onConnect(espMqttClientTypes::OnConnectCallback callback, uint32_t id = 0) { + #if EMC_MULTIPLE_CALLBACKS + _onConnectCallbacks.emplace_back(callback, id); + #else + (void) id; + _onConnectCallback = callback; + #endif + return static_cast(*this); + } + + T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback, uint32_t id = 0) { + #if EMC_MULTIPLE_CALLBACKS + _onDisconnectCallbacks.emplace_back(callback, id); + #else + (void) id; + _onDisconnectCallback = callback; + #endif + return static_cast(*this); + } + + T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback, uint32_t id = 0) { + #if EMC_MULTIPLE_CALLBACKS + _onSubscribeCallbacks.emplace_back(callback, id); + #else + (void) id; + _onSubscribeCallback = callback; + #endif + return static_cast(*this); + } + + T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback, uint32_t id = 0) { + #if EMC_MULTIPLE_CALLBACKS + _onUnsubscribeCallbacks.emplace_back(callback, id); + #else + (void) id; + _onUnsubscribeCallback = callback; + #endif + return static_cast(*this); + } + + T& onMessage(espMqttClientTypes::OnMessageCallback callback, uint32_t id = 0) { + #if EMC_MULTIPLE_CALLBACKS + _onMessageCallbacks.emplace_back(callback, id); + #else + (void) id; + _onMessageCallback = callback; + #endif + return static_cast(*this); + } + + T& onPublish(espMqttClientTypes::OnPublishCallback callback, uint32_t id = 0) { + #if EMC_MULTIPLE_CALLBACKS + _onPublishCallbacks.emplace_back(callback, id); + #else + (void) id; + _onPublishCallback = callback; + #endif + return static_cast(*this); + } + + #if EMC_MULTIPLE_CALLBACKS + T& removeOnConnect(uint32_t id) { + for (auto it = _onConnectCallbacks.begin(); it != _onConnectCallbacks.end(); ++it) { + if (it->second == id) { + _onConnectCallbacks.erase(it); + break; + } + } + return static_cast(*this); + } + + T& removeOnDisconnect(uint32_t id) { + for (auto it = _onDisconnectCallbacks.begin(); it != _onDisconnectCallbacks.end(); ++it) { + if (it->second == id) { + _onDisconnectCallbacks.erase(it); + break; + } + } + return static_cast(*this); + } + + T& removeOnSubscribe(uint32_t id) { + for (auto it = _onSubscribeCallbacks.begin(); it != _onSubscribeCallbacks.end(); ++it) { + if (it->second == id) { + _onSubscribeCallbacks.erase(it); + break; + } + } + return static_cast(*this); + } + + T& removeOnUnsubscribe(uint32_t id) { + for (auto it = _onUnsubscribeCallbacks.begin(); it != _onUnsubscribeCallbacks.end(); ++it) { + if (it->second == id) { + _onUnsubscribeCallbacks.erase(it); + break; + } + } + return static_cast(*this); + } + + T& removeOnMessage(uint32_t id) { + for (auto it = _onMessageCallbacks.begin(); it != _onMessageCallbacks.end(); ++it) { + if (it->second == id) { + _onMessageCallbacks.erase(it); + break; + } + } + return static_cast(*this); + } + + T& removeOnPublish(uint32_t id) { + for (auto it = _onPublishCallbacks.begin(); it != _onPublishCallbacks.end(); ++it) { + if (it->second == id) { + _onPublishCallbacks.erase(it); + break; + } + } + return static_cast(*this); + } + #endif + + /* + T& onError(espMqttClientTypes::OnErrorCallback callback) { + _onErrorCallback = callback; + return static_cast(*this); + } + */ + + protected: + explicit MqttClientSetup(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1) + : MqttClient(useInternalTask, priority, core) { + #if EMC_MULTIPLE_CALLBACKS + _onConnectCallback = [this](bool sessionPresent) { + for (auto callback : _onConnectCallbacks) if (callback.first) callback.first(sessionPresent); + }; + _onDisconnectCallback = [this](espMqttClientTypes::DisconnectReason reason) { + for (auto callback : _onDisconnectCallbacks) if (callback.first) callback.first(reason); + }; + _onSubscribeCallback = [this](uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* returncodes, size_t len) { + for (auto callback : _onSubscribeCallbacks) if (callback.first) callback.first(packetId, returncodes, len); + }; + _onUnsubscribeCallback = [this](int16_t packetId) { + for (auto callback : _onUnsubscribeCallbacks) if (callback.first) callback.first(packetId); + }; + _onMessageCallback = [this](const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { + for (auto callback : _onMessageCallbacks) if (callback.first) callback.first(properties, topic, payload, len, index, total); + }; + _onPublishCallback = [this](uint16_t packetId) { + for (auto callback : _onPublishCallbacks) if (callback.first) callback.first(packetId); + }; + #else + // empty + #endif + } + + #if EMC_MULTIPLE_CALLBACKS + std::list> _onConnectCallbacks; + std::list> _onDisconnectCallbacks; + std::list> _onSubscribeCallbacks; + std::list> _onUnsubscribeCallbacks; + std::list> _onMessageCallbacks; + std::list> _onPublishCallbacks; + #endif +}; diff --git a/lib/espMqttClient/src/Outbox.h b/lib/espMqttClient/src/Outbox.h new file mode 100644 index 0000000..832d5e3 --- /dev/null +++ b/lib/espMqttClient/src/Outbox.h @@ -0,0 +1,255 @@ + +/* +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 EMC_USE_MEMPOOL + #include "MemoryPool/src/MemoryPool.h" + #include "Config.h" +#else + #include // new (std::nothrow) +#endif +#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) + #if EMC_USE_MEMPOOL + , _memPool() + #endif + {} + ~Outbox() { + while (_first) { + Node* n = _first->next; + #if EMC_USE_MEMPOOL + _first->~Node(); + _memPool.free(_first); + #else + delete _first; + #endif + _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; + #if EMC_USE_MEMPOOL + void* buf = _memPool.malloc(); + Node* node = nullptr; + if (buf) { + node = new(buf) Node(std::forward(args) ...); + } + #else + Node* node = new(std::nothrow) Node(std::forward(args) ...); + #endif + 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; + #if EMC_USE_MEMPOOL + void* buf = _memPool.malloc(); + Node* node = nullptr; + if (buf) { + node = new(buf) Node(std::forward(args) ...); + } + #else + Node* node = new(std::nothrow) Node(std::forward(args) ...); + #endif + 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) + if (!it) return; + 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; + } + + void resetCurrent() { + _current = _first; + } + + 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; + } + + size_t size() const { + Node* n = _first; + size_t count = 0; + while (n) { + n = n->next; + ++count; + } + return count; + } + + private: + Node* _first; + Node* _last; + Node* _current; + Node* _prev; // element just before _current + #if EMC_USE_MEMPOOL + MemoryPool::Fixed _memPool; + #endif + + 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 + #if EMC_USE_MEMPOOL + node->~Node(); + _memPool.free(node); + #else + delete node; + #endif + } +}; + +} // 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..14d241b --- /dev/null +++ b/lib/espMqttClient/src/Packets/Packet.cpp @@ -0,0 +1,454 @@ +/* +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 { + +#if EMC_USE_MEMPOOL +MemoryPool::Variable Packet::_memPool; +#endif + +Packet::~Packet() { + #if EMC_USE_MEMPOOL + _memPool.free(_data); + #else + free(_data); + #endif +} + +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) +: _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, false)) { + 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) +: _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, true)) { + 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) +: _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)), true)) { + 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) +: _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) +: _packetId(packetId) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (!_allocate(2, true)) { + 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) +: _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) +: _packetId(0) +, _data(nullptr) +, _size(0) +, _payloadIndex(0) +, _payloadStartIndex(0) +, _payloadEndIndex(0) +, _getPayload(nullptr) { + if (!_allocate(0, true)) { + error = espMqttClientTypes::Error::OUT_OF_MEMORY; + return; + } + _data[0] |= type; + + error = espMqttClientTypes::Error::SUCCESS; +} + + +bool Packet::_allocate(size_t remainingLength, bool check) { + #if EMC_USE_MEMPOOL + (void) check; + #else + if (check && EMC_GET_FREE_MEMORY() < EMC_MIN_FREE_MEMORY) { + emc_log_w("Packet buffer not allocated: low memory"); + return false; + } + #endif + _size = 1 + remainingLengthLength(remainingLength) + remainingLength; + #if EMC_USE_MEMPOOL + _data = reinterpret_cast(_memPool.malloc(_size)); + #else + _data = reinterpret_cast(malloc(_size)); + #endif + 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, true)) { + 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, true)) { + 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..5d0b67b --- /dev/null +++ b/lib/espMqttClient/src/Packets/Packet.h @@ -0,0 +1,163 @@ +/* +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 "StringUtil.h" + +#if EMC_USE_MEMPOOL + #include "MemoryPool/src/MemoryPool.h" +#endif + +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; + + 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) + : _packetId(packetId) + , _data(nullptr) + , _size(0) + , _payloadIndex(0) + , _payloadStartIndex(0) + , _payloadEndIndex(0) + , _getPayload(nullptr) { + static_assert(sizeof...(Args) % 2 == 0, "Subscribe should be in topic/qos pairs"); + 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) + : _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, bool check); + + // 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; + + #if EMC_USE_MEMPOOL + static MemoryPool::Variable _memPool; + #endif +}; + +} // 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/StringUtil.cpp b/lib/espMqttClient/src/Packets/StringUtil.cpp new file mode 100644 index 0000000..7cd3dd8 --- /dev/null +++ b/lib/espMqttClient/src/Packets/StringUtil.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 "StringUtil.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/StringUtil.h b/lib/espMqttClient/src/Packets/StringUtil.h new file mode 100644 index 0000000..7f1e1e8 --- /dev/null +++ b/lib/espMqttClient/src/Packets/StringUtil.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/ClientPosix.cpp b/lib/espMqttClient/src/Transport/ClientPosix.cpp new file mode 100644 index 0000000..4b086d1 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosix.cpp @@ -0,0 +1,130 @@ +/* +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: \"%s\" opening socket", errno, strerror(errno)); + } + + int flag = 1; + if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) { + emc_log_e("Error %d: \"%s\" disabling nagle", errno, strerror(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, reinterpret_cast(&_host), sizeof(_host)); + + if (ret < 0) { + emc_log_e("Error connecting: %d - (%d) %s", ret, errno, strerror(errno)); + return false; + } + + emc_log_i("Socket connected"); + return true; +} + +bool ClientPosix::connect(const char* hostname, uint16_t port) { + IPAddress ipAddress = _hostToIP(hostname); + if (ipAddress == IPAddress(0)) { + emc_log_e("No such host '%s'", hostname); + return false; + } + return connect(ipAddress, port); +} + +size_t ClientPosix::write(const uint8_t* buf, size_t size) { + return ::send(_sockfd, buf, size, 0); +} + +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; +} + +IPAddress ClientPosix::_hostToIP(const char* hostname) { + IPAddress returnIP(0); + struct addrinfo hints, *servinfo, *p; + struct sockaddr_in *h; + int rv; + +// Set up request addrinfo struct + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + emc_log_i("Looking for '%s'", hostname); + +// ask for host data + if ((rv = getaddrinfo(hostname, NULL, &hints, &servinfo)) != 0) { + emc_log_e("getaddrinfo: %s", gai_strerror(rv)); + return returnIP; + } + + // loop through all the results and connect to the first we can + for (p = servinfo; p != NULL; p = p->ai_next) { + h = (struct sockaddr_in *)p->ai_addr; + returnIP = ::htonl(h->sin_addr.s_addr); + if (returnIP != IPAddress(0)) break; + } + // Release allocated memory + freeaddrinfo(servinfo); + + if (returnIP != IPAddress(0)) { + emc_log_i("Host '%s' = %u", hostname, (uint32_t)returnIP); + } else { + emc_log_e("No IP for '%s' found", hostname); + } + return returnIP; +} + +} // 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..adffaa1 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosix.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 + +#if defined(__linux__) + +#include +#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* hostname, uint16_t port) override; + size_t write(const uint8_t* buf, size_t size) override; + int read(uint8_t* buf, size_t size) override; + void stop() override; + bool connected() override; + bool disconnected() override; + + protected: + int _sockfd; + sockaddr_in _host; + + IPAddress _hostToIP(const char* hostname); +}; + +} // namespace espMqttClientInternals + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientPosixIPAddress.cpp b/lib/espMqttClient/src/Transport/ClientPosixIPAddress.cpp new file mode 100644 index 0000000..c6a78fc --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosixIPAddress.cpp @@ -0,0 +1,40 @@ +/* +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 "ClientPosixIPAddress.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; +} + +bool IPAddress::operator==(IPAddress other) { + return _address == other._address; +} + +bool IPAddress::operator!=(IPAddress other) { + return _address != other._address; +} + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientPosixIPAddress.h b/lib/espMqttClient/src/Transport/ClientPosixIPAddress.h new file mode 100644 index 0000000..9941ec5 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientPosixIPAddress.h @@ -0,0 +1,30 @@ +/* +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); + IPAddress(uint32_t address); // NOLINT(runtime/explicit) + operator uint32_t(); + bool operator==(IPAddress other); + bool operator!=(IPAddress other); + + protected: + uint32_t _address; +}; + +#endif diff --git a/lib/espMqttClient/src/Transport/ClientSecureSync.cpp b/lib/espMqttClient/src/Transport/ClientSecureSync.cpp new file mode 100644 index 0000000..36288c6 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSecureSync.cpp @@ -0,0 +1,71 @@ +/* +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::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..b81681e --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSecureSync.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_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 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..b2c4045 --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSync.cpp @@ -0,0 +1,71 @@ +/* +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() +: client() { + // 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::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..ccfbdba --- /dev/null +++ b/lib/espMqttClient/src/Transport/ClientSync.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_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include // includes IPAddress + +#include "Transport.h" + +namespace espMqttClientInternals { + +class ClientSync : public Transport { + public: + ClientSync(); + 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 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/Transport.h b/lib/espMqttClient/src/Transport/Transport.h new file mode 100644 index 0000000..d368d01 --- /dev/null +++ b/lib/espMqttClient/src/Transport/Transport.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 + +#include // size_t + +#include "ClientPosixIPAddress.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 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..0f15360 --- /dev/null +++ b/lib/espMqttClient/src/TypeDefs.h @@ -0,0 +1,73 @@ +/* +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; + +enum class UseInternalTask { + NO = 0, + YES = 1, +}; + +} // end namespace espMqttClientTypes diff --git a/lib/espMqttClient/src/espMqttClient.cpp b/lib/espMqttClient/src/espMqttClient.cpp new file mode 100644 index 0000000..833ece1 --- /dev/null +++ b/lib/espMqttClient/src/espMqttClient.cpp @@ -0,0 +1,113 @@ +/* +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_ESP8266) +espMqttClient::espMqttClient() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { + _transport = &_client; +} + +espMqttClientSecure& espMqttClientSecure::setInsecure() { + _client.client.setInsecure(); + return *this; +} + +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 + +#if defined(ARDUINO_ARCH_ESP32) +espMqttClient::espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask) +: MqttClientSetup(useInternalTask) +, _client() { + _transport = &_client; +} + +espMqttClient::espMqttClient(uint8_t priority, uint8_t core) +: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask) +: MqttClientSetup(useInternalTask) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core) +: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core) +, _client() { + _transport = &_client; +} + +espMqttClientSecure& espMqttClientSecure::setInsecure() { + _client.client.setInsecure(); + return *this; +} + +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; +} + +#endif + +#if defined(__linux__) +espMqttClient::espMqttClient() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { + _transport = &_client; +} +#endif diff --git a/lib/espMqttClient/src/espMqttClient.h b/lib/espMqttClient/src/espMqttClient.h new file mode 100644 index 0000000..4e44801 --- /dev/null +++ b/lib/espMqttClient/src/espMqttClient.h @@ -0,0 +1,80 @@ +/* +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" + +#if defined(ARDUINO_ARCH_ESP8266) +class espMqttClient : public MqttClientSetup { + public: + espMqttClient(); + + protected: + espMqttClientInternals::ClientSync _client; +}; + +class espMqttClientSecure : public MqttClientSetup { + public: + espMqttClientSecure(); + espMqttClientSecure& setInsecure(); + 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); + + protected: + espMqttClientInternals::ClientSecureSync _client; +}; +#endif + +#if defined(ARDUINO_ARCH_ESP32) +class espMqttClient : public MqttClientSetup { + public: + explicit espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1); + + protected: + espMqttClientInternals::ClientSync _client; +}; + +class espMqttClientSecure : public MqttClientSetup { + public: + explicit espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1); + espMqttClientSecure& setInsecure(); + espMqttClientSecure& setCACert(const char* rootCA); + espMqttClientSecure& setCertificate(const char* clientCa); + espMqttClientSecure& setPrivateKey(const char* privateKey); + espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey); + + protected: + espMqttClientInternals::ClientSecureSync _client; +}; +#endif + +#if defined(__linux__) +class espMqttClient : public MqttClientSetup { + public: + espMqttClient(); + + protected: + espMqttClientInternals::ClientPosix _client; +}; +#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..d2eef9d --- /dev/null +++ b/lib/espMqttClient/test/test_client_native/test_client_native.cpp @@ -0,0 +1,405 @@ +#include +#include +#include +#include // espMqttClient for Linux also defines millis() + +void setUp() {} +void tearDown() {} + +espMqttClient mqttClient; +uint32_t onConnectCbId = 1; +uint32_t onDisconnectCbId = 2; +uint32_t onSubscribeCbId = 3; +uint32_t onUnsubscribeCbId = 4; +uint32_t onMessageCbId = 5; +uint32_t onPublishCbId = 6; +std::atomic_bool exitProgram(false); +std::thread t; + +//const IPAddress broker(127,0,0,1); +const char* broker = "mqtt"; +//const char* broker = "test.mosquitto.org"; +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; + }, onConnectCbId); + 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); + + mqttClient.removeOnConnect(onConnectCbId); +} + +/* + +- 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; + } + }, onSubscribeCbId); + 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); + + mqttClient.removeOnSubscribe(onSubscribeCbId); +} + +/* + +- 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++; + }, onPublishCbId); + 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++; + }, onMessageCbId); + 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); + + mqttClient.removeOnPublish(onPublishCbId); + mqttClient.removeOnMessage(onMessageCbId); +} + +void test_publish_empty() { + std::atomic publishSendEmptyTest(0); + mqttClient.onPublish([&](uint16_t packetId) mutable { + (void) packetId; + publishSendEmptyTest++; + }, onPublishCbId); + 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++; + }, onMessageCbId); + 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); + + mqttClient.removeOnPublish(onPublishCbId); + mqttClient.removeOnMessage(onMessageCbId); +} + +/* + +- 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++; + }, onMessageCbId); + 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, ""); + } + }, onSubscribeCbId); + 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); + + mqttClient.removeOnMessage(onMessageCbId); + mqttClient.removeOnSubscribe(onSubscribeCbId); +} + +/* + +- 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++; + }, onMessageCbId); + 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, ""); + } + }, onSubscribeCbId); + 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); + + mqttClient.removeOnMessage(onMessageCbId); + mqttClient.removeOnSubscribe(onSubscribeCbId); +} + + +/* + +- client unsibscribes from topic + +*/ + +void test_unsubscribe() { + std::atomic unsubscribeTest(false); + mqttClient.onUnsubscribe([&](uint16_t packetId) mutable { + (void) packetId; + unsubscribeTest = true; + }, onUnsubscribeCbId); + 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); + + mqttClient.removeOnUnsubscribe(onUnsubscribeCbId); +} + +/* + +- 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; + }, onDisconnectCbId); + 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()); + + mqttClient.removeOnDisconnect(onDisconnectCbId); +} + +void test_pub_before_connect() { + std::atomic onConnectCalledTest(false); + std::atomic publishSendTest(0); + bool sessionPresentTest = true; + mqttClient.setServer(broker, broker_port) + .setCleanSession(true) + .setKeepAlive(5) + .onConnect([&](bool sessionPresent) mutable { + sessionPresentTest = sessionPresent; + onConnectCalledTest = true; + }, onConnectCbId) + .onPublish([&](uint16_t packetId) mutable { + (void) packetId; + publishSendTest++; + }, onPublishCbId); + 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"); + 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); + start = millis(); + while (millis() - start < 10000) { + std::this_thread::yield(); + } + + 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); + + mqttClient.removeOnConnect(onConnectCbId); + mqttClient.removeOnPublish(onPublishCbId); +} + +void final_disconnect() { + std::atomic onDisconnectCalled(false); + mqttClient.onDisconnect([&](espMqttClientTypes::DisconnectReason reason) mutable { + (void) reason; + onDisconnectCalled = true; + }, onDisconnectCbId); + mqttClient.disconnect(); + uint32_t start = millis(); + while (millis() - start < 2000) { + if (onDisconnectCalled) { + break; + } + std::this_thread::yield(); + } + if (mqttClient.connected()) { + mqttClient.disconnect(true); + } + mqttClient.removeOnDisconnect(onDisconnectCbId); +} + +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); + RUN_TEST(test_pub_before_connect); + final_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..7dc3f7d --- /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(523); + // 523, current points to 523 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(523, *(outbox.getCurrent())); + TEST_ASSERT_FALSE(outbox.empty()); + + outbox.next(); + // 523, current points to nullptr + TEST_ASSERT_NULL(outbox.getCurrent()); + + outbox.emplace(286); + // 523 286, current points to 286 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(286, *(outbox.getCurrent())); + + outbox.emplace(364); + // 523 286 364, current points to 286 + TEST_ASSERT_NOT_NULL(outbox.getCurrent()); + TEST_ASSERT_EQUAL_UINT32(286, *(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..e7cf5cd --- /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..c51064c --- /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..85a9c09 --- /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..bf262f2 --- /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/platformio.ini b/platformio.ini index 2538d4e..fd5dd54 100644 --- a/platformio.ini +++ b/platformio.ini @@ -40,9 +40,6 @@ build_flags = -DARDUINO_ARCH_ESP32 -DUSE_ESP_IDF_LOG -DCONFIG_BTDM_BLE_SCAN_DUPL=y - -DCONFIG_ASYNC_TCP_MAX_ACK_TIME=3000 - -DCONFIG_ASYNC_TCP_PRIORITY=10 - -DCONFIG_ASYNC_TCP_RUNNING_CORE=1 -DNUKI_ALT_CONNECT -DBLESCANNER_USE_LATEST_NIMBLE -DNUKI_USE_LATEST_NIMBLE @@ -80,9 +77,6 @@ extra_scripts = post:pio_package_post.py build_flags = ${env.build_flags} - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=128 -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 @@ -144,16 +138,12 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DWM_DEBUG_LEVEL=4 -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_COMMUNICATION ;-DDEBUG_NUKI_HEX_DATA -DDEBUG_NUKI_READABLE_DATA - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=512 [env:esp32-c3_dbg] extends = env:esp32-c3 @@ -165,17 +155,13 @@ build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 - -DDEBUG_NUKIHUB - -DWM_DEBUG_LEVEL=4 + -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_COMMUNICATION ;-DDEBUG_NUKI_HEX_DATA -DDEBUG_NUKI_READABLE_DATA - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=512 [env:esp32-c6_dbg] extends = env:esp32-c6 @@ -188,16 +174,12 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DWM_DEBUG_LEVEL=4 -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_COMMUNICATION ;-DDEBUG_NUKI_HEX_DATA -DDEBUG_NUKI_READABLE_DATA - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=512 [env:esp32-h2_dbg] extends = env:esp32-h2 @@ -211,16 +193,12 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DWM_DEBUG_LEVEL=4 -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_COMMUNICATION ;-DDEBUG_NUKI_HEX_DATA -DDEBUG_NUKI_READABLE_DATA - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=512 [env:esp32-s3_dbg] extends = env:esp32-s3 @@ -239,9 +217,6 @@ build_flags = -DDEBUG_NUKI_COMMUNICATION ;-DDEBUG_NUKI_HEX_DATA -DDEBUG_NUKI_READABLE_DATA - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=512 [env:esp32-s3-oct_dbg] extends = env:esp32-s3-oct @@ -254,16 +229,12 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DWM_DEBUG_LEVEL=4 -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_COMMUNICATION ;-DDEBUG_NUKI_HEX_DATA -DDEBUG_NUKI_READABLE_DATA - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=512 [env:esp32-solo1_dbg] extends = env:esp32-solo1 @@ -276,13 +247,9 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DWM_DEBUG_LEVEL=4 -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_COMMUNICATION ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA - -DCONFIG_ASYNC_TCP_QUEUE_SIZE=128 - -DCONFIG_ASYNC_TCP_STACK_SIZE=8192 - -DWS_MAX_QUEUED_MESSAGES=512 \ No newline at end of file + -DDEBUG_NUKI_READABLE_DATA \ No newline at end of file diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 89c8474..ba24c42 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -91,4 +91,4 @@ CONFIG_HTTPD_MAX_URI_LEN=512 CONFIG_HTTPD_ERR_RESP_NO_DELAY=y CONFIG_HTTPD_PURGE_BUF_LEN=32 CONFIG_HTTPD_WS_SUPPORT=y -CONFIG_ESP_HTTPS_SERVER_ENABLE=y \ No newline at end of file +CONFIG_ESP_HTTPS_SERVER_ENABLE=n \ No newline at end of file diff --git a/src/Config.h b/src/Config.h index ebe9ada..29bb237 100644 --- a/src/Config.h +++ b/src/Config.h @@ -4,7 +4,7 @@ #define NUKI_HUB_VERSION "9.02" #define NUKI_HUB_BUILD "unknownbuildnr" -#define NUKI_HUB_DATE "2024-10-28" +#define NUKI_HUB_DATE "2024-11-01" #define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest" #define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json" @@ -110,11 +110,9 @@ #define MQTT_QOS_LEVEL 1 #define MQTT_CLEAN_SESSIONS false #define MQTT_KEEP_ALIVE 60 -#define MQTT_STACK_SIZE 12288 #define GPIO_DEBOUNCE_TIME 200 #define CHAR_BUFFER_SIZE 4096 #define NUKI_TASK_SIZE 8192 -#define PD_TASK_SIZE 1024 #define MAX_AUTHLOG 5 #define MAX_KEYPAD 10 #define MAX_TIMECONTROL 10 @@ -122,3 +120,4 @@ #endif #define NETWORK_TASK_SIZE 12288 +#define HTTPD_TASK_SIZE 8192 diff --git a/src/MqttReceiver.h b/src/MqttReceiver.h index 5529cfa..3db350d 100644 --- a/src/MqttReceiver.h +++ b/src/MqttReceiver.h @@ -5,5 +5,5 @@ class MqttReceiver { public: - virtual void onMqttDataReceived(char* topic, int topic_len, char* data, int data_len) = 0; + virtual void onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) = 0; }; \ No newline at end of file diff --git a/src/MqttTopics.h b/src/MqttTopics.h index 3ac1515..f842b13 100644 --- a/src/MqttTopics.h +++ b/src/MqttTopics.h @@ -1,28 +1,28 @@ #pragma once -#define mqtt_topic_lock_action "/lock/action" -#define mqtt_topic_lock_status_updated "/lock/statusUpdated" -#define mqtt_topic_lock_state "/lock/state" -#define mqtt_topic_lock_ha_state "/lock/hastate" -#define mqtt_topic_lock_json "/lock/json" -#define mqtt_topic_lock_binary_state "/lock/binaryState" -#define mqtt_topic_lock_continuous_mode "/lock/continuousMode" -#define mqtt_topic_lock_ring "/lock/ring" -#define mqtt_topic_lock_binary_ring "/lock/binaryRing" -#define mqtt_topic_lock_trigger "/lock/trigger" -#define mqtt_topic_lock_last_lock_action "/lock/lastLockAction" -#define mqtt_topic_lock_log "/lock/log" -#define mqtt_topic_lock_log_latest "/lock/shortLog" -#define mqtt_topic_lock_log_rolling "/lock/rollingLog" -#define mqtt_topic_lock_log_rolling_last "/lock/lastRollingLog" -#define mqtt_topic_lock_auth_id "/lock/authorizationId" -#define mqtt_topic_lock_auth_name "/lock/authorizationName" -#define mqtt_topic_lock_completionStatus "/lock/completionStatus" -#define mqtt_topic_lock_action_command_result "/lock/commandResult" -#define mqtt_topic_lock_door_sensor_state "/lock/doorSensorState" -#define mqtt_topic_lock_rssi "/lock/rssi" -#define mqtt_topic_lock_address "/lock/address" -#define mqtt_topic_lock_retry "/lock/retry" +#define mqtt_topic_lock_action "/action" +#define mqtt_topic_lock_status_updated "/statusUpdated" +#define mqtt_topic_lock_state "/state" +#define mqtt_topic_lock_ha_state "/hastate" +#define mqtt_topic_lock_json "/json" +#define mqtt_topic_lock_binary_state "/binaryState" +#define mqtt_topic_lock_continuous_mode "/continuousMode" +#define mqtt_topic_lock_ring "/ring" +#define mqtt_topic_lock_binary_ring "/binaryRing" +#define mqtt_topic_lock_trigger "/trigger" +#define mqtt_topic_lock_last_lock_action "/lastLockAction" +#define mqtt_topic_lock_log "/log" +#define mqtt_topic_lock_log_latest "/shortLog" +#define mqtt_topic_lock_log_rolling "/rollingLog" +#define mqtt_topic_lock_log_rolling_last "/lastRollingLog" +#define mqtt_topic_lock_auth_id "/authorizationId" +#define mqtt_topic_lock_auth_name "/authorizationName" +#define mqtt_topic_lock_completionStatus "/completionStatus" +#define mqtt_topic_lock_action_command_result "/commandResult" +#define mqtt_topic_lock_door_sensor_state "/doorSensorState" +#define mqtt_topic_lock_rssi "/rssi" +#define mqtt_topic_lock_address "/address" +#define mqtt_topic_lock_retry "/retry" #define mqtt_topic_official_lock_action "/lockAction" //#define mqtt_topic_official_mode "/mode" @@ -49,11 +49,11 @@ #define mqtt_topic_config_single_lock "/configuration/singleLock" #define mqtt_topic_config_sound_level "/configuration/soundLevel" -#define mqtt_topic_query_config "/lock/query/config" -#define mqtt_topic_query_lockstate "/lock/query/lockstate" -#define mqtt_topic_query_keypad "/lock/query/keypad" -#define mqtt_topic_query_battery "/lock/query/battery" -#define mqtt_topic_query_lockstate_command_result "/lock/query/lockstateCommandResult" +#define mqtt_topic_query_config "/query/config" +#define mqtt_topic_query_lockstate "/query/lockstate" +#define mqtt_topic_query_keypad "/query/keypad" +#define mqtt_topic_query_battery "/query/battery" +#define mqtt_topic_query_lockstate_command_result "/query/lockstateCommandResult" #define mqtt_topic_battery_level "/battery/level" #define mqtt_topic_battery_critical "/battery/critical" @@ -110,9 +110,7 @@ #define mqtt_topic_restart_reason_esp "/maintenance/restartReasonNukiEsp" #define mqtt_topic_mqtt_connection_state "/maintenance/mqttConnectionState" #define mqtt_topic_network_device "/maintenance/networkDevice" -#define mqtt_hybrid_state "/maintenance/hybridConnected" - -#define mqtt_topic_presence "/presence/devices" +#define mqtt_hybrid_state "/hybridConnected" #define mqtt_topic_gpio_prefix "/gpio" #define mqtt_topic_gpio_pin "/pin_" diff --git a/src/NukiNetwork.cpp b/src/NukiNetwork.cpp index 6d46b1a..a297e4f 100644 --- a/src/NukiNetwork.cpp +++ b/src/NukiNetwork.cpp @@ -17,7 +17,8 @@ NukiNetwork* NukiNetwork::_inst = nullptr; -RTC_NOINIT_ATTR char WiFi_fallbackDetect[14]; +extern bool wifiFallback; +extern bool disableNetwork; extern bool forceEnableWebServer; extern const uint8_t x509_crt_imported_bundle_bin_start[] asm("_binary_x509_crt_bundle_start"); extern const uint8_t x509_crt_imported_bundle_bin_end[] asm("_binary_x509_crt_bundle_end"); @@ -92,13 +93,13 @@ void NukiNetwork::setupDevice() _preferences->putInt(preference_network_hardware, hardwareDetect); } - if(strcmp(WiFi_fallbackDetect, "wifi_fallback") == 0) + if(wifiFallback == true) { #ifndef CONFIG_IDF_TARGET_ESP32H2 if(!_firstBootAfterDeviceChange) { Log->println(F("Failed to connect to network. Wi-Fi fallback is disabled, rebooting.")); - memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect)); + wifiFallback = false; sleep(5); restartEsp(RestartReason::NetworkDeviceCriticalFailureNoWifiFallback); } @@ -129,6 +130,17 @@ void NukiNetwork::setupDevice() Log->print(F("Network device: ")); Log->println(_device->deviceName()); + +#ifndef NUKI_HUB_UPDATER + _device->mqttOnConnect([&](bool sessionPresent) + { + onMqttConnect(sessionPresent); + }); + _device->mqttOnDisconnect([&](espMqttClientTypes::DisconnectReason reason) + { + onMqttDisconnect(reason); + }); +#endif } void NukiNetwork::reconfigureDevice() @@ -166,14 +178,10 @@ void NukiNetwork::setKeepAliveCallback(std::function reconnectTick) _keepAliveCallback = reconnectTick; } + void NukiNetwork::clearWifiFallback() { - memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect)); -} - -NetworkDevice *NukiNetwork::device() -{ - return _device; + wifiFallback = false; } bool NukiNetwork::isConnected() @@ -181,6 +189,15 @@ bool NukiNetwork::isConnected() return _device->isConnected(); } +bool NukiNetwork::wifiConnected() +{ + if(_networkDeviceType != NetworkDeviceType::WiFi) + { + return true; + } + else return _device->isConnected(); +} + #ifdef NUKI_HUB_UPDATER void NukiNetwork::initialize() { @@ -191,6 +208,7 @@ void NukiNetwork::initialize() _hostname = "nukihub"; _preferences->putString(preference_hostname, _hostname); } + strcpy(_hostnameArr, _hostname.c_str()); _device->initialize(); @@ -206,138 +224,114 @@ bool NukiNetwork::update() #else void NukiNetwork::initialize() { - _mqtt_cfg.task.stack_size = MQTT_STACK_SIZE; - _hostname = _preferences->getString(preference_hostname, ""); - - if(_hostname == "") + _gpio->addCallback([this](const GpioAction& action, const int& pin) { - _hostname = "nukihub"; - _preferences->putString(preference_hostname, _hostname); - } + gpioActionCallback(action, pin); + }); - _mqttPort = _preferences->getInt(preference_mqtt_broker_port, 0); - - if(_mqttPort == 0) + if(!disableNetwork) { - _mqttPort = 1883; - _preferences->putInt(preference_mqtt_broker_port, _mqttPort); - } + _hostname = _preferences->getString(preference_hostname, ""); - strcpy(_hostnameArr, _hostname.c_str()); - _device->initialize(); - - Log->print(F("Host name: ")); - Log->println(_hostname); - - String brokerAddr = _preferences->getString(preference_mqtt_broker); - strcpy(_mqttBrokerAddr, brokerAddr.c_str()); - - String mqttUser = _preferences->getString(preference_mqtt_user); - if(mqttUser.length() > 0) - { - size_t len = mqttUser.length(); - for(int i=0; i < len; i++) + if(_hostname == "") { - _mqttUser[i] = mqttUser.charAt(i); + _hostname = "nukihub"; + _preferences->putString(preference_hostname, _hostname); } - } - String mqttPass = _preferences->getString(preference_mqtt_password); - if(mqttPass.length() > 0) - { - size_t len = mqttPass.length(); - for(int i=0; i < len; i++) + _mqttPort = _preferences->getInt(preference_mqtt_broker_port, 0); + + if(_mqttPort == 0) { - _mqttPass[i] = mqttPass.charAt(i); + _mqttPort = 1883; + _preferences->putInt(preference_mqtt_broker_port, _mqttPort); + } + + strcpy(_hostnameArr, _hostname.c_str()); + _device->initialize(); + + Log->print(F("Host name: ")); + Log->println(_hostname); + + String brokerAddr = _preferences->getString(preference_mqtt_broker); + strcpy(_mqttBrokerAddr, brokerAddr.c_str()); + + String mqttUser = _preferences->getString(preference_mqtt_user); + if(mqttUser.length() > 0) + { + size_t len = mqttUser.length(); + for(int i=0; i < len; i++) + { + _mqttUser[i] = mqttUser.charAt(i); + } + } + + String mqttPass = _preferences->getString(preference_mqtt_password); + if(mqttPass.length() > 0) + { + size_t len = mqttPass.length(); + for(int i=0; i < len; i++) + { + _mqttPass[i] = mqttPass.charAt(i); + + } } - } - if(strcmp(_mqttBrokerAddr, "") == 0) - { - Log->println(F("MQTT Broker not configured, aborting connection attempt.")); - } - else - { Log->print(F("MQTT Broker: ")); Log->print(_mqttBrokerAddr); Log->print(F(":")); Log->println(_mqttPort); - _mqtt_cfg.credentials.client_id = _hostnameArr; - _mqtt_cfg.session.disable_clean_session = !MQTT_CLEAN_SESSIONS; - _mqtt_cfg.session.keepalive = MQTT_KEEP_ALIVE; + _device->mqttSetClientId(_hostnameArr); + _device->mqttSetCleanSession(MQTT_CLEAN_SESSIONS); + _device->mqttSetKeepAlive(MQTT_KEEP_ALIVE); - size_t caLength = _preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE); - size_t crtLength = _preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE); - size_t keyLength = _preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE); + char gpioPath[250]; + bool rebGpio = rebuildGpio(); - if(caLength > 1) + if(rebGpio) { - Log->println(F("MQTT over TLS.")); - - String uri = "mqtts://"; - uri.concat(_preferences->getString(preference_mqtt_broker, "")); - uri.concat(":"); - uri.concat(_preferences->getInt(preference_mqtt_broker_port, 8883)); - Log->print("URI: "); - Log->println(uri.c_str()); - //_mqtt_cfg.broker.address.uri = uri.c_str(); - _mqtt_cfg.broker.address.hostname = _mqttBrokerAddr; - _mqtt_cfg.broker.address.transport = MQTT_TRANSPORT_OVER_SSL; - _mqtt_cfg.broker.address.port = _preferences->getInt(preference_mqtt_broker_port, 8883); - _mqtt_cfg.broker.verification.certificate = _ca; - - if(crtLength > 1 && keyLength > 1) // length is 1 when empty + Log->println(F("Rebuild MQTT GPIO structure")); + } + for (const auto &pinEntry: _gpio->pinConfiguration()) + { + switch (pinEntry.role) { - Log->println(F("MQTT with client certificate.")); - _mqtt_cfg.credentials.authentication.certificate = _cert; - _mqtt_cfg.credentials.authentication.key = _key; + case PinRole::GeneralInputPullDown: + case PinRole::GeneralInputPullUp: + if(rebGpio) + { + buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_role}); + publishString(_lockPath.c_str(), gpioPath, "input", false); + buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state}); + publishString(_lockPath.c_str(), gpioPath, std::to_string(digitalRead(pinEntry.pin)).c_str(), false); + } + break; + case PinRole::GeneralOutput: + if(rebGpio) + { + buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_role}); + publishString(_lockPath.c_str(), gpioPath, "output", false); + buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state}); + publishString(_lockPath.c_str(), gpioPath, "0", false); + } + buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state}); + subscribe(_lockPath.c_str(), gpioPath); + break; + default: + break; } } - else - { - Log->println(F("MQTT without TLS.")); - String uri = "mqtt://"; - uri.concat(_preferences->getString(preference_mqtt_broker, "")); - uri.concat(":"); - uri.concat(_preferences->getInt(preference_mqtt_broker_port, 1883)); - Log->print("URI: "); - Log->println(uri.c_str()); - //_mqtt_cfg.broker.address.uri = uri.c_str(); - _mqtt_cfg.broker.address.hostname = _mqttBrokerAddr; - _mqtt_cfg.broker.address.transport = MQTT_TRANSPORT_OVER_TCP; - _mqtt_cfg.broker.address.port = _preferences->getInt(preference_mqtt_broker_port, 1883); - } - if(strlen(_mqttUser) == 0) - { - Log->println(F("MQTT: Connecting without credentials")); - } - else - { - Log->print(F("MQTT: Connecting with user: ")); - Log->println(_mqttUser); - _mqtt_cfg.credentials.username = _mqttUser; - _mqtt_cfg.credentials.authentication.password = _mqttPass; - } - - _mqtt_cfg.session.last_will.topic = _mqttConnectionStateTopic; - _mqtt_cfg.session.last_will.msg = _lastWillPayload; - _mqtt_cfg.session.last_will.msg_len = sizeof(_lastWillPayload); - _mqtt_cfg.session.last_will.qos = 1; - _mqtt_cfg.session.last_will.retain = true; - - _mqttClient = esp_mqtt_client_init(&_mqtt_cfg); - esp_mqtt_client_register_event(_mqttClient, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler_cb, NULL); + _discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery, ""); + _offEnabled = _preferences->getBool(preference_official_hybrid_enabled, false); + readSettings(); } - - _discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery, ""); - _offEnabled = _preferences->getBool(preference_official_hybrid_enabled, false); - readSettings(); } void NukiNetwork::readSettings() { + _disableNetworkIfNotConnected = _preferences->getBool(preference_disable_network_not_connected, false); _restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect, false); _checkUpdates = _preferences->getBool(preference_check_updates, false); _rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval, 0) * 1000; @@ -363,15 +357,31 @@ bool NukiNetwork::update() int64_t ts = espMillis(); _device->update(); - if(!_mqttEnabled || _device->isApOpen()) + if(_networkTimeout > 0 && (ts - _lastConnectedTs > _networkTimeout * 1000) && ts > 60000) { - return true; + if(!_webEnabled) + { + forceEnableWebServer = true; + } + Log->println("Network timeout has been reached, restarting ..."); + delay(200); + restartEsp(RestartReason::NetworkTimeoutWatchdog); + } + + if(disableNetwork || !_mqttEnabled || _device->isApOpen()) + { + return false; } if(!_device->isConnected() || (_mqttConnectCounter > 15 && !_firstConnect)) { _mqttConnectCounter = 0; + if(_firstDisconnected) { + _firstDisconnected = false; + _device->mqttDisconnect(true); + } + if(!_webEnabled) { forceEnableWebServer = true; @@ -380,79 +390,11 @@ bool NukiNetwork::update() { restartEsp(RestartReason::RestartOnDisconnectWatchdog); } - } - - if(_device->isConnected() && !_mqttClientInitiated && strcmp(_mqttBrokerAddr, "") != 0) - { - Log->println(F("Attempting MQTT connection")); - esp_mqtt_client_start(_mqttClient); - - if(_preferences->getBool(preference_mqtt_log_enabled, false) || _preferences->getBool(preference_webserial_enabled, false)) + else if(_disableNetworkIfNotConnected && espMillis() > 60000) { - MqttLoggerMode mode; - - if(_preferences->getBool(preference_mqtt_log_enabled, false) && _preferences->getBool(preference_webserial_enabled, false)) - { - mode = MqttLoggerMode::MqttAndSerialAndWeb; - } - else if (_preferences->getBool(preference_webserial_enabled, false)) - { - mode = MqttLoggerMode::SerialAndWeb; - } - else - { - mode = MqttLoggerMode::MqttAndSerial; - } - - char* _path = new char[200]; - memset(_path, 0, sizeof(_path)); - - String pathStr = _preferences->getString(preference_mqtt_lock_path); - pathStr.concat(mqtt_topic_log); - strcpy(_path, pathStr.c_str()); - Log = new MqttLogger(_mqttClient, _path, mode); + disableNetwork = true; + restartEsp(RestartReason::DisableNetworkIfNotConnected); } - - char gpioPath[250]; - bool rebGpio = rebuildGpio(); - - if(rebGpio) - { - Log->println(F("Rebuild MQTT GPIO structure")); - } - for (const auto &pinEntry: _gpio->pinConfiguration()) - { - switch (pinEntry.role) - { - case PinRole::GeneralInputPullDown: - case PinRole::GeneralInputPullUp: - if(rebGpio) - { - buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_role}); - publishString(_lockPath.c_str(), gpioPath, "input", false); - buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state}); - publishString(_lockPath.c_str(), gpioPath, std::to_string(digitalRead(pinEntry.pin)).c_str(), false); - } - break; - case PinRole::GeneralOutput: - if(rebGpio) - { - buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_role}); - publishString(_lockPath.c_str(), gpioPath, "output", false); - buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state}); - publishString(_lockPath.c_str(), gpioPath, "0", false); - } - buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state}); - subscribe(_lockPath.c_str(), gpioPath); - break; - default: - break; - } - } - _gpio->addCallback([this](const GpioAction& action, const int& pin) - { - gpioActionCallback(action, pin); - }); } if(_logIp && _device->isConnected() && !_device->localIP().equals("0.0.0.0")) @@ -460,17 +402,19 @@ bool NukiNetwork::update() _logIp = false; Log->print(F("IP: ")); Log->println(_device->localIP()); + _firstDisconnected = true; } - while(!_mqttConnected && _device->isConnected()) + if(!_device->mqttConnected() && _device->isConnected()) { - delay(2000); - _mqttConnectCounter++; - return false; - } + bool success = reconnect(); + if(!success) + { + delay(2000); + _mqttConnectCounter++; + return false; + } - if(!_mqttConnected && _device->isConnected()) - { _mqttConnectCounter = 0; if(forceEnableWebServer && !_webEnabled) { @@ -478,21 +422,15 @@ bool NukiNetwork::update() delay(200); restartEsp(RestartReason::ReconfigureWebServer); } - else if(!_webEnabled) - { - forceEnableWebServer = false; - } + else if(!_webEnabled) forceEnableWebServer = false; delay(2000); } - if(!_mqttConnected || !_device->isConnected()) + if(!_device->mqttConnected() || !_device->isConnected()) { if(_networkTimeout > 0 && (ts - _lastConnectedTs > _networkTimeout * 1000) && ts > 60000) { - if(!_webEnabled) - { - forceEnableWebServer = true; - } + if(!_webEnabled) forceEnableWebServer = true; Log->println("Network timeout has been reached, restarting ..."); delay(200); restartEsp(RestartReason::NetworkTimeoutWatchdog); @@ -624,95 +562,143 @@ bool NukiNetwork::update() return true; } -void NukiNetwork::mqtt_event_handler_cb(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +void NukiNetwork::onMqttConnect(const bool &sessionPresent) { - _inst->mqtt_event_handler(handler_args, base, event_id, event_data); + _connectReplyReceived = true; } -void NukiNetwork::mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +void NukiNetwork::onMqttDisconnect(const espMqttClientTypes::DisconnectReason &reason) { - static const char *MQTT_TAG = "mqtt"; - - ESP_LOGD(MQTT_TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); - esp_mqtt_event_handle_t event = (esp_mqtt_event_t*)event_data; - esp_mqtt_client_handle_t client = event->client; - int msg_id; - switch ((esp_mqtt_event_id_t)event_id) + _connectReplyReceived = false; + Log->print("MQTT disconnected. Reason: "); + switch(reason) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED"); - Log->println("MQTT Connected"); - _mqttClientInitiated = true; - _mqttConnected = true; - reconnect(); - break; - case MQTT_EVENT_DISCONNECTED: - ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DISCONNECTED"); - Log->println("MQTT Disconnected"); - _mqttConnected = false; - reconnect(); - break; - case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(MQTT_TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_UNSUBSCRIBED: - ESP_LOGI(MQTT_TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_PUBLISHED: - ESP_LOGI(MQTT_TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_DATA: - ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DATA"); - //printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - //printf("DATA=%.*s\r\n", event->data_len, event->data); - onMqttDataReceived(event->topic, event->topic_len, event->data, event->data_len); - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(MQTT_TAG, "MQTT_EVENT_ERROR"); - if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) - { - ESP_LOGI(MQTT_TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); - } - break; - default: - ESP_LOGI(MQTT_TAG, "Other event id:%d", event->event_id); - break; + case espMqttClientTypes::DisconnectReason::USER_OK: + Log->println(F("USER_OK")); + break; + case espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION: + Log->println(F("MQTT_UNACCEPTABLE_PROTOCOL_VERSION")); + break; + case espMqttClientTypes::DisconnectReason::MQTT_IDENTIFIER_REJECTED: + Log->println(F("MQTT_IDENTIFIER_REJECTED")); + break; + case espMqttClientTypes::DisconnectReason::MQTT_SERVER_UNAVAILABLE: + Log->println(F("MQTT_SERVER_UNAVAILABLE")); + break; + case espMqttClientTypes::DisconnectReason::MQTT_MALFORMED_CREDENTIALS: + Log->println(F("MQTT_MALFORMED_CREDENTIALS")); + break; + case espMqttClientTypes::DisconnectReason::MQTT_NOT_AUTHORIZED: + Log->println(F("MQTT_NOT_AUTHORIZED")); + break; + case espMqttClientTypes::DisconnectReason::TLS_BAD_FINGERPRINT: + Log->println(F("TLS_BAD_FINGERPRINT")); + break; + case espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED: + Log->println(F("TCP_DISCONNECTED")); + break; + default: + Log->println(F("Unknown")); + break; } } bool NukiNetwork::reconnect() { - if (_mqttConnected) + _mqttConnectionState = 0; + + while (!_device->mqttConnected() && (esp_timer_get_time() / 1000) > _nextReconnect) { - _mqttConnectedTs = millis(); - _mqttConnectionState = 1; - delay(100); - for(const String& topic : _subscribedTopics) + if(strcmp(_mqttBrokerAddr, "") == 0) { - esp_mqtt_client_subscribe(_mqttClient, topic.c_str(), MQTT_QOS_LEVEL); + Log->println(F("MQTT Broker not configured, aborting connection attempt.")); + _nextReconnect = (esp_timer_get_time() / 1000) + 5000; + return false; } - if(_firstConnect) + + Log->println(F("Attempting MQTT connection")); + + _connectReplyReceived = false; + + if(strlen(_mqttUser) == 0) { - _firstConnect = false; - publishString(_maintenancePathPrefix, mqtt_topic_network_device, _device->deviceName().c_str(), true); - for(const auto& it : _initTopics) + Log->println(F("MQTT: Connecting without credentials")); + } + else + { + Log->print(F("MQTT: Connecting with user: ")); Log->println(_mqttUser); + _device->mqttSetCredentials(_mqttUser, _mqttPass); + } + + _device->setWill(_mqttConnectionStateTopic, 1, true, _lastWillPayload); + _device->mqttSetServer(_mqttBrokerAddr, _mqttPort); + _device->mqttConnect(); + + int64_t timeout = (esp_timer_get_time() / 1000) + 60000; + + while(!_connectReplyReceived && (esp_timer_get_time() / 1000) < timeout) + { + delay(50); + _device->update(); + if(_keepAliveCallback != nullptr) { - esp_mqtt_client_publish(_mqttClient, it.first.c_str(), it.second.c_str(), 0, MQTT_QOS_LEVEL, 1); + _keepAliveCallback(); } } - publishString(_maintenancePathPrefix, mqtt_topic_mqtt_connection_state, "online", true); - publishString(_maintenancePathPrefix, mqtt_topic_info_nuki_hub_ip, _device->localIP().c_str(), true); - - _mqttConnectionState = 2; - for(const auto& callback : _reconnectedCallbacks) + if (_device->mqttConnected()) { - callback(); + Log->println(F("MQTT connected")); + _mqttConnectedTs = millis(); + _mqttConnectionState = 1; + delay(100); + _device->mqttOnMessage(onMqttDataReceivedCallback); + + initTopic(_maintenancePathPrefix, mqtt_topic_reset, "0"); + subscribe(_maintenancePathPrefix, mqtt_topic_reset); + + if(_preferences->getBool(preference_update_from_mqtt, false)) + { + initTopic(_maintenancePathPrefix, mqtt_topic_update, "0"); + subscribe(_maintenancePathPrefix, mqtt_topic_update); + } + + initTopic(_maintenancePathPrefix, mqtt_topic_webserver_action, "--"); + subscribe(_maintenancePathPrefix, mqtt_topic_webserver_action); + initTopic(_maintenancePathPrefix, mqtt_topic_webserver_state, (_preferences->getBool(preference_webserver_enabled, true) || forceEnableWebServer ? "1" : "0")); + + if(_firstConnect) + { + _firstConnect = false; + publishString(_maintenancePathPrefix, mqtt_topic_network_device, _device->deviceName().c_str(), true); + for(const auto& it : _initTopics) + { + _device->mqttPublish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str()); + } + } + + for(const String& topic : _subscribedTopics) + { + _device->mqttSubscribe(topic.c_str(), MQTT_QOS_LEVEL); + } + + publishString(_maintenancePathPrefix, mqtt_topic_mqtt_connection_state, "online", true); + publishString(_maintenancePathPrefix, mqtt_topic_info_nuki_hub_ip, _device->localIP().c_str(), true); + + _mqttConnectionState = 2; + for(const auto& callback : _reconnectedCallbacks) + { + callback(); + } + } + else + { + Log->print(F("MQTT connect failed, rc=")); + _device->printError(); + _mqttConnectionState = 0; + _nextReconnect = (esp_timer_get_time() / 1000) + 5000; + //_device->mqttDisconnect(true); } - } - else - { - _mqttConnectionState = 0; } return _mqttConnectionState > 0; } @@ -764,26 +750,34 @@ void NukiNetwork::registerMqttReceiver(MqttReceiver* receiver) _mqttReceivers.push_back(receiver); } -void NukiNetwork::onMqttDataReceived(char* topic, int topic_len, char* data, int data_len) +void NukiNetwork::onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { - char value[800] = {0}; - for(int i=0; ionMqttDataReceived(properties, topic, value, len, index, total); +} + +void NukiNetwork::onMqttDataReceived(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t& len, size_t& index, size_t& total) +{ + if(_mqttConnectedTs == -1 || (millis() - _mqttConnectedTs < 2000)) return; + + parseGpioTopics(properties, topic, payload, len, index, total); for(auto receiver : _mqttReceivers) { - receiver->onMqttDataReceived(topic, topic_len, (char*)value, data_len); + receiver->onMqttDataReceived(topic, (byte*)payload, index); } } -void NukiNetwork::parseGpioTopics(char* topic, int topic_len, char* data, int data_len) +void NukiNetwork::parseGpioTopics(const espMqttClientTypes::MessageProperties &properties, const char *topic, const uint8_t *payload, size_t& len, size_t& index, size_t& total) { char gpioPath[250]; buildMqttPath(gpioPath, {_lockPath.c_str(), mqtt_topic_gpio_prefix, mqtt_topic_gpio_pin}); @@ -802,7 +796,7 @@ void NukiNetwork::parseGpioTopics(char* topic, int topic_len, char* data, int da if(_gpio->getPinRole(pin) == PinRole::GeneralOutput) { - const uint8_t pinState = strcmp(data, "1") == 0 ? HIGH : LOW; + const uint8_t pinState = strcmp((const char*)payload, "1") == 0 ? HIGH : LOW; Log->print(F("GPIO ")); Log->print(pin); Log->print(F(" (Output) --> ")); @@ -843,62 +837,42 @@ bool NukiNetwork::pathEquals(const char* prefix, const char* path, const char* r void NukiNetwork::publishFloat(const char* prefix, const char* topic, const float value, bool retain, const uint8_t precision) { - if(!_mqttClientInitiated) - { - return; - } char str[30]; dtostrf(value, 0, precision, str); char path[200] = {0}; buildMqttPath(path, { prefix, topic }); - esp_mqtt_client_publish(_mqttClient, path, str, 0, MQTT_QOS_LEVEL, retain); + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, str); } void NukiNetwork::publishInt(const char* prefix, const char *topic, const int value, bool retain) { - if(!_mqttClientInitiated) - { - return; - } char str[30]; itoa(value, str, 10); char path[200] = {0}; buildMqttPath(path, { prefix, topic }); - esp_mqtt_client_publish(_mqttClient, path, str, 0, MQTT_QOS_LEVEL, retain); + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, str); } void NukiNetwork::publishUInt(const char* prefix, const char *topic, const unsigned int value, bool retain) { - if(!_mqttClientInitiated) - { - return; - } char str[30]; utoa(value, str, 10); char path[200] = {0}; buildMqttPath(path, { prefix, topic }); - esp_mqtt_client_publish(_mqttClient, path, str, 0, MQTT_QOS_LEVEL, retain); + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, str); } void NukiNetwork::publishULong(const char* prefix, const char *topic, const unsigned long value, bool retain) { - if(!_mqttClientInitiated) - { - return; - } char str[30]; utoa(value, str, 10); char path[200] = {0}; buildMqttPath(path, { prefix, topic }); - esp_mqtt_client_publish(_mqttClient, path, str, 0, MQTT_QOS_LEVEL, retain); + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, str); } void NukiNetwork::publishLongLong(const char* prefix, const char *topic, int64_t value, bool retain) { - if(!_mqttClientInitiated) - { - return; - } static char result[21] = ""; memset(&result[0], 0, sizeof(result)); char temp[21] = ""; @@ -915,39 +889,27 @@ void NukiNetwork::publishLongLong(const char* prefix, const char *topic, int64_t } char path[200] = {0}; buildMqttPath(path, { prefix, topic }); - esp_mqtt_client_publish(_mqttClient, path, result, 0, MQTT_QOS_LEVEL, retain); + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, result); } void NukiNetwork::publishBool(const char* prefix, const char *topic, const bool value, bool retain) { - if(!_mqttClientInitiated) - { - return; - } char str[2] = {0}; str[0] = value ? '1' : '0'; char path[200] = {0}; buildMqttPath(path, { prefix, topic }); - esp_mqtt_client_publish(_mqttClient, path, str, 0, MQTT_QOS_LEVEL, retain); + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, str); } -bool NukiNetwork::publishString(const char* prefix, const char *topic, const char *value, bool retain) +void NukiNetwork::publishString(const char* prefix, const char *topic, const char *value, bool retain) { - if(!_mqttClientInitiated) - { - return false; - } char path[200] = {0}; buildMqttPath(path, { prefix, topic }); - return esp_mqtt_client_publish(_mqttClient, path, value, 0, MQTT_QOS_LEVEL, retain) > 0; + _device->mqttPublish(path, MQTT_QOS_LEVEL, retain, value); } void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, const char *softwareVersion, const char *hardwareVersion, const char* availabilityTopic, const bool& hasKeypad, char* lockAction, char* unlockAction, char* openAction) { - if(!_mqttClientInitiated) - { - return; - } JsonDocument json; json.clear(); JsonObject dev = json["dev"].to(); @@ -1003,7 +965,7 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha path.concat(uidString); path.concat("/smartlock/config"); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); // Battery critical publishHassTopic("binary_sensor", @@ -1184,6 +1146,9 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha if(_offEnabled) { // Hybrid connected + String hybridPath = _lockPath; + hybridPath.concat("/lock"); + hybridPath.concat(mqtt_hybrid_state); publishHassTopic("binary_sensor", "hybrid_connected", uidString, @@ -1191,7 +1156,7 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha "Hybrid connected", name, baseTopic, - _lockPath + mqtt_hybrid_state, + hybridPath, deviceType, "", "", @@ -1488,10 +1453,6 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, const char *baseTopic, char *name, char *uidString) { - if(!_mqttClientInitiated) - { - return; - } uint32_t aclPrefs[17]; _preferences->getBytes(preference_acl, &aclPrefs, sizeof(aclPrefs)); @@ -1930,7 +1891,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons json["options"][4] = "Intelligent"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "fob_action_1", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -1948,7 +1909,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons json["options"][4] = "Intelligent"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "fob_action_2", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -1966,7 +1927,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons json["options"][4] = "Intelligent"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "fob_action_3", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -1983,7 +1944,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons json["options"][3] = "Slowest"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "advertising_mode", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2044,7 +2005,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "timezone", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2204,7 +2165,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons json["options"][6] = "Show Status"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "single_button_press_action", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2224,7 +2185,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons json["options"][6] = "Show Status"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "double_button_press_action", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2270,7 +2231,7 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons json["options"][2] = "Lithium"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "battery_type", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2606,10 +2567,6 @@ void NukiNetwork::publishHASSConfigAdditionalLockEntities(char *deviceType, cons void NukiNetwork::publishHASSConfigDoorSensor(char *deviceType, const char *baseTopic, char *name, char *uidString) { - if(!_mqttClientInitiated) - { - return; - } publishHassTopic("binary_sensor", "door_sensor", uidString, @@ -2632,10 +2589,6 @@ void NukiNetwork::publishHASSConfigDoorSensor(char *deviceType, const char *base void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, const char *baseTopic, char *name, char *uidString) { - if(!_mqttClientInitiated) - { - return; - } uint32_t aclPrefs[17]; _preferences->getBytes(preference_acl, &aclPrefs, sizeof(aclPrefs)); uint32_t basicOpenerConfigAclPrefs[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; @@ -2743,7 +2696,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["event_types"][1] = "ringlocked"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("event", "ring", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); if((int)basicOpenerConfigAclPrefs[5] == 1) { @@ -2939,7 +2892,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][5] = "Ring"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "fob_action_1", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2958,7 +2911,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][5] = "Ring"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "fob_action_2", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2977,7 +2930,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][5] = "Ring"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "fob_action_3", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -2994,7 +2947,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][3] = "Slowest"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "advertising_mode", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3055,7 +3008,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "timezone", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3084,7 +3037,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][15] = "Spare"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "operating_mode", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3308,7 +3261,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][7] = "CM & RTO & Ring"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "doorbell_suppression", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3354,7 +3307,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][3] = "Sound 3"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "sound_ring", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3371,7 +3324,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][3] = "Sound 3"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "sound_open", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3388,7 +3341,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][3] = "Sound 3"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "sound_rto", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3405,7 +3358,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][3] = "Sound 3"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "sound_cm", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3456,7 +3409,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][7] = "Open"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "single_button_press_action", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3477,7 +3430,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][7] = "Open"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "double_button_press_action", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3493,7 +3446,7 @@ void NukiNetwork::publishHASSConfigAdditionalOpenerEntities(char *deviceType, co json["options"][2] = "Lithium"; serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath("select", "battery_type", uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } else { @@ -3674,17 +3627,13 @@ void NukiNetwork::publishHassTopic(const String& mqttDeviceType, std::vector> additionalEntries ) { - if(!_mqttClientInitiated) - { - return; - } if (_discoveryTopic != "") { JsonDocument json; json = createHassJson(uidString, uidStringPostfix, displayName, name, baseTopic, stateTopic, deviceType, deviceClass, stateClass, entityCat, commandTopic, additionalEntries); serializeJson(json, _buffer, _bufferSize); String path = createHassTopicPath(mqttDeviceType, mqttDeviceName, uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), _buffer, 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer); } } @@ -3704,26 +3653,18 @@ String NukiNetwork::createHassTopicPath(const String& mqttDeviceType, const Stri void NukiNetwork::removeHassTopic(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString) { - if(!_mqttClientInitiated) - { - return; - } if (_discoveryTopic != "") { String path = createHassTopicPath(mqttDeviceType, mqttDeviceName, uidString); - esp_mqtt_client_publish(_mqttClient, path.c_str(), "", 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); } } void NukiNetwork::removeTopic(const String& mqttPath, const String& mqttTopic) { - if(!_mqttClientInitiated) - { - return; - } String path = mqttPath; path.concat(mqttTopic); - esp_mqtt_client_publish(_mqttClient, path.c_str(), "", 0, MQTT_QOS_LEVEL, 1); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); #ifdef DEBUG_NUKIHUB Log->print(F("Removing MQTT topic: ")); @@ -4099,11 +4040,7 @@ void NukiNetwork::timeZoneIdToString(const Nuki::TimeZoneId timeZoneId, char* st uint16_t NukiNetwork::subscribe(const char *topic, uint8_t qos) { - if(!_mqttClientInitiated) - { - return -1; - } - return esp_mqtt_client_subscribe(_mqttClient, topic, qos); + return _device->mqttSubscribe(topic, qos); } void NukiNetwork::addReconnectedCallback(std::function reconnectedCallback) @@ -4113,7 +4050,7 @@ void NukiNetwork::addReconnectedCallback(std::function reconnectedCallba void NukiNetwork::disableMqtt() { - esp_mqtt_client_disconnect(_mqttClient); + _device->disableMqtt(); _mqttEnabled = false; } diff --git a/src/NukiNetwork.h b/src/NukiNetwork.h index 6908686..517d670 100644 --- a/src/NukiNetwork.h +++ b/src/NukiNetwork.h @@ -11,7 +11,6 @@ #ifndef NUKI_HUB_UPDATER #include "MqttReceiver.h" -#include "mqtt_client.h" #include "MqttTopics.h" #include "Gpio.h" #include @@ -28,6 +27,7 @@ public: void scan(bool passive = false, bool async = true); bool isApOpen(); bool isConnected(); + bool wifiConnected(); void clearWifiFallback(); const String networkDeviceName() const; @@ -55,7 +55,7 @@ public: void publishULong(const char* prefix, const char* topic, const unsigned long value, bool retain); void publishLongLong(const char* prefix, const char* topic, int64_t value, bool retain); void publishBool(const char* prefix, const char* topic, const bool value, bool retain); - bool publishString(const char* prefix, const char* topic, const char* value, bool retain); + void publishString(const char* prefix, const char* topic, const char* value, bool retain); void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, const char *softwareVersion, const char *hardwareVersion, const char* availabilityTopic, const bool& hasKeypad, char* lockAction, char* unlockAction, char* openAction); void publishHASSConfigAdditionalLockEntities(char* deviceType, const char* baseTopic, char* name, char* uidString); @@ -91,7 +91,6 @@ public: bool mqttRecentlyConnected(); bool pathEquals(const char* prefix, const char* path, const char* referencePath); uint16_t subscribe(const char* topic, uint8_t qos); - void addReconnectedCallback(std::function reconnectedCallback); #endif private: @@ -106,8 +105,8 @@ private: IPConfiguration* _ipConfiguration = nullptr; String _hostname; char _hostnameArr[101] = {0}; + char _nukiHubPath[181] = {0}; NetworkDevice* _device = nullptr; - std::function _keepAliveCallback = nullptr; std::vector> _reconnectedCallbacks; @@ -118,10 +117,11 @@ private: bool _offEnabled = false; #ifndef NUKI_HUB_UPDATER - static void mqtt_event_handler_cb(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data); - void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data); - void onMqttDataReceived(char* topic, int topic_len, char* data, int data_len); - void parseGpioTopics(char* topic, int topic_len, char* data, int data_len); + 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 onMqttConnect(const bool& sessionPresent); + void onMqttDisconnect(const espMqttClientTypes::DisconnectReason& reason); + void parseGpioTopics(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t& len, size_t& index, size_t& total); void gpioActionCallback(const GpioAction& action, const int& pin); String createHassTopicPath(const String& mqttDeviceType, const String& mqttDeviceName, const String& uidString); @@ -144,23 +144,17 @@ private: char _mqttConnectionStateTopic[211] = {0}; String _lockPath; String _discoveryTopic; + String _brokerAddr; Gpio* _gpio; - - esp_mqtt_client_config_t _mqtt_cfg = { 0 }; - bool _mqttClientInitiated = false; + int _mqttConnectionState = 0; - bool _mqttConnected = false; int _mqttConnectCounter = 0; int _mqttPort = 1883; long _mqttConnectedTs = -1; + bool _connectReplyReceived = false; bool _firstDisconnected = true; - esp_mqtt_client_handle_t _mqttClient; - char _ca[TLS_CA_MAX_SIZE] = {0}; - char _cert[TLS_CERT_MAX_SIZE] = {0}; - char _key[TLS_KEY_MAX_SIZE] = {0}; - int64_t _nextReconnect = 0; char _mqttBrokerAddr[101] = {0}; char _mqttUser[31] = {0}; @@ -170,6 +164,7 @@ private: int _networkTimeout = 0; std::vector _mqttReceivers; bool _restartOnDisconnect = false; + bool _disableNetworkIfNotConnected = false; bool _checkUpdates = false; bool _firstConnect = true; bool _publishDebugInfo = false; @@ -177,6 +172,7 @@ private: std::vector _subscribedTopics; std::map _initTopics; int64_t _lastConnectedTs = 0; + int64_t _lastMQTTConnectionAttemptTs = 0; int64_t _lastMaintenanceTs = 0; int64_t _lastUpdateCheckTs = 0; int64_t _lastRssiTs = 0; diff --git a/src/NukiNetworkLock.cpp b/src/NukiNetworkLock.cpp index 4397650..9855787 100644 --- a/src/NukiNetworkLock.cpp +++ b/src/NukiNetworkLock.cpp @@ -37,18 +37,12 @@ NukiNetworkLock::~NukiNetworkLock() void NukiNetworkLock::initialize() { String mqttPath = _preferences->getString(preference_mqtt_lock_path, ""); - if(mqttPath.length() > 0) + mqttPath.concat("/lock"); + + size_t len = mqttPath.length(); + for(int i=0; i < len; i++) { - size_t len = mqttPath.length(); - for(int i=0; i < len; i++) - { - _mqttPath[i] = mqttPath.charAt(i); - } - } - else - { - strcpy(_mqttPath, "nuki"); - _preferences->putString(preference_mqtt_lock_path, _mqttPath); + _mqttPath[i] = mqttPath.charAt(i); } _haEnabled = _preferences->getString(preference_mqtt_hass_discovery, "") != ""; @@ -58,18 +52,6 @@ void NukiNetworkLock::initialize() _network->subscribe(_mqttPath, mqtt_topic_lock_action); _network->initTopic(_mqttPath, mqtt_topic_config_action, "--"); _network->subscribe(_mqttPath, mqtt_topic_config_action); - _network->subscribe(_mqttPath, mqtt_topic_reset); - _network->initTopic(_mqttPath, mqtt_topic_reset, "0"); - - if(_preferences->getBool(preference_update_from_mqtt, false)) - { - _network->subscribe(_mqttPath, mqtt_topic_update); - _network->initTopic(_mqttPath, mqtt_topic_update, "0"); - } - - _network->subscribe(_mqttPath, mqtt_topic_webserver_action); - _network->initTopic(_mqttPath, mqtt_topic_webserver_action, "--"); - _network->initTopic(_mqttPath, mqtt_topic_webserver_state, (_preferences->getBool(preference_webserver_enabled, true) || forceEnableWebServer ? "1" : "0")); _network->initTopic(_mqttPath, mqtt_topic_query_config, "0"); _network->initTopic(_mqttPath, mqtt_topic_query_lockstate, "0"); @@ -100,7 +82,6 @@ void NukiNetworkLock::initialize() _network->removeTopic(_mqttPath, mqtt_topic_battery_max_turn_current); _network->removeTopic(_mqttPath, mqtt_topic_battery_lock_distance); _network->removeTopic(_mqttPath, mqtt_topic_battery_keypad_critical); - //_network->removeTopic(_mqttPath, mqtt_topic_presence); } if(!_preferences->getBool(preference_conf_info_enabled, true)) @@ -119,34 +100,34 @@ void NukiNetworkLock::initialize() { if(!_disableNonJSON) { - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); } - _network->subscribe(_mqttPath, mqtt_topic_query_keypad); - _network->subscribe(_mqttPath, mqtt_topic_keypad_json_action); _network->initTopic(_mqttPath, mqtt_topic_query_keypad, "0"); _network->initTopic(_mqttPath, mqtt_topic_keypad_json_action, "--"); + _network->subscribe(_mqttPath, mqtt_topic_query_keypad); + _network->subscribe(_mqttPath, mqtt_topic_keypad_json_action); } if(_preferences->getBool(preference_timecontrol_control_enabled)) { - _network->subscribe(_mqttPath, mqtt_topic_timecontrol_action); _network->initTopic(_mqttPath, mqtt_topic_timecontrol_action, "--"); + _network->subscribe(_mqttPath, mqtt_topic_timecontrol_action); } if(_preferences->getBool(preference_auth_control_enabled)) { - _network->subscribe(_mqttPath, mqtt_topic_auth_action); _network->initTopic(_mqttPath, mqtt_topic_auth_action, "--"); + _network->subscribe(_mqttPath, mqtt_topic_auth_action); } if(_nukiOfficial->getOffEnabled()) @@ -163,7 +144,7 @@ void NukiNetworkLock::initialize() { _network->subscribe(_mqttPath, mqtt_topic_lock_log_rolling_last); } - + _network->addReconnectedCallback([&]() { _reconnected = true; @@ -178,8 +159,10 @@ void NukiNetworkLock::update() } } -void NukiNetworkLock::onMqttDataReceived(char* topic, int topic_len, char* data, int data_len) +void NukiNetworkLock::onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) { + char* data = (char*)payload; + if(_network->mqttRecentlyConnected() && _network->pathEquals(_mqttPath, mqtt_topic_lock_action, topic)) { Log->println("MQTT recently connected, ignoring lock action."); @@ -445,22 +428,22 @@ void NukiNetworkLock::onMqttDataReceived(char* topic, int topic_len, char* data, if(comparePrefixedPath(topic, mqtt_topic_query_config) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_CONFIG; - publishString(mqtt_topic_query_config, "0", true); + publishInt(mqtt_topic_query_config, 0, true); } else if(comparePrefixedPath(topic, mqtt_topic_query_lockstate) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_LOCKSTATE; - publishString(mqtt_topic_query_lockstate, "0", true); + publishInt(mqtt_topic_query_lockstate, 0, true); } else if(comparePrefixedPath(topic, mqtt_topic_query_keypad) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_KEYPAD; - publishString(mqtt_topic_query_keypad, "0", true); + publishInt(mqtt_topic_query_keypad, 0, true); } else if(comparePrefixedPath(topic, mqtt_topic_query_battery) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_BATTERY; - publishString(mqtt_topic_query_battery, "0", true); + publishInt(mqtt_topic_query_battery, 0, true); } if(comparePrefixedPath(topic, mqtt_topic_config_action)) @@ -1053,6 +1036,7 @@ void NukiNetworkLock::publishKeypad(const std::list& entr char uidString[20]; itoa(_preferences->getUInt(preference_nuki_id_lock, 0), uidString, 16); String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/lock"); JsonDocument json; for(const auto& entry : entries) @@ -1296,6 +1280,7 @@ void NukiNetworkLock::publishTimeControl(const std::listgetUInt(preference_nuki_id_lock, 0), uidString, 16); String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/lock"); JsonDocument json; for(const auto& entry : timeControlEntries) @@ -1428,6 +1413,7 @@ void NukiNetworkLock::publishAuth(const std::list& char uidString[20]; itoa(_preferences->getUInt(preference_nuki_id_lock, 0), uidString, 16); String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/lock"); JsonDocument json; for(const auto& entry : authEntries) @@ -1680,7 +1666,9 @@ bool NukiNetworkLock::comparePrefixedPath(const char *fullPath, const char *subP void NukiNetworkLock::publishHASSConfig(char *deviceType, const char *baseTopic, char *name, char *uidString, const char *softwareVersion, const char *hardwareVersion, const bool& hasDoorSensor, const bool& hasKeypad, const bool& publishAuthData, char *lockAction, char *unlockAction, char *openAction) { - _network->publishHASSConfig(deviceType, baseTopic, name, uidString, softwareVersion, hardwareVersion, "~/maintenance/mqttConnectionState", hasKeypad, lockAction, unlockAction, openAction); + String availabilityTopic = _preferences->getString(preference_mqtt_lock_path); + availabilityTopic.concat("/maintenance/mqttConnectionState"); + _network->publishHASSConfig(deviceType, baseTopic, name, uidString, softwareVersion, hardwareVersion, availabilityTopic.c_str(), hasKeypad, lockAction, unlockAction, openAction); _network->publishHASSConfigAdditionalLockEntities(deviceType, baseTopic, name, uidString); if(hasDoorSensor) @@ -1719,57 +1707,63 @@ void NukiNetworkLock::publishHASSConfig(char *deviceType, const char *baseTopic, void NukiNetworkLock::removeHASSConfig(char *uidString) { - return _network->removeHASSConfig(uidString); + _network->removeHASSConfig(uidString); } void NukiNetworkLock::publishOffAction(const int value) { - return _network->publishInt(_nukiOfficial->getMqttPath(), mqtt_topic_official_lock_action, value, false); + _network->publishInt(_nukiOfficial->getMqttPath(), mqtt_topic_official_lock_action, value, false); } void NukiNetworkLock::publishFloat(const char *topic, const float value, bool retain, const uint8_t precision) { - return _nukiPublisher->publishFloat(topic, value, retain, precision); + _nukiPublisher->publishFloat(topic, value, retain, precision); } void NukiNetworkLock::publishInt(const char *topic, const int value, bool retain) { - return _nukiPublisher->publishInt(topic, value, retain); + _nukiPublisher->publishInt(topic, value, retain); } void NukiNetworkLock::publishUInt(const char *topic, const unsigned int value, bool retain) { - return _nukiPublisher->publishUInt(topic, value, retain); + _nukiPublisher->publishUInt(topic, value, retain); } void NukiNetworkLock::publishBool(const char *topic, const bool value, bool retain) { - return _nukiPublisher->publishBool(topic, value, retain); + _nukiPublisher->publishBool(topic, value, retain); } -bool NukiNetworkLock::publishString(const char *topic, const String &value, bool retain) +void NukiNetworkLock::publishString(const char *topic, const String &value, bool retain) { - return _nukiPublisher->publishString(topic, value, retain); + char str[value.length() + 1]; + memset(str, 0, sizeof(str)); + memcpy(str, value.begin(), value.length()); + publishString(topic, str, retain); } -bool NukiNetworkLock::publishString(const char *topic, const std::string &value, bool retain) +void NukiNetworkLock::publishString(const char *topic, const std::string &value, bool retain) { - return _nukiPublisher->publishString(topic, value, retain); + char str[value.size() + 1]; + memset(str, 0, sizeof(str)); + memcpy(str, value.data(), value.length()); + publishString(topic, str, retain); } -bool NukiNetworkLock::publishString(const char *topic, const char *value, bool retain) +void NukiNetworkLock::publishString(const char *topic, const char *value, bool retain) { - return _nukiPublisher->publishString(topic, value, retain); + _nukiPublisher->publishString(topic, value, retain); } void NukiNetworkLock::publishULong(const char *topic, const unsigned long value, bool retain) { - return _nukiPublisher->publishULong(topic, value, retain); + _nukiPublisher->publishULong(topic, value, retain); } void NukiNetworkLock::publishLongLong(const char *topic, int64_t value, bool retain) { - return _nukiPublisher->publishLongLong(topic, value, retain); + _nukiPublisher->publishLongLong(topic, value, retain); } String NukiNetworkLock::concat(String a, String b) @@ -1786,6 +1780,11 @@ bool NukiNetworkLock::reconnected() return r; } +int NukiNetworkLock::mqttConnectionState() +{ + return _network->mqttConnectionState(); +} + uint8_t NukiNetworkLock::queryCommands() { uint8_t qc = _queryCommands; diff --git a/src/NukiNetworkLock.h b/src/NukiNetworkLock.h index fd045a1..1dbdb3d 100644 --- a/src/NukiNetworkLock.h +++ b/src/NukiNetworkLock.h @@ -57,7 +57,7 @@ public: void setKeypadJsonCommandReceivedCallback(void (*keypadJsonCommandReceivedReceivedCallback)(const char* value)); void setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char* value)); void setAuthCommandReceivedCallback(void (*authCommandReceivedReceivedCallback)(const char* value)); - void onMqttDataReceived(char* topic, int topic_len, char* data, int data_len) override; + void onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) override; void publishFloat(const char* topic, const float value, bool retain, const uint8_t precision = 2); void publishInt(const char* topic, const int value, bool retain); @@ -65,12 +65,12 @@ public: void publishULong(const char* topic, const unsigned long value, bool retain); void publishLongLong(const char* topic, int64_t value, bool retain); void publishBool(const char* topic, const bool value, bool retain); - bool publishString(const char* topic, const String& value, bool retain); - bool publishString(const char* topic, const std::string& value, bool retain); - bool publishString(const char* topic, const char* value, bool retain); + void publishString(const char* topic, const String& value, bool retain); + void publishString(const char* topic, const std::string& value, bool retain); + void publishString(const char* topic, const char* value, bool retain); const uint32_t getAuthId() const; - + int mqttConnectionState(); bool reconnected(); uint8_t queryCommands(); @@ -99,7 +99,7 @@ private: bool _firstTunerStatePublish = true; int64_t _lastMaintenanceTs = 0; bool _haEnabled = false; - bool _reconnected = false; + bool _reconnected = false; //SETBACK bool _disableNonJSON = false; String _keypadCommandName = ""; diff --git a/src/NukiNetworkOpener.cpp b/src/NukiNetworkOpener.cpp index 71069c1..b1e2692 100644 --- a/src/NukiNetworkOpener.cpp +++ b/src/NukiNetworkOpener.cpp @@ -22,24 +22,18 @@ NukiNetworkOpener::NukiNetworkOpener(NukiNetwork* network, Preferences* preferen void NukiNetworkOpener::initialize() { - String mqttPath = _preferences->getString(preference_mqtt_opener_path); - if(mqttPath.length() > 0) + String mqttPath = _preferences->getString(preference_mqtt_lock_path, ""); + mqttPath.concat("/opener"); + + size_t len = mqttPath.length(); + for(int i=0; i < len; i++) { - size_t len = mqttPath.length(); - for(int i=0; i < len; i++) - { - _mqttPath[i] = mqttPath.charAt(i); - } - } - else - { - strcpy(_mqttPath, "nukiopener"); - _preferences->putString(preference_mqtt_opener_path, _mqttPath); + _mqttPath[i] = mqttPath.charAt(i); } _haEnabled = _preferences->getString(preference_mqtt_hass_discovery, "") != ""; _disableNonJSON = _preferences->getBool(preference_disable_non_json, false); - + _network->initTopic(_mqttPath, mqtt_topic_lock_action, "--"); _network->subscribe(_mqttPath, mqtt_topic_lock_action); _network->initTopic(_mqttPath, mqtt_topic_config_action, "--"); @@ -52,7 +46,7 @@ void NukiNetworkOpener::initialize() _network->subscribe(_mqttPath, mqtt_topic_query_config); _network->subscribe(_mqttPath, mqtt_topic_query_lockstate); _network->subscribe(_mqttPath, mqtt_topic_query_battery); - + if(_disableNonJSON) { _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_action); @@ -69,7 +63,6 @@ void NukiNetworkOpener::initialize() _network->removeTopic(_mqttPath, mqtt_topic_battery_charging); _network->removeTopic(_mqttPath, mqtt_topic_battery_voltage); _network->removeTopic(_mqttPath, mqtt_topic_battery_keypad_critical); - //_network->removeTopic(_mqttPath, mqtt_topic_presence); } if(!_preferences->getBool(preference_conf_info_enabled, true)) @@ -88,34 +81,34 @@ void NukiNetworkOpener::initialize() { if(!_disableNonJSON) { + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); } - _network->subscribe(_mqttPath, mqtt_topic_query_keypad); - _network->subscribe(_mqttPath, mqtt_topic_keypad_json_action); _network->initTopic(_mqttPath, mqtt_topic_query_keypad, "0"); _network->initTopic(_mqttPath, mqtt_topic_keypad_json_action, "--"); + _network->subscribe(_mqttPath, mqtt_topic_query_keypad); + _network->subscribe(_mqttPath, mqtt_topic_keypad_json_action); } if(_preferences->getBool(preference_timecontrol_control_enabled, false)) { - _network->subscribe(_mqttPath, mqtt_topic_timecontrol_action); _network->initTopic(_mqttPath, mqtt_topic_timecontrol_action, "--"); + _network->subscribe(_mqttPath, mqtt_topic_timecontrol_action); } if(_preferences->getBool(preference_auth_control_enabled)) { - _network->subscribe(_mqttPath, mqtt_topic_auth_action); _network->initTopic(_mqttPath, mqtt_topic_auth_action, "--"); + _network->subscribe(_mqttPath, mqtt_topic_auth_action); } if(_preferences->getBool(preference_publish_authdata, false)) @@ -124,9 +117,9 @@ void NukiNetworkOpener::initialize() } _network->addReconnectedCallback([&]() - { - _reconnected = true; - }); + { + _reconnected = true; + }); } void NukiNetworkOpener::update() @@ -138,8 +131,10 @@ void NukiNetworkOpener::update() } } -void NukiNetworkOpener::onMqttDataReceived(char* topic, int topic_len, char* data, int data_len) +void NukiNetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) { + char* data = (char*)payload; + if(_network->mqttRecentlyConnected() && _network->pathEquals(_mqttPath, mqtt_topic_lock_action, topic)) { Log->println("MQTT recently connected, ignoring opener action."); @@ -246,22 +241,22 @@ void NukiNetworkOpener::onMqttDataReceived(char* topic, int topic_len, char* dat if(comparePrefixedPath(topic, mqtt_topic_query_config) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_CONFIG; - publishString(mqtt_topic_query_config, "0", true); + publishInt(mqtt_topic_query_config, 0, true); } else if(comparePrefixedPath(topic, mqtt_topic_query_lockstate) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_LOCKSTATE; - publishString(mqtt_topic_query_lockstate, "0", true); + publishInt(mqtt_topic_query_lockstate, 0, true); } else if(comparePrefixedPath(topic, mqtt_topic_query_keypad) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_KEYPAD; - publishString(mqtt_topic_query_keypad, "0", true); + publishInt(mqtt_topic_query_keypad, 0, true); } else if(comparePrefixedPath(topic, mqtt_topic_query_battery) && strcmp(data, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_BATTERY; - publishString(mqtt_topic_query_battery, "0", true); + publishInt(mqtt_topic_query_battery, 0, true); } if(comparePrefixedPath(topic, mqtt_topic_config_action)) @@ -849,7 +844,7 @@ void NukiNetworkOpener::publishBleAddress(const std::string &address) void NukiNetworkOpener::publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, const char *softwareVersion, const char *hardwareVersion, const bool& publishAuthData, const bool& hasKeypad, char* lockAction, char* unlockAction, char* openAction) { - String availabilityTopic = _preferences->getString("mqttpath"); + String availabilityTopic = _preferences->getString(preference_mqtt_lock_path); availabilityTopic.concat("/maintenance/mqttConnectionState"); _network->publishHASSConfig(deviceType, baseTopic, name, uidString, softwareVersion, hardwareVersion, availabilityTopic.c_str(), hasKeypad, lockAction, unlockAction, openAction); @@ -886,7 +881,8 @@ void NukiNetworkOpener::publishKeypad(const std::list& en uint index = 0; char uidString[20]; itoa(_preferences->getUInt(preference_nuki_id_opener, 0), uidString, 16); - String baseTopic = _preferences->getString(preference_mqtt_opener_path); + String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/opener"); JsonDocument json; for(const auto& entry : entries) @@ -1098,7 +1094,8 @@ void NukiNetworkOpener::publishTimeControl(const std::listgetUInt(preference_nuki_id_opener, 0), uidString, 16); - String baseTopic = _preferences->getString(preference_mqtt_opener_path); + String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/opener"); JsonDocument json; for(const auto& entry : timeControlEntries) @@ -1228,7 +1225,8 @@ void NukiNetworkOpener::publishAuth(const std::listgetUInt(preference_nuki_id_opener, 0), uidString, 16); - String baseTopic = _preferences->getString(preference_mqtt_opener_path); + String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/opener"); JsonDocument json; for(const auto& entry : authEntries) @@ -1561,6 +1559,11 @@ bool NukiNetworkOpener::reconnected() return r; } +int NukiNetworkOpener::mqttConnectionState() +{ + return _network->mqttConnectionState(); +} + uint8_t NukiNetworkOpener::queryCommands() { uint8_t qc = _queryCommands; diff --git a/src/NukiNetworkOpener.h b/src/NukiNetworkOpener.h index 08aafd3..3facab4 100644 --- a/src/NukiNetworkOpener.h +++ b/src/NukiNetworkOpener.h @@ -48,9 +48,10 @@ public: void setKeypadJsonCommandReceivedCallback(void (*keypadJsonCommandReceivedReceivedCallback)(const char* value)); void setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char* value)); void setAuthCommandReceivedCallback(void (*authCommandReceivedReceivedCallback)(const char* value)); - void onMqttDataReceived(char* topic, int topic_len, char* data, int data_len) override; + void onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) override; - bool reconnected(); + int mqttConnectionState(); + bool reconnected(); //SETBACK uint8_t queryCommands(); char _nukiName[33]; @@ -87,7 +88,7 @@ private: bool _isConnected = false; bool _firstTunerStatePublish = true; bool _haEnabled = false; - bool _reconnected = false; + bool _reconnected = false; //SETBACK bool _disableNonJSON = false; String _keypadCommandName = ""; diff --git a/src/NukiOfficial.h b/src/NukiOfficial.h index 366b555..17383c3 100644 --- a/src/NukiOfficial.h +++ b/src/NukiOfficial.h @@ -69,7 +69,7 @@ private: uint8_t offTrigger = 0; uint32_t offAuthId = 0; uint32_t offCodeId = 0; -// uint8_t offContext = 0; + //uint8_t offContext = 0; bool offEnabled = false; }; diff --git a/src/NukiOpenerWrapper.cpp b/src/NukiOpenerWrapper.cpp index 4e70499..5ae4d50 100644 --- a/src/NukiOpenerWrapper.cpp +++ b/src/NukiOpenerWrapper.cpp @@ -114,6 +114,8 @@ void NukiOpenerWrapper::readSettings() _rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval) * 1000; _disableNonJSON = _preferences->getBool(preference_disable_non_json, false); _checkKeypadCodes = _preferences->getBool(preference_keypad_check_code_enabled, false); + _pairedAsApp = _preferences->getBool(preference_register_opener_as_app, false); + _preferences->getBytes(preference_conf_opener_basic_acl, &_basicOpenerConfigAclPrefs, sizeof(_basicOpenerConfigAclPrefs)); _preferences->getBytes(preference_conf_opener_advanced_acl, &_advancedOpenerConfigAclPrefs, sizeof(_advancedOpenerConfigAclPrefs)); @@ -216,67 +218,65 @@ void NukiOpenerWrapper::update() _nukiOpener.updateConnectionState(); - if(_statusUpdated || _nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs || (queryCommands & QUERY_COMMAND_LOCKSTATE) > 0) + if(_network->mqttConnectionState() == 2) { - _statusUpdated = false; - _nextLockStateUpdateTs = ts + _intervalLockstate * 1000; - updateKeyTurnerState(); - _network->publishStatusUpdated(_statusUpdated); - } - if(_nextBatteryReportTs == 0 || ts > _nextBatteryReportTs || (queryCommands & QUERY_COMMAND_BATTERY) > 0) - { - _nextBatteryReportTs = ts + _intervalBattery * 1000; - updateBatteryState(); - } - if(_nextConfigUpdateTs == 0 || ts > _nextConfigUpdateTs || (queryCommands & QUERY_COMMAND_CONFIG) > 0) - { - _nextConfigUpdateTs = ts + _intervalConfig * 1000; - updateConfig(); - if(_hassEnabled && !_hassSetupCompleted) + if(_statusUpdated || _nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs || (queryCommands & QUERY_COMMAND_LOCKSTATE) > 0) + { + _statusUpdated = false; + _nextLockStateUpdateTs = ts + _intervalLockstate * 1000; + updateKeyTurnerState(); + _network->publishStatusUpdated(_statusUpdated); + } + if(_nextBatteryReportTs == 0 || ts > _nextBatteryReportTs || (queryCommands & QUERY_COMMAND_BATTERY) > 0) + { + _nextBatteryReportTs = ts + _intervalBattery * 1000; + updateBatteryState(); + } + if(_nextConfigUpdateTs == 0 || ts > _nextConfigUpdateTs || (queryCommands & QUERY_COMMAND_CONFIG) > 0) + { + _nextConfigUpdateTs = ts + _intervalConfig * 1000; + updateConfig(); + } + if(_waitAuthLogUpdateTs != 0 && ts > _waitAuthLogUpdateTs) + { + _waitAuthLogUpdateTs = 0; + updateAuthData(true); + } + if(_waitKeypadUpdateTs != 0 && ts > _waitKeypadUpdateTs) + { + _waitKeypadUpdateTs = 0; + updateKeypad(true); + } + if(_waitTimeControlUpdateTs != 0 && ts > _waitTimeControlUpdateTs) + { + _waitTimeControlUpdateTs = 0; + updateTimeControl(true); + } + if(_waitAuthUpdateTs != 0 && ts > _waitAuthUpdateTs) + { + _waitAuthUpdateTs = 0; + updateAuth(true); + } + if(_hassEnabled && _nukiConfigValid && _nukiAdvancedConfigValid && !_hassSetupCompleted) { setupHASS(); } - } - if(_waitAuthLogUpdateTs != 0 && ts > _waitAuthLogUpdateTs) - { - _waitAuthLogUpdateTs = 0; - updateAuthData(true); - } - if(_waitKeypadUpdateTs != 0 && ts > _waitKeypadUpdateTs) - { - _waitKeypadUpdateTs = 0; - updateKeypad(true); - } - if(_waitTimeControlUpdateTs != 0 && ts > _waitTimeControlUpdateTs) - { - _waitTimeControlUpdateTs = 0; - updateTimeControl(true); - } - if(_waitAuthUpdateTs != 0 && ts > _waitAuthUpdateTs) - { - _waitAuthUpdateTs = 0; - updateAuth(true); - } - if(_hassEnabled && _nukiConfigValid && _nukiAdvancedConfigValid && _network->reconnected()) - { - setupHASS(); - } - if(_rssiPublishInterval > 0 && (_nextRssiTs == 0 || ts > _nextRssiTs)) - { - _nextRssiTs = ts + _rssiPublishInterval; - - int rssi = _nukiOpener.getRssi(); - if(rssi != _lastRssi) + if(_rssiPublishInterval > 0 && (_nextRssiTs == 0 || ts > _nextRssiTs)) { - _network->publishRssi(rssi); - _lastRssi = rssi; - } - } + _nextRssiTs = ts + _rssiPublishInterval; - if(_hasKeypad && _keypadEnabled && (_nextKeypadUpdateTs == 0 || ts > _nextKeypadUpdateTs || (queryCommands & QUERY_COMMAND_KEYPAD) > 0)) - { - _nextKeypadUpdateTs = ts + _intervalKeypad * 1000; - updateKeypad(false); + int rssi = _nukiOpener.getRssi(); + if(rssi != _lastRssi) + { + _network->publishRssi(rssi); + _lastRssi = rssi; + } + } + if(_hasKeypad && _keypadEnabled && (_nextKeypadUpdateTs == 0 || ts > _nextKeypadUpdateTs || (queryCommands & QUERY_COMMAND_KEYPAD) > 0)) + { + _nextKeypadUpdateTs = ts + _intervalKeypad * 1000; + updateKeypad(false); + } } if(_checkKeypadCodes && _invalidCount > 0 && ts - 120000 < _lastCodeCheck) @@ -540,7 +540,7 @@ void NukiOpenerWrapper::updateConfig() if(_preferences->getUInt(preference_nuki_id_opener, 0) == _nukiConfig.nukiId) { - _hasKeypad = _nukiConfig.hasKeypad > 0 || _nukiConfig.hasKeypadV2 > 0; + _hasKeypad = _nukiConfig.hasKeypad == 1 || _nukiConfig.hasKeypadV2 == 1; _firmwareVersion = std::to_string(_nukiConfig.firmwareVersion[0]) + "." + std::to_string(_nukiConfig.firmwareVersion[1]) + "." + std::to_string(_nukiConfig.firmwareVersion[2]); _hardwareVersion = std::to_string(_nukiConfig.hardwareRevision[0]) + "." + std::to_string(_nukiConfig.hardwareRevision[1]); if(_preferences->getBool(preference_conf_info_enabled, true)) @@ -889,6 +889,12 @@ void NukiOpenerWrapper::updateTimeControl(bool retrieved) void NukiOpenerWrapper::updateAuth(bool retrieved) { + if(!isPinValid()) + { + Log->println(F("No valid Nuki Lock PIN set")); + return; + } + if(!_preferences->getBool(preference_auth_info_enabled)) { return; @@ -3870,7 +3876,7 @@ BleScanner::Scanner *NukiOpenerWrapper::bleScanner() void NukiOpenerWrapper::notify(Nuki::EventType eventType) { - if(eventType == Nuki::EventType::KeyTurnerStatusUpdated) + if(!_pairedAsApp && eventType == Nuki::EventType::KeyTurnerStatusUpdated && !_statusUpdated) { Log->println("KeyTurnerStatusUpdated"); _statusUpdated = true; @@ -3946,7 +3952,8 @@ void NukiOpenerWrapper::setupHASS() return; } - String baseTopic = _preferences->getString(preference_mqtt_opener_path); + String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/opener"); char uidString[20]; itoa(_nukiConfig.nukiId, uidString, 16); diff --git a/src/NukiOpenerWrapper.h b/src/NukiOpenerWrapper.h index 6853711..2dab4aa 100644 --- a/src/NukiOpenerWrapper.h +++ b/src/NukiOpenerWrapper.h @@ -107,6 +107,7 @@ private: bool _clearAuthData = false; bool _disableNonJSON = false; bool _checkKeypadCodes = false; + bool _pairedAsApp = false; int _nrOfRetries = 0; int _retryDelay = 0; int _retryConfigCount = 0; diff --git a/src/NukiPublisher.cpp b/src/NukiPublisher.cpp index 2ee3289..5d4765c 100644 --- a/src/NukiPublisher.cpp +++ b/src/NukiPublisher.cpp @@ -27,33 +27,27 @@ void NukiPublisher::publishBool(const char *topic, const bool value, bool retain _network->publishBool(_mqttPath, topic, value, retain); } -bool NukiPublisher::publishString(const char *topic, const String &value, bool retain) +void NukiPublisher::publishString(const char *topic, const String &value, bool retain) { - char str[value.length() + 1]; - memset(str, 0, sizeof(str)); - memcpy(str, value.begin(), value.length()); - return publishString(topic, str, retain); + publishString(topic, value.c_str(), retain); } -bool NukiPublisher::publishString(const char *topic, const std::string &value, bool retain) +void NukiPublisher::publishString(const char *topic, const std::string &value, bool retain) { - char str[value.size() + 1]; - memset(str, 0, sizeof(str)); - memcpy(str, value.data(), value.length()); - return publishString(topic, str, retain); + publishString(topic, value.c_str(), retain); } -bool NukiPublisher::publishString(const char *topic, const char *value, bool retain) +void NukiPublisher::publishString(const char *topic, const char *value, bool retain) { - return _network->publishString(_mqttPath, topic, value, retain); + _network->publishString(_mqttPath, topic, value, retain); } void NukiPublisher::publishULong(const char *topic, const unsigned long value, bool retain) { - return _network->publishULong(_mqttPath, topic, value, retain); + _network->publishULong(_mqttPath, topic, value, retain); } void NukiPublisher::publishLongLong(const char *topic, int64_t value, bool retain) { - return _network->publishLongLong(_mqttPath, topic, value, retain); + _network->publishLongLong(_mqttPath, topic, value, retain); } diff --git a/src/NukiPublisher.h b/src/NukiPublisher.h index d59a839..1a6ea79 100644 --- a/src/NukiPublisher.h +++ b/src/NukiPublisher.h @@ -14,9 +14,9 @@ public: void publishULong(const char* topic, const unsigned long value, bool retain); void publishLongLong(const char* topic, int64_t value, bool retain); void publishBool(const char* topic, const bool value, bool retain); - bool publishString(const char* topic, const String& value, bool retain); - bool publishString(const char* topic, const std::string& value, bool retain); - bool publishString(const char* topic, const char* value, bool retain); + void publishString(const char* topic, const String& value, bool retain); + void publishString(const char* topic, const std::string& value, bool retain); + void publishString(const char* topic, const char* value, bool retain); private: NukiNetwork* _network; diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index 4d279eb..aa419a4 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -82,6 +82,8 @@ void NukiWrapper::initialize(const bool& firstStart) Log->println("Failed to clear NVS Wi-Fi configuration"); } #endif + _preferences->putString(preference_mqtt_lock_path, "nukihub"); + _preferences->putBool(preference_check_updates, true); _preferences->putBool(preference_opener_continuous_mode, false); _preferences->putBool(preference_official_hybrid_enabled, false); @@ -184,6 +186,8 @@ void NukiWrapper::readSettings() _rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval) * 1000; _disableNonJSON = _preferences->getBool(preference_disable_non_json, false); _checkKeypadCodes = _preferences->getBool(preference_keypad_check_code_enabled, false); + _pairedAsApp = _preferences->getBool(preference_register_as_app, false); + _preferences->getBytes(preference_conf_lock_basic_acl, &_basicLockConfigaclPrefs, sizeof(_basicLockConfigaclPrefs)); _preferences->getBytes(preference_conf_lock_advanced_acl, &_advancedLockConfigaclPrefs, sizeof(_advancedLockConfigaclPrefs)); @@ -355,84 +359,83 @@ void NukiWrapper::update() _nextLockAction = (NukiLock::LockAction) 0xff; } } - if(_nukiOfficial->getStatusUpdated() || _statusUpdated || _nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs || (queryCommands & QUERY_COMMAND_LOCKSTATE) > 0) + if(_network->mqttConnectionState() == 2) { - Log->println("Updating Lock state based on status, timer or query"); - _statusUpdated = false; - _nextLockStateUpdateTs = ts + _intervalLockstate * 1000; - updateKeyTurnerState(); - _network->publishStatusUpdated(_statusUpdated); - } - if(!_statusUpdated) - { - if(_nextBatteryReportTs == 0 || ts > _nextBatteryReportTs || (queryCommands & QUERY_COMMAND_BATTERY) > 0) + if(_nukiOfficial->getStatusUpdated() || _statusUpdated || _nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs || (queryCommands & QUERY_COMMAND_LOCKSTATE) > 0) { - Log->println("Updating Lock battery state based on timer or query"); - _nextBatteryReportTs = ts + _intervalBattery * 1000; - updateBatteryState(); + Log->println("Updating Lock state based on status, timer or query"); + _statusUpdated = false; + _nextLockStateUpdateTs = ts + _intervalLockstate * 1000; + updateKeyTurnerState(); + _network->publishStatusUpdated(_statusUpdated); } - if(_nextConfigUpdateTs == 0 || ts > _nextConfigUpdateTs || (queryCommands & QUERY_COMMAND_CONFIG) > 0) + if(!_statusUpdated) { - Log->println("Updating Lock config based on timer or query"); - _nextConfigUpdateTs = ts + _intervalConfig * 1000; - updateConfig(); - if(_hassEnabled && !_hassSetupCompleted) + if(_nextBatteryReportTs == 0 || ts > _nextBatteryReportTs || (queryCommands & QUERY_COMMAND_BATTERY) > 0) + { + Log->println("Updating Lock battery state based on timer or query"); + _nextBatteryReportTs = ts + _intervalBattery * 1000; + updateBatteryState(); + } + if(_nextConfigUpdateTs == 0 || ts > _nextConfigUpdateTs || (queryCommands & QUERY_COMMAND_CONFIG) > 0) + { + Log->println("Updating Lock config based on timer or query"); + _nextConfigUpdateTs = ts + _intervalConfig * 1000; + updateConfig(); + } + if(_waitAuthLogUpdateTs != 0 && ts > _waitAuthLogUpdateTs) + { + _waitAuthLogUpdateTs = 0; + updateAuthData(true); + } + if(_waitKeypadUpdateTs != 0 && ts > _waitKeypadUpdateTs) + { + _waitKeypadUpdateTs = 0; + updateKeypad(true); + } + if(_waitTimeControlUpdateTs != 0 && ts > _waitTimeControlUpdateTs) + { + _waitTimeControlUpdateTs = 0; + updateTimeControl(true); + } + if(_waitAuthUpdateTs != 0 && ts > _waitAuthUpdateTs) + { + _waitAuthUpdateTs = 0; + updateAuth(true); + } + if(_hassEnabled && _nukiConfigValid && _nukiAdvancedConfigValid && !_hassSetupCompleted) { setupHASS(); } - } - if(_waitAuthLogUpdateTs != 0 && ts > _waitAuthLogUpdateTs) - { - _waitAuthLogUpdateTs = 0; - updateAuthData(true); - } - if(_waitKeypadUpdateTs != 0 && ts > _waitKeypadUpdateTs) - { - _waitKeypadUpdateTs = 0; - updateKeypad(true); - } - if(_waitTimeControlUpdateTs != 0 && ts > _waitTimeControlUpdateTs) - { - _waitTimeControlUpdateTs = 0; - updateTimeControl(true); - } - if(_waitAuthUpdateTs != 0 && ts > _waitAuthUpdateTs) - { - _waitAuthUpdateTs = 0; - updateAuth(true); - } - if(_hassEnabled && _nukiConfigValid && _nukiAdvancedConfigValid && _network->reconnected()) - { - setupHASS(); - } - if(_rssiPublishInterval > 0 && (_nextRssiTs == 0 || ts > _nextRssiTs)) - { - _nextRssiTs = ts + _rssiPublishInterval; - - int rssi = _nukiLock.getRssi(); - if(rssi != _lastRssi) + if(_rssiPublishInterval > 0 && (_nextRssiTs == 0 || ts > _nextRssiTs)) { - _network->publishRssi(rssi); - _lastRssi = rssi; + _nextRssiTs = ts + _rssiPublishInterval; + + int rssi = _nukiLock.getRssi(); + if(rssi != _lastRssi) + { + _network->publishRssi(rssi); + _lastRssi = rssi; + } + } + if(_hasKeypad && _keypadEnabled && (_nextKeypadUpdateTs == 0 || ts > _nextKeypadUpdateTs || (queryCommands & QUERY_COMMAND_KEYPAD) > 0)) + { + Log->println("Updating Lock keypad based on timer or query"); + _nextKeypadUpdateTs = ts + _intervalKeypad * 1000; + updateKeypad(false); } } - if(_hasKeypad && _keypadEnabled && (_nextKeypadUpdateTs == 0 || ts > _nextKeypadUpdateTs || (queryCommands & QUERY_COMMAND_KEYPAD) > 0)) + if(_clearAuthData) { - Log->println("Updating Lock keypad based on timer or query"); - _nextKeypadUpdateTs = ts + _intervalKeypad * 1000; - updateKeypad(false); + Log->println("Clearing Lock auth data"); + _network->clearAuthorizationInfo(); + _clearAuthData = false; } if(_checkKeypadCodes && _invalidCount > 0 && ts - 120000 < _lastCodeCheck) { _invalidCount--; } } - if(_clearAuthData) - { - Log->println("Clearing Lock auth data"); - _network->clearAuthorizationInfo(); - _clearAuthData = false; - } memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(NukiLock::KeyTurnerState)); } @@ -625,7 +628,7 @@ void NukiWrapper::updateConfig() if(_preferences->getUInt(preference_nuki_id_lock, 0) == _nukiConfig.nukiId) { - _hasKeypad = _nukiConfig.hasKeypad > 0 || _nukiConfig.hasKeypadV2 > 0; + _hasKeypad = _nukiConfig.hasKeypad == 1 || _nukiConfig.hasKeypadV2 == 1; _firmwareVersion = std::to_string(_nukiConfig.firmwareVersion[0]) + "." + std::to_string(_nukiConfig.firmwareVersion[1]) + "." + std::to_string(_nukiConfig.firmwareVersion[2]); _hardwareVersion = std::to_string(_nukiConfig.hardwareRevision[0]) + "." + std::to_string(_nukiConfig.hardwareRevision[1]); if(_preferences->getBool(preference_conf_info_enabled, true)) @@ -968,6 +971,12 @@ void NukiWrapper::updateTimeControl(bool retrieved) void NukiWrapper::updateAuth(bool retrieved) { + if(!isPinValid()) + { + Log->println(F("No valid Nuki Lock PIN set")); + return; + } + if(!_preferences->getBool(preference_auth_info_enabled)) { return; @@ -3985,7 +3994,7 @@ void NukiWrapper::notify(Nuki::EventType eventType) } else { - if(eventType == Nuki::EventType::KeyTurnerStatusUpdated) + if(!_pairedAsApp && eventType == Nuki::EventType::KeyTurnerStatusUpdated && !_statusUpdated) { Log->println("KeyTurnerStatusUpdated"); _statusUpdated = true; @@ -4064,6 +4073,7 @@ void NukiWrapper::setupHASS() } String baseTopic = _preferences->getString(preference_mqtt_lock_path); + baseTopic.concat("/lock"); char uidString[20]; itoa(_nukiConfig.nukiId, uidString, 16); diff --git a/src/NukiWrapper.h b/src/NukiWrapper.h index 28443d2..4cdb937 100644 --- a/src/NukiWrapper.h +++ b/src/NukiWrapper.h @@ -131,6 +131,7 @@ private: bool _hassEnabled = false; bool _hassSetupCompleted = false; bool _disableNonJSON = false; + bool _pairedAsApp = false; bool _paired = false; bool _statusUpdated = false; bool _hasKeypad = false; diff --git a/src/PreferencesKeys.h b/src/PreferencesKeys.h index 81d7bb6..b01a959 100644 --- a/src/PreferencesKeys.h +++ b/src/PreferencesKeys.h @@ -18,7 +18,6 @@ #define preference_lock_enabled (char*)"lockena" #define preference_mqtt_lock_path (char*)"mqttpath" #define preference_opener_enabled (char*)"openerena" -#define preference_mqtt_opener_path (char*)"mqttoppath" #define preference_mqtt_ca (char*)"mqttca" #define preference_mqtt_crt (char*)"mqttcrt" #define preference_mqtt_key (char*)"mqttkey" @@ -54,6 +53,7 @@ #define preference_official_hybrid_enabled (char*)"offHybrid" #define preference_wifi_ssid (char*)"wifiSSID" #define preference_wifi_pass (char*)"wifiPass" +#define preference_disable_network_not_connected (char*)"disNtwNoCon" // CHANGE DOES NOT REQUIRE REBOOT TO TAKE EFFECT #define preference_ntw_reconfigure (char*)"ntwRECONF" @@ -276,7 +276,7 @@ private: preference_started_before, preference_config_version, preference_device_id_lock, preference_device_id_opener, preference_nuki_id_lock, preference_nuki_id_opener, preference_mqtt_broker, preference_mqtt_broker_port, preference_mqtt_user, preference_mqtt_password, preference_mqtt_log_enabled, preference_check_updates, preference_webserver_enabled, preference_lock_enabled, preference_lock_pin_status, preference_mqtt_lock_path, preference_opener_enabled, preference_opener_pin_status, - preference_opener_continuous_mode, preference_mqtt_opener_path, preference_lock_max_keypad_code_count, preference_opener_max_keypad_code_count, + preference_opener_continuous_mode, preference_lock_max_keypad_code_count, preference_opener_max_keypad_code_count, preference_lock_max_timecontrol_entry_count, preference_opener_max_timecontrol_entry_count, preference_enable_bootloop_reset, preference_mqtt_ca, preference_mqtt_crt, preference_mqtt_key, preference_mqtt_hass_discovery, preference_mqtt_hass_cu_url, preference_buffer_size, preference_ip_dhcp_enabled, preference_ip_address, preference_ip_subnet, preference_ip_gateway, preference_ip_dns_server, preference_network_hardware, @@ -292,7 +292,8 @@ private: preference_network_custom_mdc, preference_network_custom_clk, preference_network_custom_phy, preference_network_custom_addr, preference_network_custom_irq, preference_network_custom_rst, preference_network_custom_cs, preference_network_custom_sck, preference_network_custom_miso, preference_network_custom_mosi, preference_network_custom_pwr, preference_network_custom_mdio, preference_ntw_reconfigure, preference_lock_max_auth_entry_count, preference_opener_max_auth_entry_count, - preference_auth_control_enabled, preference_auth_topic_per_entry, preference_auth_info_enabled, preference_auth_max_entries, preference_keypad_check_code_enabled, preference_wifi_ssid, preference_wifi_pass + preference_auth_control_enabled, preference_auth_topic_per_entry, preference_auth_info_enabled, preference_auth_max_entries, preference_wifi_ssid, preference_wifi_pass, + preference_keypad_check_code_enabled, preference_disable_network_not_connected }; std::vector _redact = { @@ -308,7 +309,7 @@ private: preference_publish_authdata, preference_publish_debug_info, preference_official_hybrid_enabled, preference_official_hybrid_actions, preference_official_hybrid_retry, preference_conf_info_enabled, preference_disable_non_json, preference_update_from_mqtt, preference_auth_control_enabled, preference_auth_topic_per_entry, preference_auth_info_enabled, preference_webserial_enabled, - preference_ntw_reconfigure, preference_keypad_check_code_enabled + preference_ntw_reconfigure, preference_keypad_check_code_enabled, preference_disable_network_not_connected }; std::vector _bytePrefs = { diff --git a/src/RestartReason.h b/src/RestartReason.h index 9d7857d..d92640b 100644 --- a/src/RestartReason.h +++ b/src/RestartReason.h @@ -25,6 +25,7 @@ enum class RestartReason DeviceUnpaired, NukiHubReset, ReconfigureWebServer, + DisableNetworkIfNotConnected, NotApplicable }; @@ -112,6 +113,8 @@ inline static String getRestartReason() return "DeviceUnpaired"; case RestartReason::NukiHubReset: return "NukiHubFactoryReset"; + case RestartReason::DisableNetworkIfNotConnected: + return "NetworkDisabledOnNotConnected"; case RestartReason::NotApplicable: return "NotApplicable"; default: diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index c727e82..02d44fd 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -245,6 +245,14 @@ void WebCfgServer::initialize() } return buildCredHtml(request); }); + _psychicServer->on("/ntwconfig", HTTP_GET, [&](PsychicRequest *request) + { + if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) + { + return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in."); + } + return buildNetworkConfigHtml(request); + }); _psychicServer->on("/mqttconfig", HTTP_GET, [&](PsychicRequest *request) { if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) @@ -1518,16 +1526,6 @@ bool WebCfgServer::processArgs(PsychicRequest *request, String& message) configChanged = true; } } - else if(key == "MQTTOPPATH") - { - if(_preferences->getString(preference_mqtt_opener_path, "") != value) - { - _preferences->putString(preference_mqtt_opener_path, value); - Log->print(F("Setting changed: ")); - Log->println(key); - configChanged = true; - } - } else if(key == "MQTTCA") { if(_preferences->getString(preference_mqtt_ca, "") != value) @@ -2128,6 +2126,16 @@ bool WebCfgServer::processArgs(PsychicRequest *request, String& message) //configChanged = true; } } + else if(key == "DISNTWNOCON") + { + if(_preferences->getBool(preference_disable_network_not_connected, false) != (value == "1")) + { + _preferences->putBool(preference_disable_network_not_connected, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } else if(key == "OTAUPD") { if(_preferences->getString(preference_ota_updater_url, "") != value) @@ -3344,7 +3352,8 @@ esp_err_t WebCfgServer::buildHtml(PsychicRequest *request) } response.print("
"); response.print("
    "); - buildNavigationMenuEntry(&response, "MQTT and Network Configuration", "/mqttconfig", _brokerConfigured ? "" : "Please configure MQTT broker"); + buildNavigationMenuEntry(&response, "Network Configuration", "/ntwconfig"); + buildNavigationMenuEntry(&response, "MQTT Configuration", "/mqttconfig", _brokerConfigured ? "" : "Please configure MQTT broker"); buildNavigationMenuEntry(&response, "Nuki Configuration", "/nukicfg"); buildNavigationMenuEntry(&response, "Access Level Configuration", "/acclvl"); buildNavigationMenuEntry(&response, "Credentials", "/cred", _pinsConfigured ? "" : "Please configure PIN"); @@ -3454,48 +3463,23 @@ esp_err_t WebCfgServer::buildCredHtml(PsychicRequest *request) return response.endSend(); } -esp_err_t WebCfgServer::buildMqttConfigHtml(PsychicRequest *request) +esp_err_t WebCfgServer::buildNetworkConfigHtml(PsychicRequest *request) { PsychicStreamResponse response(request, "text/plain"); response.beginSend(); buildHtmlHeader(&response); response.print("
    "); - response.print("

    Basic MQTT and Network Configuration

    "); + response.print("

    Network Configuration

    "); response.print(""); printInputField(&response, "HOSTNAME", "Host name", _preferences->getString(preference_hostname).c_str(), 100, ""); - printInputField(&response, "MQTTSERVER", "MQTT Broker", _preferences->getString(preference_mqtt_broker).c_str(), 100, ""); - printInputField(&response, "MQTTPORT", "MQTT Broker port", _preferences->getInt(preference_mqtt_broker_port), 5, ""); - printInputField(&response, "MQTTUSER", "MQTT User (# to clear)", _preferences->getString(preference_mqtt_user).c_str(), 30, "", false, true); - printInputField(&response, "MQTTPASS", "MQTT Password", "*", 30, "", true, true); - response.print("

    "); - - response.print("

    Advanced MQTT and Network Configuration

    "); - response.print(""); - printInputField(&response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable; usually homeassistant)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30, ""); - printInputField(&response, "HASSCUURL", "Home Assistant device configuration URL (empty to use http://LOCALIP; fill when using a reverse proxy for example)", _preferences->getString(preference_mqtt_hass_cu_url).c_str(), 261, ""); - if(_preferences->getBool(preference_opener_enabled, false)) - { - printCheckBox(&response, "OPENERCONT", "Set Nuki Opener Lock/Unlock action in Home Assistant to Continuous mode", _preferences->getBool(preference_opener_continuous_mode), ""); - } - printTextarea(&response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE, true, true); - printTextarea(&response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE, true, true); - printTextarea(&response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE, true, true); printDropDown(&response, "NWHW", "Network hardware", String(_preferences->getInt(preference_network_hardware)), getNetworkDetectionOptions(), ""); + printInputField(&response, "HASSCUURL", "Home Assistant device configuration URL (empty to use http://LOCALIP; fill when using a reverse proxy for example)", _preferences->getString(preference_mqtt_hass_cu_url).c_str(), 261, ""); #ifndef CONFIG_IDF_TARGET_ESP32H2 printInputField(&response, "RSSI", "RSSI Publish interval (seconds; -1 to disable)", _preferences->getInt(preference_rssi_publish_interval), 6, ""); #endif - printInputField(&response, "NETTIMEOUT", "MQTT Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5, ""); printCheckBox(&response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect), ""); - printCheckBox(&response, "MQTTLOG", "Enable MQTT logging", _preferences->getBool(preference_mqtt_log_enabled), ""); printCheckBox(&response, "CHECKUPDATE", "Check for Firmware Updates every 24h", _preferences->getBool(preference_check_updates), ""); - printCheckBox(&response, "UPDATEMQTT", "Allow updating using MQTT", _preferences->getBool(preference_update_from_mqtt), ""); - printCheckBox(&response, "DISNONJSON", "Disable some extraneous non-JSON topics", _preferences->getBool(preference_disable_non_json), ""); - printCheckBox(&response, "OFFHYBRID", "Enable hybrid official MQTT and Nuki Hub setup", _preferences->getBool(preference_official_hybrid_enabled), ""); - printCheckBox(&response, "HYBRIDACT", "Enable sending actions through official MQTT", _preferences->getBool(preference_official_hybrid_actions), ""); - printInputField(&response, "HYBRIDTIMER", "Time between status updates when official MQTT is offline (seconds)", _preferences->getInt(preference_query_interval_hybrid_lockstate), 5, ""); - // printCheckBox(&response, "HYBRIDRETRY", "Retry command sent using official MQTT over BLE if failed", _preferences->getBool(preference_official_hybrid_retry), ""); // NOT IMPLEMENTED (YET?) response.print("
    "); - response.print("* If no encryption is configured for the MQTT broker, leave empty.

    "); response.print("

    IP Address assignment

    "); response.print(""); printCheckBox(&response, "DHCPENA", "Enable DHCP", _preferences->getBool(preference_ip_dhcp_enabled), ""); @@ -3510,6 +3494,47 @@ esp_err_t WebCfgServer::buildMqttConfigHtml(PsychicRequest *request) return response.endSend(); } +esp_err_t WebCfgServer::buildMqttConfigHtml(PsychicRequest *request) +{ + PsychicStreamResponse response(request, "text/plain"); + response.beginSend(); + buildHtmlHeader(&response); + response.print(""); + response.print("

    Basic MQTT Configuration

    "); + response.print("
    "); + printInputField(&response, "MQTTSERVER", "MQTT Broker", _preferences->getString(preference_mqtt_broker).c_str(), 100, ""); + printInputField(&response, "MQTTPORT", "MQTT Broker port", _preferences->getInt(preference_mqtt_broker_port), 5, ""); + printInputField(&response, "MQTTUSER", "MQTT User (# to clear)", _preferences->getString(preference_mqtt_user).c_str(), 30, "", false, true); + printInputField(&response, "MQTTPASS", "MQTT Password", "*", 30, "", true, true); + printInputField(&response, "MQTTPATH", "MQTT NukiHub Path", _preferences->getString(preference_mqtt_lock_path).c_str(), 180, ""); + response.print("

    "); + + response.print("

    Advanced MQTT Configuration

    "); + response.print(""); + printInputField(&response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable; usually homeassistant)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30, ""); + if(_preferences->getBool(preference_opener_enabled, false)) + { + printCheckBox(&response, "OPENERCONT", "Set Nuki Opener Lock/Unlock action in Home Assistant to Continuous mode", _preferences->getBool(preference_opener_continuous_mode), ""); + } + printTextarea(&response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE, true, true); + printTextarea(&response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE, true, true); + printTextarea(&response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE, true, true); + printInputField(&response, "NETTIMEOUT", "MQTT Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5, ""); + printCheckBox(&response, "MQTTLOG", "Enable MQTT logging", _preferences->getBool(preference_mqtt_log_enabled), ""); + printCheckBox(&response, "UPDATEMQTT", "Allow updating using MQTT", _preferences->getBool(preference_update_from_mqtt), ""); + printCheckBox(&response, "DISNONJSON", "Disable some extraneous non-JSON topics", _preferences->getBool(preference_disable_non_json), ""); + printCheckBox(&response, "OFFHYBRID", "Enable hybrid official MQTT and Nuki Hub setup", _preferences->getBool(preference_official_hybrid_enabled), ""); + printCheckBox(&response, "HYBRIDACT", "Enable sending actions through official MQTT", _preferences->getBool(preference_official_hybrid_actions), ""); + printInputField(&response, "HYBRIDTIMER", "Time between status updates when official MQTT is offline (seconds)", _preferences->getInt(preference_query_interval_hybrid_lockstate), 5, ""); + // printCheckBox(&response, "HYBRIDRETRY", "Retry command sent using official MQTT over BLE if failed", _preferences->getBool(preference_official_hybrid_retry), ""); // NOT IMPLEMENTED (YET?) + response.print("
    "); + response.print("* If no encryption is configured for the MQTT broker, leave empty.

    "); + response.print("
    "); + response.print("
    "); + response.print(""); + return response.endSend(); +} + esp_err_t WebCfgServer::buildAdvancedConfigHtml(PsychicRequest *request) { PsychicStreamResponse response(request, "text/plain"); @@ -3522,6 +3547,7 @@ esp_err_t WebCfgServer::buildAdvancedConfigHtml(PsychicRequest *request) response.print("Current bootloop prevention state"); response.print(_preferences->getBool(preference_enable_bootloop_reset, false) ? "Enabled" : "Disabled"); response.print(""); + printCheckBox(&response, "DISNTWNOCON", "Disable Network if not connected within 60s", _preferences->getBool(preference_disable_network_not_connected, false), ""); printCheckBox(&response, "WEBLOG", "Enable WebSerial logging", _preferences->getBool(preference_webserial_enabled), ""); printCheckBox(&response, "BTLPRST", "Enable Bootloop prevention (Try to reset these settings to default on bootloop)", true, ""); printInputField(&response, "BUFFSIZE", "Char buffer size (min 4096, max 32768)", _preferences->getInt(preference_buffer_size, CHAR_BUFFER_SIZE), 6, ""); @@ -3864,15 +3890,7 @@ esp_err_t WebCfgServer::buildNukiConfigHtml(PsychicRequest *request) response.print("

    Basic Nuki Configuration

    "); response.print(""); printCheckBox(&response, "LOCKENA", "Nuki Lock enabled", _preferences->getBool(preference_lock_enabled), ""); - if(_preferences->getBool(preference_lock_enabled)) - { - printInputField(&response, "MQTTPATH", "MQTT Nuki Lock Path", _preferences->getString(preference_mqtt_lock_path).c_str(), 180, ""); - } printCheckBox(&response, "OPENA", "Nuki Opener enabled", _preferences->getBool(preference_opener_enabled), ""); - if(_preferences->getBool(preference_opener_enabled)) - { - printInputField(&response, "MQTTOPPATH", "MQTT Nuki Opener Path", _preferences->getString(preference_mqtt_opener_path).c_str(), 180, ""); - } response.print("

    "); response.print("

    Advanced Nuki Configuration

    "); response.print(""); @@ -4137,16 +4155,8 @@ esp_err_t WebCfgServer::buildInfoHtml(PsychicRequest *request) response.print(_preferences->getString(preference_mqtt_user, "").length() > 0 ? "***" : "Not set"); response.print("\nMQTT password: "); response.print(_preferences->getString(preference_mqtt_password, "").length() > 0 ? "***" : "Not set"); - if(_preferences->getBool(preference_lock_enabled, true)) - { - response.print("\nMQTT lock base topic: "); - response.print(_preferences->getString(preference_mqtt_lock_path, "")); - } - if(_preferences->getBool(preference_opener_enabled, false)) - { - response.print("\nMQTT opener base topic: "); - response.print(_preferences->getString(preference_mqtt_lock_path, "")); - } + response.print("\nMQTT base topic: "); + response.print(_preferences->getString(preference_mqtt_lock_path, "")); response.print("\nMQTT SSL CA: "); response.print(_preferences->getString(preference_mqtt_ca, "").length() > 0 ? "***" : "Not set"); response.print("\nMQTT SSL CRT: "); @@ -4430,7 +4440,7 @@ esp_err_t WebCfgServer::buildInfoHtml(PsychicRequest *request) response.print(_nukiOpener->isPaired() ? _nukiOpener->isPinValid() ? "Yes" : "No" : "-"); response.print("\nOpener has keypad: "); response.print(_nukiOpener->hasKeypad() ? "Yes" : "No"); - if(_nuki->hasKeypad()) + if(_nukiOpener->hasKeypad()) { response.print("\nKeypad highest entries count: "); response.print(_preferences->getInt(preference_opener_max_keypad_code_count, 0)); diff --git a/src/WebCfgServer.h b/src/WebCfgServer.h index e9be8b9..31e7ab7 100644 --- a/src/WebCfgServer.h +++ b/src/WebCfgServer.h @@ -54,6 +54,7 @@ private: esp_err_t buildAccLvlHtml(PsychicRequest *request); esp_err_t buildCredHtml(PsychicRequest *request); esp_err_t buildImportExportHtml(PsychicRequest *request); + esp_err_t buildNetworkConfigHtml(PsychicRequest *request); esp_err_t buildMqttConfigHtml(PsychicRequest *request); esp_err_t buildStatusHtml(PsychicRequest *request); esp_err_t buildAdvancedConfigHtml(PsychicRequest *request); diff --git a/src/main.cpp b/src/main.cpp index b71e6f9..4fb617f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,7 @@ Gpio* gpio = nullptr; bool lockEnabled = false; bool openerEnabled = false; +bool wifiConnected = false; TaskHandle_t nukiTaskHandle = nullptr; @@ -64,12 +65,16 @@ NukiNetwork* network = nullptr; WebCfgServer* webCfgServer = nullptr; Preferences* preferences = nullptr; +RTC_NOINIT_ATTR int espRunning; RTC_NOINIT_ATTR int restartReason; RTC_NOINIT_ATTR uint64_t restartReasonValidDetect; RTC_NOINIT_ATTR bool rebuildGpioRequested; RTC_NOINIT_ATTR uint64_t bootloopValidDetect; RTC_NOINIT_ATTR int8_t bootloopCounter; RTC_NOINIT_ATTR bool forceEnableWebServer; +RTC_NOINIT_ATTR bool disableNetwork; +RTC_NOINIT_ATTR bool wifiFallback; +RTC_NOINIT_ATTR bool ethCriticalFailure; bool restartReason_isValid; RestartReason currentRestartReason = RestartReason::NotApplicable; @@ -144,14 +149,9 @@ void networkTask(void *pvParameters) } } - bool connected = network->update(); - -#ifndef NUKI_HUB_UPDATER - if(connected && networkLock != nullptr) - { - networkLock->update(); - } - + network->update(); + bool connected = network->isConnected(); + #ifdef DEBUG_NUKIHUB if(connected && reroute) { @@ -159,6 +159,15 @@ void networkTask(void *pvParameters) setReroute(); } #endif + +#ifndef NUKI_HUB_UPDATER + wifiConnected = network->wifiConnected(); + + if(connected && lockEnabled) + { + networkLock->update(); + } + if(connected && openerEnabled) { networkOpener->update(); @@ -172,7 +181,6 @@ void networkTask(void *pvParameters) } esp_task_wdt_reset(); - delay(100); } } @@ -184,37 +192,40 @@ void nukiTask(void *pvParameters) while(true) { - bleScanner->update(); - delay(20); - - bool needsPairing = (lockEnabled && !nuki->isPaired()) || (openerEnabled && !nukiOpener->isPaired()); - - if (needsPairing) + if(disableNetwork || wifiConnected) { - delay(5000); - } - else if (!whiteListed) - { - whiteListed = true; + bleScanner->update(); + delay(20); + + bool needsPairing = (lockEnabled && !nuki->isPaired()) || (openerEnabled && !nukiOpener->isPaired()); + + if (needsPairing) + { + delay(5000); + } + else if (!whiteListed) + { + whiteListed = true; + if(lockEnabled) + { + bleScanner->whitelist(nuki->getBleAddress()); + } + if(openerEnabled) + { + bleScanner->whitelist(nukiOpener->getBleAddress()); + } + } + if(lockEnabled) { - bleScanner->whitelist(nuki->getBleAddress()); + nuki->update(); } if(openerEnabled) { - bleScanner->whitelist(nukiOpener->getBleAddress()); + nukiOpener->update(); } } - if(lockEnabled) - { - nuki->update(); - } - if(openerEnabled) - { - nukiOpener->update(); - } - if(espMillis() - nukiLoopTs > 120000) { Log->println("nukiTask is running"); @@ -241,7 +252,6 @@ void bootloopDetection() if(esp_reset_reason() == esp_reset_reason_t::ESP_RST_PANIC || esp_reset_reason() == esp_reset_reason_t::ESP_RST_INT_WDT || esp_reset_reason() == esp_reset_reason_t::ESP_RST_TASK_WDT || - true || esp_reset_reason() == esp_reset_reason_t::ESP_RST_WDT) { bootloopCounter++; @@ -400,10 +410,13 @@ void setupTasks(bool ota) } else { - xTaskCreatePinnedToCore(networkTask, "ntw", preferences->getInt(preference_task_size_network, NETWORK_TASK_SIZE), NULL, 3, &networkTaskHandle, 1); - esp_task_wdt_add(networkTaskHandle); + if(!disableNetwork) + { + xTaskCreatePinnedToCore(networkTask, "ntw", preferences->getInt(preference_task_size_network, NETWORK_TASK_SIZE), NULL, 3, &networkTaskHandle, 1); + esp_task_wdt_add(networkTaskHandle); + } #ifndef NUKI_HUB_UPDATER - if(!network->isApOpen()) + if(!network->isApOpen() && (lockEnabled || openerEnabled)) { xTaskCreatePinnedToCore(nukiTask, "nuki", preferences->getInt(preference_task_size_nuki, NUKI_TASK_SIZE), NULL, 2, &nukiTaskHandle, 0); esp_task_wdt_add(nukiTaskHandle); @@ -414,12 +427,16 @@ void setupTasks(bool ota) void setup() { + //Set Log level to error for all TAGS esp_log_level_set("*", ESP_LOG_ERROR); + //Set Log level to none for mqtt TAG esp_log_level_set("mqtt", ESP_LOG_NONE); + //Start Serial and setup Log class Serial.begin(115200); Log = &Serial; #ifndef NUKI_HUB_UPDATER + // stdout = funopen(NULL, NULL, &write_fn, NULL, NULL); static char linebuf[1024]; setvbuf(stdout, linebuf, _IOLBF, sizeof(linebuf)); @@ -435,6 +452,17 @@ void setup() initializeRestartReason(); + //default disableNetwork RTC_ATTR to false on power-on + if(espRunning != 1) + { + espRunning = 1; + forceEnableWebServer = false; + disableNetwork = false; + wifiFallback = false; + ethCriticalFailure = false; + } + + //determine if an OTA update was requested if((partitionType==1 && preferences->getString(preference_ota_updater_url, "").length() > 0) || (partitionType==2 && preferences->getString(preference_ota_main_url, "").length() > 0)) { doOta = true; @@ -496,7 +524,7 @@ void setup() deviceIdOpener->assignId(deviceIdLock->get()); } - char16_t buffer_size = preferences->getInt(preference_buffer_size, 4096); + char16_t buffer_size = preferences->getInt(preference_buffer_size, CHAR_BUFFER_SIZE); CharBuffer::initialize(buffer_size); gpio = new Gpio(preferences); @@ -520,26 +548,26 @@ void setup() openerEnabled = false; } - bleScanner = new BleScanner::Scanner(); - // Scan interval and window according to Nuki recommendations: - // https://developer.nuki.io/t/bluetooth-specification-questions/1109/27 - bleScanner->initialize("NukiHub", true, 40, 40); - bleScanner->setScanDuration(0); - - nukiOfficial = new NukiOfficial(preferences); - - networkLock = new NukiNetworkLock(network, nukiOfficial, preferences, CharBuffer::get(), buffer_size); - networkLock->initialize(); - - if(openerEnabled) + if(lockEnabled || openerEnabled) { - networkOpener = new NukiNetworkOpener(network, preferences, CharBuffer::get(), buffer_size); - networkOpener->initialize(); + bleScanner = new BleScanner::Scanner(); + // Scan interval and window according to Nuki recommendations: + // https://developer.nuki.io/t/bluetooth-specification-questions/1109/27 + bleScanner->initialize("NukiHub", true, 40, 40); + bleScanner->setScanDuration(0); } Log->println(lockEnabled ? F("Nuki Lock enabled") : F("Nuki Lock disabled")); if(lockEnabled) { + nukiOfficial = new NukiOfficial(preferences); + networkLock = new NukiNetworkLock(network, nukiOfficial, preferences, CharBuffer::get(), buffer_size); + + if(!disableNetwork) + { + networkLock->initialize(); + } + nuki = new NukiWrapper("NukiHub", deviceIdLock, bleScanner, networkLock, nukiOfficial, gpio, preferences); nuki->initialize(firstStart); } @@ -547,41 +575,47 @@ void setup() Log->println(openerEnabled ? F("Nuki Opener enabled") : F("Nuki Opener disabled")); if(openerEnabled) { + networkOpener = new NukiNetworkOpener(network, preferences, CharBuffer::get(), buffer_size); + + if(!disableNetwork) + { + networkOpener->initialize(); + } + nukiOpener = new NukiOpenerWrapper("NukiHub", deviceIdOpener, bleScanner, networkOpener, gpio, preferences); nukiOpener->initialize(); } - if(forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true) || preferences->getBool(preference_webserial_enabled, false)) + if(!doOta && !disableNetwork && (forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true) || preferences->getBool(preference_webserial_enabled, false))) { - if(!doOta) + psychicServer = new PsychicHttpServer; + psychicServer->config.max_uri_handlers = 40; + psychicServer->config.stack_size = HTTPD_TASK_SIZE; + psychicServer->maxUploadSize = 8192; + psychicServer->maxRequestBodySize = 8192; + psychicServer->listen(80); + + if(forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true)) { - psychicServer = new PsychicHttpServer; - psychicServer->config.max_uri_handlers = 40; - psychicServer->config.stack_size = 8192; - psychicServer->listen(80); - - if(forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true)) + webCfgServer = new WebCfgServer(nuki, nukiOpener, network, gpio, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, psychicServer); + webCfgServer->initialize(); + psychicServer->onNotFound([](PsychicRequest* request) { - webCfgServer = new WebCfgServer(nuki, nukiOpener, network, gpio, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, psychicServer); - webCfgServer->initialize(); - psychicServer->onNotFound([](PsychicRequest* request) - { - return request->redirect("/"); - }); - } - /* -#ifdef DEBUG_NUKIHUB - else psychicServer->onNotFound([](PsychicRequest* request) { return request->redirect("/webserial"); }); - - if(preferences->getBool(preference_webserial_enabled, false)) - { - WebSerial.setAuthentication(preferences->getString(preference_cred_user), preferences->getString(preference_cred_password)); - WebSerial.begin(asyncServer); - WebSerial.setBuffer(1024); - } -#endif - */ + return request->redirect("/"); + }); } + /* +#ifdef DEBUG_NUKIHUB + else psychicServer->onNotFound([](PsychicRequest* request) { return request->redirect("/webserial"); }); + + if(preferences->getBool(preference_webserial_enabled, false)) + { + WebSerial.setAuthentication(preferences->getString(preference_cred_user), preferences->getString(preference_cred_password)); + WebSerial.begin(asyncServer); + WebSerial.setBuffer(1024); + } +#endif + */ } #endif diff --git a/src/networkDevices/EthernetDevice.cpp b/src/networkDevices/EthernetDevice.cpp index 4ec1070..f75277c 100644 --- a/src/networkDevices/EthernetDevice.cpp +++ b/src/networkDevices/EthernetDevice.cpp @@ -1,10 +1,14 @@ #include "EthernetDevice.h" #include "../PreferencesKeys.h" #include "../Logger.h" +#ifndef NUKI_HUB_UPDATER +#include "../MqttTopics.h" +#include "espMqttClient.h" +#endif #include "../RestartReason.h" -RTC_NOINIT_ATTR bool criticalEthFailure; -extern char WiFi_fallbackDetect[14]; +extern bool ethCriticalFailure; +extern bool wifiFallback; EthernetDevice::EthernetDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode) : NetworkDevice(hostname, ipConfiguration), @@ -49,6 +53,51 @@ EthernetDevice::EthernetDevice(const String &hostname, init(); } +void EthernetDevice::init() +{ +#ifndef NUKI_HUB_UPDATER + size_t caLength = _preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE); + size_t crtLength = _preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE); + size_t keyLength = _preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE); + + _useEncryption = caLength > 1; // length is 1 when empty + + if(_useEncryption) + { + Log->println(F("MQTT over TLS.")); + _mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO); + _mqttClientSecure->setCACert(_ca); + if(crtLength > 1 && keyLength > 1) // length is 1 when empty + { + Log->println(F("MQTT with client certificate.")); + _mqttClientSecure->setCertificate(_cert); + _mqttClientSecure->setPrivateKey(_key); + } + } else + { + Log->println(F("MQTT without TLS.")); + _mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO); + } + + if(_preferences->getBool(preference_mqtt_log_enabled, false) || _preferences->getBool(preference_webserial_enabled, false)) + { + MqttLoggerMode mode; + + if(_preferences->getBool(preference_mqtt_log_enabled, false) && _preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb; + else if (_preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb; + else mode = MqttLoggerMode::MqttAndSerial; + + _path = new char[200]; + memset(_path, 0, sizeof(_path)); + + String pathStr = _preferences->getString(preference_mqtt_lock_path); + pathStr.concat(mqtt_topic_log); + strcpy(_path, pathStr.c_str()); + Log = new MqttLogger(*getMqttClient(), _path, mode); + } +#endif +} + const String EthernetDevice::deviceName() const { return _deviceName.c_str(); @@ -57,12 +106,12 @@ const String EthernetDevice::deviceName() const void EthernetDevice::initialize() { delay(250); - if(criticalEthFailure) + if(ethCriticalFailure) { - criticalEthFailure = false; + ethCriticalFailure = false; Log->println(F("Failed to initialize ethernet hardware")); Log->println("Network device has a critical failure, enable fallback to Wi-Fi and reboot."); - strcpy(WiFi_fallbackDetect, "wifi_fallback"); + wifiFallback = true; delay(200); restartEsp(RestartReason::NetworkDeviceCriticalFailure); return; @@ -73,18 +122,18 @@ void EthernetDevice::initialize() if(_useSpi) { Log->println(F("Use SPI")); - criticalEthFailure = true; + ethCriticalFailure = true; SPI.begin(_spi_sck, _spi_miso, _spi_mosi); _hardwareInitialized = ETH.begin(_type, _phy_addr, _cs, _irq, _rst, SPI); - criticalEthFailure = false; + ethCriticalFailure = false; } #ifdef CONFIG_IDF_TARGET_ESP32 else { Log->println(F("Use RMII")); - criticalEthFailure = true; + ethCriticalFailure = true; _hardwareInitialized = ETH.begin(_type, _phy_addr, _mdc, _mdio, _power, _clock_mode); - criticalEthFailure = false; + ethCriticalFailure = false; if(!_ipConfiguration->dhcpEnabled()) { _checkIpTs = espMillis() + 2000; @@ -95,7 +144,7 @@ void EthernetDevice::initialize() if(_hardwareInitialized) { Log->println(F("Ethernet hardware Initialized")); - memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect)); + wifiFallback = false; if(_useSpi && !_ipConfiguration->dhcpEnabled()) { @@ -111,7 +160,7 @@ void EthernetDevice::initialize() { Log->println(F("Failed to initialize ethernet hardware")); Log->println("Network device has a critical failure, enable fallback to Wi-Fi and reboot."); - strcpy(WiFi_fallbackDetect, "wifi_fallback"); + wifiFallback = true; delay(200); restartEsp(RestartReason::NetworkDeviceCriticalFailure); return; @@ -120,18 +169,19 @@ void EthernetDevice::initialize() void EthernetDevice::update() { - if(_checkIpTs != -1) + NetworkDevice::update(); + + if(_checkIpTs != -1 && _checkIpTs < espMillis()) { if(_ipConfiguration->ipAddress() != ETH.localIP()) { Log->println(F("ETH Set static IP")); ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer()); - _checkIpTs = espMillis() + 2000; - } - else - { - _checkIpTs = -1; + _checkIpTs = espMillis() + 5000; + return; } + + _checkIpTs = -1; } } diff --git a/src/networkDevices/EthernetDevice.h b/src/networkDevices/EthernetDevice.h index c68a22e..d5503c8 100644 --- a/src/networkDevices/EthernetDevice.h +++ b/src/networkDevices/EthernetDevice.h @@ -11,6 +11,9 @@ #include #include #include "NetworkDevice.h" +#ifndef NUKI_HUB_UPDATER +#include "espMqttClient.h" +#endif class EthernetDevice : public NetworkDevice { @@ -45,8 +48,8 @@ public: virtual void initialize(); virtual void reconfigure(); virtual void update(); - virtual void scan(bool passive = false, bool async = true); + virtual void scan(bool passive = false, bool async = true); virtual bool isConnected(); virtual bool isApOpen(); @@ -58,10 +61,12 @@ public: private: Preferences* _preferences; + void init(); void onDisconnected(); void onNetworkEvent(arduino_event_id_t event, arduino_event_info_t info); bool _connected = false; + char* _path; bool _hardwareInitialized = false; const std::string _deviceName; @@ -85,4 +90,10 @@ private: eth_phy_type_t _type; eth_clock_mode_t _clock_mode; bool _useSpi = false; + + #ifndef NUKI_HUB_UPDATER + char _ca[TLS_CA_MAX_SIZE] = {0}; + char _cert[TLS_CERT_MAX_SIZE] = {0}; + char _key[TLS_KEY_MAX_SIZE] = {0}; + #endif }; \ No newline at end of file diff --git a/src/networkDevices/NetworkDevice.cpp b/src/networkDevices/NetworkDevice.cpp index bcea055..9e65acd 100644 --- a/src/networkDevices/NetworkDevice.cpp +++ b/src/networkDevices/NetworkDevice.cpp @@ -1,6 +1,179 @@ #include #include "NetworkDevice.h" +#include "../Logger.h" +void NetworkDevice::printError() +{ + Log->print(F("Free Heap: ")); + Log->println(ESP.getFreeHeap()); +} + +#ifndef NUKI_HUB_UPDATER void NetworkDevice::update() { -} \ No newline at end of file + if (_mqttEnabled) + { + getMqttClient()->loop(); + } +} + +void NetworkDevice::mqttSetClientId(const char *clientId) +{ + if (_useEncryption) + { + _mqttClientSecure->setClientId(clientId); + } + else + { + _mqttClient->setClientId(clientId); + } +} + +void NetworkDevice::mqttSetCleanSession(bool cleanSession) +{ + if (_useEncryption) + { + _mqttClientSecure->setCleanSession(cleanSession); + } + else + { + _mqttClient->setCleanSession(cleanSession); + } +} + +void NetworkDevice::mqttSetKeepAlive(uint16_t keepAlive) +{ + if (_useEncryption) + { + _mqttClientSecure->setKeepAlive(keepAlive); + } + else + { + _mqttClient->setKeepAlive(keepAlive); + } +} + +uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) +{ + return getMqttClient()->publish(topic, qos, retain, payload); +} + +uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) +{ + return getMqttClient()->publish(topic, qos, retain, payload, length); +} + +bool NetworkDevice::mqttConnected() const +{ + return getMqttClient()->connected(); +} + +void NetworkDevice::mqttSetServer(const char *host, uint16_t port) +{ + if (_useEncryption) + { + _mqttClientSecure->setServer(host, port); + } + else + { + _mqttClient->setServer(host, port); + } +} + +bool NetworkDevice::mqttConnect() +{ + return getMqttClient()->connect(); +} + +bool NetworkDevice::mqttDisconnect(bool force) +{ + return getMqttClient()->disconnect(force); +} + +void NetworkDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) +{ + if (_useEncryption) + { + _mqttClientSecure->setWill(topic, qos, retain, payload); + } + else + { + _mqttClient->setWill(topic, qos, retain, payload); + } +} + +void NetworkDevice::mqttSetCredentials(const char *username, const char *password) +{ + if (_useEncryption) + { + _mqttClientSecure->setCredentials(username, password); + } + else + { + _mqttClient->setCredentials(username, password); + } +} + +void NetworkDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) +{ + if (_useEncryption) + { + _mqttClientSecure->onMessage(callback); + } + else + { + _mqttClient->onMessage(callback); + } +} + +void NetworkDevice::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) +{ + if(_useEncryption) + { + _mqttClientSecure->onConnect(callback); + } + else + { + _mqttClient->onConnect(callback); + } +} + +void NetworkDevice::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) +{ + if (_useEncryption) + { + _mqttClientSecure->onDisconnect(callback); + } + else + { + _mqttClient->onDisconnect(callback); + } +} + +uint16_t NetworkDevice::mqttSubscribe(const char *topic, uint8_t qos) +{ + return getMqttClient()->subscribe(topic, qos); +} + +void NetworkDevice::disableMqtt() +{ + getMqttClient()->disconnect(); + _mqttEnabled = false; +} + +MqttClient *NetworkDevice::getMqttClient() const +{ + if (_useEncryption) + { + return _mqttClientSecure; + } + else + { + return _mqttClient; + } +} +#else +void NetworkDevice::update() +{ +} +#endif \ No newline at end of file diff --git a/src/networkDevices/NetworkDevice.h b/src/networkDevices/NetworkDevice.h index abbade2..f5b2b11 100644 --- a/src/networkDevices/NetworkDevice.h +++ b/src/networkDevices/NetworkDevice.h @@ -1,4 +1,9 @@ #pragma once + +#ifndef NUKI_HUB_UPDATER +#include "espMqttClient.h" +#include "MqttClientSetup.h" +#endif #include "IPConfiguration.h" #include "../EspMillis.h" @@ -14,16 +19,48 @@ public: virtual void initialize() = 0; virtual void reconfigure() = 0; + virtual void printError(); + virtual void update(); virtual void scan(bool passive = false, bool async = true) = 0; - virtual bool isConnected() = 0; virtual bool isApOpen() = 0; virtual int8_t signalStrength() = 0; virtual String localIP() = 0; virtual String BSSIDstr() = 0; -protected: + + #ifndef NUKI_HUB_UPDATER + virtual void mqttSetClientId(const char* clientId); + virtual void mqttSetCleanSession(bool cleanSession); + virtual void mqttSetKeepAlive(uint16_t keepAlive); + virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const char* payload); + virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length); + virtual bool mqttConnected() const; + virtual void mqttSetServer(const char* host, uint16_t port); + virtual bool mqttConnect(); + virtual bool mqttDisconnect(bool force); + virtual void setWill(const char* topic, uint8_t qos, bool retain, const char* payload); + virtual void mqttSetCredentials(const char* username, const char* password); + virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback); + virtual void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback); + virtual void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback); + virtual void disableMqtt(); + + virtual uint16_t mqttSubscribe(const char* topic, uint8_t qos); + #endif + +protected: + #ifndef NUKI_HUB_UPDATER + espMqttClient *_mqttClient = nullptr; + espMqttClientSecure *_mqttClientSecure = nullptr; + + bool _useEncryption = false; + bool _mqttEnabled = true; + + MqttClient *getMqttClient() const; + #endif + const String _hostname; const IPConfiguration* _ipConfiguration = nullptr; }; \ No newline at end of file diff --git a/src/networkDevices/WifiDevice.cpp b/src/networkDevices/WifiDevice.cpp index 8625db3..bbacc52 100644 --- a/src/networkDevices/WifiDevice.cpp +++ b/src/networkDevices/WifiDevice.cpp @@ -3,14 +3,59 @@ #include "WifiDevice.h" #include "../PreferencesKeys.h" #include "../Logger.h" +#ifndef NUKI_HUB_UPDATER +#include "../MqttTopics.h" +#include "espMqttClient.h" +#endif #include "../RestartReason.h" WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration) : NetworkDevice(hostname, ipConfiguration), _preferences(preferences) { + #ifndef NUKI_HUB_UPDATER + size_t caLength = preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE); + size_t crtLength = preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE); + size_t keyLength = preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE); + + _useEncryption = caLength > 1; // length is 1 when empty + + if(_useEncryption) + { + Log->println(F("MQTT over TLS.")); + _mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO); + _mqttClientSecure->setCACert(_ca); + if(crtLength > 1 && keyLength > 1) // length is 1 when empty + { + Log->println(F("MQTT with client certificate.")); + _mqttClientSecure->setCertificate(_cert); + _mqttClientSecure->setPrivateKey(_key); + } + } + else + { + Log->println(F("MQTT without TLS.")); + _mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO); + } + + if(preferences->getBool(preference_mqtt_log_enabled, false) || preferences->getBool(preference_webserial_enabled, false)) + { + MqttLoggerMode mode; + + if(preferences->getBool(preference_mqtt_log_enabled, false) && preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb; + else if (preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb; + else mode = MqttLoggerMode::MqttAndSerial; + _path = new char[200]; + memset(_path, 0, sizeof(_path)); + String pathStr = preferences->getString(preference_mqtt_lock_path); + pathStr.concat(mqtt_topic_log); + strcpy(_path, pathStr.c_str()); + Log = new MqttLogger(*getMqttClient(), _path, mode); + } + #endif } + const String WifiDevice::deviceName() const { return "Built-in Wi-Fi"; @@ -373,7 +418,7 @@ void WifiDevice::onDisconnected() } _connecting = false; - //END QUICK RECONECT + //END QUICK RECONNECT if(!isConnected()) { diff --git a/src/networkDevices/WifiDevice.h b/src/networkDevices/WifiDevice.h index 50e9d25..e49d540 100644 --- a/src/networkDevices/WifiDevice.h +++ b/src/networkDevices/WifiDevice.h @@ -29,6 +29,7 @@ private: void onDisconnected(); void onConnected(); bool connect(); + char* _path; Preferences* _preferences = nullptr; @@ -44,4 +45,10 @@ private: uint8_t _connectedChannel = 0; uint8_t* _connectedBSSID; int64_t _disconnectTs = 0; + + #ifndef NUKI_HUB_UPDATER + char _ca[TLS_CA_MAX_SIZE] = {0}; + char _cert[TLS_CERT_MAX_SIZE] = {0}; + char _key[TLS_KEY_MAX_SIZE] = {0}; + #endif }; diff --git a/updater/sdkconfig.defaults b/updater/sdkconfig.defaults index 14d722b..60592b8 100644 --- a/updater/sdkconfig.defaults +++ b/updater/sdkconfig.defaults @@ -31,4 +31,4 @@ CONFIG_HTTPD_MAX_URI_LEN=512 CONFIG_HTTPD_ERR_RESP_NO_DELAY=y CONFIG_HTTPD_PURGE_BUF_LEN=32 CONFIG_HTTPD_WS_SUPPORT=y -CONFIG_ESP_HTTPS_SERVER_ENABLE=y \ No newline at end of file +CONFIG_ESP_HTTPS_SERVER_ENABLE=n \ No newline at end of file