Add Webserial (#444)

* Webserial

* Webserial
This commit is contained in:
iranl
2024-08-09 11:35:42 +02:00
committed by GitHub
parent 44b5f71b40
commit 43ac62a22d
86 changed files with 1846 additions and 1192 deletions

View File

@@ -0,0 +1,151 @@
// SPDX-License-Identifier: MIT
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#include "MycilaWebSerial.h"
#include "MycilaWebSerialPage.h"
void WebSerialClass::setAuthentication(const String& username, const String& password) {
_username = username;
_password = password;
_authenticate = !_username.isEmpty() && !_password.isEmpty();
if (_ws) {
_ws->setAuthentication(_username.c_str(), _password.c_str());
}
}
void WebSerialClass::begin(AsyncWebServer* server, const char* url) {
_server = server;
String backendUrl = url;
backendUrl.concat("ws");
_ws = new AsyncWebSocket(backendUrl);
if (_authenticate) {
_ws->setAuthentication(_username.c_str(), _password.c_str());
}
_server->on(url, HTTP_GET, [&](AsyncWebServerRequest* request) {
if (_authenticate) {
if (!request->authenticate(_username.c_str(), _password.c_str()))
return request->requestAuthentication();
}
AsyncWebServerResponse* response = request->beginResponse(200, "text/html", WEBSERIAL_HTML, sizeof(WEBSERIAL_HTML));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
_ws->onEvent([&](__unused AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, __unused void* arg, uint8_t* data, __unused size_t len) -> void {
if (type == WS_EVT_CONNECT) {
client->setCloseClientOnQueueFull(false);
return;
}
if (type == WS_EVT_DATA) {
AwsFrameInfo* info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len) {
if (info->opcode == WS_TEXT) {
data[len] = 0;
}
if (strcmp((char*)data, "ping") == 0)
client->text("pong");
else if (_recv)
_recv(data, len);
}
}
});
_server->addHandler(_ws);
}
void WebSerialClass::onMessage(WSLMessageHandler recv) {
_recv = recv;
}
void WebSerialClass::onMessage(WSLStringMessageHandler callback) {
_recvString = callback;
_recv = [&](uint8_t* data, size_t len) {
if (data && len) {
String msg;
msg.reserve(len);
msg.concat((char*)data);
_recvString(msg);
}
};
}
size_t WebSerialClass::write(uint8_t m) {
if (!_ws)
return 0;
// We do not support non-buffered write on webserial for the HIGH_PERF version
// we fail with a stack trace allowing the user to change the code to use write(const uint8_t* buffer, size_t size) instead
if (!_initialBufferCapacity) {
#ifdef ESP8266
ets_printf("'-D WSL_FAIL_ON_NON_BUFFERED_WRITE' is set: non-buffered write is not supported. Please use write(const uint8_t* buffer, size_t size) instead.");
#else
log_e("'-D WSL_FAIL_ON_NON_BUFFERED_WRITE' is set: non-buffered write is not supported. Please use write(const uint8_t* buffer, size_t size) instead.");
#endif
assert(false);
return 0;
}
write(&m, 1);
return (1);
}
size_t WebSerialClass::write(const uint8_t* buffer, size_t size) {
if (!_ws || size == 0)
return 0;
// No buffer, send directly (i.e. use case for log streaming)
if (!_initialBufferCapacity) {
size = buffer[size - 1] == '\n' ? size - 1 : size;
_send(buffer, size);
return size;
}
// fill the buffer while sending data for each EOL
size_t start = 0, end = 0;
while (end < size) {
if (buffer[end] == '\n') {
if (end > start) {
_buffer.concat(reinterpret_cast<const char*>(buffer + start), end - start);
}
_send(reinterpret_cast<const uint8_t*>(_buffer.c_str()), _buffer.length());
start = end + 1;
}
end++;
}
if (end > start) {
_buffer.concat(reinterpret_cast<const char*>(buffer + start), end - start);
}
return size;
}
void WebSerialClass::_send(const uint8_t* buffer, size_t size) {
if (_ws && size > 0) {
_ws->cleanupClients(WSL_MAX_WS_CLIENTS);
if (_ws->count()) {
_ws->textAll((const char*)buffer, size);
}
}
// if buffer grew too much, free it, otherwise clear it
if (_initialBufferCapacity) {
if (_buffer.length() > _initialBufferCapacity) {
setBuffer(_initialBufferCapacity);
} else {
_buffer.clear();
}
}
}
void WebSerialClass::setBuffer(size_t initialCapacity) {
assert(initialCapacity <= UINT16_MAX);
_initialBufferCapacity = initialCapacity;
_buffer = String();
_buffer.reserve(initialCapacity);
}
WebSerialClass WebSerial;

View File

@@ -0,0 +1,70 @@
// SPDX-License-Identifier: MIT
/*
* Copyright (C) 2023-2024 Mathieu Carbou
*/
#pragma once
#if defined(ESP8266)
#include "ESP8266WiFi.h"
#elif defined(ESP32)
#include "WiFi.h"
#endif
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <functional>
#define WSL_VERSION "6.3.0"
#define WSL_VERSION_MAJOR 6
#define WSL_VERSION_MINOR 3
#define WSL_VERSION_REVISION 0
#ifndef WSL_MAX_WS_CLIENTS
#define WSL_MAX_WS_CLIENTS DEFAULT_MAX_WS_CLIENTS
#endif
// High performance mode:
// - Low memory footprint (no stack allocation, no global buffer by default)
// - Low latency (messages sent immediately to the WebSocket queue)
// - High throughput (up to 20 messages per second, no locking mechanism)
// Also recommended to tweak AsyncTCP and ESPAsyncWebServer settings, for example:
// -D CONFIG_ASYNC_TCP_QUEUE_SIZE=128 // AsyncTCP queue size
// -D CONFIG_ASYNC_TCP_RUNNING_CORE=1 // core for the async_task
// -D WS_MAX_QUEUED_MESSAGES=128 // WS message queue size
typedef std::function<void(uint8_t* data, size_t len)> WSLMessageHandler;
typedef std::function<void(const String& msg)> WSLStringMessageHandler;
class WebSerialClass : public Print {
public:
void begin(AsyncWebServer* server, const char* url = "/webserial");
inline void setAuthentication(const char* username, const char* password) { setAuthentication(String(username), String(password)); }
void setAuthentication(const String& username, const String& password);
void onMessage(WSLMessageHandler recv);
void onMessage(WSLStringMessageHandler recv);
size_t write(uint8_t) override;
size_t write(const uint8_t* buffer, size_t size) override;
// A buffer (shared across cores) can be initialised with an initial capacity to be able to use any Print functions event those that are not buffered and would
// create a performance impact for WS calls. The goal of this buffer is to be used with lines ending with '\n', like log messages.
// The buffer size will eventually grow until a '\n' is found, then the message will be sent to the WS clients and a new buffer will be created.
// Set initialCapacity to 0 to disable buffering.
// Must be called before begin(): calling it after will erase the buffer and its content will be lost.
// The buffer is not enabled by default.
void setBuffer(size_t initialCapacity);
private:
// Server
AsyncWebServer* _server;
AsyncWebSocket* _ws;
WSLMessageHandler _recv = nullptr;
WSLStringMessageHandler _recvString = nullptr;
bool _authenticate = false;
String _username;
String _password;
size_t _initialBufferCapacity = 0;
String _buffer;
void _send(const uint8_t* buffer, size_t size);
};
extern WebSerialClass WebSerial;

View File

@@ -0,0 +1,84 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
const uint32_t WEBSERIAL_HTML_SIZE = 2320;
const uint8_t WEBSERIAL_HTML[] PROGMEM = {
31,139,8,0,0,0,0,0,2,3,116,85,247,146,179,54,16,127,21,197,95,202,57,99,225,242,117,48,78,239,
189,231,95,33,45,160,156,144,136,36,184,115,24,222,61,139,4,87,231,238,108,107,181,179,229,183,85,199,119,132,
225,254,220,2,169,125,163,78,199,249,23,152,56,29,27,240,140,240,154,89,7,62,239,124,73,223,44,60,163,61,
104,159,175,190,249,34,7,81,193,10,149,125,75,225,223,78,246,249,223,244,143,79,232,103,166,105,153,151,133,130,
211,209,75,143,199,95,80,144,207,140,118,70,193,113,27,89,15,140,93,73,225,235,92,64,47,57,208,112,217,72,
45,189,100,138,58,206,20,228,251,21,209,172,129,188,151,112,213,26,235,79,71,231,207,104,71,200,126,16,210,181,
138,157,211,66,25,126,57,178,161,97,182,146,58,77,94,88,104,50,15,215,158,10,224,198,34,38,163,83,109,52,
140,31,110,210,148,149,30,44,158,5,148,198,194,64,175,160,184,148,158,22,230,154,58,249,159,212,85,42,117,13,
86,250,236,49,107,108,23,39,59,178,35,123,244,51,22,70,156,7,211,131,117,220,26,165,104,1,53,235,165,177,
193,97,86,131,172,106,159,238,119,187,247,178,16,94,36,23,35,89,203,132,64,251,72,149,152,18,90,178,70,170,
115,74,89,219,42,160,238,236,60,52,155,120,208,78,110,62,85,82,95,254,192,248,111,129,243,37,106,108,86,191,
65,101,128,252,241,205,106,243,171,41,140,55,155,213,215,160,122,240,146,51,242,35,116,176,218,56,166,29,117,8,
191,28,147,144,21,142,169,7,59,4,154,41,89,233,52,114,198,164,178,236,60,112,163,16,254,179,87,175,94,239,
223,188,29,19,87,51,97,174,134,82,42,20,73,133,53,45,141,172,139,29,121,209,94,147,231,248,181,85,129,183,
233,127,75,146,221,235,245,154,220,151,59,160,204,225,145,220,171,245,122,76,174,104,217,41,53,220,38,103,66,33,
197,77,113,167,11,178,88,75,15,3,254,166,201,203,41,235,73,169,224,250,70,102,186,76,106,8,115,34,233,68,
165,251,49,249,167,115,94,150,103,42,49,91,142,130,22,195,61,78,10,26,77,91,211,105,1,98,40,140,21,96,
169,101,66,118,110,113,227,128,79,221,51,220,105,133,89,14,57,89,193,248,101,21,244,233,156,180,242,77,249,182,
100,99,210,48,169,239,193,203,2,48,33,109,52,152,162,124,215,232,108,193,51,15,68,16,165,206,51,235,179,80,
153,25,104,172,207,157,110,18,125,29,157,144,164,101,90,131,26,90,227,100,176,108,65,49,47,123,200,34,80,196,
84,150,51,77,195,236,164,206,40,41,22,86,76,124,8,55,187,159,130,169,189,31,133,24,205,69,37,206,20,191,
152,74,70,104,152,133,117,108,98,204,19,164,13,160,141,102,110,116,234,77,155,30,230,148,70,188,164,232,188,199,
196,242,206,58,180,218,26,25,34,92,230,225,13,54,203,126,135,63,72,60,182,106,58,175,164,134,24,78,24,180,
37,216,93,118,7,229,99,236,187,221,171,215,229,139,7,24,82,172,19,43,20,136,225,177,194,75,246,106,255,234,
237,141,194,51,11,28,29,13,13,198,52,23,227,121,114,8,185,91,144,199,76,90,8,120,123,176,211,32,170,236,
169,5,49,62,155,42,143,124,26,177,220,150,145,21,88,166,206,67,134,185,91,140,78,14,35,253,80,111,73,231,
211,17,48,133,88,136,195,64,134,206,77,173,0,10,184,159,147,119,163,53,37,14,246,47,196,77,56,135,185,16,
217,189,101,155,221,228,115,135,163,104,204,180,78,22,141,169,19,8,130,124,170,121,198,132,155,246,28,98,89,116,
112,152,16,125,131,170,113,180,13,243,84,65,233,135,64,166,19,185,176,163,94,228,7,122,60,110,227,139,112,220,
198,23,108,90,200,167,163,144,61,225,138,57,151,175,194,152,204,131,76,238,44,192,21,190,121,123,34,69,30,94,
38,18,172,228,75,152,152,151,7,239,87,189,63,29,25,169,45,148,249,150,156,142,178,169,38,93,101,42,67,80,
214,90,99,115,132,143,133,254,30,89,23,107,226,44,207,183,10,105,68,198,238,1,138,107,145,132,157,70,166,253,
70,98,123,173,162,20,90,189,95,220,211,49,158,139,254,188,177,72,180,179,66,247,92,73,126,153,99,76,216,152,
76,125,166,128,233,139,53,62,149,125,69,152,149,140,214,82,8,208,185,183,29,16,193,60,163,18,61,228,2,20,
120,32,165,84,42,231,157,181,152,149,207,166,50,145,210,240,46,140,68,94,50,245,63,173,85,181,237,214,14,3,
127,69,55,239,210,146,101,89,86,46,115,15,190,148,222,187,92,56,204,252,245,149,189,147,148,185,93,49,206,140,
33,123,76,231,207,96,90,234,191,165,48,182,223,195,127,31,223,252,54,51,133,8,62,183,30,103,48,93,228,161,
248,253,215,120,4,236,192,211,223,102,219,30,2,41,182,86,179,93,161,115,99,204,133,50,138,83,69,211,41,172,
229,34,109,224,192,81,133,5,9,166,87,206,107,201,184,97,170,84,131,204,2,73,41,247,60,203,85,150,198,160,
164,144,201,192,251,111,199,152,244,64,52,196,69,114,75,129,103,165,4,50,39,7,75,96,153,230,145,239,104,209,
22,132,0,131,73,159,142,145,15,18,45,141,230,24,205,215,220,189,245,238,25,28,199,16,232,87,216,7,29,211,
193,197,76,166,112,119,136,194,12,188,150,141,175,176,202,78,102,189,170,114,55,11,231,251,199,136,44,156,136,116,
97,39,124,166,159,179,103,71,221,132,255,15,142,175,183,142,163,254,211,155,245,217,39,13,62,8,213,15,180,55,
11,168,233,14,154,63,22,237,246,86,238,95,175,82,198,36,190,140,107,217,189,13,10,184,215,97,41,128,136,87,
34,163,131,15,185,236,218,24,58,53,208,128,166,176,99,202,109,106,17,53,92,112,209,232,177,206,237,253,30,109,
231,104,57,77,51,51,205,65,10,37,44,6,197,34,221,17,245,54,96,14,96,112,48,184,62,191,181,104,217,255,
224,221,161,26,131,49,175,73,0,37,219,78,9,191,51,235,221,182,186,66,229,116,85,242,59,171,82,249,221,117,
84,242,19,117,226,4,83,202,144,184,79,133,191,215,138,121,184,123,248,44,30,17,135,39,177,100,222,4,62,189,
102,90,95,52,216,118,207,218,193,15,60,26,74,146,208,252,35,166,100,48,233,87,229,224,206,133,57,42,170,254,
122,196,21,138,17,31,212,50,39,93,52,136,236,238,144,193,133,91,168,74,224,140,185,10,38,51,178,81,138,120,
190,170,65,196,101,14,3,133,37,186,172,173,248,149,41,175,77,223,220,168,130,101,39,219,42,170,125,181,228,66,
229,177,184,55,70,37,237,94,163,247,223,154,6,214,33,96,116,24,48,248,149,212,66,125,105,11,25,36,18,40,
193,100,48,42,7,201,130,129,36,76,214,66,61,20,62,84,14,41,164,152,168,30,72,32,152,231,77,122,70,181,
147,232,84,177,231,93,244,161,133,20,133,151,149,88,5,147,235,32,16,254,43,57,158,193,132,161,61,25,57,225,
185,187,187,49,47,91,153,201,145,78,224,180,237,127,127,11,132,54,228,124,164,73,214,89,190,101,151,98,135,185,
189,227,186,160,170,65,117,241,20,134,113,146,97,183,54,114,189,159,197,201,72,198,23,215,156,194,12,148,225,140,
109,228,70,63,243,221,179,33,162,253,104,58,186,26,173,198,183,94,232,204,56,6,141,39,246,118,9,54,195,171,
151,65,180,129,229,19,156,2,28,72,113,178,227,136,66,13,255,108,252,28,42,11,7,234,72,254,61,48,91,189,
222,117,188,41,104,196,188,64,152,250,139,157,24,85,198,230,93,162,91,187,187,55,206,15,204,95,238,66,186,135,
33,201,2,193,187,11,218,138,33,216,71,37,184,215,251,71,207,10,74,66,244,132,241,238,137,219,231,39,42,170,
143,1,238,238,42,93,60,29,27,91,213,71,242,199,175,148,130,22,15,37,94,232,246,116,24,57,218,83,193,244,
204,16,74,228,19,38,130,48,101,252,76,65,159,111,148,244,243,138,104,230,21,73,252,92,70,230,11,175,238,26,
141,243,203,91,53,4,116,235,190,30,36,65,162,139,70,228,131,223,2,202,82,221,174,244,20,183,197,216,12,164,
203,113,249,1,137,104,89,140,2,123,187,147,18,184,243,252,195,155,215,184,11,181,1,10,108,235,212,20,165,20,
81,130,30,218,81,119,1,49,58,244,95,130,170,27,80,208,20,35,140,39,204,22,152,88,27,89,114,101,181,3,
46,105,32,190,82,74,153,188,61,141,51,212,151,122,108,129,146,31,46,2,168,199,170,252,149,61,8,170,153,13,
10,2,225,178,0,176,192,99,211,192,41,57,101,124,29,214,25,191,216,44,134,41,68,98,83,106,56,200,150,182,
154,58,104,59,173,116,53,1,45,43,253,6,31,9,221,135,131,140,7,238,153,112,16,199,70,13,29,239,149,127,
75,185,97,142,173,244,35,247,92,238,69,240,64,225,130,93,184,120,149,80,248,69,182,45,123,87,222,116,152,180,
5,86,66,170,235,37,86,8,135,147,9,79,44,249,197,158,103,41,70,225,196,189,126,33,208,106,98,163,73,138,
179,66,38,149,30,18,41,65,184,238,120,135,94,22,99,108,43,205,67,208,111,121,233,176,177,58,7,180,115,204,
176,41,72,242,131,228,212,97,224,161,178,192,132,173,240,72,82,65,230,131,173,209,37,101,57,249,149,145,28,114,
242,83,147,121,60,230,196,190,42,247,32,151,129,220,57,210,102,51,186,144,11,23,210,143,213,68,118,72,79,125,
83,79,34,78,110,22,211,212,149,36,132,95,214,232,220,128,125,166,209,30,106,54,37,215,202,222,108,182,220,96,
212,44,142,62,88,92,251,138,92,84,129,120,243,246,209,107,92,67,208,185,122,13,191,69,189,171,22,240,42,108,
180,5,13,232,130,18,23,0,97,142,189,230,234,187,233,63,170,40,30,237,131,182,47,199,198,130,134,154,146,135,
111,94,33,232,172,163,85,170,128,130,112,26,179,138,121,76,14,105,180,133,224,217,233,53,13,114,167,231,255,151,
253,15,145,100,45,14,173,21,0,0
};