gpio  1.4.2
General Purpose I/O Package
gpiounexport.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: gpio
4 //
5 // Program: gpiounexport
6 //
7 // File: gpiounexport.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-01-13 13:31:27 -0700 (Tue, 13 Jan 2015) $
12  * $Rev: 3852 $
13  *
14  * \brief Destroy GPIO exported interface.
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 gpiounexport gpiounexport
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 
59 static int ArgsGpio; ///< gpio number
60 
61 /*!
62  * \brief Program information.
63  */
64 static OptsPgmInfo_T PgmInfo =
65 {
66  // usage_args
67  "<gpio>",
68 
69  // synopsis
70  "Destroy GPIO exported interface.",
71 
72  // long_desc =
73  "The %P command destroys a GPIO exported interface for the specified GPIO "
74  "number.\n\n"
75  "NOTE: This command requires root privileges.",
76 
77  // diagnostics
78  NULL
79 };
80 
81 /*!
82  * \brief Command line options information.
83  */
84 static OptsInfo_T OptsInfo[] =
85 {
86  {NULL, }
87 };
88 
89 static int strToInt(const string &str, int &val)
90 {
91  long long int val1; // must use 64-bit for arm 32-bit compilers
92 
93  if( sscanf(str.c_str(), "%lli", &val1) != 1 )
94  {
95  return RC_ERROR;
96  }
97 
98  val = (int)val1;
99 
100  return OK;
101 }
102 
103 /*!
104  * \brief Main initialization.
105  *
106  * \param argc Command-line argument count.
107  * \param argv Command-line argument list.
108  *
109  * \par Exits:
110  * Program terminates on conversion error.
111  */
112 static void mainInit(int argc, char *argv[])
113 {
114  // name of this process
115  Argv0 = basename(argv[0]);
116 
117  // parse input options
118  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
119 
120  if( argc == 0 )
121  {
122  fprintf(stderr, "%s: No GPIO pin number <gpio> specified.\n", Argv0);
123  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
124  exit(APP_EC_ARGS);
125  }
126 
127  else if( strToInt(argv[0], ArgsGpio) < 0 )
128  {
129  fprintf(stderr, "%s: '%s': Bad GPIO number.\n", Argv0, argv[0]);
130  fprintf(stderr, "Try '%s --help' for more information.\n", Argv0);
131  exit(APP_EC_ARGS);
132  }
133 }
134 
135 /*!
136  * \brief Main.
137  *
138  * \param argc Command-line argument count.
139  * \param argv Command-line argument list.
140  *
141  * \return Returns 0 on succes, non-zero on failure.
142  */
143 int main(int argc, char* argv[])
144 {
145  char cmd[MAX_PATH];
146  char dir[MAX_PATH];
147 
148  mainInit(argc, argv);
149 
150  if( gpioUnexport(ArgsGpio) < 0 )
151  {
152  return APP_EC_EXEC;
153  }
154 
155  return APP_EC_OK;
156 }
157 
158 /*!
159  * \}
160  */
static OptsPgmInfo_T PgmInfo
Program information.
int main(int argc, char *argv[])
Main.
GPIO interface declarations and defines.
#define APP_EC_EXEC
execution exit code
static void mainInit(int argc, char *argv[])
Main initialization.
int gpioUnexport(int gpio)
Unexport (delete) a GPIO interface.
Definition: gpio.c:221
static OptsInfo_T OptsInfo[]
Command line options information.
static const PkgInfo_T PkgInfo
Definition: version.h:45
Package version information.
#define APP_EC_OK
success exit code
static char * Argv0
the command
static int ArgsGpio
gpio number
#define APP_EC_ARGS
command-line options/arguments error exit code