fp@1142: /***************************************************************************** fp@1142: * fp@1363: * $Id$ fp@1363: * fp@1363: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1363: * fp@1363: * This file is part of the IgH EtherCAT Master. fp@1363: * fp@1363: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1363: * modify it under the terms of the GNU General Public License version 2, as fp@1363: * published by the Free Software Foundation. fp@1363: * fp@1363: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1363: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1363: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1363: * Public License for more details. fp@1363: * fp@1363: * You should have received a copy of the GNU General Public License along fp@1363: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1363: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1363: * fp@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1142: * fp@1880: * vim: expandtab fp@1880: * fp@1142: ****************************************************************************/ fp@1142: fp@2010: #include fp@2010: using namespace std; fp@2010: fp@1142: #include "Command.h" fp@1826: #include "MasterDevice.h" fp@1869: #include "NumberListParser.h" fp@1869: fp@1869: /*****************************************************************************/ fp@1869: fp@2013: typedef map AliasMap; fp@2013: typedef map ConfigMap; fp@2013: fp@2013: /*****************************************************************************/ fp@2013: fp@1869: class MasterIndexParser: fp@1869: public NumberListParser fp@1869: { fp@2010: protected: fp@2010: int getMax() { fp@2010: MasterDevice dev; fp@2010: dev.setIndex(0U); fp@2010: dev.open(MasterDevice::Read); fp@2010: return (int) dev.getMasterCount() - 1; fp@2010: }; fp@2010: }; fp@2010: fp@2010: /*****************************************************************************/ fp@2010: fp@2010: class SlaveAliasParser: fp@2010: public NumberListParser fp@2010: { fp@2010: public: fp@2010: SlaveAliasParser(ec_ioctl_master_t &master, MasterDevice &dev): fp@2010: master(master), dev(dev) {} fp@2010: fp@2010: protected: fp@2010: int getMax() { fp@2010: unsigned int i; fp@2010: fp@2010: uint16_t maxAlias = 0; fp@2010: for (i = 0; i < master.slave_count; i++) { fp@2010: ec_ioctl_slave_t slave; fp@2010: dev.getSlave(&slave, i); fp@2010: if (slave.alias > maxAlias) { fp@2010: maxAlias = slave.alias; fp@2010: } fp@2010: } fp@2010: return maxAlias ? maxAlias : -1; fp@2010: }; fp@2010: fp@2010: private: fp@2010: ec_ioctl_master_t &master; fp@2010: MasterDevice &dev; fp@2010: }; fp@2010: fp@2010: /*****************************************************************************/ fp@2010: fp@2010: class ConfigAliasParser: fp@2010: public NumberListParser fp@2010: { fp@2010: public: fp@2013: ConfigAliasParser(unsigned int maxAlias): fp@2013: maxAlias(maxAlias) {} fp@2010: fp@2010: protected: fp@2013: int getMax() { return maxAlias; }; fp@2010: fp@2010: private: fp@2013: unsigned int maxAlias; fp@2010: }; fp@2010: fp@2010: /*****************************************************************************/ fp@2010: fp@2010: class PositionParser: fp@2010: public NumberListParser fp@2010: { fp@2010: public: fp@2010: PositionParser(unsigned int count): fp@2010: count(count) {} fp@2010: fp@2010: protected: fp@2010: int getMax() { fp@2010: return count - 1; fp@2010: }; fp@2010: fp@2010: private: fp@2010: const unsigned int count; fp@1869: }; fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@2013: class AliasPositionParser: fp@2013: public NumberListParser fp@2013: { fp@2013: public: fp@2013: AliasPositionParser(const AliasMap &aliasMap): fp@2013: aliasMap(aliasMap) {} fp@2013: fp@2013: protected: fp@2013: int getMax() { fp@2013: AliasMap::const_iterator i; fp@2013: int maxPos = -1; fp@2013: fp@2013: for (i = aliasMap.begin(); i != aliasMap.end(); i++) { fp@2013: if (i->first > maxPos) { fp@2013: maxPos = i->first; fp@2013: } fp@2013: } fp@2013: fp@2013: return maxPos; fp@2013: }; fp@2013: fp@2013: private: fp@2013: const AliasMap &aliasMap; fp@2013: }; fp@2013: fp@2013: /*****************************************************************************/ fp@2013: fp@1142: Command::Command(const string &name, const string &briefDesc): fp@1142: name(name), fp@1142: briefDesc(briefDesc), fp@2529: verbosity(Normal), fp@2529: emergency(false), fp@2529: force(false) fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: Command::~Command() fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1869: void Command::setMasters(const string &m) fp@1869: { fp@1869: masters = m; fp@1826: }; fp@1826: fp@1826: /*****************************************************************************/ fp@1826: fp@1142: void Command::setVerbosity(Verbosity v) fp@1142: { fp@1804: verbosity = v; fp@1142: }; fp@1142: fp@1151: /*****************************************************************************/ fp@1151: fp@2010: void Command::setAliases(const string &a) fp@2010: { fp@2010: aliases = a; fp@2010: }; fp@2010: fp@2010: /*****************************************************************************/ fp@2010: fp@2010: void Command::setPositions(const string &p) fp@2010: { fp@2010: positions = p; fp@2010: }; fp@2010: fp@2010: /*****************************************************************************/ fp@2010: fp@2010: void Command::setDomains(const string &d) fp@2010: { fp@2010: domains = d; fp@1166: }; fp@1166: fp@1166: /*****************************************************************************/ fp@1166: fp@1166: void Command::setDataType(const string &t) fp@1166: { fp@1804: dataType = t; fp@1166: }; fp@1166: fp@1166: /*****************************************************************************/ fp@1166: fp@2529: void Command::setEmergency(bool e) fp@2529: { fp@2529: emergency = e; fp@2529: }; fp@2529: fp@2529: /*****************************************************************************/ fp@2529: fp@1166: void Command::setForce(bool f) fp@1166: { fp@1804: force = f; fp@1166: }; fp@1166: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: void Command::setOutputFile(const string &f) fp@1335: { fp@1804: outputFile = f; fp@1335: }; fp@1335: fp@2436: /*****************************************************************************/ fp@2436: fp@2436: void Command::setSkin(const string &s) fp@2436: { fp@2436: skin = s; fp@2436: }; fp@2436: fp@1142: /****************************************************************************/ fp@1142: fp@1142: bool Command::matchesSubstr(const string &cmd) const fp@1142: { fp@1142: return name.substr(0, cmd.length()) == cmd; fp@1142: } fp@2421: fp@1142: /****************************************************************************/ fp@1142: fp@1142: bool Command::matchesAbbrev(const string &abb) const fp@1142: { fp@1142: unsigned int i; fp@1142: size_t pos = 0; fp@1142: fp@1142: for (i = 0; i < abb.length(); i++) { fp@1142: pos = name.find(abb[i], pos); fp@1142: if (pos == string::npos) fp@1142: return false; fp@1142: } fp@1142: fp@1142: return true; fp@1142: } fp@2421: fp@1142: /*****************************************************************************/ fp@1142: fp@1144: string Command::numericInfo() fp@1144: { fp@1144: stringstream str; fp@1144: fp@1144: str << "Numerical values can be specified either with decimal (no" << endl fp@1144: << "prefix), octal (prefix '0') or hexadecimal (prefix '0x') base." fp@1144: << endl; fp@1144: fp@1144: return str.str(); fp@1144: } fp@1144: fp@1144: /*****************************************************************************/ fp@1144: fp@1155: void Command::throwInvalidUsageException(const stringstream &s) const fp@1142: { fp@1142: throw InvalidUsageException(s); fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1834: void Command::throwCommandException(const string &msg) const fp@1834: { fp@1834: throw CommandException(msg); fp@1834: } fp@1834: fp@1834: /*****************************************************************************/ fp@1834: fp@1155: void Command::throwCommandException(const stringstream &s) const fp@1142: { fp@1142: throw CommandException(s); fp@1142: } fp@1142: fp@1151: /*****************************************************************************/ fp@1151: fp@1155: void Command::throwSingleSlaveRequired(unsigned int size) const fp@1155: { fp@1155: stringstream err; fp@1155: fp@1199: err << "The slave selection matches " << size << " slaves. '" fp@1155: << name << "' requires a single slave."; fp@1155: fp@1155: throwInvalidUsageException(err); fp@1155: } fp@1155: fp@1155: /*****************************************************************************/ fp@1155: fp@1869: Command::MasterIndexList Command::getMasterIndices() const fp@1869: { fp@1880: MasterIndexList indices; fp@1869: fp@1869: try { fp@1869: MasterIndexParser p; fp@1869: indices = p.parse(masters.c_str()); fp@1869: } catch (MasterDeviceException &e) { fp@1880: stringstream err; fp@1880: err << "Failed to obtain number of masters: " << e.what(); fp@1880: throwCommandException(err); fp@1869: } catch (runtime_error &e) { fp@1880: stringstream err; fp@1869: err << "Invalid master argument '" << masters << "': " << e.what(); fp@1880: throwInvalidUsageException(err); fp@1880: } fp@1880: fp@1880: return indices; fp@1869: } fp@1869: fp@1869: /*****************************************************************************/ fp@1869: fp@1870: unsigned int Command::getSingleMasterIndex() const fp@1870: { fp@1880: MasterIndexList masterIndices = getMasterIndices(); fp@2013: fp@1870: if (masterIndices.size() != 1) { fp@1880: stringstream err; fp@1870: err << getName() << " requires to select a single master!"; fp@1870: throwInvalidUsageException(err); fp@1870: } fp@2013: fp@1880: return masterIndices.front(); fp@1870: } fp@1870: fp@1870: /*****************************************************************************/ fp@1870: fp@1151: Command::SlaveList Command::selectedSlaves(MasterDevice &m) fp@1151: { fp@1160: ec_ioctl_master_t master; fp@2010: unsigned int i; fp@1151: ec_ioctl_slave_t slave; fp@1151: SlaveList list; fp@1151: fp@1160: m.getMaster(&master); fp@1160: fp@2010: if (aliases == "-") { // no alias given fp@2010: PositionParser pp(master.slave_count); fp@2010: NumberListParser::List posList = pp.parse(positions.c_str()); fp@2010: NumberListParser::List::const_iterator pi; fp@2013: fp@2010: for (pi = posList.begin(); pi != posList.end(); pi++) { fp@2014: if (*pi < master.slave_count) { fp@2014: m.getSlave(&slave, *pi); fp@2014: list.push_back(slave); fp@2014: } fp@1151: } fp@2010: } else { // aliases given fp@2010: SlaveAliasParser ap(master, m); fp@2010: NumberListParser::List aliasList = ap.parse(aliases.c_str()); fp@2010: NumberListParser::List::const_iterator ai; fp@2010: fp@2010: for (ai = aliasList.begin(); ai != aliasList.end(); ai++) { fp@2010: fp@2010: // gather slaves with that alias (and following) fp@2010: uint16_t lastAlias = 0; fp@2010: vector aliasSlaves; fp@2010: fp@1160: for (i = 0; i < master.slave_count; i++) { fp@1151: m.getSlave(&slave, i); fp@1151: if (slave.alias) { fp@2010: if (lastAlias && lastAlias == *ai && slave.alias != *ai) { fp@2010: // ignore multiple ocurrences of the same alias to fp@2010: // assure consistency for the position argument fp@2010: break; fp@2010: } fp@1151: lastAlias = slave.alias; fp@1151: } fp@2010: if (lastAlias == *ai) { fp@2010: aliasSlaves.push_back(slave); fp@2010: } fp@2010: } fp@2010: fp@2010: PositionParser pp(aliasSlaves.size()); fp@2010: NumberListParser::List posList = pp.parse(positions.c_str()); fp@2010: NumberListParser::List::const_iterator pi; fp@2010: fp@2010: for (pi = posList.begin(); pi != posList.end(); pi++) { fp@2010: if (*pi < aliasSlaves.size()) { fp@2010: list.push_back(aliasSlaves[*pi]); fp@2010: } fp@1151: } fp@1151: } fp@1151: } fp@1151: fp@1151: return list; fp@1151: } fp@1151: fp@1156: /*****************************************************************************/ fp@1156: fp@1156: bool operator<( fp@1156: const ec_ioctl_config_t &a, fp@1156: const ec_ioctl_config_t &b fp@1156: ) fp@1156: { fp@1156: return a.alias < b.alias fp@1156: || (a.alias == b.alias && a.position < b.position); fp@1156: } fp@1156: fp@1156: /*****************************************************************************/ fp@1156: fp@1156: Command::ConfigList Command::selectedConfigs(MasterDevice &m) fp@1156: { fp@1156: unsigned int i; fp@1156: ec_ioctl_master_t master; fp@1156: ec_ioctl_config_t config; fp@1156: ConfigList list; fp@1156: stringstream err; fp@1156: fp@1156: m.getMaster(&master); fp@1156: fp@2013: if (aliases == "-" && positions == "-") { // shortcut fp@2013: for (i = 0; i < master.config_count; i++) { fp@2013: m.getConfig(&config, i); fp@2010: list.push_back(config); fp@2010: } fp@2013: } else { // take the long way home... fp@2013: ConfigMap configs; fp@2013: uint16_t maxAlias = 0; fp@2013: fp@2013: // fill cascaded map structure with all configs fp@2013: for (i = 0; i < master.config_count; i++) { fp@2013: m.getConfig(&config, i); fp@2013: AliasMap &aliasMap = configs[config.alias]; fp@2013: aliasMap[config.position] = config; fp@2013: if (config.alias > maxAlias) { fp@2013: maxAlias = config.alias; fp@2013: } fp@2013: } fp@2013: fp@2013: ConfigAliasParser ap(maxAlias); fp@2010: NumberListParser::List aliasList = ap.parse(aliases.c_str()); fp@2010: NumberListParser::List::const_iterator ai; fp@2010: fp@2010: for (ai = aliasList.begin(); ai != aliasList.end(); ai++) { fp@2010: fp@2013: ConfigMap::iterator ci = configs.find(*ai); fp@2013: if (ci == configs.end()) { fp@2013: continue; fp@2013: } fp@2013: fp@2013: AliasMap &aliasMap = configs[*ai]; fp@2013: AliasPositionParser pp(aliasMap); fp@2010: NumberListParser::List posList = pp.parse(positions.c_str()); fp@2010: NumberListParser::List::const_iterator pi; fp@2010: fp@2010: for (pi = posList.begin(); pi != posList.end(); pi++) { fp@2013: AliasMap::const_iterator ci; fp@2013: fp@2013: ci = aliasMap.find(*pi); fp@2013: if (ci != aliasMap.end()) { fp@2010: list.push_back(ci->second); fp@1156: } fp@1156: } fp@1156: } fp@1156: } fp@1156: fp@1156: list.sort(); fp@1156: return list; fp@1156: } fp@1156: fp@1142: /****************************************************************************/ fp@1146: fp@2453: Command::DomainList Command::selectedDomains(MasterDevice &m, fp@2453: const ec_ioctl_master_t &io) fp@2453: { fp@1804: DomainList list; fp@1166: fp@2453: PositionParser pp(io.domain_count); fp@2010: NumberListParser::List domList = pp.parse(domains.c_str()); fp@2010: NumberListParser::List::const_iterator di; fp@2010: fp@2010: for (di = domList.begin(); di != domList.end(); di++) { fp@2453: if (*di < io.domain_count) { fp@2014: ec_ioctl_domain_t d; fp@2014: m.getDomain(&d, *di); fp@2014: list.push_back(d); fp@2014: } fp@1804: } fp@1804: fp@1804: return list; fp@1166: } fp@1166: fp@1166: /****************************************************************************/ fp@1166: fp@2529: int Command::emergencySlave() const fp@2529: { fp@2529: unsigned int ret; fp@2529: fp@2529: stringstream str; fp@2529: str << positions; fp@2529: str >> ret; fp@2529: fp@2529: return ret; fp@2529: } fp@2529: fp@2529: /****************************************************************************/ fp@2529: fp@1146: string Command::alStateString(uint8_t state) fp@1146: { fp@1918: string ret; fp@1918: fp@1918: switch (state & EC_SLAVE_STATE_MASK) { fp@1918: case 1: ret = "INIT"; break; fp@1918: case 2: ret = "PREOP"; break; fp@1918: case 3: ret = "BOOT"; break; fp@1918: case 4: ret = "SAFEOP"; break; fp@1918: case 8: ret = "OP"; break; fp@1918: default: ret = "???"; fp@1918: } fp@1918: fp@2421: if (state & EC_SLAVE_STATE_ACK_ERR) { fp@1918: ret += "+ERROR"; fp@1918: } fp@1918: fp@1918: return ret; fp@1918: } fp@1918: fp@1918: /****************************************************************************/