tool/cmd_download.cpp
author Florian Pose <fp@igh-essen.com>
Thu, 24 Jul 2008 07:40:45 +0000
changeset 1139 074caa25365e
parent 1137 a73c0f54be42
permissions -rw-r--r--
Output requested help to stdout, help on invalid usage to stderr.
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <iostream>
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#include <iomanip>
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
using namespace std;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
#include "globals.h"
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
#include "coe_datatypes.h"
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
/****************************************************************************/
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
const char *help_download =
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    17
    "[OPTIONS] <INDEX> <SUBINDEX> <VALUE>\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    18
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    19
    "Download an Sdo entry to a slave.\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    20
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    21
    "The data type of the Sdo entry is taken from the Sdo dictionary by\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    22
    "default. It can be overridden with the --type option. If the slave\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    23
    "does not support the Sdo information service or the Sdo is not in the\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    24
    "dictionary, the --type option is mandatory.\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    25
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    26
    "These are the valid Sdo entry data types:\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    27
    "  int8, int16, int32, uint8, uint16, uint32, string.\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    28
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    29
    "Arguments:\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    30
    "  INDEX    is the Sdo index and must be an unsigned 16 bit number.\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    31
    "  SUBINDEX is the Sdo entry subindex and must be an unsigned 8 bit\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    32
    "           number.\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    33
    "  VALUE    is the value to download and must correspond to the Sdo\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    34
    "           entry datatype (see above).\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    35
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    36
    "Command-specific options:\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    37
    "  --slave -s <index>  Positive numerical ring position (mandatory).\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    38
    "  --type  -t <type>   Forced Sdo entry data type (see above).\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    39
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    40
    "Numerical values can be specified either with decimal (no prefix),\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    41
    "octal (prefix '0') or hexadecimal (prefix '0x') base.\n";
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    42
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
/****************************************************************************/
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
void command_download(void)
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
{
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
    stringstream strIndex, strSubIndex, strValue, err;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
    ec_ioctl_slave_sdo_download_t data;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
    unsigned int number;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
    const CoEDataType *dataType = NULL;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
    if (slavePosition < 0) {
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    54
        err << "'" << commandName << "' requires a slave! "
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    55
            << "Please specify --slave.";
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    56
        throw InvalidUsageException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    data.slave_position = slavePosition;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
    if (commandArgs.size() != 3) {
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    61
        err << "'" << commandName << "' takes 3 arguments!";
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    62
        throw InvalidUsageException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
    strIndex << commandArgs[0];
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
    strIndex
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
        >> resetiosflags(ios::basefield) // guess base from prefix
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
        >> data.sdo_index;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
    if (strIndex.fail()) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
        err << "Invalid Sdo index '" << commandArgs[0] << "'!";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    71
        throw InvalidUsageException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
    strSubIndex << commandArgs[1];
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
    strSubIndex
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
        >> resetiosflags(ios::basefield) // guess base from prefix
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
        >> number;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
    if (strSubIndex.fail() || number > 0xff) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
        err << "Invalid Sdo subindex '" << commandArgs[1] << "'!";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    80
        throw InvalidUsageException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
    data.sdo_entry_subindex = number;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    if (dataTypeStr != "") { // data type specified
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
        if (!(dataType = findDataType(dataTypeStr))) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
            err << "Invalid data type '" << dataTypeStr << "'!";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
    87
            throw InvalidUsageException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
        }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
    } else { // no data type specified: fetch from dictionary
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
        ec_ioctl_slave_sdo_entry_t entry;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
        masterDev.open(MasterDevice::ReadWrite);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
        try {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
            masterDev.getSdoEntry(&entry, slavePosition,
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
                    data.sdo_index, data.sdo_entry_subindex);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
        } catch (MasterDeviceException &e) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
            err << "Failed to determine Sdo entry data type. "
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
                << "Please specify --type.";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
   100
            throw CommandException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
        }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
        if (!(dataType = findDataType(entry.data_type))) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
            err << "Pdo entry has unknown data type 0x"
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
                << hex << setfill('0') << setw(4) << entry.data_type << "!"
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
                << " Please specify --type.";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
   106
            throw CommandException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
        }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
    if (dataType->byteSize) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
        data.data_size = dataType->byteSize;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    } else {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
        data.data_size = DefaultBufferSize;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
    data.data = new uint8_t[data.data_size + 1];
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
    strValue << commandArgs[2];
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
    strValue >> resetiosflags(ios::basefield); // guess base from prefix
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
    strValue.exceptions(ios::failbit);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
    try {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
        switch (dataType->coeCode) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
            case 0x0002: // int8
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
                {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
                    int16_t val; // uint8_t is interpreted as char
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
                    strValue >> val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
                    if (val > 127 || val < -128)
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
                        throw ios::failure("Value out of range");
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
                    *data.data = val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
                    break;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
                }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
            case 0x0003: // int16
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
                {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
                    int16_t val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
                    strValue >> val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
                    *(int16_t *) data.data = cputole16(val);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
                    break;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
                }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
            case 0x0004: // int32
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
                {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
                    int32_t val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
                    strValue >> val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
                    *(int32_t *) data.data = cputole32(val);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
                    break;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
                }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
            case 0x0005: // uint8
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
                {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
                    uint16_t val; // uint8_t is interpreted as char
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
                    strValue >> val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
                    if (val > 0xff)
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
                        throw ios::failure("Value out of range");
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
                    *data.data = val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
                    break;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
                }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
            case 0x0006: // uint16
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
                {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
                    uint16_t val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
                    strValue >> val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
                    *(uint16_t *) data.data = cputole16(val);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
                    break;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
                }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
            case 0x0007: // uint32
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
                {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
                    uint32_t val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
                    strValue >> val;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
                    *(uint32_t *) data.data = cputole32(val);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
                    break;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
                }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
            case 0x0009: // string
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
                if (strValue.str().size() >= data.data_size) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
                    err << "String too large";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
   173
                    throw CommandException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
                }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
                data.data_size = strValue.str().size();
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
                strValue >> (char *) data.data;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
                break;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
            default:
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
                delete [] data.data;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
                err << "Unknown data type 0x" << hex << dataType->coeCode;
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
   182
                throw CommandException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
        }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
    } catch (ios::failure &e) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
        delete [] data.data;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
        err << "Invalid value argument '" << commandArgs[2]
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
            << "' for type '" << dataType->name << "'!";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1134
diff changeset
   188
        throw InvalidUsageException(err);
1134
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
    masterDev.open(MasterDevice::ReadWrite);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
	try {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
		masterDev.sdoDownload(&data);
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
	} catch(MasterDeviceException &e) {
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
        delete [] data.data;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
        throw e;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
    }
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
    delete [] data.data;
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
}
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
3356141fa2f8 Renamed sdo_download command to download. Same with upload.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
/*****************************************************************************/