diff --git a/tool/Command.cpp b/tool/Command.cpp index 8b9da9b7a96850dbd9a5f3c68cf7914e0695820a..565999e1c66fa754610d678a652b58ad866e22d9 100644 --- a/tool/Command.cpp +++ b/tool/Command.cpp @@ -209,6 +209,19 @@ Command::MasterIndexList Command::getMasterIndices() const /*****************************************************************************/ +unsigned int Command::getSingleMasterIndex() const +{ + MasterIndexList masterIndices = getMasterIndices(); + if (masterIndices.size() != 1) { + stringstream err; + err << getName() << " requires to select a single master!"; + throwInvalidUsageException(err); + } + return masterIndices.front(); +} + +/*****************************************************************************/ + Command::SlaveList Command::selectedSlaves(MasterDevice &m) { ec_ioctl_master_t master; diff --git a/tool/Command.h b/tool/Command.h index b98001f89698d36398e746f776b0f97c0ccb4a82..14306c5379e87eddd9cbaf6ccec04b07630b3c5d 100644 --- a/tool/Command.h +++ b/tool/Command.h @@ -87,6 +87,7 @@ class Command typedef list<unsigned int> MasterIndexList; void setMasters(const string &); MasterIndexList getMasterIndices() const; + unsigned int getSingleMasterIndex() const; enum Verbosity { Quiet, diff --git a/tool/CommandAlias.cpp b/tool/CommandAlias.cpp index c6d588f336354b26d57b36da908884d192be5ccb..6c53379ef66d5c69f57c119f5f7a7752de4ae0c8 100644 --- a/tool/CommandAlias.cpp +++ b/tool/CommandAlias.cpp @@ -81,7 +81,6 @@ void CommandAlias::execute(const StringVector &args) uint16_t alias; stringstream err, strAlias; int number; - MasterIndexList masterIndices; SlaveList slaves; SlaveList::const_iterator si; @@ -100,12 +99,7 @@ void CommandAlias::execute(const StringVector &args) } alias = number; - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::ReadWrite); slaves = selectedSlaves(m); diff --git a/tool/CommandDownload.cpp b/tool/CommandDownload.cpp index fbbfcb44a8abc8c64c1730edba2080917df6d709..7253d0b525283326730768cf9ae93f5bd96fc013 100644 --- a/tool/CommandDownload.cpp +++ b/tool/CommandDownload.cpp @@ -85,7 +85,6 @@ 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; @@ -115,12 +114,7 @@ void CommandDownload::execute(const StringVector &args) } data.sdo_entry_subindex = number; - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::ReadWrite); slaves = selectedSlaves(m); if (slaves.size() != 1) { diff --git a/tool/CommandFoeRead.cpp b/tool/CommandFoeRead.cpp index 1afc78551c2dc321ed4e6911f9a87dd36122c786..3ab6d91374816dd7c67aa8fb2aff201d8ec41633 100644 --- a/tool/CommandFoeRead.cpp +++ b/tool/CommandFoeRead.cpp @@ -76,7 +76,6 @@ 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; @@ -88,12 +87,7 @@ void CommandFoeRead::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::Read); slaves = selectedSlaves(m); diff --git a/tool/CommandFoeWrite.cpp b/tool/CommandFoeWrite.cpp index c1c3139ba5a62de942311a10823e4c84359b8383..58094e1d011e7ef68f7b051ccfaf32596fc8339b 100644 --- a/tool/CommandFoeWrite.cpp +++ b/tool/CommandFoeWrite.cpp @@ -82,7 +82,6 @@ string CommandFoeWrite::helpString() const void CommandFoeWrite::execute(const StringVector &args) { - MasterIndexList masterIndices; stringstream err; ec_ioctl_slave_foe_t data; ifstream file; @@ -94,13 +93,6 @@ void CommandFoeWrite::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); - if (args[0] == "-") { loadFoeData(&data, cin); if (getOutputFile().empty()) { @@ -128,6 +120,7 @@ void CommandFoeWrite::execute(const StringVector &args) } } + MasterDevice m(getSingleMasterIndex()); try { m.open(MasterDevice::ReadWrite); } catch (MasterDeviceException &e) { diff --git a/tool/CommandGraph.cpp b/tool/CommandGraph.cpp index 78cf2b2f23841c5264238229e9ca10a2208f0b0f..951c5d27935290d9150ea6bdf0dea59ae60d7dad 100644 --- a/tool/CommandGraph.cpp +++ b/tool/CommandGraph.cpp @@ -67,7 +67,6 @@ 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; @@ -91,13 +90,7 @@ void CommandGraph::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - stringstream err; - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::Read); m.getMaster(&master); diff --git a/tool/CommandRegRead.cpp b/tool/CommandRegRead.cpp index 65851865352689a7dfd120cfc498a8d92df33ae9..8af9635f1e4e778a5784d5fd9d9af23b60bd4196 100644 --- a/tool/CommandRegRead.cpp +++ b/tool/CommandRegRead.cpp @@ -79,7 +79,6 @@ string CommandRegRead::helpString() const void CommandRegRead::execute(const StringVector &args) { - MasterIndexList masterIndices; SlaveList slaves; ec_ioctl_slave_reg_t data; stringstream strOffset, err; @@ -141,12 +140,7 @@ void CommandRegRead::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::Read); slaves = selectedSlaves(m); diff --git a/tool/CommandRegWrite.cpp b/tool/CommandRegWrite.cpp index 6de5034ec526a8e1d736357633a6707bd86b903b..964dc1bbc98191c1cb2b407c715a2e9bfe824e73 100644 --- a/tool/CommandRegWrite.cpp +++ b/tool/CommandRegWrite.cpp @@ -81,7 +81,6 @@ string CommandRegWrite::helpString() const void CommandRegWrite::execute(const StringVector &args) { - MasterIndexList masterIndices; stringstream strOffset, err; ec_ioctl_slave_reg_t data; ifstream file; @@ -101,13 +100,6 @@ void CommandRegWrite::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); - if (getDataType().empty()) { if (args[1] == "-") { loadRegData(&data, cin); @@ -157,6 +149,7 @@ void CommandRegWrite::execute(const StringVector &args) throwInvalidUsageException(err); } + MasterDevice m(getSingleMasterIndex()); try { m.open(MasterDevice::ReadWrite); } catch (MasterDeviceException &e) { diff --git a/tool/CommandSiiRead.cpp b/tool/CommandSiiRead.cpp index 368b2e598adbe4ddf9867ad5f69ffc68df432a7f..24e23ae8873ce9c67a422236541b59f3c3419dc7 100644 --- a/tool/CommandSiiRead.cpp +++ b/tool/CommandSiiRead.cpp @@ -76,7 +76,6 @@ 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; @@ -90,12 +89,7 @@ void CommandSiiRead::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::Read); slaves = selectedSlaves(m); diff --git a/tool/CommandSiiWrite.cpp b/tool/CommandSiiWrite.cpp index 4aa3eddd8113beb88a5b08f705bdd7c266a66d7e..de6d0507e0dc80c4a63294f11881d53f8c746107 100644 --- a/tool/CommandSiiWrite.cpp +++ b/tool/CommandSiiWrite.cpp @@ -78,7 +78,6 @@ string CommandSiiWrite::helpString() const void CommandSiiWrite::execute(const StringVector &args) { - MasterIndexList masterIndices; stringstream err; ec_ioctl_slave_sii_t data; ifstream file; @@ -89,13 +88,6 @@ void CommandSiiWrite::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); - if (args[0] == "-") { loadSiiData(&data, cin); } else { @@ -117,6 +109,7 @@ void CommandSiiWrite::execute(const StringVector &args) } } + MasterDevice m(getSingleMasterIndex()); try { m.open(MasterDevice::ReadWrite); } catch (MasterDeviceException &e) { diff --git a/tool/CommandSoeRead.cpp b/tool/CommandSoeRead.cpp index d46999a065d48c0c94bda98a38d73baced75547a..a528e9807eb4b89dba34e15cc6fff3c11f46ad82 100644 --- a/tool/CommandSoeRead.cpp +++ b/tool/CommandSoeRead.cpp @@ -78,7 +78,6 @@ string CommandSoeRead::helpString() const void CommandSoeRead::execute(const StringVector &args) { - MasterIndexList masterIndices; SlaveList slaves; stringstream err; const DataType *dataType = NULL; @@ -96,12 +95,7 @@ void CommandSoeRead::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::Read); slaves = selectedSlaves(m); if (slaves.size() != 1) { diff --git a/tool/CommandSoeWrite.cpp b/tool/CommandSoeWrite.cpp index 6c348521ae6d4ee372973caf3b8da64702cc95c0..ab5f17404f9f1913551f44f70f63968c6e08fc9f 100644 --- a/tool/CommandSoeWrite.cpp +++ b/tool/CommandSoeWrite.cpp @@ -80,7 +80,6 @@ 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; @@ -99,12 +98,7 @@ void CommandSoeWrite::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::ReadWrite); slaves = selectedSlaves(m); if (slaves.size() != 1) { diff --git a/tool/CommandUpload.cpp b/tool/CommandUpload.cpp index 38d3fbf7517e454f01015116baf9a85a203f7f53..4146ab6937826bb1ac30d8665745154048afc7d3 100644 --- a/tool/CommandUpload.cpp +++ b/tool/CommandUpload.cpp @@ -82,7 +82,6 @@ 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; @@ -113,12 +112,7 @@ void CommandUpload::execute(const StringVector &args) } data.sdo_entry_subindex = uval; - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::Read); slaves = selectedSlaves(m); if (slaves.size() != 1) { diff --git a/tool/CommandXml.cpp b/tool/CommandXml.cpp index 4969fa59045edf86b437cc8c21f49d9f6f61c9ce..baf42a3d00e38d120f2c30aed517e711395c5e61 100644 --- a/tool/CommandXml.cpp +++ b/tool/CommandXml.cpp @@ -71,7 +71,6 @@ string CommandXml::helpString() const void CommandXml::execute(const StringVector &args) { - MasterIndexList masterIndices; SlaveList slaves; SlaveList::const_iterator si; @@ -81,13 +80,7 @@ void CommandXml::execute(const StringVector &args) throwInvalidUsageException(err); } - masterIndices = getMasterIndices(); - if (masterIndices.size() != 1) { - stringstream err; - err << getName() << " requires to select a single master!"; - throwInvalidUsageException(err); - } - MasterDevice m(masterIndices.front()); + MasterDevice m(getSingleMasterIndex()); m.open(MasterDevice::Read); slaves = selectedSlaves(m);