gpio  1.4.2
General Purpose I/O Package
gpiowrite.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: gpio
4 //
5 // Program: gpiowrite
6 //
7 // File: gpiowrite.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-04-08 17:22:10 -0600 (Wed, 08 Apr 2015) $
12  * $Rev: 3913 $
13  *
14  * \brief Set GPIO pin direction.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2015-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 <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 
34 #include <string>
35 
36 #include "rnr/rnrconfig.h"
37 #include "rnr/log.h"
38 #include "rnr/opts.h"
39 #include "rnr/pkg.h"
40 
41 #include "rnr/gpio.h"
42 
43 #include "version.h"
44 
45 using namespace std;
46 
47 /*!
48  * \ingroup cmds
49  * \defgroup gpiowrite gpiowrite
50  * \{
51  */
52 
53 #define APP_EC_OK 0 ///< success exit code
54 #define APP_EC_ARGS 2 ///< command-line options/arguments error exit code
55 #define APP_EC_EXEC 4 ///< execution exit code
56 
57 static char *Argv0; ///< the command
58 static char *OptsMethod = (char *)"sysfs"; ///< system file system
59 static int ArgsGpio; ///< gpio number
60 static int ArgsValue; ///< value to write
61 
62 /*!
63  * \brief Program information.
64  */
65 static OptsPgmInfo_T PgmInfo =
66 {
67  // usage_args
68  "<gpio> {0 | 1}",
69 
70  // synopsis
71  "Write a value to a GPIO pin.",
72 
73  // long_desc =
74  "The %P command writes the specified value to the GPIO at the given "
75  "<gpio> exported number."
76  "A value of 0 sets the pin low. "
77  "A value of 1 set the pin high.",
78 
79  // diagnostics
80  NULL
81 };
82 
83 /*!
84  * \brief Command line options information.
85  */
86 static OptsInfo_T OptsInfo[] =
87 {
88 #ifdef MMAP_GPIO
89  // -m, --method
90  {
91  "method", // long_opt
92  'm', // short_opt
93  required_argument, // has_arg
94  true, // has_default
95  &OptsMethod, // opt_addr
96  OptsCvtArgStr, // fn_cvt
97  OptsFmtStr, // fn_fmt
98  "<method>", // arg_name
99  // opt desc
100  "GPIO access method. One of: sysfs mmap."
101  },
102 #endif // MMAP_GPIO
103 
104  {NULL, }
105 };
106 
107 static int strToInt(const string &str, int &val)
108 {
109  long long int val1; // must use 64-bit for arm 32-bit compilers
110 
111  if( sscanf(str.c_str(), "%lli", &val1) != 1 )
112  {
113  return RC_ERROR;
114  }
115 
116  val = (int)val1;
117 
118  return OK;
119 }
120 
121 /*!
122  * \brief Main initialization.
123  *
124  * \param argc Command-line argument count.
125  * \param argv Command-line argument list.
126  *
127  * \par Exits:
128  * Program terminates on conversion error.
129  */
130 static void mainInit(int argc, char *argv[])
131 {
132  // name of this process
133  Argv0 = basename(argv[0]);
134 
135  // parse input options
136  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
137 
138  if( argc == 0 )
139  {
140  fprintf(stderr, "%s: No GPIO pin number <gpio> specified.\n", Argv0);
141  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
142  exit(APP_EC_ARGS);
143  }
144 
145  else if( strToInt(argv[0], ArgsGpio) < 0 )
146  {
147  fprintf(stderr, "%s: '%s': Bad GPIO number.\n", Argv0, argv[0]);
148  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
149  exit(APP_EC_ARGS);
150  }
151 
152  if( argc == 1 )
153  {
154  fprintf(stderr, "%s: No value specified.\n", Argv0);
155  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
156  exit(APP_EC_ARGS);
157  }
158 
159  else if( (strToInt(argv[1], ArgsValue) < 0) ||
160  ((ArgsValue != 0) && (ArgsValue != 1)) )
161  {
162  fprintf(stderr, "%s: '%s': Bad value.\n", Argv0, argv[1]);
163  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
164  exit(APP_EC_ARGS);
165  }
166 }
167 
168 /*!
169  * \brief Write value to GPIO.
170  *
171  * Method: sysfs
172  *
173  * \return Application exit code.
174  */
175 static int sysfsWrite()
176 {
177  int ec;
178 
179  if( gpioWrite(ArgsGpio, ArgsValue) < 0 )
180  {
181  ec = APP_EC_EXEC;
182  }
183  else
184  {
185  ec = APP_EC_OK;
186  }
187 
188  return ec;
189 }
190 
191 #ifdef MMAP_GPIO
192 /*!
193  * \brief Write value to GPIO.
194  *
195  * Method: mmap
196  *
197  * \return Application exit code.
198  */
199 static int mmapWrite()
200 {
201  int ec;
202 
203  if( mmapGpioMap() < 0 )
204  {
205  ec = APP_EC_EXEC;
206  }
207 
208  if( mmapGpioWrite(ArgsGpio, ArgsValue) < 0 )
209  {
210  ec = APP_EC_EXEC;
211  }
212  else
213  {
214  ec = APP_EC_OK;
215  }
216 
217  mmapGpioUnmap();
218 
219  return ec;
220 }
221 #endif // MMAP_GPIO
222 
223 /*!
224  * \brief Main.
225  *
226  * \param argc Command-line argument count.
227  * \param argv Command-line argument list.
228  *
229  * \return Returns 0 on succes, non-zero on failure.
230  */
231 int main(int argc, char* argv[])
232 {
233  int pin;
234  int ec = APP_EC_OK;
235 
236  mainInit(argc, argv);
237 
238  if( !strcmp(OptsMethod, "sysfs") )
239  {
240  ec = sysfsWrite();
241  }
242 
243 #ifdef MMAP_GPIO
244  else if( !strcmp(OptsMethod, "mmap") )
245  {
246  ec = mmapWrite();
247  }
248 #endif // MMAP_GPIO
249 
250  else
251  {
252  fprintf(stderr,"%s: Unknown GPIO access method.", OptsMethod);
253  ec = APP_EC_ARGS;
254  }
255 
256  return ec;
257 }
258 
259 /*!
260  * \}
261  */
#define APP_EC_OK
success exit code
Definition: gpiowrite.cxx:53
static int ArgsGpio
gpio number
Definition: gpiowrite.cxx:59
static char * OptsMethod
system file system
Definition: gpiowrite.cxx:58
static OptsPgmInfo_T PgmInfo
Program information.
Definition: gpiowrite.cxx:65
GPIO interface declarations and defines.
static char * Argv0
the command
Definition: gpiowrite.cxx:57
int gpioWrite(int gpio, int value)
Write GPIO value.
Definition: gpio.c:414
static int sysfsWrite()
Write value to GPIO.
Definition: gpiowrite.cxx:175
static int ArgsValue
value to write
Definition: gpiowrite.cxx:60
static const PkgInfo_T PkgInfo
Definition: version.h:45
#define APP_EC_ARGS
command-line options/arguments error exit code
Definition: gpiowrite.cxx:54
int main(int argc, char *argv[])
Main.
Definition: gpiowrite.cxx:231
Package version information.
static OptsInfo_T OptsInfo[]
Command line options information.
Definition: gpiowrite.cxx:86
static void mainInit(int argc, char *argv[])
Main initialization.
Definition: gpiowrite.cxx:130
#define APP_EC_EXEC
execution exit code
Definition: gpiowrite.cxx:55