An Arduino API that sits on top of Linux and other operating systems. This lets you run Arduino code on Raspberry PI, desktops, etc... All as a standard user-space application.
GNU Lesser General Public License v2.1
22
stars
11
forks
source link
Add error handling for LinuxSerial::available ioctl call #13
If LinuxSerial::available() is called when the port has not been opened the ioctl will currently silently fail (from a EBADF) and return an undefined value for bytes.
Reproduction
On a test environment consisting of a:
RaspberryPi 3B+
A UBlox NEO-6M module attached to /dev/ttyAMA0
Native build of the meshtastic firmware for aarch64
The following meshtastic config.yaml set:
...
GPS:
SerialPath: /dev/ttyAMA0
...
The following function will fail to to the value for x being a potentially undefined value returned by available:
...
void GPS::setGPSPower(bool on, bool standbyOnly, uint32_t sleepTime)
{
LOG_INFO("Setting GPS power=%d\n", on);
if (on) {
clearBuffer(); // drop any old data waiting in the buffer before re-enabling
if (en_gpio)
digitalWrite(en_gpio, on ? GPS_EN_ACTIVE : !GPS_EN_ACTIVE); // turn this on if defined, every time
...
Calls to LinuxSerial::available() work as expected once LinuxSerial::begin(...) has been called.
Problem
If
LinuxSerial::available()
is called when the port has not been opened theioctl
will currently silently fail (from aEBADF
) and return an undefined value forbytes
.Reproduction
On a test environment consisting of a:
/dev/ttyAMA0
The following function will fail to to the value for
x
being a potentially undefined value returned byavailable
:https://github.com/meshtastic/firmware/blob/d604a76c7325e54122306308b5c22298ae101726/src/gps/GPS.cpp#L750
This is called very early during startup before the
begin
is called on:https://github.com/meshtastic/firmware/blob/d604a76c7325e54122306308b5c22298ae101726/src/gps/GPS.cpp#L476
Calls to
LinuxSerial::available()
work as expected onceLinuxSerial::begin(...)
has been called.Solution
Add a check for the return code of the
ioctl
used inLinuxSerial::available()
and return zero on error. Returning zero appears to be consistent with the esp32 arduino core does on error: https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-uart.c#L624Testing
Before change:
After change: