tool/cmd_alias.cpp
author Florian Pose <fp@igh-essen.com>
Wed, 23 Jul 2008 08:06:10 +0000
changeset 1136 a0982873d655
parent 1126 b09fd81894cb
child 1137 a73c0f54be42
permissions -rw-r--r--
Improved exceptions and commandName usage.
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <iostream>
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#include <iomanip>
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
#include <sstream>
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
using namespace std;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
#include "globals.h"
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    13
#include "sii_crc.h"
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
/*****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
const char *help_alias =
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
    "[OPTIONS] <ALIAS>\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
    "\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
    "Write the secondary slave address (alias) for either\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
    "one or for multiple slaves.\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
    "\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
    "Arguments:\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
    "  ALIAS must be a 16 bit unsigned integer, specified\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
    "        either in decimal (no prefix), octal (prefix '0')\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
    "        or hexadecimal (prefix '0x').\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
    "\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
    "Command-specific options:\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
    "  -s <SLAVE>  Write the alias of the slave with the given\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
    "              ring position. If this option is not\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
    "              specified, the alias of all slaves is set.\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
    "              The --force option is required in this\n"
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
    "              case.\n";
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
/*****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
void writeSlaveAlias(uint16_t, uint16_t);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
/*****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
/** Writes the Secondary slave address (alias) to the slave's SII.
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
 */
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
void command_alias(void)
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
{
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
    uint16_t alias;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
    stringstream err, strAlias;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
    int number;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
    unsigned int numSlaves, i;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
    if (commandArgs.size() != 1) {
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
    51
        err << "'" << commandName << "' takes exactly one argument!";
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
        throw InvalidUsageException(err);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
    strAlias << commandArgs[0];
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    strAlias
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
        >> resetiosflags(ios::basefield) // guess base from prefix
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
        >> number;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
    if (strAlias.fail() || number < 0x0000 || number > 0xffff) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        err << "Invalid alias '" << commandArgs[0] << "'!";
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
        throw InvalidUsageException(err);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    alias = number;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
    if (slavePosition == -1) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
        if (!force) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
            err << "This will write the alias addresses of all slaves to "
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
                << alias << "! Please specify --force to proceed.";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
    69
            throw CommandException(err);
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
        }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
        masterDev.open(MasterDevice::ReadWrite);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
        numSlaves = masterDev.slaveCount();
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
        for (i = 0; i < numSlaves; i++) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
            writeSlaveAlias(i, alias);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
        }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
    } else {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
        masterDev.open(MasterDevice::ReadWrite);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
        writeSlaveAlias(slavePosition, alias);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
}
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
/*****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
/** Writes the Secondary slave address (alias) to the slave's SII.
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
 */
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
void writeSlaveAlias(
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
        uint16_t slavePosition,
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
        uint16_t alias
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
        )
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
{
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    ec_ioctl_slave_sii_t data;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
    ec_ioctl_slave_t slave;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    stringstream err;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    uint8_t crc;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    masterDev.getSlave(&slave, slavePosition);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
    if (slave.sii_nwords < 8) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
        err << "Current SII contents are too small to set an alias "
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
            << "(" << slave.sii_nwords << " words)!";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   103
        throw CommandException(err);
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
    // read first 8 SII words
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    data.slave_position = slavePosition;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
    data.offset = 0;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
    data.nwords = 8;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
    data.words = new uint16_t[data.nwords];
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    try {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
        masterDev.readSii(&data);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
    } catch (MasterDeviceException &e) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
        delete [] data.words;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
        err << "Failed to read SII: " << e.what();
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   117
        throw CommandException(err);
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
    // write new alias address in word 4
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
    data.words[4] = cputole16(alias);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
    // calculate checksum over words 0 to 6
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
    crc = calcSiiCrc((const uint8_t *) data.words, 14);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
    // write new checksum into first byte of word 7
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    *(uint8_t *) (data.words + 7) = crc;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
    // write first 8 words with new alias and checksum
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
    try {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
        masterDev.writeSii(&data);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
    } catch (MasterDeviceException &e) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
        delete [] data.words;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
        err << "Failed to read SII: " << e.what();
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   135
        throw CommandException(err);
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
    delete [] data.words;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
}
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
/*****************************************************************************/