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 "CommandSiiWrite.h" fp@1142: #include "sii_crc.h" fp@1826: #include "MasterDevice.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandSiiWrite::CommandSiiWrite(): fp@1142: Command("sii_write", "Write SII contents to a slave.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1968: string CommandSiiWrite::helpString(const string &binaryBaseName) const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1968: str << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@2421: << endl fp@1142: << getBriefDescription() << endl fp@1142: << endl fp@1157: << "This command requires a single slave to be selected." << endl fp@1804: << endl fp@1142: << "The file contents are checked for validity and integrity." << endl fp@1142: << "These checks can be overridden with the --force option." << endl fp@1142: << endl fp@1142: << "Arguments:" << endl fp@1142: << " FILENAME must be a path to a file that contains a" << endl fp@1161: << " positive number of words. If it is '-'," << endl fp@1161: << " data are read from stdin." << endl fp@1142: << 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 Override validity checks." << endl fp@1142: << endl fp@1142: << numericInfo(); fp@1142: fp@1142: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1826: void CommandSiiWrite::execute(const StringVector &args) fp@1142: { fp@1142: stringstream err; fp@1142: ec_ioctl_slave_sii_t data; fp@1142: ifstream file; fp@1161: SlaveList slaves; 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@1161: if (args[0] == "-") { fp@1161: loadSiiData(&data, cin); fp@1161: } else { fp@1161: file.open(args[0].c_str(), ifstream::in | ifstream::binary); fp@1161: if (file.fail()) { fp@1161: err << "Failed to open '" << args[0] << "'!"; fp@1161: throwCommandException(err); fp@1161: } fp@1161: loadSiiData(&data, file); fp@1161: file.close(); fp@1142: } fp@1142: fp@1166: if (!getForce()) { fp@1161: try { fp@1161: checkSiiData(&data); fp@1161: } catch (CommandException &e) { fp@1161: delete [] data.words; fp@1161: throw e; fp@1142: } fp@1142: } fp@1142: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1161: try { fp@1161: m.open(MasterDevice::ReadWrite); fp@1161: } catch (MasterDeviceException &e) { fp@1161: delete [] data.words; fp@1161: throw e; fp@1161: } fp@1161: fp@1157: slaves = selectedSlaves(m); fp@1157: if (slaves.size() != 1) { fp@1161: delete [] data.words; fp@1157: throwSingleSlaveRequired(slaves.size()); fp@1157: } fp@1157: data.slave_position = slaves.front().position; fp@1157: fp@1142: // send data to master fp@1142: data.offset = 0; fp@1161: try { fp@1161: m.writeSii(&data); fp@1161: } catch (MasterDeviceException &e) { fp@1161: delete [] data.words; fp@1161: throw e; fp@1161: } fp@1161: fp@1161: if (getVerbosity() == Verbose) { fp@1161: cerr << "SII writing finished." << endl; fp@1161: } fp@1161: fp@1161: delete [] data.words; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1161: fp@1161: void CommandSiiWrite::loadSiiData( fp@1161: ec_ioctl_slave_sii_t *data, fp@1161: const istream &in fp@1161: ) fp@1161: { fp@1161: stringstream err; fp@1161: ostringstream tmp; fp@1161: fp@1161: tmp << in.rdbuf(); fp@1161: string const &contents = tmp.str(); fp@1161: fp@1161: if (getVerbosity() == Verbose) { fp@1161: cerr << "Read " << contents.size() << " bytes of SII data." << endl; fp@1161: } fp@1161: fp@1161: if (!contents.size() || contents.size() % 2) { fp@1161: err << "Invalid data size " << contents.size() << "! " fp@1161: << "Must be non-zero and even."; fp@1161: throwCommandException(err); fp@1161: } fp@1161: data->nwords = contents.size() / 2; fp@1161: fp@1161: // allocate buffer and read file into buffer fp@1161: data->words = new uint16_t[data->nwords]; fp@1161: contents.copy((char *) data->words, contents.size()); fp@1161: } fp@1161: fp@1161: /*****************************************************************************/ fp@1161: fp@1161: void CommandSiiWrite::checkSiiData( fp@1161: const ec_ioctl_slave_sii_t *data fp@1161: ) fp@1161: { fp@1161: stringstream err; fp@1161: const uint16_t *categoryHeader; fp@1161: uint16_t categoryType, categorySize; fp@1161: uint8_t crc; fp@1161: fp@1161: if (data->nwords < 0x0041) { fp@1161: err << "SII data too short (" << data->nwords << " words)! Mimimum is" fp@1161: " 40 fixed words + 1 delimiter. Use --force to write anyway."; fp@1161: throwCommandException(err); fp@1161: } fp@1161: fp@1161: // calculate checksum over words 0 to 6 fp@1161: crc = calcSiiCrc((const uint8_t *) data->words, 14); fp@1161: if (crc != ((const uint8_t *) data->words)[14]) { fp@1161: err << "CRC incorrect. Must be 0x" fp@1161: << hex << setfill('0') << setw(2) << (unsigned int) crc fp@1161: << ". Use --force to write anyway."; fp@1161: throwCommandException(err); fp@1161: } fp@1161: fp@1161: // cycle through categories to detect corruption fp@1161: categoryHeader = data->words + 0x0040U; fp@1254: categoryType = le16_to_cpup(categoryHeader); fp@1161: while (categoryType != 0xffff) { fp@1161: if (categoryHeader + 1 > data->words + data->nwords) { fp@1161: err << "SII data seem to be corrupted! " fp@1161: << "Use --force to write anyway."; fp@1161: throwCommandException(err); fp@1161: } fp@1254: categorySize = le16_to_cpup(categoryHeader + 1); fp@1161: if (categoryHeader + 2 + categorySize + 1 fp@1161: > data->words + data->nwords) { fp@1161: err << "SII data seem to be corrupted! " fp@1161: "Use --force to write anyway."; fp@1161: throwCommandException(err); fp@1161: } fp@1161: categoryHeader += 2 + categorySize; fp@1254: categoryType = le16_to_cpup(categoryHeader); fp@1161: } fp@1161: } fp@1161: fp@1161: /*****************************************************************************/