Open master device as late as possible.

This commit is contained in:
Florian Pose 2010-03-11 15:03:52 +01:00
parent fb8a75570d
commit ddaee8a1fc
27 changed files with 154 additions and 107 deletions

View File

@ -29,6 +29,21 @@
#include "Command.h"
#include "MasterDevice.h"
#include "NumberListParser.h"
/*****************************************************************************/
class MasterIndexParser:
public NumberListParser
{
unsigned int getMax()
{
MasterDevice dev;
dev.setIndex(0U);
dev.open(MasterDevice::Read);
return dev.getMasterCount() - 1;
};
};
/*****************************************************************************/
@ -47,9 +62,9 @@ Command::~Command()
/*****************************************************************************/
void Command::setMasterIndices(const MasterIndexList &indices)
void Command::setMasters(const string &m)
{
masterIndices = indices;
masters = m;
};
/*****************************************************************************/
@ -172,6 +187,28 @@ void Command::throwSingleSlaveRequired(unsigned int size) const
/*****************************************************************************/
Command::MasterIndexList Command::getMasterIndices() const
{
MasterIndexList indices;
try {
MasterIndexParser p;
indices = p.parse(masters.c_str());
} catch (MasterDeviceException &e) {
stringstream err;
err << "Failed to obtain number of masters: " << e.what();
throwCommandException(err);
} catch (runtime_error &e) {
stringstream err;
err << "Invalid master argument '" << masters << "': " << e.what();
throwInvalidUsageException(err);
}
return indices;
}
/*****************************************************************************/
Command::SlaveList Command::selectedSlaves(MasterDevice &m)
{
ec_ioctl_master_t master;

View File

@ -85,8 +85,9 @@ class Command
const string &getBriefDescription() const;
typedef list<unsigned int> MasterIndexList;
void setMasterIndices(const MasterIndexList &);
const MasterIndexList &getMasterIndices() const;
void setMasters(const string &);
MasterIndexList getMasterIndices() const;
enum Verbosity {
Quiet,
Normal,
@ -137,7 +138,7 @@ class Command
private:
string name;
string briefDesc;
MasterIndexList masterIndices;
string masters;
Verbosity verbosity;
int alias;
int position;
@ -165,13 +166,6 @@ inline const string &Command::getBriefDescription() const
/****************************************************************************/
inline const Command::MasterIndexList &Command::getMasterIndices() const
{
return masterIndices;
}
/****************************************************************************/
inline Command::Verbosity Command::getVerbosity() const
{
return verbosity;

View File

@ -81,6 +81,7 @@ void CommandAlias::execute(const StringVector &args)
uint16_t alias;
stringstream err, strAlias;
int number;
MasterIndexList masterIndices;
SlaveList slaves;
SlaveList::const_iterator si;
@ -99,11 +100,12 @@ void CommandAlias::execute(const StringVector &args)
}
alias = number;
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::ReadWrite);
slaves = selectedSlaves(m);

View File

@ -70,6 +70,7 @@ string CommandCStruct::helpString() const
void CommandCStruct::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
SlaveList::const_iterator si;
@ -79,9 +80,10 @@ void CommandCStruct::execute(const StringVector &args)
throwInvalidUsageException(err);
}
masterIndices = getMasterIndices();
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -104,8 +104,9 @@ string CommandConfig::helpString() const
*/
void CommandConfig::execute(const StringVector &args)
{
ConfigList configs;
MasterIndexList masterIndices;
bool doIndent;
ConfigList configs;
if (args.size()) {
stringstream err;
@ -113,10 +114,11 @@ void CommandConfig::execute(const StringVector &args)
throwInvalidUsageException(err);
}
doIndent = getMasterIndices().size() > 1;
masterIndices = getMasterIndices();
doIndent = masterIndices.size() > 1;
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
configs = selectedConfigs(m);

View File

@ -66,6 +66,7 @@ string CommandData::helpString() const
void CommandData::execute(const StringVector &args)
{
MasterIndexList masterIndices;
DomainList domains;
DomainList::const_iterator di;
@ -75,9 +76,10 @@ void CommandData::execute(const StringVector &args)
throwInvalidUsageException(err);
}
masterIndices = getMasterIndices();
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
domains = selectedDomains(m);

View File

@ -68,6 +68,7 @@ string CommandDebug::helpString() const
void CommandDebug::execute(const StringVector &args)
{
MasterIndexList masterIndices;
stringstream str;
int debugLevel;
@ -87,9 +88,10 @@ void CommandDebug::execute(const StringVector &args)
throwInvalidUsageException(err);
}
masterIndices = getMasterIndices();
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::ReadWrite);
m.setDebug(debugLevel);

View File

@ -92,9 +92,10 @@ string CommandDomains::helpString() const
void CommandDomains::execute(const StringVector &args)
{
MasterIndexList masterIndices;
bool doIndent;
DomainList domains;
DomainList::const_iterator di;
bool doIndent;
if (args.size()) {
stringstream err;
@ -102,10 +103,11 @@ void CommandDomains::execute(const StringVector &args)
throwInvalidUsageException(err);
}
doIndent = getMasterIndices().size() > 1;
masterIndices = getMasterIndices();
doIndent = masterIndices.size() > 1;
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
domains = selectedDomains(m);

View File

@ -85,6 +85,7 @@ string CommandDownload::helpString() const
void CommandDownload::execute(const StringVector &args)
{
stringstream strIndex, strSubIndex, err;
MasterIndexList masterIndices;
ec_ioctl_slave_sdo_download_t data;
unsigned int number;
const DataType *dataType = NULL;
@ -114,11 +115,12 @@ void CommandDownload::execute(const StringVector &args)
}
data.sdo_entry_subindex = number;
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::ReadWrite);
slaves = selectedSlaves(m);
if (slaves.size() != 1) {

View File

@ -63,6 +63,7 @@ string CommandEoe::helpString() const
void CommandEoe::execute(const StringVector &args)
{
MasterIndexList masterIndices;
ec_ioctl_master_t master;
unsigned int i;
ec_ioctl_eoe_handler_t eoe;
@ -75,11 +76,12 @@ void CommandEoe::execute(const StringVector &args)
throwInvalidUsageException(err);
}
doIndent = getMasterIndices().size();
masterIndices = getMasterIndices();
doIndent = masterIndices.size();
indent = doIndent ? " " : "";
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
m.getMaster(&master);

View File

@ -76,6 +76,7 @@ string CommandFoeRead::helpString() const
void CommandFoeRead::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
ec_ioctl_slave_t *slave;
ec_ioctl_slave_foe_t data;
@ -87,11 +88,12 @@ void CommandFoeRead::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -82,6 +82,7 @@ string CommandFoeWrite::helpString() const
void CommandFoeWrite::execute(const StringVector &args)
{
MasterIndexList masterIndices;
stringstream err;
ec_ioctl_slave_foe_t data;
ifstream file;
@ -93,11 +94,12 @@ void CommandFoeWrite::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
if (args[0] == "-") {
loadFoeData(&data, cin);

View File

@ -67,6 +67,7 @@ string CommandGraph::helpString() const
void CommandGraph::execute(const StringVector &args)
{
MasterIndexList masterIndices;
ec_ioctl_master_t master;
unsigned int i;
typedef vector<ec_ioctl_slave_t> SlaveVector;
@ -90,12 +91,13 @@ void CommandGraph::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
stringstream err;
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::Read);
m.getMaster(&master);

View File

@ -67,6 +67,7 @@ string CommandMaster::helpString() const
void CommandMaster::execute(const StringVector &args)
{
MasterIndexList masterIndices;
ec_ioctl_master_t data;
stringstream err;
unsigned int i, j;
@ -79,9 +80,10 @@ void CommandMaster::execute(const StringVector &args)
throwInvalidUsageException(err);
}
masterIndices = getMasterIndices();
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
m.getMaster(&data);

View File

@ -91,6 +91,7 @@ string CommandPdos::helpString() const
void CommandPdos::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
SlaveList::const_iterator si;
bool showHeader, multiMaster;
@ -101,10 +102,11 @@ void CommandPdos::execute(const StringVector &args)
throwInvalidUsageException(err);
}
multiMaster = getMasterIndices().size() > 1;
masterIndices = getMasterIndices();
multiMaster = masterIndices.size() > 1;
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -79,6 +79,7 @@ string CommandRegRead::helpString() const
void CommandRegRead::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
ec_ioctl_slave_reg_t data;
stringstream strOffset, err;
@ -140,11 +141,12 @@ void CommandRegRead::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -81,6 +81,7 @@ string CommandRegWrite::helpString() const
void CommandRegWrite::execute(const StringVector &args)
{
MasterIndexList masterIndices;
stringstream strOffset, err;
ec_ioctl_slave_reg_t data;
ifstream file;
@ -100,11 +101,12 @@ void CommandRegWrite::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
if (getDataType().empty()) {
if (args[1] == "-") {

View File

@ -89,6 +89,7 @@ string CommandSdos::helpString() const
void CommandSdos::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
SlaveList::const_iterator si;
bool showHeader, multiMaster;
@ -99,10 +100,11 @@ void CommandSdos::execute(const StringVector &args)
throwInvalidUsageException(err);
}
multiMaster = getMasterIndices().size() > 1;
masterIndices = getMasterIndices();
multiMaster = masterIndices.size() > 1;
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -76,6 +76,7 @@ string CommandSiiRead::helpString() const
void CommandSiiRead::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
ec_ioctl_slave_t *slave;
ec_ioctl_slave_sii_t data;
@ -89,11 +90,12 @@ void CommandSiiRead::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -78,6 +78,7 @@ string CommandSiiWrite::helpString() const
void CommandSiiWrite::execute(const StringVector &args)
{
MasterIndexList masterIndices;
stringstream err;
ec_ioctl_slave_sii_t data;
ifstream file;
@ -88,11 +89,12 @@ void CommandSiiWrite::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
if (args[0] == "-") {
loadSiiData(&data, cin);

View File

@ -108,6 +108,7 @@ string CommandSlaves::helpString() const
void CommandSlaves::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
bool doIndent;
@ -117,10 +118,11 @@ void CommandSlaves::execute(const StringVector &args)
throwInvalidUsageException(err);
}
doIndent = getMasterIndices().size() > 1;
masterIndices = getMasterIndices();
doIndent = masterIndices.size() > 1;
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -78,6 +78,7 @@ string CommandSoeRead::helpString() const
void CommandSoeRead::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
stringstream err;
const DataType *dataType = NULL;
@ -95,11 +96,12 @@ void CommandSoeRead::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);
if (slaves.size() != 1) {

View File

@ -80,6 +80,7 @@ string CommandSoeWrite::helpString() const
void CommandSoeWrite::execute(const StringVector &args)
{
MasterIndexList masterIndices;
stringstream strIdn, err;
const DataType *dataType = NULL;
ec_ioctl_slave_soe_write_t ioctl;
@ -98,11 +99,12 @@ void CommandSoeWrite::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::ReadWrite);
slaves = selectedSlaves(m);
if (slaves.size() != 1) {

View File

@ -68,6 +68,7 @@ string CommandStates::helpString() const
void CommandStates::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
SlaveList::const_iterator si;
stringstream err;
@ -98,9 +99,10 @@ void CommandStates::execute(const StringVector &args)
throwInvalidUsageException(err);
}
masterIndices = getMasterIndices();
MasterIndexList::const_iterator mi;
for (mi = getMasterIndices().begin();
mi != getMasterIndices().end(); mi++) {
for (mi = masterIndices.begin();
mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::ReadWrite);
slaves = selectedSlaves(m);

View File

@ -82,6 +82,7 @@ string CommandUpload::helpString() const
void CommandUpload::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
stringstream err, strIndex, strSubIndex;
ec_ioctl_slave_sdo_upload_t data;
@ -112,11 +113,12 @@ void CommandUpload::execute(const StringVector &args)
}
data.sdo_entry_subindex = uval;
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);
if (slaves.size() != 1) {

View File

@ -71,6 +71,7 @@ string CommandXml::helpString() const
void CommandXml::execute(const StringVector &args)
{
MasterIndexList masterIndices;
SlaveList slaves;
SlaveList::const_iterator si;
@ -80,12 +81,13 @@ void CommandXml::execute(const StringVector &args)
throwInvalidUsageException(err);
}
if (getMasterIndices().size() != 1) {
masterIndices = getMasterIndices();
if (masterIndices.size() != 1) {
stringstream err;
err << getName() << " requires to select a single master!";
throwInvalidUsageException(err);
}
MasterDevice m(getMasterIndices().front());
MasterDevice m(masterIndices.front());
m.open(MasterDevice::Read);
slaves = selectedSlaves(m);

View File

@ -63,7 +63,6 @@ using namespace std;
#include "CommandVersion.h"
#include "CommandXml.h"
#include "NumberListParser.h"
#include "MasterDevice.h"
/*****************************************************************************/
@ -76,8 +75,7 @@ string commandName;
Command::StringVector commandArgs;
// option variables
list<unsigned int> masterIndices;
string masterIndexList = "-"; // all masters
string masters = "-"; // all masters
int slavePosition = -1;
int slaveAlias = -1;
int domainIndex = -1;
@ -113,7 +111,10 @@ string usage()
str << endl
<< "Global options:" << endl
<< " --master -m <master> Index of the master to use. Default: 0."
<< " --master -m <master> Comma separated list of masters" << endl
<< " to select, ranges are allowed." << endl
<< " Examples: '1,3', '5-7,9', '-3'." << endl
<< " Default: '-' (all)."
<< endl
<< " --force -f Force a command." << endl
<< " --quiet -q Output less information." << endl
@ -132,20 +133,6 @@ string usage()
/*****************************************************************************/
class MasterIndexParser:
public NumberListParser
{
unsigned int getMax()
{
MasterDevice dev;
dev.setIndex(0U);
dev.open(MasterDevice::Read);
return dev.getMasterCount() - 1;
};
};
/*****************************************************************************/
void getOptions(int argc, char **argv)
{
int c, argCount;
@ -171,7 +158,7 @@ void getOptions(int argc, char **argv)
switch (c) {
case 'm':
masterIndexList = optarg;
masters = optarg;
break;
case 'a':
@ -261,19 +248,6 @@ void getOptions(int argc, char **argv)
}
}
try {
MasterIndexParser p;
masterIndices = p.parse(masterIndexList.c_str());
} catch (MasterDeviceException &e) {
cerr << "Failed to obtain number of masters: " << e.what() << endl;
exit(1);
} catch (runtime_error &e) {
cerr << "Invalid master argument " << masterIndexList
<< ": " << e.what() << endl
<< endl << usage();
exit(1);
}
commandName = argv[optind];
while (++optind < argc)
commandArgs.push_back(string(argv[optind]));
@ -348,18 +322,12 @@ int main(int argc, char **argv)
matchingCommands = getMatchingCommands(commandName);
if (masterIndices.empty()) {
cerr << "List of master indices may not be empty!" << endl
<< endl << usage();
exit(1);
}
if (matchingCommands.size()) {
if (matchingCommands.size() == 1) {
cmd = matchingCommands.front();
if (!helpRequested) {
try {
cmd->setMasterIndices(masterIndices);
cmd->setMasters(masters);
cmd->setVerbosity(verbosity);
cmd->setAlias(slaveAlias);
cmd->setPosition(slavePosition);