diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md
index f67cbcf99..59a8636de 100644
--- a/CHANGELOG_LATEST.md
+++ b/CHANGELOG_LATEST.md
@@ -23,6 +23,7 @@ There are breaking changes in 3.6.0. Please read carefully before applying the u
- Ventilation device [#1172](https://github.com/emsesp/EMS-ESP32/issues/1172)
- Turn ETH off on wifi connect [#1167](https://github.com/emsesp/EMS-ESP32/issues/1167)
- Support for multiple EMS-ESPs with HA [#1196](https://github.com/emsesp/EMS-ESP32/issues/1196)
+- Italian translation [#1199](https://github.com/emsesp/EMS-ESP32/issues/1199)
## Fixed
@@ -42,3 +43,4 @@ There are breaking changes in 3.6.0. Please read carefully before applying the u
- Retry timeout for Mqtt-QOS1/2 10seconds
- Optimize WebUI rendering when using Dialog Boxes [#1116](https://github.com/emsesp/EMS-ESP32/issues/1116)
- Optimize Web libraries to reduce bundle size (3.6.x) [#1112](https://github.com/emsesp/EMS-ESP32/issues/1112)
+- Use [espMqttClient](https://github.com/bertmelis/espMqttClient) with integrated queue [#1178](https://github.com/emsesp/EMS-ESP32/issues/1178)
diff --git a/interface/src/SignIn.tsx b/interface/src/SignIn.tsx
index 138427b55..5513da4ae 100644
--- a/interface/src/SignIn.tsx
+++ b/interface/src/SignIn.tsx
@@ -17,6 +17,7 @@ import { AuthenticationContext } from 'contexts/authentication';
import { ReactComponent as DEflag } from 'i18n/DE.svg';
import { ReactComponent as FRflag } from 'i18n/FR.svg';
import { ReactComponent as GBflag } from 'i18n/GB.svg';
+import { ReactComponent as ITflag } from 'i18n/IT.svg';
import { ReactComponent as NLflag } from 'i18n/NL.svg';
import { ReactComponent as NOflag } from 'i18n/NO.svg';
import { ReactComponent as PLflag } from 'i18n/PL.svg';
@@ -128,6 +129,10 @@ const SignIn: FC = () => {
FR
+
+ {changed !== 0 && (
+
+ }
+ disabled={saving}
+ variant="outlined"
+ color="primary"
+ type="submit"
+ onClick={onCancelSubmit}
+ >
+ {LL.CANCEL()}
+
+ }
+ disabled={saving || noAdminConfigured()}
+ variant="contained"
+ color="info"
+ type="submit"
+ onClick={onSubmit}
+ >
+ {LL.APPLY_CHANGES(changed)}
+
+
+ )}
@@ -208,6 +234,7 @@ const ManageUsersForm: FC = () => {
return (
+ {blocker ? : null}
{content()}
);
diff --git a/interface/src/i18n/IT.svg b/interface/src/i18n/IT.svg
new file mode 100644
index 000000000..36677a0b9
--- /dev/null
+++ b/interface/src/i18n/IT.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/interface/src/i18n/it/index.ts b/interface/src/i18n/it/index.ts
new file mode 100644
index 000000000..8293e7c3a
--- /dev/null
+++ b/interface/src/i18n/it/index.ts
@@ -0,0 +1,331 @@
+import type { Translation } from '../i18n-types';
+/* prettier-ignore */
+/* eslint-disable */
+
+const it: Translation = {
+ LANGUAGE: 'Lingua',
+ RETRY: 'Riprovare',
+ LOADING: 'Caricamento',
+ IS_REQUIRED: '{0} é richiesto',
+ SIGN_IN: 'Connettersi',
+ SIGN_OUT: 'Disconnettersi',
+ USERNAME: 'Nome Utente',
+ PASSWORD: 'Password',
+ SU_PASSWORD: 'su Password',
+ DASHBOARD: 'Pannello di Controllo',
+ SETTINGS_OF: 'Impostazioni {0}',
+ HELP_OF: '{0} Aiuto',
+ LOGGED_IN: 'Registrato come {name}',
+ PLEASE_SIGNIN: 'Prego registrarsi per continuare',
+ UPLOAD_SUCCESSFUL: 'Caricamento finito',
+ DOWNLOAD_SUCCESSFUL: 'Scaricamento finito',
+ INVALID_LOGIN: 'Dettagli accesso invalidi',
+ NETWORK: 'Rete',
+ SECURITY: 'Sicurezza',
+ ONOFF_CAP: 'ON/OFF',
+ ONOFF: 'on/off',
+ TYPE: 'Tipo',
+ DESCRIPTION: 'Descrizione',
+ ENTITIES: 'Entità',
+ REFRESH: 'Ricaricare',
+ EXPORT: 'Esporta',
+ DEVICE_DETAILS: 'Dettagli dispositivo',
+ ID_OF: '{0} ID',
+ DEVICE: 'Dispositivo',
+ PRODUCT: 'Prodotto',
+ VERSION: 'Versione',
+ BRAND: 'Marca',
+ ENTITY_NAME: 'Nome Entità',
+ VALUE: '{{Valore|valore}}',
+ DEVICE_DATA: 'Device Data',
+ SENSOR_DATA: 'Sensor Data',
+ DEVICES: 'Dispositivi',
+ SENSORS: 'Sensori',
+ RUN_COMMAND: 'Esegui',
+ CHANGE_VALUE: 'Cambia Valore',
+ CANCEL: 'Annulla',
+ RESET: 'Reset',
+ APPLY_CHANGES: 'Apply Changes ({0})',
+ UPDATE: 'Update',
+ EXECUTE: 'Execute',
+ REMOVE: 'Elimina',
+ PROBLEM_UPDATING: 'Problema aggiornamento',
+ PROBLEM_LOADING: 'Problema caricamento',
+ ACCESS_DENIED: 'Access Denied',
+ ANALOG_SENSOR: 'Sensore Analogico',
+ ANALOG_SENSORS: 'Sensori Analogici',
+ SETTINGS: 'Settings',
+ UPDATED_OF: '{0} Aggiornati',
+ UPDATE_OF: 'Aggiorna {0}',
+ REMOVED_OF: '{0} Rimossi',
+ DELETION_OF: '{0} Cancellati',
+ OFFSET: 'Offset',
+ FACTOR: 'Fattore',
+ FREQ: 'Frequenza',
+ DUTY_CYCLE: 'Ciclo di lavoro',
+ UNIT: 'UoM',
+ STARTVALUE: 'Valore di partenza',
+ WARN_GPIO: 'Avvertimento: prestare attenzione quando si assegna un GPIO!',
+ EDIT: 'Modifica',
+ SENSOR: 'Sensore',
+ TEMP_SENSOR: 'Sensore Temperatura',
+ TEMP_SENSORS: 'Sensori Temperatura',
+ WRITE_CMD_SENT: 'Scrittura comando inviata',
+ WRITE_CMD_FAILED: 'Scittura comando fallita',
+ EMS_BUS_WARNING: 'EMS bus disconnesso. Se questo avvertimento persiste dopo alcuni secondi prego verificare impostazioni scheda',
+ EMS_BUS_SCANNING: 'Scansione dispositivi EMS ...',
+ CONNECTED: 'Connesso',
+ TX_ISSUES: 'Problema di Tx - prova una modalità differente',
+ DISCONNECTED: 'Disconnesso',
+ EMS_SCAN: 'Sei sicuro di voler iniziare una scansione completa del bus EMS ?',
+ EMS_BUS_STATUS: 'Stato Bus EMS',
+ ACTIVE_DEVICES: 'Dispositivi & sensori attivi',
+ EMS_DEVICE: 'Dispositivo EMS ',
+ SUCCESS: 'SUCCESSO',
+ FAIL: 'FALLITO',
+ QUALITY: 'QUALITÂ',
+ SCAN_DEVICES: 'Scansione per nuovi dispositivi',
+ EMS_BUS_STATUS_TITLE: 'Bus EMS & Stato Attività',
+ SCAN: 'Scansione',
+ STATUS_NAMES: [
+ 'Telegrammi EMS Ricevuti (Rx)',
+ 'EMS Letti (Tx)',
+ 'EMS Scritti (Tx)',
+ 'Letture Sensori Temperatura',
+ 'Letture Sensori Analogici',
+ 'Pubblicazioni MQTT',
+ 'Chiamate API',
+ 'Messaggi Syslog'
+ ],
+ NUM_DEVICES: '{num} Dispositivi {{s}}',
+ NUM_TEMP_SENSORS: '{num} Sensori Temperatura {{s}}',
+ NUM_ANALOG_SENSORS: '{num} Sensori Analogici {{s}}',
+ NUM_DAYS: '{num} giorni {{s}}',
+ NUM_SECONDS: '{num} secondi {{s}}',
+ NUM_HOURS: '{num} ore {{s}}',
+ NUM_MINUTES: '{num} minuti {{s}}',
+ APPLICATION_SETTINGS: 'Impostazione Applicazione',
+ CUSTOMIZATIONS: 'Personalizzazione',
+ APPLICATION_RESTARTING: 'EMS-ESP sta riavviando',
+ INTERFACE_BOARD_PROFILE: 'Profilo scheda di interfaccia',
+ BOARD_PROFILE_TEXT: 'Selezionare un profilo di interfaccia pre-configurato dalla lista sottostante o scegliere un profilo personalizzato per configurare le impostazioni del tuo hardware',
+ BOARD_PROFILE: 'Profilo Scheda',
+ CUSTOM: 'Personalizzazione',
+ GPIO_OF: 'GPIO {0}',
+ BUTTON: 'Pulsante',
+ TEMPERATURE: 'Temperatura',
+ PHY_TYPE: 'Eth PHY Type',
+ DISABLED: 'disattivato',
+ TX_MODE: 'Modo Tx ',
+ HARDWARE: 'Hardware',
+ EMS_BUS: '{{BUS|EMS BUS}}',
+ GENERAL_OPTIONS: 'Opzioni Generali',
+ LANGUAGE_ENTITIES: 'Lingua (per entità dispositivi)',
+ HIDE_LED: 'Nascondi LED',
+ ENABLE_TELNET: 'Abilità la Console Telnet',
+ ENABLE_ANALOG: 'Abilita Sensori Analogici',
+ CONVERT_FAHRENHEIT: 'Converti valori temperatura in Fahrenheit',
+ BYPASS_TOKEN: 'Ignora autorizzazione del token di accesso sulle chiamate API',
+ READONLY: 'Abilita modalità sola-lettura (blocca tutti i comandi di scrittura EMS Tx in uscita)',
+ UNDERCLOCK_CPU: 'Abbassa velocità della CPU',
+ ENABLE_SHOWER_TIMER: 'Abilita timer doccia',
+ ENABLE_SHOWER_ALERT: 'Abilita avviso doccia',
+ TRIGGER_TIME: 'Tempo di avvio',
+ COLD_SHOT_DURATION: 'Durata colpo freddo',
+ FORMATTING_OPTIONS: 'Opzioni di formattazione',
+ BOOLEAN_FORMAT_DASHBOARD: 'Pannello di controllo in formato booleano',
+ BOOLEAN_FORMAT_API: 'Formato booleano API/MQTT',
+ ENUM_FORMAT: 'Enum Format API/MQTT',
+ INDEX: 'Indice',
+ ENABLE_PARASITE: 'Abilita potenza parassita',
+ LOGGING: 'Registrazione',
+ LOG_HEX: 'Registra telegrammi EMS in esadecimale',
+ ENABLE_SYSLOG: 'Abilita Syslog',
+ LOG_LEVEL: 'Livello registrazione',
+ MARK_INTERVAL: 'Segna Intervallo',
+ SECONDS: 'secondi',
+ MINUTES: 'minuti',
+ HOURS: 'ore',
+ RESTART: 'Riavvia',
+ RESTART_TEXT: 'EMS-ESP necessita di essere riavviato per applicare il cambio impostazioni del sistema',
+ RESTART_CONFIRM: 'Sei sicuro di voler riavviare EMS-ESP?',
+ COMMAND: 'Comando',
+ CUSTOMIZATIONS_RESTART: 'Tutte le personalizzazioni sono state rimosse. Riavvio ...',
+ CUSTOMIZATIONS_FULL: 'Le entità selezionate hanno superato il limite. Si prega di salvare in batch',
+ CUSTOMIZATIONS_SAVED: 'Personalizzazioni salvate',
+ CUSTOMIZATIONS_HELP_1: 'Seleziona un dispositivo e personalizza le opzioni delle entità o fai clic per rinominarlo',
+ CUSTOMIZATIONS_HELP_2: 'seleziona come preferito',
+ CUSTOMIZATIONS_HELP_3: 'disabilita azione scrittura',
+ CUSTOMIZATIONS_HELP_4: 'esculdi da MQTT e API',
+ CUSTOMIZATIONS_HELP_5: 'nascondi dal Pannello di controllo',
+ CUSTOMIZATIONS_HELP_6: 'rimuovi dalla memoria',
+ SELECT_DEVICE: 'Seleziona un dispositivo',
+ SET_ALL: 'imposta tutto',
+ OPTIONS: 'Opzioni',
+ NAME: 'Nome',
+ CUSTOMIZATIONS_RESET: 'Sei sicuro di voler rimuovere tutte le personalizzazioni incluse le impostazioni personalizzate dei sensori di temperatura e analogici?',
+ DEVICE_ENTITIES: 'Entità Dispositivo',
+ SUPPORT_INFORMATION: 'Informazioni di Supporto',
+ CLICK_HERE: 'Clicca qui',
+ HELP_INFORMATION_1: 'Visita il wiki online per ottenere istruzioni su come configurare EMS-ESP',
+ HELP_INFORMATION_2: 'Per la chat della community dal vivo unisciti al nostro server Discord',
+ HELP_INFORMATION_3: 'Per richiedere una funzionalità o segnalare un errore',
+ HELP_INFORMATION_4: 'ricordati di scaricare e allegare le informazioni del tuo sistema per una risposta più rapida quando segnali un problema',
+ HELP_INFORMATION_5: 'EMS-ESP è un progetto gratuito e open-source. Supporta il suo sviluppo futuro assegnandogli una stella su Github!',
+ SUPPORT_INFO: 'Info Supporto',
+ UPLOAD: 'Carica',
+ DOWNLOAD: 'Scarica',
+ ABORTED: 'Annullato',
+ FAILED: 'Fallito',
+ SUCCESSFUL: 'Riuscito',
+ SYSTEM: 'Sistema',
+ LOG_OF: 'Registro {0}',
+ STATUS_OF: 'Stato {0}',
+ UPLOAD_DOWNLOAD: 'Caricamento/Scaricamento',
+ VERSION_ON: 'Attualmente stai eseguendo la versione',
+ SYSTEM_APPLY_FIRMWARE: 'per applicare il nuovo firmware',
+ CLOSE: 'Chiudere',
+ USE: 'Usa',
+ FACTORY_RESET: 'Impostazioni di fabbrica',
+ SYSTEM_FACTORY_TEXT: 'Il dispositivo è stato ripristinato alle impostazioni di fabbrica e ora verrà riavviato',
+ SYSTEM_FACTORY_TEXT_DIALOG: 'Sei sicuro di voler ripristinare il dispositivo alle impostazioni di fabbrica??',
+ VERSION_CHECK: 'Verifica Versione',
+ THE_LATEST: 'Ultima',
+ OFFICIAL: 'ufficiale',
+ DEVELOPMENT: 'sviluppo',
+ RELEASE_IS: 'rilascio é',
+ RELEASE_NOTES: 'note rilascio',
+ EMS_ESP_VER: 'Versione EMS-ESP',
+ PLATFORM: 'Dispositivo (Piattaforma / SDK)',
+ UPTIME: 'Tempo di attività del sistema',
+ CPU_FREQ: 'Frequenza CPU ',
+ HEAP: 'Heap (Free / Max Alloc)',
+ PSRAM: 'PSRAM (Size / Free)',
+ FLASH: 'Flash Chip (Size / Speed)',
+ APPSIZE: 'Applicazione (Usata / Libera)',
+ FILESYSTEM: 'Memoria Sistema (Usata / Libera)',
+ BUFFER_SIZE: 'Max Buffer Size',
+ COMPACT: 'Compact',
+ ENABLE_OTA: 'Abilita aggiornamenti OTA',
+ DOWNLOAD_CUSTOMIZATION_TEXT: 'Scarica personalizzazioni entità',
+ DOWNLOAD_SCHEDULE_TEXT: 'Download Scheduler Events',
+ DOWNLOAD_SETTINGS_TEXT: 'Scarica le impostazioni dell applicazione. Fai attenzione quando condividi le tue impostazioni poiché questo file contiene password e altre informazioni di sistema riservate',
+ UPLOAD_TEXT: 'Carica un nuovo file firmware (.bin) , file delle impostazioni o delle personalizzazioni (.json) di seguito, per un opzione di convalida scaricare dapprima un file "*.MD5" ',
+ UPLOADING: 'Caricamento',
+ UPLOAD_DROP_TEXT: 'Trascina il file o clicca qui',
+ ERROR: 'Errore Inaspettato, prego tenta ancora',
+ TIME_SET: 'Imposta Ora',
+ MANAGE_USERS: 'Gestione Utenti',
+ IS_ADMIN: 'Amministratore',
+ USER_WARNING: 'Devi avere configurato almeno un utente amministratore',
+ ADD: 'Aggiungi',
+ ACCESS_TOKEN_FOR: 'Token di accesso per',
+ ACCESS_TOKEN_TEXT: 'Il token seguente viene utilizzato con le chiamate API REST che richiedono l autorizzazione. Può essere passato come token Bearer nell intestazione di autorizzazione o nel parametro di query URL access_token.',
+ GENERATING_TOKEN: 'Generazione token',
+ USER: 'Utente',
+ MODIFY: 'Modifica',
+ SU_TEXT: 'La password su (super utente) viene utilizzata per firmare i token di autenticazione e abilitare anche i privilegi di amministratore all interno della console.',
+ NOT_ENABLED: 'Non abilitato',
+ ERRORS_OF: 'Errori {0}',
+ DISCONNECT_REASON: 'Motivo disconnessione',
+ ENABLE_MQTT: 'Abilita MQTT',
+ BROKER: 'Broker',
+ CLIENT: 'Cliente',
+ BASE_TOPIC: 'Base',
+ OPTIONAL: 'Opzionale',
+ FORMATTING: 'Formattazione',
+ MQTT_FORMAT: 'Formato Topic/Payload ',
+ MQTT_NEST_1: 'Inserito in un singolo argomento',
+ MQTT_NEST_2: 'Come argomenti individuali',
+ MQTT_RESPONSE: 'Pubblica uscita del comando in un argomento di risposta',
+ MQTT_PUBLISH_TEXT_1: 'Pubblica argomenti a valore singolo sul cambiamento',
+ MQTT_PUBLISH_TEXT_2: 'Pubblica per comandare gli argomenti (ioBroker)',
+ MQTT_PUBLISH_TEXT_3: 'Abilita rilevamento MQTT (Home Assistant, Domoticz)',
+ MQTT_PUBLISH_TEXT_4: 'Prefisso per gli argomenti di scoperta',
+ MQTT_PUBLISH_TEXT_5: 'Discovery type',
+ MQTT_PUBLISH_INTERVALS: 'Pubblica intervalli',
+ MQTT_INT_BOILER: 'Caldaie e Pompe di Calore',
+ MQTT_INT_THERMOSTATS: 'Termostati',
+ MQTT_INT_SOLAR: 'Moduli solari',
+ MQTT_INT_MIXER: 'Moduli Mixer',
+ MQTT_INT_HEARTBEAT: 'Heartbeat',
+ MQTT_QUEUE: 'Coda MQTT',
+ DEFAULT: 'Predefinito',
+ MQTT_ENTITY_FORMAT: 'Formato ID entità',
+ MQTT_ENTITY_FORMAT_0: 'Singola istanza, nome lungo (v3.4)',
+ MQTT_ENTITY_FORMAT_1: 'Sinola istanza, nome breve',
+ MQTT_ENTITY_FORMAT_2: 'Istanze multiple, nome breve',
+ MQTT_CLEAN_SESSION: 'Imposta sessione pulita',
+ MQTT_RETAIN_FLAG: 'Imposta sempre il flag Retain',
+ INACTIVE: 'Inattivo',
+ ACTIVE: 'Attivo',
+ UNKNOWN: 'Sconosciuto',
+ SET_TIME: 'Imposta ora',
+ SET_TIME_TEXT: 'Immettere la data e l ora locale di seguito per impostare l ora',
+ LOCAL_TIME: 'Ora locale',
+ UTC_TIME: 'Ora UTC',
+ ENABLE_NTP: 'Abilita NTP',
+ NTP_SERVER: 'Server NTP',
+ TIME_ZONE: 'Fuso orario',
+ ACCESS_POINT: 'Access Point',
+ AP_PROVIDE: 'Abilita Access Point',
+ AP_PROVIDE_TEXT_1: 'sempre',
+ AP_PROVIDE_TEXT_2: 'quando WiFi é disconnessa',
+ AP_PROVIDE_TEXT_3: 'mai',
+ AP_PREFERRED_CHANNEL: 'Canale preferito',
+ AP_HIDE_SSID: 'Nascondi SSID',
+ AP_CLIENTS: 'Clienti AP',
+ AP_MAX_CLIENTS: 'Clienti Massimi',
+ AP_LOCAL_IP: 'IP Locale',
+ NETWORK_SCAN: 'Scansione reti WiFi',
+ IDLE: 'Inattivo',
+ LOST: 'Perso',
+ SCANNING: 'Scansione',
+ SCAN_AGAIN: 'Scansiona ancora',
+ NETWORK_SCANNER: 'Scansione Rete',
+ NETWORK_NO_WIFI: 'Nessuana rete WiFi trovata',
+ NETWORK_BLANK_SSID: 'lasciare vuoto per disattivare WiFi',
+ TX_POWER: 'Potenza Tx',
+ HOSTNAME: 'Nome ospite',
+ NETWORK_DISABLE_SLEEP: 'Disabilita la modalità sospensione Wi-Fi',
+ NETWORK_LOW_BAND: 'Usa una larghezza di banda WiFi inferiore',
+ NETWORK_USE_DNS: 'Abilita servizio mDNS',
+ NETWORK_ENABLE_CORS: 'Abilita CORS',
+ NETWORK_CORS_ORIGIN: 'origine CORS',
+ NETWORK_ENABLE_IPV6: 'Abilita supporto IPv6',
+ NETWORK_FIXED_IP: 'Usa indirizzo IP fisso',
+ NETWORK_GATEWAY: 'Gateway',
+ NETWORK_SUBNET: 'Maschera Sottorete',
+ NETWORK_DNS: 'Server DNS',
+ ADDRESS_OF: 'Indirizzo {0}',
+ ADMIN: 'Amministratore',
+ GUEST: 'Ospite',
+ NEW: 'Nuovo',
+ NEW_NAME_OF: 'Nuovo nome {0}',
+ ENTITY: 'entità',
+ MIN: 'min',
+ MAX: 'max',
+ BLOCK_NAVIGATE_1: 'You have unsaved changes',
+ BLOCK_NAVIGATE_2: 'If you navigate to a different page, your unsaved changes will be lost. Are you sure you want to leave this page?',
+ STAY: 'Stay',
+ LEAVE: 'Leave',
+ SCHEDULER: 'Scheduler',
+ SCHEDULER_HELP_1: 'Automate commands by adding scheduled events below. Set a unique Name to enable/disable activation via API/MQTT.',
+ SCHEDULER_HELP_2: 'Use 00:00 to trigger once on start-up',
+ SCHEDULE: 'Schedule',
+ TIME: 'Time',
+ TIMER: 'Timer',
+ SCHEDULE_UPDATED: 'Schedule updated',
+ SCHEDULE_TIMER_1: 'on startup',
+ SCHEDULE_TIMER_2: 'every minute',
+ SCHEDULE_TIMER_3: 'every hour',
+ CUSTOM_ENTITIES: 'Custom Entities',
+ ENTITIES_HELP_1: 'Fetch custom entities from the EMS bus',
+ ENTITIES_UPDATED: 'Entities Updated',
+ WRITEABLE: 'Writeable',
+ SHOWING: 'Showing',
+ SEARCH: 'Search'
+
+};
+
+export default it;
diff --git a/interface/src/project/DashboardDevicesDialog.tsx b/interface/src/project/DashboardDevicesDialog.tsx
index f2c32a0bb..f1f43167a 100644
--- a/interface/src/project/DashboardDevicesDialog.tsx
+++ b/interface/src/project/DashboardDevicesDialog.tsx
@@ -98,12 +98,12 @@ const DashboarDevicesDialog = ({
}
let helperText = '<';
- if (dv.u !== DeviceValueUOM.NONE) {
+ if (dv.s) {
helperText += 'n';
- if (dv.m && dv.x) {
+ if (dv.m !== undefined && dv.x !== undefined) {
+
helperText += ' between ' + dv.m + ' and ' + dv.x;
- }
- if (dv.s) {
+ } else {
helperText += ' , step ' + dv.s;
}
} else {
@@ -149,7 +149,7 @@ const DashboarDevicesDialog = ({
))}
- ) : editItem.u !== DeviceValueUOM.NONE ? (
+ ) : editItem.s || editItem.u !== DeviceValueUOM.NONE ? (
{setUom(editItem.u)}
}}
diff --git a/interface/src/project/SettingsApplication.tsx b/interface/src/project/SettingsApplication.tsx
index 8dc037f06..3771efa73 100644
--- a/interface/src/project/SettingsApplication.tsx
+++ b/interface/src/project/SettingsApplication.tsx
@@ -381,6 +381,7 @@ const SettingsApplication: FC = () => {
+
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient.cpp b/lib/async-mqtt-client/src/AsyncMqttClient.cpp
deleted file mode 100644
index 7c0f269e9..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient.cpp
+++ /dev/null
@@ -1,776 +0,0 @@
-#include "AsyncMqttClient.hpp"
-
-AsyncMqttClient::AsyncMqttClient()
-: _client()
-, _head(nullptr)
-, _tail(nullptr)
-, _sent(0)
-, _state(DISCONNECTED)
-, _disconnectReason(AsyncMqttClientDisconnectReason::TCP_DISCONNECTED)
-, _lastClientActivity(0)
-, _lastServerActivity(0)
-, _lastPingRequestTime(0)
-, _generatedClientId{0}
-, _ip()
-, _ipv6()
-, _host(nullptr)
-, _useIp(false)
-, _useIpv6(false)
-#if ASYNC_TCP_SSL_ENABLED
-, _secure(false)
-#endif
-, _port(0)
-, _keepAlive(15)
-, _cleanSession(true)
-, _clientId(nullptr)
-, _username(nullptr)
-, _password(nullptr)
-, _willTopic(nullptr)
-, _willPayload(nullptr)
-, _willPayloadLength(0)
-, _willQos(0)
-, _willRetain(false)
-#if ASYNC_TCP_SSL_ENABLED
-, _secureServerFingerprints()
-#endif
-, _onConnectUserCallbacks()
-, _onDisconnectUserCallbacks()
-, _onSubscribeUserCallbacks()
-, _onUnsubscribeUserCallbacks()
-, _onMessageUserCallbacks()
-, _onPublishUserCallbacks()
-, _parsingInformation { .bufferState = AsyncMqttClientInternals::BufferState::NONE }
-, _currentParsedPacket(nullptr)
-, _remainingLengthBufferPosition(0)
-, _remainingLengthBuffer{0}
-, _pendingPubRels() {
- _client.onConnect([](void* obj, AsyncClient* c) { (static_cast(obj))->_onConnect(); }, this);
- _client.onDisconnect([](void* obj, AsyncClient* c) { (static_cast(obj))->_onDisconnect(); }, this);
- // _client.onError([](void* obj, AsyncClient* c, int8_t error) { (static_cast(obj))->_onError(error); }, this);
- // _client.onTimeout([](void* obj, AsyncClient* c, uint32_t time) { (static_cast(obj))->_onTimeout(); }, this);
- _client.onAck([](void* obj, AsyncClient* c, size_t len, uint32_t time) { (static_cast(obj))->_onAck(len); }, this);
- _client.onData([](void* obj, AsyncClient* c, void* data, size_t len) { (static_cast(obj))->_onData(static_cast(data), len); }, this);
- _client.onPoll([](void* obj, AsyncClient* c) { (static_cast(obj))->_onPoll(); }, this);
- _client.setNoDelay(true); // send small packets immediately (PINGREQ/DISCONN are only 2 bytes)
-#ifdef ESP32
- sprintf(_generatedClientId, "esp32-%06llx", ESP.getEfuseMac());
- _xSemaphore = xSemaphoreCreateMutex();
-#elif defined(ESP8266)
- sprintf(_generatedClientId, "esp8266-%06x", ESP.getChipId());
-#endif
- _clientId = _generatedClientId;
-
- setMaxTopicLength(128);
-}
-
-AsyncMqttClient::~AsyncMqttClient() {
- delete _currentParsedPacket;
- delete[] _parsingInformation.topicBuffer;
- _clear();
- _pendingPubRels.clear();
- _pendingPubRels.shrink_to_fit();
- _clearQueue(false); // _clear() doesn't clear session data
-#ifdef ESP32
- vSemaphoreDelete(_xSemaphore);
-#endif
-}
-
-AsyncMqttClient& AsyncMqttClient::setKeepAlive(uint16_t keepAlive) {
- _keepAlive = keepAlive;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setClientId(const char* clientId) {
- _clientId = clientId;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setCleanSession(bool cleanSession) {
- _cleanSession = cleanSession;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setMaxTopicLength(uint16_t maxTopicLength) {
- _parsingInformation.maxTopicLength = maxTopicLength;
- delete[] _parsingInformation.topicBuffer;
- _parsingInformation.topicBuffer = new char[maxTopicLength + 1];
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setCredentials(const char* username, const char* password) {
- _username = username;
- _password = password;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setWill(const char* topic, uint8_t qos, bool retain, const char* payload, size_t length) {
- _willTopic = topic;
- _willQos = qos;
- _willRetain = retain;
- _willPayload = payload;
- _willPayloadLength = length;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setServer(IPAddress ip, uint16_t port) {
- _useIp = true;
- _useIpv6 = false;
- _ip = ip;
- _port = port;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setServer(IPv6Address ipv6, uint16_t port) {
- _useIpv6 = true;
- _useIp = false;
- _ipv6 = ipv6;
- _port = port;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::setServer(const char* host, uint16_t port) {
- _port = port;
- _useIp = false;
- _useIpv6 = false;
- _host = host;
- if (_ipv6.fromString(host)) {
- _useIpv6 = true;
- _useIp = false;
- } else if (_ip.fromString(host)) {
- _useIpv6 = false;
- _useIp = true;
- }
- return *this;
-}
-
-#if ASYNC_TCP_SSL_ENABLED
-AsyncMqttClient& AsyncMqttClient::setSecure(bool secure) {
- _secure = secure;
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::addServerFingerprint(const uint8_t* fingerprint) {
- std::array newFingerprint;
- memcpy(newFingerprint.data(), fingerprint, SHA1_SIZE);
- _secureServerFingerprints.push_back(newFingerprint);
- return *this;
-}
-#endif
-
-AsyncMqttClient& AsyncMqttClient::onConnect(AsyncMqttClientInternals::OnConnectUserCallback callback) {
- _onConnectUserCallbacks.push_back(callback);
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::onDisconnect(AsyncMqttClientInternals::OnDisconnectUserCallback callback) {
- _onDisconnectUserCallbacks.push_back(callback);
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::onSubscribe(AsyncMqttClientInternals::OnSubscribeUserCallback callback) {
- _onSubscribeUserCallbacks.push_back(callback);
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::onUnsubscribe(AsyncMqttClientInternals::OnUnsubscribeUserCallback callback) {
- _onUnsubscribeUserCallbacks.push_back(callback);
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::onMessage(AsyncMqttClientInternals::OnMessageUserCallback callback) {
- _onMessageUserCallbacks.push_back(callback);
- return *this;
-}
-
-AsyncMqttClient& AsyncMqttClient::onPublish(AsyncMqttClientInternals::OnPublishUserCallback callback) {
- _onPublishUserCallbacks.push_back(callback);
- return *this;
-}
-
-void AsyncMqttClient::_freeCurrentParsedPacket() {
- delete _currentParsedPacket;
- _currentParsedPacket = nullptr;
-}
-
-void AsyncMqttClient::_clear() {
- _lastPingRequestTime = 0;
- _freeCurrentParsedPacket();
- _clearQueue(true); // keep session data for now
-
- _parsingInformation.bufferState = AsyncMqttClientInternals::BufferState::NONE;
-
- _client.setRxTimeout(0);
-}
-
-/* TCP */
-void AsyncMqttClient::_onConnect() {
- log_i("TCP conn, MQTT CONNECT");
-#if ASYNC_TCP_SSL_ENABLED
- if (_secure && _secureServerFingerprints.size() > 0) {
- SSL* clientSsl = _client.getSSL();
-
- bool sslFoundFingerprint = false;
- for (std::array fingerprint : _secureServerFingerprints) {
- if (ssl_match_fingerprint(clientSsl, fingerprint.data()) == SSL_OK) {
- sslFoundFingerprint = true;
- break;
- }
- }
-
- if (!sslFoundFingerprint) {
- _disconnectReason = AsyncMqttClientDisconnectReason::TLS_BAD_FINGERPRINT;
- _client.close(true);
- return;
- }
- }
-#endif
- AsyncMqttClientInternals::OutPacket* msg =
- new AsyncMqttClientInternals::ConnectOutPacket(_cleanSession,
- _username,
- _password,
- _willTopic,
- _willRetain,
- _willQos,
- _willPayload,
- _willPayloadLength,
- _keepAlive,
- _clientId);
- _addFront(msg);
- _handleQueue();
-}
-
-void AsyncMqttClient::_onDisconnect() {
- log_i("TCP disconn");
- _state = DISCONNECTED;
-
- _clear();
-
- for (auto callback : _onDisconnectUserCallbacks) callback(_disconnectReason);
-}
-
-/*
-void AsyncMqttClient::_onError(int8_t error) {
- (void)error;
- // _onDisconnect called anyway
-}
-
-void AsyncMqttClient::_onTimeout() {
- // disconnection will be handled by ping/pong management
-}
-*/
-
-void AsyncMqttClient::_onAck(size_t len) {
- log_i("ack %u", len);
- _handleQueue();
-}
-
-void AsyncMqttClient::_onData(char* data, size_t len) {
- log_i("data rcv (%u)", len);
- size_t currentBytePosition = 0;
- char currentByte;
- _lastServerActivity = millis();
- do {
- switch (_parsingInformation.bufferState) {
- case AsyncMqttClientInternals::BufferState::NONE:
- currentByte = data[currentBytePosition++];
- _parsingInformation.packetType = currentByte >> 4;
- _parsingInformation.packetFlags = (currentByte << 4) >> 4;
- _parsingInformation.bufferState = AsyncMqttClientInternals::BufferState::REMAINING_LENGTH;
- switch (_parsingInformation.packetType) {
- case AsyncMqttClientInternals::PacketType.CONNACK:
- log_i("rcv CONNACK");
- _currentParsedPacket = new AsyncMqttClientInternals::ConnAckPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onConnAck, this, std::placeholders::_1, std::placeholders::_2));
- _client.setRxTimeout(0);
- break;
- case AsyncMqttClientInternals::PacketType.PINGRESP:
- log_i("rcv PINGRESP");
- _currentParsedPacket = new AsyncMqttClientInternals::PingRespPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onPingResp, this));
- break;
- case AsyncMqttClientInternals::PacketType.SUBACK:
- log_i("rcv SUBACK");
- _currentParsedPacket = new AsyncMqttClientInternals::SubAckPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onSubAck, this, std::placeholders::_1, std::placeholders::_2));
- break;
- case AsyncMqttClientInternals::PacketType.UNSUBACK:
- log_i("rcv UNSUBACK");
- _currentParsedPacket = new AsyncMqttClientInternals::UnsubAckPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onUnsubAck, this, std::placeholders::_1));
- break;
- case AsyncMqttClientInternals::PacketType.PUBLISH:
- log_i("rcv PUBLISH");
- _currentParsedPacket = new AsyncMqttClientInternals::PublishPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6, std::placeholders::_7, std::placeholders::_8, std::placeholders::_9), std::bind(&AsyncMqttClient::_onPublish, this, std::placeholders::_1, std::placeholders::_2));
- break;
- case AsyncMqttClientInternals::PacketType.PUBREL:
- log_i("rcv PUBREL");
- _currentParsedPacket = new AsyncMqttClientInternals::PubRelPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onPubRel, this, std::placeholders::_1));
- break;
- case AsyncMqttClientInternals::PacketType.PUBACK:
- log_i("rcv PUBACK");
- _currentParsedPacket = new AsyncMqttClientInternals::PubAckPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onPubAck, this, std::placeholders::_1));
- break;
- case AsyncMqttClientInternals::PacketType.PUBREC:
- log_i("rcv PUBREC");
- _currentParsedPacket = new AsyncMqttClientInternals::PubRecPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onPubRec, this, std::placeholders::_1));
- break;
- case AsyncMqttClientInternals::PacketType.PUBCOMP:
- log_i("rcv PUBCOMP");
- _currentParsedPacket = new AsyncMqttClientInternals::PubCompPacket(&_parsingInformation, std::bind(&AsyncMqttClient::_onPubComp, this, std::placeholders::_1));
- break;
- default:
- log_i("rcv PROTOCOL VIOLATION");
- disconnect(true);
- break;
- }
- break;
- case AsyncMqttClientInternals::BufferState::REMAINING_LENGTH:
- currentByte = data[currentBytePosition++];
- _remainingLengthBuffer[_remainingLengthBufferPosition++] = currentByte;
- if (currentByte >> 7 == 0) {
- _parsingInformation.remainingLength = AsyncMqttClientInternals::Helpers::decodeRemainingLength(_remainingLengthBuffer);
- _remainingLengthBufferPosition = 0;
- if (_parsingInformation.remainingLength > 0) {
- _parsingInformation.bufferState = AsyncMqttClientInternals::BufferState::VARIABLE_HEADER;
- } else {
- // PINGRESP is a special case where it has no variable header, so the packet ends right here
- _parsingInformation.bufferState = AsyncMqttClientInternals::BufferState::NONE;
- _onPingResp();
- }
- }
- break;
- case AsyncMqttClientInternals::BufferState::VARIABLE_HEADER:
- _currentParsedPacket->parseVariableHeader(data, len, ¤tBytePosition);
- break;
- case AsyncMqttClientInternals::BufferState::PAYLOAD:
- _currentParsedPacket->parsePayload(data, len, ¤tBytePosition);
- break;
- default:
- currentBytePosition = len;
- }
- } while (currentBytePosition != len);
-}
-
-void AsyncMqttClient::_onPoll() {
- // if there is too much time the client has sent a ping request without a response, disconnect client to avoid half open connections
- if (_lastPingRequestTime != 0 && (millis() - _lastPingRequestTime) >= (_keepAlive * 1000 * 2)) {
- log_w("PING t/o, disconnecting");
- disconnect(true);
- return;
- }
- // send ping to ensure the server will receive at least one message inside keepalive window
- if (_state == CONNECTED && _lastPingRequestTime == 0 && (millis() - _lastClientActivity) >= (_keepAlive * 1000 * 0.7)) {
- _sendPing();
- // send ping to verify if the server is still there (ensure this is not a half connection)
- } else if (_state == CONNECTED && _lastPingRequestTime == 0 && (millis() - _lastServerActivity) >= (_keepAlive * 1000 * 0.7)) {
- _sendPing();
- }
- _handleQueue();
-}
-
-/* QUEUE */
-
-void AsyncMqttClient::_insert(AsyncMqttClientInternals::OutPacket* packet) {
- // We only use this for QoS2 PUBREL so there must be a PUBLISH packet present.
- // The queue therefore cannot be empty and _head points to this PUBLISH packet.
- SEMAPHORE_TAKE();
- log_i("new insert #%u", packet->packetType());
- packet->next = _head->next;
- _head->next = packet;
- if (_head == _tail) { // PUB packet is the only one in the queue
- _tail = packet;
- }
- SEMAPHORE_GIVE();
- _handleQueue();
-}
-
-void AsyncMqttClient::_addFront(AsyncMqttClientInternals::OutPacket* packet) {
- // This is only used for the CONNECT packet, to be able to establish a connection
- // before anything else. The queue can be empty or has packets from the continued session.
- // In both cases, _head should always point to the CONNECT packet afterwards.
- SEMAPHORE_TAKE();
- log_i("new front #%u", packet->packetType());
- if (_head == nullptr) {
- _tail = packet;
- } else {
- packet->next = _head;
- }
- _head = packet;
- SEMAPHORE_GIVE();
- _handleQueue();
-}
-
-void AsyncMqttClient::_addBack(AsyncMqttClientInternals::OutPacket* packet) {
- SEMAPHORE_TAKE();
- log_i("new back #%u", packet->packetType());
- if (!_tail) {
- _head = packet;
- } else {
- _tail->next = packet;
- }
- _tail = packet;
- _tail->next = nullptr;
- SEMAPHORE_GIVE();
- _handleQueue();
-}
-
-void AsyncMqttClient::_handleQueue() {
- SEMAPHORE_TAKE();
- // On ESP32, onDisconnect is called within the close()-call. So we need to make sure we don't lock
- bool disconnect = false;
-
- while (_head && _client.space() > 10) { // safe but arbitrary value, send at least 10 bytes
- // 1. try to send
- if (_head->size() > _sent) {
- // On SSL the TCP library returns the total amount of bytes, not just the unencrypted payload length.
- // So we calculate the amount to be written ourselves.
- size_t willSend = std::min(_head->size() - _sent, _client.space());
- size_t realSent = _client.add(reinterpret_cast(_head->data(_sent)), willSend, ASYNC_WRITE_FLAG_COPY); // flag is set by LWIP anyway, added for clarity
- _sent += willSend;
- (void)realSent;
- _client.send();
- _lastClientActivity = millis();
- // _lastPingRequestTime = 0; // https://github.com/marvinroger/async-mqtt-client/issues/281#issuecomment-1112897839
- #if ASYNC_TCP_SSL_ENABLED
- log_i("snd #%u: (tls: %u) %u/%u", _head->packetType(), realSent, _sent, _head->size());
- #else
- log_i("snd #%u: %u/%u", _head->packetType(), _sent, _head->size());
- #endif
- if (_head->packetType() == AsyncMqttClientInternals::PacketType.DISCONNECT) {
- disconnect = true;
- }
- }
-
- // 2. stop processing when we have to wait for an MQTT acknowledgment
- if (_head->size() == _sent) {
- if (_head->released()) {
- log_i("p #%d rel", _head->packetType());
- AsyncMqttClientInternals::OutPacket* tmp = _head;
- _head = _head->next;
- if (!_head) _tail = nullptr;
- delete tmp;
- _sent = 0;
- } else {
- break; // sending is complete however send next only after mqtt confirmation
- }
- }
- }
-
- SEMAPHORE_GIVE();
- if (disconnect) {
- log_i("snd DISCONN, disconnecting");
- _client.close();
- }
-}
-
-void AsyncMqttClient::_clearQueue(bool keepSessionData) {
- SEMAPHORE_TAKE();
- AsyncMqttClientInternals::OutPacket* packet = _head;
- _head = nullptr;
- _tail = nullptr;
-
- while (packet) {
- /* MQTT spec 3.1.2.4 Clean Session:
- * - QoS 1 and QoS 2 messages which have been sent to the Server, but have not been completely acknowledged.
- * - QoS 2 messages which have been received from the Server, but have not been completely acknowledged.
- * + (unsent PUB messages with QoS > 0)
- *
- * To be kept:
- * - possibly first message (sent to server but not acked)
- * - PUBREC messages (QoS 2 PUB received but not acked)
- * - PUBCOMP messages (QoS 2 PUBREL received but not acked)
- */
- if (keepSessionData) {
- if (packet->qos() > 0 && packet->size() <= _sent) { // check for qos includes check for PUB-packet type
- reinterpret_cast(packet)->setDup();
- AsyncMqttClientInternals::OutPacket* next = packet->next;
- log_i("keep #%u", packet->packetType());
- SEMAPHORE_GIVE();
- _addBack(packet);
- SEMAPHORE_TAKE();
- packet = next;
- } else if (packet->qos() > 0 ||
- packet->packetType() == AsyncMqttClientInternals::PacketType.PUBREC ||
- packet->packetType() == AsyncMqttClientInternals::PacketType.PUBCOMP) {
- AsyncMqttClientInternals::OutPacket* next = packet->next;
- log_i("keep #%u", packet->packetType());
- SEMAPHORE_GIVE();
- _addBack(packet);
- SEMAPHORE_TAKE();
- packet = next;
- } else {
- AsyncMqttClientInternals::OutPacket* next = packet->next;
- delete packet;
- packet = next;
- }
- /* Delete everything when not keeping session data
- */
- } else {
- AsyncMqttClientInternals::OutPacket* next = packet->next;
- delete packet;
- packet = next;
- }
- }
- _sent = 0;
- SEMAPHORE_GIVE();
-}
-
-/* MQTT */
-void AsyncMqttClient::_onPingResp() {
- log_i("PINGRESP");
- _freeCurrentParsedPacket();
- _lastPingRequestTime = 0;
-}
-
-void AsyncMqttClient::_onConnAck(bool sessionPresent, uint8_t connectReturnCode) {
- log_i("CONNACK");
- _freeCurrentParsedPacket();
-
- if (!sessionPresent) {
- _pendingPubRels.clear();
- _pendingPubRels.shrink_to_fit();
- _clearQueue(false); // remove session data
- }
-
- if (connectReturnCode == 0) {
- _state = CONNECTED;
- for (auto callback : _onConnectUserCallbacks) callback(sessionPresent);
- } else {
- // Callbacks are handled by the onDisconnect function which is called from the AsyncTcp lib
- _disconnectReason = static_cast(connectReturnCode);
- return;
- }
- _handleQueue(); // send any remaining data from continued session
-}
-
-void AsyncMqttClient::_onSubAck(uint16_t packetId, char status) {
- log_i("SUBACK");
- _freeCurrentParsedPacket();
- SEMAPHORE_TAKE();
- if (_head && _head->packetId() == packetId) {
- _head->release();
- log_i("SUB released");
- }
- SEMAPHORE_GIVE();
-
- for (auto callback : _onSubscribeUserCallbacks) callback(packetId, status);
-
- _handleQueue(); // subscribe confirmed, ready to send next queued item
-}
-
-void AsyncMqttClient::_onUnsubAck(uint16_t packetId) {
- log_i("UNSUBACK");
- _freeCurrentParsedPacket();
- SEMAPHORE_TAKE();
- if (_head && _head->packetId() == packetId) {
- _head->release();
- log_i("UNSUB released");
- }
- SEMAPHORE_GIVE();
-
- for (auto callback : _onUnsubscribeUserCallbacks) callback(packetId);
-
- _handleQueue(); // unsubscribe confirmed, ready to send next queued item
-}
-
-void AsyncMqttClient::_onMessage(char* topic, char* payload, uint8_t qos, bool dup, bool retain, size_t len, size_t index, size_t total, uint16_t packetId) {
- bool notifyPublish = true;
-
- if (qos == 2) {
- for (AsyncMqttClientInternals::PendingPubRel pendingPubRel : _pendingPubRels) {
- if (pendingPubRel.packetId == packetId) {
- notifyPublish = false;
- break;
- }
- }
- }
-
- if (notifyPublish) {
- AsyncMqttClientMessageProperties properties;
- properties.qos = qos;
- properties.dup = dup;
- properties.retain = retain;
-
- for (auto callback : _onMessageUserCallbacks) callback(topic, payload, properties, len, index, total);
- }
-}
-
-void AsyncMqttClient::_onPublish(uint16_t packetId, uint8_t qos) {
- AsyncMqttClientInternals::PendingAck pendingAck;
-
- if (qos == 1) {
- pendingAck.packetType = AsyncMqttClientInternals::PacketType.PUBACK;
- pendingAck.headerFlag = AsyncMqttClientInternals::HeaderFlag.PUBACK_RESERVED;
- pendingAck.packetId = packetId;
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::PubAckOutPacket(pendingAck);
- _addBack(msg);
- } else if (qos == 2) {
- pendingAck.packetType = AsyncMqttClientInternals::PacketType.PUBREC;
- pendingAck.headerFlag = AsyncMqttClientInternals::HeaderFlag.PUBREC_RESERVED;
- pendingAck.packetId = packetId;
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::PubAckOutPacket(pendingAck);
- _addBack(msg);
-
- bool pubRelAwaiting = false;
- for (AsyncMqttClientInternals::PendingPubRel pendingPubRel : _pendingPubRels) {
- if (pendingPubRel.packetId == packetId) {
- pubRelAwaiting = true;
- break;
- }
- }
-
- if (!pubRelAwaiting) {
- AsyncMqttClientInternals::PendingPubRel pendingPubRel;
- pendingPubRel.packetId = packetId;
- _pendingPubRels.push_back(pendingPubRel);
- }
- }
-
- _freeCurrentParsedPacket();
-}
-
-void AsyncMqttClient::_onPubRel(uint16_t packetId) {
- _freeCurrentParsedPacket();
-
- AsyncMqttClientInternals::PendingAck pendingAck;
- pendingAck.packetType = AsyncMqttClientInternals::PacketType.PUBCOMP;
- pendingAck.headerFlag = AsyncMqttClientInternals::HeaderFlag.PUBCOMP_RESERVED;
- pendingAck.packetId = packetId;
- if (_head && _head->packetId() == packetId) {
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::PubAckOutPacket(pendingAck);
- _head->release();
- _insert(msg);
- log_i("PUBREC released");
- }
-
- for (size_t i = 0; i < _pendingPubRels.size(); i++) {
- if (_pendingPubRels[i].packetId == packetId) {
- _pendingPubRels.erase(_pendingPubRels.begin() + i);
- _pendingPubRels.shrink_to_fit();
- }
- }
-}
-
-void AsyncMqttClient::_onPubAck(uint16_t packetId) {
- _freeCurrentParsedPacket();
- if (_head && _head->packetId() == packetId) {
- _head->release();
- log_i("PUB released");
- }
-
- for (auto callback : _onPublishUserCallbacks) callback(packetId);
-}
-
-void AsyncMqttClient::_onPubRec(uint16_t packetId) {
- _freeCurrentParsedPacket();
-
- // We will only be sending 1 QoS>0 PUB message at a time (to honor message
- // ordering). So no need to store ACKS in a separate container as it will
- // be stored in the outgoing queue until a PUBCOMP comes in.
- AsyncMqttClientInternals::PendingAck pendingAck;
- pendingAck.packetType = AsyncMqttClientInternals::PacketType.PUBREL;
- pendingAck.headerFlag = AsyncMqttClientInternals::HeaderFlag.PUBREL_RESERVED;
- pendingAck.packetId = packetId;
- log_i("snd PUBREL");
-
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::PubAckOutPacket(pendingAck);
- if (_head && _head->packetId() == packetId) {
- _head->release();
- log_i("PUB released");
- }
- _insert(msg);
-}
-
-void AsyncMqttClient::_onPubComp(uint16_t packetId) {
- _freeCurrentParsedPacket();
-
- // _head points to the PUBREL package
- if (_head && _head->packetId() == packetId) {
- _head->release();
- log_i("PUBREL released");
- }
-
- for (auto callback : _onPublishUserCallbacks) callback(packetId);
-}
-
-void AsyncMqttClient::_sendPing() {
- log_i("PING");
- _lastPingRequestTime = millis();
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::PingReqOutPacket;
- _addBack(msg);
-}
-
-bool AsyncMqttClient::connected() const {
- return _state == CONNECTED;
-}
-
-void AsyncMqttClient::connect() {
- if (_state != DISCONNECTED) return;
- log_i("CONNECTING");
- _state = CONNECTING;
- _disconnectReason = AsyncMqttClientDisconnectReason::TCP_DISCONNECTED; // reset any previous
-
- _client.setRxTimeout(_keepAlive);
-
-#if ASYNC_TCP_SSL_ENABLED
- if (_useIp) {
- _client.connect(_ip, _port, _secure);
- } else {
- _client.connect(_host, _port, _secure);
- }
-#else
- if (_useIp) {
- _client.connect(_ip, _port);
- } else if (_useIpv6) {
- _client.connect(_ipv6, _port);
- } else {
- _client.connect(_host, _port);
- }
-#endif
-}
-
-void AsyncMqttClient::disconnect(bool force) {
- if (_state == DISCONNECTED) return;
- log_i("DISCONNECT (f:%d)", force);
- if (force) {
- _state = DISCONNECTED;
- _client.close(true);
- } else if (_state != DISCONNECTING) {
- _state = DISCONNECTING;
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::DisconnOutPacket;
- _addBack(msg);
- }
-}
-
-uint16_t AsyncMqttClient::subscribe(const char* topic, uint8_t qos) {
- if (_state != CONNECTED) return 0;
- log_i("SUBSCRIBE");
-
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::SubscribeOutPacket(topic, qos);
- _addBack(msg);
- return msg->packetId();
-}
-
-uint16_t AsyncMqttClient::unsubscribe(const char* topic) {
- if (_state != CONNECTED) return 0;
- log_i("UNSUBSCRIBE");
-
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::UnsubscribeOutPacket(topic);
- _addBack(msg);
- return msg->packetId();
-}
-
-uint16_t AsyncMqttClient::publish(const char* topic, uint8_t qos, bool retain, const char* payload, size_t length, bool dup, uint16_t message_id) {
- if (_state != CONNECTED || GET_FREE_MEMORY() < MQTT_MIN_FREE_MEMORY) return 0;
- log_i("PUBLISH");
-
- AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::PublishOutPacket(topic, qos, retain, payload, length);
- _addBack(msg);
- return msg->packetId();
-}
-
-bool AsyncMqttClient::clearQueue() {
- if (_state != DISCONNECTED) return false;
- _clearQueue(false);
- return true;
-}
-
-const char* AsyncMqttClient::getClientId() const {
- return _clientId;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient.h b/lib/async-mqtt-client/src/AsyncMqttClient.h
deleted file mode 100644
index 23d30554d..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef SRC_ASYNCMQTTCLIENT_H_
-#define SRC_ASYNCMQTTCLIENT_H_
-
-#include "AsyncMqttClient.hpp"
-
-#endif // SRC_ASYNCMQTTCLIENT_H_
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient.hpp b/lib/async-mqtt-client/src/AsyncMqttClient.hpp
deleted file mode 100644
index fc2462601..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient.hpp
+++ /dev/null
@@ -1,182 +0,0 @@
-#pragma once
-
-#include
-#include
-
-#include "Arduino.h"
-
-#ifndef MQTT_MIN_FREE_MEMORY
-#define MQTT_MIN_FREE_MEMORY 4096
-#endif
-
-#ifdef ESP32
-#include
-#include
-#elif defined(ESP8266)
-#include
-#else
-#error Platform not supported
-#endif
-
-#if ASYNC_TCP_SSL_ENABLED
-#include
-#define SHA1_SIZE 20
-#endif
-
-#include "AsyncMqttClient/Flags.hpp"
-#include "AsyncMqttClient/ParsingInformation.hpp"
-#include "AsyncMqttClient/MessageProperties.hpp"
-#include "AsyncMqttClient/Helpers.hpp"
-#include "AsyncMqttClient/Callbacks.hpp"
-#include "AsyncMqttClient/DisconnectReasons.hpp"
-#include "AsyncMqttClient/Storage.hpp"
-
-#include "AsyncMqttClient/Packets/Packet.hpp"
-#include "AsyncMqttClient/Packets/ConnAckPacket.hpp"
-#include "AsyncMqttClient/Packets/PingRespPacket.hpp"
-#include "AsyncMqttClient/Packets/SubAckPacket.hpp"
-#include "AsyncMqttClient/Packets/UnsubAckPacket.hpp"
-#include "AsyncMqttClient/Packets/PublishPacket.hpp"
-#include "AsyncMqttClient/Packets/PubRelPacket.hpp"
-#include "AsyncMqttClient/Packets/PubAckPacket.hpp"
-#include "AsyncMqttClient/Packets/PubRecPacket.hpp"
-#include "AsyncMqttClient/Packets/PubCompPacket.hpp"
-
-#include "AsyncMqttClient/Packets/Out/Connect.hpp"
-#include "AsyncMqttClient/Packets/Out/PingReq.hpp"
-#include "AsyncMqttClient/Packets/Out/PubAck.hpp"
-#include "AsyncMqttClient/Packets/Out/Disconn.hpp"
-#include "AsyncMqttClient/Packets/Out/Subscribe.hpp"
-#include "AsyncMqttClient/Packets/Out/Unsubscribe.hpp"
-#include "AsyncMqttClient/Packets/Out/Publish.hpp"
-
-class AsyncMqttClient {
- public:
- AsyncMqttClient();
- ~AsyncMqttClient();
-
- AsyncMqttClient& setKeepAlive(uint16_t keepAlive);
- AsyncMqttClient& setClientId(const char* clientId);
- AsyncMqttClient& setCleanSession(bool cleanSession);
- AsyncMqttClient& setMaxTopicLength(uint16_t maxTopicLength);
- AsyncMqttClient& setCredentials(const char* username, const char* password = nullptr);
- AsyncMqttClient& setWill(const char* topic, uint8_t qos, bool retain, const char* payload = nullptr, size_t length = 0);
- AsyncMqttClient& setServer(IPAddress ip, uint16_t port);
- AsyncMqttClient& setServer(IPv6Address ipv6, uint16_t port);
- AsyncMqttClient& setServer(const char* host, uint16_t port);
-#if ASYNC_TCP_SSL_ENABLED
- AsyncMqttClient& setSecure(bool secure);
- AsyncMqttClient& addServerFingerprint(const uint8_t* fingerprint);
-#endif
-
- AsyncMqttClient& onConnect(AsyncMqttClientInternals::OnConnectUserCallback callback);
- AsyncMqttClient& onDisconnect(AsyncMqttClientInternals::OnDisconnectUserCallback callback);
- AsyncMqttClient& onSubscribe(AsyncMqttClientInternals::OnSubscribeUserCallback callback);
- AsyncMqttClient& onUnsubscribe(AsyncMqttClientInternals::OnUnsubscribeUserCallback callback);
- AsyncMqttClient& onMessage(AsyncMqttClientInternals::OnMessageUserCallback callback);
- AsyncMqttClient& onPublish(AsyncMqttClientInternals::OnPublishUserCallback callback);
-
- bool connected() const;
- void connect();
- void disconnect(bool force = false);
- uint16_t subscribe(const char* topic, uint8_t qos);
- uint16_t unsubscribe(const char* topic);
- uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload = nullptr, size_t length = 0, bool dup = false, uint16_t message_id = 0);
- bool clearQueue(); // Not MQTT compliant!
-
- const char* getClientId() const;
-
- private:
- AsyncClient _client;
- AsyncMqttClientInternals::OutPacket* _head;
- AsyncMqttClientInternals::OutPacket* _tail;
- size_t _sent;
- enum {
- CONNECTING,
- CONNECTED,
- DISCONNECTING,
- DISCONNECTED
- } _state;
- AsyncMqttClientDisconnectReason _disconnectReason;
- uint32_t _lastClientActivity;
- uint32_t _lastServerActivity;
- uint32_t _lastPingRequestTime;
-
- char _generatedClientId[18 + 1]; // esp8266-abc123 and esp32-abcdef123456
- IPAddress _ip;
- IPv6Address _ipv6;
- const char* _host;
- bool _useIp;
- bool _useIpv6;
-#if ASYNC_TCP_SSL_ENABLED
- bool _secure;
-#endif
- uint16_t _port;
- uint16_t _keepAlive;
- bool _cleanSession;
- const char* _clientId;
- const char* _username;
- const char* _password;
- const char* _willTopic;
- const char* _willPayload;
- uint16_t _willPayloadLength;
- uint8_t _willQos;
- bool _willRetain;
-
-#if ASYNC_TCP_SSL_ENABLED
- std::vector> _secureServerFingerprints;
-#endif
-
- std::vector _onConnectUserCallbacks;
- std::vector _onDisconnectUserCallbacks;
- std::vector _onSubscribeUserCallbacks;
- std::vector _onUnsubscribeUserCallbacks;
- std::vector _onMessageUserCallbacks;
- std::vector _onPublishUserCallbacks;
-
- AsyncMqttClientInternals::ParsingInformation _parsingInformation;
- AsyncMqttClientInternals::Packet* _currentParsedPacket;
- uint8_t _remainingLengthBufferPosition;
- char _remainingLengthBuffer[4];
-
- std::vector _pendingPubRels;
-
-#if defined(ESP32)
- SemaphoreHandle_t _xSemaphore = nullptr;
-#elif defined(ESP8266)
- bool _xSemaphore = false;
-#endif
-
- void _clear();
- void _freeCurrentParsedPacket();
-
- // TCP
- void _onConnect();
- void _onDisconnect();
- // void _onError(int8_t error);
- // void _onTimeout();
- void _onAck(size_t len);
- void _onData(char* data, size_t len);
- void _onPoll();
-
- // QUEUE
- void _insert(AsyncMqttClientInternals::OutPacket* packet); // for PUBREL
- void _addFront(AsyncMqttClientInternals::OutPacket* packet); // for CONNECT
- void _addBack(AsyncMqttClientInternals::OutPacket* packet); // all the rest
- void _handleQueue();
- void _clearQueue(bool keepSessionData);
-
- // MQTT
- void _onPingResp();
- void _onConnAck(bool sessionPresent, uint8_t connectReturnCode);
- void _onSubAck(uint16_t packetId, char status);
- void _onUnsubAck(uint16_t packetId);
- void _onMessage(char* topic, char* payload, uint8_t qos, bool dup, bool retain, size_t len, size_t index, size_t total, uint16_t packetId);
- void _onPublish(uint16_t packetId, uint8_t qos);
- void _onPubRel(uint16_t packetId);
- void _onPubAck(uint16_t packetId);
- void _onPubRec(uint16_t packetId);
- void _onPubComp(uint16_t packetId);
-
- void _sendPing();
-};
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Callbacks.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Callbacks.hpp
deleted file mode 100644
index 414034daa..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Callbacks.hpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#pragma once
-
-#include
-
-#include "DisconnectReasons.hpp"
-#include "MessageProperties.hpp"
-#include "Errors.hpp"
-
-namespace AsyncMqttClientInternals {
-// user callbacks
-typedef std::function OnConnectUserCallback;
-typedef std::function OnDisconnectUserCallback;
-typedef std::function OnSubscribeUserCallback;
-typedef std::function OnUnsubscribeUserCallback;
-typedef std::function OnMessageUserCallback;
-typedef std::function OnPublishUserCallback;
-typedef std::function OnErrorUserCallback;
-
-// internal callbacks
-typedef std::function OnConnAckInternalCallback;
-typedef std::function OnPingRespInternalCallback;
-typedef std::function OnSubAckInternalCallback;
-typedef std::function OnUnsubAckInternalCallback;
-typedef std::function OnMessageInternalCallback;
-typedef std::function OnPublishInternalCallback;
-typedef std::function OnPubRelInternalCallback;
-typedef std::function OnPubAckInternalCallback;
-typedef std::function OnPubRecInternalCallback;
-typedef std::function OnPubCompInternalCallback;
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/DisconnectReasons.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/DisconnectReasons.hpp
deleted file mode 100644
index a15114000..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/DisconnectReasons.hpp
+++ /dev/null
@@ -1,15 +0,0 @@
-#pragma once
-
-enum class AsyncMqttClientDisconnectReason : uint8_t {
- TCP_DISCONNECTED = 0,
-
- MQTT_UNACCEPTABLE_PROTOCOL_VERSION = 1,
- MQTT_IDENTIFIER_REJECTED = 2,
- MQTT_SERVER_UNAVAILABLE = 3,
- MQTT_MALFORMED_CREDENTIALS = 4,
- MQTT_NOT_AUTHORIZED = 5,
-
- ESP8266_NOT_ENOUGH_SPACE = 6,
-
- TLS_BAD_FINGERPRINT = 7
-};
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Errors.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Errors.hpp
deleted file mode 100644
index f93e80e39..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Errors.hpp
+++ /dev/null
@@ -1,6 +0,0 @@
-#pragma once
-
-enum class AsyncMqttClientError : uint8_t {
- MAX_RETRIES = 0,
- OUT_OF_MEMORY = 1
-};
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Helpers.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Helpers.hpp
deleted file mode 100644
index ecb620f74..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Helpers.hpp
+++ /dev/null
@@ -1,61 +0,0 @@
-#pragma once
-
-namespace AsyncMqttClientInternals {
-class Helpers {
- public:
- static uint32_t decodeRemainingLength(char* bytes) {
- uint32_t multiplier = 1;
- uint32_t value = 0;
- uint8_t currentByte = 0;
- uint8_t encodedByte;
- do {
- encodedByte = bytes[currentByte++];
- value += (encodedByte & 127) * multiplier;
- multiplier *= 128;
- } while ((encodedByte & 128) != 0);
-
- return value;
- }
-
- static uint8_t encodeRemainingLength(uint32_t remainingLength, char* 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;
- }
-};
-
-#if defined(ARDUINO_ARCH_ESP32)
- #define SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY)
- #define SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
- #define GET_FREE_MEMORY() ESP.getMaxAllocHeap()
- #include
-#elif defined(ARDUINO_ARCH_ESP8266)
- #define SEMAPHORE_TAKE(X) while (_xSemaphore) { /*ESP.wdtFeed();*/ } _xSemaphore = true
- #define SEMAPHORE_GIVE() _xSemaphore = false
- #define GET_FREE_MEMORY() ESP.getMaxFreeBlockSize()
- #if defined(DEBUG_ESP_PORT) && defined(DEBUG_ASYNC_MQTT_CLIENT)
- #define log_i(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n")
- #define log_e(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n")
- #define log_w(...) DEBUG_ESP_PORT.printf(__VA_ARGS__); DEBUG_ESP_PORT.print("\n")
- #else
- #define log_i(...)
- #define log_e(...)
- #define log_w(...)
- #endif
-#else
- #pragma error "No valid architecture"
-#endif
-
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/MessageProperties.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/MessageProperties.hpp
deleted file mode 100644
index c04b59664..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/MessageProperties.hpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-
-struct AsyncMqttClientMessageProperties {
- uint8_t qos;
- bool dup;
- bool retain;
-};
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/ConnAckPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/ConnAckPacket.cpp
deleted file mode 100644
index fb4b97195..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/ConnAckPacket.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "ConnAckPacket.hpp"
-
-using AsyncMqttClientInternals::ConnAckPacket;
-
-ConnAckPacket::ConnAckPacket(ParsingInformation* parsingInformation, OnConnAckInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback)
-, _bytePosition(0)
-, _sessionPresent(false)
-, _connectReturnCode(0) {
-}
-
-ConnAckPacket::~ConnAckPacket() {
-}
-
-void ConnAckPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition++ == 0) {
- _sessionPresent = (currentByte << 7) >> 7;
- } else {
- _connectReturnCode = currentByte;
- _parsingInformation->bufferState = BufferState::NONE;
- _callback(_sessionPresent, _connectReturnCode);
- }
-}
-
-void ConnAckPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/ConnAckPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/ConnAckPacket.hpp
deleted file mode 100644
index 3dcd162ee..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/ConnAckPacket.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class ConnAckPacket : public Packet {
- public:
- explicit ConnAckPacket(ParsingInformation* parsingInformation, OnConnAckInternalCallback callback);
- ~ConnAckPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnConnAckInternalCallback _callback;
-
- uint8_t _bytePosition;
- bool _sessionPresent;
- uint8_t _connectReturnCode;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Connect.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Connect.cpp
deleted file mode 100644
index a9a86e453..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Connect.cpp
+++ /dev/null
@@ -1,162 +0,0 @@
-#include "Connect.hpp"
-
-using AsyncMqttClientInternals::ConnectOutPacket;
-
-ConnectOutPacket::ConnectOutPacket(bool cleanSession,
- const char* username,
- const char* password,
- const char* willTopic,
- bool willRetain,
- uint8_t willQos,
- const char* willPayload,
- uint16_t willPayloadLength,
- uint16_t keepAlive,
- const char* clientId) {
- char fixedHeader[5];
- fixedHeader[0] = AsyncMqttClientInternals::PacketType.CONNECT;
- fixedHeader[0] = fixedHeader[0] << 4;
- fixedHeader[0] = fixedHeader[0] | AsyncMqttClientInternals::HeaderFlag.CONNECT_RESERVED;
-
- uint16_t protocolNameLength = 4;
- char protocolNameLengthBytes[2];
- protocolNameLengthBytes[0] = protocolNameLength >> 8;
- protocolNameLengthBytes[1] = protocolNameLength & 0xFF;
-
- char protocolLevel[1];
- protocolLevel[0] = 0x04;
-
- char connectFlags[1];
- connectFlags[0] = 0;
- if (cleanSession) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.CLEAN_SESSION;
- if (username != nullptr) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.USERNAME;
- if (password != nullptr) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.PASSWORD;
- if (willTopic != nullptr) {
- connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.WILL;
- if (willRetain) connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.WILL_RETAIN;
- switch (willQos) {
- case 0:
- connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.WILL_QOS0;
- break;
- case 1:
- connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.WILL_QOS1;
- break;
- case 2:
- connectFlags[0] |= AsyncMqttClientInternals::ConnectFlag.WILL_QOS2;
- break;
- }
- }
-
- char keepAliveBytes[2];
- keepAliveBytes[0] = keepAlive >> 8;
- keepAliveBytes[1] = keepAlive & 0xFF;
-
- uint16_t clientIdLength = strlen(clientId);
- char clientIdLengthBytes[2];
- clientIdLengthBytes[0] = clientIdLength >> 8;
- clientIdLengthBytes[1] = clientIdLength & 0xFF;
-
- // Optional fields
- uint16_t willTopicLength = 0;
- char willTopicLengthBytes[2];
- char willPayloadLengthBytes[2];
- if (willTopic != nullptr) {
- willTopicLength = strlen(willTopic);
- willTopicLengthBytes[0] = willTopicLength >> 8;
- willTopicLengthBytes[1] = willTopicLength & 0xFF;
-
- if (willPayload != nullptr && willPayloadLength == 0) willPayloadLength = strlen(willPayload);
-
- willPayloadLengthBytes[0] = willPayloadLength >> 8;
- willPayloadLengthBytes[1] = willPayloadLength & 0xFF;
- }
-
- uint16_t usernameLength = 0;
- char usernameLengthBytes[2];
- if (username != nullptr) {
- usernameLength = strlen(username);
- usernameLengthBytes[0] = usernameLength >> 8;
- usernameLengthBytes[1] = usernameLength & 0xFF;
- }
-
- uint16_t passwordLength = 0;
- char passwordLengthBytes[2];
- if (password != nullptr) {
- passwordLength = strlen(password);
- passwordLengthBytes[0] = passwordLength >> 8;
- passwordLengthBytes[1] = passwordLength & 0xFF;
- }
-
- uint32_t remainingLength = 2 + protocolNameLength + 1 + 1 + 2 + 2 + clientIdLength; // always present
- if (willTopic != nullptr) remainingLength += 2 + willTopicLength + 2 + willPayloadLength;
- if (username != nullptr) remainingLength += 2 + usernameLength;
- if (password != nullptr) remainingLength += 2 + passwordLength;
- uint8_t remainingLengthLength = AsyncMqttClientInternals::Helpers::encodeRemainingLength(remainingLength, fixedHeader + 1);
-
- uint32_t neededSpace = 1 + remainingLengthLength;
- neededSpace += 2;
- neededSpace += protocolNameLength;
- neededSpace += 1;
- neededSpace += 1;
- neededSpace += 2;
- neededSpace += 2;
- neededSpace += clientIdLength;
- if (willTopic != nullptr) {
- neededSpace += 2;
- neededSpace += willTopicLength;
-
- neededSpace += 2;
- if (willPayload != nullptr) neededSpace += willPayloadLength;
- }
- if (username != nullptr) {
- neededSpace += 2;
- neededSpace += usernameLength;
- }
- if (password != nullptr) {
- neededSpace += 2;
- neededSpace += passwordLength;
- }
-
- _data.reserve(neededSpace);
-
- _data.insert(_data.end(), fixedHeader, fixedHeader + 1 + remainingLengthLength);
-
- _data.push_back(protocolNameLengthBytes[0]);
- _data.push_back(protocolNameLengthBytes[1]);
-
- _data.push_back('M');
- _data.push_back('Q');
- _data.push_back('T');
- _data.push_back('T');
-
- _data.push_back(protocolLevel[0]);
- _data.push_back(connectFlags[0]);
- _data.push_back(keepAliveBytes[0]);
- _data.push_back(keepAliveBytes[1]);
- _data.push_back(clientIdLengthBytes[0]);
- _data.push_back(clientIdLengthBytes[1]);
-
- _data.insert(_data.end(), clientId, clientId + clientIdLength);
- if (willTopic != nullptr) {
- _data.insert(_data.end(), willTopicLengthBytes, willTopicLengthBytes + 2);
- _data.insert(_data.end(), willTopic, willTopic + willTopicLength);
-
- _data.insert(_data.end(), willPayloadLengthBytes, willPayloadLengthBytes + 2);
- if (willPayload != nullptr) _data.insert(_data.end(), willPayload, willPayload + willPayloadLength);
- }
- if (username != nullptr) {
- _data.insert(_data.end(), usernameLengthBytes, usernameLengthBytes + 2);
- _data.insert(_data.end(), username, username + usernameLength);
- }
- if (password != nullptr) {
- _data.insert(_data.end(), passwordLengthBytes, passwordLengthBytes + 2);
- _data.insert(_data.end(), password, password + passwordLength);
- }
-}
-
-const uint8_t* ConnectOutPacket::data(size_t index) const {
- return &_data.data()[index];
-}
-
-size_t ConnectOutPacket::size() const {
- return _data.size();
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Connect.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Connect.hpp
deleted file mode 100644
index 5b17632fd..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Connect.hpp
+++ /dev/null
@@ -1,29 +0,0 @@
-#pragma once
-
-#include
-#include // strlen
-
-#include "OutPacket.hpp"
-#include "../../Flags.hpp"
-#include "../../Helpers.hpp"
-
-namespace AsyncMqttClientInternals {
-class ConnectOutPacket : public OutPacket {
- public:
- ConnectOutPacket(bool cleanSession,
- const char* username,
- const char* password,
- const char* willTopic,
- bool willRetain,
- uint8_t willQos,
- const char* willPayload,
- uint16_t willPayloadLength,
- uint16_t keepAlive,
- const char* clientId);
- const uint8_t* data(size_t index = 0) const;
- size_t size() const;
-
- private:
- std::vector _data;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Disconn.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Disconn.cpp
deleted file mode 100644
index 3e2890df1..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Disconn.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-#include "Disconn.hpp"
-
-using AsyncMqttClientInternals::DisconnOutPacket;
-
-DisconnOutPacket::DisconnOutPacket() {
- _data[0] = AsyncMqttClientInternals::PacketType.DISCONNECT;
- _data[0] = _data[0] << 4;
- _data[0] = _data[0] | AsyncMqttClientInternals::HeaderFlag.DISCONNECT_RESERVED;
- _data[1] = 0;
-}
-
-const uint8_t* DisconnOutPacket::data(size_t index) const {
- return &_data[index];
-}
-
-size_t DisconnOutPacket::size() const {
- return 2;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Disconn.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Disconn.hpp
deleted file mode 100644
index 38dc9152f..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Disconn.hpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-#include "OutPacket.hpp"
-#include "../../Flags.hpp"
-#include "../../Helpers.hpp"
-
-namespace AsyncMqttClientInternals {
-class DisconnOutPacket : public OutPacket {
- public:
- DisconnOutPacket();
- const uint8_t* data(size_t index = 0) const;
- size_t size() const;
-
- private:
- uint8_t _data[2];
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/OutPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/OutPacket.cpp
deleted file mode 100644
index e69a87f8f..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/OutPacket.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-#include "OutPacket.hpp"
-
-using AsyncMqttClientInternals::OutPacket;
-
-OutPacket::OutPacket()
-: next(nullptr)
-, timeout(0)
-, noTries(0)
-, _released(true)
-, _packetId(0) {}
-
-OutPacket::~OutPacket() {}
-
-bool OutPacket::released() const {
- return _released;
-}
-
-uint8_t OutPacket::packetType() const {
- return data(0)[0] >> 4;
-}
-
-uint16_t OutPacket::packetId() const {
- return _packetId;
-}
-
-uint8_t OutPacket::qos() const {
- if (packetType() == AsyncMqttClientInternals::PacketType.PUBLISH) {
- return (data()[1] & 0x06) >> 1;
- }
- return 0;
-}
-
-void OutPacket::release() {
- _released = true;
-}
-
-uint16_t OutPacket::_nextPacketId = 0;
-
-uint16_t OutPacket::_getNextPacketId() {
- if (++_nextPacketId == 0) {
- ++_nextPacketId;
- }
- return _nextPacketId;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/OutPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/OutPacket.hpp
deleted file mode 100644
index 52c37de0b..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/OutPacket.hpp
+++ /dev/null
@@ -1,35 +0,0 @@
-#pragma once
-
-#include // uint*_t
-#include // size_t
-#include // std::min
-
-#include "../../Flags.hpp"
-
-namespace AsyncMqttClientInternals {
-class OutPacket {
- public:
- OutPacket();
- virtual ~OutPacket();
- virtual const uint8_t* data(size_t index = 0) const = 0;
- virtual size_t size() const = 0;
- bool released() const;
- uint8_t packetType() const;
- uint16_t packetId() const;
- uint8_t qos() const;
- void release();
-
- public:
- OutPacket* next;
- uint32_t timeout;
- uint8_t noTries;
-
- protected:
- static uint16_t _getNextPacketId();
- bool _released;
- uint16_t _packetId;
-
- private:
- static uint16_t _nextPacketId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PingReq.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PingReq.cpp
deleted file mode 100644
index d59cf3dc1..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PingReq.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-#include "PingReq.hpp"
-
-using AsyncMqttClientInternals::PingReqOutPacket;
-
-PingReqOutPacket::PingReqOutPacket() {
- _data[0] = AsyncMqttClientInternals::PacketType.PINGREQ;
- _data[0] = _data[0] << 4;
- _data[0] = _data[0] | AsyncMqttClientInternals::HeaderFlag.PINGREQ_RESERVED;
- _data[1] = 0;
-}
-
-const uint8_t* PingReqOutPacket::data(size_t index) const {
- return &_data[index];;
-}
-
-size_t PingReqOutPacket::size() const {
- return 2;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PingReq.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PingReq.hpp
deleted file mode 100644
index 1cb19a39e..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PingReq.hpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-#include "OutPacket.hpp"
-#include "../../Flags.hpp"
-#include "../../Helpers.hpp"
-
-namespace AsyncMqttClientInternals {
-class PingReqOutPacket : public OutPacket {
- public:
- PingReqOutPacket();
- const uint8_t* data(size_t index = 0) const;
- size_t size() const;
-
- private:
- uint8_t _data[2];
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PubAck.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PubAck.cpp
deleted file mode 100644
index 634607ba5..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PubAck.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#include "PubAck.hpp"
-
-using AsyncMqttClientInternals::PubAckOutPacket;
-
-PubAckOutPacket::PubAckOutPacket(PendingAck pendingAck) {
- _data[0] = pendingAck.packetType;
- _data[0] = _data[0] << 4;
- _data[0] = _data[0] | pendingAck.headerFlag;
- _data[1] = 2;
- _packetId = pendingAck.packetId;
- _data[2] = pendingAck.packetId >> 8;
- _data[3] = pendingAck.packetId & 0xFF;
- if (packetType() == AsyncMqttClientInternals::PacketType.PUBREL ||
- packetType() == AsyncMqttClientInternals::PacketType.PUBREC) {
- _released = false;
- }
-}
-
-const uint8_t* PubAckOutPacket::data(size_t index) const {
- return &_data[index];
-}
-
-size_t PubAckOutPacket::size() const {
- return 4;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PubAck.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PubAck.hpp
deleted file mode 100644
index 9cd830ee4..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/PubAck.hpp
+++ /dev/null
@@ -1,18 +0,0 @@
-#pragma once
-
-#include "OutPacket.hpp"
-#include "../../Flags.hpp"
-#include "../../Helpers.hpp"
-#include "../../Storage.hpp"
-
-namespace AsyncMqttClientInternals {
-class PubAckOutPacket : public OutPacket {
- public:
- explicit PubAckOutPacket(PendingAck pendingAck);
- const uint8_t* data(size_t index = 0) const;
- size_t size() const;
-
- private:
- uint8_t _data[4];
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Publish.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Publish.cpp
deleted file mode 100644
index 3f4365b2d..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Publish.cpp
+++ /dev/null
@@ -1,69 +0,0 @@
-#include "Publish.hpp"
-
-using AsyncMqttClientInternals::PublishOutPacket;
-
-PublishOutPacket::PublishOutPacket(const char* topic, uint8_t qos, bool retain, const char* payload, size_t length) {
- char fixedHeader[5];
- fixedHeader[0] = AsyncMqttClientInternals::PacketType.PUBLISH;
- fixedHeader[0] = fixedHeader[0] << 4;
- // if (dup) fixedHeader[0] |= AsyncMqttClientInternals::HeaderFlag.PUBLISH_DUP;
- if (retain) fixedHeader[0] |= AsyncMqttClientInternals::HeaderFlag.PUBLISH_RETAIN;
- switch (qos) {
- case 0:
- fixedHeader[0] |= AsyncMqttClientInternals::HeaderFlag.PUBLISH_QOS0;
- break;
- case 1:
- fixedHeader[0] |= AsyncMqttClientInternals::HeaderFlag.PUBLISH_QOS1;
- break;
- case 2:
- fixedHeader[0] |= AsyncMqttClientInternals::HeaderFlag.PUBLISH_QOS2;
- break;
- }
-
- uint16_t topicLength = strlen(topic);
- char topicLengthBytes[2];
- topicLengthBytes[0] = topicLength >> 8;
- topicLengthBytes[1] = topicLength & 0xFF;
-
- uint32_t payloadLength = length;
- if (payload != nullptr && payloadLength == 0) payloadLength = strlen(payload);
-
- uint32_t remainingLength = 2 + topicLength + payloadLength;
- if (qos != 0) remainingLength += 2;
- uint8_t remainingLengthLength = AsyncMqttClientInternals::Helpers::encodeRemainingLength(remainingLength, fixedHeader + 1);
-
- size_t neededSpace = 0;
- neededSpace += 1 + remainingLengthLength;
- neededSpace += 2;
- neededSpace += topicLength;
- if (qos != 0) neededSpace += 2;
- if (payload != nullptr) neededSpace += payloadLength;
-
- _data.reserve(neededSpace);
-
- _packetId = (qos !=0) ? _getNextPacketId() : 1;
- char packetIdBytes[2];
- packetIdBytes[0] = _packetId >> 8;
- packetIdBytes[1] = _packetId & 0xFF;
-
- _data.insert(_data.end(), fixedHeader, fixedHeader + 1 + remainingLengthLength);
- _data.insert(_data.end(), topicLengthBytes, topicLengthBytes + 2);
- _data.insert(_data.end(), topic, topic + topicLength);
- if (qos != 0) {
- _data.insert(_data.end(), packetIdBytes, packetIdBytes + 2);
- _released = false;
- }
- if (payload != nullptr) _data.insert(_data.end(), payload, payload + payloadLength);
-}
-
-const uint8_t* PublishOutPacket::data(size_t index) const {
- return &_data.data()[index];
-}
-
-size_t PublishOutPacket::size() const {
- return _data.size();
-}
-
-void PublishOutPacket::setDup() {
- _data[0] |= AsyncMqttClientInternals::HeaderFlag.PUBLISH_DUP;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Publish.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Publish.hpp
deleted file mode 100644
index 6b8272ec9..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Publish.hpp
+++ /dev/null
@@ -1,23 +0,0 @@
-#pragma once
-
-#include // strlen
-#include
-
-#include "OutPacket.hpp"
-#include "../../Flags.hpp"
-#include "../../Helpers.hpp"
-#include "../../Storage.hpp"
-
-namespace AsyncMqttClientInternals {
-class PublishOutPacket : public OutPacket {
- public:
- PublishOutPacket(const char* topic, uint8_t qos, bool retain, const char* payload, size_t length);
- const uint8_t* data(size_t index = 0) const;
- size_t size() const;
-
- void setDup(); // you cannot unset dup
-
- private:
- std::vector _data;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Subscribe.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Subscribe.cpp
deleted file mode 100644
index 85c10db4b..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Subscribe.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-#include "Subscribe.hpp"
-
-using AsyncMqttClientInternals::SubscribeOutPacket;
-
-SubscribeOutPacket::SubscribeOutPacket(const char* topic, uint8_t qos) {
- char fixedHeader[5];
- fixedHeader[0] = AsyncMqttClientInternals::PacketType.SUBSCRIBE;
- fixedHeader[0] = fixedHeader[0] << 4;
- fixedHeader[0] = fixedHeader[0] | AsyncMqttClientInternals::HeaderFlag.SUBSCRIBE_RESERVED;
-
- uint16_t topicLength = strlen(topic);
- char topicLengthBytes[2];
- topicLengthBytes[0] = topicLength >> 8;
- topicLengthBytes[1] = topicLength & 0xFF;
-
- char qosByte[1];
- qosByte[0] = qos;
-
- uint8_t remainingLengthLength = AsyncMqttClientInternals::Helpers::encodeRemainingLength(2 + 2 + topicLength + 1, fixedHeader + 1);
-
- size_t neededSpace = 0;
- neededSpace += 1 + remainingLengthLength;
- neededSpace += 2;
- neededSpace += 2;
- neededSpace += topicLength;
- neededSpace += 1;
-
- _data.reserve(neededSpace);
-
- _packetId = _getNextPacketId();
- char packetIdBytes[2];
- packetIdBytes[0] = _packetId >> 8;
- packetIdBytes[1] = _packetId & 0xFF;
-
- _data.insert(_data.end(), fixedHeader, fixedHeader + 1 + remainingLengthLength);
- _data.insert(_data.end(), packetIdBytes, packetIdBytes + 2);
- _data.insert(_data.end(), topicLengthBytes, topicLengthBytes + 2);
- _data.insert(_data.end(), topic, topic + topicLength);
- _data.push_back(qosByte[0]);
- _released = false;
-}
-
-const uint8_t* SubscribeOutPacket::data(size_t index) const {
- return &_data.data()[index];
-}
-
-size_t SubscribeOutPacket::size() const {
- return _data.size();
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Subscribe.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Subscribe.hpp
deleted file mode 100644
index 1f85f5913..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Subscribe.hpp
+++ /dev/null
@@ -1,21 +0,0 @@
-#pragma once
-
-#include // strlen
-#include
-
-#include "OutPacket.hpp"
-#include "../../Flags.hpp"
-#include "../../Helpers.hpp"
-#include "../../Storage.hpp"
-
-namespace AsyncMqttClientInternals {
-class SubscribeOutPacket : public OutPacket {
- public:
- SubscribeOutPacket(const char* topic, uint8_t qos);
- const uint8_t* data(size_t index = 0) const;
- size_t size() const;
-
- private:
- std::vector _data;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Unsubscribe.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Unsubscribe.cpp
deleted file mode 100644
index 4d859c9a4..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Unsubscribe.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-#include "Unsubscribe.hpp"
-
-using AsyncMqttClientInternals::UnsubscribeOutPacket;
-
-UnsubscribeOutPacket::UnsubscribeOutPacket(const char* topic) {
- char fixedHeader[5];
- fixedHeader[0] = AsyncMqttClientInternals::PacketType.UNSUBSCRIBE;
- fixedHeader[0] = fixedHeader[0] << 4;
- fixedHeader[0] = fixedHeader[0] | AsyncMqttClientInternals::HeaderFlag.UNSUBSCRIBE_RESERVED;
-
- uint16_t topicLength = strlen(topic);
- char topicLengthBytes[2];
- topicLengthBytes[0] = topicLength >> 8;
- topicLengthBytes[1] = topicLength & 0xFF;
-
- uint8_t remainingLengthLength = AsyncMqttClientInternals::Helpers::encodeRemainingLength(2 + 2 + topicLength, fixedHeader + 1);
-
- size_t neededSpace = 0;
- neededSpace += 1 + remainingLengthLength;
- neededSpace += 2;
- neededSpace += 2;
- neededSpace += topicLength;
-
- _packetId = _getNextPacketId();
- char packetIdBytes[2];
- packetIdBytes[0] = _packetId >> 8;
- packetIdBytes[1] = _packetId & 0xFF;
-
- _data.insert(_data.end(), fixedHeader, fixedHeader + 1 + remainingLengthLength);
- _data.insert(_data.end(), packetIdBytes, packetIdBytes + 2);
- _data.insert(_data.end(), topicLengthBytes, topicLengthBytes + 2);
- _data.insert(_data.end(), topic, topic + topicLength);
- _released = false;
-}
-
-const uint8_t* UnsubscribeOutPacket::data(size_t index) const {
- return &_data.data()[index];
-}
-
-size_t UnsubscribeOutPacket::size() const {
- return _data.size();
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Unsubscribe.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Unsubscribe.hpp
deleted file mode 100644
index 621802f6f..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Out/Unsubscribe.hpp
+++ /dev/null
@@ -1,21 +0,0 @@
-#pragma once
-
-#include // strlen
-#include
-
-#include "OutPacket.hpp"
-#include "../../Flags.hpp"
-#include "../../Helpers.hpp"
-#include "../../Storage.hpp"
-
-namespace AsyncMqttClientInternals {
-class UnsubscribeOutPacket : public OutPacket {
- public:
- explicit UnsubscribeOutPacket(const char* topic);
- const uint8_t* data(size_t index = 0) const;
- size_t size() const;
-
- private:
- std::vector _data;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Packet.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Packet.hpp
deleted file mode 100644
index e8dfdfba4..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/Packet.hpp
+++ /dev/null
@@ -1,11 +0,0 @@
-#pragma once
-
-namespace AsyncMqttClientInternals {
-class Packet {
- public:
- virtual ~Packet() {}
-
- virtual void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) = 0;
- virtual void parsePayload(char* data, size_t len, size_t* currentBytePosition) = 0;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PingRespPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PingRespPacket.cpp
deleted file mode 100644
index 4aa4db892..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PingRespPacket.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
-#include "PingRespPacket.hpp"
-
-using AsyncMqttClientInternals::PingRespPacket;
-
-PingRespPacket::PingRespPacket(ParsingInformation* parsingInformation, OnPingRespInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback) {
-}
-
-PingRespPacket::~PingRespPacket() {
-}
-
-void PingRespPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
-
-void PingRespPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PingRespPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PingRespPacket.hpp
deleted file mode 100644
index 098049adb..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PingRespPacket.hpp
+++ /dev/null
@@ -1,21 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class PingRespPacket : public Packet {
- public:
- explicit PingRespPacket(ParsingInformation* parsingInformation, OnPingRespInternalCallback callback);
- ~PingRespPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnPingRespInternalCallback _callback;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubAckPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubAckPacket.cpp
deleted file mode 100644
index 66e303b9d..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubAckPacket.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "PubAckPacket.hpp"
-
-using AsyncMqttClientInternals::PubAckPacket;
-
-PubAckPacket::PubAckPacket(ParsingInformation* parsingInformation, OnPubAckInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback)
-, _bytePosition(0)
-, _packetIdMsb(0)
-, _packetId(0) {
-}
-
-PubAckPacket::~PubAckPacket() {
-}
-
-void PubAckPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition++ == 0) {
- _packetIdMsb = currentByte;
- } else {
- _packetId = currentByte | _packetIdMsb << 8;
- _parsingInformation->bufferState = BufferState::NONE;
- _callback(_packetId);
- }
-}
-
-void PubAckPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubAckPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubAckPacket.hpp
deleted file mode 100644
index ea8a526a7..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubAckPacket.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class PubAckPacket : public Packet {
- public:
- explicit PubAckPacket(ParsingInformation* parsingInformation, OnPubAckInternalCallback callback);
- ~PubAckPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnPubAckInternalCallback _callback;
-
- uint8_t _bytePosition;
- char _packetIdMsb;
- uint16_t _packetId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubCompPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubCompPacket.cpp
deleted file mode 100644
index b845d2367..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubCompPacket.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "PubCompPacket.hpp"
-
-using AsyncMqttClientInternals::PubCompPacket;
-
-PubCompPacket::PubCompPacket(ParsingInformation* parsingInformation, OnPubCompInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback)
-, _bytePosition(0)
-, _packetIdMsb(0)
-, _packetId(0) {
-}
-
-PubCompPacket::~PubCompPacket() {
-}
-
-void PubCompPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition++ == 0) {
- _packetIdMsb = currentByte;
- } else {
- _packetId = currentByte | _packetIdMsb << 8;
- _parsingInformation->bufferState = BufferState::NONE;
- _callback(_packetId);
- }
-}
-
-void PubCompPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubCompPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubCompPacket.hpp
deleted file mode 100644
index b6aaaa96e..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubCompPacket.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class PubCompPacket : public Packet {
- public:
- explicit PubCompPacket(ParsingInformation* parsingInformation, OnPubCompInternalCallback callback);
- ~PubCompPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnPubCompInternalCallback _callback;
-
- uint8_t _bytePosition;
- char _packetIdMsb;
- uint16_t _packetId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRecPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRecPacket.cpp
deleted file mode 100644
index 8e19135b6..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRecPacket.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "PubRecPacket.hpp"
-
-using AsyncMqttClientInternals::PubRecPacket;
-
-PubRecPacket::PubRecPacket(ParsingInformation* parsingInformation, OnPubRecInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback)
-, _bytePosition(0)
-, _packetIdMsb(0)
-, _packetId(0) {
-}
-
-PubRecPacket::~PubRecPacket() {
-}
-
-void PubRecPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition++ == 0) {
- _packetIdMsb = currentByte;
- } else {
- _packetId = currentByte | _packetIdMsb << 8;
- _parsingInformation->bufferState = BufferState::NONE;
- _callback(_packetId);
- }
-}
-
-void PubRecPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRecPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRecPacket.hpp
deleted file mode 100644
index 09e2d77af..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRecPacket.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class PubRecPacket : public Packet {
- public:
- explicit PubRecPacket(ParsingInformation* parsingInformation, OnPubRecInternalCallback callback);
- ~PubRecPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnPubRecInternalCallback _callback;
-
- uint8_t _bytePosition;
- char _packetIdMsb;
- uint16_t _packetId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRelPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRelPacket.cpp
deleted file mode 100644
index 8a1f2d468..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRelPacket.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "PubRelPacket.hpp"
-
-using AsyncMqttClientInternals::PubRelPacket;
-
-PubRelPacket::PubRelPacket(ParsingInformation* parsingInformation, OnPubRelInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback)
-, _bytePosition(0)
-, _packetIdMsb(0)
-, _packetId(0) {
-}
-
-PubRelPacket::~PubRelPacket() {
-}
-
-void PubRelPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition++ == 0) {
- _packetIdMsb = currentByte;
- } else {
- _packetId = currentByte | _packetIdMsb << 8;
- _parsingInformation->bufferState = BufferState::NONE;
- _callback(_packetId);
- }
-}
-
-void PubRelPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRelPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRelPacket.hpp
deleted file mode 100644
index 96e9af720..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PubRelPacket.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class PubRelPacket : public Packet {
- public:
- explicit PubRelPacket(ParsingInformation* parsingInformation, OnPubRelInternalCallback callback);
- ~PubRelPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnPubRelInternalCallback _callback;
-
- uint8_t _bytePosition;
- char _packetIdMsb;
- uint16_t _packetId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PublishPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PublishPacket.cpp
deleted file mode 100644
index 2c5192f0c..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PublishPacket.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-#include "PublishPacket.hpp"
-
-using AsyncMqttClientInternals::PublishPacket;
-
-PublishPacket::PublishPacket(ParsingInformation* parsingInformation, OnMessageInternalCallback dataCallback, OnPublishInternalCallback completeCallback)
-: _parsingInformation(parsingInformation)
-, _dataCallback(dataCallback)
-, _completeCallback(completeCallback)
-, _dup(false)
-, _qos(0)
-, _retain(0)
-, _bytePosition(0)
-, _topicLengthMsb(0)
-, _topicLength(0)
-, _ignore(false)
-, _packetIdMsb(0)
-, _packetId(0)
-, _payloadLength(0)
-, _payloadBytesRead(0) {
- _dup = _parsingInformation->packetFlags & HeaderFlag.PUBLISH_DUP;
- _retain = _parsingInformation->packetFlags & HeaderFlag.PUBLISH_RETAIN;
- char qosMasked = _parsingInformation->packetFlags & 0x06;
- switch (qosMasked) {
- case HeaderFlag.PUBLISH_QOS0:
- _qos = 0;
- break;
- case HeaderFlag.PUBLISH_QOS1:
- _qos = 1;
- break;
- case HeaderFlag.PUBLISH_QOS2:
- _qos = 2;
- break;
- }
-}
-
-PublishPacket::~PublishPacket() {
-}
-
-void PublishPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition == 0) {
- _topicLengthMsb = currentByte;
- } else if (_bytePosition == 1) {
- _topicLength = currentByte | _topicLengthMsb << 8;
- if (_topicLength > _parsingInformation->maxTopicLength) {
- _ignore = true;
- } else {
- _parsingInformation->topicBuffer[_topicLength] = '\0';
- }
- } else if (_bytePosition >= 2 && _bytePosition < 2 + _topicLength) {
- // Starting from here, _ignore might be true
- if (!_ignore) _parsingInformation->topicBuffer[_bytePosition - 2] = currentByte;
- if (_bytePosition == 2 + _topicLength - 1 && _qos == 0) {
- _preparePayloadHandling(_parsingInformation->remainingLength - (_bytePosition + 1));
- return;
- }
- } else if (_bytePosition == 2 + _topicLength) {
- _packetIdMsb = currentByte;
- } else {
- _packetId = currentByte | _packetIdMsb << 8;
- _preparePayloadHandling(_parsingInformation->remainingLength - (_bytePosition + 1));
- }
- _bytePosition++;
-}
-
-void PublishPacket::_preparePayloadHandling(uint32_t payloadLength) {
- _payloadLength = payloadLength;
- if (payloadLength == 0) {
- _parsingInformation->bufferState = BufferState::NONE;
- if (!_ignore) {
- _dataCallback(_parsingInformation->topicBuffer, nullptr, _qos, _dup, _retain, 0, 0, 0, _packetId);
- _completeCallback(_packetId, _qos);
- }
- } else {
- _parsingInformation->bufferState = BufferState::PAYLOAD;
- }
-}
-
-void PublishPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- size_t remainToRead = len - (*currentBytePosition);
- if (_payloadBytesRead + remainToRead > _payloadLength) remainToRead = _payloadLength - _payloadBytesRead;
-
- if (!_ignore) _dataCallback(_parsingInformation->topicBuffer, data + (*currentBytePosition), _qos, _dup, _retain, remainToRead, _payloadBytesRead, _payloadLength, _packetId);
- _payloadBytesRead += remainToRead;
- (*currentBytePosition) += remainToRead;
-
- if (_payloadBytesRead == _payloadLength) {
- _parsingInformation->bufferState = BufferState::NONE;
- if (!_ignore) _completeCallback(_packetId, _qos);
- }
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PublishPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PublishPacket.hpp
deleted file mode 100644
index d97205ce1..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/PublishPacket.hpp
+++ /dev/null
@@ -1,38 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../Flags.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class PublishPacket : public Packet {
- public:
- explicit PublishPacket(ParsingInformation* parsingInformation, OnMessageInternalCallback dataCallback, OnPublishInternalCallback completeCallback);
- ~PublishPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnMessageInternalCallback _dataCallback;
- OnPublishInternalCallback _completeCallback;
-
- void _preparePayloadHandling(uint32_t payloadLength);
-
- bool _dup;
- uint8_t _qos;
- bool _retain;
-
- uint8_t _bytePosition;
- char _topicLengthMsb;
- uint16_t _topicLength;
- bool _ignore;
- char _packetIdMsb;
- uint16_t _packetId;
- uint32_t _payloadLength;
- uint32_t _payloadBytesRead;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/SubAckPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/SubAckPacket.cpp
deleted file mode 100644
index 724f05fab..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/SubAckPacket.cpp
+++ /dev/null
@@ -1,46 +0,0 @@
-#include "SubAckPacket.hpp"
-
-using AsyncMqttClientInternals::SubAckPacket;
-
-SubAckPacket::SubAckPacket(ParsingInformation* parsingInformation, OnSubAckInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback)
-, _bytePosition(0)
-, _packetIdMsb(0)
-, _packetId(0) {
-}
-
-SubAckPacket::~SubAckPacket() {
-}
-
-void SubAckPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition++ == 0) {
- _packetIdMsb = currentByte;
- } else {
- _packetId = currentByte | _packetIdMsb << 8;
- _parsingInformation->bufferState = BufferState::PAYLOAD;
- }
-}
-
-void SubAckPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- char status = data[(*currentBytePosition)++];
-
- /* switch (status) {
- case 0:
- Serial.println("Success QoS 0");
- break;
- case 1:
- Serial.println("Success QoS 1");
- break;
- case 2:
- Serial.println("Success QoS 2");
- break;
- case 0x80:
- Serial.println("Failure");
- break;
- } */
-
- _parsingInformation->bufferState = BufferState::NONE;
- _callback(_packetId, status);
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/SubAckPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/SubAckPacket.hpp
deleted file mode 100644
index 26077c23d..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/SubAckPacket.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class SubAckPacket : public Packet {
- public:
- explicit SubAckPacket(ParsingInformation* parsingInformation, OnSubAckInternalCallback callback);
- ~SubAckPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnSubAckInternalCallback _callback;
-
- uint8_t _bytePosition;
- char _packetIdMsb;
- uint16_t _packetId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/UnsubAckPacket.cpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/UnsubAckPacket.cpp
deleted file mode 100644
index 3c0ed7424..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/UnsubAckPacket.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "UnsubAckPacket.hpp"
-
-using AsyncMqttClientInternals::UnsubAckPacket;
-
-UnsubAckPacket::UnsubAckPacket(ParsingInformation* parsingInformation, OnUnsubAckInternalCallback callback)
-: _parsingInformation(parsingInformation)
-, _callback(callback)
-, _bytePosition(0)
-, _packetIdMsb(0)
-, _packetId(0) {
-}
-
-UnsubAckPacket::~UnsubAckPacket() {
-}
-
-void UnsubAckPacket::parseVariableHeader(char* data, size_t len, size_t* currentBytePosition) {
- char currentByte = data[(*currentBytePosition)++];
- if (_bytePosition++ == 0) {
- _packetIdMsb = currentByte;
- } else {
- _packetId = currentByte | _packetIdMsb << 8;
- _parsingInformation->bufferState = BufferState::NONE;
- _callback(_packetId);
- }
-}
-
-void UnsubAckPacket::parsePayload(char* data, size_t len, size_t* currentBytePosition) {
- (void)data;
- (void)currentBytePosition;
-}
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/UnsubAckPacket.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Packets/UnsubAckPacket.hpp
deleted file mode 100644
index 9c17c2d02..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Packets/UnsubAckPacket.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include "Arduino.h"
-#include "Packet.hpp"
-#include "../ParsingInformation.hpp"
-#include "../Callbacks.hpp"
-
-namespace AsyncMqttClientInternals {
-class UnsubAckPacket : public Packet {
- public:
- explicit UnsubAckPacket(ParsingInformation* parsingInformation, OnUnsubAckInternalCallback callback);
- ~UnsubAckPacket();
-
- void parseVariableHeader(char* data, size_t len, size_t* currentBytePosition);
- void parsePayload(char* data, size_t len, size_t* currentBytePosition);
-
- private:
- ParsingInformation* _parsingInformation;
- OnUnsubAckInternalCallback _callback;
-
- uint8_t _bytePosition;
- char _packetIdMsb;
- uint16_t _packetId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/ParsingInformation.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/ParsingInformation.hpp
deleted file mode 100644
index a14640cb3..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/ParsingInformation.hpp
+++ /dev/null
@@ -1,21 +0,0 @@
-#pragma once
-
-namespace AsyncMqttClientInternals {
-enum class BufferState : uint8_t {
- NONE = 0,
- REMAINING_LENGTH = 2,
- VARIABLE_HEADER = 3,
- PAYLOAD = 4
-};
-
-struct ParsingInformation {
- BufferState bufferState;
-
- uint16_t maxTopicLength;
- char* topicBuffer;
-
- uint8_t packetType;
- uint16_t packetFlags;
- uint32_t remainingLength;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Storage.hpp b/lib/async-mqtt-client/src/AsyncMqttClient/Storage.hpp
deleted file mode 100644
index 725307b7f..000000000
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Storage.hpp
+++ /dev/null
@@ -1,13 +0,0 @@
-#pragma once
-
-namespace AsyncMqttClientInternals {
-struct PendingPubRel {
- uint16_t packetId;
-};
-
-struct PendingAck {
- uint8_t packetType;
- uint8_t headerFlag;
- uint16_t packetId;
-};
-} // namespace AsyncMqttClientInternals
diff --git a/lib/async-mqtt-client/LICENSE b/lib/espMqttClient/LICENSE
similarity index 94%
rename from lib/async-mqtt-client/LICENSE
rename to lib/espMqttClient/LICENSE
index a3ee21720..1cc5546f9 100644
--- a/lib/async-mqtt-client/LICENSE
+++ b/lib/espMqttClient/LICENSE
@@ -1,6 +1,6 @@
-The MIT License (MIT)
+MIT License
-Copyright (c) 2015-2021 Marvin Roger
+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
diff --git a/lib/espMqttClient/README.md b/lib/espMqttClient/README.md
new file mode 100644
index 000000000..586d3339e
--- /dev/null
+++ b/lib/espMqttClient/README.md
@@ -0,0 +1,54 @@
+# espMqttClient
+
+MQTT client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework.
+Aims to be a non-blocking, fully compliant MQTT 3.1.1 client.
+
+
+
+
+[](https://registry.platformio.org/libraries/bertmelis/espMqttClient)
+
+# Features
+
+- MQTT 3.1.1 compliant library
+- Sending and receiving at all QoS levels
+- TCP and TCP/TLS using standard WiFiClient and WiFiClientSecure connections
+- Virtually unlimited incoming and outgoing payload sizes
+- Readable and understandable code
+- Fully async clients available via [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) or [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP) (no TLS supported)
+- Supported platforms:
+ - Espressif ESP8266 and ESP32 using the Arduino framework
+- Basic Linux compatibility*. This includes WSL on Windows
+
+ > Linux compatibility is mainly for automatic testing. It relies on a quick and dirty Arduino-style `Client` with a POSIX TCP client underneath and Arduino-style `IPAddress` class. These are lacking many features needed for proper Linux support.
+
+# Documentation
+
+See [documentation](https://www.emelis.net/espMqttClient/) and the [examples](examples/).
+
+## Limitations
+
+### MQTT 3.1.1 Compliancy
+
+Outgoing messages and session data are not stored in non-volatile memory. Any events like loss of power or sudden resets result in loss of data. Despite this limitation, one could still consider this library as fully complaint based on the non normative remark in point 4.1.1 of the specification.
+
+### Non-blocking
+
+This library aims to be fully non-blocking. It is however limited by the underlying `WiFiClient` library which is part of the Arduino framework and has a blocking `connect` method. This is not an issue on ESP32 because the call is offloaded to a separate task. On ESP8266 however, connecting will block until succesful or until the connection timeouts.
+
+If you need a fully asynchronous MQTT client, you can use `espMqttClientAsync` which uses AsyncTCP/ESPAsyncTCP under the hood. These underlying libraries do not support TLS (anymore). I will not provide support TLS for the async client.
+
+# Bugs and feature requests
+
+Please use Github's facilities to get in touch.
+
+# About this library
+
+This client wouldn't exist without [Async-mqtt-client](https://github.com/marvinroger/async-mqtt-client). It has been my go-to MQTT client for many years. It was fast, reliable and had features that were non-existing in alternative libraries. However, the underlying async TCP libraries are lacking updates, especially updates related to secure connections. Adapting this library to use up-to-date TCP clients would not be trivial. I eventually decided to write my own MQTT library, from scratch.
+
+The result is an almost non-blocking library with no external dependencies. The library is almost a drop-in replacement for the async-mqtt-client except a few parameter type changes (eg. `uint8_t*` instead of `char*` for payloads).
+
+# License
+
+This library is released under the MIT Licence. A copy is included in the repo.
+Parts of this library, most notably the API, are based on [Async MQTT client for ESP8266 and ESP32](https://github.com/marvinroger/async-mqtt-client).
diff --git a/lib/espMqttClient/src/Config.h b/lib/espMqttClient/src/Config.h
new file mode 100644
index 000000000..aba779565
--- /dev/null
+++ b/lib/espMqttClient/src/Config.h
@@ -0,0 +1,58 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#ifndef EMC_TX_TIMEOUT
+#define EMC_TX_TIMEOUT 5000
+#endif
+
+#ifndef EMC_RX_BUFFER_SIZE
+#define EMC_RX_BUFFER_SIZE 1440
+#endif
+
+#ifndef EMC_TX_BUFFER_SIZE
+#define EMC_TX_BUFFER_SIZE 1440
+#endif
+
+#ifndef EMC_MAX_TOPIC_LENGTH
+#define EMC_MAX_TOPIC_LENGTH 128
+#endif
+
+#ifndef EMC_PAYLOAD_BUFFER_SIZE
+#define EMC_PAYLOAD_BUFFER_SIZE 32
+#endif
+
+#ifndef EMC_MIN_FREE_MEMORY
+#define EMC_MIN_FREE_MEMORY 4096
+#endif
+
+#ifndef EMC_ESP8266_MULTITHREADING
+#define EMC_ESP8266_MULTITHREADING 0
+#endif
+
+#ifndef EMC_ALLOW_NOT_CONNECTED_PUBLISH
+#define EMC_ALLOW_NOT_CONNECTED_PUBLISH 1
+#endif
+
+#ifndef EMC_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_USE_WATCHDOG
+#define EMC_USE_WATCHDOG 0
+#endif
diff --git a/lib/espMqttClient/src/Helpers.h b/lib/espMqttClient/src/Helpers.h
new file mode 100644
index 000000000..4a19224ba
--- /dev/null
+++ b/lib/espMqttClient/src/Helpers.h
@@ -0,0 +1,49 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#if defined(ARDUINO_ARCH_ESP32)
+ #include // millis(), ESP.getFreeHeap();
+ #include "freertos/FreeRTOS.h"
+ #include "freertos/task.h"
+ #include "esp_task_wdt.h"
+ #define EMC_SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY)
+ #define EMC_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
+ #define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram())
+ #define EMC_YIELD() taskYIELD()
+ #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp32%06llx", ESP.getEfuseMac());
+#elif defined(ARDUINO_ARCH_ESP8266)
+ #include // millis(), ESP.getFreeHeap();
+ #if EMC_ESP8266_MULTITHREADING
+ // This lib doesn't run use multithreading on ESP8266
+ // _xSemaphore defined as std::atomic
+ #define EMC_SEMAPHORE_TAKE() while (_xSemaphore) { /*ESP.wdtFeed();*/ } _xSemaphore = true
+ #define EMC_SEMAPHORE_GIVE() _xSemaphore = false
+ #else
+ #define EMC_SEMAPHORE_TAKE()
+ #define EMC_SEMAPHORE_GIVE()
+ #endif
+ #define EMC_GET_FREE_MEMORY() ESP.getMaxFreeBlockSize()
+ // no need to yield for ESP8266, the Arduino framework does this internally
+ // yielding in async is forbidden (will crash)
+ #define EMC_YIELD()
+ #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "esp8266%06x", ESP.getChipId());
+#elif defined(__linux__)
+ #include // NOLINT [build/c++11]
+ #include // NOLINT [build/c++11] for yield()
+ #define millis() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count()
+ #define EMC_GET_FREE_MEMORY() 1000000000
+ #define EMC_YIELD() std::this_thread::yield()
+ #define EMC_GENERATE_CLIENTID(x) snprintf(x, EMC_CLIENTID_LENGTH, "Client%04d%04d%04d", rand()%10000, rand()%10000, rand()%10000)
+ #include // NOLINT [build/c++11]
+ #define EMC_SEMAPHORE_TAKE() mtx.lock();
+ #define EMC_SEMAPHORE_GIVE() mtx.unlock();
+#else
+ #error Target platform not supported
+#endif
diff --git a/lib/espMqttClient/src/Logging.h b/lib/espMqttClient/src/Logging.h
new file mode 100644
index 000000000..3ba096a8d
--- /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/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp
new file mode 100644
index 000000000..2f1d0d643
--- /dev/null
+++ b/lib/espMqttClient/src/MqttClient.cpp
@@ -0,0 +1,754 @@
+/*
+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)
+#if defined(ARDUINO_ARCH_ESP32)
+ : _useInternalTask(useInternalTask)
+ , _transport(nullptr)
+#else
+ : _transport(nullptr)
+#endif
+ , _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 = true;
+ if (_state == State::disconnected) {
+ EMC_SEMAPHORE_TAKE();
+ if (_addPacketFront(_cleanSession,
+ _username,
+ _password,
+ _willTopic,
+ _willRetain,
+ _willQos,
+ _willPayload,
+ _willPayloadLength,
+ (uint16_t)(_keepAlive / 1000), // 32b to 16b doesn't overflow because it comes from 16b orignally
+ _clientId)) {
+#if defined(ARDUINO_ARCH_ESP32)
+ if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) {
+ vTaskResume(_taskHandle);
+ }
+#endif
+ _state = State::connectingTcp1;
+ } else {
+ EMC_SEMAPHORE_GIVE();
+ emc_log_e("Could not create CONNECT packet");
+ _onError(0, Error::OUT_OF_MEMORY);
+ result = false;
+ }
+ EMC_SEMAPHORE_GIVE();
+ }
+ return result;
+}
+
+bool MqttClient::disconnect(bool force) {
+ if (force && _state != State::disconnected && _state != State::disconnectingTcp1 && _state != State::disconnectingTcp2) {
+ _state = State::disconnectingTcp1;
+ return true;
+ }
+ if (!force && _state == State::connected) {
+ _state = State::disconnectingMqtt1;
+ return true;
+ }
+ return false;
+}
+
+uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, const uint8_t * payload, size_t length) {
+#if !EMC_ALLOW_NOT_CONNECTED_PUBLISH
+ if (_state != State::connected) {
+#else
+ if (_state > State::connected) {
+#endif
+ return 0;
+ }
+ uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1;
+ EMC_SEMAPHORE_TAKE();
+ if (!_addPacket(packetId, topic, payload, length, qos, retain)) {
+ emc_log_e("Could not create PUBLISH packet");
+ _onError(packetId, Error::OUT_OF_MEMORY);
+ packetId = 0;
+ }
+ EMC_SEMAPHORE_GIVE();
+ return packetId;
+}
+
+uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, const char * payload) {
+ size_t len = strlen(payload);
+ return publish(topic, qos, retain, reinterpret_cast(payload), len);
+}
+
+uint16_t MqttClient::publish(const char * topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length) {
+#if !EMC_ALLOW_NOT_CONNECTED_PUBLISH
+ if (_state != State::connected) {
+#else
+ if (_state > State::connected) {
+#endif
+ return 0;
+ }
+ uint16_t packetId = (qos > 0) ? _getNextPacketId() : 1;
+ EMC_SEMAPHORE_TAKE();
+ if (!_addPacket(packetId, topic, callback, length, qos, retain)) {
+ emc_log_e("Could not create PUBLISH packet");
+ _onError(packetId, Error::OUT_OF_MEMORY);
+ packetId = 0;
+ }
+ EMC_SEMAPHORE_GIVE();
+ return packetId;
+}
+
+void MqttClient::clearQueue(bool deleteSessionData) {
+ _clearQueue(deleteSessionData ? 2 : 0);
+}
+
+const char * MqttClient::getClientId() const {
+ return _clientId;
+}
+
+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)) {
+ _state = State::connectingTcp2;
+ } else {
+ _state = State::disconnectingTcp1;
+ _disconnectReason = DisconnectReason::TCP_DISCONNECTED;
+ break;
+ }
+ // Falling through to speed up connecting on blocking transport 'connect' implementations
+ [[fallthrough]];
+ case State::connectingTcp2:
+ if (_transport->connected()) {
+ _parser.reset();
+ _lastClientActivity = _lastServerActivity = millis();
+ _state = State::connectingMqtt;
+ }
+ break;
+ case State::connectingMqtt:
+#if EMC_WAIT_FOR_CONNACK
+ if (_transport->connected()) {
+ _sendPacket();
+ _checkIncoming();
+ _checkPing();
+ } else {
+ _state = 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
+ _checkOutbox();
+ _checkIncoming();
+ _checkPing();
+ _checkTimeout();
+ } else {
+ _state = 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);
+ } else {
+ _state = State::disconnectingMqtt2;
+ }
+ }
+ EMC_SEMAPHORE_GIVE();
+ _checkOutbox();
+ _checkIncoming();
+ _checkPing();
+ _checkTimeout();
+ break;
+ case State::disconnectingTcp1:
+ _transport->stop();
+ _state = State::disconnectingTcp2;
+ break; // keep break to accomodate async clients
+ case State::disconnectingTcp2:
+ if (_transport->disconnected()) {
+ _clearQueue(0);
+ _bytesSent = 0;
+ _state = State::disconnected;
+ if (_onDisconnectCallback)
+ _onDisconnectCallback(_disconnectReason);
+ }
+ break;
+ // all cases covered, no default case
+ }
+ EMC_YIELD();
+#if defined(ARDUINO_ARCH_ESP32) && ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
+ size_t waterMark = uxTaskGetStackHighWaterMark(NULL);
+ if (waterMark < _highWaterMark) {
+ _highWaterMark = waterMark;
+ emc_log_i("Stack usage: %zu/%i", EMC_TASK_STACK_SIZE - _highWaterMark, EMC_TASK_STACK_SIZE);
+ }
+#endif
+}
+
+#if defined(ARDUINO_ARCH_ESP32)
+void MqttClient::_loop(MqttClient * c) {
+#if EMC_USE_WATCHDOG
+ if (esp_task_wdt_add(NULL) != ESP_OK) {
+ emc_log_e("Failed to add async task to WDT");
+ }
+#endif
+ for (;;) {
+ c->loop();
+#if EMC_USE_WATCHDOG
+ esp_task_wdt_reset();
+#endif
+ }
+}
+#endif
+
+uint16_t MqttClient::_getNextPacketId() {
+ uint16_t packetId = 0;
+ EMC_SEMAPHORE_TAKE();
+ // cppcheck-suppress knownConditionTrueFalse
+ packetId = (++_packetId == 0) ? ++_packetId : _packetId;
+ EMC_SEMAPHORE_GIVE();
+ return packetId;
+}
+
+void MqttClient::_checkOutbox() {
+ while (_sendPacket() > 0) {
+ if (!_advanceOutbox()) {
+ break;
+ }
+ }
+}
+
+int MqttClient::_sendPacket() {
+ EMC_SEMAPHORE_TAKE();
+ OutgoingPacket * packet = _outbox.getCurrent();
+
+ int32_t wantToWrite = 0;
+ int32_t written = 0;
+ if (packet && (wantToWrite == written)) {
+ // mixing signed with unsigned here but safe because of MQTT packet size limits
+ wantToWrite = packet->packet.available(_bytesSent);
+ if (wantToWrite == 0) {
+ EMC_SEMAPHORE_GIVE();
+ return 0;
+ }
+ written = _transport->write(packet->packet.data(_bytesSent), wantToWrite);
+ if (written < 0) {
+ emc_log_w("Write error, check connection");
+ EMC_SEMAPHORE_GIVE();
+ return -1;
+ }
+ packet->timeSent = millis();
+ _lastClientActivity = millis();
+ _bytesSent += written;
+ emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->packet.size(), packet->packet.packetType());
+ }
+ EMC_SEMAPHORE_GIVE();
+ return written;
+}
+
+bool MqttClient::_advanceOutbox() {
+ EMC_SEMAPHORE_TAKE();
+ OutgoingPacket * packet = _outbox.getCurrent();
+ if (packet && _bytesSent == packet->packet.size()) {
+ if ((packet->packet.packetType()) == PacketType.DISCONNECT) {
+ _state = 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;
+ }
+ EMC_SEMAPHORE_GIVE();
+ 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");
+ _state = State::disconnectingTcp1;
+ return;
+ }
+ switch (packetType & 0xF0) {
+ 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");
+ _state = State::disconnectingTcp1;
+ _disconnectReason = DisconnectReason::TCP_DISCONNECTED;
+ return;
+ }
+ remainingBufferLength -= bytesParsed;
+ index += bytesParsed;
+ emc_log_i("Parsed %zu - remaining %i", bytesParsed, remainingBufferLength);
+ bytesParsed = 0;
+ }
+ }
+}
+
+void MqttClient::_checkPing() {
+ if (_keepAlive == 0)
+ return; // keepalive is disabled
+
+ uint32_t currentMillis = millis();
+
+ // disconnect when server was inactive for twice the keepalive time
+ if (currentMillis - _lastServerActivity > 2 * _keepAlive) {
+ emc_log_w("Disconnecting, server exceeded keepalive");
+ _state = State::disconnectingTcp1;
+ _disconnectReason = DisconnectReason::TCP_DISCONNECTED;
+ return;
+ }
+
+ // send ping when client was inactive during the keepalive time
+ // or when server hasn't responded within keepalive time (typically due to QOS 0)
+ if (!_pingSent && ((currentMillis - _lastClientActivity > _keepAlive) || (currentMillis - _lastServerActivity > _keepAlive))) {
+ EMC_SEMAPHORE_TAKE();
+ if (!_addPacket(PacketType.PINGREQ)) {
+ EMC_SEMAPHORE_GIVE();
+ emc_log_e("Could not create PING packet");
+ return;
+ }
+ EMC_SEMAPHORE_GIVE();
+ _pingSent = true;
+ }
+}
+
+void MqttClient::_checkTimeout() {
+ EMC_SEMAPHORE_TAKE();
+ 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();
+ }
+ }
+ EMC_SEMAPHORE_GIVE();
+}
+
+void MqttClient::_onConnack() {
+ if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) {
+ _pingSent = false; // reset after keepalive timeout disconnect
+ _state = State::connected;
+ _advanceOutbox();
+ if (_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent == 0) {
+ _clearQueue(1);
+ }
+ if (_onConnectCallback) {
+ _onConnectCallback(_parser.getPacket().variableHeader.fixed.connackVarHeader.sessionPresent);
+ }
+ } else {
+ _state = State::disconnectingTcp1;
+ // cast is safe because the parser already checked for a valid return code
+ _disconnectReason = static_cast(_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode);
+ }
+}
+
+void MqttClient::_onPublish() {
+ espMqttClientInternals::IncomingPacket p = _parser.getPacket();
+ uint8_t qos = p.qos();
+ bool retain = p.retain();
+ bool dup = p.dup();
+ uint16_t packetId = p.variableHeader.fixed.packetId;
+ bool callback = true;
+ if (qos == 1) {
+ if (p.payload.index + p.payload.length == p.payload.total) {
+ EMC_SEMAPHORE_TAKE();
+ if (!_addPacket(PacketType.PUBACK, packetId)) {
+ emc_log_e("Could not create PUBACK packet");
+ }
+ EMC_SEMAPHORE_GIVE();
+ }
+ } else if (qos == 2) {
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ while (it) {
+ if ((it.get()->packet.packetType()) == PacketType.PUBREC && it.get()->packet.packetId() == packetId) {
+ callback = false;
+ _outbox.remove(it);
+ emc_log_e("QoS2 packet previously delivered");
+ break;
+ }
+ ++it;
+ }
+ if (p.payload.index + p.payload.length == p.payload.total) {
+ if (!_addPacket(PacketType.PUBREC, packetId)) {
+ emc_log_e("Could not create PUBREC packet");
+ }
+ }
+ EMC_SEMAPHORE_GIVE();
+ }
+ if (callback && _onMessageCallback)
+ _onMessageCallback({qos, dup, retain, packetId}, p.variableHeader.topic, p.payload.data, p.payload.length, p.payload.index, p.payload.total);
+}
+
+void MqttClient::_onPuback() {
+ bool callback = false;
+ uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId;
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ while (it) {
+ // PUBACKs come in the order PUBs are sent. So we only check the first PUB packet in outbox
+ // if it doesn't match the ID, return
+ if ((it.get()->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;
+ }
+ EMC_SEMAPHORE_GIVE();
+ if (callback) {
+ if (_onPublishCallback)
+ _onPublishCallback(idToMatch);
+ } else {
+ emc_log_w("No matching PUBLISH packet found");
+ }
+}
+
+void MqttClient::_onPubrec() {
+ bool success = false;
+ uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId;
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ while (it) {
+ // PUBRECs come in the order PUBs are sent. So we only check the first PUB packet in outbox
+ // if it doesn't match the ID, return
+ if ((it.get()->packet.packetType()) == PacketType.PUBLISH || (it.get()->packet.packetType()) == PacketType.PUBREL) {
+ 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");
+ }
+ EMC_SEMAPHORE_GIVE();
+}
+
+void MqttClient::_onPubrel() {
+ bool success = false;
+ uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId;
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ while (it) {
+ // PUBRELs come in the order PUBRECs are sent. So we only check the first PUBREC packet in outbox
+ // if it doesn't match the ID, return
+ if ((it.get()->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");
+ }
+ EMC_SEMAPHORE_GIVE();
+}
+
+void MqttClient::_onPubcomp() {
+ bool callback = false;
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId;
+ while (it) {
+ // PUBCOMPs come in the order PUBRELs are sent. So we only check the first PUBREL packet in outbox
+ // if it doesn't match the ID, return
+ if ((it.get()->packet.packetType()) == PacketType.PUBREL) {
+ if (it.get()->packet.packetId() == idToMatch) {
+ // if (!_addPacket(PacketType.PUBCOMP, idToMatch)) {
+ // emc_log_e("Could not create PUBCOMP packet");
+ // }
+ callback = true;
+ _outbox.remove(it);
+ break;
+ }
+ emc_log_w("Received out of order PUBCOMP");
+ break;
+ }
+ ++it;
+ }
+ EMC_SEMAPHORE_GIVE();
+ if (callback) {
+ if (_onPublishCallback)
+ _onPublishCallback(idToMatch);
+ } else {
+ emc_log_w("No matching PUBREL packet found");
+ }
+}
+
+void MqttClient::_onSuback() {
+ bool callback = false;
+ uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId;
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ while (it) {
+ if (((it.get()->packet.packetType()) == PacketType.SUBSCRIBE) && it.get()->packet.packetId() == idToMatch) {
+ callback = true;
+ _outbox.remove(it);
+ break;
+ }
+ ++it;
+ }
+ EMC_SEMAPHORE_GIVE();
+ if (callback) {
+ if (_onSubscribeCallback)
+ _onSubscribeCallback(idToMatch,
+ reinterpret_cast(_parser.getPacket().payload.data),
+ _parser.getPacket().payload.total);
+ } else {
+ emc_log_w("received SUBACK without SUB");
+ }
+}
+
+void MqttClient::_onUnsuback() {
+ bool callback = false;
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ uint16_t idToMatch = _parser.getPacket().variableHeader.fixed.packetId;
+ while (it) {
+ if (it.get()->packet.packetId() == idToMatch) {
+ callback = true;
+ _outbox.remove(it);
+ break;
+ }
+ ++it;
+ }
+ EMC_SEMAPHORE_GIVE();
+ if (callback) {
+ if (_onUnsubscribeCallback)
+ _onUnsubscribeCallback(idToMatch);
+ } else {
+ emc_log_w("received UNSUBACK without UNSUB");
+ }
+}
+
+uint16_t MqttClient::getQueue() const {
+ EMC_SEMAPHORE_TAKE();
+ espMqttClientInternals::Outbox::Iterator it = _outbox.front();
+ uint16_t count = 0;
+ while (it) {
+ ++count;
+ ++it;
+ }
+ EMC_SEMAPHORE_GIVE();
+ return count;
+}
+
+void MqttClient::_clearQueue(int clearData) {
+ emc_log_i("clearing queue (clear session: %d)", clearData);
+ EMC_SEMAPHORE_TAKE();
+ 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);
+ }
+ }
+ EMC_SEMAPHORE_GIVE();
+}
+
+void MqttClient::_onError(uint16_t packetId, espMqttClientTypes::Error error) {
+ if (_onErrorCallback) {
+ _onErrorCallback(packetId, error);
+ }
+}
diff --git a/lib/espMqttClient/src/MqttClient.h b/lib/espMqttClient/src/MqttClient.h
new file mode 100644
index 000000000..6d36e0af5
--- /dev/null
+++ b/lib/espMqttClient/src/MqttClient.h
@@ -0,0 +1,190 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+API is based on the original work of Marvin Roger:
+https://github.com/marvinroger/async-mqtt-client
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#include
+#include
+
+#include "Helpers.h"
+#include "Config.h"
+#include "TypeDefs.h"
+#include "Logging.h"
+#include "Outbox.h"
+#include "Packets/Packet.h"
+#include "Packets/Parser.h"
+#include "Transport/Transport.h"
+
+class MqttClient {
+ public:
+ virtual ~MqttClient();
+ bool connected() const;
+ bool disconnected() const;
+ bool connect();
+ bool disconnect(bool force = false);
+ template
+ uint16_t subscribe(const char* topic, uint8_t qos, Args&&... args) {
+ uint16_t packetId = _getNextPacketId();
+ if (_state != State::connected) {
+ packetId = 0;
+ } else {
+ EMC_SEMAPHORE_TAKE();
+ if (!_addPacket(packetId, topic, qos, std::forward(args) ...)) {
+ emc_log_e("Could not create SUBSCRIBE packet");
+ packetId = 0;
+ }
+ EMC_SEMAPHORE_GIVE();
+ }
+ return packetId;
+ }
+ template
+ uint16_t unsubscribe(const char* topic, Args&&... args) {
+ uint16_t packetId = _getNextPacketId();
+ if (_state != State::connected) {
+ packetId = 0;
+ } else {
+ EMC_SEMAPHORE_TAKE();
+ if (!_addPacket(packetId, topic, std::forward(args) ...)) {
+ emc_log_e("Could not create UNSUBSCRIBE packet");
+ packetId = 0;
+ }
+ EMC_SEMAPHORE_GIVE();
+ }
+ return packetId;
+ }
+ uint16_t publish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length);
+ uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload);
+ uint16_t publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length);
+ void clearQueue(bool deleteSessionData = false); // Not MQTT compliant and may cause unpredictable results when `deleteSessionData` = true!
+ const char* getClientId() const;
+ uint16_t getQueue() const;
+ 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;
+
+ 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) :
+ 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;
+ 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;
+ 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 000000000..73458d477
--- /dev/null
+++ b/lib/espMqttClient/src/MqttClientSetup.h
@@ -0,0 +1,116 @@
+/*
+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"
+
+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) {
+ _onConnectCallback = callback;
+ return static_cast(*this);
+ }
+
+ T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) {
+ _onDisconnectCallback = callback;
+ return static_cast(*this);
+ }
+
+ T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) {
+ _onSubscribeCallback = callback;
+ return static_cast(*this);
+ }
+
+ T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) {
+ _onUnsubscribeCallback = callback;
+ return static_cast(*this);
+ }
+
+ T& onMessage(espMqttClientTypes::OnMessageCallback callback) {
+ _onMessageCallback = callback;
+ return static_cast(*this);
+ }
+
+ T& onPublish(espMqttClientTypes::OnPublishCallback callback) {
+ _onPublishCallback = callback;
+ return static_cast(*this);
+ }
+
+ /*
+ 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) {}
+};
diff --git a/lib/espMqttClient/src/Outbox.h b/lib/espMqttClient/src/Outbox.h
new file mode 100644
index 000000000..dfbbd13c0
--- /dev/null
+++ b/lib/espMqttClient/src/Outbox.h
@@ -0,0 +1,207 @@
+
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#include // new (std::nothrow)
+#include // std::forward
+
+namespace espMqttClientInternals {
+
+/**
+ * @brief Singly linked queue with builtin non-invalidating forward iterator
+ *
+ * Queue items can only be emplaced, at front and back of the queue.
+ * Remove items using an iterator or the builtin iterator.
+ */
+
+template
+class Outbox {
+ public:
+ Outbox()
+ : _first(nullptr)
+ , _last(nullptr)
+ , _current(nullptr)
+ , _prev(nullptr) {}
+ ~Outbox() {
+ while (_first) {
+ Node* n = _first->next;
+ delete _first;
+ _first = n;
+ }
+ }
+
+ struct Node {
+ public:
+ template
+ explicit Node(Args&&... args)
+ : data(std::forward(args) ...)
+ , next(nullptr) {
+ // empty
+ }
+
+ T data;
+ Node* next;
+ };
+
+ class Iterator {
+ friend class Outbox;
+ public:
+ void operator++() {
+ if (_node) {
+ _prev = _node;
+ _node = _node->next;
+ }
+ }
+
+ explicit operator bool() const {
+ if (_node) return true;
+ return false;
+ }
+
+ T* get() const {
+ if (_node) return &(_node->data);
+ return nullptr;
+ }
+
+ private:
+ Node* _node = nullptr;
+ Node* _prev = nullptr;
+ };
+
+ // add node to back, advance current to new if applicable
+ template
+ Iterator emplace(Args&&... args) {
+ Iterator it;
+ Node* node = new (std::nothrow) Node(std::forward(args) ...);
+ if (node != nullptr) {
+ if (!_first) {
+ // queue is empty
+ _first = _current = node;
+ } else {
+ // queue has at least one item
+ _last->next = node;
+ it._prev = _last;
+ }
+ _last = node;
+ it._node = node;
+ // point current to newly created if applicable
+ if (!_current) {
+ _current = _last;
+ }
+ }
+ return it;
+ }
+
+ // add item to front, current points to newly created front.
+ template
+ Iterator emplaceFront(Args&&... args) {
+ Iterator it;
+ Node* node = new (std::nothrow) Node(std::forward(args) ...);
+ if (node != nullptr) {
+ if (!_first) {
+ // queue is empty
+ _last = node;
+ } else {
+ // queue has at least one item
+ node->next = _first;
+ }
+ _current = _first = node;
+ _prev = nullptr;
+ it._node = node;
+ }
+ return it;
+ }
+
+ // remove node at iterator, iterator points to next
+ void remove(Iterator& it) { // NOLINT(runtime/references)
+ 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;
+ }
+
+ private:
+ Node* _first;
+ Node* _last;
+ Node* _current;
+ Node* _prev; // element just before _current
+
+ void _remove(Node* prev, Node* node) {
+ if (!node) return;
+
+ // set current to next, node->next may be nullptr
+ if (_current == node) {
+ _current = node->next;
+ }
+
+ if (_prev == node) {
+ _prev = prev;
+ }
+
+ // only one element in outbox
+ if (_first == _last) {
+ _first = _last = nullptr;
+
+ // delete first el in longer outbox
+ } else if (_first == node) {
+ _first = node->next;
+
+ // delete last in longer outbox
+ } else if (_last == node) {
+ _last = prev;
+ _last->next = nullptr;
+
+ // delete somewhere in the middle
+ } else {
+ prev->next = node->next;
+ }
+
+ // finally, delete the node
+ delete node;
+ }
+};
+
+} // end namespace espMqttClientInternals
diff --git a/lib/async-mqtt-client/src/AsyncMqttClient/Flags.hpp b/lib/espMqttClient/src/Packets/Constants.h
similarity index 54%
rename from lib/async-mqtt-client/src/AsyncMqttClient/Flags.hpp
rename to lib/espMqttClient/src/Packets/Constants.h
index a1fb3e3c8..ee92e3114 100644
--- a/lib/async-mqtt-client/src/AsyncMqttClient/Flags.hpp
+++ b/lib/espMqttClient/src/Packets/Constants.h
@@ -1,23 +1,42 @@
+/*
+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
-namespace AsyncMqttClientInternals {
+#include
+
+namespace espMqttClientInternals {
+
+constexpr const char PROTOCOL[] = "MQTT";
+constexpr const uint8_t PROTOCOL_LEVEL = 0b00000100;
+
+typedef uint8_t MQTTPacketType;
+
constexpr struct {
- const uint8_t RESERVED = 0;
- const uint8_t CONNECT = 1;
- const uint8_t CONNACK = 2;
- const uint8_t PUBLISH = 3;
- const uint8_t PUBACK = 4;
- const uint8_t PUBREC = 5;
- const uint8_t PUBREL = 6;
- const uint8_t PUBCOMP = 7;
- const uint8_t SUBSCRIBE = 8;
- const uint8_t SUBACK = 9;
- const uint8_t UNSUBSCRIBE = 10;
- const uint8_t UNSUBACK = 11;
- const uint8_t PINGREQ = 12;
- const uint8_t PINGRESP = 13;
- const uint8_t DISCONNECT = 14;
- const uint8_t RESERVED2 = 1;
+ 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 {
@@ -54,4 +73,5 @@ constexpr struct {
const uint8_t CLEAN_SESSION = 0x02;
const uint8_t RESERVED = 0x00;
} ConnectFlag;
-} // namespace AsyncMqttClientInternals
+
+} // end namespace espMqttClientInternals
diff --git a/lib/espMqttClient/src/Packets/Packet.cpp b/lib/espMqttClient/src/Packets/Packet.cpp
new file mode 100644
index 000000000..df463ef7b
--- /dev/null
+++ b/lib/espMqttClient/src/Packets/Packet.cpp
@@ -0,0 +1,438 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#include "Packet.h"
+
+namespace espMqttClientInternals {
+
+Packet::~Packet() {
+ free(_data);
+}
+
+size_t Packet::available(size_t index) {
+ if (index >= _size) return 0;
+ if (!_getPayload) return _size - index;
+ return _chunkedAvailable(index);
+}
+
+const uint8_t* Packet::data(size_t index) const {
+ if (!_getPayload) {
+ if (!_data) return nullptr;
+ if (index >= _size) return nullptr;
+ return &_data[index];
+ }
+ return _chunkedData(index);
+}
+
+size_t Packet::size() const {
+ return _size;
+}
+
+void Packet::setDup() {
+ if (!_data) return;
+ if (packetType() != PacketType.PUBLISH) return;
+ if (_packetId == 0) return;
+ _data[0] |= 0x08;
+}
+
+uint16_t Packet::packetId() const {
+ return _packetId;
+}
+
+MQTTPacketType Packet::packetType() const {
+ if (_data) return static_cast(_data[0] & 0xF0);
+ return static_cast(0);
+}
+
+bool Packet::removable() const {
+ if (_packetId == 0) return true;
+ if ((packetType() == PacketType.PUBACK) || (packetType() == PacketType.PUBCOMP)) return true;
+ return false;
+}
+
+Packet::Packet(espMqttClientTypes::Error& error,
+ bool cleanSession,
+ const char* username,
+ const char* password,
+ const char* willTopic,
+ bool willRetain,
+ uint8_t willQos,
+ const uint8_t* willPayload,
+ uint16_t willPayloadLength,
+ uint16_t keepAlive,
+ const char* clientId)
+: _packetId(0)
+, _data(nullptr)
+, _size(0)
+, _payloadIndex(0)
+, _payloadStartIndex(0)
+, _payloadEndIndex(0)
+, _getPayload(nullptr) {
+ if (willPayload && willPayloadLength == 0) {
+ size_t length = strlen(reinterpret_cast(willPayload));
+ if (length > UINT16_MAX) {
+ emc_log_w("Payload length truncated (l:%zu)", length);
+ willPayloadLength = UINT16_MAX;
+ } else {
+ willPayloadLength = length;
+ }
+ }
+ if (!clientId || strlen(clientId) == 0) {
+ emc_log_w("clientId not set error");
+ error = espMqttClientTypes::Error::MALFORMED_PARAMETER;
+ return;
+ }
+
+ // Calculate size
+ size_t remainingLength =
+ 6 + // protocol
+ 1 + // protocol level
+ 1 + // connect flags
+ 2 + // keepalive
+ 2 + strlen(clientId) +
+ (willTopic ? 2 + strlen(willTopic) + 2 + willPayloadLength : 0) +
+ (username ? 2 + strlen(username) : 0) +
+ (password ? 2 + strlen(password) : 0);
+
+ // allocate memory
+ if (!_allocate(remainingLength)) {
+ error = espMqttClientTypes::Error::OUT_OF_MEMORY;
+ return;
+ }
+
+ // serialize
+ size_t pos = 0;
+
+ // FIXED HEADER
+ _data[pos++] = PacketType.CONNECT | HeaderFlag.CONNECT_RESERVED;
+ pos += encodeRemainingLength(remainingLength, &_data[pos]);
+ pos += encodeString(PROTOCOL, &_data[pos]);
+ _data[pos++] = PROTOCOL_LEVEL;
+ uint8_t connectFlags = 0;
+ if (cleanSession) connectFlags |= espMqttClientInternals::ConnectFlag.CLEAN_SESSION;
+ if (username != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.USERNAME;
+ if (password != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.PASSWORD;
+ if (willTopic != nullptr) {
+ connectFlags |= espMqttClientInternals::ConnectFlag.WILL;
+ if (willRetain) connectFlags |= espMqttClientInternals::ConnectFlag.WILL_RETAIN;
+ switch (willQos) {
+ case 0:
+ connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS0;
+ break;
+ case 1:
+ connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS1;
+ break;
+ case 2:
+ connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS2;
+ break;
+ }
+ }
+ _data[pos++] = connectFlags;
+ _data[pos++] = keepAlive >> 8;
+ _data[pos++] = keepAlive & 0xFF;
+
+ // PAYLOAD
+ // client ID
+ pos += encodeString(clientId, &_data[pos]);
+ // will
+ if (willTopic != nullptr && willPayload != nullptr) {
+ pos += encodeString(willTopic, &_data[pos]);
+ _data[pos++] = willPayloadLength >> 8;
+ _data[pos++] = willPayloadLength & 0xFF;
+ memcpy(&_data[pos], willPayload, willPayloadLength);
+ pos += willPayloadLength;
+ }
+ // credentials
+ if (username != nullptr) pos += encodeString(username, &_data[pos]);
+ if (password != nullptr) encodeString(password, &_data[pos]);
+
+ error = espMqttClientTypes::Error::SUCCESS;
+}
+
+Packet::Packet(espMqttClientTypes::Error& error,
+ uint16_t packetId,
+ const char* topic,
+ const uint8_t* payload,
+ size_t payloadLength,
+ uint8_t qos,
+ bool retain)
+: _packetId(packetId)
+, _data(nullptr)
+, _size(0)
+, _payloadIndex(0)
+, _payloadStartIndex(0)
+, _payloadEndIndex(0)
+, _getPayload(nullptr) {
+ size_t remainingLength =
+ 2 + strlen(topic) + // topic length + topic
+ 2 + // packet ID
+ payloadLength;
+
+ if (qos == 0) {
+ remainingLength -= 2;
+ _packetId = 0;
+ }
+
+ if (!_allocate(remainingLength)) {
+ error = espMqttClientTypes::Error::OUT_OF_MEMORY;
+ return;
+ }
+
+ size_t pos = _fillPublishHeader(packetId, topic, remainingLength, qos, retain);
+
+ // PAYLOAD
+ memcpy(&_data[pos], payload, payloadLength);
+
+ error = espMqttClientTypes::Error::SUCCESS;
+}
+
+Packet::Packet(espMqttClientTypes::Error& error,
+ uint16_t packetId,
+ const char* topic,
+ espMqttClientTypes::PayloadCallback payloadCallback,
+ size_t payloadLength,
+ uint8_t qos,
+ bool retain)
+: _packetId(packetId)
+, _data(nullptr)
+, _size(0)
+, _payloadIndex(0)
+, _payloadStartIndex(0)
+, _payloadEndIndex(0)
+, _getPayload(payloadCallback) {
+ size_t remainingLength =
+ 2 + strlen(topic) + // topic length + topic
+ 2 + // packet ID
+ payloadLength;
+
+ if (qos == 0) {
+ remainingLength -= 2;
+ _packetId = 0;
+ }
+
+ if (!_allocate(remainingLength - payloadLength + std::min(payloadLength, static_cast(EMC_RX_BUFFER_SIZE)))) {
+ error = espMqttClientTypes::Error::OUT_OF_MEMORY;
+ return;
+ }
+
+ size_t pos = _fillPublishHeader(packetId, topic, remainingLength, qos, retain);
+
+ // payload will be added by 'Packet::available'
+ _size = pos + payloadLength;
+ _payloadIndex = pos;
+ _payloadStartIndex = _payloadIndex;
+ _payloadEndIndex = _payloadIndex;
+
+ error = espMqttClientTypes::Error::SUCCESS;
+}
+
+Packet::Packet(espMqttClientTypes::Error& error, uint16_t packetId, const char* topic, uint8_t qos)
+: _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)) {
+ 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)) {
+ error = espMqttClientTypes::Error::OUT_OF_MEMORY;
+ return;
+ }
+ _data[0] |= type;
+
+ error = espMqttClientTypes::Error::SUCCESS;
+}
+
+
+bool Packet::_allocate(size_t remainingLength) {
+ if (EMC_GET_FREE_MEMORY() < EMC_MIN_FREE_MEMORY) {
+ emc_log_w("Packet buffer not allocated: low memory");
+ return false;
+ }
+ _size = 1 + remainingLengthLength(remainingLength) + remainingLength;
+ _data = reinterpret_cast(malloc(_size));
+ if (!_data) {
+ _size = 0;
+ emc_log_w("Alloc failed (l:%zu)", _size);
+ return false;
+ }
+ emc_log_i("Alloc (l:%zu)", _size);
+ memset(_data, 0, _size);
+ return true;
+}
+
+size_t Packet::_fillPublishHeader(uint16_t packetId,
+ const char* topic,
+ size_t remainingLength,
+ uint8_t qos,
+ bool retain) {
+ size_t index = 0;
+
+ // FIXED HEADER
+ _data[index] = PacketType.PUBLISH;
+ if (retain) _data[index] |= HeaderFlag.PUBLISH_RETAIN;
+ if (qos == 0) {
+ _data[index++] |= HeaderFlag.PUBLISH_QOS0;
+ } else if (qos == 1) {
+ _data[index++] |= HeaderFlag.PUBLISH_QOS1;
+ } else if (qos == 2) {
+ _data[index++] |= HeaderFlag.PUBLISH_QOS2;
+ }
+ index += encodeRemainingLength(remainingLength, &_data[index]);
+
+ // VARIABLE HEADER
+ index += encodeString(topic, &_data[index]);
+ if (qos > 0) {
+ _data[index++] = packetId >> 8;
+ _data[index++] = packetId & 0xFF;
+ }
+
+ return index;
+}
+
+void Packet::_createSubscribe(espMqttClientTypes::Error& error,
+ SubscribeItem* list,
+ size_t numberTopics) {
+ // Calculate size
+ size_t payload = 0;
+ for (size_t i = 0; i < numberTopics; ++i) {
+ payload += 2 + strlen(list[i].topic) + 1; // length bytes, string, qos
+ }
+ size_t remainingLength = 2 + payload; // packetId + payload
+
+ // allocate memory
+ if (!_allocate(remainingLength)) {
+ error = espMqttClientTypes::Error::OUT_OF_MEMORY;
+ return;
+ }
+
+ // serialize
+ size_t pos = 0;
+ _data[pos++] = PacketType.SUBSCRIBE | HeaderFlag.SUBSCRIBE_RESERVED;
+ pos += encodeRemainingLength(remainingLength, &_data[pos]);
+ _data[pos++] = _packetId >> 8;
+ _data[pos++] = _packetId & 0xFF;
+ for (size_t i = 0; i < numberTopics; ++i) {
+ pos += encodeString(list[i].topic, &_data[pos]);
+ _data[pos++] = list[i].qos;
+ }
+
+ error = espMqttClientTypes::Error::SUCCESS;
+}
+
+void Packet::_createUnsubscribe(espMqttClientTypes::Error& error,
+ const char** list,
+ size_t numberTopics) {
+ // Calculate size
+ size_t payload = 0;
+ for (size_t i = 0; i < numberTopics; ++i) {
+ payload += 2 + strlen(list[i]); // length bytes, string
+ }
+ size_t remainingLength = 2 + payload; // packetId + payload
+
+ // allocate memory
+ if (!_allocate(remainingLength)) {
+ error = espMqttClientTypes::Error::OUT_OF_MEMORY;
+ return;
+ }
+
+ // serialize
+ size_t pos = 0;
+ _data[pos++] = PacketType.UNSUBSCRIBE | HeaderFlag.UNSUBSCRIBE_RESERVED;
+ pos += encodeRemainingLength(remainingLength, &_data[pos]);
+ _data[pos++] = _packetId >> 8;
+ _data[pos++] = _packetId & 0xFF;
+ for (size_t i = 0; i < numberTopics; ++i) {
+ pos += encodeString(list[i], &_data[pos]);
+ }
+
+ error = espMqttClientTypes::Error::SUCCESS;
+}
+
+size_t Packet::_chunkedAvailable(size_t index) {
+ // index vs size check done in 'available(index)'
+
+ // index points to header or first payload byte
+ if (index < _payloadIndex) {
+ if (_size > _payloadIndex && _payloadEndIndex != 0) {
+ size_t copied = _getPayload(&_data[_payloadIndex], std::min(static_cast(EMC_TX_BUFFER_SIZE), _size - _payloadStartIndex), index);
+ _payloadStartIndex = _payloadIndex;
+ _payloadEndIndex = _payloadStartIndex + copied - 1;
+ }
+
+ // index points to payload unavailable
+ } else if (index > _payloadEndIndex || _payloadStartIndex > index) {
+ _payloadStartIndex = index;
+ size_t copied = _getPayload(&_data[_payloadIndex], std::min(static_cast(EMC_TX_BUFFER_SIZE), _size - _payloadStartIndex), index);
+ _payloadEndIndex = _payloadStartIndex + copied - 1;
+ }
+
+ // now index points to header or payload available
+ return _payloadEndIndex - index + 1;
+}
+
+const uint8_t* Packet::_chunkedData(size_t index) const {
+ // CAUTION!! available(index) has to be called first to check available data and possibly fill payloadbuffer
+ if (index < _payloadIndex) {
+ return &_data[index];
+ }
+ return &_data[index - _payloadStartIndex + _payloadIndex];
+}
+
+} // end namespace espMqttClientInternals
diff --git a/lib/espMqttClient/src/Packets/Packet.h b/lib/espMqttClient/src/Packets/Packet.h
new file mode 100644
index 000000000..1af2f06af
--- /dev/null
+++ b/lib/espMqttClient/src/Packets/Packet.h
@@ -0,0 +1,155 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#include
+#include
+
+#include "Constants.h"
+#include "../Config.h"
+#include "../TypeDefs.h"
+#include "../Helpers.h"
+#include "../Logging.h"
+#include "RemainingLength.h"
+#include "String.h"
+
+namespace espMqttClientInternals {
+
+class Packet {
+ public:
+ ~Packet();
+ size_t available(size_t index);
+ const uint8_t* data(size_t index) const;
+
+ size_t size() const;
+ void setDup();
+ uint16_t packetId() const;
+ MQTTPacketType packetType() const;
+ bool removable() const;
+
+ 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);
+
+ // fills header and returns index of next available byte in buffer
+ size_t _fillPublishHeader(uint16_t packetId,
+ const char* topic,
+ size_t remainingLength,
+ uint8_t qos,
+ bool retain);
+ void _createSubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
+ SubscribeItem* list,
+ size_t numberTopics);
+ void _createUnsubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
+ const char** list,
+ size_t numberTopics);
+
+ size_t _chunkedAvailable(size_t index);
+ const uint8_t* _chunkedData(size_t index) const;
+};
+
+} // end namespace espMqttClientInternals
diff --git a/lib/espMqttClient/src/Packets/Parser.cpp b/lib/espMqttClient/src/Packets/Parser.cpp
new file mode 100644
index 000000000..07998a3da
--- /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 000000000..2f6334e42
--- /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 000000000..d8644a33a
--- /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 000000000..0b84e23e5
--- /dev/null
+++ b/lib/espMqttClient/src/Packets/RemainingLength.h
@@ -0,0 +1,32 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#include
+
+#include "../Logging.h"
+
+namespace espMqttClientInternals {
+
+// Calculations are based on non normative comment in section 2.2.3 Remaining Length of the MQTT specification
+
+// returns decoded length based on input stream
+// stream is expected to contain full encoded remaining length
+// return -1 on error.
+int32_t decodeRemainingLength(const uint8_t* stream);
+
+
+// returns the number of bytes needed to encode the remaining length
+uint8_t remainingLengthLength(uint32_t remainingLength);
+
+// encodes the given remaining length to destination and returns number of bytes used
+// destination is expected to be large enough to hold the number of bytes needed
+uint8_t encodeRemainingLength(uint32_t remainingLength, uint8_t* destination);
+
+} // namespace espMqttClientInternals
diff --git a/lib/espMqttClient/src/Packets/String.cpp b/lib/espMqttClient/src/Packets/String.cpp
new file mode 100644
index 000000000..c3fe23fdc
--- /dev/null
+++ b/lib/espMqttClient/src/Packets/String.cpp
@@ -0,0 +1,26 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#include "String.h"
+
+namespace espMqttClientInternals {
+
+size_t encodeString(const char* source, uint8_t* dest) {
+ size_t length = strlen(source);
+ if (length > 65535) {
+ emc_log_e("String length error");
+ return 0;
+ }
+
+ dest[0] = static_cast(length) >> 8;
+ dest[1] = static_cast(length) & 0xFF;
+ memcpy(&dest[2], source, length);
+ return 2 + length;
+}
+
+} // namespace espMqttClientInternals
diff --git a/lib/espMqttClient/src/Packets/String.h b/lib/espMqttClient/src/Packets/String.h
new file mode 100644
index 000000000..7f1e1e848
--- /dev/null
+++ b/lib/espMqttClient/src/Packets/String.h
@@ -0,0 +1,22 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#include
+#include // memcpy
+
+#include "../Logging.h"
+
+namespace espMqttClientInternals {
+
+// encodes the given source string into destination and returns number of bytes used
+// destination is expected to be large enough to hold the number of bytes needed
+size_t encodeString(const char* source, uint8_t* dest);
+
+} // namespace espMqttClientInternals
diff --git a/lib/espMqttClient/src/Transport/ClientAsync.cpp b/lib/espMqttClient/src/Transport/ClientAsync.cpp
new file mode 100644
index 000000000..4f8d69eeb
--- /dev/null
+++ b/lib/espMqttClient/src/Transport/ClientAsync.cpp
@@ -0,0 +1,58 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
+
+#include "ClientAsync.h"
+
+namespace espMqttClientInternals {
+
+ClientAsync::ClientAsync()
+: client()
+, availableData(0)
+, bufData(nullptr) {
+ // empty
+}
+
+bool ClientAsync::connect(IPAddress ip, uint16_t port) {
+ return client.connect(ip, port);
+}
+
+bool ClientAsync::connect(const char* host, uint16_t port) {
+ return client.connect(host, port);
+}
+
+size_t ClientAsync::write(const uint8_t* buf, size_t size) {
+ return client.write(reinterpret_cast(buf), size);
+}
+
+int ClientAsync::read(uint8_t* buf, size_t size) {
+ size_t willRead = std::min(size, availableData);
+ memcpy(buf, bufData, std::min(size, availableData));
+ if (availableData > size) {
+ emc_log_w("Buffer is smaller than available data: %zu - %zu", size, availableData);
+ }
+ availableData = 0;
+ return willRead;
+}
+
+void ClientAsync::stop() {
+ client.close(false);
+}
+
+bool ClientAsync::connected() {
+ return client.connected();
+}
+
+bool ClientAsync::disconnected() {
+ return client.disconnected();
+}
+
+} // namespace espMqttClientInternals
+
+#endif
diff --git a/lib/espMqttClient/src/Transport/ClientAsync.h b/lib/espMqttClient/src/Transport/ClientAsync.h
new file mode 100644
index 000000000..c3ddd0388
--- /dev/null
+++ b/lib/espMqttClient/src/Transport/ClientAsync.h
@@ -0,0 +1,44 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
+
+#pragma once
+
+#if defined(ARDUINO_ARCH_ESP32)
+ #include "freertos/FreeRTOS.h"
+ #include
+#elif defined(ARDUINO_ARCH_ESP8266)
+ #include
+#endif
+
+#include "Transport.h"
+#include "../Config.h"
+#include "../Logging.h"
+
+namespace espMqttClientInternals {
+
+class ClientAsync : public Transport {
+ public:
+ ClientAsync();
+ bool connect(IPAddress ip, uint16_t port) override;
+ bool connect(const char* host, uint16_t port) override;
+ size_t write(const uint8_t* buf, size_t size) override;
+ int read(uint8_t* buf, size_t size) override;
+ void stop() override;
+ bool connected() override;
+ bool disconnected() override;
+
+ AsyncClient client;
+ size_t availableData;
+ uint8_t* bufData;
+};
+
+} // namespace espMqttClientInternals
+
+#endif
diff --git a/lib/espMqttClient/src/Transport/ClientPosix.cpp b/lib/espMqttClient/src/Transport/ClientPosix.cpp
new file mode 100644
index 000000000..83b555d1b
--- /dev/null
+++ b/lib/espMqttClient/src/Transport/ClientPosix.cpp
@@ -0,0 +1,92 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#include "ClientPosix.h"
+
+#if defined(__linux__)
+
+namespace espMqttClientInternals {
+
+ClientPosix::ClientPosix()
+: _sockfd(-1)
+, _host() {
+ // empty
+}
+
+ClientPosix::~ClientPosix() {
+ ClientPosix::stop();
+}
+
+bool ClientPosix::connect(IPAddress ip, uint16_t port) {
+ if (connected()) stop();
+
+ _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
+ if (_sockfd < 0) {
+ emc_log_e("Error %d opening socket", errno);
+ }
+
+ int flag = 1;
+ if (setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)) < 0) {
+ emc_log_e("Error %d disabling nagle", errno);
+ }
+
+ memset(&_host, 0, sizeof(_host));
+ _host.sin_family = AF_INET;
+ _host.sin_addr.s_addr = htonl(uint32_t(ip));
+ _host.sin_port = ::htons(port);
+
+ int ret = ::connect(_sockfd, (struct sockaddr *)&_host, sizeof(_host));
+
+ if (ret < 0) {
+ emc_log_e("Error connecting: %d - (%d) %s", ret, errno, strerror(errno));
+ return false;
+ }
+
+ emc_log_i("Connected");
+ return true;
+}
+
+bool ClientPosix::connect(const char* host, uint16_t port) {
+ // tbi
+ (void) host;
+ (void) port;
+ return false;
+}
+
+size_t ClientPosix::write(const uint8_t* buf, size_t size) {
+ return ::send(_sockfd, buf, size, 0);
+}
+
+int ClientPosix::read(uint8_t* buf, size_t size) {
+ int ret = ::recv(_sockfd, buf, size, MSG_DONTWAIT);
+ /*
+ if (ret < 0) {
+ emc_log_e("Error reading: %s", strerror(errno));
+ }
+ */
+ return ret;
+}
+
+void ClientPosix::stop() {
+ if (_sockfd >= 0) {
+ ::close(_sockfd);
+ _sockfd = -1;
+ }
+}
+
+bool ClientPosix::connected() {
+ return _sockfd >= 0;
+}
+
+bool ClientPosix::disconnected() {
+ return _sockfd < 0;
+}
+
+} // namespace espMqttClientInternals
+
+#endif
diff --git a/lib/espMqttClient/src/Transport/ClientPosix.h b/lib/espMqttClient/src/Transport/ClientPosix.h
new file mode 100644
index 000000000..a2f9c9fae
--- /dev/null
+++ b/lib/espMqttClient/src/Transport/ClientPosix.h
@@ -0,0 +1,51 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#if defined(__linux__)
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "Transport.h" // includes IPAddress
+#include "../Logging.h"
+
+#ifndef EMC_POSIX_PEEK_SIZE
+#define EMC_POSIX_PEEK_SIZE 1500
+#endif
+
+namespace espMqttClientInternals {
+
+class ClientPosix : public Transport {
+ public:
+ ClientPosix();
+ ~ClientPosix();
+ bool connect(IPAddress ip, uint16_t port) override;
+ bool connect(const char* host, uint16_t port) override;
+ size_t write(const uint8_t* buf, size_t size) override;
+ int read(uint8_t* buf, size_t size) override;
+ void stop() override;
+ bool connected() override;
+ bool disconnected() override;
+
+ protected:
+ int _sockfd;
+ struct sockaddr_in _host;
+};
+
+} // namespace espMqttClientInternals
+
+#endif
diff --git a/lib/espMqttClient/src/Transport/ClientSecureSync.cpp b/lib/espMqttClient/src/Transport/ClientSecureSync.cpp
new file mode 100644
index 000000000..36288c6a5
--- /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 000000000..b81681e36
--- /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 000000000..b2c4045f1
--- /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 000000000..ccfbdbae5
--- /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/IPAddress.cpp b/lib/espMqttClient/src/Transport/IPAddress.cpp
new file mode 100644
index 000000000..b198429dc
--- /dev/null
+++ b/lib/espMqttClient/src/Transport/IPAddress.cpp
@@ -0,0 +1,32 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#if defined(__linux__)
+
+#include "IPAddress.h"
+
+IPAddress::IPAddress()
+: _address(0) {
+ // empty
+}
+
+IPAddress::IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3)
+: _address(0) {
+ _address = (uint32_t)p0 << 24 | (uint32_t)p1 << 16 | (uint32_t)p2 << 8 | p3;
+}
+
+IPAddress::IPAddress(uint32_t address)
+: _address(address) {
+ // empty
+}
+
+IPAddress::operator uint32_t() {
+ return _address;
+}
+
+#endif
diff --git a/lib/espMqttClient/src/Transport/IPAddress.h b/lib/espMqttClient/src/Transport/IPAddress.h
new file mode 100644
index 000000000..279a1957d
--- /dev/null
+++ b/lib/espMqttClient/src/Transport/IPAddress.h
@@ -0,0 +1,28 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#if defined(ARDUINO)
+ #include
+#else
+
+#include
+
+class IPAddress {
+ public:
+ IPAddress();
+ IPAddress(uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3);
+ explicit IPAddress(uint32_t address);
+ operator uint32_t();
+
+ protected:
+ uint32_t _address;
+};
+
+#endif
diff --git a/lib/espMqttClient/src/Transport/Transport.h b/lib/espMqttClient/src/Transport/Transport.h
new file mode 100644
index 000000000..6720c024f
--- /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 "IPAddress.h"
+
+namespace espMqttClientInternals {
+
+class Transport {
+ public:
+ virtual bool connect(IPAddress ip, uint16_t port) = 0;
+ virtual bool connect(const char* host, uint16_t port) = 0;
+ virtual size_t write(const uint8_t* buf, size_t size) = 0;
+ virtual int 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 000000000..4f92c1f64
--- /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 000000000..0f1536069
--- /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 000000000..833ece10b
--- /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 000000000..4e448011d
--- /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/src/espMqttClientAsync.cpp b/lib/espMqttClient/src/espMqttClientAsync.cpp
new file mode 100644
index 000000000..98b7f1519
--- /dev/null
+++ b/lib/espMqttClient/src/espMqttClientAsync.cpp
@@ -0,0 +1,61 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
+
+#include "espMqttClientAsync.h"
+
+espMqttClientAsync::espMqttClientAsync()
+: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO)
+, _clientAsync() {
+ _transport = &_clientAsync;
+ _clientAsync.client.onConnect(onConnectCb, this);
+ _clientAsync.client.onDisconnect(onDisconnectCb, this);
+ _clientAsync.client.onData(onDataCb, this);
+ _clientAsync.client.onPoll(onPollCb, this);
+}
+
+bool espMqttClientAsync::connect() {
+ bool ret = MqttClient::connect();
+ loop();
+ return ret;
+}
+
+void espMqttClientAsync::_setupClient(espMqttClientAsync* c) {
+ (void)c;
+}
+
+void espMqttClientAsync::onConnectCb(void* a, AsyncClient* c) {
+ c->setNoDelay(true);
+ espMqttClientAsync* client = reinterpret_cast(a);
+ client->_state = MqttClient::State::connectingTcp2;
+ client->loop();
+}
+
+void espMqttClientAsync::onDataCb(void* a, AsyncClient* c, void* data, size_t len) {
+ (void)c;
+ espMqttClientAsync* client = reinterpret_cast(a);
+ client->_clientAsync.bufData = reinterpret_cast(data);
+ client->_clientAsync.availableData = len;
+ client->loop();
+}
+
+void espMqttClientAsync::onDisconnectCb(void* a, AsyncClient* c) {
+ (void)c;
+ espMqttClientAsync* client = reinterpret_cast(a);
+ client->_state = MqttClient::State::disconnectingTcp2;
+ client->loop();
+}
+
+void espMqttClientAsync::onPollCb(void* a, AsyncClient* c) {
+ (void)c;
+ espMqttClientAsync* client = reinterpret_cast(a);
+ client->loop();
+}
+
+#endif
diff --git a/lib/espMqttClient/src/espMqttClientAsync.h b/lib/espMqttClient/src/espMqttClientAsync.h
new file mode 100644
index 000000000..1b9ed8beb
--- /dev/null
+++ b/lib/espMqttClient/src/espMqttClientAsync.h
@@ -0,0 +1,36 @@
+/*
+Copyright (c) 2022 Bert Melis. All rights reserved.
+
+API is based on the original work of Marvin Roger:
+https://github.com/marvinroger/async-mqtt-client
+
+This work is licensed under the terms of the MIT license.
+For a copy, see or
+the LICENSE file.
+*/
+
+#pragma once
+
+#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
+
+#include "Transport/ClientAsync.h"
+
+#include "MqttClientSetup.h"
+
+class espMqttClientAsync : public MqttClientSetup {
+ public:
+ espMqttClientAsync();
+ bool connect();
+
+ protected:
+ espMqttClientInternals::ClientAsync _clientAsync;
+ static void _setupClient(espMqttClientAsync* c);
+ static void _disconnectClient(espMqttClientAsync* c);
+
+ static void onConnectCb(void* a, AsyncClient* c);
+ static void onDataCb(void* a, AsyncClient* c, void* data, size_t len);
+ static void onDisconnectCb(void* a, AsyncClient* c);
+ static void onPollCb(void* a, AsyncClient* c);
+};
+
+#endif
diff --git a/lib/framework/ESP8266React.h b/lib/framework/ESP8266React.h
index 953232f78..32ca0f55c 100644
--- a/lib/framework/ESP8266React.h
+++ b/lib/framework/ESP8266React.h
@@ -59,7 +59,7 @@ class ESP8266React {
return &_mqttSettingsService;
}
- AsyncMqttClient * getMqttClient() {
+ espMqttClient * getMqttClient() {
return _mqttSettingsService.getMqttClient();
}
diff --git a/lib/framework/MqttPubSub.h b/lib/framework/MqttPubSub.h
deleted file mode 100644
index fe8e56434..000000000
--- a/lib/framework/MqttPubSub.h
+++ /dev/null
@@ -1,163 +0,0 @@
-#ifndef MqttPubSub_h
-#define MqttPubSub_h
-
-#include
-#include
-
-using namespace std::placeholders; // for `_1` etc
-
-#define MQTT_ORIGIN_ID "mqtt"
-
-template
-class MqttConnector {
- protected:
- StatefulService * _statefulService;
- AsyncMqttClient * _mqttClient;
- size_t _bufferSize;
-
- MqttConnector(StatefulService * statefulService, AsyncMqttClient * mqttClient, size_t bufferSize)
- : _statefulService(statefulService)
- , _mqttClient(mqttClient)
- , _bufferSize(bufferSize) {
- _mqttClient->onConnect(std::bind(&MqttConnector::onConnect, this));
- }
-
- virtual void onConnect() = 0;
-
- public:
- inline AsyncMqttClient * getMqttClient() const {
- return _mqttClient;
- }
-};
-
-template
-class MqttPub : virtual public MqttConnector {
- public:
- MqttPub(JsonStateReader stateReader,
- StatefulService * statefulService,
- AsyncMqttClient * mqttClient,
- const String & pubTopic = "",
- size_t bufferSize = DEFAULT_BUFFER_SIZE)
- : MqttConnector(statefulService, mqttClient, bufferSize)
- , _stateReader(stateReader)
- , _pubTopic(pubTopic) {
- MqttConnector::_statefulService->addUpdateHandler([&](const String & originId) { publish(); }, false);
- }
-
- void setPubTopic(const String & pubTopic) {
- _pubTopic = pubTopic;
- publish();
- }
-
- protected:
- virtual void onConnect() {
- publish();
- }
-
- private:
- JsonStateReader _stateReader;
- String _pubTopic;
-
- void publish() {
- if (_pubTopic.length() > 0 && MqttConnector::_mqttClient->connected()) {
- // serialize to json doc
- DynamicJsonDocument json(MqttConnector::_bufferSize);
- JsonObject jsonObject = json.to();
- MqttConnector::_statefulService->read(jsonObject, _stateReader);
-
- // serialize to string
- String payload;
- serializeJson(json, payload);
-
- // publish the payload
- MqttConnector::_mqttClient->publish(_pubTopic.c_str(), 0, false, payload.c_str());
- }
- }
-};
-
-template
-class MqttSub : virtual public MqttConnector {
- public:
- MqttSub(JsonStateUpdater stateUpdater,
- StatefulService * statefulService,
- AsyncMqttClient * mqttClient,
- const String & subTopic = "",
- size_t bufferSize = DEFAULT_BUFFER_SIZE)
- : MqttConnector(statefulService, mqttClient, bufferSize)
- , _stateUpdater(stateUpdater)
- , _subTopic(subTopic) {
- MqttConnector::_mqttClient->onMessage(std::bind(&MqttSub::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
- }
-
- void setSubTopic(const String & subTopic) {
- if (!_subTopic.equals(subTopic)) {
- // unsubscribe from the existing topic if one was set
- if (_subTopic.length() > 0) {
- MqttConnector::_mqttClient->unsubscribe(_subTopic.c_str());
- }
- // set the new topic and re-configure the subscription
- _subTopic = subTopic;
- subscribe();
- }
- }
-
- protected:
- virtual void onConnect() {
- subscribe();
- }
-
- private:
- JsonStateUpdater _stateUpdater;
- String _subTopic;
-
- void subscribe() {
- if (_subTopic.length() > 0) {
- MqttConnector::_mqttClient->subscribe(_subTopic.c_str(), 2);
- }
- }
-
- void onMqttMessage(char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
- // we only care about the topic we are watching in this class
- if (strcmp(_subTopic.c_str(), topic)) {
- return;
- }
-
- // deserialize from string
- DynamicJsonDocument json(MqttConnector::_bufferSize);
- DeserializationError error = deserializeJson(json, payload, len);
- if (!error && json.is()) {
- JsonObject jsonObject = json.as();
- MqttConnector::_statefulService->update(jsonObject, _stateUpdater, MQTT_ORIGIN_ID);
- }
- }
-};
-
-template