tool/CommandPhyWrite.cpp
author Florian Pose <fp@igh-essen.com>
Fri, 10 Oct 2008 08:34:15 +0000
changeset 1258 900f1124e8f8
parent 1254 c19d273a9e76
child 1363 11c0b2caa253
permissions -rw-r--r--
Memory-mapped process data.
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
#include <fstream>
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
using namespace std;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
#include "CommandPhyWrite.h"
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
#include "sii_crc.h"
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
/*****************************************************************************/
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
CommandPhyWrite::CommandPhyWrite():
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
    Command("phy_write", "Write data to a slave's physical memory.")
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
/*****************************************************************************/
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
string CommandPhyWrite::helpString() const
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
    stringstream str;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
    str << getName() << " [OPTIONS] <OFFSET> <FILENAME>" << 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
        << getBriefDescription() << 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
        << "This command requires a single slave to be selected." << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
    	<< endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
        << "Arguments:" << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
        << "  OFFSET   must be the physical memory offset to start." << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
        << "  FILENAME must be a path to a file with data to write." << endl
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
        << "           If it is '-', data are read from stdin." << 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 CommandPhyWrite::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
    stringstream strOffset, err;
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
    ifstream file;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    SlaveList slaves;
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 exactly one argument!";
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
    if (args[1] == "-") {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
        loadPhyData(&data, cin);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
    } else {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
        file.open(args[1].c_str(), ifstream::in | ifstream::binary);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
        if (file.fail()) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
            err << "Failed to open '" << args[0] << "'!";
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
            throwCommandException(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
        loadPhyData(&data, file);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
        file.close();
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
    }
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
    if ((uint32_t) data.offset + data.length > 0xffff) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
        err << "Offset and length exceeding 64k!";
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
        delete [] data.data;
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
    try {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
        m.open(MasterDevice::ReadWrite);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
    } catch (MasterDeviceException &e) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
        delete [] data.data;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
        throw e;
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
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
    slaves = selectedSlaves(m);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    if (slaves.size() != 1) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
        delete [] data.data;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
        throwSingleSlaveRequired(slaves.size());
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    data.slave_position = slaves.front().position;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
    // send data to master
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
    try {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
        m.writePhy(&data);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    } catch (MasterDeviceException &e) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
        delete [] data.data;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
        throw e;
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
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    if (getVerbosity() == Verbose) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
        cerr << "Physical memory writing finished." << endl;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
    delete [] data.data;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
}
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
/*****************************************************************************/
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
void CommandPhyWrite::loadPhyData(
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
        ec_ioctl_slave_phy_t *data,
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
        const istream &in
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
        )
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
{
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
    stringstream err;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    ostringstream tmp;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
    tmp << in.rdbuf();
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
    string const &contents = tmp.str();
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
    if (getVerbosity() == Verbose) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
        cerr << "Read " << contents.size() << " bytes of data." << endl;
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
    if (contents.size() > 0xffff) {
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
        err << "Invalid data size " << contents.size() << "!";
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
        throwInvalidUsageException(err);
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
    }
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
    data->length = contents.size();
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
    // allocate buffer and read file into buffer
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
    data->data = new uint8_t[data->length];
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
    contents.copy((char *) data->data, contents.size());
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
}
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
ce1a65f06efc Added phy commands.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
/*****************************************************************************/