swixteen  0.03
Switch a lamp on/off with X10 RF commands
 All Files Functions Variables Macros Pages
GpioExport.c
Go to the documentation of this file.
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 
7 #include "../Sysfs/Sysfs.h"
8 #include "../config.h"
9 
10 #define BUFFER_MAX 3 /* pin 0 .. 99 and a trailing zero */
11 
17 int GpioExport(int Pin)
18  {
19  char buffer[BUFFER_MAX];
20  ssize_t BytesToWrite;
21  ssize_t BytesWritten;
22  int fd;
23 
24  fd = open("/sys/class/gpio/export", O_WRONLY);
25  if (fd == -1)
26  {
27  fprintf(stderr, "201309202230 Failed to open export for writing, are you root?\n");
28  return(-1);
29  }
30 
31  BytesToWrite = snprintf(buffer, BUFFER_MAX, "%d", Pin);
32  BytesWritten = write(fd, buffer, BytesToWrite);
33  if( BytesWritten != BytesToWrite )
34  {
35  fprintf( stderr, "201309210045 Encountered short write\n" );
36  fprintf( stderr, "201309210045 BytesToWrite was %i, BytesWritten was %i\n", (int)BytesToWrite, (int)BytesWritten );
37  return(-2);
38  }
39 
40  close(fd);
41  return(0);
42  }
43