fp@1388: /***************************************************************************** fp@1388: * fp@1388: * $Id$ fp@1388: * fp@1388: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1388: * fp@1388: * This file is part of the IgH EtherCAT Master. fp@1388: * fp@1388: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1388: * modify it under the terms of the GNU General Public License version 2, as fp@1388: * published by the Free Software Foundation. fp@1388: * fp@1388: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1388: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1388: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1388: * Public License for more details. fp@1388: * fp@1388: * You should have received a copy of the GNU General Public License along fp@1388: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1388: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1388: * fp@1388: * --- fp@1388: * fp@1388: * The license mentioned above concerns the source code only. Using the fp@1388: * EtherCAT technology and brand is only permitted in compliance with the fp@1388: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1388: * fp@1388: ****************************************************************************/ fp@1388: fp@1388: #include fp@1388: #include fp@1388: #include fp@1388: using namespace std; fp@1388: fp@1388: #include "CommandRegWrite.h" fp@1388: #include "sii_crc.h" fp@1388: fp@1388: /*****************************************************************************/ fp@1388: fp@1388: CommandRegWrite::CommandRegWrite(): fp@1389: CommandReg("reg_write", "Write data to a slave's registers.") fp@1388: { fp@1388: } fp@1388: fp@1388: /*****************************************************************************/ fp@1388: fp@1388: string CommandRegWrite::helpString() const fp@1388: { fp@1388: stringstream str; fp@1388: fp@1389: str << getName() << " [OPTIONS] " << endl fp@1388: << endl fp@1388: << getBriefDescription() << endl fp@1388: << endl fp@1388: << "This command requires a single slave to be selected." << endl fp@1388: << endl fp@1388: << "Arguments:" << endl fp@1389: << " OFFSET is the register address to write to." << endl fp@1389: << " DATA depends on whether a datatype was specified" << endl fp@1389: << " with the --type option: If not, DATA must be" << endl fp@1389: << " either a path to a file with data to write," << endl fp@1389: << " or '-', which means, that data are read from" << endl fp@1389: << " stdin. If a datatype was specified, VALUE is" << endl fp@1389: << " interpreted respective to the given type." << endl fp@1388: << endl fp@1389: << "These are the valid data types:" << endl fp@1389: << " int8, int16, int32, int64, uint8, uint16, uint32," << endl fp@1389: << " uint64, string." << endl fp@1389: << endl fp@1388: << "Command-specific options:" << endl fp@1388: << " --alias -a " << endl fp@1388: << " --position -p Slave selection. See the help of" << endl fp@1388: << " the 'slaves' command." << endl fp@1389: << " --type -t Data type (see above)." << endl fp@1388: << endl fp@1388: << numericInfo(); fp@1388: fp@1388: return str.str(); fp@1388: } fp@1388: fp@1388: /****************************************************************************/ fp@1388: fp@1388: void CommandRegWrite::execute(MasterDevice &m, const StringVector &args) fp@1388: { fp@1388: stringstream strOffset, err; fp@1388: ec_ioctl_slave_reg_t data; fp@1388: ifstream file; fp@1388: SlaveList slaves; fp@1388: fp@1388: if (args.size() != 2) { fp@1388: err << "'" << getName() << "' takes exactly two arguments!"; fp@1388: throwInvalidUsageException(err); fp@1388: } fp@1388: fp@1388: strOffset << args[0]; fp@1388: strOffset fp@1388: >> resetiosflags(ios::basefield) // guess base from prefix fp@1388: >> data.offset; fp@1388: if (strOffset.fail()) { fp@1388: err << "Invalid offset '" << args[0] << "'!"; fp@1388: throwInvalidUsageException(err); fp@1388: } fp@1388: fp@1389: if (getDataType().empty()) { fp@1389: if (args[1] == "-") { fp@1389: loadRegData(&data, cin); fp@1389: } else { fp@1389: file.open(args[1].c_str(), ifstream::in | ifstream::binary); fp@1389: if (file.fail()) { fp@1389: err << "Failed to open '" << args[1] << "'!"; fp@1389: throwCommandException(err); fp@1389: } fp@1389: loadRegData(&data, file); fp@1389: file.close(); fp@1389: } fp@1389: } else { fp@1389: stringstream strValue; fp@1389: const DataType *dataType = findDataType(getDataType()); fp@1389: fp@1389: if (!dataType) { fp@1389: err << "Invalid data type '" << getDataType() << "'!"; fp@1389: throwInvalidUsageException(err); fp@1388: } fp@1389: fp@1389: if (dataType->byteSize) { fp@1389: data.length = dataType->byteSize; fp@1389: data.data = new uint8_t[data.length]; fp@1389: } fp@1389: fp@1389: strValue << args[1]; fp@1389: strValue >> resetiosflags(ios::basefield); // guess base from prefix fp@1389: strValue.exceptions(ios::failbit); fp@1389: fp@1389: try { fp@1431: if (string(dataType->name) == "int8") { fp@1389: int16_t val; // uint8_t is interpreted as char fp@1389: strValue >> val; fp@1389: if (val > 127 || val < -128) fp@1389: throw ios::failure("Value out of range"); fp@1389: *data.data = (int8_t) val; fp@1431: } else if (string(dataType->name) == "int16") { fp@1389: int16_t val; fp@1389: strValue >> val; fp@1389: *(int16_t *) data.data = cpu_to_le16(val); fp@1431: } else if (string(dataType->name) == "int32") { fp@1389: int32_t val; fp@1389: strValue >> val; fp@1389: *(int32_t *) data.data = cpu_to_le32(val); fp@1431: } else if (string(dataType->name) == "int64") { fp@1395: int64_t val; fp@1395: strValue >> val; fp@1395: *(int64_t *) data.data = cpu_to_le64(val); fp@1431: } else if (string(dataType->name) == "uint8") { fp@1389: uint16_t val; // uint8_t is interpreted as char fp@1389: strValue >> val; fp@1389: if (val > 0xff) fp@1389: throw ios::failure("Value out of range"); fp@1389: *data.data = (uint8_t) val; fp@1431: } else if (string(dataType->name) == "uint16") { fp@1389: uint16_t val; fp@1389: strValue >> val; fp@1389: *(uint16_t *) data.data = cpu_to_le16(val); fp@1431: } else if (string(dataType->name) == "uint32") { fp@1389: uint32_t val; fp@1389: strValue >> val; fp@1389: *(uint32_t *) data.data = cpu_to_le32(val); fp@1431: } else if (string(dataType->name) == "uint64") { fp@1395: uint64_t val; fp@1395: strValue >> val; fp@1395: *(uint64_t *) data.data = cpu_to_le64(val); fp@1431: } else if (string(dataType->name) == "string" || fp@1431: string(dataType->name) == "octet_string") { fp@1389: data.length = strValue.str().size(); fp@1389: if (!data.length) { fp@1389: err << "Zero-size string now allowed!"; fp@1389: throwCommandException(err); fp@1389: } fp@1389: data.data = new uint8_t[data.length]; fp@1389: strValue >> (char *) data.data; fp@1389: } else { fp@1389: err << "Invalid data type " << dataType->name; fp@1389: throwCommandException(err); fp@1389: } fp@1389: } catch (ios::failure &e) { fp@1389: delete [] data.data; fp@1389: err << "Invalid value argument '" << args[1] fp@1389: << "' for type '" << dataType->name << "'!"; fp@1389: throwInvalidUsageException(err); fp@1389: } fp@1389: } fp@1388: fp@1388: if ((uint32_t) data.offset + data.length > 0xffff) { fp@1388: err << "Offset and length exceeding 64k!"; fp@1388: delete [] data.data; fp@1388: throwInvalidUsageException(err); fp@1388: } fp@1388: fp@1388: try { fp@1388: m.open(MasterDevice::ReadWrite); fp@1388: } catch (MasterDeviceException &e) { fp@1388: delete [] data.data; fp@1388: throw e; fp@1388: } fp@1388: fp@1388: slaves = selectedSlaves(m); fp@1388: if (slaves.size() != 1) { fp@1388: delete [] data.data; fp@1388: throwSingleSlaveRequired(slaves.size()); fp@1388: } fp@1388: data.slave_position = slaves.front().position; fp@1388: fp@1388: // send data to master fp@1388: try { fp@1388: m.writeReg(&data); fp@1388: } catch (MasterDeviceException &e) { fp@1388: delete [] data.data; fp@1388: throw e; fp@1388: } fp@1388: fp@1388: if (getVerbosity() == Verbose) { fp@1388: cerr << "Register writing finished." << endl; fp@1388: } fp@1388: fp@1388: delete [] data.data; fp@1388: } fp@1388: fp@1388: /*****************************************************************************/ fp@1388: fp@1388: void CommandRegWrite::loadRegData( fp@1388: ec_ioctl_slave_reg_t *data, fp@1388: const istream &in fp@1388: ) fp@1388: { fp@1388: stringstream err; fp@1388: ostringstream tmp; fp@1388: fp@1388: tmp << in.rdbuf(); fp@1388: string const &contents = tmp.str(); fp@1388: fp@1388: if (getVerbosity() == Verbose) { fp@1388: cerr << "Read " << contents.size() << " bytes of data." << endl; fp@1388: } fp@1388: fp@1388: if (contents.size() > 0xffff) { fp@1388: err << "Invalid data size " << contents.size() << "!"; fp@1388: throwInvalidUsageException(err); fp@1388: } fp@1388: data->length = contents.size(); fp@1388: fp@1388: // allocate buffer and read file into buffer fp@1388: data->data = new uint8_t[data->length]; fp@1388: contents.copy((char *) data->data, contents.size()); fp@1388: } fp@1388: fp@1388: /*****************************************************************************/