Exemple #1
0
//****************************************************************************
//
//! Parses the user input command and invokes the I2C APIs
//!
//! \param pcCmdBuffer pointer to the user command
//! 
//! This function  
//!    1. Parses the user command.
//!    2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ParseNProcessCmd(char *pcCmdBuffer)
{
    char *pcInpString;
    int iRetVal = FAILURE;

    pcInpString = strtok(pcCmdBuffer, " \n\r");
    if(pcInpString != NULL)

    {
           
        if(!strcmp(pcInpString, "read"))
        {
            //
            // Invoke the read command handler
            //
            iRetVal = ProcessReadCommand(pcInpString);
        }
        else if(!strcmp(pcInpString, "readreg"))
        {
            //
            // Invoke the readreg command handler
            //
            iRetVal = ProcessReadRegCommand(pcInpString);
        }
        else if(!strcmp(pcInpString, "writereg"))
        {
            //
            // Invoke the writereg command handler
            //
            iRetVal = ProcessWriteRegCommand(pcInpString);
        }
        else if(!strcmp(pcInpString, "write"))
        {
            //
            // Invoke the write command handler
            //
            iRetVal = ProcessWriteCommand(pcInpString);
        }
        else
        {
            UART_PRINT("Unsupported command\n\r");
            return FAILURE;
        }
    }

    return iRetVal;
}
Exemple #2
0
int main( int argc, char **argv )
{
    char                shortOptsStr[ sizeof( gOption ) / sizeof( gOption[ 0 ] ) + 1 ];
    char               *shortOpts = shortOptsStr;
    struct option       *scanOpt;
    int                 opt;
    const char         *i2cDevName = "/dev/i2c-0";
    int                 i2cDev;
    int                 cmdIdx;

    LogInit( stdout );

    // Figure out the short options from our options structure

    for ( scanOpt = gOption; scanOpt->name != NULL; scanOpt++ ) 
    {
        if (( scanOpt->flag == NULL ) && ( scanOpt->val < OPT_FIRST_LONG_OPT ))
        {
            *shortOpts++ = (char)scanOpt->val;

            if ( scanOpt->has_arg != no_argument )
            {
                *shortOpts++ = ':';
            }
        }
    }
    *shortOpts++ = '\0';

    // Parse the command line options

    while (( opt = getopt_long( argc, argv, shortOptsStr, gOption, NULL )) != -1 )
    {
        switch ( opt )
        {
            case 0: 
            {
                // getopt_long returns 0 for entries where flag is non-NULL

                break;
            }

            case OPT_BASE_DECIMAL:
            case OPT_BASE_HEX:
            {
                gBase = opt;
                break;
            }

            case OPT_VERSION:
            {   
                printf( "i2c-io: SVN Revision: %d\n", SVN_REVISION );
                exit( 0 );
            }

            case '?':
            case OPT_HELP:
            default:
            {
                LogError( "opt:%d\n", opt );
                Usage();
                exit( 1 );
            }
        }
    }
    argc -= optind;
    argv += optind;

    // Verify that an i2c-address was specified

    if ( argc < 1 )
    {
        LogError( "Must specify an i2c address\n\n" );
        Usage();
        exit( 1 );
    }
    gI2cAddr = strtol( argv[ 0 ], NULL, 0 );
    if (( gI2cAddr <= 0 ) || ( gI2cAddr > 127 ))
    {
        LogError( "Expecting i2c address in the range of 1-127, Found: %d\n", gI2cAddr );
        Usage();
        exit( 1 );
    }

    // Verify that a command has been specified

    if ( argc < 2 )
    {
        LogError( "Must specify a command\n" );
        Usage();
        exit( 1 );
    }
    gCmdStr = argv[ 1 ];
    for ( cmdIdx = 0; cmdIdx < gNumCmds; cmdIdx++ ) 
    {
        if ( strcasecmp( gCmdStr, gCmdMap[ cmdIdx ].cmdStr ) == 0 )
        {
            gCmd = gCmdMap[ cmdIdx ].cmd;
            break;
        }
    }
    if ( gCmd == CMD_DEFAULT )
    {
        LogError( "Unrecognized command '%s'\n", gCmdStr );
        exit( 1 );
    }

    // Process command specific arguments

    if ( gCmd == CMD_INFO )
    {
        if ( argc != 2 )
        {
            LogError( "Unexpected extra parameters\n" );
            Usage();
            exit( 1 );
        }
    }
    else
    if (( gCmd == CMD_GET )
    ||  ( gCmd == CMD_GET_DIR ))
    {
        if ( argc < 3 )
        {
            LogError( "Expecting port.pin\n" );
            Usage();
            exit( 1 );
        }

        gPortPinStr = argv[ 2 ];
    }
    else
    if (( gCmd == CMD_SET )
    ||  ( gCmd == CMD_SET_DIR ))
    {
        if ( argc < 4 )
        {
            LogError( "port.pin followed by value\n" );
            Usage();
            exit( 1 );
        }
        gPortPinStr = argv[ 2 ];
        gValStr     = argv[ 3 ];
    }
    else
    if ( gCmd == CMD_READ_REG  )
    {
        if ( argc < 3 )
        {
            LogError( "Expecting register index\n" );
            Usage();
            exit( 1 );
        }
        gRegStr = argv[ 2 ];
    }
    else
    if ( gCmd == CMD_WRITE_REG  )
    {
        if ( argc < 4 )
        {
            LogError( "Expecting register index and value\n" );
            Usage();
            exit( 1 );
        }
        gRegStr = argv[ 2 ];
        gValStr = argv[ 3 ];
    }

    if ( gDebug )
    {
        Log( "i2cAddr:0x%02x Cmd: %s (%d)", gI2cAddr, gCmdStr, gCmd );
        if ( gPortPinStr != NULL )
        {
            Log( " Pin: %s", gPortPinStr );
        }
        if ( gValStr != NULL )
        {
            Log( " Val: %s", gValStr );
        }
        Log( "\n" );
    }

    // Try to open the i2c device

    if (( i2cDev = open( i2cDevName, O_RDWR )) < 0 )
    {
        LogError( "Error  opening '%s': %s\n", i2cDevName, strerror( errno ));
        exit( 1 );
    }

    // Indicate which slave we wish to speak to

    I2cSetSlaveAddress( i2cDev, gI2cAddr, I2C_USE_CRC );

    switch ( gCmd )
    {
        case CMD_INFO:
        {
            ProcessInfoCommand( i2cDev );
            break;
        }

        case CMD_GET:
        {
            ProcessGetCommand( i2cDev, gPortPinStr );
            break;
        }

        case CMD_SET:
        {
            ProcessSetCommand( i2cDev, gPortPinStr, gValStr );
            break;
        }

        case CMD_GET_DIR:
        {
            ProcessGetDirCommand( i2cDev, gPortPinStr );
            break;
        }

        case CMD_SET_DIR:
        {
            ProcessSetDirCommand( i2cDev, gPortPinStr, gValStr );
            break;
        }

        case CMD_READ_REG:
        {
            ProcessReadRegCommand( i2cDev, gRegStr );
            break;
        }

        case CMD_WRITE_REG:
        {
            ProcessWriteRegCommand( i2cDev, gRegStr, gValStr );
            break;
        }
    }

    close( i2cDev );

    return 0;

} // main