fp@1200: /***************************************************************************** fp@1200: * fp@1200: * $Id$ fp@1200: * fp@1200: ****************************************************************************/ fp@1200: fp@1200: #include fp@1200: #include fp@1200: #include fp@1200: using namespace std; fp@1200: fp@1200: #include "CommandPhyWrite.h" fp@1200: #include "sii_crc.h" fp@1200: #include "byteorder.h" fp@1200: fp@1200: /*****************************************************************************/ fp@1200: fp@1200: CommandPhyWrite::CommandPhyWrite(): fp@1200: Command("phy_write", "Write data to a slave's physical memory.") fp@1200: { fp@1200: } fp@1200: fp@1200: /*****************************************************************************/ fp@1200: fp@1200: string CommandPhyWrite::helpString() const fp@1200: { fp@1200: stringstream str; fp@1200: fp@1200: str << getName() << " [OPTIONS] " << endl fp@1200: << endl fp@1200: << getBriefDescription() << endl fp@1200: << endl fp@1200: << "This command requires a single slave to be selected." << endl fp@1200: << endl fp@1200: << "Arguments:" << endl fp@1200: << " OFFSET must be the physical memory offset to start." << endl fp@1200: << " FILENAME must be a path to a file with data to write." << endl fp@1200: << " If it is '-', data are read from stdin." << endl fp@1200: << endl fp@1200: << "Command-specific options:" << endl fp@1200: << " --alias -a " << endl fp@1200: << " --position -p Slave selection. See the help of" << endl fp@1200: << " the 'slaves' command." << endl fp@1200: << endl fp@1200: << numericInfo(); fp@1200: fp@1200: return str.str(); fp@1200: } fp@1200: fp@1200: /****************************************************************************/ fp@1200: fp@1200: void CommandPhyWrite::execute(MasterDevice &m, const StringVector &args) fp@1200: { fp@1200: stringstream strOffset, err; fp@1200: ec_ioctl_slave_phy_t data; fp@1200: ifstream file; fp@1200: SlaveList slaves; fp@1200: fp@1200: if (args.size() != 2) { fp@1200: err << "'" << getName() << "' takes exactly one argument!"; fp@1200: throwInvalidUsageException(err); fp@1200: } fp@1200: fp@1200: strOffset << args[0]; fp@1200: strOffset fp@1200: >> resetiosflags(ios::basefield) // guess base from prefix fp@1200: >> data.offset; fp@1200: if (strOffset.fail()) { fp@1200: err << "Invalid offset '" << args[0] << "'!"; fp@1200: throwInvalidUsageException(err); fp@1200: } fp@1200: fp@1200: if (args[1] == "-") { fp@1200: loadPhyData(&data, cin); fp@1200: } else { fp@1200: file.open(args[1].c_str(), ifstream::in | ifstream::binary); fp@1200: if (file.fail()) { fp@1200: err << "Failed to open '" << args[0] << "'!"; fp@1200: throwCommandException(err); fp@1200: } fp@1200: loadPhyData(&data, file); fp@1200: file.close(); fp@1200: } fp@1200: fp@1200: if ((uint32_t) data.offset + data.length > 0xffff) { fp@1200: err << "Offset and length exceeding 64k!"; fp@1200: delete [] data.data; fp@1200: throwInvalidUsageException(err); fp@1200: } fp@1200: fp@1200: try { fp@1200: m.open(MasterDevice::ReadWrite); fp@1200: } catch (MasterDeviceException &e) { fp@1200: delete [] data.data; fp@1200: throw e; fp@1200: } fp@1200: fp@1200: slaves = selectedSlaves(m); fp@1200: if (slaves.size() != 1) { fp@1200: delete [] data.data; fp@1200: throwSingleSlaveRequired(slaves.size()); fp@1200: } fp@1200: data.slave_position = slaves.front().position; fp@1200: fp@1200: // send data to master fp@1200: try { fp@1200: m.writePhy(&data); fp@1200: } catch (MasterDeviceException &e) { fp@1200: delete [] data.data; fp@1200: throw e; fp@1200: } fp@1200: fp@1200: if (getVerbosity() == Verbose) { fp@1200: cerr << "Physical memory writing finished." << endl; fp@1200: } fp@1200: fp@1200: delete [] data.data; fp@1200: } fp@1200: fp@1200: /*****************************************************************************/ fp@1200: fp@1200: void CommandPhyWrite::loadPhyData( fp@1200: ec_ioctl_slave_phy_t *data, fp@1200: const istream &in fp@1200: ) fp@1200: { fp@1200: stringstream err; fp@1200: ostringstream tmp; fp@1200: fp@1200: tmp << in.rdbuf(); fp@1200: string const &contents = tmp.str(); fp@1200: fp@1200: if (getVerbosity() == Verbose) { fp@1200: cerr << "Read " << contents.size() << " bytes of data." << endl; fp@1200: } fp@1200: fp@1200: if (contents.size() > 0xffff) { fp@1200: err << "Invalid data size " << contents.size() << "!"; fp@1200: throwInvalidUsageException(err); fp@1200: } fp@1200: data->length = contents.size(); fp@1200: fp@1200: // allocate buffer and read file into buffer fp@1200: data->data = new uint8_t[data->length]; fp@1200: contents.copy((char *) data->data, contents.size()); fp@1200: } fp@1200: fp@1200: /*****************************************************************************/