Skip to content

Commit

Permalink
Simplify _write_row_packet by precomputing header and lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jun 3, 2024
1 parent a079df4 commit 70dfd64
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/WebSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ typedef enum {
static const uint8_t WSL_PONG[] = {WSL_MAGIC_BYTE_1, WSL_MAGIC_BYTE_2, WSLPacketType::WSL_PONG};
static const size_t WSL_PONG_LEN = sizeof(WSL_PONG) / sizeof(WSL_PONG[0]);

static const uint8_t WSL_HEAD[] = {
WSL_MAGIC_BYTE_1, // Magic Bytes
WSL_MAGIC_BYTE_2, // Magic Bytes
WSLPacketType::WSL_WRITE_ROW, // Packet Type (1 byte)
0x00, // Reserved
0x00, // Reserved
0x00, // Reserved
0x00, // Reserved
0x00, // Padding
0x00, // Padding
0x00, // Padding
0x00, // Padding
0x00 // Reserved
};
static const size_t WSL_HEAD_LEN = sizeof(WSL_HEAD) / sizeof(WSL_HEAD[0]);

static const size_t WSL_MSG_SIZE_LEN = sizeof(uint16_t);

void WebSerialClass::setAuthentication(const String& username, const String& password){
_username = username;
_password = password;
Expand Down Expand Up @@ -142,37 +160,14 @@ bool WebSerialClass::_has_enough_space(size_t 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
_buffer[_buffer_offset + header_size++] = WSL_MAGIC_BYTE_1;
_buffer[_buffer_offset + header_size++] = WSL_MAGIC_BYTE_2;

// Packet Type (1 byte)
_buffer[_buffer_offset + header_size++] = WSLPacketType::WSL_WRITE_ROW;

// Reserved (8 bytes)
_buffer[_buffer_offset + header_size++] = 0x00;
_buffer[_buffer_offset + header_size++] = 0x00;
_buffer[_buffer_offset + header_size++] = 0x00;
_buffer[_buffer_offset + header_size++] = 0x00;
_buffer[_buffer_offset + header_size++] = 0x00;
_buffer[_buffer_offset + header_size++] = 0x00;
_buffer[_buffer_offset + header_size++] = 0x00;
_buffer[_buffer_offset + header_size++] = 0x00;

// Reserved (1 byte)
_buffer[_buffer_offset + header_size++] = 0x00;

// Write header
memmove(_buffer, WSL_HEAD, WSL_HEAD_LEN);
// Message Length (2 bytes)
memset(_buffer + _buffer_offset + header_size, (uint16_t)payload_size, sizeof((uint16_t)payload_size));
header_size += sizeof((uint16_t)payload_size);

memset(_buffer + WSL_HEAD_LEN, static_cast<uint16_t>(payload_size), WSL_MSG_SIZE_LEN);
// Set Message
memcpy(_buffer + _buffer_offset + header_size, payload, payload_size);

memmove(_buffer + WSL_HEAD_LEN + WSL_MSG_SIZE_LEN, payload, payload_size);
// Return total packet size
return header_size + payload_size;
return WSL_HEAD_LEN + WSL_MSG_SIZE_LEN + payload_size;
}

size_t WebSerialClass::_write_row(uint8_t *data, size_t len) {
Expand Down

0 comments on commit 70dfd64

Please sign in to comment.