Skip to content

Commit 5bc893a

Browse files
author
Marius Elvert
committed
Use lookup table to prevent duplicate pin export
1 parent 921001c commit 5bc893a

File tree

1 file changed

+14
-48
lines changed

1 file changed

+14
-48
lines changed

source/event_gpio.c

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,8 @@ struct callback
6060
};
6161
struct callback *callbacks = NULL;
6262

63-
// gpio exports
64-
struct gpio_exp
65-
{
66-
unsigned int gpio;
67-
struct gpio_exp *next;
68-
};
69-
struct gpio_exp *exported_gpios = NULL;
70-
7163
pthread_t threads;
64+
int exported_gpios[120] = { 0 };
7265
int event_occurred[120] = { 0 };
7366
int thread_running = 0;
7467
int epfd = -1;
@@ -77,7 +70,10 @@ int gpio_export(unsigned int gpio)
7770
{
7871
int fd, len;
7972
char str_gpio[10];
80-
struct gpio_exp *new_gpio, *g;
73+
74+
// already exported
75+
if (exported_gpios[gpio] != 0)
76+
return 1;
8177

8278
if ((fd = open("/sys/class/gpio/export", O_WRONLY)) < 0)
8379
{
@@ -91,24 +87,7 @@ int gpio_export(unsigned int gpio)
9187
}
9288

9389
// add to list
94-
new_gpio = malloc(sizeof(struct gpio_exp));
95-
if (new_gpio == 0)
96-
return -1; // out of memory
97-
98-
new_gpio->gpio = gpio;
99-
new_gpio->next = NULL;
100-
101-
if (exported_gpios == NULL)
102-
{
103-
// create new list
104-
exported_gpios = new_gpio;
105-
} else {
106-
// add to end of existing list
107-
g = exported_gpios;
108-
while (g->next != NULL)
109-
g = g->next;
110-
g->next = new_gpio;
111-
}
90+
exported_gpios[gpio] = 1;
11291
return 0;
11392
}
11493

@@ -193,7 +172,9 @@ int gpio_unexport(unsigned int gpio)
193172
{
194173
int fd, len;
195174
char str_gpio[10];
196-
struct gpio_exp *g, *temp, *prev_g = NULL;
175+
176+
if (exported_gpios[gpio] == 0)
177+
return 0;
197178

198179
close_value_fd(gpio);
199180

@@ -208,24 +189,8 @@ int gpio_unexport(unsigned int gpio)
208189
}
209190

210191
// remove from list
211-
g = exported_gpios;
212-
while (g != NULL)
213-
{
214-
if (g->gpio == gpio)
215-
{
216-
if (prev_g == NULL)
217-
exported_gpios = g->next;
218-
else
219-
prev_g->next = g->next;
220-
temp = g;
221-
g = g->next;
222-
free(temp);
223-
} else {
224-
prev_g = g;
225-
g = g->next;
226-
}
227-
}
228-
return 0;
192+
exported_gpios[gpio] = 0;
193+
return 0;
229194
}
230195

231196
int gpio_set_direction(unsigned int gpio, unsigned int in_flag)
@@ -381,9 +346,10 @@ unsigned int gpio_lookup(int fd)
381346

382347
void exports_cleanup(void)
383348
{
349+
int i;
384350
// unexport everything
385-
while (exported_gpios != NULL)
386-
gpio_unexport(exported_gpios->gpio);
351+
for (i = 0; i < 120; ++i)
352+
gpio_unexport(i);
387353
}
388354

389355
int add_edge_callback(unsigned int gpio, void (*func)(unsigned int gpio))

0 commit comments

Comments
 (0)