i2c  1.4.2
RoadNarrows Robotics I2C Package
i2csh.c File Reference

Simple I2C Bus Command-Line Shell. More...

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

Go to the source code of this file.

Classes

struct  shcmd_t
 Shell Command Info. More...
 

Macros

#define RC_QUIT   (RC_ERROR-1)
 quit shell return code
 

Typedefs

typedef int( execfunc_t) (i2c_t *, int, const char *[])
 Command execution function type.
 

Functions

static int StrToUnsigned (const char *s, uint_t *pVal)
 Convert string to unsigned integer. More...
 
static int StrToByte (const char *s, byte_t *pByte)
 Convert string to byte. More...
 
static const char ** makeargs (char *sLine, int *pArgc)
 Make array of arguments separated with white-space. More...
 
static char * readline (const char *prompt)
 Read an input line from stdin. More...
 
static int execRead (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute I2C slave device read. More...
 
static int execWrite (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute I2C slave device write. More...
 
static int execTransaction (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute I2C slave device write/read transaction. More...
 
static int scanCallback (i2c_t *pI2C, i2c_addr_t addr, void *context)
 Found scanned device callback. More...
 
static int execScan (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute I2C slave device scan. More...
 
static int execCheck (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute I2C slave device check. More...
 
static int execVerbose (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute enabling/disabling verbose printing. More...
 
static int execHelp (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute shell help. More...
 
static int execQuit (i2c_t *pI2C, int nArgc, const char *sArgv[])
 Execute quit shell. More...
 
static void MainInit (int argc, char *argv[], i2c_t *pI2C)
 Command initialization. More...
 
static void MainLoop (i2c_t *pI2C)
 Shell main loop. More...
 
int main (int argc, char *argv[])
 i2csh 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 OptsPgmInfo_T I2CShPgmInfo
 Program Information. More...
 
static OptsInfo_T I2CShOptsInfo []
 Command Line Options Information. More...
 
static shcmd_t I2CCmds []
 forward declaration of shell commands More...
 

Detailed Description

Simple I2C Bus Command-Line Shell.

LastChangedDate
2009-09-09 09:44:12 -0600 (Wed, 09 Sep 2009)
Rev
130
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 i2csh.c.

Function Documentation

static int execCheck ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute I2C slave device check.

check <addr>

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 627 of file i2csh.c.

References i2c_exists(), OptVerbose, and StrToByte().

Referenced by execQuit().

628 {
629  byte_t addr;
630  int rc;
631 
632  if( nArgc != 2 )
633  {
634  printf("Error: %s requires 1 argument\n", sArgv[0]);
635  return RC_ERROR;
636  }
637 
638  LOGDIAG1CALL(_TPTR(pI2C), _TINT(nArgc), _TSTR(sArgv[0]), _TSTR(sArgv[1]));
639 
640  // address
641  if( StrToByte(sArgv[1], &addr) != OK )
642  {
643  printf("Error: %s: bad I2C address\n", sArgv[1]);
644  return RC_ERROR;
645  }
646  else if( addr > 0x7f )
647  {
648  printf("Error: 0x%02x: I2C address out of range [0,0x7f]\n", addr);
649  return RC_ERROR;
650  }
651 
652  if( OptVerbose )
653  {
654  printf("command: %s\n", sArgv[0]);
655  }
656  rc = i2c_exists(pI2C, addr);
657  if( rc < 0 )
658  {
659  LOGSYSERROR("i2c_exists()");
660  return RC_ERROR;
661  }
662 
663  if( OptVerbose )
664  {
665  printf("response:\n");
666  printf(" 0x%02X", addr);
667  }
668 
669  if( rc )
670  {
671  printf("device found\n");
672  }
673  else
674  {
675  printf("device not found\n");
676  }
677 
678  return OK;
679 }
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
Definition: i2csh.c:191
static int OptVerbose
verbose option
Definition: i2csh.c:97
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
static int execHelp ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute shell help.

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 730 of file i2csh.c.

References shcmd_t::m_sCmd, shcmd_t::m_sHelpArgs, and shcmd_t::m_sHelpBrief.

Referenced by execQuit().

731 {
732  shcmd_t *pCmd;
733 
734  //printf("execHelp(%s, %d, ...)\n", sArgv[0], nArgc);
735 
736  for(pCmd=I2CCmds; pCmd->m_sCmd!=NULL; pCmd++)
737  {
738  if( pCmd->m_sHelpArgs != NULL )
739  {
740  printf("%s %s\n", pCmd->m_sCmd, pCmd->m_sHelpArgs);
741  }
742  printf(" %s\n", pCmd->m_sHelpBrief);
743  }
744  return OK;
745 }
const char * m_sHelpArgs
command help argments
Definition: i2csh.c:91
Shell Command Info.
Definition: i2csh.c:86
const char * m_sCmd
input-line full command name
Definition: i2csh.c:88
static shcmd_t I2CCmds[]
forward declaration of shell commands
Definition: i2csh.c:719
const char * m_sHelpBrief
command help
Definition: i2csh.c:90
static int execQuit ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute quit shell.

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns RC_QUIT.

Definition at line 756 of file i2csh.c.

References execCheck(), execHelp(), execRead(), execScan(), execTransaction(), execVerbose(), execWrite(), i2c_close(), and RC_QUIT.

757 {
758  //printf("execQuit(%s, %d, ...)\n", sArgv[0], nArgc);
759  i2c_close(pI2C);
760 
761  return RC_QUIT;
762 }
void i2c_close(i2c_t *i2c)
Closes an I2C Bus.
Definition: i2ccom.c:137
#define RC_QUIT
quit shell return code
Definition: i2csh.c:72
static int execRead ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute I2C slave device read.

read <addr> <readlength>

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 297 of file i2csh.c.

References i2c_read(), OptVerbose, StrToByte(), and StrToUnsigned().

Referenced by execQuit().

298 {
299  int i;
300  byte_t addr;
301  byte_t readbuf[256];
302  uint_t readlen;
303  int rc;
304 
305  if( nArgc != 3 )
306  {
307  printf("Error: %s requires 2 arguments\n", sArgv[0]);
308  return RC_ERROR;
309  }
310 
311  LOGDIAG1CALL(_TPTR(pI2C), _TINT(nArgc), _TSTR(sArgv[0]), _TSTR(sArgv[1]),
312  _TSTR(sArgv[2]));
313 
314  // address
315  if( StrToByte(sArgv[1], &addr) != OK )
316  {
317  printf("Error: %s: bad I2C address\n", sArgv[1]);
318  return RC_ERROR;
319  }
320  else if( addr > 0x7f )
321  {
322  printf("Error: 0x%02x: I2C address out of range [0,0x7f]\n", addr);
323  return RC_ERROR;
324  }
325 
326  // read length
327  if( StrToUnsigned(sArgv[2], &readlen) != OK )
328  {
329  printf("Error: %s: bad read length\n", sArgv[2]);
330  return RC_ERROR;
331  }
332  else if( readlen > sizeof(readbuf) )
333  {
334  printf("Error: %d: I2C read length out of range [0,%u]\n",
335  readlen, (uint_t)sizeof(readbuf));
336  return RC_ERROR;
337  }
338 
339  // echo out parsed command prior to execution
340  if( OptVerbose )
341  {
342  printf("command: %s\n", sArgv[0]);
343  printf(" address: 0x%02x\n", addr);
344  printf(" read length: %d\n", readlen);
345  }
346 
347  rc = i2c_read(pI2C, addr, readbuf, readlen);
348  if( rc < 0 )
349  {
350  LOGSYSERROR("i2c_read()");
351  return RC_ERROR;
352  }
353 
354  if( OptVerbose )
355  {
356  printf("response:\n");
357  printf(" read: ");
358  }
359 
360  readlen = (uint_t)rc;
361 
362  for(i=0; i<readlen; ++i)
363  {
364  printf("0x%02x ", readbuf[i]);
365  }
366  printf("\n");
367  if( OptVerbose )
368  {
369  printf(" bytes read: %u\n", readlen);
370  }
371 
372  return OK;
373 }
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
Definition: i2csh.c:191
static int OptVerbose
verbose option
Definition: i2csh.c:97
int i2c_read(i2c_t *i2c, i2c_addr_t addr, byte_t *buf, uint_t len)
Read from an I2C device.
Definition: i2ccom.c:147
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
Definition: i2csh.c:156
static int execScan ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute I2C slave device scan.

scan

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 587 of file i2csh.c.

References i2c_scan(), OptVerbose, and scanCallback().

Referenced by execQuit().

588 {
589  int n;
590 
591  if( nArgc > 1 )
592  {
593  printf("Error: %s takes no arguments\n", sArgv[0]);
594  return RC_ERROR;
595  }
596 
597  LOGDIAG1CALL(_TPTR(pI2C), _TINT(nArgc), _TSTR(sArgv[0]));
598 
599  if( OptVerbose )
600  {
601  printf("command: %s\n", sArgv[0]);
602  printf("response:\n");
603  printf(" scanned devices: ");
604  }
605  n = i2c_scan(pI2C, scanCallback, NULL);
606  printf("\n");
607 
608  if( OptVerbose )
609  {
610  printf(" number found: %d\n", n);
611  }
612 
613  return OK;
614 }
int i2c_scan(i2c_t *i2c, int(*callback)(i2c_t *i2c, i2c_addr_t addr, void *context), void *context)
Scans the given I2C Bus to find all connected devices.
Definition: i2ccom.c:219
static int OptVerbose
verbose option
Definition: i2csh.c:97
static int scanCallback(i2c_t *pI2C, i2c_addr_t addr, void *context)
Found scanned device callback.
Definition: i2csh.c:570
static int execTransaction ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute I2C slave device write/read transaction.

transaction <addr> <wbyte0> [<wbyte1> ...] <readlength>

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 469 of file i2csh.c.

References i2c_transfer(), OptVerbose, StrToByte(), and StrToUnsigned().

Referenced by execQuit().

470 {
471  int i, j;
472  byte_t addr;
473  byte_t writebuf[256];
474  uint_t writelen;
475  byte_t readbuf[256];
476  uint_t readlen;
477  int rc;
478 
479  if( nArgc < 4 )
480  {
481  printf("Error: %s requires at least 3 arguments\n", sArgv[0]);
482  return RC_ERROR;
483  }
484 
485  LOGDIAG1CALL(_TPTR(pI2C), _TINT(nArgc), _TSTR(sArgv[0]), _TSTR(sArgv[1]),
486  _TSTR(sArgv[2]), _TSTR(sArgv[3]));
487 
488  // address
489  if( StrToByte(sArgv[1], &addr) != OK )
490  {
491  printf("Error: %s: bad I2C address\n", sArgv[1]);
492  return RC_ERROR;
493  }
494  else if( addr > 0x7f )
495  {
496  printf("Error: 0x%02x: I2C address out of range [0,0x7f]\n", addr);
497  return RC_ERROR;
498  }
499 
500  // write bytes
501  for(i=2, j=0; i<nArgc-1 && j<sizeof(writebuf); ++i, ++j)
502  {
503  if( StrToByte(sArgv[i], writebuf+j) != OK )
504  {
505  printf("Error: argv[%d]='%s': bad byte value\n", i, sArgv[i]);
506  return RC_ERROR;
507  }
508  }
509  writelen = (uint_t)j;
510 
511  // read length
512  if( StrToUnsigned(sArgv[nArgc-1], &readlen) != OK )
513  {
514  printf("Error: %s: bad read length\n", sArgv[nArgc-1]);
515  return RC_ERROR;
516  }
517  else if( readlen > sizeof(readbuf) )
518  {
519  printf("Error: %d: I2C read length out of range [0,%u]\n",
520  readlen, (uint_t)sizeof(readbuf));
521  return RC_ERROR;
522  }
523 
524  // echo out parsed command prior to execution
525  if( OptVerbose )
526  {
527  printf("command: %s\n", sArgv[0]);
528  printf(" address: 0x%02x\n", addr);
529  printf(" write length: %d\n", writelen);
530  printf(" write: ");
531  for(i=0; i<writelen; ++i)
532  {
533  printf("0x%02x ", writebuf[i]);
534  }
535  printf("\n");
536  printf(" read length: %d\n", readlen);
537  }
538 
539  rc = i2c_transfer(pI2C, addr, writebuf, writelen, readbuf, readlen);
540 
541  if( rc < 0 )
542  {
543  LOGSYSERROR("i2c_transfer()");
544  return RC_ERROR;
545  }
546 
547  if( OptVerbose )
548  {
549  printf("response:\n");
550  printf(" read: ");
551  }
552  for(i=0; i<readlen; ++i)
553  {
554  printf("0x%02x ", readbuf[i]);
555  }
556  printf("\n");
557 
558  return OK;
559 }
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
Definition: i2csh.c:191
static int OptVerbose
verbose option
Definition: i2csh.c:97
int i2c_transfer(i2c_t *i2c, i2c_addr_t addr, const byte_t *write_buf, uint_t write_len, byte_t *read_buf, uint_t read_len)
Perform a transfer with an I2C device.
Definition: i2ccom.c:171
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
Definition: i2csh.c:156
static int execVerbose ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute enabling/disabling verbose printing.

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 690 of file i2csh.c.

References OptVerbose.

Referenced by execQuit().

691 {
692  //printf("execExists(%s, %d, ...)\n", sArgv[0], nArgc);
693 
694  if( nArgc != 2 )
695  {
696  printf("Error: %s requires 1 argument\n", sArgv[0]);
697  return RC_ERROR;
698  }
699 
700  if( !strcmp(sArgv[1], "on") )
701  {
702  OptVerbose = 1;
703  printf("on\n");
704  return OK;
705  }
706  else if( !strcmp(sArgv[1], "off") )
707  {
708  OptVerbose = 0;
709  printf("off\n");
710  return OK;
711  }
712  else
713  {
714  printf("Error: %s: unknown option\n", sArgv[1]);
715  return RC_ERROR;
716  }
717 }
static int OptVerbose
verbose option
Definition: i2csh.c:97
static int execWrite ( i2c_t pI2C,
int  nArgc,
const char *  sArgv[] 
)
static

Execute I2C slave device write.

write <addr> <wbyte0> [<wbyte1> ...]

Parameters
pI2CPointer to I2C handle.
nArgcNumber of input arguments.
sArgvArray of input argument strings.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 386 of file i2csh.c.

References i2c_write(), OptVerbose, and StrToByte().

Referenced by execQuit().

387 {
388  int i, j;
389  byte_t addr;
390  byte_t writebuf[256];
391  uint_t writelen;
392  int rc;
393 
394  if( nArgc < 3 )
395  {
396  printf("Error: %s requires at least 2 arguments\n", sArgv[0]);
397  return RC_ERROR;
398  }
399 
400  LOGDIAG1CALL(_TPTR(pI2C), _TINT(nArgc), _TSTR(sArgv[0]), _TSTR(sArgv[1]),
401  _TSTR(sArgv[2]));
402 
403  // address
404  if( StrToByte(sArgv[1], &addr) != OK )
405  {
406  printf("Error: %s: bad I2C address\n", sArgv[1]);
407  return RC_ERROR;
408  }
409  else if( addr > 0x7f )
410  {
411  printf("Error: 0x%02x: I2C address out of range [0,0x7f]\n", addr);
412  return RC_ERROR;
413  }
414 
415  // write bytes
416  for(i=2, j=0; i<nArgc && j<sizeof(writebuf); ++i, ++j)
417  {
418  if( StrToByte(sArgv[i], writebuf+j) != OK )
419  {
420  printf("Error: argv[%d]='%s': bad byte value\n", i, sArgv[i]);
421  return RC_ERROR;
422  }
423  }
424  writelen = (uint_t)j;
425 
426  // echo out parsed command prior to execution
427  if( OptVerbose )
428  {
429  printf("command: %s\n", sArgv[0]);
430  printf(" address: 0x%02x\n", addr);
431  printf(" write length: %d\n", writelen);
432  printf(" write: ");
433  for(i=0; i<writelen; ++i)
434  {
435  printf("0x%02x ", writebuf[i]);
436  }
437  printf("\n");
438  }
439 
440  rc = i2c_write(pI2C, addr, writebuf, writelen);
441 
442  if( rc < 0 )
443  {
444  LOGSYSERROR("i2c_write()");
445  return RC_ERROR;
446  }
447 
448  if( OptVerbose )
449  {
450  printf("response:\n");
451  printf(" bytes written: ");
452  }
453  printf("%d\n", writelen);
454 
455  return OK;
456 }
int i2c_write(i2c_t *i2c, i2c_addr_t addr, const byte_t *buf, uint_t len)
Write to an I2C device.
Definition: i2ccom.c:159
static int StrToByte(const char *s, byte_t *pByte)
Convert string to byte.
Definition: i2csh.c:191
static int OptVerbose
verbose option
Definition: i2csh.c:97
int main ( int  argc,
char *  argv[] 
)

i2csh main()

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

Definition at line 929 of file i2csh.c.

References MainInit(), and MainLoop().

930 {
931  i2c_t i2c;
932 
933  MainInit(argc, argv, &i2c);
934 
935  printf("I2C Raw Shell\n");
936  printf("-------------\n");
937  printf("(partial command matching valid; enter 'help' for help)\n\n");
938 
939  MainLoop(&i2c);
940 
941  return 0;
942 }
I2C python modules.
I2C Bus Handle Type.
Definition: i2c.h:79
static void MainLoop(i2c_t *pI2C)
Shell main loop.
Definition: i2csh.c:866
static void MainInit(int argc, char *argv[], i2c_t *pI2C)
Command initialization.
Definition: i2csh.c:832
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 832 of file i2csh.c.

References Argv0, i2c_open(), I2CShOptsInfo, I2CShPgmInfo, OptDevName, OptVerbose, and PkgInfo.

Referenced by main().

833 {
834  // Name of this process
835  Argv0 = basename(argv[0]);
836 
837  // Get the environment
838  //EnvGet();
839 
840  // Parse input arguments
841  argv = OptsGet(Argv0, &PkgInfo, &I2CShPgmInfo, I2CShOptsInfo, true,
842  &argc, argv);
843 
844  if( OptDevName == NULL || OptDevName[0] == 0 )
845  {
846  OptDevName = "/dev/i2c/0";
847  }
848 
849  if( OptVerbose )
850  {
851  printf("I2C device: %s\n\n", OptDevName);
852  }
853 
854  if( i2c_open(pI2C, OptDevName) < 0 )
855  {
856  LOGSYSERROR("%s: Failed to open.", OptDevName);
857  exit(EC_ERROR);
858  }
859 }
static OptsPgmInfo_T I2CShPgmInfo
Program Information.
Definition: i2csh.c:103
static char * OptDevName
i2c bus device option
Definition: i2csh.c:98
static int OptVerbose
verbose option
Definition: i2csh.c:97
static OptsInfo_T I2CShOptsInfo[]
Command Line Options Information.
Definition: i2csh.c:114
static const PkgInfo_T PkgInfo
Definition: version.h:45
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
Definition: i2ccom.c:118
static char * Argv0
command name
Definition: i2csh.c:95
static void MainLoop ( i2c_t pI2C)
static

Shell main loop.

Parameters
pI2CPointer to I2C handle.

Definition at line 866 of file i2csh.c.

References shcmd_t::m_fnExec, shcmd_t::m_sCmd, makeargs(), RC_QUIT, and readline().

Referenced by main().

867 {
868  int bDoQuit = 0;
869  char *sInput;
870  int nArgc;
871  const char **sArgv;
872  //int i;
873  int bDidCmd;
874  shcmd_t *pCmd;
875  int rc;
876 
877  while( !bDoQuit )
878  {
879  sInput = readline("i2csh> ");
880  if( sInput == NULL )
881  {
882  continue;
883  }
884  //printf("'%s'\n", sInput);
885 
886  sArgv = makeargs(sInput, &nArgc);
887  //for(i=0; i<nArgc; ++i)
888  //{
889  // printf("sArgv[%d]='%s'\n", i, sArgv[i]);
890  //}
891 
892  if( nArgc == 0 )
893  {
894  continue;
895  }
896 
897  for(pCmd=I2CCmds, bDidCmd=0; pCmd->m_sCmd!=NULL && !bDidCmd; pCmd++)
898  {
899  if( !strncmp(sArgv[0], pCmd->m_sCmd, strlen(sArgv[0])) )
900  {
901  sArgv[0] = pCmd->m_sCmd;
902  rc = pCmd->m_fnExec(pI2C, nArgc, sArgv);
903  bDidCmd = 1;
904  if( rc == RC_QUIT )
905  {
906  bDoQuit = 1;
907  }
908  }
909  }
910 
911  if( !bDidCmd )
912  {
913  printf("Error: Unknown command: %s\n", sArgv[0]);
914  }
915 #ifdef HAVE_READLINE
916  free(sInput);
917 #endif // HAVE_READLINE
918  }
919 }
static const char ** makeargs(char *sLine, int *pArgc)
Make array of arguments separated with white-space.
Definition: i2csh.c:218
static char * readline(const char *prompt)
Read an input line from stdin.
Definition: i2csh.c:243
Shell Command Info.
Definition: i2csh.c:86
const char * m_sCmd
input-line full command name
Definition: i2csh.c:88
execfunc_t * m_fnExec
assoc. exec. function
Definition: i2csh.c:89
static shcmd_t I2CCmds[]
forward declaration of shell commands
Definition: i2csh.c:719
#define RC_QUIT
quit shell return code
Definition: i2csh.c:72
static const char** makeargs ( char *  sLine,
int *  pArgc 
)
static

Make array of arguments separated with white-space.

Parameters
sLineInput line.
[out]pArgcNumber of parsed arguments
Returns
Returns pointer to array of strings.

Definition at line 218 of file i2csh.c.

Referenced by MainLoop().

219 {
220  static const char *args[256];
221  char *s;
222  char *sDelim = " \t\n\r";
223  int i = 0;
224 
225  for(s=strtok(sLine, sDelim); s!=NULL && i<256; s=strtok(NULL, sDelim))
226  {
227  args[i++] = s;
228  }
229  *pArgc = i;
230  return args;
231 }
static char* readline ( const char *  prompt)
static

Read an input line from stdin.

This function is used only if there is no readline functionality.

Parameters
promptUser prompt string.
Returns
Returns read line buffer.

Definition at line 243 of file i2csh.c.

Referenced by MainLoop().

244 {
245  static char linebuf[512]; // real readline() uses malloc's so adjust
246  int bIsBlank;
247  char *s;
248 
249  if( prompt && *prompt )
250  {
251  printf("%s", prompt);
252  }
253 
254  if( fgets(linebuf, (int)sizeof(linebuf), stdin) == NULL )
255  {
256  return 0;
257  }
258 
259  linebuf[sizeof(linebuf)-1] = 0;
260 
261  bIsBlank = 1;
262  for(s=linebuf; *s && bIsBlank; ++s)
263  {
264  if( !isspace(*s) )
265  {
266  bIsBlank = 0;
267  }
268  }
269 
270  if( bIsBlank )
271  {
272  return NULL;
273  }
274  else
275  {
276  return linebuf;
277  }
278 }
static int scanCallback ( i2c_t pI2C,
i2c_addr_t  addr,
void *  context 
)
static

Found scanned device callback.

Parameters
pI2CPointer to I2C handle.
addrSlave device address.
contextUser provided context.
Returns
Returns 1.

Definition at line 570 of file i2csh.c.

Referenced by execScan().

571 {
572  printf("0x%02x ", addr);
573  return 1;
574 }
static int StrToByte ( const char *  s,
byte_t *  pByte 
)
static

Convert string to byte.

Parameters
sString.
[out]pBytePointer to converted value.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 191 of file i2csh.c.

References StrToUnsigned().

Referenced by execCheck(), execRead(), execTransaction(), and execWrite().

192 {
193  uint_t uVal;
194 
195  if( StrToUnsigned(s, &uVal) != OK )
196  {
197  return RC_ERROR;
198  }
199  else if( uVal > 0xff )
200  {
201  return RC_ERROR;
202  }
203  else
204  {
205  *pByte = (byte_t)uVal;
206  return OK;
207  }
208 }
static int StrToUnsigned(const char *s, uint_t *pVal)
Convert string to unsigned integer.
Definition: i2csh.c:156
static int StrToUnsigned ( const char *  s,
uint_t *  pVal 
)
static

Convert string to unsigned integer.

Parameters
sString.
[out]pValPointer to converted value.
Returns
Returns OK on success, RC_ERROR on error.

Definition at line 156 of file i2csh.c.

Referenced by execRead(), execTransaction(), and StrToByte().

157 {
158  long lVal;
159  char *sEnd;
160 
161  if( (s == NULL) || (*s == 0) )
162  {
163  return RC_ERROR;
164  }
165 
166  lVal = strtol(s, &sEnd, 0);
167 
168  if( *sEnd != 0 )
169  {
170  return RC_ERROR;
171  }
172  else if( lVal < 0 )
173  {
174  return RC_ERROR;
175  }
176  else
177  {
178  *pVal = (uint_t)lVal;
179  return OK;
180  }
181 }

Variable Documentation

static shcmd_t I2CCmds
static

forward declaration of shell commands

Shell commands.

Definition at line 719 of file i2csh.c.

OptsInfo_T I2CShOptsInfo[]
static
Initial value:
=
{
{
.long_opt = "device",
.short_opt = 'd',
.has_arg = required_argument,
.has_default= true,
.opt_addr = &OptDevName,
.fn_cvt = OptsCvtArgStr,
.fn_fmt = OptsFmtStr,
.arg_name = "<device>",
.opt_desc = "I2C device."
},
{
.long_opt = "verbose",
.short_opt = 'v',
.has_arg = no_argument,
.has_default= true,
.opt_addr = &OptVerbose,
.fn_fmt = OptsFmtBool,
.opt_desc = "Set print verbosity."
},
{NULL, }
}
static char * OptDevName
i2c bus device option
Definition: i2csh.c:98
static int OptVerbose
verbose option
Definition: i2csh.c:97

Command Line Options Information.

Definition at line 114 of file i2csh.c.

Referenced by MainInit().

OptsPgmInfo_T I2CShPgmInfo
static
Initial value:
=
{
.synopsis = "I2C Simple Raw Shell",
.long_desc =
"The I2C Shell (%P) provides a simple interactive interface to devices "
"attached to an I2C Bus."
}

Program Information.

Definition at line 103 of file i2csh.c.

Referenced by MainInit().