2019-08-05 20:41:53 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2019-08-05 20:41:53 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* 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
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-08-05 20:41:53 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#define BINARY_STREAM_COMPRESSION
|
|
|
|
|
|
|
|
#if ENABLED(BINARY_STREAM_COMPRESSION)
|
|
|
|
#include "../libs/heatshrink/heatshrink_decoder.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline bool bs_serial_data_available(const uint8_t index) {
|
|
|
|
switch (index) {
|
|
|
|
case 0: return MYSERIAL0.available();
|
2020-05-12 05:50:28 -05:00
|
|
|
#if HAS_MULTI_SERIAL
|
2019-08-05 20:41:53 -05:00
|
|
|
case 1: return MYSERIAL1.available();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int bs_read_serial(const uint8_t index) {
|
|
|
|
switch (index) {
|
|
|
|
case 0: return MYSERIAL0.read();
|
2020-05-12 05:50:28 -05:00
|
|
|
#if HAS_MULTI_SERIAL
|
2019-08-05 20:41:53 -05:00
|
|
|
case 1: return MYSERIAL1.read();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(BINARY_STREAM_COMPRESSION)
|
|
|
|
static heatshrink_decoder hsd;
|
|
|
|
static uint8_t decode_buffer[512] = {};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class SDFileTransferProtocol {
|
|
|
|
private:
|
|
|
|
struct Packet {
|
|
|
|
struct [[gnu::packed]] Open {
|
|
|
|
static bool validate(char* buffer, size_t length) {
|
|
|
|
return (length > sizeof(Open) && buffer[length - 1] == '\0');
|
|
|
|
}
|
|
|
|
static Open& decode(char* buffer) {
|
|
|
|
data = &buffer[2];
|
|
|
|
return *reinterpret_cast<Open*>(buffer);
|
|
|
|
}
|
|
|
|
bool compression_enabled() { return compression & 0x1; }
|
|
|
|
bool dummy_transfer() { return dummy & 0x1; }
|
|
|
|
static char* filename() { return data; }
|
|
|
|
private:
|
|
|
|
uint8_t dummy, compression;
|
|
|
|
static char* data; // variable length strings complicate things
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool file_open(char* filename) {
|
|
|
|
if (!dummy_transfer) {
|
2019-09-15 03:10:59 -05:00
|
|
|
card.mount();
|
2019-12-12 18:46:49 -06:00
|
|
|
card.openFileWrite(filename);
|
2019-08-05 20:41:53 -05:00
|
|
|
if (!card.isFileOpen()) return false;
|
|
|
|
}
|
|
|
|
transfer_active = true;
|
|
|
|
data_waiting = 0;
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(BINARY_STREAM_COMPRESSION, heatshrink_decoder_reset(&hsd));
|
2019-08-05 20:41:53 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool file_write(char* buffer, const size_t length) {
|
|
|
|
#if ENABLED(BINARY_STREAM_COMPRESSION)
|
|
|
|
if (compression) {
|
|
|
|
size_t total_processed = 0, processed_count = 0;
|
|
|
|
HSD_poll_res presult;
|
|
|
|
|
|
|
|
while (total_processed < length) {
|
|
|
|
heatshrink_decoder_sink(&hsd, reinterpret_cast<uint8_t*>(&buffer[total_processed]), length - total_processed, &processed_count);
|
|
|
|
total_processed += processed_count;
|
|
|
|
do {
|
|
|
|
presult = heatshrink_decoder_poll(&hsd, &decode_buffer[data_waiting], sizeof(decode_buffer) - data_waiting, &processed_count);
|
|
|
|
data_waiting += processed_count;
|
|
|
|
if (data_waiting == sizeof(decode_buffer)) {
|
|
|
|
if (!dummy_transfer)
|
|
|
|
if (card.write(decode_buffer, data_waiting) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
data_waiting = 0;
|
|
|
|
}
|
|
|
|
} while (presult == HSDR_POLL_MORE);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (dummy_transfer || card.write(buffer, length) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool file_close() {
|
|
|
|
if (!dummy_transfer) {
|
|
|
|
#if ENABLED(BINARY_STREAM_COMPRESSION)
|
|
|
|
// flush any buffered data
|
|
|
|
if (data_waiting) {
|
|
|
|
if (card.write(decode_buffer, data_waiting) < 0) return false;
|
|
|
|
data_waiting = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
card.closefile();
|
|
|
|
card.release();
|
|
|
|
}
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(BINARY_STREAM_COMPRESSION, heatshrink_decoder_finish(&hsd));
|
2019-08-05 20:41:53 -05:00
|
|
|
transfer_active = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void transfer_abort() {
|
|
|
|
if (!dummy_transfer) {
|
|
|
|
card.closefile();
|
|
|
|
card.removeFile(card.filename);
|
|
|
|
card.release();
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(BINARY_STREAM_COMPRESSION, heatshrink_decoder_finish(&hsd));
|
2019-08-05 20:41:53 -05:00
|
|
|
}
|
|
|
|
transfer_active = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class FileTransfer : uint8_t { QUERY, OPEN, CLOSE, WRITE, ABORT };
|
|
|
|
|
|
|
|
static size_t data_waiting, transfer_timeout, idle_timeout;
|
|
|
|
static bool transfer_active, dummy_transfer, compression;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
static void idle() {
|
|
|
|
// If a transfer is interrupted and a file is left open, abort it after TIMEOUT ms
|
|
|
|
const millis_t ms = millis();
|
|
|
|
if (transfer_active && ELAPSED(ms, idle_timeout)) {
|
|
|
|
idle_timeout = ms + IDLE_PERIOD;
|
|
|
|
if (ELAPSED(ms, transfer_timeout)) transfer_abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process(uint8_t packet_type, char* buffer, const uint16_t length) {
|
|
|
|
transfer_timeout = millis() + TIMEOUT;
|
|
|
|
switch (static_cast<FileTransfer>(packet_type)) {
|
|
|
|
case FileTransfer::QUERY:
|
|
|
|
SERIAL_ECHOPAIR("PFT:version:", VERSION_MAJOR, ".", VERSION_MINOR, ".", VERSION_PATCH);
|
|
|
|
#if ENABLED(BINARY_STREAM_COMPRESSION)
|
|
|
|
SERIAL_ECHOLNPAIR(":compresion:heatshrink,", HEATSHRINK_STATIC_WINDOW_BITS, ",", HEATSHRINK_STATIC_LOOKAHEAD_BITS);
|
|
|
|
#else
|
|
|
|
SERIAL_ECHOLNPGM(":compresion:none");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case FileTransfer::OPEN:
|
|
|
|
if (transfer_active)
|
|
|
|
SERIAL_ECHOLNPGM("PFT:busy");
|
|
|
|
else {
|
|
|
|
if (Packet::Open::validate(buffer, length)) {
|
|
|
|
auto packet = Packet::Open::decode(buffer);
|
|
|
|
compression = packet.compression_enabled();
|
|
|
|
dummy_transfer = packet.dummy_transfer();
|
|
|
|
if (file_open(packet.filename())) {
|
|
|
|
SERIAL_ECHOLNPGM("PFT:success");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SERIAL_ECHOLNPGM("PFT:fail");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FileTransfer::CLOSE:
|
|
|
|
if (transfer_active) {
|
|
|
|
if (file_close())
|
|
|
|
SERIAL_ECHOLNPGM("PFT:success");
|
|
|
|
else
|
|
|
|
SERIAL_ECHOLNPGM("PFT:ioerror");
|
|
|
|
}
|
|
|
|
else SERIAL_ECHOLNPGM("PFT:invalid");
|
|
|
|
break;
|
|
|
|
case FileTransfer::WRITE:
|
|
|
|
if (!transfer_active)
|
|
|
|
SERIAL_ECHOLNPGM("PFT:invalid");
|
|
|
|
else if (!file_write(buffer, length))
|
|
|
|
SERIAL_ECHOLNPGM("PFT:ioerror");
|
|
|
|
break;
|
|
|
|
case FileTransfer::ABORT:
|
|
|
|
transfer_abort();
|
|
|
|
SERIAL_ECHOLNPGM("PFT:success");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SERIAL_ECHOLNPGM("PTF:invalid");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint16_t VERSION_MAJOR = 0, VERSION_MINOR = 1, VERSION_PATCH = 0, TIMEOUT = 10000, IDLE_PERIOD = 1000;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BinaryStream {
|
|
|
|
public:
|
|
|
|
enum class Protocol : uint8_t { CONTROL, FILE_TRANSFER };
|
|
|
|
|
|
|
|
enum class ProtocolControl : uint8_t { SYNC = 1, CLOSE };
|
|
|
|
|
|
|
|
enum class StreamState : uint8_t { PACKET_RESET, PACKET_WAIT, PACKET_HEADER, PACKET_DATA, PACKET_FOOTER,
|
|
|
|
PACKET_PROCESS, PACKET_RESEND, PACKET_TIMEOUT, PACKET_ERROR };
|
|
|
|
|
|
|
|
struct Packet { // 10 byte protocol overhead, ascii with checksum and line number has a minimum of 7 increasing with line
|
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
union Header {
|
|
|
|
static constexpr uint16_t HEADER_TOKEN = 0xB5AD;
|
|
|
|
struct [[gnu::packed]] {
|
|
|
|
uint16_t token; // packet start token
|
|
|
|
uint8_t sync; // stream sync, resend id and packet loss detection
|
|
|
|
uint8_t meta; // 4 bit protocol,
|
|
|
|
// 4 bit packet type
|
|
|
|
uint16_t size; // data length
|
|
|
|
uint16_t checksum; // header checksum
|
|
|
|
};
|
2019-08-05 20:41:53 -05:00
|
|
|
uint8_t protocol() { return (meta >> 4) & 0xF; }
|
|
|
|
uint8_t type() { return meta & 0xF; }
|
|
|
|
void reset() { token = 0; sync = 0; meta = 0; size = 0; checksum = 0; }
|
2020-01-29 00:06:06 -06:00
|
|
|
uint8_t data[2];
|
2019-08-05 20:41:53 -05:00
|
|
|
};
|
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
union Footer {
|
|
|
|
struct [[gnu::packed]] {
|
|
|
|
uint16_t checksum; // full packet checksum
|
|
|
|
};
|
2019-08-05 20:41:53 -05:00
|
|
|
void reset() { checksum = 0; }
|
2019-09-02 19:35:05 -05:00
|
|
|
uint8_t data[1];
|
2019-08-05 20:41:53 -05:00
|
|
|
};
|
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
Header header;
|
|
|
|
Footer footer;
|
2019-08-05 20:41:53 -05:00
|
|
|
uint32_t bytes_received;
|
|
|
|
uint16_t checksum, header_checksum;
|
|
|
|
millis_t timeout;
|
|
|
|
char* buffer;
|
|
|
|
|
|
|
|
void reset() {
|
2019-09-02 19:35:05 -05:00
|
|
|
header.reset();
|
|
|
|
footer.reset();
|
2019-08-05 20:41:53 -05:00
|
|
|
bytes_received = 0;
|
|
|
|
checksum = 0;
|
|
|
|
header_checksum = 0;
|
|
|
|
timeout = millis() + PACKET_MAX_WAIT;
|
|
|
|
buffer = nullptr;
|
|
|
|
}
|
|
|
|
} packet{};
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
sync = 0;
|
|
|
|
packet_retries = 0;
|
|
|
|
buffer_next_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fletchers 16 checksum
|
|
|
|
uint32_t checksum(uint32_t cs, uint8_t value) {
|
|
|
|
uint16_t cs_low = (((cs & 0xFF) + value) % 255);
|
|
|
|
return ((((cs >> 8) + cs_low) % 255) << 8) | cs_low;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the next byte from the data stream keeping track of
|
|
|
|
// whether the stream times out from data starvation
|
|
|
|
// takes the data variable by reference in order to return status
|
|
|
|
bool stream_read(uint8_t& data) {
|
|
|
|
if (stream_state != StreamState::PACKET_WAIT && ELAPSED(millis(), packet.timeout)) {
|
|
|
|
stream_state = StreamState::PACKET_TIMEOUT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!bs_serial_data_available(card.transfer_port_index)) return false;
|
|
|
|
data = bs_read_serial(card.transfer_port_index);
|
|
|
|
packet.timeout = millis() + PACKET_MAX_WAIT;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const size_t buffer_size>
|
|
|
|
void receive(char (&buffer)[buffer_size]) {
|
|
|
|
uint8_t data = 0;
|
|
|
|
millis_t transfer_window = millis() + RX_TIMESLICE;
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
PORT_REDIRECT(card.transfer_port_index);
|
|
|
|
#endif
|
|
|
|
|
2019-10-07 19:31:48 -05:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
|
|
|
|
2019-08-05 20:41:53 -05:00
|
|
|
while (PENDING(millis(), transfer_window)) {
|
|
|
|
switch (stream_state) {
|
|
|
|
/**
|
|
|
|
* Data stream packet handling
|
|
|
|
*/
|
|
|
|
case StreamState::PACKET_RESET:
|
|
|
|
packet.reset();
|
|
|
|
stream_state = StreamState::PACKET_WAIT;
|
|
|
|
case StreamState::PACKET_WAIT:
|
|
|
|
if (!stream_read(data)) { idle(); return; } // no active packet so don't wait
|
2019-09-02 19:35:05 -05:00
|
|
|
packet.header.data[1] = data;
|
|
|
|
if (packet.header.token == packet.header.HEADER_TOKEN) {
|
2019-08-05 20:41:53 -05:00
|
|
|
packet.bytes_received = 2;
|
|
|
|
stream_state = StreamState::PACKET_HEADER;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// stream corruption drop data
|
2019-09-02 19:35:05 -05:00
|
|
|
packet.header.data[0] = data;
|
2019-08-05 20:41:53 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_HEADER:
|
|
|
|
if (!stream_read(data)) break;
|
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
packet.header.data[packet.bytes_received++] = data;
|
2019-08-05 20:41:53 -05:00
|
|
|
packet.checksum = checksum(packet.checksum, data);
|
|
|
|
|
|
|
|
// header checksum calculation can't contain the checksum
|
2019-09-02 19:35:05 -05:00
|
|
|
if (packet.bytes_received == sizeof(Packet::header) - 2)
|
2019-08-05 20:41:53 -05:00
|
|
|
packet.header_checksum = packet.checksum;
|
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
if (packet.bytes_received == sizeof(Packet::header)) {
|
|
|
|
if (packet.header.checksum == packet.header_checksum) {
|
2019-08-05 20:41:53 -05:00
|
|
|
// The SYNC control packet is a special case in that it doesn't require the stream sync to be correct
|
2019-09-02 19:35:05 -05:00
|
|
|
if (static_cast<Protocol>(packet.header.protocol()) == Protocol::CONTROL && static_cast<ProtocolControl>(packet.header.type()) == ProtocolControl::SYNC) {
|
2019-08-05 20:41:53 -05:00
|
|
|
SERIAL_ECHOLNPAIR("ss", sync, ",", buffer_size, ",", VERSION_MAJOR, ".", VERSION_MINOR, ".", VERSION_PATCH);
|
|
|
|
stream_state = StreamState::PACKET_RESET;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-02 19:35:05 -05:00
|
|
|
if (packet.header.sync == sync) {
|
2019-08-05 20:41:53 -05:00
|
|
|
buffer_next_index = 0;
|
|
|
|
packet.bytes_received = 0;
|
2019-09-02 19:35:05 -05:00
|
|
|
if (packet.header.size) {
|
2019-08-05 20:41:53 -05:00
|
|
|
stream_state = StreamState::PACKET_DATA;
|
|
|
|
packet.buffer = static_cast<char *>(&buffer[0]); // multipacket buffering not implemented, always allocate whole buffer to packet
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stream_state = StreamState::PACKET_PROCESS;
|
|
|
|
}
|
2019-09-02 19:35:05 -05:00
|
|
|
else if (packet.header.sync == sync - 1) { // ok response must have been lost
|
|
|
|
SERIAL_ECHOLNPAIR("ok", packet.header.sync); // transmit valid packet received and drop the payload
|
2019-08-05 20:41:53 -05:00
|
|
|
stream_state = StreamState::PACKET_RESET;
|
|
|
|
}
|
|
|
|
else if (packet_retries) {
|
|
|
|
stream_state = StreamState::PACKET_RESET; // could be packets already buffered on flow controlled connections, drop them without ack
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SERIAL_ECHO_MSG("Datastream packet out of order");
|
|
|
|
stream_state = StreamState::PACKET_RESEND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SERIAL_ECHO_START();
|
2019-09-02 19:35:05 -05:00
|
|
|
SERIAL_ECHOLNPAIR("Packet header(", packet.header.sync, "?) corrupt");
|
2019-08-05 20:41:53 -05:00
|
|
|
stream_state = StreamState::PACKET_RESEND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_DATA:
|
|
|
|
if (!stream_read(data)) break;
|
|
|
|
|
|
|
|
if (buffer_next_index < buffer_size)
|
|
|
|
packet.buffer[buffer_next_index] = data;
|
|
|
|
else {
|
|
|
|
SERIAL_ECHO_MSG("Datastream packet data buffer overrun");
|
|
|
|
stream_state = StreamState::PACKET_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.checksum = checksum(packet.checksum, data);
|
|
|
|
packet.bytes_received++;
|
|
|
|
buffer_next_index++;
|
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
if (packet.bytes_received == packet.header.size) {
|
2019-08-05 20:41:53 -05:00
|
|
|
stream_state = StreamState::PACKET_FOOTER;
|
|
|
|
packet.bytes_received = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_FOOTER:
|
|
|
|
if (!stream_read(data)) break;
|
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
packet.footer.data[packet.bytes_received++] = data;
|
|
|
|
if (packet.bytes_received == sizeof(Packet::footer)) {
|
|
|
|
if (packet.footer.checksum == packet.checksum) {
|
2019-08-05 20:41:53 -05:00
|
|
|
stream_state = StreamState::PACKET_PROCESS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SERIAL_ECHO_START();
|
2019-09-02 19:35:05 -05:00
|
|
|
SERIAL_ECHOLNPAIR("Packet(", packet.header.sync, ") payload corrupt");
|
2019-08-05 20:41:53 -05:00
|
|
|
stream_state = StreamState::PACKET_RESEND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_PROCESS:
|
|
|
|
sync++;
|
|
|
|
packet_retries = 0;
|
2019-09-02 19:35:05 -05:00
|
|
|
bytes_received += packet.header.size;
|
2019-08-05 20:41:53 -05:00
|
|
|
|
2019-09-02 19:35:05 -05:00
|
|
|
SERIAL_ECHOLNPAIR("ok", packet.header.sync); // transmit valid packet received
|
2019-08-05 20:41:53 -05:00
|
|
|
dispatch();
|
|
|
|
stream_state = StreamState::PACKET_RESET;
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_RESEND:
|
|
|
|
if (packet_retries < MAX_RETRIES || MAX_RETRIES == 0) {
|
|
|
|
packet_retries++;
|
|
|
|
stream_state = StreamState::PACKET_RESET;
|
|
|
|
SERIAL_ECHO_START();
|
|
|
|
SERIAL_ECHOLNPAIR("Resend request ", int(packet_retries));
|
|
|
|
SERIAL_ECHOLNPAIR("rs", sync);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stream_state = StreamState::PACKET_ERROR;
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_TIMEOUT:
|
|
|
|
SERIAL_ECHO_MSG("Datastream timeout");
|
|
|
|
stream_state = StreamState::PACKET_RESEND;
|
|
|
|
break;
|
|
|
|
case StreamState::PACKET_ERROR:
|
2019-09-02 19:35:05 -05:00
|
|
|
SERIAL_ECHOLNPAIR("fe", packet.header.sync);
|
2019-08-05 20:41:53 -05:00
|
|
|
reset(); // reset everything, resync required
|
|
|
|
stream_state = StreamState::PACKET_RESET;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 19:31:48 -05:00
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
2019-08-05 20:41:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void dispatch() {
|
2020-09-20 18:24:05 -05:00
|
|
|
switch (static_cast<Protocol>(packet.header.protocol())) {
|
2019-08-05 20:41:53 -05:00
|
|
|
case Protocol::CONTROL:
|
2020-09-20 18:24:05 -05:00
|
|
|
switch (static_cast<ProtocolControl>(packet.header.type())) {
|
2019-08-05 20:41:53 -05:00
|
|
|
case ProtocolControl::CLOSE: // revert back to ASCII mode
|
|
|
|
card.flag.binary_mode = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SERIAL_ECHO_MSG("Unknown BinaryProtocolControl Packet");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Protocol::FILE_TRANSFER:
|
2019-09-02 19:35:05 -05:00
|
|
|
SDFileTransferProtocol::process(packet.header.type(), packet.buffer, packet.header.size); // send user data to be processed
|
2019-08-05 20:41:53 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SERIAL_ECHO_MSG("Unsupported Binary Protocol");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void idle() {
|
|
|
|
// Some Protocols may need periodic updates without new data
|
|
|
|
SDFileTransferProtocol::idle();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint16_t PACKET_MAX_WAIT = 500, RX_TIMESLICE = 20, MAX_RETRIES = 0, VERSION_MAJOR = 0, VERSION_MINOR = 1, VERSION_PATCH = 0;
|
|
|
|
uint8_t packet_retries, sync;
|
|
|
|
uint16_t buffer_next_index;
|
|
|
|
uint32_t bytes_received;
|
|
|
|
StreamState stream_state = StreamState::PACKET_RESET;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern BinaryStream binaryStream[NUM_SERIAL];
|