Skip to content

Commit

Permalink
Fixed crash on WiFi disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchBradley committed Oct 18, 2024
1 parent f073e00 commit ad2cc0a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
3 changes: 3 additions & 0 deletions FluidNC/ld/esp32/vtable_in_dram.ld
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* List of files and sections to place in RAM instead of FLASH */
/* See README.md in this directory for a complete explanation */

**Detail.cpp.o(.rodata .rodata.*)
/* *Pin.cpp.o(.rodata .rodata.*) */

/* All files whose name ends with Spindle.cpp */

/* An earlier version included .xt.prop and .xt.prop.* but */
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/Pins/ChannelPinDetail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Pins {
return PinCapabilities::Output | PinCapabilities::Input | PinCapabilities::PWM | PinCapabilities::Void;
}

void ChannelPinDetail::write(int high) {
void IRAM_ATTR ChannelPinDetail::write(int high) {
if (high == _value) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/Pins/DebugPinDetail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Pins {

// I/O:
void DebugPinDetail::write(int high) {
void IRAM_ATTR DebugPinDetail::write(int high) {
if (high != int(_isHigh)) {
_isHigh = bool(high);
if (shouldEvent()) {
Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Pins/ErrorPinDetail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Pins {
}

#ifdef ESP32
void ErrorPinDetail::write(int high) {
void IRAM_ATTR ErrorPinDetail::write(int high) {
log_error("Cannot write to pin " << _description.c_str() << ". The config is incorrect.");
}
int ErrorPinDetail::read() {
Expand All @@ -25,7 +25,7 @@ namespace Pins {
}

#else
void ErrorPinDetail::write(int high) {
void IRAM_ATTR ErrorPinDetail::write(int high) {
Assert(false, "Cannot write to an error pin.");
}
int ErrorPinDetail::read() {
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/Pins/I2SOPinDetail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace Pins {

// Write and wait for completion. Not suitable for use from an ISR
// cppcheck-suppress unusedFunction
void I2SOPinDetail::synchronousWrite(int high) {
void IRAM_ATTR I2SOPinDetail::synchronousWrite(int high) {
if (high != _lastWrittenValue) {
_lastWrittenValue = high;

Expand Down
22 changes: 10 additions & 12 deletions FluidNC/src/WebUI/WSChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "WSChannel.h"

#include "src/UartChannel.h"
#include "WebServer.h"
#include <WebSocketsServer.h>
#include <WiFi.h>
Expand Down Expand Up @@ -62,12 +63,10 @@ namespace WebUI {
int stat = _server->canSend(_clientNum);
if (stat < 0) {
_active = false;
log_debug("WebSocket is dead; closing");
return 0;
}
if (!_server->sendBIN(_clientNum, out, outlen)) {
_active = false;
log_debug("WebSocket is unresponsive; closing");
}
if (_output_line.length()) {
_output_line = "";
Expand All @@ -82,7 +81,7 @@ namespace WebUI {
}
if (!_server->sendTXT(_clientNum, s.c_str())) {
_active = false;
log_debug("WebSocket is unresponsive; closing");
log_debug_to(Uart0, "WebSocket is unresponsive; closing");
return false;
}
return true;
Expand All @@ -95,7 +94,7 @@ namespace WebUI {
int stat = _server->canSend(_clientNum);
if (stat < 0) {
_active = false;
log_debug("WebSocket is dead; closing");
log_debug_to(Uart0, "WebSocket is dead; closing");
return;
}
if (stat == 0) {
Expand Down Expand Up @@ -195,18 +194,18 @@ namespace WebUI {
void WSChannels::handleEvent(WebSocketsServer* server, uint8_t num, uint8_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
log_debug("WebSocket disconnect " << num);
log_debug_to(Uart0, "WebSocket disconnect " << num);
WSChannels::removeChannel(num);
break;
case WStype_CONNECTED: {
WSChannel* wsChannel = new WSChannel(server, num);
if (!wsChannel) {
log_error("Creating WebSocket channel failed");
log_error_to(Uart0, "Creating WebSocket channel failed");
} else {
std::string uri((char*)payload, length);

IPAddress ip = server->remoteIP(num);
log_debug("WebSocket " << num << " from " << ip << " uri " << uri);
log_debug_to(Uart0, "WebSocket " << num << " from " << ip << " uri " << uri);

_lastWSChannel = wsChannel;
allChannels.registration(wsChannel);
Expand Down Expand Up @@ -238,19 +237,19 @@ namespace WebUI {
void WSChannels::handlev3Event(WebSocketsServer* server, uint8_t num, uint8_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
log_debug("WebSocket disconnect " << num);
printf("WebSocket disconnect %d\n", num);
WSChannels::removeChannel(num);
break;
case WStype_CONNECTED: {
log_debug("WStype_Connected");
log_debug_to(Uart0, "WStype_Connected");
WSChannel* wsChannel = new WSChannel(server, num);
if (!wsChannel) {
log_error("Creating WebSocket channel failed");
log_error_to(Uart0, "Creating WebSocket channel failed");
} else {
std::string uri((char*)payload, length);

IPAddress ip = server->remoteIP(num);
log_debug("WebSocket " << num << " from " << ip << " uri " << uri);
log_debug_to(Uart0, "WebSocket " << num << " from " << ip << " uri " << uri);

_lastWSChannel = wsChannel;
allChannels.registration(wsChannel);
Expand All @@ -276,7 +275,6 @@ namespace WebUI {
case WStype_TEXT:
try {
std::string msg = (const char*)payload;
//log_debug("WSv3Channels::handleEvent WStype_TEXT:" << msg)
if (msg.rfind("PING:", 0) == 0) {
std::string response("PING:60000:60000");
_wsChannels.at(num)->sendTXT(response);
Expand Down
31 changes: 28 additions & 3 deletions FluidNC/src/WebUI/WifiConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,18 @@ namespace WebUI {
s << "no";
#endif
s << " # webcommunication: Sync: ";
s << std::to_string(Web_Server::port() + 1) + ":";
s << std::to_string(Web_Server::port() + 1);
#if 0
// If we omit the explicit IP address for the websocket,
// WebUI will use the same IP address that it uses for
// HTTP, with the port number as above. That is better
// than providing an explicit address, because if the WiFi
// drops and comes back up again, DHCP might assign a
// different IP address so the one provided below would no
// longer work. But if we are using an MDNS address like
// fluidnc.local, a websocket reconnection will succeed
// because MDNS will offer the new IP address.
s << ":";
switch (WiFi.getMode()) {
case WIFI_AP:
s << IP_string(WiFi.softAPIP());
Expand All @@ -575,6 +586,7 @@ namespace WebUI {
s << "0.0.0.0";
break;
}
#endif
s << " # hostname:";
s << WiFi.getHostname();
if (WiFi.getMode() == WIFI_AP) {
Expand Down Expand Up @@ -616,14 +628,26 @@ namespace WebUI {
*/

static void WiFiEvent(WiFiEvent_t event) {
static bool disconnect_seen = false;
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
log_info("WiFi Disconnected");
if (!disconnect_seen) {
log_info_to(Uart0, "WiFi Disconnected");
disconnect_seen = true;
}
break;
case SYSTEM_EVENT_STA_START:
break;
case SYSTEM_EVENT_STA_STOP:
break;
case SYSTEM_EVENT_STA_CONNECTED:
disconnect_seen = false;
log_info_to(Uart0, "WiFi STA Connected");
break;
default:
//log_info("WiFi event:" << event);
log_debug_to(Uart0, "WiFi event: " << (int)event);
break;
}
}
Expand Down Expand Up @@ -692,6 +716,7 @@ namespace WebUI {
WiFi.mode(WIFI_STA);
WiFi.setMinSecurity(static_cast<wifi_auth_mode_t>(_sta_min_security->get()));
WiFi.setScanMethod(_fast_scan->get() ? WIFI_FAST_SCAN : WIFI_ALL_CHANNEL_SCAN);
WiFi.setAutoReconnect(true);
//Get parameters for STA
//password
const char* password = _sta_password->get();
Expand Down

0 comments on commit ad2cc0a

Please sign in to comment.