tool/cmd_states.cpp
author Florian Pose <fp@igh-essen.com>
Wed, 23 Jul 2008 08:06:10 +0000
changeset 1136 a0982873d655
parent 1130 bb1c11adb2db
child 1137 a73c0f54be42
permissions -rw-r--r--
Improved exceptions and commandName usage.
/*****************************************************************************
 *
 * $Id$
 *
 ****************************************************************************/

#include <iostream>
using namespace std;

#include "globals.h"

/****************************************************************************/

// FIXME
const char *help_states =
    "[OPTIONS]\n"
    "\n"
    "\n"
    "Command-specific options:\n";

/****************************************************************************/

void command_states(void)
{
    stringstream err;
    string stateStr;
    uint8_t state;
    
    if (commandArgs.size() != 1) {
        err << "'" << commandName << "' takes exactly one argument!";
        throw InvalidUsageException(err);
    }

    stateStr = commandArgs[0];
    transform(stateStr.begin(), stateStr.end(),
            stateStr.begin(), (int (*) (int)) std::toupper);

    if (stateStr == "INIT") {
        state = 0x01;
    } else if (stateStr == "PREOP") {
        state = 0x02;
    } else if (stateStr == "SAFEOP") {
        state = 0x04;
    } else if (stateStr == "OP") {
        state = 0x08;
    } else {
        err << "Invalid state '" << commandArgs[0] << "'!";
        throw InvalidUsageException(err);
    }

    masterDev.open(MasterDevice::ReadWrite);

    if (slavePosition == -1) {
        unsigned int i, numSlaves = masterDev.slaveCount();
        for (i = 0; i < numSlaves; i++)
            masterDev.requestState(i, state);
    } else {
        masterDev.requestState(slavePosition, state);
    }
}

/*****************************************************************************/