tool/CommandSiiWrite.cpp
author Florian Pose <fp@igh-essen.com>
Thu, 15 Dec 2011 14:05:44 +0100
branchstable-1.5
changeset 2202 14728d0c4a66
parent 1968 4f682084c643
child 2421 bc2d4bf9cbe5
permissions -rw-r--r--
Added e1000 driver for 2.6.37.
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
1363
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
     3
 *  $Id$
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
     4
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
     5
 *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
     6
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
     8
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    11
 *  published by the Free Software Foundation.
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    12
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    16
 *  Public License for more details.
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    17
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    21
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    22
 *  ---
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    23
 *
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    24
 *  The license mentioned above concerns the source code only. Using the
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
11c0b2caa253 Improved and added license headers.
Florian Pose <fp@igh-essen.com>
parents: 1254
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 ****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#include <iostream>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#include <iomanip>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include <fstream>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
using namespace std;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include "CommandSiiWrite.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include "sii_crc.h"
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1804
diff changeset
    37
#include "MasterDevice.h"
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
/*****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
CommandSiiWrite::CommandSiiWrite():
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
    Command("sii_write", "Write SII contents to a slave.")
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
/*****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
1968
4f682084c643 Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents: 1870
diff changeset
    48
string CommandSiiWrite::helpString(const string &binaryBaseName) const
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
    stringstream str;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
1968
4f682084c643 Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents: 1870
diff changeset
    52
    str << binaryBaseName << " " << getName()
4f682084c643 Implemented drive_no for command-line tool; binary base name is now a
Florian Pose <fp@igh-essen.com>
parents: 1870
diff changeset
    53
        << " [OPTIONS] <FILENAME>" << endl
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        << endl 
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
        << getBriefDescription() << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
        << endl
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    57
        << "This command requires a single slave to be selected." << endl
1804
742607c464c4 Removed tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1363
diff changeset
    58
        << endl
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
        << "The file contents are checked for validity and integrity." << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        << "These checks can be overridden with the --force option." << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
        << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
        << "Arguments:" << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
        << "  FILENAME must be a path to a file that contains a" << endl
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    64
        << "           positive number of words. If it is '-'," << endl
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    65
        << "           data are read from stdin." << endl
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
        << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
        << "Command-specific options:" << endl
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    68
        << "  --alias    -a <alias>" << endl
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    69
        << "  --position -p <pos>    Slave selection. See the help of" << endl
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    70
        << "                         the 'slaves' command." << endl
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    71
        << "  --force    -f          Override validity checks." << endl
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
        << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
        << numericInfo();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
    return str.str();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
1826
ec6223c3b7ec Multi-master support for command-line tool. The --master option supports
Florian Pose <fp@igh-essen.com>
parents: 1804
diff changeset
    80
void CommandSiiWrite::execute(const StringVector &args)
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
    stringstream err;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
    ec_ioctl_slave_sii_t data;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    ifstream file;
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    85
    SlaveList slaves;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
    if (args.size() != 1) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
        err << "'" << getName() << "' takes exactly one argument!";
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
        throwInvalidUsageException(err);
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
    }
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    92
    if (args[0] == "-") {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    93
        loadSiiData(&data, cin);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    94
    } else {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    95
        file.open(args[0].c_str(), ifstream::in | ifstream::binary);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    96
        if (file.fail()) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    97
            err << "Failed to open '" << args[0] << "'!";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    98
            throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    99
        }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   100
        loadSiiData(&data, file);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   101
        file.close();
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    }
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
1166
006244d53f68 Removed global variables.
Florian Pose <fp@igh-essen.com>
parents: 1161
diff changeset
   104
    if (!getForce()) {
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   105
        try {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   106
            checkSiiData(&data);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   107
        } catch (CommandException &e) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   108
            delete [] data.words;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   109
            throw e;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
        }
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    }
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
1870
0cee1fb7f0fd Added getSingleMasterIndex() for command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1869
diff changeset
   113
    MasterDevice m(getSingleMasterIndex());
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   114
    try {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   115
        m.open(MasterDevice::ReadWrite);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   116
    } catch (MasterDeviceException &e) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   117
        delete [] data.words;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   118
        throw e;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   119
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   120
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   121
    slaves = selectedSlaves(m);
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   122
    if (slaves.size() != 1) {
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   123
        delete [] data.words;
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   124
        throwSingleSlaveRequired(slaves.size());
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   125
    }
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   126
    data.slave_position = slaves.front().position;
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   127
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
    // send data to master
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
    data.offset = 0;
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   130
    try {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   131
        m.writeSii(&data);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   132
    } catch (MasterDeviceException &e) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   133
        delete [] data.words;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   134
        throw e;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   135
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   136
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   137
    if (getVerbosity() == Verbose) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   138
        cerr << "SII writing finished." << endl;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   139
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   140
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   141
    delete [] data.words;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
/*****************************************************************************/
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   145
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   146
void CommandSiiWrite::loadSiiData(
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   147
        ec_ioctl_slave_sii_t *data,
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   148
        const istream &in
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   149
        )
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   150
{
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   151
    stringstream err;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   152
    ostringstream tmp;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   153
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   154
    tmp << in.rdbuf();
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   155
    string const &contents = tmp.str();
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   156
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   157
    if (getVerbosity() == Verbose) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   158
        cerr << "Read " << contents.size() << " bytes of SII data." << endl;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   159
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   160
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   161
    if (!contents.size() || contents.size() % 2) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   162
        err << "Invalid data size " << contents.size() << "! "
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   163
            << "Must be non-zero and even.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   164
        throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   165
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   166
    data->nwords = contents.size() / 2;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   167
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   168
    // allocate buffer and read file into buffer
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   169
    data->words = new uint16_t[data->nwords];
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   170
    contents.copy((char *) data->words, contents.size());
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   171
}
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   172
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   173
/*****************************************************************************/
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   174
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   175
void CommandSiiWrite::checkSiiData(
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   176
        const ec_ioctl_slave_sii_t *data
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   177
        )
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   178
{
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   179
    stringstream err;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   180
    const uint16_t *categoryHeader;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   181
    uint16_t categoryType, categorySize;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   182
    uint8_t crc;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   183
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   184
    if (data->nwords < 0x0041) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   185
        err << "SII data too short (" << data->nwords << " words)! Mimimum is"
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   186
            " 40 fixed words + 1 delimiter. Use --force to write anyway.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   187
        throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   188
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   189
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   190
    // calculate checksum over words 0 to 6
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   191
    crc = calcSiiCrc((const uint8_t *) data->words, 14);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   192
    if (crc != ((const uint8_t *) data->words)[14]) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   193
        err << "CRC incorrect. Must be 0x"
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   194
            << hex << setfill('0') << setw(2) << (unsigned int) crc
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   195
            << ". Use --force to write anyway.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   196
        throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   197
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   198
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   199
    // cycle through categories to detect corruption
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   200
    categoryHeader = data->words + 0x0040U;
1254
c19d273a9e76 Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents: 1166
diff changeset
   201
    categoryType = le16_to_cpup(categoryHeader);
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   202
    while (categoryType != 0xffff) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   203
        if (categoryHeader + 1 > data->words + data->nwords) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   204
            err << "SII data seem to be corrupted! "
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   205
                << "Use --force to write anyway.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   206
            throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   207
        }
1254
c19d273a9e76 Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents: 1166
diff changeset
   208
        categorySize = le16_to_cpup(categoryHeader + 1);
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   209
        if (categoryHeader + 2 + categorySize + 1
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   210
                > data->words + data->nwords) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   211
            err << "SII data seem to be corrupted! "
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   212
                "Use --force to write anyway.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   213
            throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   214
        }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   215
        categoryHeader += 2 + categorySize;
1254
c19d273a9e76 Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents: 1166
diff changeset
   216
        categoryType = le16_to_cpup(categoryHeader);
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   217
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   218
}
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   219
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   220
/*****************************************************************************/