tool/CommandFoeRead.cpp
branch1.4-foe
changeset 1707 11ec009e145d
child 1708 fae3a1759126
equal deleted inserted replaced
1706:c55ebaa206f8 1707:11ec009e145d
       
     1 /*****************************************************************************
       
     2  *
       
     3  * $Id:$
       
     4  *
       
     5  ****************************************************************************/
       
     6 
       
     7 #include <iostream>
       
     8 #include <iomanip>
       
     9 using namespace std;
       
    10 
       
    11 #include "CommandFoeRead.h"
       
    12 #include "byteorder.h"
       
    13 
       
    14 /*****************************************************************************/
       
    15 
       
    16 CommandFoeRead::CommandFoeRead():
       
    17     FoeCommand("foe_read", "Read a file from a slave via FoE.")
       
    18 {
       
    19 }
       
    20 
       
    21 /*****************************************************************************/
       
    22 
       
    23 string CommandFoeRead::helpString() const
       
    24 {
       
    25     stringstream str;
       
    26 
       
    27     str << getName() << " [OPTIONS] <SOURCEFILE>" << endl
       
    28     	<< endl
       
    29     	<< getBriefDescription() << endl
       
    30     	<< endl
       
    31         << "This command requires a single slave to be selected." << endl
       
    32     	<< endl
       
    33         << "Arguments:" << endl
       
    34         << "  SOURCEFILE is the name of the source file on the slave." << endl
       
    35         << endl
       
    36     	<< "Command-specific options:" << endl
       
    37         << "  --output-file -o <file>   Local target filename. If" << endl
       
    38         << "                            '-' (default), data are" << endl
       
    39         << "                            printed to stdout." << endl
       
    40         << "  --alias       -a <alias>  " << endl
       
    41         << "  --position    -p <pos>    Slave selection. See the help" << endl
       
    42         << "                            of the 'slaves' command." << endl
       
    43     	<< endl
       
    44 		<< numericInfo();
       
    45 
       
    46 	return str.str();
       
    47 }
       
    48 
       
    49 /****************************************************************************/
       
    50 
       
    51 void CommandFoeRead::execute(MasterDevice &m, const StringVector &args)
       
    52 {
       
    53     SlaveList slaves;
       
    54     ec_ioctl_slave_t *slave;
       
    55     ec_ioctl_slave_foe_t data;
       
    56     unsigned int i;
       
    57     stringstream err;
       
    58 
       
    59     if (args.size() != 1) {
       
    60         err << "'" << getName() << "' takes exactly one argument!";
       
    61         throwInvalidUsageException(err);
       
    62     }
       
    63 
       
    64     m.open(MasterDevice::Read);
       
    65     slaves = selectedSlaves(m);
       
    66 
       
    67     if (slaves.size() != 1) {
       
    68         throwSingleSlaveRequired(slaves.size());
       
    69     }
       
    70     slave = &slaves.front();
       
    71     data.slave_position = slave->position;
       
    72 
       
    73     /* FIXME: No good idea to have a fixed buffer size.
       
    74      * Read in chunks and fill a buffer instead.
       
    75      */
       
    76     data.offset = 0;
       
    77     data.buffer_size = 0x8800;
       
    78     data.buffer = new uint8_t[data.buffer_size];
       
    79 
       
    80     strncpy(data.file_name, args[0].c_str(), sizeof(data.file_name));
       
    81 
       
    82 	try {
       
    83 		m.readFoe(&data);
       
    84 	} catch (MasterDeviceException &e) {
       
    85         delete [] data.buffer;
       
    86 		throw e;
       
    87 	}
       
    88 
       
    89     // TODO --output-file
       
    90 	for (i = 0; i < data.data_size; i++) {
       
    91 		uint8_t *w = data.buffer + i;
       
    92 		cout << *(uint8_t *) w ;
       
    93 	}
       
    94 
       
    95     delete [] data.buffer;
       
    96 }
       
    97 
       
    98 /*****************************************************************************/