fp@1707: /***************************************************************************** fp@1707: * fp@1708: * $Id$ fp@1707: * fp@1707: ****************************************************************************/ fp@1707: fp@1707: #include fp@1707: #include fp@1707: using namespace std; fp@1707: fp@1707: #include "CommandFoeRead.h" fp@1707: #include "byteorder.h" fp@1708: #include "foe.h" fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: CommandFoeRead::CommandFoeRead(): fp@1707: FoeCommand("foe_read", "Read a file from a slave via FoE.") fp@1707: { fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: string CommandFoeRead::helpString() const fp@1707: { fp@1707: stringstream str; fp@1707: fp@1707: str << getName() << " [OPTIONS] " << endl fp@1707: << endl fp@1707: << getBriefDescription() << endl fp@1707: << endl fp@1707: << "This command requires a single slave to be selected." << endl fp@1707: << endl fp@1707: << "Arguments:" << endl fp@1707: << " SOURCEFILE is the name of the source file on the slave." << endl fp@1707: << endl fp@1707: << "Command-specific options:" << endl fp@1707: << " --output-file -o Local target filename. If" << endl fp@1707: << " '-' (default), data are" << endl fp@1707: << " printed to stdout." << endl fp@1707: << " --alias -a " << endl fp@1707: << " --position -p Slave selection. See the help" << endl fp@1707: << " of the 'slaves' command." << endl fp@1707: << endl fp@1707: << numericInfo(); fp@1707: fp@1707: return str.str(); fp@1707: } fp@1707: fp@1707: /****************************************************************************/ fp@1707: fp@1707: void CommandFoeRead::execute(MasterDevice &m, const StringVector &args) fp@1707: { fp@1707: SlaveList slaves; fp@1707: ec_ioctl_slave_t *slave; fp@1707: ec_ioctl_slave_foe_t data; fp@1707: unsigned int i; fp@1707: stringstream err; fp@1707: fp@1707: if (args.size() != 1) { fp@1707: err << "'" << getName() << "' takes exactly one argument!"; fp@1707: throwInvalidUsageException(err); fp@1707: } fp@1707: fp@1707: m.open(MasterDevice::Read); fp@1707: slaves = selectedSlaves(m); fp@1707: fp@1707: if (slaves.size() != 1) { fp@1707: throwSingleSlaveRequired(slaves.size()); fp@1707: } fp@1707: slave = &slaves.front(); fp@1707: data.slave_position = slave->position; fp@1707: fp@1707: /* FIXME: No good idea to have a fixed buffer size. fp@1707: * Read in chunks and fill a buffer instead. fp@1707: */ fp@1707: data.offset = 0; fp@1707: data.buffer_size = 0x8800; fp@1707: data.buffer = new uint8_t[data.buffer_size]; fp@1707: fp@1707: strncpy(data.file_name, args[0].c_str(), sizeof(data.file_name)); fp@1707: fp@1707: try { fp@1707: m.readFoe(&data); fp@1707: } catch (MasterDeviceException &e) { fp@1707: delete [] data.buffer; fp@1708: if (data.result) { fp@1708: if (data.result == FOE_OPCODE_ERROR) { fp@1708: err << "FoE read aborted with error code 0x" fp@1708: << setw(8) << setfill('0') << hex << data.error_code fp@1708: << ": " << errorText(data.error_code); fp@1708: } else { fp@1708: err << "Failed to write via FoE: " fp@1708: << resultText(data.result); fp@1708: } fp@1708: throwCommandException(err); fp@1708: } else { fp@1708: throw e; fp@1708: } fp@1707: } fp@1707: fp@1707: // TODO --output-file fp@1707: for (i = 0; i < data.data_size; i++) { fp@1707: uint8_t *w = data.buffer + i; fp@1707: cout << *(uint8_t *) w ; fp@1707: } fp@1707: fp@1707: delete [] data.buffer; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/