Merge pull request #1201 from MichaelDvP/dev2

Merge dev2
This commit is contained in:
Proddy
2023-07-02 15:43:16 +02:00
committed by GitHub
107 changed files with 5114 additions and 3622 deletions

View File

@@ -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)

View File

@@ -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 = () => {
<FRflag style={{ width: 24 }} />
&nbsp;FR
</Button>
<Button size="small" variant={locale === 'it' ? 'contained' : 'outlined'} onClick={() => selectLocale('it')}>
<ITflag style={{ width: 24 }} />
&nbsp;IT
</Button>
<Button size="small" variant={locale === 'nl' ? 'contained' : 'outlined'} onClick={() => selectLocale('nl')}>
<NLflag style={{ width: 24 }} />
&nbsp;NL

View File

@@ -22,6 +22,7 @@ import { AuthenticatedContext } 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';
@@ -86,6 +87,10 @@ const LayoutAuthMenu: FC = () => {
<FRflag style={{ width: 16, verticalAlign: 'middle' }} />
&nbsp;FR
</MenuItem>
<MenuItem key="it" value="it">
<ITflag style={{ width: 16, verticalAlign: 'middle' }} />
&nbsp;IT
</MenuItem>
<MenuItem key="nl" value="nl">
<NLflag style={{ width: 16, verticalAlign: 'middle' }} />
&nbsp;NL

View File

@@ -1,22 +1,24 @@
import CancelIcon from '@mui/icons-material/Cancel';
import CheckIcon from '@mui/icons-material/Check';
import CloseIcon from '@mui/icons-material/Close';
import DeleteIcon from '@mui/icons-material/Delete';
import EditIcon from '@mui/icons-material/Edit';
import PersonAddIcon from '@mui/icons-material/PersonAdd';
import SaveIcon from '@mui/icons-material/Save';
import VpnKeyIcon from '@mui/icons-material/VpnKey';
import WarningIcon from '@mui/icons-material/Warning';
import { Button, IconButton, Box } from '@mui/material';
import { Table, Header, HeaderRow, HeaderCell, Body, Row, Cell } from '@table-library/react-table-library/table';
import { useTheme } from '@table-library/react-table-library/theme';
import { useContext, useState } from 'react';
import { unstable_useBlocker as useBlocker } from 'react-router-dom';
import GenerateToken from './GenerateToken';
import UserForm from './UserForm';
import type { FC } from 'react';
import type { SecuritySettings, User } from 'types';
import * as SecurityApi from 'api/security';
import { ButtonRow, FormLoader, MessageBox, SectionContent } from 'components';
import { ButtonRow, FormLoader, MessageBox, SectionContent, BlockNavigation } from 'components';
import { AuthenticatedContext } from 'contexts/authentication';
import { useI18nContext } from 'i18n/i18n-react';
import { useRest } from 'utils';
@@ -30,9 +32,11 @@ const ManageUsersForm: FC = () => {
const [user, setUser] = useState<User>();
const [creating, setCreating] = useState<boolean>(false);
const [changed, setChanged] = useState<number>(0);
const [generatingToken, setGeneratingToken] = useState<string>();
const authenticatedContext = useContext(AuthenticatedContext);
const blocker = useBlocker(changed !== 0);
const { LL } = useI18nContext();
const table_theme = useTheme({
@@ -85,6 +89,7 @@ const ManageUsersForm: FC = () => {
const removeUser = (toRemove: User) => {
const users = data.users.filter((u) => u.username !== toRemove.username);
updateDataValue({ ...data, users });
setChanged(changed + 1);
};
const createUser = () => {
@@ -110,6 +115,7 @@ const ManageUsersForm: FC = () => {
const users = [...data.users.filter((u) => u.username !== user.username), user];
updateDataValue({ ...data, users });
setUser(undefined);
setChanged(changed + 1);
}
};
@@ -124,6 +130,12 @@ const ManageUsersForm: FC = () => {
const onSubmit = async () => {
await saveData();
await authenticatedContext.refresh();
setChanged(0);
};
const onCancelSubmit = () => {
loadData();
setChanged(0);
};
const user_table = data.users.map((u) => ({ ...u, id: u.username }));
@@ -172,16 +184,30 @@ const ManageUsersForm: FC = () => {
<Box display="flex" flexWrap="wrap">
<Box flexGrow={1} sx={{ '& button': { mt: 2 } }}>
<Button
startIcon={<SaveIcon />}
disabled={saving || noAdminConfigured()}
variant="outlined"
color="primary"
type="submit"
onClick={onSubmit}
>
{LL.UPDATE()}
</Button>
{changed !== 0 && (
<ButtonRow>
<Button
startIcon={<CancelIcon />}
disabled={saving}
variant="outlined"
color="primary"
type="submit"
onClick={onCancelSubmit}
>
{LL.CANCEL()}
</Button>
<Button
startIcon={<WarningIcon color="warning" />}
disabled={saving || noAdminConfigured()}
variant="contained"
color="info"
type="submit"
onClick={onSubmit}
>
{LL.APPLY_CHANGES(changed)}
</Button>
</ButtonRow>
)}
</Box>
<Box flexWrap="nowrap" whiteSpace="nowrap">
@@ -208,6 +234,7 @@ const ManageUsersForm: FC = () => {
return (
<SectionContent title={LL.MANAGE_USERS()} titleGutter>
{blocker ? <BlockNavigation blocker={blocker} /> : null}
{content()}
</SectionContent>
);

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 513 342"><path fill="#FFF" d="M342 0H0v341.3h512V0z"/><path fill="#6DA544" d="M0 0h171v342H0z"/><path fill="#D80027" d="M342 0h171v342H342z"/></svg>

After

Width:  |  Height:  |  Size: 201 B

View File

@@ -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;

View File

@@ -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 = ({
</MenuItem>
))}
</TextField>
) : editItem.u !== DeviceValueUOM.NONE ? (
) : editItem.s || editItem.u !== DeviceValueUOM.NONE ? (
<ValidatedTextField
fieldErrors={fieldErrors}
name="v"
@@ -160,7 +160,7 @@ const DashboarDevicesDialog = ({
type="number"
sx={{ width: '30ch' }}
onChange={updateFormValue}
inputProps={editItem.u ? { min: editItem.m, max: editItem.x, step: editItem.s } : {}}
inputProps={editItem.s ? { min: editItem.m, max: editItem.x, step: editItem.s } : {}}
InputProps={{
startAdornment: <InputAdornment position="start">{setUom(editItem.u)}</InputAdornment>
}}

View File

@@ -381,6 +381,7 @@ const SettingsApplication: FC = () => {
<Divider />
<MenuItem value="de">Deutsch (DE)</MenuItem>
<MenuItem value="fr">Français (FR)</MenuItem>
<MenuItem value="it">Italiano (IT)</MenuItem>
<MenuItem value="nl">Nederlands (NL)</MenuItem>
<MenuItem value="no">Norsk (NO)</MenuItem>
<MenuItem value="pl">Polski (PL)</MenuItem>

View File

@@ -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<AsyncMqttClient*>(obj))->_onConnect(); }, this);
_client.onDisconnect([](void* obj, AsyncClient* c) { (static_cast<AsyncMqttClient*>(obj))->_onDisconnect(); }, this);
// _client.onError([](void* obj, AsyncClient* c, int8_t error) { (static_cast<AsyncMqttClient*>(obj))->_onError(error); }, this);
// _client.onTimeout([](void* obj, AsyncClient* c, uint32_t time) { (static_cast<AsyncMqttClient*>(obj))->_onTimeout(); }, this);
_client.onAck([](void* obj, AsyncClient* c, size_t len, uint32_t time) { (static_cast<AsyncMqttClient*>(obj))->_onAck(len); }, this);
_client.onData([](void* obj, AsyncClient* c, void* data, size_t len) { (static_cast<AsyncMqttClient*>(obj))->_onData(static_cast<char*>(data), len); }, this);
_client.onPoll([](void* obj, AsyncClient* c) { (static_cast<AsyncMqttClient*>(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<uint8_t, SHA1_SIZE> 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<uint8_t, SHA1_SIZE> 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, &currentBytePosition);
break;
case AsyncMqttClientInternals::BufferState::PAYLOAD:
_currentParsedPacket->parsePayload(data, len, &currentBytePosition);
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<const char*>(_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<AsyncMqttClientInternals::PublishOutPacket*>(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<AsyncMqttClientDisconnectReason>(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;
}

View File

@@ -1,6 +0,0 @@
#ifndef SRC_ASYNCMQTTCLIENT_H_
#define SRC_ASYNCMQTTCLIENT_H_
#include "AsyncMqttClient.hpp"
#endif // SRC_ASYNCMQTTCLIENT_H_

View File

@@ -1,182 +0,0 @@
#pragma once
#include <functional>
#include <vector>
#include "Arduino.h"
#ifndef MQTT_MIN_FREE_MEMORY
#define MQTT_MIN_FREE_MEMORY 4096
#endif
#ifdef ESP32
#include <AsyncTCP.h>
#include <freertos/semphr.h>
#elif defined(ESP8266)
#include <ESPAsyncTCP.h>
#else
#error Platform not supported
#endif
#if ASYNC_TCP_SSL_ENABLED
#include <tcp_axtls.h>
#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<std::array<uint8_t, SHA1_SIZE>> _secureServerFingerprints;
#endif
std::vector<AsyncMqttClientInternals::OnConnectUserCallback> _onConnectUserCallbacks;
std::vector<AsyncMqttClientInternals::OnDisconnectUserCallback> _onDisconnectUserCallbacks;
std::vector<AsyncMqttClientInternals::OnSubscribeUserCallback> _onSubscribeUserCallbacks;
std::vector<AsyncMqttClientInternals::OnUnsubscribeUserCallback> _onUnsubscribeUserCallbacks;
std::vector<AsyncMqttClientInternals::OnMessageUserCallback> _onMessageUserCallbacks;
std::vector<AsyncMqttClientInternals::OnPublishUserCallback> _onPublishUserCallbacks;
AsyncMqttClientInternals::ParsingInformation _parsingInformation;
AsyncMqttClientInternals::Packet* _currentParsedPacket;
uint8_t _remainingLengthBufferPosition;
char _remainingLengthBuffer[4];
std::vector<AsyncMqttClientInternals::PendingPubRel> _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();
};

View File

@@ -1,30 +0,0 @@
#pragma once
#include <functional>
#include "DisconnectReasons.hpp"
#include "MessageProperties.hpp"
#include "Errors.hpp"
namespace AsyncMqttClientInternals {
// user callbacks
typedef std::function<void(bool sessionPresent)> OnConnectUserCallback;
typedef std::function<void(AsyncMqttClientDisconnectReason reason)> OnDisconnectUserCallback;
typedef std::function<void(uint16_t packetId, uint8_t qos)> OnSubscribeUserCallback;
typedef std::function<void(uint16_t packetId)> OnUnsubscribeUserCallback;
typedef std::function<void(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)> OnMessageUserCallback;
typedef std::function<void(uint16_t packetId)> OnPublishUserCallback;
typedef std::function<void(uint16_t packetId, AsyncMqttClientError error)> OnErrorUserCallback;
// internal callbacks
typedef std::function<void(bool sessionPresent, uint8_t connectReturnCode)> OnConnAckInternalCallback;
typedef std::function<void()> OnPingRespInternalCallback;
typedef std::function<void(uint16_t packetId, char status)> OnSubAckInternalCallback;
typedef std::function<void(uint16_t packetId)> OnUnsubAckInternalCallback;
typedef std::function<void(char* topic, char* payload, uint8_t qos, bool dup, bool retain, size_t len, size_t index, size_t total, uint16_t packetId)> OnMessageInternalCallback;
typedef std::function<void(uint16_t packetId, uint8_t qos)> OnPublishInternalCallback;
typedef std::function<void(uint16_t packetId)> OnPubRelInternalCallback;
typedef std::function<void(uint16_t packetId)> OnPubAckInternalCallback;
typedef std::function<void(uint16_t packetId)> OnPubRecInternalCallback;
typedef std::function<void(uint16_t packetId)> OnPubCompInternalCallback;
} // namespace AsyncMqttClientInternals

View File

@@ -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
};

View File

@@ -1,6 +0,0 @@
#pragma once
enum class AsyncMqttClientError : uint8_t {
MAX_RETRIES = 0,
OUT_OF_MEMORY = 1
};

View File

@@ -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 <esp32-hal-log.h>
#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

View File

@@ -1,7 +0,0 @@
#pragma once
struct AsyncMqttClientMessageProperties {
uint8_t qos;
bool dup;
bool retain;
};

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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();
}

View File

@@ -1,29 +0,0 @@
#pragma once
#include <vector>
#include <cstring> // 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<uint8_t> _data;
};
} // namespace AsyncMqttClientInternals

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -1,35 +0,0 @@
#pragma once
#include <stdint.h> // uint*_t
#include <stddef.h> // size_t
#include <algorithm> // 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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -1,23 +0,0 @@
#pragma once
#include <cstring> // strlen
#include <vector>
#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<uint8_t> _data;
};
} // namespace AsyncMqttClientInternals

View File

@@ -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();
}

View File

@@ -1,21 +0,0 @@
#pragma once
#include <cstring> // strlen
#include <vector>
#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<uint8_t> _data;
};
} // namespace AsyncMqttClientInternals

View File

@@ -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();
}

View File

@@ -1,21 +0,0 @@
#pragma once
#include <cstring> // strlen
#include <vector>
#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<uint8_t> _data;
};
} // namespace AsyncMqttClientInternals

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,54 @@
# espMqttClient
MQTT client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework.
Aims to be a non-blocking, fully compliant MQTT 3.1.1 client.
![platformio](https://github.com/bertmelis/espMqttClient/actions/workflows/build_platformio.yml/badge.svg)
![cpplint](https://github.com/bertmelis/espMqttClient/actions/workflows/cpplint.yml/badge.svg)
![cppcheck](https://github.com/bertmelis/espMqttClient/actions/workflows/cppcheck.yml/badge.svg)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/bertmelis/library/espMqttClient.svg)](https://registry.platformio.org/libraries/bertmelis/espMqttClient)
# Features
- MQTT 3.1.1 compliant library
- Sending and receiving at all QoS levels
- TCP and TCP/TLS using standard WiFiClient and WiFiClientSecure connections
- Virtually unlimited incoming and outgoing payload sizes
- Readable and understandable code
- Fully async clients available via [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) or [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP) (no TLS supported)
- Supported platforms:
- Espressif ESP8266 and ESP32 using the Arduino framework
- Basic Linux compatibility*. This includes WSL on Windows
> Linux compatibility is mainly for automatic testing. It relies on a quick and dirty Arduino-style `Client` with a POSIX TCP client underneath and Arduino-style `IPAddress` class. These are lacking many features needed for proper Linux support.
# Documentation
See [documentation](https://www.emelis.net/espMqttClient/) and the [examples](examples/).
## Limitations
### MQTT 3.1.1 Compliancy
Outgoing messages and session data are not stored in non-volatile memory. Any events like loss of power or sudden resets result in loss of data. Despite this limitation, one could still consider this library as fully complaint based on the non normative remark in point 4.1.1 of the specification.
### Non-blocking
This library aims to be fully non-blocking. It is however limited by the underlying `WiFiClient` library which is part of the Arduino framework and has a blocking `connect` method. This is not an issue on ESP32 because the call is offloaded to a separate task. On ESP8266 however, connecting will block until succesful or until the connection timeouts.
If you need a fully asynchronous MQTT client, you can use `espMqttClientAsync` which uses AsyncTCP/ESPAsyncTCP under the hood. These underlying libraries do not support TLS (anymore). I will not provide support TLS for the async client.
# Bugs and feature requests
Please use Github's facilities to get in touch.
# About this library
This client wouldn't exist without [Async-mqtt-client](https://github.com/marvinroger/async-mqtt-client). It has been my go-to MQTT client for many years. It was fast, reliable and had features that were non-existing in alternative libraries. However, the underlying async TCP libraries are lacking updates, especially updates related to secure connections. Adapting this library to use up-to-date TCP clients would not be trivial. I eventually decided to write my own MQTT library, from scratch.
The result is an almost non-blocking library with no external dependencies. The library is almost a drop-in replacement for the async-mqtt-client except a few parameter type changes (eg. `uint8_t*` instead of `char*` for payloads).
# License
This library is released under the MIT Licence. A copy is included in the repo.
Parts of this library, most notably the API, are based on [Async MQTT client for ESP8266 and ESP32](https://github.com/marvinroger/async-mqtt-client).

View File

@@ -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 <https://opensource.org/licenses/MIT> 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#if defined(ARDUINO_ARCH_ESP32)
#include <Arduino.h> // 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 <Arduino.h> // millis(), ESP.getFreeHeap();
#if EMC_ESP8266_MULTITHREADING
// This lib doesn't run use multithreading on ESP8266
// _xSemaphore defined as std::atomic<bool>
#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 <chrono> // NOLINT [build/c++11]
#include <thread> // NOLINT [build/c++11] for yield()
#define millis() std::chrono::duration_cast<std::chrono::milliseconds>(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 <mutex> // NOLINT [build/c++11]
#define EMC_SEMAPHORE_TAKE() mtx.lock();
#define EMC_SEMAPHORE_GIVE() mtx.unlock();
#else
#error Target platform not supported
#endif

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#if defined(ARDUINO_ARCH_ESP32)
#include <esp32-hal-log.h>
#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 <Arduino.h>
#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 <iostream>
#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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<const uint8_t *>(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<OutgoingPacket>::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<DisconnectReason>(_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<OutgoingPacket>::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<OutgoingPacket>::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<OutgoingPacket>::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<OutgoingPacket>::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<OutgoingPacket>::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<OutgoingPacket>::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<const espMqttClientTypes::SubscribeReturncode *>(_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<OutgoingPacket>::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<OutgoingPacket>::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<OutgoingPacket>::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);
}
}

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <atomic>
#include <utility>
#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 <typename... Args>
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>(args) ...)) {
emc_log_e("Could not create SUBSCRIBE packet");
packetId = 0;
}
EMC_SEMAPHORE_GIVE();
}
return packetId;
}
template <typename... Args>
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>(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> _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<bool> _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 <typename... Args>
OutgoingPacket(uint32_t t, espMqttClientTypes::Error error, Args&&... args) :
timeSent(t),
packet(error, std::forward<Args>(args) ...) {}
};
espMqttClientInternals::Outbox<OutgoingPacket> _outbox;
size_t _bytesSent;
espMqttClientInternals::Parser _parser;
uint32_t _lastClientActivity;
uint32_t _lastServerActivity;
bool _pingSent;
espMqttClientTypes::DisconnectReason _disconnectReason;
uint16_t _getNextPacketId();
template <typename... Args>
bool _addPacket(Args&&... args) {
espMqttClientTypes::Error error(espMqttClientTypes::Error::SUCCESS);
espMqttClientInternals::Outbox<OutgoingPacket>::Iterator it = _outbox.emplace(0, error, std::forward<Args>(args) ...);
if (it && error == espMqttClientTypes::Error::SUCCESS) return true;
return false;
}
template <typename... Args>
bool _addPacketFront(Args&&... args) {
espMqttClientTypes::Error error(espMqttClientTypes::Error::SUCCESS);
espMqttClientInternals::Outbox<OutgoingPacket>::Iterator it = _outbox.emplaceFront(0, error, std::forward<Args>(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
};

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include "MqttClient.h"
template <typename T>
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<T&>(*this);
}
T& setClientId(const char* clientId) {
_clientId = clientId;
return static_cast<T&>(*this);
}
T& setCleanSession(bool cleanSession) {
_cleanSession = cleanSession;
return static_cast<T&>(*this);
}
T& setCredentials(const char* username, const char* password) {
_username = username;
_password = password;
return static_cast<T&>(*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<T&>(*this);
}
T& setWill(const char* topic, uint8_t qos, bool retain, const char* payload) {
return setWill(topic, qos, retain, reinterpret_cast<const uint8_t*>(payload), strlen(payload));
}
T& setServer(IPAddress ip, uint16_t port) {
_ip = ip;
_port = port;
_useIp = true;
return static_cast<T&>(*this);
}
T& setServer(const char* host, uint16_t port) {
_host = host;
_port = port;
_useIp = false;
return static_cast<T&>(*this);
}
T& setTimeout(uint16_t timeout) {
_timeout = timeout * 1000; // s to ms conversion, will also do 16 to 32 bit conversion
return static_cast<T&>(*this);
}
T& onConnect(espMqttClientTypes::OnConnectCallback callback) {
_onConnectCallback = callback;
return static_cast<T&>(*this);
}
T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) {
_onDisconnectCallback = callback;
return static_cast<T&>(*this);
}
T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) {
_onSubscribeCallback = callback;
return static_cast<T&>(*this);
}
T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) {
_onUnsubscribeCallback = callback;
return static_cast<T&>(*this);
}
T& onMessage(espMqttClientTypes::OnMessageCallback callback) {
_onMessageCallback = callback;
return static_cast<T&>(*this);
}
T& onPublish(espMqttClientTypes::OnPublishCallback callback) {
_onPublishCallback = callback;
return static_cast<T&>(*this);
}
/*
T& onError(espMqttClientTypes::OnErrorCallback callback) {
_onErrorCallback = callback;
return static_cast<T&>(*this);
}
*/
protected:
explicit MqttClientSetup(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1)
: MqttClient(useInternalTask, priority, core) {}
};

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <new> // new (std::nothrow)
#include <utility> // 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 <typename T>
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 <typename... Args>
explicit Node(Args&&... args)
: data(std::forward<Args>(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 <class... Args>
Iterator emplace(Args&&... args) {
Iterator it;
Node* node = new (std::nothrow) Node(std::forward<Args>(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 <class... Args>
Iterator emplaceFront(Args&&... args) {
Iterator it;
Node* node = new (std::nothrow) Node(std::forward<Args>(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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
namespace AsyncMqttClientInternals {
#include <stdint.h>
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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<MQTTPacketType>(_data[0] & 0xF0);
return static_cast<MQTTPacketType>(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<const char*>(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<size_t>(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<uint8_t*>(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<size_t>(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<size_t>(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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <stdint.h>
#include <stddef.h>
#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<typename ... Args>
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<typename ... Args>
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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<char>(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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <algorithm>
#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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <stdint.h>
#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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<uint16_t>(length) >> 8;
dest[1] = static_cast<uint16_t>(length) & 0xFF;
memcpy(&dest[2], source, length);
return 2 + length;
}
} // namespace espMqttClientInternals

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <stdint.h>
#include <cstring> // 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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<const char*>(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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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 <AsyncTCP.h>
#elif defined(ARDUINO_ARCH_ESP8266)
#include <ESPAsyncTCP.h>
#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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#if defined(__linux__)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "ClientSecureSync.h"
#include <lwip/sockets.h> // 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include <WiFiClientSecure.h> // 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "ClientSync.h"
#include <lwip/sockets.h> // 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include <WiFiClient.h> // 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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#if defined(ARDUINO)
#include <IPAddress.h>
#else
#include <stdint.h>
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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <stddef.h> // 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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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

View File

@@ -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 <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <functional>
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<void(bool sessionPresent)> OnConnectCallback;
typedef std::function<void(DisconnectReason reason)> OnDisconnectCallback;
typedef std::function<void(uint16_t packetId, const SubscribeReturncode* returncodes, size_t len)> OnSubscribeCallback;
typedef std::function<void(uint16_t packetId)> OnUnsubscribeCallback;
typedef std::function<void(const MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total)> OnMessageCallback;
typedef std::function<void(uint16_t packetId)> OnPublishCallback;
typedef std::function<size_t(uint8_t* data, size_t maxSize, size_t index)> PayloadCallback;
typedef std::function<void(uint16_t packetId, Error error)> OnErrorCallback;
enum class UseInternalTask {
NO = 0,
YES = 1,
};
} // end namespace espMqttClientTypes

View File

@@ -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 <https://opensource.org/licenses/MIT> 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

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<espMqttClient> {
public:
espMqttClient();
protected:
espMqttClientInternals::ClientSync _client;
};
class espMqttClientSecure : public MqttClientSetup<espMqttClientSecure> {
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<espMqttClient> {
public:
explicit espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask);
explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1);
protected:
espMqttClientInternals::ClientSync _client;
};
class espMqttClientSecure : public MqttClientSetup<espMqttClientSecure> {
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<espMqttClient> {
public:
espMqttClient();
protected:
espMqttClientInternals::ClientPosix _client;
};
#endif

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<espMqttClientAsync*>(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<espMqttClientAsync*>(a);
client->_clientAsync.bufData = reinterpret_cast<uint8_t*>(data);
client->_clientAsync.availableData = len;
client->loop();
}
void espMqttClientAsync::onDisconnectCb(void* a, AsyncClient* c) {
(void)c;
espMqttClientAsync* client = reinterpret_cast<espMqttClientAsync*>(a);
client->_state = MqttClient::State::disconnectingTcp2;
client->loop();
}
void espMqttClientAsync::onPollCb(void* a, AsyncClient* c) {
(void)c;
espMqttClientAsync* client = reinterpret_cast<espMqttClientAsync*>(a);
client->loop();
}
#endif

View File

@@ -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 <https://opensource.org/licenses/MIT> 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<espMqttClientAsync> {
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

View File

@@ -59,7 +59,7 @@ class ESP8266React {
return &_mqttSettingsService;
}
AsyncMqttClient * getMqttClient() {
espMqttClient * getMqttClient() {
return _mqttSettingsService.getMqttClient();
}

View File

@@ -1,163 +0,0 @@
#ifndef MqttPubSub_h
#define MqttPubSub_h
#include <StatefulService.h>
#include <AsyncMqttClient.h>
using namespace std::placeholders; // for `_1` etc
#define MQTT_ORIGIN_ID "mqtt"
template <class T>
class MqttConnector {
protected:
StatefulService<T> * _statefulService;
AsyncMqttClient * _mqttClient;
size_t _bufferSize;
MqttConnector(StatefulService<T> * 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 T>
class MqttPub : virtual public MqttConnector<T> {
public:
MqttPub(JsonStateReader<T> stateReader,
StatefulService<T> * statefulService,
AsyncMqttClient * mqttClient,
const String & pubTopic = "",
size_t bufferSize = DEFAULT_BUFFER_SIZE)
: MqttConnector<T>(statefulService, mqttClient, bufferSize)
, _stateReader(stateReader)
, _pubTopic(pubTopic) {
MqttConnector<T>::_statefulService->addUpdateHandler([&](const String & originId) { publish(); }, false);
}
void setPubTopic(const String & pubTopic) {
_pubTopic = pubTopic;
publish();
}
protected:
virtual void onConnect() {
publish();
}
private:
JsonStateReader<T> _stateReader;
String _pubTopic;
void publish() {
if (_pubTopic.length() > 0 && MqttConnector<T>::_mqttClient->connected()) {
// serialize to json doc
DynamicJsonDocument json(MqttConnector<T>::_bufferSize);
JsonObject jsonObject = json.to<JsonObject>();
MqttConnector<T>::_statefulService->read(jsonObject, _stateReader);
// serialize to string
String payload;
serializeJson(json, payload);
// publish the payload
MqttConnector<T>::_mqttClient->publish(_pubTopic.c_str(), 0, false, payload.c_str());
}
}
};
template <class T>
class MqttSub : virtual public MqttConnector<T> {
public:
MqttSub(JsonStateUpdater<T> stateUpdater,
StatefulService<T> * statefulService,
AsyncMqttClient * mqttClient,
const String & subTopic = "",
size_t bufferSize = DEFAULT_BUFFER_SIZE)
: MqttConnector<T>(statefulService, mqttClient, bufferSize)
, _stateUpdater(stateUpdater)
, _subTopic(subTopic) {
MqttConnector<T>::_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<T>::_mqttClient->unsubscribe(_subTopic.c_str());
}
// set the new topic and re-configure the subscription
_subTopic = subTopic;
subscribe();
}
}
protected:
virtual void onConnect() {
subscribe();
}
private:
JsonStateUpdater<T> _stateUpdater;
String _subTopic;
void subscribe() {
if (_subTopic.length() > 0) {
MqttConnector<T>::_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<T>::_bufferSize);
DeserializationError error = deserializeJson(json, payload, len);
if (!error && json.is<JsonObject>()) {
JsonObject jsonObject = json.as<JsonObject>();
MqttConnector<T>::_statefulService->update(jsonObject, _stateUpdater, MQTT_ORIGIN_ID);
}
}
};
template <class T>
class MqttPubSub : public MqttPub<T>, public MqttSub<T> {
public:
MqttPubSub(JsonStateReader<T> stateReader,
JsonStateUpdater<T> stateUpdater,
StatefulService<T> * statefulService,
AsyncMqttClient * mqttClient,
const String & pubTopic = "",
const String & subTopic = "",
size_t bufferSize = DEFAULT_BUFFER_SIZE)
: MqttConnector<T>(statefulService, mqttClient, bufferSize)
, MqttPub<T>(stateReader, statefulService, mqttClient, pubTopic, bufferSize)
, MqttSub<T>(stateUpdater, statefulService, mqttClient, subTopic, bufferSize) {
}
public:
void configureTopics(const String & pubTopic, const String & subTopic) {
MqttSub<T>::setSubTopic(subTopic);
MqttPub<T>::setPubTopic(pubTopic);
}
protected:
void onConnect() {
MqttSub<T>::onConnect();
MqttPub<T>::onConnect();
}
};
#endif

View File

@@ -33,8 +33,8 @@ MqttSettingsService::MqttSettingsService(AsyncWebServer * server, FS * fs, Secur
, _retainedPassword(nullptr)
, _reconfigureMqtt(false)
, _disconnectedAt(0)
, _disconnectReason(AsyncMqttClientDisconnectReason::TCP_DISCONNECTED)
, _mqttClient() {
, _disconnectReason(espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED)
, _mqttClient(espMqttClientTypes::UseInternalTask::NO) {
WiFi.onEvent(std::bind(&MqttSettingsService::WiFiEvent, this, _1, _2));
_mqttClient.onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, _1));
_mqttClient.onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, _1));
@@ -51,12 +51,13 @@ void MqttSettingsService::begin() {
void MqttSettingsService::loop() {
if (_reconfigureMqtt || (_disconnectedAt && (uint32_t)(uuid::get_uptime() - _disconnectedAt) >= MQTT_RECONNECTION_DELAY)) {
// reconfigure MQTT client
configureMqtt();
// clear the reconnection flags
_disconnectedAt = uuid::get_uptime();
if (configureMqtt()) {
_disconnectedAt = 0;
}
_reconfigureMqtt = false;
_disconnectedAt = 0;
}
_mqttClient.loop();
}
bool MqttSettingsService::isEnabled() {
@@ -71,22 +72,27 @@ const char * MqttSettingsService::getClientId() {
return _mqttClient.getClientId();
}
AsyncMqttClientDisconnectReason MqttSettingsService::getDisconnectReason() {
espMqttClientTypes::DisconnectReason MqttSettingsService::getDisconnectReason() {
return _disconnectReason;
}
AsyncMqttClient * MqttSettingsService::getMqttClient() {
espMqttClient * MqttSettingsService::getMqttClient() {
return &_mqttClient;
}
void MqttSettingsService::onMqttConnect(bool sessionPresent) {
// _disconnectedAt = 0;
emsesp::EMSESP::mqtt_.on_connect();
// emsesp::EMSESP::logger().info("Connected to MQTT, %s", (sessionPresent) ? ("with persistent session") : ("without persistent session"));
}
void MqttSettingsService::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
void MqttSettingsService::onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
// emsesp::EMSESP::logger().info("Disconnected from MQTT reason: %d", (uint8_t)reason);
_disconnectReason = reason;
_disconnectedAt = uuid::get_uptime();
if (!_disconnectedAt) {
_disconnectedAt = uuid::get_uptime();
}
emsesp::EMSESP::mqtt_.on_disconnect(reason);
}
void MqttSettingsService::onConfigUpdated() {
@@ -112,7 +118,7 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
case ARDUINO_EVENT_ETH_DISCONNECTED:
if (_state.enabled) {
// emsesp::EMSESP::logger().info("Network connection dropped, stopping MQTT client");
_mqttClient.disconnect();
_mqttClient.disconnect(true);
}
break;
@@ -121,9 +127,9 @@ void MqttSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
}
}
void MqttSettingsService::configureMqtt() {
bool MqttSettingsService::configureMqtt() {
// disconnect if connected
_mqttClient.disconnect();
_mqttClient.disconnect(true);
// only connect if WiFi is connected and MQTT is enabled
if (_state.enabled && emsesp::EMSESP::system_.network_connected() && !_state.host.isEmpty()) {
// emsesp::EMSESP::logger().info("Configuring MQTT client");
@@ -137,11 +143,11 @@ void MqttSettingsService::configureMqtt() {
_mqttClient.setClientId(retainCstr(_state.clientId.c_str(), &_retainedClientId));
_mqttClient.setKeepAlive(_state.keepAlive);
_mqttClient.setCleanSession(_state.cleanSession);
_mqttClient.setMaxTopicLength(FACTORY_MQTT_MAX_TOPIC_LENGTH); // hardcode. We don't take this from the settings anymore.
_mqttClient.connect();
return _mqttClient.connect();
// } else {
// emsesp::EMSESP::logger().info("Error configuring MQTT client");
}
return false;
}
void MqttSettings::read(MqttSettings & settings, JsonObject & root) {

View File

@@ -4,7 +4,7 @@
#include <StatefulService.h>
#include <HttpEndpoint.h>
#include <FSPersistence.h>
#include <AsyncMqttClient.h>
#include <espMqttClient.h>
#include <ESPUtils.h>
#include <uuid/common.h>
@@ -104,13 +104,13 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
MqttSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
~MqttSettingsService();
void begin();
void loop();
bool isEnabled();
bool isConnected();
const char * getClientId();
AsyncMqttClientDisconnectReason getDisconnectReason();
AsyncMqttClient * getMqttClient();
void begin();
void loop();
bool isEnabled();
bool isConnected();
const char * getClientId();
espMqttClientTypes::DisconnectReason getDisconnectReason();
espMqttClient * getMqttClient();
protected:
void onConfigUpdated();
@@ -120,7 +120,7 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
FSPersistence<MqttSettings> _fsPersistence;
// Pointers to hold retained copies of the mqtt client connection strings.
// This is required as AsyncMqttClient holds references to the supplied connection strings.
// This is required as espMqttClient holds references to the supplied connection strings.
char * _retainedHost;
char * _retainedClientId;
char * _retainedUsername;
@@ -131,15 +131,15 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
unsigned long _disconnectedAt;
// connection status
AsyncMqttClientDisconnectReason _disconnectReason;
espMqttClientTypes::DisconnectReason _disconnectReason;
// the MQTT client instance
AsyncMqttClient _mqttClient;
espMqttClient _mqttClient;
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void configureMqtt();
void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason);
bool configureMqtt();
};
#endif

View File

@@ -602,6 +602,19 @@ void Command::show_all(uuid::console::Shell & shell) {
shell.print(COLOR_RESET);
show(shell, EMSdevice::DeviceType::SYSTEM, true);
// show Custom
if (EMSESP::webEntityService.has_commands()) {
shell.print(COLOR_BOLD_ON);
shell.print(COLOR_YELLOW);
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::CUSTOM));
shell.println(COLOR_RESET);
shell.printf(" info: %slists all values %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED);
shell.println(COLOR_RESET);
shell.printf(" commands: %slists all commands %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED);
shell.print(COLOR_RESET);
show(shell, EMSdevice::DeviceType::CUSTOM, true);
}
// show scheduler
if (EMSESP::webSchedulerService.has_commands()) {
shell.print(COLOR_BOLD_ON);

View File

@@ -136,6 +136,8 @@ Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_i
summer_typeids = {0x02AF, 0x02B0, 0x02B1, 0x02B2, 0x02B3, 0x02B4, 0x02B5, 0x02B6};
curve_typeids = {0x029B, 0x029C, 0x029D, 0x029E, 0x029F, 0x02A0, 0x02A1, 0x02A2};
summer2_typeids = {0x0471, 0x0472, 0x0473, 0x0474, 0x0475, 0x0476, 0x0477, 0x0478};
hp_typeids = {0x0467, 0x0468, 0x0469, 0x046A};
hpmode_typeids = {0x0291, 0x0292, 0x0293, 0x0294};
for (uint8_t i = 0; i < monitor_typeids.size(); i++) {
register_telegram_type(monitor_typeids[i], "RC300Monitor", false, MAKE_PF_CB(process_RC300Monitor));
register_telegram_type(set_typeids[i], "RC300Set", false, MAKE_PF_CB(process_RC300Set));
@@ -145,6 +147,8 @@ Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_i
}
for (uint8_t i = 0; i < set2_typeids.size(); i++) {
register_telegram_type(set2_typeids[i], "RC300Set2", false, MAKE_PF_CB(process_RC300Set2));
register_telegram_type(hp_typeids[i], "HPSet", false, MAKE_PF_CB(process_HPSet));
register_telegram_type(hpmode_typeids[i], "HPMode", true, MAKE_PF_CB(process_HPMode));
}
register_telegram_type(0x2F5, "RC300WWmode", true, MAKE_PF_CB(process_RC300WWmode));
register_telegram_type(0x31B, "RC300WWtemp", true, MAKE_PF_CB(process_RC300WWtemp));
@@ -306,6 +310,25 @@ std::shared_ptr<Thermostat::HeatingCircuit> Thermostat::heating_circuit(std::sha
}
}
// not found, search heatpump message types
if (hc_num == 0) {
for (uint8_t i = 0; i < hp_typeids.size(); i++) {
if (hp_typeids[i] == telegram->type_id) {
hc_num = i + 1;
break;
}
}
}
if (hc_num == 0) {
for (uint8_t i = 0; i < hpmode_typeids.size(); i++) {
if (hpmode_typeids[i] == telegram->type_id) {
hc_num = i + 1;
break;
}
}
}
// not found, search device-id types for remote thermostats
if (hc_num == 0 && telegram->src >= 0x18 && telegram->src <= 0x1F) {
hc_num = telegram->src - 0x17;
@@ -383,6 +406,9 @@ std::shared_ptr<Thermostat::HeatingCircuit> Thermostat::heating_circuit(std::sha
if (summer2_typeids.size()) {
toggle_fetch(summer2_typeids[hc_num - 1], toggle_);
}
if (hp_typeids.size()) {
toggle_fetch(hp_typeids[hc_num - 1], toggle_);
}
return new_hc; // return back point to new HC object
}
@@ -993,6 +1019,7 @@ void Thermostat::process_RC300Set(std::shared_ptr<const Telegram> telegram) {
has_update(telegram, hc->reducetemp, 9);
has_update(telegram, hc->noreducetemp, 12);
has_update(telegram, hc->remoteseltemp, 17); // see https://github.com/emsesp/EMS-ESP32/issues/590
has_update(telegram, hc->cooling, 28);
}
// types 0x2AF ff
@@ -1128,6 +1155,26 @@ void Thermostat::process_RC300Floordry(std::shared_ptr<const Telegram> telegram)
has_update(telegram, floordrytemp_, 1);
}
// 0x291 ff. HP mode
void Thermostat::process_HPMode(std::shared_ptr<const Telegram> telegram) {
std::shared_ptr<Thermostat::HeatingCircuit> hc = heating_circuit(telegram);
if (hc == nullptr) {
return;
}
has_update(telegram, hc->hpmode, 0);
}
// 0x467 ff HP settings
void Thermostat::process_HPSet(std::shared_ptr<const Telegram> telegram) {
std::shared_ptr<Thermostat::HeatingCircuit> hc = heating_circuit(telegram);
if (hc == nullptr) {
return;
}
has_update(telegram, hc->dewoffset, 0); // 7-35°C
has_update(telegram, hc->roomtempdiff, 3); // 1-10K
has_update(telegram, hc->hpminflowtemp, 4); // 2-10K
}
// type 0x41 - data from the RC30 thermostat(0x10) - 14 bytes long
// RC30Monitor(0x41), data: 80 20 00 AC 00 00 00 02 00 05 09 00 AC 00
void Thermostat::process_RC30Monitor(std::shared_ptr<const Telegram> telegram) {
@@ -1447,6 +1494,62 @@ void Thermostat::process_RCErrorMessage(std::shared_ptr<const Telegram> telegram
*
*/
// hp mode RC300
bool Thermostat::set_roomtempdiff(const char * value, const int8_t id) {
uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id;
std::shared_ptr<Thermostat::HeatingCircuit> hc = heating_circuit(hc_num);
if (hc == nullptr) {
return false;
}
int v;
if (Helpers::value2number(value, v)) {
write_command(hp_typeids[hc->hc()], 3, v, hp_typeids[hc->hc()]);
return true;
}
return false;
}
bool Thermostat::set_dewoffset(const char * value, const int8_t id) {
uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id;
std::shared_ptr<Thermostat::HeatingCircuit> hc = heating_circuit(hc_num);
if (hc == nullptr) {
return false;
}
int v;
if (Helpers::value2number(value, v)) {
write_command(hp_typeids[hc->hc()], 4, v, hp_typeids[hc->hc()]);
return true;
}
return false;
}
bool Thermostat::set_hpminflowtemp(const char * value, const int8_t id) {
uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id;
std::shared_ptr<Thermostat::HeatingCircuit> hc = heating_circuit(hc_num);
if (hc == nullptr) {
return false;
}
int v;
if (Helpers::value2temperature(value, v)) {
write_command(hp_typeids[hc->hc()], 0, v, hp_typeids[hc->hc()]);
return true;
}
return false;
}
bool Thermostat::set_hpmode(const char * value, const int8_t id) {
uint8_t hc_num = (id == -1) ? AUTO_HEATING_CIRCUIT : id;
std::shared_ptr<Thermostat::HeatingCircuit> hc = heating_circuit(hc_num);
if (hc == nullptr) {
return false;
}
uint8_t v;
if (!Helpers::value2enum(value, v, FL_(enum_hpmode))) {
return false;
}
write_command(hpmode_typeids[hc->hc()], 0, v, hpmode_typeids[hc->hc()]);
return true;
}
// 0xBB Hybrid pump
bool Thermostat::set_hybridStrategy(const char * value, const int8_t id) {
uint8_t v;
@@ -1868,6 +1971,20 @@ bool Thermostat::set_wwprio(const char * value, const int8_t id) {
return true;
}
// set cooling
bool Thermostat::set_cooling(const char * value, const int8_t id) {
std::shared_ptr<Thermostat::HeatingCircuit> hc = heating_circuit((id == -1) ? AUTO_HEATING_CIRCUIT : id);
if (hc == nullptr) {
return false;
}
bool b;
if (!Helpers::value2bool(value, b)) {
return false;
}
write_command(set_typeids[hc->hc()], 28, b ? 0x01 : 0x00, set_typeids[hc->hc()]);
return true;
}
// sets the thermostat ww circulation working mode, where mode is a string
bool Thermostat::set_wwcircmode(const char * value, const int8_t id) {
@@ -4076,6 +4193,13 @@ void Thermostat::register_device_values_hc(std::shared_ptr<Thermostat::HeatingCi
register_device_value(tag, &hc->noreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp));
register_device_value(tag, &hc->reducetemp, DeviceValueType::INT, FL_(reducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_reducetemp));
register_device_value(tag, &hc->wwprio, DeviceValueType::BOOL, FL_(wwprio), DeviceValueUOM::NONE, MAKE_CF_CB(set_wwprio));
register_device_value(tag, &hc->cooling, DeviceValueType::BOOL, FL_(hpcooling), DeviceValueUOM::NONE, MAKE_CF_CB(set_cooling));
register_device_value(tag, &hc->hpmode, DeviceValueType::ENUM, FL_(enum_hpmode), FL_(hpmode), DeviceValueUOM::NONE, MAKE_CF_CB(set_hpmode));
register_device_value(tag, &hc->dewoffset, DeviceValueType::UINT, FL_(dewoffset), DeviceValueUOM::K, MAKE_CF_CB(set_dewoffset));
register_device_value(tag, &hc->roomtempdiff, DeviceValueType::UINT, FL_(roomtempdiff), DeviceValueUOM::K, MAKE_CF_CB(set_roomtempdiff));
register_device_value(tag, &hc->hpminflowtemp, DeviceValueType::UINT, FL_(hpminflowtemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_hpminflowtemp));
break;
case EMS_DEVICE_FLAG_CRF:
register_device_value(tag, &hc->mode, DeviceValueType::ENUM, FL_(enum_mode5), FL_(mode), DeviceValueUOM::NONE);
@@ -4242,7 +4366,7 @@ void Thermostat::register_device_values_hc(std::shared_ptr<Thermostat::HeatingCi
MAKE_CF_CB(set_tempautotemp),
0,
30);
register_device_value(tag, &hc->noreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp), -30, 10);
register_device_value(tag, &hc->noreducetemp, DeviceValueType::INT, FL_(noreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_noreducetemp), -31, 10);
register_device_value(tag, &hc->reducetemp, DeviceValueType::INT, FL_(reducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_reducetemp), -20, 10);
register_device_value(tag, &hc->vacreducetemp, DeviceValueType::INT, FL_(vacreducetemp), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_vacreducetemp), -20, 10);
register_device_value(

View File

@@ -89,6 +89,12 @@ class Thermostat : public EMSdevice {
uint16_t reduceminutes; // remaining minutes to night->day
// FW100 temperature
uint8_t roomsensor; // 1-intern, 2-extern, 3-autoselect the lower value
// hp
uint8_t dewoffset;
uint8_t roomtempdiff;
uint8_t hpminflowtemp;
uint8_t hpmode;
uint8_t cooling;
uint8_t hc_num() const {
return hc_num_;
@@ -170,6 +176,8 @@ class Thermostat : public EMSdevice {
std::vector<uint16_t> summer_typeids;
std::vector<uint16_t> summer2_typeids;
std::vector<uint16_t> curve_typeids;
std::vector<uint16_t> hp_typeids;
std::vector<uint16_t> hpmode_typeids;
// standard for all thermostats
char status_[20]; // online or offline
@@ -392,6 +400,8 @@ class Thermostat : public EMSdevice {
void process_RemoteTemp(std::shared_ptr<const Telegram> telegram);
void process_RemoteHumidity(std::shared_ptr<const Telegram> telegram);
void process_RemoteCorrection(std::shared_ptr<const Telegram> telegram);
void process_HPSet(std::shared_ptr<const Telegram> telegram);
void process_HPMode(std::shared_ptr<const Telegram> telegram);
// internal helper functions
bool set_mode_n(const uint8_t mode, const uint8_t hc_num);
@@ -551,6 +561,12 @@ class Thermostat : public EMSdevice {
bool set_pvEnableWw(const char * value, const int8_t id);
bool set_pvRaiseHeat(const char * value, const int8_t id);
bool set_pvLowerCool(const char * value, const int8_t id);
bool set_roomtempdiff(const char * value, const int8_t id);
bool set_dewoffset(const char * value, const int8_t id);
bool set_hpminflowtemp(const char * value, const int8_t id);
bool set_hpmode(const char * value, const int8_t id);
bool set_cooling(const char * value, const int8_t id);
};
} // namespace emsesp

View File

@@ -29,6 +29,7 @@ Ventilation::Ventilation(uint8_t device_type, uint8_t device_id, uint8_t product
register_telegram_type(0x585, "Blowerspeed", false, MAKE_PF_CB(process_BlowerMessage));
register_telegram_type(0x583, "VentilationMonitor", false, MAKE_PF_CB(process_MonitorMessage));
register_telegram_type(0x5D9, "Airquality", false, MAKE_PF_CB(process_VOCMessage));
register_telegram_type(0x587, "Bypass", false, MAKE_PF_CB(process_BypassMessage));
// register_telegram_type(0x5, "VentilationSet", true, MAKE_PF_CB(process_SetMessage));
register_device_value(DeviceValueTAG::TAG_DEVICE_DATA,
@@ -43,8 +44,9 @@ Ventilation::Ventilation(uint8_t device_type, uint8_t device_id, uint8_t product
register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &ventInSpeed_, DeviceValueType::UINT, FL_(ventInSpeed), DeviceValueUOM::PERCENT);
register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &ventOutSpeed_, DeviceValueType::UINT, FL_(ventOutSpeed), DeviceValueUOM::PERCENT);
register_device_value(
DeviceValueTAG::TAG_DEVICE_DATA, &mode_, DeviceValueType::ENUM, FL_(enum_ventMode), FL_(ventInSpeed), DeviceValueUOM::NONE, MAKE_CF_CB(set_ventMode));
register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &voc_, DeviceValueType::USHORT, DeviceValueNumOp::DV_NUMOP_DIV10, FL_(airquality), DeviceValueUOM::NONE);
DeviceValueTAG::TAG_DEVICE_DATA, &mode_, DeviceValueType::ENUM, FL_(enum_ventMode), FL_(ventMode), DeviceValueUOM::NONE, MAKE_CF_CB(set_ventMode));
register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &voc_, DeviceValueType::USHORT, FL_(airquality), DeviceValueUOM::NONE);
register_device_value(DeviceValueTAG::TAG_DEVICE_DATA, &bypass_, DeviceValueType::BOOL, FL_(bypass), DeviceValueUOM::NONE, MAKE_CF_CB(set_bypass));
}
// message
@@ -81,6 +83,11 @@ void Ventilation::process_ModeMessage(std::shared_ptr<const Telegram> telegram)
has_enumupdate(telegram, mode_, 0, -1);
}
// message 0x0587, data: 01 00
void Ventilation::process_BypassMessage(std::shared_ptr<const Telegram> telegram) {
has_update(telegram, bypass_, 1);
}
bool Ventilation::set_ventMode(const char * value, const int8_t id) {
uint8_t v;
if (!Helpers::value2enum(value, v, FL_(enum_ventMode))) {
@@ -99,4 +106,13 @@ bool Ventilation::set_filter(const char * value, const int8_t id) {
return true;
}
bool Ventilation::set_bypass(const char * value, const int8_t id) {
bool b;
if (!Helpers::value2bool(value, b)) {
return false;
}
write_command(0x55C, 1, b ? 1 : 0, 0x587);
return true;
}
} // namespace emsesp

View File

@@ -34,7 +34,7 @@ class Ventilation : public EMSdevice {
int16_t inEx_;
int16_t outEx_;
uint16_t voc_;
// uint8_t bypass_;
uint8_t bypass_;
// uint16_t filterRemain_;
uint8_t ventInSpeed_;
uint8_t ventOutSpeed_;
@@ -45,8 +45,10 @@ class Ventilation : public EMSdevice {
void process_ModeMessage(std::shared_ptr<const Telegram> telegram); // 0x56B
void process_BlowerMessage(std::shared_ptr<const Telegram> telegram); // 0x56B
void process_VOCMessage(std::shared_ptr<const Telegram> telegram); // 0x56B
void process_BypassMessage(std::shared_ptr<const Telegram> telegram); // 0x56B
bool set_ventMode(const char * value, const int8_t id);
bool set_bypass(const char * value, const int8_t id);
bool set_filter(const char * value, const int8_t id);

View File

@@ -1616,7 +1616,7 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c
: (dv.uom == DeviceValueUOM::DEGREES) ? 2
: (dv.uom == DeviceValueUOM::DEGREES_R) ? 1
: 0;
char val[10];
char val[10] = {'\0'};
if (dv.type == DeviceValueType::INT) {
json[name] = serialized(Helpers::render_value(val, *(int8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
} else if (dv.type == DeviceValueType::UINT) {
@@ -1645,7 +1645,7 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c
Helpers::translated_word(FL_(minutes)));
json[name] = time_s;
} else {
json[name] = time_value;
json[name] = serialized(Helpers::render_value(val, time_value, 1));
}
}
@@ -1657,18 +1657,16 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c
// check for value outside min/max range and adapt the limits to avoid HA complains
// Should this also check for api output?
if ((output_target == OUTPUT_TARGET::MQTT) && (dv.min != 0 || dv.max != 0)) {
if (json[name].is<float>() || json[name].is<int>()) {
int v = json[name];
if (fahrenheit) {
v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C
}
if (v < dv.min) {
dv.min = v;
dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED);
} else if (v > dv.max) {
dv.max = v;
dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED);
}
int v = Helpers::atoint(val);
if (fahrenheit) {
v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C
}
if (v < dv.min) {
dv.min = v;
dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED);
} else if (v > dv.max) {
dv.max = v;
dv.remove_state(DeviceValueState::DV_HA_CONFIG_CREATED);
}
}
}
@@ -1689,30 +1687,33 @@ void EMSdevice::mqtt_ha_entity_config_create() {
for (auto & dv : devicevalues_) {
if (!strcmp(dv.short_name, FL_(haclimate)[0]) && !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE) && dv.has_state(DeviceValueState::DV_ACTIVE)) {
if (*(int8_t *)(dv.value_p) == 1 && (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) || dv.has_state(DeviceValueState::DV_HA_CLIMATE_NO_RT))) {
dv.remove_state(DeviceValueState::DV_HA_CLIMATE_NO_RT);
dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED);
Mqtt::publish_ha_climate_config(dv.tag, true, false, dv.min, dv.max); // roomTemp
if (Mqtt::publish_ha_climate_config(dv.tag, true, false, dv.min, dv.max)) { // roomTemp
dv.remove_state(DeviceValueState::DV_HA_CLIMATE_NO_RT);
dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED);
}
} else if (*(int8_t *)(dv.value_p) == 0
&& (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) || !dv.has_state(DeviceValueState::DV_HA_CLIMATE_NO_RT))) {
dv.add_state(DeviceValueState::DV_HA_CLIMATE_NO_RT);
dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED);
Mqtt::publish_ha_climate_config(dv.tag, false, false, dv.min, dv.max); // no roomTemp
if (Mqtt::publish_ha_climate_config(dv.tag, false, false, dv.min, dv.max)) { // no roomTemp
dv.add_state(DeviceValueState::DV_HA_CLIMATE_NO_RT);
dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED);
}
}
}
if (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) && (dv.type != DeviceValueType::CMD) && dv.has_state(DeviceValueState::DV_ACTIVE)
&& !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE)) {
// create_device_config is only done once for the EMS device. It can added to any entity, so we take the first
Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config);
dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED);
create_device_config = false; // only create the main config once
}
if (Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config)) {
dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED);
create_device_config = false; // only create the main config once
}
#ifndef EMSESP_STANDALONE
if (ESP.getFreeHeap() < (65 * 1024)) {
break;
}
// always create minimum one config
if (ESP.getMaxAllocHeap() < (6 * 1024) || (!emsesp::EMSESP::system_.PSram() && ESP.getFreeHeap() < (65 * 1024))) {
break;
}
#endif
}
}
ha_config_done(!create_device_config);

View File

@@ -493,7 +493,7 @@ void EMSESP::publish_all_loop() {
}
// wait for free queue before sending next message, HA-messages are also queued
if (!Mqtt::is_empty()) {
if (Mqtt::publish_queued() > 0) {
return;
}
@@ -597,6 +597,7 @@ void EMSESP::publish_other_values() {
publish_device_values(EMSdevice::DeviceType::SWITCH);
publish_device_values(EMSdevice::DeviceType::HEATPUMP);
publish_device_values(EMSdevice::DeviceType::HEATSOURCE);
publish_device_values(EMSdevice::DeviceType::VENTILATION);
// other devices without values yet
// publish_device_values(EMSdevice::DeviceType::GATEWAY);
// publish_device_values(EMSdevice::DeviceType::CONNECT);
@@ -1297,6 +1298,9 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
LOG_ERROR("Last Tx write rejected by host");
txservice_.send_poll(); // close the bus
}
} else if (tx_state == Telegram::Operation::TX_READ && length == 1) {
EMSbus::tx_state(Telegram::Operation::TX_READ); // reset Tx wait state
return;
} else if (tx_state == Telegram::Operation::TX_READ) {
// got a telegram with data in it. See if the src/dest matches that from the last one we sent and continue to process it
uint8_t src = data[0];

View File

@@ -292,6 +292,7 @@ MAKE_ENUM(enum_summermode, FL_(summer), FL_(auto), FL_(winter))
MAKE_ENUM(enum_hpoperatingmode, FL_(off), FL_(auto), FL_(heating), FL_(cooling))
MAKE_ENUM(enum_summer, FL_(winter), FL_(summer))
MAKE_ENUM(enum_operatingstate, FL_(heating), FL_(off), FL_(cooling))
MAKE_ENUM(enum_hpmode, FL_(heating), FL_(cooling), FL_(heatandcool))
MAKE_ENUM(enum_mode, FL_(manual), FL_(auto)) // RC100, RC300, RC310
MAKE_ENUM(enum_mode2, FL_(off), FL_(manual), FL_(auto)) // RC20, RC30

Some files were not shown because too many files have changed in this diff Show More