tool/CommandSiiWrite.cpp
author Florian Pose <fp@igh-essen.com>
Fri, 19 Jun 2009 11:46:53 +0000
branchstable-1.4
changeset 1699 89ba41136b21
parent 1166 006244d53f68
child 1254 c19d273a9e76
permissions -rw-r--r--
Ignores for recent kernel versions.
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
 *
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <iostream>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#include <iomanip>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
#include <fstream>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
using namespace std;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
#include "CommandSiiWrite.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
#include "sii_crc.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
#include "byteorder.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
/*****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
CommandSiiWrite::CommandSiiWrite():
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
    Command("sii_write", "Write SII contents to a slave.")
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
/*****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
string CommandSiiWrite::helpString() const
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
    stringstream str;
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
    str << getName() << " [OPTIONS] <FILENAME>" << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
        << endl 
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
        << getBriefDescription() << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
        << endl
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    33
        << "This command requires a single slave to be selected." << endl
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    34
    	<< endl
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
        << "The file contents are checked for validity and integrity." << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
        << "These checks can be overridden with the --force option." << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
        << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
        << "Arguments:" << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
        << "  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
    40
        << "           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
    41
        << "           data are read from stdin." << endl
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
        << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
        << "Command-specific options:" << endl
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    44
        << "  --alias    -a <alias>" << endl
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    45
        << "  --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
    46
        << "                         the 'slaves' command." << endl
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    47
        << "  --force    -f          Override validity checks." << endl
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
        << endl
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
        << numericInfo();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
    return str.str();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
void CommandSiiWrite::execute(MasterDevice &m, const StringVector &args)
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    stringstream err;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
    ec_ioctl_slave_sii_t data;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
    ifstream file;
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    61
    SlaveList slaves;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    if (args.size() != 1) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
        err << "'" << getName() << "' takes exactly one argument!";
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
        throwInvalidUsageException(err);
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
    }
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    68
    if (args[0] == "-") {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    69
        loadSiiData(&data, cin);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    70
    } else {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    71
        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
    72
        if (file.fail()) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    73
            err << "Failed to open '" << args[0] << "'!";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    74
            throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    75
        }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    76
        loadSiiData(&data, file);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    77
        file.close();
1142
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
1166
006244d53f68 Removed global variables.
Florian Pose <fp@igh-essen.com>
parents: 1161
diff changeset
    80
    if (!getForce()) {
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    81
        try {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    82
            checkSiiData(&data);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    83
        } catch (CommandException &e) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    84
            delete [] data.words;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    85
            throw e;
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
    }
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    89
    try {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    90
        m.open(MasterDevice::ReadWrite);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    91
    } catch (MasterDeviceException &e) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    92
        delete [] data.words;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    93
        throw e;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    94
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    95
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    96
    slaves = selectedSlaves(m);
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    97
    if (slaves.size() != 1) {
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
    98
        delete [] data.words;
1157
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    99
        throwSingleSlaveRequired(slaves.size());
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   100
    }
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   101
    data.slave_position = slaves.front().position;
04d1c950cf9d Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
   102
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
    // send data to master
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
    data.offset = 0;
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
        m.writeSii(&data);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   107
    } catch (MasterDeviceException &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;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   110
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   111
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   112
    if (getVerbosity() == Verbose) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   113
        cerr << "SII writing finished." << endl;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   114
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   115
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   116
    delete [] data.words;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
/*****************************************************************************/
1161
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   120
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   121
void CommandSiiWrite::loadSiiData(
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   122
        ec_ioctl_slave_sii_t *data,
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   123
        const istream &in
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   124
        )
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   125
{
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   126
    stringstream err;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   127
    ostringstream tmp;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   128
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   129
    tmp << in.rdbuf();
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   130
    string const &contents = tmp.str();
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   131
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   132
    if (getVerbosity() == Verbose) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   133
        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
   134
    }
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
    if (!contents.size() || contents.size() % 2) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   137
        err << "Invalid data size " << contents.size() << "! "
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   138
            << "Must be non-zero and even.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   139
        throwCommandException(err);
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
    data->nwords = contents.size() / 2;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   142
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   143
    // 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
   144
    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
   145
    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
   146
}
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   147
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   148
/*****************************************************************************/
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
void CommandSiiWrite::checkSiiData(
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   151
        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
   152
        )
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
    stringstream err;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   155
    const uint16_t *categoryHeader;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   156
    uint16_t categoryType, categorySize;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   157
    uint8_t crc;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   158
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   159
    if (data->nwords < 0x0041) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   160
        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
   161
            " 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
   162
        throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   163
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   164
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   165
    // 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
   166
    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
   167
    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
   168
        err << "CRC incorrect. Must be 0x"
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   169
            << 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
   170
            << ". Use --force to write anyway.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   171
        throwCommandException(err);
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
    // cycle through categories to detect corruption
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   175
    categoryHeader = data->words + 0x0040U;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   176
    categoryType = le16tocpu(*categoryHeader);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   177
    while (categoryType != 0xffff) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   178
        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
   179
            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
   180
                << "Use --force to write anyway.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   181
            throwCommandException(err);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   182
        }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   183
        categorySize = le16tocpu(*(categoryHeader + 1));
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   184
        if (categoryHeader + 2 + categorySize + 1
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   185
                > data->words + data->nwords) {
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   186
            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
   187
                "Use --force to write anyway.";
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   188
            throwCommandException(err);
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
        categoryHeader += 2 + categorySize;
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   191
        categoryType = le16tocpu(*categoryHeader);
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   192
    }
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   193
}
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   194
d1324ac71232 Accept '-' as filename to read from stdin.
Florian Pose <fp@igh-essen.com>
parents: 1157
diff changeset
   195
/*****************************************************************************/