Skip to content

Commit

Permalink
Merge pull request #80 from ayushsharma82/dev
Browse files Browse the repository at this point in the history
v2.0.1 Release
  • Loading branch information
ayushsharma82 authored Jun 2, 2024
2 parents d735de2 + 98ce236 commit e6bbfd6
Show file tree
Hide file tree
Showing 10 changed files with 1,376 additions and 1,364 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/mathieucarbou/esphome-ESPAsyncTCP#v2.0.0

- name: Install ESPAsyncWebServer
run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/mathieucarbou/ESPAsyncWebServer#v2.10.1
run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/mathieucarbou/ESPAsyncWebServer#v2.10.4

- name: Build Demo
run: arduino-cli compile --library . --warnings none -b ${{ matrix.board }} "examples/Demo/Demo.ino"
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ dkms.conf

/portal
/.pio
/logs
/logs
/.docusaurus
/node_modules
6 changes: 3 additions & 3 deletions examples/Demo_AP/Demo_AP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ void setup() {
WiFi.softAP(ssid, password);
// Once connected, print IP
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPIP());

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! This is WebSerial demo. You can access webserial interface at http://" + WiFi.localIP().toString() + "/webserial");
request->send(200, "text/plain", "Hi! This is WebSerial demo. You can access webserial interface at http://" + WiFi.softAPIP().toString() + "/webserial");
});

// WebSerial is accessible at "<IP Address>/webserial" in browser
Expand All @@ -72,7 +72,7 @@ void loop() {
// Print every 2 seconds (non-blocking)
if ((unsigned long)(millis() - last_print_time) > 2000) {
WebSerial.print(F("IP address: "));
WebSerial.println(WiFi.localIP());
WebSerial.println(WiFi.softAPIP());
WebSerial.printf("Uptime: %lums\n", millis());
WebSerial.printf("Free heap: %u\n", ESP.getFreeHeap());
last_print_time = millis();
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"maintainer": true
}
],
"version": "2.0.0",
"version": "2.0.1",
"frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"],
"dependencies": [
{
"owner": "mathieucarbou",
"name": "ESP Async WebServer",
"version": "^2.10.1",
"version": "^2.10.4",
"platforms": ["espressif8266", "espressif32"]
}
]
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=WebSerial
version=2.0.0
version=2.0.1
author=Ayush Sharma
category=Communication
maintainer=Ayush Sharma <[email protected]>
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build_flags =
-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
lib_deps =
mathieucarbou/Async TCP @ ^3.1.4
mathieucarbou/ESP Async WebServer @ 2.10.1
mathieucarbou/ESP Async WebServer @ 2.10.4
upload_protocol = esptool
monitor_speed = 115200
monitor_filters = esp32_exception_decoder, log2file
Expand Down Expand Up @@ -35,5 +35,5 @@ board = esp32-s3-devkitc-1
platform = espressif8266
board = huzzah
lib_deps =
mathieucarbou/ESP Async WebServer @ 2.10.1
mathieucarbou/ESP Async WebServer @ 2.10.4
esphome/ESPAsyncTCP-esphome @ 2.0.0
36 changes: 25 additions & 11 deletions src/WebSerial.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
#include "WebSerial.h"

void WebSerialClass::setAuthentication(const char* username, const char* password){
_authenticate = true;
strncpy(_username, username, sizeof(_username));
strncpy(_password, password, sizeof(_password));

#include "wslp.h"

// DO NOT change magic bytes
#define WSL_MAGIC_BYTE_1 0xAB
#define WSL_MAGIC_BYTE_2 0xCD
#define WSL_LOG_PACKET_HEADER_SIZE 14
#define WSL_CALC_LOG_PACKET_SIZE(len) (WSL_LOG_PACKET_HEADER_SIZE + len)

typedef enum {
WSL_WRITE_ROW = 0x01,
WSL_MESSAGE = 0x02,
WSL_PING = 0x03,
WSL_PONG = 0x04,
} WSLPacketType;

void WebSerialClass::setAuthentication(const String& username, const String& password){
_username = username;
_password = password;
_authenticate = !_username.isEmpty() && !_password.isEmpty();
if (_ws != nullptr) {
_ws->setAuthentication(_username, _password);
_ws->setAuthentication(_username.c_str(), _password.c_str());
}
}

Expand All @@ -20,8 +34,8 @@ void WebSerialClass::begin(AsyncWebServer *server, const char* url) {

// Webpage Handler
_server->on(url, HTTP_GET, [&](AsyncWebServerRequest *request){
if(_authenticate == true){
if(!request->authenticate(_username, _password))
if(_authenticate){
if(!request->authenticate(_username.c_str(), _password.c_str()))
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", WEBSERIAL_HTML, sizeof(WEBSERIAL_HTML));
Expand All @@ -30,7 +44,7 @@ void WebSerialClass::begin(AsyncWebServer *server, const char* url) {
});

// WS Handler
_ws->onEvent([&](AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) -> void {
_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){
// } else if(type == WS_EVT_DISCONNECT){
// } else if(type == WS_EVT_DATA){
Expand All @@ -42,7 +56,7 @@ void WebSerialClass::begin(AsyncWebServer *server, const char* url) {
size_t message_size = (data[4] << 8) | data[3];
// Issue callback
if(_recv != nullptr){
_recv(data + 4, message_size);
_recv(data + 5, message_size);
}
} else if (data[2] == WSLPacketType::WSL_PING) {
// Send pong
Expand Down Expand Up @@ -110,7 +124,7 @@ bool WebSerialClass::_has_enough_space(size_t size) {
return (_buffer_offset + WSL_CALC_LOG_PACKET_SIZE(size) > WSL_BUFFER_SIZE);
}

size_t WebSerialClass::_write_row_packet(uint64_t reserved1, uint8_t reserved2, const uint8_t *payload, const size_t payload_size) {
size_t WebSerialClass::_write_row_packet(__unused uint64_t reserved1, __unused uint8_t reserved2, const uint8_t *payload, const size_t payload_size) {
size_t header_size = 0;

// Write Magic Bytes
Expand Down
36 changes: 16 additions & 20 deletions src/WebSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html)
#include "ESPAsyncWebServer.h"
#endif

#include "wslp.h"

// DO NOT change magic bytes
#define WSL_MAGIC_BYTE_1 0xAB
#define WSL_MAGIC_BYTE_2 0xCD

// Global buffer ( buffers all packets )
#ifndef WSL_BUFFER_SIZE
#define WSL_BUFFER_SIZE 2048
#endif
#ifndef WSL_PRINT_BUFFER_SIZE
#define WSL_PRINT_BUFFER_SIZE 1024
#endif
#ifndef WSL_MAX_ROW_PACKET_PAYLOAD_SIZE
#define WSL_MAX_ROW_PACKET_PAYLOAD_SIZE 512
#endif

#define WSL_LOG_PACKET_HEADER_SIZE 14
#define WSL_MAX_LOG_PACKET_MESSAGE_SIZE 512
#define WSL_CALC_LOG_PACKET_SIZE(len) (WSL_LOG_PACKET_HEADER_SIZE + len)

#ifndef WSL_PRINT_FLUSH_TIME_US
#define WSL_PRINT_FLUSH_TIME_US 100
#endif
#ifndef WSL_GLOBAL_FLUSH_TIME_MS
#define WSL_GLOBAL_FLUSH_TIME_MS 100
#endif
#ifndef WSL_CLEANUP_TIME_MS
#define WSL_CLEANUP_TIME_MS 5000
#endif

#if WSL_BUFFER_SIZE < 512
#error "WSL_BUFFER_SIZE must be >= 512 bytes"
Expand All @@ -70,19 +72,13 @@ License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html)
#error "WSL_GLOBAL_FLUSH_TIME_MS must be greater than 50ms"
#endif

typedef enum {
WSL_WRITE_ROW = 0x01,
WSL_MESSAGE = 0x02,
WSL_PING = 0x03,
WSL_PONG = 0x04,
} WSLPacketType;

typedef std::function<void(uint8_t *data, size_t len)> WSLMessageHandler;

class WebSerialClass : public Print {
public:
void begin(AsyncWebServer *server, const char* url = "/webserial");
void setAuthentication(const char* username, const char* passsword);
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);
size_t write(uint8_t);
size_t write(uint8_t* buffer, size_t size);
Expand All @@ -107,8 +103,8 @@ class WebSerialClass : public Print {
WSLMessageHandler _recv = nullptr;
unsigned long _last_cleanup_time = 0;
bool _authenticate = false;
char _username[64];
char _password[64];
String _username;
String _password;

// Print
void _wait_for_global_mutex();
Expand Down
Loading

0 comments on commit e6bbfd6

Please sign in to comment.