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@1142: ****************************************************************************/ fp@1142: fp@1142: #include fp@1142: #include fp@1142: #include fp@1142: using namespace std; fp@1142: fp@1142: #include "CommandAlias.h" fp@1142: #include "sii_crc.h" fp@1826: #include "MasterDevice.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandAlias::CommandAlias(): fp@1142: Command("alias", "Write alias addresses.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1968: string CommandAlias::helpString(const string &binaryBaseName) const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1968: str << binaryBaseName << " " << getName() << " [OPTIONS] " << endl fp@1142: << endl fp@1142: << getBriefDescription() << endl fp@1142: << endl fp@1142: << "Arguments:" << endl fp@1144: << " ALIAS must be an unsigned 16 bit number. Zero means" << endl fp@1144: << " removing an alias address." << endl fp@1157: << endl fp@1157: << "If multiple slaves are selected, the --force option" << endl fp@1157: << "is required." << endl fp@1157: << endl fp@1142: << "Command-specific options:" << endl fp@1157: << " --alias -a " << endl fp@1157: << " --position -p Slave selection. See the help of" << endl fp@1157: << " the 'slaves' command." << endl fp@1157: << " --force -f Acknowledge writing aliases of" << endl fp@1157: << " multiple slaves." << endl fp@1142: << endl fp@1142: << numericInfo(); fp@1142: fp@1142: return str.str(); fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: /** Writes the Secondary slave address (alias) to the slave's SII. fp@1142: */ fp@1826: void CommandAlias::execute(const StringVector &args) fp@1142: { fp@1142: uint16_t alias; fp@1142: stringstream err, strAlias; fp@1142: int number; fp@1151: SlaveList slaves; fp@1151: SlaveList::const_iterator si; fp@1142: fp@1142: if (args.size() != 1) { fp@1142: err << "'" << getName() << "' takes exactly one argument!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: fp@1142: strAlias << args[0]; fp@1142: strAlias fp@1142: >> resetiosflags(ios::basefield) // guess base from prefix fp@1142: >> number; fp@1142: if (strAlias.fail() || number < 0x0000 || number > 0xffff) { fp@1142: err << "Invalid alias '" << args[0] << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: alias = number; fp@1142: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1151: m.open(MasterDevice::ReadWrite); fp@1151: slaves = selectedSlaves(m); fp@2421: fp@1166: if (slaves.size() > 1 && !getForce()) { fp@1151: err << "This will write the alias addresses of " fp@1151: << slaves.size() << " slaves to " << alias fp@1151: << "! Please specify --force to proceed."; fp@1151: throwCommandException(err); fp@1151: } fp@1142: fp@1157: if (!slaves.size() && getVerbosity() != Quiet) { fp@1157: cerr << "Warning: Selection matches no slaves!" << endl; fp@1157: } fp@1157: fp@1151: for (si = slaves.begin(); si != slaves.end(); si++) { fp@1151: writeSlaveAlias(m, *si, alias); fp@1142: } fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: /** Writes the Secondary slave address (alias) to the slave's SII. fp@1142: */ fp@1142: void CommandAlias::writeSlaveAlias( fp@1142: MasterDevice &m, fp@1151: const ec_ioctl_slave_t &slave, fp@1142: uint16_t alias fp@1142: ) fp@1142: { fp@1142: ec_ioctl_slave_sii_t data; fp@1142: stringstream err; fp@1142: uint8_t crc; fp@1142: fp@1142: if (slave.sii_nwords < 8) { fp@1142: err << "Current SII contents are too small to set an alias " fp@1142: << "(" << slave.sii_nwords << " words)!"; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: // read first 8 SII words fp@1151: data.slave_position = slave.position; fp@1142: data.offset = 0; fp@1142: data.nwords = 8; fp@1142: data.words = new uint16_t[data.nwords]; fp@1142: fp@1142: try { fp@1142: m.readSii(&data); fp@1142: } catch (MasterDeviceException &e) { fp@1142: delete [] data.words; fp@1142: err << "Failed to read SII: " << e.what(); fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: // write new alias address in word 4 fp@1254: data.words[4] = cpu_to_le16(alias); fp@1142: fp@1142: // calculate checksum over words 0 to 6 fp@1142: crc = calcSiiCrc((const uint8_t *) data.words, 14); fp@1142: fp@1142: // write new checksum into first byte of word 7 fp@1142: *(uint8_t *) (data.words + 7) = crc; fp@1142: fp@1142: // write first 8 words with new alias and checksum fp@1142: try { fp@1142: m.writeSii(&data); fp@1142: } catch (MasterDeviceException &e) { fp@1142: delete [] data.words; fp@1142: err << "Failed to read SII: " << e.what(); fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: delete [] data.words; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/