2019-03-13 06:48:08 +01:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2019-03-13 06:48:08 +01:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2019-03-13 06:48:08 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
#include "../../inc/MarlinConfigPre.h"
|
2019-03-13 06:48:08 +01:00
|
|
|
|
|
|
|
#if ENABLED(WIFISUPPORT)
|
|
|
|
|
|
|
|
#include "WebSocketSerial.h"
|
2019-03-13 11:25:54 +01:00
|
|
|
#include "wifi.h"
|
2019-03-13 06:48:08 +01:00
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
WebSocketSerial webSocketSerial;
|
|
|
|
AsyncWebSocket ws("/ws"); // TODO Move inside the class.
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
// RingBuffer impl
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
#define NEXT_INDEX(I, SIZE) ((I + 1) & (ring_buffer_pos_t)(SIZE - 1))
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
RingBuffer::RingBuffer(ring_buffer_pos_t size)
|
|
|
|
: data(new uint8_t[size]),
|
2019-05-26 00:06:00 +02:00
|
|
|
size(size),
|
2019-04-15 23:32:20 +02:00
|
|
|
read_index(0),
|
2019-05-26 00:06:00 +02:00
|
|
|
write_index(0)
|
2019-04-15 23:32:20 +02:00
|
|
|
{}
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
RingBuffer::~RingBuffer() { delete[] data; }
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
ring_buffer_pos_t RingBuffer::write(const uint8_t c) {
|
|
|
|
const ring_buffer_pos_t n = NEXT_INDEX(write_index, size);
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
if (n != read_index) {
|
|
|
|
this->data[write_index] = c;
|
|
|
|
write_index = n;
|
|
|
|
return 1;
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
// TODO: buffer is full, handle?
|
|
|
|
return 0;
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
ring_buffer_pos_t RingBuffer::write(const uint8_t *buffer, ring_buffer_pos_t size) {
|
|
|
|
ring_buffer_pos_t written = 0;
|
|
|
|
for (ring_buffer_pos_t i = 0; i < size; i++) {
|
|
|
|
written += write(buffer[i]);
|
|
|
|
}
|
|
|
|
return written;
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
int RingBuffer::available() {
|
2019-04-15 23:32:20 +02:00
|
|
|
return (size - read_index + write_index) & (size - 1);
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
int RingBuffer::peek() {
|
2019-04-15 23:32:20 +02:00
|
|
|
return available() ? data[read_index] : -1;
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
int RingBuffer::read() {
|
2019-04-15 23:32:20 +02:00
|
|
|
if (available()) {
|
|
|
|
const int ret = data[read_index];
|
|
|
|
read_index = NEXT_INDEX(read_index, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return -1;
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
ring_buffer_pos_t RingBuffer::read(uint8_t *buffer) {
|
|
|
|
ring_buffer_pos_t len = available();
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
for(ring_buffer_pos_t i = 0; read_index != write_index; i++) {
|
|
|
|
buffer[i] = data[read_index];
|
|
|
|
read_index = NEXT_INDEX(read_index, size);
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
return len;
|
|
|
|
}
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
void RingBuffer::flush() { read_index = write_index; }
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
// WebSocketSerial impl
|
|
|
|
WebSocketSerial::WebSocketSerial()
|
|
|
|
: rx_buffer(RingBuffer(RX_BUFFER_SIZE)),
|
|
|
|
tx_buffer(RingBuffer(TX_BUFFER_SIZE))
|
|
|
|
{}
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
void WebSocketSerial::begin(const long baud_setting) {
|
|
|
|
ws.onEvent([this](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
|
|
|
switch (type) {
|
|
|
|
case WS_EVT_CONNECT: client->ping(); break; // client connected
|
|
|
|
case WS_EVT_DISCONNECT: // client disconnected
|
|
|
|
case WS_EVT_ERROR: // error was received from the other end
|
|
|
|
case WS_EVT_PONG: break; // pong message was received (in response to a ping request maybe)
|
|
|
|
case WS_EVT_DATA: { // data packet
|
|
|
|
AwsFrameInfo * info = (AwsFrameInfo*)arg;
|
|
|
|
if (info->opcode == WS_TEXT || info->message_opcode == WS_TEXT)
|
|
|
|
this->rx_buffer.write(data, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
server.addHandler(&ws);
|
|
|
|
}
|
2019-03-13 06:48:08 +01:00
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
void WebSocketSerial::end() { }
|
2019-09-17 03:31:08 +02:00
|
|
|
int WebSocketSerial::peek() { return rx_buffer.peek(); }
|
|
|
|
int WebSocketSerial::read() { return rx_buffer.read(); }
|
|
|
|
int WebSocketSerial::available() { return rx_buffer.available(); }
|
|
|
|
void WebSocketSerial::flush() { rx_buffer.flush(); }
|
2019-04-15 23:32:20 +02:00
|
|
|
|
|
|
|
size_t WebSocketSerial::write(const uint8_t c) {
|
|
|
|
size_t ret = tx_buffer.write(c);
|
|
|
|
|
|
|
|
if (ret && c == '\n') {
|
|
|
|
uint8_t tmp[TX_BUFFER_SIZE];
|
|
|
|
ring_buffer_pos_t size = tx_buffer.read(tmp);
|
|
|
|
ws.textAll(tmp, size);
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
return ret;
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:32:20 +02:00
|
|
|
size_t WebSocketSerial::write(const uint8_t* buffer, size_t size) {
|
|
|
|
size_t written = 0;
|
|
|
|
for(size_t i = 0; i < size; i++) {
|
|
|
|
written += write(buffer[i]);
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
2019-04-15 23:32:20 +02:00
|
|
|
return written;
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
void WebSocketSerial::flushTX() {
|
2019-04-15 23:32:20 +02:00
|
|
|
// No need to do anything as there's no benefit to sending partial lines over the websocket connection.
|
2019-03-13 06:48:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // WIFISUPPORT
|
|
|
|
#endif // ARDUINO_ARCH_ESP32
|