fp@1335: /***************************************************************************** fp@1335: * 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@1335: * fp@1335: ****************************************************************************/ fp@1335: fp@1335: #include // basename() fp@1357: #include fp@1335: fp@1335: #include fp@1335: #include fp@1335: #include fp@1335: using namespace std; fp@1335: fp@1335: #include "CommandFoeWrite.h" fp@1336: #include "foe.h" fp@1826: #include "MasterDevice.h" fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: CommandFoeWrite::CommandFoeWrite(): fp@1335: FoeCommand("foe_write", "Store a file on a slave via FoE.") fp@1335: { fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1968: string CommandFoeWrite::helpString(const string &binaryBaseName) const fp@1335: { fp@1335: stringstream str; fp@1335: fp@1968: str << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@1335: << endl fp@1335: << getBriefDescription() << endl fp@1335: << endl fp@1335: << "This command requires a single slave to be selected." << endl fp@1804: << endl fp@1335: << "Arguments:" << endl fp@1335: << " FILENAME can either be a path to a file, or '-'. In" << endl fp@1335: << " the latter case, data are read from stdin and" << endl fp@1335: << " the --output-file option has to be specified." << endl fp@1335: << endl fp@1335: << "Command-specific options:" << endl fp@1335: << " --output-file -o Target filename on the slave." << endl fp@1335: << " If the FILENAME argument is" << endl fp@1335: << " '-', this is mandatory." << endl fp@1335: << " Otherwise, the basename() of" << endl fp@1335: << " FILENAME is used by default." << endl fp@1335: << " --alias -a " << endl fp@1335: << " --position -p Slave selection. See the help" << endl fp@1335: << " of the 'slaves' command." << endl fp@1335: << endl fp@1335: << numericInfo(); fp@1335: fp@1335: return str.str(); fp@1335: } fp@1335: fp@1335: /****************************************************************************/ fp@1335: fp@1826: void CommandFoeWrite::execute(const StringVector &args) fp@1335: { fp@1335: stringstream err; fp@1335: ec_ioctl_slave_foe_t data; fp@1335: ifstream file; fp@1335: SlaveList slaves; fp@1335: string storeFileName; fp@1335: fp@1335: if (args.size() != 1) { fp@1335: err << "'" << getName() << "' takes exactly one argument!"; fp@1335: throwInvalidUsageException(err); fp@1335: } fp@1335: fp@1335: if (args[0] == "-") { fp@1335: loadFoeData(&data, cin); fp@1335: if (getOutputFile().empty()) { fp@1335: err << "Please specify a filename for the slave side" fp@1335: << " with --output-file!"; fp@1335: throwCommandException(err); fp@1335: } else { fp@1335: storeFileName = getOutputFile(); fp@1335: } fp@1335: } else { fp@1335: file.open(args[0].c_str(), ifstream::in | ifstream::binary); fp@1335: if (file.fail()) { fp@1335: err << "Failed to open '" << args[0] << "'!"; fp@1335: throwCommandException(err); fp@1335: } fp@1335: loadFoeData(&data, file); fp@1335: file.close(); fp@1335: if (getOutputFile().empty()) { fp@1335: char *cpy = strdup(args[0].c_str()); // basename can modify fp@1335: // the string contents fp@1335: storeFileName = basename(cpy); fp@1335: free(cpy); fp@1335: } else { fp@1335: storeFileName = getOutputFile(); fp@1335: } fp@1335: } fp@1335: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1335: try { fp@1335: m.open(MasterDevice::ReadWrite); fp@1335: } catch (MasterDeviceException &e) { fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1335: throw e; fp@1335: } fp@1335: fp@1335: slaves = selectedSlaves(m); fp@1335: if (slaves.size() != 1) { fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1335: throwSingleSlaveRequired(slaves.size()); fp@1335: } fp@1335: data.slave_position = slaves.front().position; fp@1335: fp@1335: // write data via foe to the slave fp@1335: data.offset = 0; fp@1335: strncpy(data.file_name, storeFileName.c_str(), sizeof(data.file_name)); fp@1335: fp@1335: try { fp@1335: m.writeFoe(&data); fp@1335: } catch (MasterDeviceException &e) { fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1336: if (data.result) { fp@1336: if (data.result == FOE_OPCODE_ERROR) { fp@1336: err << "FoE write aborted with error code 0x" fp@1336: << setw(8) << setfill('0') << hex << data.error_code fp@1336: << ": " << errorText(data.error_code); fp@1336: } else { fp@1336: err << "Failed to write via FoE: " fp@1336: << resultText(data.result); fp@1336: } fp@1335: throwCommandException(err); fp@1335: } else { fp@1335: throw e; fp@1335: } fp@1335: } fp@1335: fp@1335: if (getVerbosity() == Verbose) { fp@1335: cerr << "FoE writing finished." << endl; fp@1335: } fp@1335: fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: void CommandFoeWrite::loadFoeData( fp@1335: ec_ioctl_slave_foe_t *data, fp@1335: const istream &in fp@1335: ) fp@1335: { fp@1335: stringstream err; fp@1335: ostringstream tmp; fp@1335: fp@1335: tmp << in.rdbuf(); fp@1335: string const &contents = tmp.str(); fp@1335: fp@1335: if (getVerbosity() == Verbose) { fp@1335: cerr << "Read " << contents.size() << " bytes of FoE data." << endl; fp@1335: } fp@1335: fp@1335: data->buffer_size = contents.size(); fp@1335: fp@1335: if (data->buffer_size) { fp@1335: // allocate buffer and read file into buffer fp@1335: data->buffer = new uint8_t[data->buffer_size]; fp@1335: contents.copy((char *) data->buffer, contents.size()); fp@1335: } fp@1335: } fp@1335: fp@1335: /*****************************************************************************/