Skip to content

(re)introduce timeout in HardwareSerial::readBytes(buffer, size) #5558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cores/esp8266/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <PolledTimeout.h>
#include "Arduino.h"
#include "HardwareSerial.h"
#include "Esp.h"
Expand Down Expand Up @@ -132,6 +133,22 @@ unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis)
return detectedBaudrate;
}

size_t HardwareSerial::readBytes(char* buffer, size_t size)
{
size_t got = 0;

while (got < size)
{
esp8266::polledTimeout::oneShot timeOut(_timeout);
size_t avail;
while ((avail = available()) == 0 && !timeOut);
if (avail == 0)
Copy link
Collaborator

@devyte devyte Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the correct condition. It's common to check for the counterpart, i.e.: if(timeOut) in this case, but this is better, because it accounts for the case when both sides of the && become false at the same time.

break;
got += read(buffer + got, std::min(size - got, avail));
}
return got;
}

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
HardwareSerial Serial(UART0);
#endif
Expand Down
6 changes: 4 additions & 2 deletions cores/esp8266/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ class HardwareSerial: public Stream
// return -1 when data is unvailable (arduino api)
return uart_read_char(_uart);
}
size_t readBytes(char* buffer, size_t size) override
// ::read(buffer, size): same as readBytes without timeout
size_t read(char* buffer, size_t size)
{
return uart_read(_uart, buffer, size);
}
size_t readBytes(char* buffer, size_t size) override;
size_t readBytes(uint8_t* buffer, size_t size) override
{
return uart_read(_uart, (char*)buffer, size);
return readBytes((char*)buffer, size);
}
int availableForWrite(void)
{
Expand Down
22 changes: 17 additions & 5 deletions libraries/esp8266/examples/SerialStress/SerialStress.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
#define BUFFER_SIZE 4096 // may be useless to use more than 2*SERIAL_SIZE_RX
#define SERIAL_SIZE_RX 1024 // Serial.setRxBufferSize()

#define FAKE_INCREASED_AVAILABLE 100 // test readBytes's timeout

#define TIMEOUT 5000
#define DEBUG(x...) //x

uint8_t buf [BUFFER_SIZE];
uint8_t temp [BUFFER_SIZE];
bool reading = true;
size_t testReadBytesTimeout = 0;

static size_t out_idx = 0, in_idx = 0;
static size_t local_receive_size = 0;
Expand Down Expand Up @@ -83,6 +86,7 @@ void setup() {
size_for_1sec = baud / 10; // 8n1=10baudFor8bits
logger->printf("led changes state every %zd bytes (= 1 second)\n", size_for_1sec);
logger->printf("press 's' to stop reading, not writing (induces overrun)\n");
logger->printf("press 't' to toggle timeout testing on readBytes\n");

// prepare send/compare buffer
for (size_t i = 0; i < sizeof buf; i++) {
Expand Down Expand Up @@ -136,7 +140,7 @@ void loop() {

if (reading) {
// receive data
maxlen = Serial.available();
maxlen = Serial.available() + testReadBytesTimeout;
if (maxlen > maxavail) {
maxavail = maxlen;
}
Expand Down Expand Up @@ -180,8 +184,16 @@ void loop() {
timeout = (last_ms = now_ms) + TIMEOUT;
}

if (logger->read() == 's') {
logger->println("now stopping reading, keeping writing");
reading = false;
}
if (logger->available())
switch (logger->read()) {
case 's':
logger->println("now stopping reading, keeping writing");
reading = false;
break;
case 't':
testReadBytesTimeout ^= FAKE_INCREASED_AVAILABLE;
logger->printf("testing readBytes timeout: %d\n", !!testReadBytesTimeout);
break;
default:;
}
}
17 changes: 17 additions & 0 deletions tests/host/common/MockSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include <Arduino.h>
#include <PolledTimeout.h>

#include <unistd.h> // write

Expand Down Expand Up @@ -64,6 +65,22 @@ void HardwareSerial::flush ()
fflush(stdout);
}

size_t HardwareSerial::readBytes(char* buffer, size_t size)
{
size_t got = 0;

while (got < size)
{
esp8266::polledTimeout::oneShot timeOut(_timeout);
size_t avail;
while ((avail = available()) == 0 && !timeOut);
if (avail == 0)
break;
got += read(buffer + got, std::min(size - got, avail));
}
return got;
}

// uart.c

extern "C"
Expand Down