Skip to content

Commit dcba8f6

Browse files
author
Marius Elvert
committed
Handle already exported pins
1 parent 5bc893a commit dcba8f6

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

source/event_gpio.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ SOFTWARE.
3838
#include "event_gpio.h"
3939
#include "common.h"
4040

41+
#define GPIO_NOT_EXPORTED 0
42+
#define GPIO_EXPORTED 1
43+
#define GPIO_ALREADY_EXPORTED 2
44+
4145
const char *stredge[4] = {"none", "rising", "falling", "both"};
4246

4347
// file descriptors
@@ -61,7 +65,7 @@ struct callback
6165
struct callback *callbacks = NULL;
6266

6367
pthread_t threads;
64-
int exported_gpios[120] = { 0 };
68+
int exported_gpios[120] = { GPIO_NOT_EXPORTED };
6569
int event_occurred[120] = { 0 };
6670
int thread_running = 0;
6771
int epfd = -1;
@@ -71,9 +75,19 @@ int gpio_export(unsigned int gpio)
7175
int fd, len;
7276
char str_gpio[10];
7377

74-
// already exported
75-
if (exported_gpios[gpio] != 0)
76-
return 1;
78+
// already exported by us?
79+
if (exported_gpios[gpio] != GPIO_NOT_EXPORTED)
80+
return 0;
81+
82+
// already exported by someone else?
83+
char gpio_path[64];
84+
snprintf(gpio_path, sizeof(gpio_path), "/sys/class/gpio/gpio%d", gpio);
85+
86+
if (access(gpio_path, R_OK|W_OK|X_OK) != -1)
87+
{
88+
exported_gpios[gpio] = GPIO_ALREADY_EXPORTED;
89+
return 0;
90+
}
7791

7892
if ((fd = open("/sys/class/gpio/export", O_WRONLY)) < 0)
7993
{
@@ -87,7 +101,7 @@ int gpio_export(unsigned int gpio)
87101
}
88102

89103
// add to list
90-
exported_gpios[gpio] = 1;
104+
exported_gpios[gpio] = GPIO_EXPORTED;
91105
return 0;
92106
}
93107

@@ -173,7 +187,7 @@ int gpio_unexport(unsigned int gpio)
173187
int fd, len;
174188
char str_gpio[10];
175189

176-
if (exported_gpios[gpio] == 0)
190+
if (exported_gpios[gpio] != GPIO_EXPORTED)
177191
return 0;
178192

179193
close_value_fd(gpio);
@@ -189,7 +203,7 @@ int gpio_unexport(unsigned int gpio)
189203
}
190204

191205
// remove from list
192-
exported_gpios[gpio] = 0;
206+
exported_gpios[gpio] = GPIO_NOT_EXPORTED;
193207
return 0;
194208
}
195209

0 commit comments

Comments
 (0)