Skip to content

Commit ebc4a8d

Browse files
authored
Merge pull request #25 from tier4/feat/close_exist_port_on_start
feat: close GPIO port on node boot if is is already used
2 parents 5fbaae9 + 4cb91a0 commit ebc4a8d

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

src/jetson_gpio.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <sensor_trigger/jetson_gpio.hpp>
1616

1717
#include <cstdio>
18+
#include <sys/stat.h>
1819

1920
namespace jetson_gpio
2021
{
@@ -65,6 +66,15 @@ bool JetsonGpio::export_gpio()
6566
int file_descriptor, buffer_length;
6667
char buffer[BUFFER_SIZE];
6768

69+
// Check target GPIO is available (not opened) first.
70+
// If it is unavailable, close the port and try to open it.
71+
std::string target_gpio = std::string(SYSFS_GPIO_DIR) + std::string("/gpio")
72+
+ std::to_string(gpio_);
73+
struct stat return_status;
74+
if (stat(target_gpio.c_str(), &return_status) == 0) {
75+
unexport_gpio();
76+
}
77+
6878
file_descriptor = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
6979
if (file_descriptor < 0) {
7080
return false;

0 commit comments

Comments
 (0)