tool/Command.h
author Florian Pose <fp@igh-essen.com>
Mon, 28 Jul 2008 12:11:50 +0000
changeset 1159 25cc77cf3993
parent 1156 ecaf2a896ea3
child 1166 006244d53f68
permissions -rw-r--r--
Improved alias/position addressing in case od duplicated aliases.
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#ifndef __COMMAND_H__
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#define __COMMAND_H__
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
#include <stdexcept>
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
#include <vector>
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    12
#include <list>
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
using namespace std;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
#include "MasterDevice.h"
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
/*****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
extern unsigned int masterIndex;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
extern int domainIndex;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
extern string dataTypeStr;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
extern bool force;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
class InvalidUsageException:
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
    public runtime_error
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
    friend class Command;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
    protected:
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
        /** Constructor with stringstream parameter. */
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
        InvalidUsageException(
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
                const stringstream &s /**< Message. */
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
                ): runtime_error(s.str()) {}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
};
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
class CommandException:
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
    public runtime_error
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
    friend class Command;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
    protected:
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
        /** Constructor with stringstream parameter. */
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
        CommandException(
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
                const stringstream &s /**< Message. */
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
                ): runtime_error(s.str()) {}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
};
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
class Command
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    public:
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
        Command(const string &, const string &);
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
		virtual ~Command();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    60
        const string &getName() const;
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    61
        const string &getBriefDescription() const;
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    62
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
        enum Verbosity {
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
            Quiet,
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
            Normal,
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
            Verbose
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
        };
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
        void setVerbosity(Verbosity);
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
		Verbosity getVerbosity() const;
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    70
        void setAlias(int);
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    71
        int getAlias() const;
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    72
        void setPosition(int);
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    73
        int getPosition() const;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
        bool matchesSubstr(const string &) const;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
        bool matchesAbbrev(const string &) const;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
        virtual string helpString() const = 0;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
        typedef vector<string> StringVector;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
        virtual void execute(MasterDevice &, const StringVector &) = 0;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
1144
7dbfdd61812c Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents: 1142
diff changeset
    83
        static string numericInfo();
7dbfdd61812c Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents: 1142
diff changeset
    84
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
    protected:
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    86
		enum {BreakAfterBytes = 16};
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    87
1155
bd4e5b544473 Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents: 1151
diff changeset
    88
        void throwInvalidUsageException(const stringstream &) const;
bd4e5b544473 Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents: 1151
diff changeset
    89
        void throwCommandException(const stringstream &) const;
bd4e5b544473 Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents: 1151
diff changeset
    90
        void throwSingleSlaveRequired(unsigned int) const;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    92
        typedef list<ec_ioctl_slave_t> SlaveList;
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    93
        SlaveList selectedSlaves(MasterDevice &);
1156
ecaf2a896ea3 Implemented alias and position for configs.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    94
        typedef list<ec_ioctl_config_t> ConfigList;
ecaf2a896ea3 Implemented alias and position for configs.
Florian Pose <fp@igh-essen.com>
parents: 1155
diff changeset
    95
        ConfigList selectedConfigs(MasterDevice &);
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
    96
1146
f18d124d7fbc Moved slaveState() to Command::alStateString().
Florian Pose <fp@igh-essen.com>
parents: 1144
diff changeset
    97
        static string alStateString(uint8_t);
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    private:
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
		string name;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
        string briefDesc;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
        Verbosity verbosity;
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   103
        int alias;
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   104
        int position;
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
        Command();
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
};
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
inline const string &Command::getName() const
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    return name;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
inline const string &Command::getBriefDescription() const
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
    return briefDesc;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
inline Command::Verbosity Command::getVerbosity() const
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
{
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    return verbosity;
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
}
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
/****************************************************************************/
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
1151
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   132
inline int Command::getAlias() const
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   133
{
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   134
    return alias;
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   135
}
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   136
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   137
/****************************************************************************/
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   138
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   139
inline int Command::getPosition() const
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   140
{
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   141
    return position;
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   142
}
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   143
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   144
/****************************************************************************/
1fc1535dec29 Alias/Position selection.
Florian Pose <fp@igh-essen.com>
parents: 1146
diff changeset
   145
1142
59be91dfcbe1 Redesigned command interface.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
#endif