Both limits were 2047 bytes. An answer repeats the correlationToken of its directive, which is most of a large directive, and adds 140 to 170 bytes for every property. So a directive between about 1,750 and 2,047 bytes was accepted, the sketch acted on it, and the answer was refused: the lamp switched and Alexa reported a device that does not respond.
The limit for messages now follows the limit for directives unless it is set: 2047 + 1024 = 3071 bytes, room for six properties on top of the largest directive. Both defines moved to src/AlexaLimits.h, next to each other, with the relation in the comment. The limit also applies to the discovery object of a device, which may now be 3071 bytes.
For a sketch: a report between 2,048 and 3,071 bytes is sent, where it was refused with an error. A project that sets ALEX2ESP_MAX_MESSAGE keeps its value.
Measured with the bridge built for the host against a fake MQTT client (not in the repository), a TurnOn of 2,047 bytes answered with two properties:
8ea7778 handler ran, send() false, "2374 bytes not sent, the limit is 2047"
this commit handler ran, send() true, 2,374 bytes published
Tests: test_largest_directive_can_be_answered puts a directive of ALEX2ESP_MAX_DIRECTIVE bytes through the receive buffer, builds the Response of a lamp to it and checks it against ALEX2ESP_MAX_MESSAGE, also with six properties. 33 host tests pass.
examples/basicLight.cpp for d1_mini, static RAM / flash in bytes: 34,116 / 336,757 -> 34,116 / 336,757, no warnings.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
126 lines
5.8 KiB
C++
126 lines
5.8 KiB
C++
/*
|
|
* @title Alex2ESP Library
|
|
* @author David
|
|
* @license MIT
|
|
* @contributors chaos511
|
|
*
|
|
* @description Companion library of the Alex2MQTT Alexa skill: the devices a sketch declares become Alexa
|
|
* endpoints through the MQTT broker at alex2mqtt.stormysdream.club.
|
|
*
|
|
* <root>/discover in answered with one discovery object per device on <root>/discover_r
|
|
* <root>/<endpointId>/alexaDirective in the directive, handed to the device's ReportState / Event handler
|
|
* <root>/<endpointId>/alexaResponce out the report the handler built with buildStatusMessage()
|
|
*/
|
|
|
|
#ifndef ALEX2ESP_H
|
|
#define ALEX2ESP_H
|
|
|
|
#include <Arduino.h>
|
|
#include <AsyncMqttClient.h>
|
|
#include <ArduinoJson.h>
|
|
#include "AlexaBridgeLogic.h"
|
|
#include "AlexaDevice.h"
|
|
#include "AlexaInterface.h"
|
|
#include "AlexaLimits.h"
|
|
#include "AlexaLog.h"
|
|
#include "AlexaTransport.h"
|
|
#include "AlexaUtils.h"
|
|
#include <deque>
|
|
|
|
enum class Alex2ESPState
|
|
{
|
|
UNINITIALIZED, // begin() has not been called
|
|
INITIALIZED, // begin() has been called; the first connect waits for the clock
|
|
CONNECTING,
|
|
SUBSCRIBING, // session open, the subscriptions are not acknowledged yet
|
|
CONNECTED, // subscribed: discovery requests and directives arrive
|
|
DISCONNECTED
|
|
};
|
|
|
|
class Alex2ESP : public AlexaTransport
|
|
{
|
|
public:
|
|
// Constructor
|
|
Alex2ESP();
|
|
|
|
// Begin function for initialization: MQTT username, MQTT password, root topic (the same order as alex2node).
|
|
// The username and the password are not copied: they have to stay valid for as long as the client is used.
|
|
// Starts SNTP; loop() opens the MQTT session once the clock is set, or after 5 s without an answer.
|
|
void begin(const char *username, const char *password, const char *rootTopic);
|
|
Alex2ESPState getState() const;
|
|
|
|
// Call from the sketch's loop(): connects, answers discovery requests and hands directives to the devices.
|
|
// It does not block; the handlers of the sketch run inside it.
|
|
void loop();
|
|
|
|
AsyncMqttClientDisconnectReason getDisconnectReason() const;
|
|
|
|
// Returns the device with this endpointId, creating it on first use. The pointer stays valid for the lifetime
|
|
// of the client: devices live in a std::deque, which never relocates its elements when another one is added.
|
|
// Call it after begin(): a device takes the root topic of its reports when it is created.
|
|
AlexaDevice *getDevice(const String &name, const String &endpointId);
|
|
|
|
// What the library prints on Serial: AlexaLogLevel::NONE, ERROR, INFO (the default) or DEBUG
|
|
void setLogLevel(AlexaLogLevel level);
|
|
|
|
// false before begin(): the sketch sets the clock itself (its own configTime() with a time zone, an RTC).
|
|
// begin() then leaves SNTP alone; the library only reads time().
|
|
void setTimeSource(bool useSntp);
|
|
|
|
// AlexaTransport: what the devices' reports are sent and stamped with
|
|
AlexaSendResult publish(const char *topic, JsonDocument &doc) override;
|
|
void timestamp(char *buffer, size_t size) override;
|
|
|
|
private:
|
|
static const unsigned long CLOCK_WAIT_MS = 5000; // How long the first connect waits for SNTP
|
|
static const unsigned long RECONNECT_INTERVAL_MS = 5000;
|
|
static const unsigned long DISCOVERY_WINDOW_MS = 5000; // How long the backend keeps collecting a discovery answer
|
|
static const unsigned long DISCOVERY_RETRY_MS = 20; // Pause before a refused discovery publish is tried again
|
|
|
|
AsyncMqttClient mqttClient; // MQTT client instance
|
|
String rootTopic; // Root topic for communication
|
|
String discoverTopic; // The topic we listen on for discovery messages
|
|
String discoverTopicSend; // The topic we send discovery messages
|
|
String directiveFilter; // The subscription that delivers the directives of every endpoint
|
|
|
|
std::deque<AlexaDevice> devices; // Collection of devices (deque: pointers handed out by getDevice stay valid)
|
|
|
|
Alex2ESPState state;
|
|
AsyncMqttClientDisconnectReason disconnectReason;
|
|
bool useSntp;
|
|
unsigned long beginTime; // millis() when begin() ran
|
|
unsigned long lastReconnectTime;
|
|
uint16_t discoverSubscription; // Packet ids of the two SUBSCRIBEs, to match their acknowledgements
|
|
uint16_t directiveSubscription;
|
|
uint8_t subscriptionsPending;
|
|
|
|
bool discoveryRequested; // Set by the MQTT callback, taken by loop()
|
|
bool discoveryActive; // An answer is going out
|
|
bool discoveryDeferred; // The answer had to pause: the MQTT client or the heap was full
|
|
size_t discoveryNext; // Next device to announce
|
|
size_t discoveryAnnounced; // Devices announced in this answer
|
|
unsigned long discoveryStarted; // millis() when the Discover arrived
|
|
unsigned long discoveryLastAttempt; // millis() of the publish that was refused
|
|
|
|
AlexaDirectiveBuffer directive; // The directive that waits for loop(), or is still arriving
|
|
AlexaRecentIds recentIds; // messageIds of the last directives handled
|
|
|
|
// Internal event handlers (called by the MQTT client from the network context: they only take notes)
|
|
void onMqttConnect(bool sessionPresent);
|
|
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
|
void onSubscribe(uint16_t packetId, uint8_t qos);
|
|
void onMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t length, size_t index, size_t total);
|
|
|
|
//loop processing function
|
|
void connectWhenClockIsSet();
|
|
void handleMqttReconnection();
|
|
void continueDiscovery();
|
|
void publishDiscovery();
|
|
void processDirective();
|
|
|
|
AlexaDevice *findDevice(const char *endpointId, size_t length);
|
|
AlexaSendResult trySend(const char *topic, JsonDocument &doc, size_t *length);
|
|
void logRefusal(const char *topic, AlexaSendResult result, size_t length);
|
|
};
|
|
|
|
#endif // ALEX2ESP_H
|