i2c  1.4.2
RoadNarrows Robotics I2C Package
i2ccheck.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RCB3
4 //
5 // File: i2ccheck.c
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2011-09-12 16:08:28 -0600 (Mon, 12 Sep 2011) $
10  * $Rev: 1279 $
11  *
12  * \brief Check if I2C device with the specified address exists on the I2C Bus.
13  *
14  * \author Robin Knight (robin.knight@roadnarrows.com)
15  *
16  * \copyright
17  * \h_copy 2007-2017. RoadNarrows LLC.\n
18  * http://www.roadnarrows.com\n
19  * All Rights Reserved
20  */
21 // Permission is hereby granted, without written agreement and without
22 // license or royalty fees, to use, copy, modify, and distribute this
23 // software and its documentation for any purpose, provided that
24 // (1) The above copyright notice and the following two paragraphs
25 // appear in all copies of the source code and (2) redistributions
26 // including binaries reproduces these notices in the supporting
27 // documentation. Substantial modifications to this software may be
28 // copyrighted by their authors and need not follow the licensing terms
29 // described here, provided that the new terms are clearly indicated in
30 // all files where they apply.
31 //
32 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
33 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
34 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
35 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
36 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
37 // THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
40 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
41 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
42 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
43 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
44 //
45 ////////////////////////////////////////////////////////////////////////////////
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <ctype.h>
53 #include <libgen.h>
54 #include <unistd.h>
55 
56 #include "rnr/rnrconfig.h"
57 #include "rnr/log.h"
58 #include "rnr/opts.h"
59 
60 #include "rnr/i2c-dev.h"
61 #include "rnr/i2c.h"
62 
63 #include "version.h"
64 
65 // ---------------------------------------------------------------------------
66 // Private Interface
67 // ---------------------------------------------------------------------------
68 
69 // The Command
70 static char *Argv0; ///< command name
71 
72 //
73 // Command Option Values
74 //
75 static int OptVerbose = 0; ///< verbose option
76 static char *OptDevName = "/dev/i2c/0"; ///< i2c bus device option
77 static int OptDevFd = -1; ///< opened i2c bus device fd option
78 static int OptI2CAddr = -1; ///< slave device address option
79 
80 /*!
81  * \brief Program Information
82  */
83 static OptsPgmInfo_T I2CCheckPgmInfo =
84 {
85  .usage_args = "--address <addr>",
86 
87  .synopsis = "Check if an I2C device exists on an I2C Bus.",
88 
89  .long_desc =
90  "The %P command queries the specified I2C Bus for a device "
91  "with the specified address.",
92 
93  .diagnostics =
94  "Exit status is 0 if device with address is found, 1 otherwise. "
95  "Exit status is >= 2 if error(s) are encountered."
96 };
97 
98 /*!
99  * \brief Command Line Options Information
100  */
101 static OptsInfo_T I2CCheckOptsInfo[] =
102 {
103  // I2C device address
104  {
105  .long_opt = "address",
106  .short_opt = 'a',
107  .has_arg = required_argument,
108  .opt_addr = &OptI2CAddr,
109  .fn_cvt = OptsCvtArgInt,
110  .fn_fmt = OptsFmtInt,
111  .arg_name = "<addr>",
112  .opt_desc = "I2C device address. REQUIRED"
113  },
114 
115  // I2C device name
116  {
117  .long_opt = "device",
118  .short_opt = 'd',
119  .has_arg = required_argument,
120  .has_default= true,
121  .opt_addr = &OptDevName,
122  .fn_cvt = OptsCvtArgStr,
123  .fn_fmt = OptsFmtStr,
124  .arg_name = "<device>",
125  .opt_desc = "I2C device."
126  },
127 
128  // opend I2C file descriptor
129  {
130  .long_opt = "fd",
131  .short_opt = OPTS_NO_SHORT,
132  .has_arg = required_argument,
133  .opt_addr = &OptDevFd,
134  .fn_cvt = OptsCvtArgInt,
135  .fn_fmt = OptsFmtInt,
136  .arg_name = "<n>",
137  .opt_desc = "Opened I2C device file descriptor."
138  },
139 
140  // verbose printing
141  {
142  .long_opt = "verbose",
143  .short_opt = 'v',
144  .has_arg = no_argument,
145  .has_default= true,
146  .opt_addr = &OptVerbose,
147  .fn_fmt = OptsFmtBool,
148  .opt_desc = "Set print verbosity."
149  },
150 
151  {NULL, }
152 };
153 
154 
155 // ---------------------------------------------------------------------------
156 // Support
157 // ---------------------------------------------------------------------------
158 
159 /*!
160  * \brief Execute \h_i2c slave device check.
161  *
162  * \param pI2C Pointer to \h_i2c handle.
163  * \param addr Slave device address.
164  *
165  * \return Returns \h_ge 0 on success, -1 on failure.
166  */
167 static int execCheck(i2c_t *pI2C, int addr)
168 {
169  int rc;
170 
171  if( OptVerbose )
172  {
173  printf("%s checking 0x%02x...\n", Argv0, (byte_t)addr);
174  }
175 
176  rc = i2c_exists(pI2C, (i2c_addr_t)addr);
177 
178  if( OptVerbose )
179  {
180  printf("device %s\n", (rc? "found": "not found"));
181  }
182 
183  return rc;
184 }
185 
186 // ---------------------------------------------------------------------------
187 // Execution Control
188 // ---------------------------------------------------------------------------
189 
190 /*!
191  * \brief Command initialization.
192  *
193  * \param argc Command-line argument count.
194  * \param argv Command-line arguments.
195  * \param pI2C Pointer to \h_i2c handle.
196  */
197 static void MainInit(int argc, char *argv[], i2c_t *pI2C)
198 {
199  // Name of this process
200  Argv0 = basename(argv[0]);
201 
202  // Get the environment
203  //EnvGet();
204 
205  // Parse input arguments
206  argv = OptsGet(Argv0, &PkgInfo, &I2CCheckPgmInfo, I2CCheckOptsInfo, true,
207  &argc, argv);
208 
209  // Final option checks
210  if( OptI2CAddr < 0x00 )
211  {
212  fprintf(stderr, "%s: Address option required\n", Argv0);
213  exit(EC_BAD_OPT);
214  }
216  {
217  fprintf(stderr, "%s: Address out of range: 0x%x\n", Argv0, OptI2CAddr);
218  exit(EC_BAD_OPT);
219  }
220 
221  // Opened device specified
222  if( OptDevFd >= 0 )
223  {
224  pI2C->fd = OptDevFd;
225  pI2C->addr = (ushort_t)(-1);
226  }
227 
228  // I2C Bus device specified
229  else
230  {
231  if( OptDevName == NULL || OptDevName[0] == 0 )
232  {
233  OptDevName = "/dev/i2c/0";
234  }
235 
236  if( OptVerbose )
237  {
238  printf("I2C device: %s\n\n", OptDevName);
239  }
240 
241  if( i2c_open(pI2C, OptDevName) < 0 )
242  {
243  LOGSYSERROR("%s: Failed to open.", OptDevName);
244  exit(EC_ERROR);
245  }
246  }
247 }
248 
249 /*!
250  * \brief i2ccheck main()
251  *
252  * \param argc Count of command-line options and arguments.
253  * \param argv Array of command-line options and arguments.
254  *
255  * \return Exit value.
256  */
257 int main(int argc, char *argv[])
258 {
259  i2c_t i2c;
260  int rc;
261 
262  MainInit(argc, argv, &i2c);
263 
264  rc = execCheck(&i2c, OptI2CAddr);
265 
266  return rc > 0? 0: (rc == 0? 1: 2);
267 }
268 
269 /*!
270 
271 \page i2ccheck I2CCHECK(1)
272 
273 \section NAME
274 i2ccheck - Check for a device on the I<sup>2</sup>C Bus.
275 
276 \section SYNOPSIS
277 i2ccheck [OPTIONS] --address <addr>
278 
279 \section DESCRIPTION
280 The i2ccheck command checks if an I<sup>2</sup>C device with the specified
281 address exists on the I<sup>2</sup>C Bus.
282 
283 \section OPTIONS RNR_OPTIONS
284 \verbatim
285  -a, --address=<addr> I2C device 7-bit address. REQUIRED.
286  -d, --device=<device> I2C device.
287  DEFAULT: /dev/i2c/0
288  --fd=<n> Opened I2C device file descriptor.
289  DEFAULT: -1
290  -v, --verbose Set print verbosity.
291  DEFAULT: false
292  RNR_OPTIONS Standard set of options provided by librnr.
293 \endverbatim
294 
295 \section DIAGNOSTICS
296 Exit status is 0 if device is found, 1 otherwise.
297 Exist status is >= 2 if error(s) are encountered.
298 
299 \section SEE_ALSO
300 \ref i2cread,
301 \ref i2cscan,
302 \ref i2ctrans,
303 \ref i2cwrite
304 
305 \section AUTHOR
306 Robin Knight (robin.knight@roadnarrows.com)
307 
308 \section COPYRIGHT
309 (C) 2007. RoadNarrows LLC.
310 (http://www.roadnarrows.com)
311 \n All Rights Reserved
312 */
i2c_addr_t addr
address of the currently selected attached I2C device
Definition: i2c.h:82
I2C python modules.
int fd
opened file descriptor of the I2C bus device
Definition: i2c.h:81
ushort_t i2c_addr_t
I2C Device Address Type.
Definition: i2c.h:72
static int OptDevFd
opened i2c bus device fd option
Definition: i2ccheck.c:77
I2C Bus Handle Type.
Definition: i2c.h:79
static int execCheck(i2c_t *pI2C, int addr)
Execute I2C slave device check.
Definition: i2ccheck.c:167
#define I2C_ADDR_DEV_HIGH
last available device address
Definition: i2c-dev.h:103
static OptsInfo_T I2CCheckOptsInfo[]
Command Line Options Information.
Definition: i2ccheck.c:101
static int OptI2CAddr
slave device address option
Definition: i2ccheck.c:78
I2C character device interface.
static const PkgInfo_T PkgInfo
Definition: version.h:45
int main(int argc, char *argv[])
i2ccheck main()
Definition: i2ccheck.c:257
static char * OptDevName
i2c bus device option
Definition: i2ccheck.c:76
static char * Argv0
command name
Definition: i2ccheck.c:70
Package version information.
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
Definition: i2ccom.c:118
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2ccheck.c:197
#define I2C_ADDR_DEV_LOW
first available device address
Definition: i2c-dev.h:101
static int OptVerbose
verbose option
Definition: i2ccheck.c:75
int i2c_exists(i2c_t *i2c, i2c_addr_t addr)
Test the existance of a device at the given address on the given I2C Bus.
Definition: i2ccom.c:199
Low-level I2C communication level.
static OptsPgmInfo_T I2CCheckPgmInfo
Program Information.
Definition: i2ccheck.c:83