i2c  1.4.2
RoadNarrows Robotics I2C Package
i2ccheck.c File Reference

Check if I2C device with the specified address exists on the I2C Bus. More...

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <libgen.h>
#include <unistd.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/opts.h"
#include "rnr/i2c-dev.h"
#include "rnr/i2c.h"
#include "version.h"

Go to the source code of this file.

Functions

static int execCheck (i2c_t *pI2C, int addr)
 Execute I2C slave device check. More...
 
static void MainInit (int argc, char *argv[], i2c_t *pI2C)
 Command initialization. More...
 
int main (int argc, char *argv[])
 i2ccheck main() More...
 

Variables

static char * Argv0
 command name
 
static int OptVerbose = 0
 verbose option
 
static char * OptDevName = "/dev/i2c/0"
 i2c bus device option
 
static int OptDevFd = -1
 opened i2c bus device fd option
 
static int OptI2CAddr = -1
 slave device address option
 
static OptsPgmInfo_T I2CCheckPgmInfo
 Program Information. More...
 
static OptsInfo_T I2CCheckOptsInfo []
 Command Line Options Information.
 

Detailed Description

Check if I2C device with the specified address exists on the I2C Bus.

LastChangedDate
2011-09-12 16:08:28 -0600 (Mon, 12 Sep 2011)
Rev
1279
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file i2ccheck.c.

Function Documentation

static int execCheck ( i2c_t pI2C,
int  addr 
)
static

Execute I2C slave device check.

Parameters
pI2CPointer to I2C handle.
addrSlave device address.
Returns
Returns ≥ 0 on success, -1 on failure.

Definition at line 167 of file i2ccheck.c.

References Argv0, i2c_exists(), and OptVerbose.

Referenced by main().

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 }
ushort_t i2c_addr_t
I2C Device Address Type.
Definition: i2c.h:72
static char * Argv0
command name
Definition: i2ccheck.c:70
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
int main ( int  argc,
char *  argv[] 
)

i2ccheck main()

Parameters
argcCount of command-line options and arguments.
argvArray of command-line options and arguments.
Returns
Exit value.

Definition at line 257 of file i2ccheck.c.

References execCheck(), MainInit(), and OptI2CAddr.

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 }
I2C python modules.
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
static int OptI2CAddr
slave device address option
Definition: i2ccheck.c:78
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2ccheck.c:197
static void MainInit ( int  argc,
char *  argv[],
i2c_t pI2C 
)
static

Command initialization.

Parameters
argcCommand-line argument count.
argvCommand-line arguments.
pI2CPointer to I2C handle.

Definition at line 197 of file i2ccheck.c.

References i2c_struct::addr, Argv0, i2c_struct::fd, I2C_ADDR_DEV_HIGH, I2C_ADDR_DEV_LOW, i2c_open(), I2CCheckOptsInfo, I2CCheckPgmInfo, OptDevFd, OptDevName, OptI2CAddr, OptVerbose, and PkgInfo.

Referenced by main().

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 }
i2c_addr_t addr
address of the currently selected attached I2C device
Definition: i2c.h:82
int fd
opened file descriptor of the I2C bus device
Definition: i2c.h:81
static int OptDevFd
opened i2c bus device fd option
Definition: i2ccheck.c:77
#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
static const PkgInfo_T PkgInfo
Definition: version.h:45
static char * OptDevName
i2c bus device option
Definition: i2ccheck.c:76
static char * Argv0
command name
Definition: i2ccheck.c:70
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
Definition: i2ccom.c:118
#define I2C_ADDR_DEV_LOW
first available device address
Definition: i2c-dev.h:101
static int OptVerbose
verbose option
Definition: i2ccheck.c:75
static OptsPgmInfo_T I2CCheckPgmInfo
Program Information.
Definition: i2ccheck.c:83

Variable Documentation

OptsPgmInfo_T I2CCheckPgmInfo
static
Initial value:
=
{
.usage_args = "--address <addr>",
.synopsis = "Check if an I2C device exists on an I2C Bus.",
.long_desc =
"The %P command queries the specified I2C Bus for a device "
"with the specified address.",
.diagnostics =
"Exit status is 0 if device with address is found, 1 otherwise. "
"Exit status is >= 2 if error(s) are encountered."
}

Program Information.

Definition at line 83 of file i2ccheck.c.

Referenced by MainInit().