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@1357: #include fp@1357: fp@1335: #include fp@1335: #include fp@1335: using namespace std; fp@1335: fp@1335: #include "CommandFoeRead.h" fp@1336: #include "foe.h" fp@1826: #include "MasterDevice.h" fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: CommandFoeRead::CommandFoeRead(): fp@1335: FoeCommand("foe_read", "Read a file from a slave via FoE.") fp@1335: { fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1968: string CommandFoeRead::helpString(const string &binaryBaseName) const fp@1335: { fp@1335: stringstream str; fp@1335: fp@1968: str << binaryBaseName << " " << getName() fp@1968: << " [OPTIONS] " << endl fp@1804: << endl fp@1804: << getBriefDescription() << endl fp@1804: << endl fp@1335: << "This command requires a single slave to be selected." << endl fp@1804: << endl fp@1335: << "Arguments:" << endl fp@1335: << " SOURCEFILE is the name of the source file on the slave." << endl fp@1335: << endl fp@1804: << "Command-specific options:" << endl fp@1335: << " --output-file -o Local target filename. If" << endl fp@1335: << " '-' (default), data are" << endl fp@1335: << " printed to stdout." << endl fp@1335: << " --alias -a " << endl fp@1335: << " --position -p Slave selection. See the help" << endl fp@1335: << " of the 'slaves' command." << endl fp@1804: << endl fp@1804: << numericInfo(); fp@1335: fp@1804: return str.str(); fp@1335: } fp@1335: fp@1335: /****************************************************************************/ fp@1335: fp@1826: void CommandFoeRead::execute(const StringVector &args) fp@1335: { fp@1335: SlaveList slaves; fp@1335: ec_ioctl_slave_t *slave; fp@1335: ec_ioctl_slave_foe_t data; fp@1335: unsigned int i; fp@1335: stringstream err; 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@1870: MasterDevice m(getSingleMasterIndex()); fp@1335: m.open(MasterDevice::Read); fp@1335: slaves = selectedSlaves(m); fp@1335: fp@1335: if (slaves.size() != 1) { fp@1335: throwSingleSlaveRequired(slaves.size()); fp@1335: } fp@1335: slave = &slaves.front(); fp@1335: data.slave_position = slave->position; fp@1335: fp@1335: /* FIXME: No good idea to have a fixed buffer size. fp@1335: * Read in chunks and fill a buffer instead. fp@1335: */ fp@1335: data.offset = 0; fp@1335: data.buffer_size = 0x8800; fp@1335: data.buffer = new uint8_t[data.buffer_size]; fp@1335: fp@1335: strncpy(data.file_name, args[0].c_str(), sizeof(data.file_name)); fp@1335: fp@1804: try { fp@1804: m.readFoe(&data); fp@1804: } catch (MasterDeviceException &e) { fp@1335: delete [] data.buffer; fp@1336: if (data.result) { fp@1336: if (data.result == FOE_OPCODE_ERROR) { fp@1336: err << "FoE read 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@1336: throwCommandException(err); fp@1336: } else { fp@1336: throw e; fp@1336: } fp@1804: } fp@1335: fp@1335: // TODO --output-file fp@1804: for (i = 0; i < data.data_size; i++) { fp@1804: uint8_t *w = data.buffer + i; fp@1804: cout << *(uint8_t *) w ; fp@1804: } fp@1335: fp@1335: delete [] data.buffer; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/