Skip to content

Commit

Permalink
cache gpio chip detection when properly initialized (#962)
Browse files Browse the repository at this point in the history
This will prevent falling back to `/dev/gpiochip0` when `/dev/gpiochipX` was previously opened successfully.

This is designed to alert users when multiple processes are simultaneously using `/dev/gpiochipX`, but it should only happen if either
1. another process is spamming/hogging the gpiochipX.
2. the rare occurrence when RF24 needs to open gpiochipX while it is still used by another process.
  • Loading branch information
2bndy5 authored Mar 19, 2024
1 parent c6c0191 commit a5a2087
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions utility/RPi/interrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ void IrqChipCache::openDevice()
msg += "; ";
msg += strerror(errno);
throw IRQException(msg);
return;
}
}
chipInitialized = true;
}

void IrqChipCache::closeDevice()
Expand Down Expand Up @@ -89,6 +91,10 @@ int attachInterrupt(rf24_gpio_pin_t pin, int mode, void (*function)(void))
irqChipCache.openDevice();
}
catch (IRQException& exc) {
if (irqChipCache.chipInitialized) {
throw exc;
return 0;
}
irqChipCache.chip = "/dev/gpiochip0";
irqChipCache.openDevice();
}
Expand Down
1 change: 1 addition & 0 deletions utility/RPi/interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct IrqChipCache
{
const char* chip = RF24_LINUX_GPIO_CHIP;
int fd = -1;
bool chipInitialized = false;

/// Open the File Descriptor for the GPIO chip
void openDevice();
Expand Down
6 changes: 6 additions & 0 deletions utility/SPIDEV/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ void GPIOChipCache::openDevice()
msg += "; ";
msg += strerror(errno);
throw GPIOException(msg);
return;
}
}
chipInitialized = true;
}

void GPIOChipCache::closeDevice()
Expand Down Expand Up @@ -77,6 +79,10 @@ void GPIO::open(rf24_gpio_pin_t port, int DDR)
gpioCache.openDevice();
}
catch (GPIOException& exc) {
if (gpioCache.chipInitialized) {
throw exc;
return;
}
gpioCache.chip = "/dev/gpiochip0";
gpioCache.openDevice();
}
Expand Down
1 change: 1 addition & 0 deletions utility/SPIDEV/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct GPIOChipCache
{
const char* chip = RF24_LINUX_GPIO_CHIP;
int fd = -1;
bool chipInitialized = false;

/// Open the File Descriptor for the GPIO chip
void openDevice();
Expand Down
4 changes: 4 additions & 0 deletions utility/SPIDEV/interrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ int attachInterrupt(rf24_gpio_pin_t pin, int mode, void (*function)(void))
irqChipCache.openDevice();
}
catch (GPIOException& exc) {
if (irqChipCache.chipInitialized) {
throw exc;
return 0;
}
irqChipCache.chip = "/dev/gpiochip0";
irqChipCache.openDevice();
}
Expand Down

0 comments on commit a5a2087

Please sign in to comment.