tool/main.cpp
author Florian Pose <fp@igh-essen.com>
Thu, 24 Jul 2008 13:29:13 +0000
changeset 1143 09ee878d7214
parent 1142 59be91dfcbe1
child 1144 7dbfdd61812c
permissions -rw-r--r--
Fixed duplicate alias command.
922
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <getopt.h>
1118
9487162e4d4d helpWanted; cmdName.
Florian Pose <fp@igh-essen.com>
parents: 1099
diff changeset
     8
#include <libgen.h> // basename()
922
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
#include <iostream>
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    11
#include <iomanip>
922
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
using namespace std;
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    14
#include "CommandAlias.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    15
#include "CommandConfig.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    16
#include "CommandData.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    17
#include "CommandDebug.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    18
#include "CommandDomains.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    19
#include "CommandMaster.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    20
#include "CommandPdos.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    21
#include "CommandSdos.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    22
#include "CommandDownload.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    23
#include "CommandUpload.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    24
#include "CommandSlaves.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    25
#include "CommandSiiRead.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    26
#include "CommandSiiWrite.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    27
#include "CommandStates.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    28
#include "CommandXml.h"
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    29
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    30
/*****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    31
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    32
typedef list<Command *> CommandList;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    33
CommandList commandList;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    34
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    35
MasterDevice masterDev;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    36
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    37
string binaryBaseName;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    38
string commandName;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    39
Command::StringVector commandArgs;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    40
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    41
// option variables
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    42
unsigned int masterIndex = 0;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    43
int slavePosition = -1;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    44
int domainIndex = -1;
968
b0e894257743 Implemented sdo_upload command.
Florian Pose <fp@igh-essen.com>
parents: 966
diff changeset
    45
string dataTypeStr;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    46
Command::Verbosity verbosity = Command::Normal;
980
c07dd38243ba Implemented SII writing (including alias writing) via ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 978
diff changeset
    47
bool force = false;
1121
52a005ffd011 helpRequested.
Florian Pose <fp@igh-essen.com>
parents: 1120
diff changeset
    48
bool helpRequested = false;
922
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    50
/*****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    51
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    52
void printUsage()
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    53
{
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    54
    CommandList::const_iterator ci;
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    55
    size_t maxWidth = 0;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    56
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    57
    for (ci = commandList.begin(); ci != commandList.end(); ci++) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    58
        if ((*ci)->getName().length() > maxWidth) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    59
            maxWidth = (*ci)->getName().length();
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    60
        }
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    61
    }
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    62
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    63
    cerr
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    64
        << "Usage: " << binaryBaseName << " <COMMAND> [OPTIONS] [ARGUMENTS]"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    65
        << endl << endl
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    66
		<< "Commands (can be abbreviated):" << endl;
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    67
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    68
    cerr << left;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    69
    for (ci = commandList.begin(); ci != commandList.end(); ci++) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    70
        cerr << "  " << setw(maxWidth) << (*ci)->getName()
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
    71
            << "  " << (*ci)->getBriefDescription() << endl;
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    72
    }
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    73
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents: 1125
diff changeset
    74
    cerr
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    75
        << endl
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    76
		<< "Global options:" << endl
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    77
        << "  --master  -m <master>  Index of the master to use. Default: 0."
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    78
		<< endl
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    79
        << "  --force   -f           Force a command." << endl
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    80
        << "  --quiet   -q           Output less information." << endl
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    81
        << "  --verbose -v           Output more information." << endl
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    82
        << "  --help    -h           Show this help." << endl
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    83
        << endl
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    84
        << "Numerical values can be specified either with decimal "
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    85
        << "(no prefix)," << endl
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    86
        << "octal (prefix '0') or hexadecimal (prefix '0x') base." << endl
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    87
        << endl
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    88
        << "Call '" << binaryBaseName
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    89
        << " <COMMAND> --help' for command-specific help." << endl
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    90
        << endl
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    91
        << "Send bug reports to " << PACKAGE_BUGREPORT << "." << endl;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    92
}
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    93
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    94
/*****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    95
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    96
void getOptions(int argc, char **argv)
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    97
{
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    98
    int c, argCount, optionIndex, number;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
    99
	char *remainder;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   100
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   101
    static struct option longOptions[] = {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   102
        //name,     has_arg,           flag, val
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   103
        {"master",  required_argument, NULL, 'm'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   104
        {"slave",   required_argument, NULL, 's'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   105
        {"domain",  required_argument, NULL, 'd'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   106
        {"type",    required_argument, NULL, 't'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   107
        {"force",   no_argument,       NULL, 'f'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   108
        {"quiet",   no_argument,       NULL, 'q'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   109
        {"verbose", no_argument,       NULL, 'v'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   110
        {"help",    no_argument,       NULL, 'h'},
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   111
        {}
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   112
    };
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   113
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   114
    do {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   115
        c = getopt_long(argc, argv, "m:s:d:t:fqvh", longOptions, &optionIndex);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   116
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   117
        switch (c) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   118
            case 'm':
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   119
                number = strtoul(optarg, &remainder, 0);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   120
                if (remainder == optarg || *remainder || number < 0) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   121
                    cerr << "Invalid master number " << optarg << "!" << endl;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   122
                    printUsage();
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   123
                    exit(1);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   124
                }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   125
				masterIndex = number;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   126
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   127
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   128
            case 's':
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   129
                if (!strcmp(optarg, "all")) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   130
                    slavePosition = -1;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   131
                } else {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   132
                    number = strtoul(optarg, &remainder, 0);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   133
                    if (remainder == optarg || *remainder
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   134
                            || number < 0 || number > 0xFFFF) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   135
                        cerr << "Invalid slave position "
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   136
                            << optarg << "!" << endl;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   137
                        printUsage();
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   138
                        exit(1);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   139
                    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   140
                    slavePosition = number;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   141
                }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   142
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   143
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   144
            case 'd':
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   145
                if (!strcmp(optarg, "all")) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   146
                    domainIndex = -1;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   147
                } else {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   148
                    number = strtoul(optarg, &remainder, 0);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   149
                    if (remainder == optarg || *remainder || number < 0) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   150
                        cerr << "Invalid domain index "
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   151
							<< optarg << "!" << endl;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   152
                        printUsage();
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   153
                        exit(1);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   154
                    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   155
                    domainIndex = number;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   156
                }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   157
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   158
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   159
            case 't':
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   160
                dataTypeStr = optarg;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   161
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   162
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   163
            case 'f':
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   164
                force = true;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   165
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   166
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   167
            case 'q':
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   168
                verbosity = Command::Quiet;
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   169
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   170
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   171
            case 'v':
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   172
                verbosity = Command::Verbose;
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   173
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   174
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   175
            case 'h':
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   176
                helpRequested = true;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   177
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   178
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   179
            case '?':
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   180
                printUsage();
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   181
                exit(1);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   182
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   183
            default:
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   184
                break;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   185
        }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   186
    }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   187
    while (c != -1);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   188
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   189
	argCount = argc - optind;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   190
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   191
    if (!argCount) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   192
        if (!helpRequested) {
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   193
            cerr << "Please specify a command!" << endl;
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   194
        }
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   195
        printUsage();
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   196
        exit(!helpRequested);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   197
	}
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   198
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   199
    commandName = argv[optind];
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   200
    while (++optind < argc)
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   201
        commandArgs.push_back(string(argv[optind]));
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   202
}
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   203
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   204
/****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   205
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   206
list<Command *> getMatchingCommands(const string &cmdStr)
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   207
{
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   208
    CommandList::iterator ci;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   209
    list<Command *> res;
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   210
1127
bd7aef6c310c Improved abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   211
    // find matching commands from beginning of the string
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   212
    for (ci = commandList.begin(); ci != commandList.end(); ci++) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   213
        if ((*ci)->matchesSubstr(cmdStr)) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   214
            res.push_back(*ci);
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   215
        }
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   216
    }
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   217
1127
bd7aef6c310c Improved abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   218
    if (!res.size()) { // nothing found
bd7aef6c310c Improved abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   219
        // find /any/ matching commands
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   220
        for (ci = commandList.begin(); ci != commandList.end(); ci++) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   221
            if ((*ci)->matchesAbbrev(cmdStr)) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   222
                res.push_back(*ci);
1127
bd7aef6c310c Improved abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   223
            }
bd7aef6c310c Improved abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   224
        }
bd7aef6c310c Improved abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   225
    }
bd7aef6c310c Improved abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1126
diff changeset
   226
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   227
    return res;
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   228
}
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   229
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   230
/****************************************************************************/
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   231
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   232
int main(int argc, char **argv)
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   233
{
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   234
    int retval = 0;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   235
    list<Command *> matchingCommands;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   236
    list<Command *>::const_iterator ci;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   237
    Command *cmd;
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   238
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   239
    binaryBaseName = basename(argv[0]);
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   240
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   241
    commandList.push_back(new CommandAlias());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   242
    commandList.push_back(new CommandConfig());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   243
    commandList.push_back(new CommandData());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   244
    commandList.push_back(new CommandDebug());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   245
    commandList.push_back(new CommandDomains());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   246
    commandList.push_back(new CommandDownload());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   247
    commandList.push_back(new CommandMaster());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   248
    commandList.push_back(new CommandPdos());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   249
    commandList.push_back(new CommandSdos());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   250
    commandList.push_back(new CommandSiiRead());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   251
    commandList.push_back(new CommandSiiWrite());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   252
    commandList.push_back(new CommandSlaves());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   253
    commandList.push_back(new CommandStates());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   254
    commandList.push_back(new CommandUpload());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   255
    commandList.push_back(new CommandXml());
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   256
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   257
	getOptions(argc, argv);
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   258
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   259
    matchingCommands = getMatchingCommands(commandName);
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   260
    masterDev.setIndex(masterIndex);
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   261
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   262
    if (matchingCommands.size()) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   263
        if (matchingCommands.size() == 1) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   264
            cmd = matchingCommands.front();
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   265
            if (!helpRequested) {
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   266
                try {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   267
                    cmd->execute(masterDev, commandArgs);
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   268
                } catch (InvalidUsageException &e) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   269
                    cerr << e.what() << endl << endl;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   270
                    cerr << cmd->helpString();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   271
                    retval = 1;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   272
                } catch (CommandException &e) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   273
                    cerr << e.what() << endl;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   274
                    retval = 1;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   275
                } catch (MasterDeviceException &e) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   276
                    cerr << e.what() << endl;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   277
                    retval = 1;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   278
                }
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   279
            } else {
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   280
                cout << cmd->helpString();
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   281
            }
935
b954e9d91ea5 Added 'pdos' command to ethercat tool.
Florian Pose <fp@igh-essen.com>
parents: 922
diff changeset
   282
        } else {
1128
b69b2c850160 Fixed typos.
Florian Pose <fp@igh-essen.com>
parents: 1127
diff changeset
   283
            cerr << "Ambiguous command abbreviation! Matching:" << endl;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   284
            for (ci = matchingCommands.begin();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   285
                    ci != matchingCommands.end();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   286
                    ci++) {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents: 1139
diff changeset
   287
                cerr << (*ci)->getName() << endl;
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   288
            }
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   289
            cerr << endl;
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   290
            printUsage();
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   291
            retval = 1;
935
b954e9d91ea5 Added 'pdos' command to ethercat tool.
Florian Pose <fp@igh-essen.com>
parents: 922
diff changeset
   292
        }
1125
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   293
    } else {
9976f7b9fe66 Command abbreviation.
Florian Pose <fp@igh-essen.com>
parents: 1122
diff changeset
   294
        cerr << "Unknown command " << commandName << "!" << endl << endl;
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   295
        printUsage();
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   296
        retval = 1;
922
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
    }
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
1122
ee305a780a02 First step of moving to command list.
Florian Pose <fp@igh-essen.com>
parents: 1121
diff changeset
   299
	return retval;
922
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
}
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
fede1d8f5b71 Added the new ethercat comman-line tool with lsec functionality.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
/****************************************************************************/