gpio  1.4.2
General Purpose I/O Package
gpiodirection.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: gpio
4 //
5 // Program: gpiodirection
6 //
7 // File: gpiodirection.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 gpiodirection gpiodirection
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 ArgsDirection; ///< gpio direction
61 
62 /*!
63  * \brief Program information.
64  */
65 static OptsPgmInfo_T PgmInfo =
66 {
67  // usage_args
68  "<gpio> {in | out}",
69 
70  // synopsis
71  "Set GPIO pin direction.",
72 
73  // long_desc =
74  "The %P command sets the direction for the GPIO associated with the given "
75  "<gpio> exported number."
76  "An input (in) direction allows for the value of the GPIO to be read. "
77  "An output (out) direction allows for writing of values to the GPIO.",
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 GPIO direction specified.\n", Argv0);
155  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
156  exit(APP_EC_ARGS);
157  }
158  else if( !strcmp(argv[1], GPIO_DIR_IN_STR) )
159  {
161  }
162  else if( !strcmp(argv[1], GPIO_DIR_OUT_STR) )
163  {
165  }
166  else
167  {
168  fprintf(stderr, "%s: '%s': Bad direction.\n", Argv0, argv[1]);
169  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
170  exit(APP_EC_ARGS);
171  }
172 }
173 
174 /*!
175  * \brief Set GPIO directiion.
176  *
177  * Method: sysfs
178  *
179  * \return Application exit code.
180  */
181 static int sysfsSetDirection()
182 {
183  int ec;
184 
186  {
187  ec = APP_EC_EXEC;
188  }
189  else
190  {
191  ec = APP_EC_EXEC;
192  }
193 
194  return ec;
195 }
196 
197 #ifdef MMAP_GPIO
198 /*!
199  * \brief Set GPIO directiion.
200  *
201  * Method: mmap
202  *
203  * \return Application exit code.
204  */
205 static int mmapSetDirection()
206 {
207  int ec;
208 
209  if( mmapGpioMap() < 0 )
210  {
211  return APP_EC_EXEC;
212  }
213 
214  if( mmapGpioSetDirection(ArgsGpio, ArgsDirection) < 0 )
215  {
216  ec = APP_EC_EXEC;
217  }
218  else
219  {
220  ec = APP_EC_OK;
221  }
222 
223  mmapGpioUnmap();
224 
225  return ec;
226 }
227 #endif // MMAP_GPIO
228 
229 /*!
230  * \brief Main.
231  *
232  * \param argc Command-line argument count.
233  * \param argv Command-line argument list.
234  *
235  * \return Returns 0 on succes, non-zero on failure.
236  */
237 int main(int argc, char* argv[])
238 {
239  int ec;
240 
241  mainInit(argc, argv);
242 
243  if( !strcmp(OptsMethod, "sysfs") )
244  {
245  ec = sysfsSetDirection();
246  }
247 #ifdef MMAP_GPIO
248  else if( !strcmp(OptsMethod, "mmap") )
249  {
250  ec = mmapSetDirection();
251  }
252 #endif // MMAP_GPIO
253  else
254  {
255  fprintf(stderr,"%s: Unknown GPIO access method.", OptsMethod);
256  ec = APP_EC_ARGS;
257  }
258 
259  return ec;
260 }
261 
262 /*!
263  * \}
264  */
static int ArgsDirection
gpio direction
#define APP_EC_ARGS
command-line options/arguments error exit code
GPIO interface declarations and defines.
#define GPIO_DIR_IN
input
Definition: gpio.h:68
#define APP_EC_EXEC
execution exit code
#define GPIO_DIR_OUT_STR
output string
Definition: gpio.h:71
static char * OptsMethod
system file system
static OptsPgmInfo_T PgmInfo
Program information.
static char * Argv0
the command
static void mainInit(int argc, char *argv[])
Main initialization.
int main(int argc, char *argv[])
Main.
#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
static OptsInfo_T OptsInfo[]
Command line options information.
Package version information.
static int ArgsGpio
gpio number
int gpioSetDirection(int gpio, int dir)
Set GPIO signal direction.
Definition: gpio.c:250
#define APP_EC_OK
success exit code
static int sysfsSetDirection()
Set GPIO directiion.