@@ -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+
4145const char * stredge [4 ] = {"none" , "rising" , "falling" , "both" };
4246
4347// file descriptors
@@ -60,15 +64,8 @@ struct callback
6064};
6165struct callback * callbacks = NULL ;
6266
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-
7167pthread_t threads ;
68+ int exported_gpios [120 ] = { GPIO_NOT_EXPORTED };
7269int event_occurred [120 ] = { 0 };
7370int thread_running = 0 ;
7471int epfd = -1 ;
@@ -77,7 +74,20 @@ int gpio_export(unsigned int gpio)
7774{
7875 int fd , len ;
7976 char str_gpio [10 ];
80- struct gpio_exp * new_gpio , * g ;
77+
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+ }
8191
8292 if ((fd = open ("/sys/class/gpio/export" , O_WRONLY )) < 0 )
8393 {
@@ -91,24 +101,7 @@ int gpio_export(unsigned int gpio)
91101 }
92102
93103 // 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- }
104+ exported_gpios [gpio ] = GPIO_EXPORTED ;
112105 return 0 ;
113106}
114107
@@ -193,7 +186,9 @@ int gpio_unexport(unsigned int gpio)
193186{
194187 int fd , len ;
195188 char str_gpio [10 ];
196- struct gpio_exp * g , * temp , * prev_g = NULL ;
189+
190+ if (exported_gpios [gpio ] != GPIO_EXPORTED )
191+ return 0 ;
197192
198193 close_value_fd (gpio );
199194
@@ -208,24 +203,8 @@ int gpio_unexport(unsigned int gpio)
208203 }
209204
210205 // 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 ;
206+ exported_gpios [gpio ] = GPIO_NOT_EXPORTED ;
207+ return 0 ;
229208}
230209
231210int gpio_set_direction (unsigned int gpio , unsigned int in_flag )
@@ -381,9 +360,10 @@ unsigned int gpio_lookup(int fd)
381360
382361void exports_cleanup (void )
383362{
363+ int i ;
384364 // unexport everything
385- while ( exported_gpios != NULL )
386- gpio_unexport (exported_gpios -> gpio );
365+ for ( i = 0 ; i < 120 ; ++ i )
366+ gpio_unexport (i );
387367}
388368
389369int add_edge_callback (unsigned int gpio , void (* func )(unsigned int gpio ))
0 commit comments