tool/CommandPhyRead.cpp
author Florian Pose <fp@igh-essen.com>
Tue, 25 Nov 2008 16:23:46 +0000
changeset 1319 38da03647aee
parent 1200 ce1a65f06efc
child 1363 11c0b2caa253
permissions -rw-r--r--
Updated features.
1200
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <iostream>
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#include <iomanip>
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
using namespace std;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
#include "CommandPhyRead.h"
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
/*****************************************************************************/
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
CommandPhyRead::CommandPhyRead():
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
    Command("phy_read", "Output a slave's physical memory contents.")
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
{
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
}
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
/*****************************************************************************/
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
string CommandPhyRead::helpString() const
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
{
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
    stringstream str;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
    str << getName() << " [OPTIONS] <OFFSET> <LENGTH>" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
    	<< endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
    	<< getBriefDescription() << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
    	<< endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
        << "This command requires a single slave to be selected." << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
    	<< endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
        << "Arguments:" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
        << "  OFFSET is the physical memory address. Must" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
        << "         be an unsigned 16 bit number." << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
        << "  LENGTH is the number of bytes to read and must also be" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
        << "         an unsigned 16 bit number. OFFSET plus LENGTH" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
        << "         may not exceed 64k." << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
        << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
    	<< "Command-specific options:" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
        << "  --alias    -a <alias>" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
        << "  --position -p <pos>    Slave selection. See the help of" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
        << "                         the 'slaves' command." << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
    	<< endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
		<< numericInfo();
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
	return str.str();
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
}
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
/****************************************************************************/
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
void CommandPhyRead::execute(MasterDevice &m, const StringVector &args)
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
{
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
    SlaveList slaves;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
    ec_ioctl_slave_phy_t data;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
    stringstream strOffset, strLength, err;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    uint16_t i;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    if (args.size() != 2) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
        err << "'" << getName() << "' takes two arguments!";
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        throwInvalidUsageException(err);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    strOffset << args[0];
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
    strOffset
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
        >> resetiosflags(ios::basefield) // guess base from prefix
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
        >> data.offset;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
    if (strOffset.fail()) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
        err << "Invalid offset '" << args[0] << "'!";
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
        throwInvalidUsageException(err);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
    strLength << args[1];
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
    strLength
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
        >> resetiosflags(ios::basefield) // guess base from prefix
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
        >> data.length;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
    if (strLength.fail()) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
        err << "Invalid length '" << args[1] << "'!";
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
        throwInvalidUsageException(err);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    if (!data.length) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
        return;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
    if ((uint32_t) data.offset + data.length > 0xffff) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
        err << "Offset and length exceeding 64k!";
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
        throwInvalidUsageException(err);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
    
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
    m.open(MasterDevice::Read);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
    slaves = selectedSlaves(m);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    if (slaves.size() != 1) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
        throwSingleSlaveRequired(slaves.size());
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    data.slave_position = slaves.front().position;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    data.data = new uint8_t[data.length];
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
	try {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
		m.readPhy(&data);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
	} catch (MasterDeviceException &e) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
        delete [] data.data;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
		throw e;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
	}
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    for (i = 0; i < data.length; i++) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
        cout << data.data[i];
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    delete [] data.data;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
}
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
/*****************************************************************************/