gpio  1.4.2
General Purpose I/O Package
gpioexport

Macros

#define APP_EC_OK   0
 success exit code
 
#define APP_EC_ARGS   2
 command-line options/arguments error exit code
 
#define APP_EC_EXEC   4
 execution exit code
 
#define NO_ARG   -1
 no argument
 
#define APP_EC_OK   0
 success exit code
 
#define APP_EC_ARGS   2
 command-line options/arguments error exit code
 
#define APP_EC_EXEC   4
 execution exit code
 

Functions

static void badCmdExit ()
 Exit program on bad command-line values.
 
static int strToInt (const string &str, int &val)
 Convert string to integer.
 
static int strToMode (const string &str, mode_t &mode)
 Convert string to file permissions.
 
static void mainInit (int argc, char *argv[])
 Main initialization. More...
 
int main (int argc, char *argv[])
 Main. More...
 

Variables

static char * Argv0
 the command
 
static char * OptsMode = NULL
 permissions
 
static int ArgsGpioNum
 gpio number
 
static int ArgsGpioDir
 gpio direction
 
static int ArgsGpioEdge
 gpio edge trigger type
 
static mode_t Permissions
 gpio permissions
 
static OptsPgmInfo_T PgmInfo
 Program information. More...
 
static OptsInfo_T OptsInfo []
 Command line options information. More...
 
static char * Argv0
 the command
 
static bool_t OptsMonitor = false
 do [not] keep listening for events
 
static double OptsTimeout = 0.0
 event timeout (none)
 
static int ArgsGpioNum
 gpio number
 
static OptsPgmInfo_T PgmInfo
 Program information. More...
 
static OptsInfo_T OptsInfo []
 Command line options information.
 

Detailed Description

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Returns
Returns 0 on succes, non-zero on failure.

Definition at line 295 of file gpioexport.cxx.

References APP_EC_EXEC, APP_EC_OK, ArgsGpioDir, ArgsGpioEdge, ArgsGpioNum, gpioExport(), gpioMakeDirname(), gpioSetDirection(), gpioSetEdge(), mainInit(), NO_ARG, OptsMode, and Permissions.

296 {
297  mainInit(argc, argv);
298 
299  if( gpioExport(ArgsGpioNum) < 0 )
300  {
301  return APP_EC_EXEC;
302  }
303 
304  if( ArgsGpioDir != NO_ARG )
305  {
307  {
308  return APP_EC_EXEC;
309  }
310  }
311 
312  if( ArgsGpioEdge != NO_ARG )
313  {
315  {
316  return APP_EC_EXEC;
317  }
318  }
319 
320  if( OptsMode != NULL )
321  {
322  char gpioPath[MAX_PATH];
323  char path[MAX_PATH];
324 
325  gpioMakeDirname(ArgsGpioNum, gpioPath, sizeof(gpioPath));
326 
327  sprintf(path, "%s/%s", gpioPath, "value");
328 
329  if( chmod(path, Permissions) < 0 )
330  {
331  LOGSYSERROR("chmod(%s, %o)", path, Permissions);
332  return APP_EC_EXEC;
333  }
334  }
335 
336  return APP_EC_OK;
337 }
#define NO_ARG
no argument
Definition: gpioexport.cxx:62
static int ArgsGpioDir
gpio direction
Definition: gpioexport.cxx:65
static int ArgsGpioNum
gpio number
Definition: gpioexport.cxx:64
int gpioSetEdge(int gpio, int edge)
Set GPIO edge trigger type.
Definition: gpio.c:292
static int ArgsGpioEdge
gpio edge trigger type
Definition: gpioexport.cxx:66
#define APP_EC_EXEC
execution exit code
Definition: gpioexport.cxx:57
static mode_t Permissions
gpio permissions
Definition: gpioexport.cxx:67
static void mainInit(int argc, char *argv[])
Main initialization.
Definition: gpioexport.cxx:201
int gpioSetDirection(int gpio, int dir)
Set GPIO signal direction.
Definition: gpio.c:250
void gpioMakeDirname(int gpio, char buf[], size_t size)
Make GPIO directory name.
Definition: gpio.c:587
static char * OptsMode
permissions
Definition: gpioexport.cxx:60
#define APP_EC_OK
success exit code
Definition: gpioexport.cxx:55
int gpioExport(int gpio)
Export (create) a GPIO interface.
Definition: gpio.c:192
static void mainInit ( int  argc,
char *  argv[] 
)
static

Main initialization.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exits:
Program terminates on conversion error.
Returns
Exit code.

Definition at line 201 of file gpioexport.cxx.

References ArgsGpioDir, ArgsGpioEdge, ArgsGpioNum, Argv0, badCmdExit(), GPIO_DIR_IN, GPIO_DIR_IN_STR, GPIO_DIR_OUT, GPIO_DIR_OUT_STR, GPIO_EDGE_BOTH, GPIO_EDGE_BOTH_STR, GPIO_EDGE_FALLING, GPIO_EDGE_FALLING_STR, GPIO_EDGE_NONE, GPIO_EDGE_NONE_STR, GPIO_EDGE_RISING, GPIO_EDGE_RISING_STR, NO_ARG, OptsInfo, OptsMode, Permissions, PgmInfo, PkgInfo, strToInt(), and strToMode().

Referenced by main().

202 {
203  // name of this process
204  Argv0 = basename(argv[0]);
205 
206  // parse input options
207  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
208 
209  if( OptsMode != NULL )
210  {
211  if( strToMode(OptsMode, Permissions) < 0 )
212  {
213  fprintf(stderr, "%s: '%s': Bad permissions mode.\n", Argv0, OptsMode);
214  badCmdExit();
215  }
216  }
217 
218  if( argc == 0 )
219  {
220  fprintf(stderr, "%s: No GPIO pin number <gpio> specified.\n", Argv0);
221  badCmdExit();
222  }
223 
224  else if( strToInt(argv[0], ArgsGpioNum) < 0 )
225  {
226  fprintf(stderr, "%s: '%s': Bad GPIO number.\n", Argv0, argv[0]);
227  badCmdExit();
228  }
229 
230  if( argc >= 2 )
231  {
232  if( !strcasecmp(argv[1], GPIO_DIR_IN_STR) )
233  {
235  }
236  else if( !strcasecmp(argv[1], GPIO_DIR_OUT_STR) )
237  {
239  }
240  else
241  {
242  fprintf(stderr, "%s: '%s': Bad GPIO direction.\n", Argv0, argv[1]);
243  badCmdExit();
244  }
245  }
246  else
247  {
249  }
250 
251  if( argc >= 3 )
252  {
253  if( !strcasecmp(argv[2], GPIO_EDGE_NONE_STR) )
254  {
256  }
257  else if( !strcasecmp(argv[2], GPIO_EDGE_RISING_STR) )
258  {
260  }
261  else if( !strcasecmp(argv[2], GPIO_EDGE_FALLING_STR) )
262  {
264  }
265  else if( !strcasecmp(argv[2], GPIO_EDGE_BOTH_STR) )
266  {
268  }
269  else
270  {
271  fprintf(stderr, "%s: '%s': Bad GPIO edge.\n", Argv0, argv[2]);
272  badCmdExit();
273  }
274  }
275  else
276  {
278  }
279 
280  if( argc >= 4 )
281  {
282  fprintf(stderr, "%s: '%s...': What is this?.\n", Argv0, argv[3]);
283  badCmdExit();
284  }
285 }
static void badCmdExit()
Exit program on bad command-line values.
Definition: gpioexport.cxx:123
#define GPIO_EDGE_FALLING
falling edge
Definition: gpio.h:78
static int strToMode(const string &str, mode_t &mode)
Convert string to file permissions.
Definition: gpioexport.cxx:149
#define NO_ARG
no argument
Definition: gpioexport.cxx:62
static int ArgsGpioDir
gpio direction
Definition: gpioexport.cxx:65
#define GPIO_EDGE_BOTH
both edges
Definition: gpio.h:79
#define GPIO_EDGE_FALLING_STR
falling edge string
Definition: gpio.h:82
#define GPIO_DIR_IN
input
Definition: gpio.h:68
static char * Argv0
the command
Definition: gpioexport.cxx:59
static int ArgsGpioNum
gpio number
Definition: gpioexport.cxx:64
#define GPIO_DIR_OUT_STR
output string
Definition: gpio.h:71
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: gpioexport.cxx:100
static int ArgsGpioEdge
gpio edge trigger type
Definition: gpioexport.cxx:66
static OptsPgmInfo_T PgmInfo
Program information.
Definition: gpioexport.cxx:72
#define GPIO_EDGE_BOTH_STR
both edges string
Definition: gpio.h:83
#define GPIO_EDGE_NONE_STR
no edge string
Definition: gpio.h:80
#define GPIO_DIR_OUT
output
Definition: gpio.h:69
static const PkgInfo_T PkgInfo
Definition: version.h:45
#define GPIO_DIR_IN_STR
input string
Definition: gpio.h:70
#define GPIO_EDGE_NONE
no edge
Definition: gpio.h:76
static mode_t Permissions
gpio permissions
Definition: gpioexport.cxx:67
#define GPIO_EDGE_RISING_STR
rising edge string
Definition: gpio.h:81
#define GPIO_EDGE_RISING
rising edge
Definition: gpio.h:77
static char * OptsMode
permissions
Definition: gpioexport.cxx:60
static int strToInt(const string &str, int &val)
Convert string to integer.
Definition: gpioexport.cxx:132

Variable Documentation

OptsInfo_T OptsInfo[]
static
Initial value:
=
{
{
"mode",
'm',
required_argument,
false,
OptsCvtArgStr,
OptsFmtStr,
"<MODE>",
"Change GPIO exported interface permissions. "
"Format: 0[0-7]+ for user,group,other."
},
{NULL, }
}
static char * OptsMode
permissions
Definition: gpioexport.cxx:60

Command line options information.

Definition at line 100 of file gpioexport.cxx.

Referenced by mainInit().

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
"<gpio>",
"Block wait for GPIO value to change.",
"The %P command block waits for the value of the inoput GPIO associated with "
"the <gpio> exported number to change. "
"When the GPIO value changes or a timeout "
"occurs, the value of \"0\" or \"1\" is printed to stdout. On error, the "
"value \"-1\" is printed to stdout, and the program terminates.\n"
"Note:\n"
" The exported GPIO interface must be configured to trigger on an "
"edge transition.\n"
"Examples:\n"
" gpionotify 19\n"
" gpionotify --timeout=1.5 --monitor 172"
"",
NULL
}

Program information.

Definition at line 68 of file gpionotify.cxx.

Referenced by mainInit().

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
"<gpio> [{in|out} [{none|rising|falling|both}]]",
"Create GPIO exported interface.",
"The %P command creates a GPIO exported interface for the specified GPIO "
"number. Optionally, the direction and edge of the GPIO can also be "
"specified.\n\n"
"Arguments:\n"
" <gpio> Exported GPIO number.\n"
" <direction> GPIO direction. One of: in out.\n"
" Default: System defined.\n"
" <edge> Input GPIO trigger. One of: none rising falling both.\n"
" Default: System defined. N/A for output GPIO."
"\n\n"
"NOTE: This command requires root privileges.",
NULL
}

Program information.

Definition at line 72 of file gpioexport.cxx.

Referenced by mainInit().