ESPSomfy-RTS/src/ESPNetwork.h
cjkas 25d548ef07 fix: send socket events only from loop()
Every socket event is formatted into one static buffer by one shared
formatter.  setConnected() runs on the WiFi/Ethernet event task and sent
memStatus, wifiStrength and ethernet, then ran sockEmit.loop(), while
loop() could be building its own event.  _safecat() checks the room and
then writes at _len, so another task moving _len in between could write
past the end of g_response.

setConnected() now sends nothing.  loop() broadcasts the status every
STATUS_EMIT_MS (30 s): memStatus, then ethernet, or wifiStrength when it
moved 3 dBm or changed channel.  A client that connects still gets all
of it at once from initClients(), so needsBroadcast and the 1.5 s tick
go, and so does the 1.5 s memStatus for the config panel.

SocketEmitter records loop()'s task in begin().  Off it, beginEmit()
logs and hands out a formatter that writes nothing, and endEmit(),
endEmitRoom() and loop() do nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 15:27:21 +02:00

69 lines
2.2 KiB
C++

#include <Arduino.h>
#include <WiFi.h>
#include "Utils.h"
#ifndef ESPNetwork_h
#define ESPNetwork_h
//enum class conn_types_t : byte;
#define CONNECT_TIMEOUT 20000
#define SSID_SCAN_INTERVAL 600000
// A weak signal moves a dBm or two all the time, so only broadcast real
// changes.
#define RSSI_EMIT_MIN_DBM 3
// How often loop() broadcasts the status: memStatus, then wifiStrength or
// ethernet. memStatus is the one event that is always sent, so this must stay
// well under Home Assistant's 60s receive timeout. A client that connects gets
// all of it straight away from SocketEmitter::initClients().
#define STATUS_EMIT_MS 30000
// A roaming scan only moves to another AP at least this much stronger.
#define ROAM_MIN_GAIN_DB 6
class ESPNetwork {
protected:
unsigned long lastEmit = 0;
unsigned long lastMDNS = 0;
RssiBroadcast rssiBroadcast;
int linkSpeed = 0;
bool _connecting = false;
public:
unsigned long lastWifiScan = 0;
bool ethStarted = false;
bool wifiFallback = false;
bool softAPOpened = false;
bool openingSoftAP = false;
conn_types_t connType = conn_types_t::unset;
conn_types_t connTarget = conn_types_t::unset;
bool connected();
bool connecting();
void clearConnecting();
conn_types_t preferredConnType();
String ssid;
String mac;
int channel;
int strength;
int disconnected = 0;
int connectAttempts = 0;
uint32_t disconnectTime = 0;
uint32_t connectStart = 0;
uint32_t connectTime = 0;
bool openSoftAP();
bool connect(conn_types_t ctype);
bool connectWiFi(const uint8_t *bssid = nullptr, const int32_t channel = -1);
bool connectWired();
void setConnected(conn_types_t connType);
bool getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel);
bool changeAP(const uint8_t *bssid, const int32_t channel);
//int getStrengthByMac(const char *mac);
//int getStrengthBySSID(const char *ssid);
void updateHostname();
bool setup();
void loop();
void end();
void emitSockets();
void emitSockets(uint8_t num);
void emitHeap(uint8_t num = 255);
uint32_t getChipId();
static void networkEvent(WiFiEvent_t event);
};
#endif