Skip to content

Commit

Permalink
Stream::Send fixes: doc + StreamConstPtr byte-by-byte + missing SSL a…
Browse files Browse the repository at this point in the history
…vailableForWrite (#7935)

* StreamConstPtr: fix doc + reading flash space byte-by-byte
* add missing availableForWrite wrapper in wificlient-ssl
* WiFiClientSecure-ctx: add missing availableForWrite()
  • Loading branch information
d-a-v committed Mar 25, 2021
1 parent 0a4fcdf commit c1118df
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cores/esp8266/StreamDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ class StreamConstPtr: public StreamNull

virtual int read() override
{
return _peekPointer < _size ? _buffer[_peekPointer++] : -1;
// valid with dram, iram and flash
return _peekPointer < _size ? pgm_read_byte(&_buffer[_peekPointer++]) : -1;
}

virtual int peek() override
{
return _peekPointer < _size ? _buffer[_peekPointer] : -1;
// valid with dram, iram and flash
return _peekPointer < _size ? pgm_read_byte(&_buffer[_peekPointer]) : -1;
}

virtual size_t readBytes(char* buffer, size_t len) override
Expand Down
4 changes: 2 additions & 2 deletions doc/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ Stream extensions

Two additional classes are provided.

- ``StreamPtr::`` is designed to hold a constant buffer (in ram or flash).
- ``StreamConstPtr::`` is designed to hold a constant buffer (in ram or flash).

With this class, a ``Stream::`` can be made from ``const char*``,
``F("some words in flash")`` or ``PROGMEM`` strings. This class makes
Expand All @@ -451,7 +451,7 @@ Stream extensions

.. code:: cpp
StreamPtr css(F("my long css data")); // CSS data not copied to RAM
StreamConstPtr css(F("my long css data")); // CSS data not copied to RAM
server.sendAll(css);
- ``S2Stream::`` is designed to make a ``Stream::`` out of a ``String::`` without copy.
Expand Down
21 changes: 21 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ uint8_t WiFiClientSecureCtx::connected() {
return false;
}

int WiFiClientSecureCtx::availableForWrite () {
// code taken from ::_write()
if (!connected() || !_handshake_done) {
return 0;
}
// Get BearSSL to a state where we can send
if (_run_until(BR_SSL_SENDAPP) < 0) {
return 0;
}
if (br_ssl_engine_current_state(_eng) & BR_SSL_SENDAPP) {
size_t sendapp_len;
(void)br_ssl_engine_sendapp_buf(_eng, &sendapp_len);
// We want to call br_ssl_engine_sendapp_ack(0) but 0 is forbidden (bssl doc).
// After checking br_ssl_engine_sendapp_buf() src code,
// it seems that it is OK to not call ack when the buffer is left untouched.
//forbidden: br_ssl_engine_sendapp_ack(_eng, 0);
return (int)sendapp_len;
}
return 0;
}

size_t WiFiClientSecureCtx::_write(const uint8_t *buf, size_t size, bool pmem) {
size_t sent_bytes = 0;

Expand Down
3 changes: 3 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class WiFiClientSecureCtx : public WiFiClient {
void flush() override { (void)flush(0); }
void stop() override { (void)stop(0); }

int availableForWrite() override;

// Allow sessions to be saved/restored automatically to a memory area
void setSession(Session *session) { _session = session; }

Expand Down Expand Up @@ -249,6 +251,7 @@ class WiFiClientSecure : public WiFiClient {
size_t write(Stream& stream) /* Note this is not virtual */ { return _ctx->write(stream); }
int read(uint8_t *buf, size_t size) override { return _ctx->read(buf, size); }
int available() override { return _ctx->available(); }
int availableForWrite() override { return _ctx->availableForWrite(); }
int read() override { return _ctx->read(); }
int peek() override { return _ctx->peek(); }
size_t peekBytes(uint8_t *buffer, size_t length) override { return _ctx->peekBytes(buffer, length); }
Expand Down

0 comments on commit c1118df

Please sign in to comment.