tool/CommandFoeRead.cpp
author Florian Pose <fp@igh-essen.com>
Tue, 12 Jan 2010 23:38:35 +0100
branch1.4-foe
changeset 1765 2b0269221410
parent 1708 fae3a1759126
permissions -rw-r--r--
Closed branch.
1707
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
1708
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
     3
 * $Id$
1707
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <iostream>
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#include <iomanip>
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
using namespace std;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
#include "CommandFoeRead.h"
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
#include "byteorder.h"
1708
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    13
#include "foe.h"
1707
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
/*****************************************************************************/
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
CommandFoeRead::CommandFoeRead():
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
    FoeCommand("foe_read", "Read a file from a slave via FoE.")
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
{
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
}
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
/*****************************************************************************/
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
string CommandFoeRead::helpString() const
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
{
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
    stringstream str;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
    str << getName() << " [OPTIONS] <SOURCEFILE>" << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
    	<< endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
    	<< getBriefDescription() << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
    	<< endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
        << "This command requires a single slave to be selected." << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
    	<< endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
        << "Arguments:" << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
        << "  SOURCEFILE is the name of the source file on the slave." << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
        << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
    	<< "Command-specific options:" << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
        << "  --output-file -o <file>   Local target filename. If" << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
        << "                            '-' (default), data are" << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
        << "                            printed to stdout." << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
        << "  --alias       -a <alias>  " << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
        << "  --position    -p <pos>    Slave selection. See the help" << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
        << "                            of the 'slaves' command." << endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
    	<< endl
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
		<< numericInfo();
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
	return str.str();
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
}
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
/****************************************************************************/
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
void CommandFoeRead::execute(MasterDevice &m, const StringVector &args)
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
{
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
    SlaveList slaves;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
    ec_ioctl_slave_t *slave;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    ec_ioctl_slave_foe_t data;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
    unsigned int i;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    stringstream err;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
    if (args.size() != 1) {
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
        err << "'" << getName() << "' takes exactly one argument!";
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
        throwInvalidUsageException(err);
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    }
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
    m.open(MasterDevice::Read);
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
    slaves = selectedSlaves(m);
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
    if (slaves.size() != 1) {
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
        throwSingleSlaveRequired(slaves.size());
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
    }
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
    slave = &slaves.front();
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
    data.slave_position = slave->position;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
    /* FIXME: No good idea to have a fixed buffer size.
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
     * Read in chunks and fill a buffer instead.
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
     */
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
    data.offset = 0;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
    data.buffer_size = 0x8800;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
    data.buffer = new uint8_t[data.buffer_size];
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    strncpy(data.file_name, args[0].c_str(), sizeof(data.file_name));
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
	try {
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
		m.readFoe(&data);
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
	} catch (MasterDeviceException &e) {
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
        delete [] data.buffer;
1708
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    87
        if (data.result) {
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    88
            if (data.result == FOE_OPCODE_ERROR) {
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    89
                err << "FoE read aborted with error code 0x"
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    90
                    << setw(8) << setfill('0') << hex << data.error_code
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    91
                    << ": " << errorText(data.error_code);
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    92
            } else {
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    93
                err << "Failed to write via FoE: "
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    94
                    << resultText(data.result);
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    95
            }
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    96
            throwCommandException(err);
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    97
        } else {
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    98
            throw e;
fae3a1759126 Improved FoE.
Florian Pose <fp@igh-essen.com>
parents: 1707
diff changeset
    99
        }
1707
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
	}
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    // TODO --output-file
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
	for (i = 0; i < data.data_size; i++) {
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
		uint8_t *w = data.buffer + i;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
		cout << *(uint8_t *) w ;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
	}
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
    delete [] data.buffer;
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
}
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
11ec009e145d Included FoE patch from Olav Zarges.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
/*****************************************************************************/