gpio  1.4.2
General Purpose I/O Package
gpionotify.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: gpio
4 //
5 // Program: gpioexport
6 //
7 // File: gpioexport.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-04-08 17:22:10 -0600 (Wed, 08 Apr 2015) $
12  * $Rev: 3913 $
13  *
14  * \brief Create GPIO exported interface.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2016-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25  * @EulaEnd@
26  */
27 ////////////////////////////////////////////////////////////////////////////////
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <string>
37 
38 #include "rnr/rnrconfig.h"
39 #include "rnr/log.h"
40 #include "rnr/opts.h"
41 #include "rnr/pkg.h"
42 
43 #include "rnr/gpio.h"
44 
45 #include "version.h"
46 
47 using namespace std;
48 
49 /*!
50  * \ingroup cmds
51  * \defgroup gpioexport gpioexport
52  * \{
53  */
54 
55 #define APP_EC_OK 0 ///< success exit code
56 #define APP_EC_ARGS 2 ///< command-line options/arguments error exit code
57 #define APP_EC_EXEC 4 ///< execution exit code
58 
59 static char *Argv0; ///< the command
60 static bool_t OptsMonitor = false; ///< do [not] keep listening for events
61 static double OptsTimeout = 0.0; ///< event timeout (none)
62 
63 static int ArgsGpioNum; ///< gpio number
64 
65 /*!
66  * \brief Program information.
67  */
68 static OptsPgmInfo_T PgmInfo =
69 {
70  // usage_args
71  "<gpio>",
72 
73  // synopsis
74  "Block wait for GPIO value to change.",
75 
76  // long_desc =
77  "The %P command block waits for the value of the inoput GPIO associated with "
78  "the <gpio> exported number to change. "
79  "When the GPIO value changes or a timeout "
80  "occurs, the value of \"0\" or \"1\" is printed to stdout. On error, the "
81  "value \"-1\" is printed to stdout, and the program terminates.\n"
82  "Note:\n"
83  " The exported GPIO interface must be configured to trigger on an "
84  "edge transition.\n"
85  "Examples:\n"
86  " gpionotify 19\n"
87  " gpionotify --timeout=1.5 --monitor 172"
88  "",
89 
90  // diagnostics
91  NULL
92 };
93 
94 /*!
95  * \brief Command line options information.
96  */
97 static OptsInfo_T OptsInfo[] =
98 {
99  // -m, --monitor
100  {
101  "monitor", // long_opt
102  'm', // short_opt
103  no_argument, // has_arg
104  true, // has_default
105  &OptsMonitor, // opt_addr
106  OptsCvtArgBool, // fn_cvt
107  OptsFmtBool, // fn_fmt
108  NULL, // arg_name
109  // opt desc
110  "Keep listening for events. Default will terminate after one trigger."
111  },
112 
113  // -t, --timeout
114  {
115  "timeout", // long_opt
116  't', // short_opt
117  required_argument, // has_arg
118  true, // has_default
119  &OptsTimeout, // opt_addr
120  OptsCvtArgFloat, // fn_cvt
121  OptsFmtFloat, // fn_fmt
122  "<seconds>", // arg_name
123  // opt desc
124  "Set wait timeout in seconds. A value of 0.0 means no timeout."
125  },
126 
127  {NULL, }
128 };
129 
130 /*!
131  * \brief Exit program on bad command-line values.
132  */
133 static void badCmdExit()
134 {
135  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
136  exit(APP_EC_ARGS);
137 }
138 
139 /*!
140  * \brief Convert string to integer.
141  */
142 static int strToInt(const string &str, int &val)
143 {
144  long long int val1; // must use 64-bit for arm 32-bit compilers
145 
146  if( sscanf(str.c_str(), "%lli", &val1) != 1 )
147  {
148  return RC_ERROR;
149  }
150 
151  val = (int)val1;
152 
153  return OK;
154 }
155 
156 /*!
157  * \brief Main initialization.
158  *
159  * \param argc Command-line argument count.
160  * \param argv Command-line argument list.
161  *
162  * \par Exits:
163  * Program terminates on conversion error.
164  *
165  * \return
166  * Exit code.
167  */
168 static void mainInit(int argc, char *argv[])
169 {
170  // name of this process
171  Argv0 = basename(argv[0]);
172 
173  // parse input options
174  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
175 
176  if( argc == 0 )
177  {
178  fprintf(stderr, "%s: No GPIO pin number <gpio> specified.\n", Argv0);
179  badCmdExit();
180  }
181 
182  else if( strToInt(argv[0], ArgsGpioNum) < 0 )
183  {
184  fprintf(stderr, "%s: '%s': Bad GPIO number.\n", Argv0, argv[0]);
185  badCmdExit();
186  }
187 
188  for(int i = 2; i<argc; ++i)
189  printf(".%s.\n", argv[i]);
190 
191 }
192 
193 int main(int argc, char *argv[])
194 {
195  int fd;
196  int rc;
197 
198  // parse command line
199  mainInit(argc, argv);
200 
201  // open sys/class/gpio exported value
202  if( (fd = gpioOpen(ArgsGpioNum)) < 0 )
203  {
204  return APP_EC_EXEC;
205  }
206 
207  // read once, to purge existing gpio triggers
208  gpioQuickRead(fd);
209 
210  // now monitor events
211  do
212  {
213  rc = gpioNotify(fd, OptsTimeout);
214 
215  fprintf(stdout, "%d\n", rc);
216  fflush(stdout);
217 
218  } while( OptsMonitor && (rc >= 0) );
219 
220  return rc < -1? APP_EC_EXEC: APP_EC_OK;
221 }
int main(int argc, char *argv[])
Main.
static void mainInit(int argc, char *argv[])
Main initialization.
Definition: gpionotify.cxx:168
int gpioOpen(int gpio)
Open GPIO pin.
Definition: gpio.c:492
GPIO interface declarations and defines.
static char * Argv0
the command
Definition: gpionotify.cxx:59
static int ArgsGpioNum
gpio number
Definition: gpionotify.cxx:63
int gpioNotify(int fd, double timeout)
Notify on GPIO input value change.
Definition: gpio.c:442
static int strToInt(const string &str, int &val)
Convert string to integer.
Definition: gpionotify.cxx:142
int gpioQuickRead(int fd)
Quick read GPIO pin&#39;s current value.
Definition: gpio.c:520
static double OptsTimeout
event timeout (none)
Definition: gpionotify.cxx:61
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: gpionotify.cxx:97
static OptsPgmInfo_T PgmInfo
Program information.
Definition: gpionotify.cxx:68
static void badCmdExit()
Exit program on bad command-line values.
Definition: gpionotify.cxx:133
static const PkgInfo_T PkgInfo
Definition: version.h:45
#define APP_EC_EXEC
execution exit code
Definition: gpionotify.cxx:57
Package version information.
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpionotify.cxx:56
#define APP_EC_OK
success exit code
Definition: gpionotify.cxx:55
static bool_t OptsMonitor
do [not] keep listening for events
Definition: gpionotify.cxx:60