@@ -28,13 +28,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828SOFTWARE.
2929*/
3030
31+ #include <errno.h>
32+ #include <fcntl.h>
3133#include <pthread.h>
32- #include <sys/epoll.h>
3334#include <stdio.h>
3435#include <stdlib.h>
35- #include <fcntl.h>
36- #include <unistd.h>
3736#include <string.h>
37+ #include <sys/epoll.h>
38+ #include <syslog.h>
39+ #include <unistd.h>
40+
3841#include "event_gpio.h"
3942#include "common.h"
4043
@@ -70,14 +73,16 @@ int event_occurred[120] = { 0 };
7073int thread_running = 0 ;
7174int epfd = -1 ;
7275
73- int gpio_export (unsigned int gpio )
76+ BBIO_err gpio_export (unsigned int gpio )
7477{
7578 int fd , len ;
7679 char str_gpio [10 ];
7780
7881 // already exported by us?
79- if (exported_gpios [gpio ] != GPIO_NOT_EXPORTED )
80- return 0 ;
82+ if (exported_gpios [gpio ] != GPIO_NOT_EXPORTED ) {
83+ syslog (LOG_DEBUG , "gpio_export: %u already exported" , gpio );
84+ return BBIO_OK ;
85+ }
8186
8287 // already exported by someone else?
8388 char gpio_path [64 ];
@@ -86,23 +91,33 @@ int gpio_export(unsigned int gpio)
8691 if (access (gpio_path , R_OK |W_OK |X_OK ) != -1 )
8792 {
8893 exported_gpios [gpio ] = GPIO_ALREADY_EXPORTED ;
89- return 0 ;
94+ syslog (LOG_DEBUG , "gpio_export: %u already exported" , gpio );
95+ return BBIO_OK ;
9096 }
9197
92- if ((fd = open ("/sys/class/gpio/export" , O_WRONLY )) < 0 )
98+ #define GPIO_EXPORT "/sys/class/gpio/export"
99+
100+ if ((fd = open (GPIO_EXPORT , O_WRONLY )) < 0 )
93101 {
94- return -1 ;
102+ syslog (LOG_ERR , "gpio_export: %u couldn't open '" GPIO_EXPORT "': %i-%s" ,
103+ gpio , errno , strerror (errno ));
104+ return BBIO_SYSFS ;
95105 }
106+
96107 len = snprintf (str_gpio , sizeof (str_gpio ), "%d" , gpio );
97108 int ret = write (fd , str_gpio , len );
98109 close (fd );
99110 if (ret < 0 ) {
100- return ret ;
111+ syslog (LOG_ERR , "gpio_export: %u couldn't write '" GPIO_EXPORT "': %i-%s" ,
112+ gpio , errno , strerror (errno ));
113+ return BBIO_SYSFS ;
101114 }
102115
103116 // add to list
104117 exported_gpios [gpio ] = GPIO_EXPORTED ;
105- return 0 ;
118+
119+ syslog (LOG_DEBUG , "gpio_export: %u OK" , gpio );
120+ return BBIO_OK ;
106121}
107122
108123void close_value_fd (unsigned int gpio )
@@ -176,13 +191,15 @@ int open_value_file(unsigned int gpio)
176191 }
177192
178193 if ((fd = open (filename , O_RDONLY | O_NONBLOCK )) < 0 ) {
179- return -1 ;
194+ syslog (LOG_ERR , "gpio open_value_file: %u couldn't open '%s': %i-%s" ,
195+ gpio , filename , errno , strerror (errno ));
196+ return -1 ;
180197 }
181198 add_fd_list (gpio , fd );
182199 return fd ;
183200}
184201
185- int gpio_unexport (unsigned int gpio )
202+ BBIO_err gpio_unexport (unsigned int gpio )
186203{
187204 int fd , len ;
188205 char str_gpio [10 ];
@@ -192,34 +209,47 @@ int gpio_unexport(unsigned int gpio)
192209
193210 close_value_fd (gpio );
194211
195- if ((fd = open ("/sys/class/gpio/unexport" , O_WRONLY )) < 0 )
196- return -1 ;
212+ #define GPIO_UNEXPORT "/sys/class/gpio/unexport"
213+
214+ if ((fd = open (GPIO_UNEXPORT , O_WRONLY )) < 0 ) {
215+ syslog (LOG_ERR , "gpio_unexport: %u couldn't open '" GPIO_UNEXPORT "': %i-%s" ,
216+ gpio , errno , strerror (errno ));
217+ return BBIO_SYSFS ;
218+ }
197219
198220 len = snprintf (str_gpio , sizeof (str_gpio ), "%d" , gpio );
199221 int ret = write (fd , str_gpio , len );
200222 close (fd );
201223 if (ret < 0 ) {
202- return ret ;
224+ syslog (LOG_ERR , "gpio_unexport: %u couldn't write '" GPIO_UNEXPORT "': %i-%s" ,
225+ gpio , errno , strerror (errno ));
226+ return BBIO_SYSFS ;
203227 }
204228
205229 // remove from list
206230 exported_gpios [gpio ] = GPIO_NOT_EXPORTED ;
207- return 0 ;
231+
232+ syslog (LOG_DEBUG , "gpio_unexport: %u OK" , gpio );
233+ return BBIO_OK ;
208234}
209235
210- int gpio_set_direction (unsigned int gpio , unsigned int in_flag )
236+ BBIO_err gpio_set_direction (unsigned int gpio , unsigned int in_flag )
211237{
212238 int fd ;
213239 char filename [40 ];
214240 char direction [10 ] = { 0 };
215241
216242 if ((gpio >= USR_LED_GPIO_MIN ) && (gpio <= USR_LED_GPIO_MAX )) {
217- return 0 ; // direction is not applicable to the USR LED pins
243+ syslog (LOG_DEBUG , "gpio_set_direction: %u not applicable to the USR LED" , gpio );
244+ return BBIO_OK ; // direction is not applicable to the USR LED pins
218245 }
219246
220247 snprintf (filename , sizeof (filename ), "/sys/class/gpio/gpio%d/direction" , gpio );
221- if ((fd = open (filename , O_WRONLY )) < 0 )
222- return -1 ;
248+ if ((fd = open (filename , O_WRONLY )) < 0 ) {
249+ syslog (LOG_ERR , "gpio_set_direction: %u couldn't open '%s': %i-%s" ,
250+ gpio , filename , errno , strerror (errno ));
251+ return BBIO_SYSFS ;
252+ }
223253
224254 if (in_flag ) {
225255 strncpy (direction , "out" , ARRAY_SIZE (direction ) - 1 );
@@ -230,38 +260,47 @@ int gpio_set_direction(unsigned int gpio, unsigned int in_flag)
230260 int ret = write (fd , direction , strlen (direction ));
231261 close (fd );
232262 if (ret < 0 ) {
233- return ret ;
263+ syslog (LOG_ERR , "gpio_set_direction: %u couldn't write '%s': %i-%s" ,
264+ gpio , filename , errno , strerror (errno ));
265+ return BBIO_SYSFS ;
234266 }
235267
236- return 0 ;
268+ syslog (LOG_DEBUG , "gpio_set_direction: %u OK" , gpio );
269+ return BBIO_OK ;
237270}
238271
239- int gpio_get_direction (unsigned int gpio , unsigned int * value )
272+ BBIO_err gpio_get_direction (unsigned int gpio , unsigned int * value )
240273{
241274 int fd ;
242275 char direction [4 ] = { 0 };
243276 char filename [40 ];
244277
245278 snprintf (filename , sizeof (filename ), "/sys/class/gpio/gpio%d/direction" , gpio );
246- if ((fd = open (filename , O_RDONLY | O_NONBLOCK )) < 0 )
247- return -1 ;
279+ if ((fd = open (filename , O_RDONLY | O_NONBLOCK )) < 0 ) {
280+ syslog (LOG_ERR , "gpio_get_direction: %u couldn't open '%s': %i-%s" ,
281+ gpio , filename , errno , strerror (errno ));
282+ return BBIO_SYSFS ;
283+ }
248284
249285 lseek (fd , 0 , SEEK_SET );
250286 int ret = read (fd , & direction , sizeof (direction ) - 1 );
251287 if (ret < 0 ) {
252- return ret ;
288+ syslog (LOG_ERR , "gpio_get_direction: %u couldn't read '%s': %i-%s" ,
289+ gpio , filename , errno , strerror (errno ));
290+ return BBIO_SYSFS ;
253291 }
254292
255293 if (strcmp (direction , "out" ) == 0 ) {
256294 * value = OUTPUT ;
257295 } else {
258296 * value = INPUT ;
259297 }
260-
261- return 0 ;
298+
299+ syslog (LOG_DEBUG , "gpio_get_direction: %u OK" , gpio );
300+ return BBIO_OK ;
262301}
263302
264- int gpio_set_value (unsigned int gpio , unsigned int value )
303+ BBIO_err gpio_set_value (unsigned int gpio , unsigned int value )
265304{
266305 int fd ;
267306 char filename [MAX_FILENAME ];
@@ -283,7 +322,9 @@ int gpio_set_value(unsigned int gpio, unsigned int value)
283322 }
284323
285324 if ((fd = open (filename , O_WRONLY )) < 0 ) {
286- return -1 ;
325+ syslog (LOG_ERR , "gpio_set_value: %u couldn't open '%s': %i-%s" ,
326+ gpio , filename , errno , strerror (errno ));
327+ return BBIO_SYSFS ;
287328 }
288329
289330 if (value ) {
@@ -295,27 +336,35 @@ int gpio_set_value(unsigned int gpio, unsigned int value)
295336 int ret = write (fd , vstr , strlen (vstr ));
296337 close (fd );
297338 if (ret < 0 ) {
298- return ret ;
339+ syslog (LOG_ERR , "gpio_set_value: %u couldn't write '%s': %i-%s" ,
340+ gpio , filename , errno , strerror (errno ));
341+ return BBIO_SYSFS ;
299342 }
300343
301- return 0 ;
344+ syslog (LOG_DEBUG , "gpio_set_value: %u %u OK" , gpio , value );
345+ return BBIO_OK ;
302346}
303347
304- int gpio_get_value (unsigned int gpio , unsigned int * value )
348+ BBIO_err gpio_get_value (unsigned int gpio , unsigned int * value )
305349{
306350 int fd = fd_lookup (gpio );
307351 char ch ;
308352
309353 if (!fd )
310354 {
311- if ((fd = open_value_file (gpio )) == -1 )
312- return -1 ;
355+ if ((fd = open_value_file (gpio )) == -1 ) {
356+ syslog (LOG_ERR , "gpio_set_value: %u couldn't open value file: %i-%s" ,
357+ gpio , errno , strerror (errno ));
358+ return BBIO_SYSFS ;
359+ }
313360 }
314361
315362 lseek (fd , 0 , SEEK_SET );
316363 int ret = read (fd , & ch , sizeof (ch ));
317364 if (ret < 0 ) {
318- return ret ;
365+ syslog (LOG_ERR , "gpio_set_value: %u couldn't read value file: %i-%s" ,
366+ gpio , errno , strerror (errno ));
367+ return BBIO_SYSFS ;
319368 }
320369
321370 if (ch != '0' ) {
@@ -324,7 +373,8 @@ int gpio_get_value(unsigned int gpio, unsigned int *value)
324373 * value = 0 ;
325374 }
326375
327- return 0 ;
376+ syslog (LOG_DEBUG , "gpio_get_value: %u OK" , gpio );
377+ return BBIO_OK ;
328378}
329379
330380int gpio_set_edge (unsigned int gpio , unsigned int edge )
0 commit comments