tool/main.cpp
changeset 1126 b09fd81894cb
parent 1125 9976f7b9fe66
child 1127 bd7aef6c310c
equal deleted inserted replaced
1125:9976f7b9fe66 1126:b09fd81894cb
     6 
     6 
     7 #include <getopt.h>
     7 #include <getopt.h>
     8 #include <libgen.h> // basename()
     8 #include <libgen.h> // basename()
     9 
     9 
    10 #include <iostream>
    10 #include <iostream>
       
    11 #include <iomanip>
    11 #include <string>
    12 #include <string>
    12 #include <vector>
    13 #include <vector>
       
    14 #include <list>
    13 using namespace std;
    15 using namespace std;
    14 
    16 
    15 #include "globals.h"
    17 #include "globals.h"
    16 
    18 
    17 /*****************************************************************************/
    19 /*****************************************************************************/
    34 
    36 
    35 struct Command {
    37 struct Command {
    36     const char *name;
    38     const char *name;
    37     void (*func)(void);
    39     void (*func)(void);
    38     const char *helpString;
    40     const char *helpString;
       
    41     const char *briefDesc;
    39 
    42 
    40     int execute(void) const;
    43     int execute(void) const;
    41     void displayHelp(void) const;
    44     void displayHelp(void) const;
    42 };
    45 };
    43 
    46 
    44 /*****************************************************************************/
    47 /*****************************************************************************/
    45 
    48 
    46 #define COMMAND(name) \
    49 #define DEFINE_EXTERN_COMMAND(name) \
    47     void command_##name(void); \
    50     void command_##name(void); \
    48     extern const char *help_##name
    51     extern const char *help_##name
    49 
    52 
    50 #define INIT_COMMAND(name) {#name, command_##name, help_##name}
    53 #define INIT_COMMAND(name, desc) \
    51 
    54     {#name, command_##name, help_##name, desc}
    52 COMMAND(alias);
    55 
    53 COMMAND(config);
    56 DEFINE_EXTERN_COMMAND(alias);
       
    57 DEFINE_EXTERN_COMMAND(config);
       
    58 DEFINE_EXTERN_COMMAND(data);
       
    59 DEFINE_EXTERN_COMMAND(debug);
       
    60 DEFINE_EXTERN_COMMAND(domains);
       
    61 DEFINE_EXTERN_COMMAND(master);
       
    62 DEFINE_EXTERN_COMMAND(pdos);
       
    63 DEFINE_EXTERN_COMMAND(sdos);
       
    64 DEFINE_EXTERN_COMMAND(sdo_download);
       
    65 DEFINE_EXTERN_COMMAND(sdo_upload);
       
    66 DEFINE_EXTERN_COMMAND(slaves);
       
    67 DEFINE_EXTERN_COMMAND(sii_read);
       
    68 DEFINE_EXTERN_COMMAND(sii_write);
       
    69 DEFINE_EXTERN_COMMAND(states);
       
    70 DEFINE_EXTERN_COMMAND(xml);
    54 
    71 
    55 static const Command commands[] = {
    72 static const Command commands[] = {
    56     INIT_COMMAND(alias),
    73     INIT_COMMAND(alias, "Write alias addresses."),
    57     INIT_COMMAND(config),
    74     INIT_COMMAND(config, "Show bus configuration."),
       
    75     INIT_COMMAND(data, "Output binary domain process data."),
       
    76     INIT_COMMAND(debug, "Set the master's debug level."),
       
    77     INIT_COMMAND(domains, "Show domain information."),
       
    78     INIT_COMMAND(master, "Show master information."),
       
    79     INIT_COMMAND(pdos, "List Pdo assignment/mapping."),
       
    80     INIT_COMMAND(sdos, "List Sdo dictionaries."),
       
    81     INIT_COMMAND(sdo_download, "Write an Sdo entry."),
       
    82     INIT_COMMAND(sdo_upload, "Read an Sdo entry."),
       
    83     INIT_COMMAND(slaves, "Show slaves."),
       
    84     INIT_COMMAND(sii_read, "Output a slave's SII contents."),
       
    85     INIT_COMMAND(sii_write, "Write slave's SII contents."),
       
    86     INIT_COMMAND(states, "Request slave states."),
       
    87     INIT_COMMAND(xml, "Generate slave information xmls."),
    58 };
    88 };
    59 
    89 
    60 #if 0
    90 static const Command *cmdEnd = commands + sizeof(commands) / sizeof(Command);
    61         } else if (command == "data") {
       
    62             master.outputData(domainIndex);
       
    63         } else if (command == "debug") {
       
    64             master.setDebug(commandArgs);
       
    65         } else if (command == "domain") {
       
    66             master.showDomains(domainIndex);
       
    67 		} else if (command == "master") {
       
    68             master.showMaster();
       
    69         } else if (command == "pdos") {
       
    70             master.listPdos(slavePosition);
       
    71         } else if (command == "sdos") {
       
    72             master.listSdos(slavePosition);
       
    73         } else if (command == "sdo_download" || command == "sd") {
       
    74             master.sdoDownload(slavePosition, dataTypeStr, commandArgs);
       
    75         } else if (command == "sdo_upload" || command == "su") {
       
    76             master.sdoUpload(slavePosition, dataTypeStr, commandArgs);
       
    77 		} else if (command == "slave" || command == "slaves"
       
    78                 || command == "list" || command == "ls") {
       
    79             master.showSlaves(slavePosition);
       
    80         } else if (command == "sii_read" || command == "sr") {
       
    81             master.siiRead(slavePosition);
       
    82         } else if (command == "sii_write" || command == "sw") {
       
    83             master.siiWrite(slavePosition, force, commandArgs);
       
    84         } else if (command == "state") {
       
    85             master.requestStates(slavePosition, commandArgs);
       
    86         } else if (command == "xml") {
       
    87             master.generateXml(slavePosition);
       
    88 #endif
       
    89 
    91 
    90 /*****************************************************************************/
    92 /*****************************************************************************/
    91 
    93 
    92 void printUsage()
    94 void printUsage()
    93 {
    95 {
       
    96     const Command *cmd;
       
    97     size_t maxWidth = 0;
       
    98 
       
    99     for (cmd = commands; cmd < cmdEnd; cmd++) {
       
   100         if (strlen(cmd->name) > maxWidth) {
       
   101             maxWidth = strlen(cmd->name);
       
   102         }
       
   103     }
       
   104 
    94     cerr
   105     cerr
    95         << "Usage: " << binaryBaseName << " <COMMAND> [OPTIONS]" << endl
   106         << "Usage: " << binaryBaseName << " <COMMAND> [OPTIONS]" << endl
    96 		<< "Commands:" << endl
   107 		<< "Commands:" << endl;
    97         << "  alias         Write alias addresses." << endl
   108 
    98         << "  config        Show bus configuration." << endl
   109     cerr << left;
    99         << "  data          Output binary domain process data." << endl
   110     for (cmd = commands; cmd < cmdEnd; cmd++) {
   100         << "  debug         Set the master's debug level." << endl
   111         cerr << "  " << setw(maxWidth) << cmd->name
   101         << "  domain        Show domain information." << endl
   112             << " " << cmd->briefDesc << endl;
   102         << "  master        Show master information." << endl
   113     }
   103         << "  pdos          List Pdo assignment/mapping." << endl
   114 
   104         << "  sdo_download  Write an Sdo entry." << endl
   115     cerr
   105         << "  sdos          List Sdo dictionaries." << endl
       
   106         << "  sdo_upload    Read an Sdo entry." << endl
       
   107         << "  sii_read      Output a slave's SII contents." << endl
       
   108         << "  sii_write     Write slave's SII contents." << endl
       
   109         << "  slaves        Show slaves." << endl
       
   110         << "  state         Request slave states." << endl
       
   111         << "  xml           Generate slave information xmls." << endl
       
   112         << "Commands can be generously abbreviated." << endl
   116         << "Commands can be generously abbreviated." << endl
   113 		<< "Global options:" << endl
   117 		<< "Global options:" << endl
   114         << "  --master  -m <master>  Index of the master to use. Default: 0"
   118         << "  --master  -m <master>  Index of the master to use. Default: 0"
   115 		<< endl
   119 		<< endl
   116         << "  --slave   -s <index>   Positive numerical ring position,"
   120         << "  --slave   -s <index>   Positive numerical ring position,"
   117         << endl
   121         << endl
   118         << "                         or 'all' for all slaves (default)."
   122         << "                         or 'all' for all slaves (default)."
   119         << endl
       
   120         << "  --domain  -d <index>   Positive numerical index,"
       
   121         << endl
   123         << endl
   122         << "                         or 'all' for all domains (default)."
   124         << "                         or 'all' for all domains (default)."
   123         << endl
   125         << endl
   124         << "  --type    -t <type>    Forced Sdo data type." << endl
   126         << "  --type    -t <type>    Forced Sdo data type." << endl
   125         << "  --force   -f           Force action." << endl
   127         << "  --force   -f           Force action." << endl
   287     
   289     
   288 /****************************************************************************/
   290 /****************************************************************************/
   289 
   291 
   290 list<const Command *> getMatchingCommands(const string &cmdStr)
   292 list<const Command *> getMatchingCommands(const string &cmdStr)
   291 {
   293 {
   292     const Command *cmd, *endCmd =
   294     const Command *cmd;
   293         commands + sizeof(commands) / sizeof(Command);
       
   294     list<const Command *> res;
   295     list<const Command *> res;
   295 
   296 
   296     // find matching commands
   297     // find matching commands
   297     for (cmd = commands; cmd < endCmd; cmd++) {
   298     for (cmd = commands; cmd < cmdEnd; cmd++) {
   298         if (abbrevMatch(cmdStr, cmd->name)) {
   299         if (abbrevMatch(cmdStr, cmd->name)) {
   299             res.push_back(cmd);
   300             res.push_back(cmd);
   300         }
   301         }
   301     }
   302     }
   302 
   303 
   343 
   344 
   344 	return retval;
   345 	return retval;
   345 }
   346 }
   346 
   347 
   347 /****************************************************************************/
   348 /****************************************************************************/
       
   349 
       
   350 void printRawData(
       
   351 		const uint8_t *data,
       
   352 		unsigned int size
       
   353 		)
       
   354 {
       
   355     cout << hex << setfill('0');
       
   356     while (size--) {
       
   357         cout << "0x" << setw(2) << (unsigned int) *data++;
       
   358         if (size)
       
   359             cout << " ";
       
   360     }
       
   361     cout << endl;
       
   362 }
       
   363 
       
   364 /****************************************************************************/