Skip to content

Commit

Permalink
BREAKING: Add Print::availableForWrite method (#7658)
Browse files Browse the repository at this point in the history
* Add Print::availableForWrite method

Adds an availableForWrite() method to the Print class, matching current
ArduinoCore-API commit 398e70f188e2b861c10d9ffe5e2bfcb6a4a4f489 .

Hook availableForWrite into the SDFS filesystem (other FSes don't have
this capability built-in).

Fixes #7650

* WiFiClient::availableForWrite proto matching Print

* Fix Netdump signedness warning

* Clean up Serial availableForWrite

This is evidently a breaking change due to the type difference.
Arduino's `availableForWrite` returns an `int`, while the
(multiply-implemented, non-virtual) core `availableForWrite` returned
`size_t`.
  • Loading branch information
earlephilhower committed Oct 27, 2020
1 parent 95de525 commit 7f38e14
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 9 deletions.
8 changes: 8 additions & 0 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ int File::available() {
return _p->size() - _p->position();
}

int File::availableForWrite() {
if (!_p)
return false;

return _p->availableForWrite();
}


int File::read() {
if (!_p)
return -1;
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class File : public Stream
// Print methods:
size_t write(uint8_t) override;
size_t write(const uint8_t *buf, size_t size) override;
int availableForWrite() override;

// Stream methods:
int available() override;
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class FileImpl {
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
virtual size_t position() const = 0;
virtual size_t size() const = 0;
virtual int availableForWrite() { return 0; }
virtual bool truncate(uint32_t size) = 0;
virtual void close() = 0;
virtual const char* name() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class HardwareSerial: public Stream
{
return readBytes((char*)buffer, size);
}
int availableForWrite(void)
int availableForWrite(void) override
{
return static_cast<int>(uart_tx_free(_uart));
}
Expand Down
4 changes: 4 additions & 0 deletions cores/esp8266/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class Print {
inline size_t write(char c) { return write((uint8_t) c); }
inline size_t write(int8_t c) { return write((uint8_t) c); }

// default to zero, meaning "a single write may block"
// should be overriden by subclasses with buffering
virtual int availableForWrite() { return 0; }

size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
size_t print(const __FlashStringHelper *);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ void loop() {

// determine maximum output size "fair TCP use"
// client.availableForWrite() returns 0 when !client.connected()
size_t maxToTcp = 0;
int maxToTcp = 0;
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
if (serverClients[i]) {
size_t afw = serverClients[i].availableForWrite();
int afw = serverClients[i].availableForWrite();
if (afw) {
if (!maxToTcp) {
maxToTcp = afw;
Expand All @@ -190,11 +190,11 @@ void loop() {
}

//check UART for data
size_t len = std::min((size_t)Serial.available(), maxToTcp);
size_t len = std::min(Serial.available(), maxToTcp);
len = std::min(len, (size_t)STACK_PROTECTOR);
if (len) {
uint8_t sbuf[len];
size_t serial_got = Serial.readBytes(sbuf, len);
int serial_got = Serial.readBytes(sbuf, len);
// push UART data to all connected telnet clients
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
// if client.availableForWrite() was 0 (congested)
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ bool WiFiClient::getSync() const
return _client->getSync();
}

size_t WiFiClient::availableForWrite ()
int WiFiClient::availableForWrite ()
{
return _client? _client->availableForWrite(): 0;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class WiFiClient : public Client, public SList<WiFiClient> {

static void setLocalPortStart(uint16_t port) { _localPort = port; }

size_t availableForWrite();
int availableForWrite() override;

friend class WiFiServer;

Expand Down
4 changes: 2 additions & 2 deletions libraries/Netdump/src/Netdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void Netdump::tcpDumpProcess(const Packet& np)
bufferIndex += incl_len;
}

if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= bufferIndex)
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= (int)bufferIndex)
{
tcpDumpClient.write(packetBuffer, bufferIndex);
bufferIndex = 0;
Expand All @@ -202,7 +202,7 @@ void Netdump::tcpDumpLoop(WiFiServer &tcpDumpServer, const Filter nf)
{
setCallback(nullptr);
}
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= bufferIndex)
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= (int)bufferIndex)
{
tcpDumpClient.write(packetBuffer, bufferIndex);
bufferIndex = 0;
Expand Down
5 changes: 5 additions & 0 deletions libraries/SDFS/src/SDFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ class SDFSFileImpl : public FileImpl
close();
}

int availableForWrite() override
{
return _opened ? _fd->availableForWrite() : 0;
}

size_t write(const uint8_t *buf, size_t size) override
{
return _opened ? _fd->write(buf, size) : -1;
Expand Down

0 comments on commit 7f38e14

Please sign in to comment.