tool/cmd_slaves.cpp
changeset 1142 59be91dfcbe1
parent 1141 7ffbca63fc72
child 1143 09ee878d7214
equal deleted inserted replaced
1141:7ffbca63fc72 1142:59be91dfcbe1
     1 /*****************************************************************************
       
     2  *
       
     3  * $Id$
       
     4  *
       
     5  ****************************************************************************/
       
     6 
       
     7 #include <iostream>
       
     8 #include <iomanip>
       
     9 #include <list>
       
    10 using namespace std;
       
    11 
       
    12 #include "globals.h"
       
    13 
       
    14 /****************************************************************************/
       
    15 
       
    16 const char *help_slaves =
       
    17     "[OPTIONS]\n"
       
    18     "\n"
       
    19     "Display slaves on the bus.\n"
       
    20     "\n"
       
    21     "If the --verbose option is not given, the slaves are displayed\n"
       
    22     "one-per-line. Example:\n"
       
    23     "\n"
       
    24     "1  5555:0  PREOP  +  EL3162 2C. Ana. Input 0-10V\n"
       
    25     "|  |    |  |      |  |\n"
       
    26     "|  |    |  |      |  \\- Name from SII if avaliable, otherwise\n"
       
    27     "|  |    |  |      |     hexadecimal vendor ID and product code\n"
       
    28     "|  |    |  |      |     separated by a colon.\n"
       
    29     "|  |    |  |      \\- Error flag. '+' means no error, 'E' means,\n"
       
    30     "|  |    |  |         that scanning or configuration failed.\n"
       
    31     "|  |    |  \\- Current slave state.\n"
       
    32     "|  |    \\- Relative position (decimal) after the last slave with an\n"
       
    33     "|  |       alias address set.\n"
       
    34     "|  \\- Alias address of the slave (if set to non-zero), or the alias\n"
       
    35     "|     of the last slave with an alias set, or zero if there is none.\n"
       
    36     "\\- Ring position (use this with any --slave option).\n"
       
    37     "\n"
       
    38     "If the --verbose option is given, a detailed (multi-line) description\n"
       
    39     "is output for each slave.\n"
       
    40     "\n"
       
    41     "Command-specific options:\n"
       
    42     "  --slave   -s <index>  Positive numerical ring position, or 'all' for\n"
       
    43     "                        all slaves (default).\n"
       
    44     "  --verbose -v          Show detailed slave information.\n"
       
    45     "\n"
       
    46     "Numerical values can be specified either with decimal (no prefix),\n"
       
    47     "octal (prefix '0') or hexadecimal (prefix '0x') base.\n";
       
    48 
       
    49 /****************************************************************************/
       
    50 
       
    51 void listSlaves(int);
       
    52 void showSlave(uint16_t);
       
    53 
       
    54 /****************************************************************************/
       
    55 
       
    56 void command_slaves()
       
    57 {
       
    58     masterDev.open(MasterDevice::Read);
       
    59 
       
    60     if (verbosity == Verbose) {
       
    61         if (slavePosition == -1) {
       
    62             unsigned int numSlaves = masterDev.slaveCount(), i;
       
    63 
       
    64             for (i = 0; i < numSlaves; i++) {
       
    65                 showSlave(i);
       
    66             }
       
    67         } else {
       
    68             showSlave(slavePosition);
       
    69         }
       
    70     } else {
       
    71         listSlaves(slavePosition);
       
    72     }
       
    73 }
       
    74 
       
    75 /****************************************************************************/
       
    76 
       
    77 string slaveState(uint8_t state)
       
    78 {
       
    79     switch (state) {
       
    80         case 1: return "INIT";
       
    81         case 2: return "PREOP";
       
    82         case 4: return "SAFEOP";
       
    83         case 8: return "OP";
       
    84         default: return "???";
       
    85     }
       
    86 }
       
    87 
       
    88 /****************************************************************************/
       
    89 
       
    90 struct SlaveInfo {
       
    91     string pos;
       
    92     string alias;
       
    93     string relPos;
       
    94     string state;
       
    95     string flag;
       
    96     string name;
       
    97 };
       
    98 
       
    99 void listSlaves(int slavePosition)
       
   100 {
       
   101     unsigned int numSlaves, i;
       
   102     ec_ioctl_slave_t slave;
       
   103     uint16_t lastAlias, aliasIndex;
       
   104     SlaveInfo slaveInfo;
       
   105     typedef list<SlaveInfo> SlaveInfoList;
       
   106     SlaveInfoList slaveInfoList;
       
   107     SlaveInfoList::const_iterator iter;
       
   108     stringstream str;
       
   109     unsigned int maxPosWidth = 0, maxAliasWidth = 0,
       
   110                  maxRelPosWidth = 0, maxStateWidth = 0;
       
   111     
       
   112     numSlaves = masterDev.slaveCount();
       
   113 
       
   114     lastAlias = 0;
       
   115     aliasIndex = 0;
       
   116     for (i = 0; i < numSlaves; i++) {
       
   117         masterDev.getSlave(&slave, i);
       
   118         
       
   119         if (slave.alias) {
       
   120             lastAlias = slave.alias;
       
   121             aliasIndex = 0;
       
   122         }
       
   123 
       
   124         if (slavePosition == -1 || i == (unsigned int) slavePosition) {
       
   125             str << dec << i;
       
   126             slaveInfo.pos = str.str();
       
   127             str.clear();
       
   128             str.str("");
       
   129 
       
   130             str << lastAlias;
       
   131             slaveInfo.alias = str.str();
       
   132             str.str("");
       
   133 
       
   134             str << aliasIndex;
       
   135             slaveInfo.relPos = str.str();
       
   136             str.str("");
       
   137 
       
   138             slaveInfo.state = slaveState(slave.state);
       
   139             slaveInfo.flag = (slave.error_flag ? 'E' : '+');
       
   140 
       
   141             if (strlen(slave.name)) {
       
   142                 slaveInfo.name = slave.name;
       
   143             } else {
       
   144                 str << "0x" << hex << setfill('0')
       
   145                     << setw(8) << slave.vendor_id << ":0x"
       
   146                     << setw(8) << slave.product_code;
       
   147                 slaveInfo.name = str.str();
       
   148                 str.str("");
       
   149             }
       
   150 
       
   151 
       
   152             slaveInfoList.push_back(slaveInfo);
       
   153 
       
   154             if (slaveInfo.pos.length() > maxPosWidth)
       
   155                 maxPosWidth = slaveInfo.pos.length();
       
   156             if (slaveInfo.alias.length() > maxAliasWidth)
       
   157                 maxAliasWidth = slaveInfo.alias.length();
       
   158             if (slaveInfo.relPos.length() > maxRelPosWidth)
       
   159                 maxRelPosWidth = slaveInfo.relPos.length();
       
   160             if (slaveInfo.state.length() > maxStateWidth)
       
   161                 maxStateWidth = slaveInfo.state.length();
       
   162         }
       
   163 
       
   164         aliasIndex++;
       
   165     }
       
   166 
       
   167     for (iter = slaveInfoList.begin(); iter != slaveInfoList.end(); iter++) {
       
   168         cout << setfill(' ') << right
       
   169             << setw(maxPosWidth) << iter->pos << "  "
       
   170             << setw(maxAliasWidth) << iter->alias
       
   171             << ":" << left
       
   172             << setw(maxRelPosWidth) << iter->relPos << "  "
       
   173             << setw(maxStateWidth) << iter->state << "  "
       
   174             << iter->flag << "  "
       
   175             << iter->name << endl;
       
   176     }
       
   177 }
       
   178 
       
   179 /****************************************************************************/
       
   180 
       
   181 void showSlave(uint16_t slavePosition)
       
   182 {
       
   183     ec_ioctl_slave_t slave;
       
   184     list<string> protoList;
       
   185     list<string>::const_iterator protoIter;
       
   186     
       
   187     masterDev.getSlave(&slave, slavePosition);
       
   188         
       
   189     cout << "=== Slave " << dec << slavePosition << " ===" << endl;
       
   190     
       
   191     if (slave.alias)
       
   192         cout << "Alias: " << slave.alias << endl;
       
   193 
       
   194     cout
       
   195         << "State: " << slaveState(slave.state) << endl
       
   196         << "Flag: " << (slave.error_flag ? 'E' : '+') << endl
       
   197         << "Identity:" << endl
       
   198         << "  Vendor Id:       0x"
       
   199         << hex << setfill('0')
       
   200         << setw(8) << slave.vendor_id << endl
       
   201         << "  Product code:    0x"
       
   202         << setw(8) << slave.product_code << endl
       
   203         << "  Revision number: 0x"
       
   204         << setw(8) << slave.revision_number << endl
       
   205         << "  Serial number:   0x"
       
   206         << setw(8) << slave.serial_number << endl;
       
   207 
       
   208     if (slave.mailbox_protocols) {
       
   209         cout << "Mailboxes:" << endl
       
   210         << "  RX: 0x"
       
   211         << hex << setw(4) << slave.rx_mailbox_offset << "/"
       
   212         << dec << slave.rx_mailbox_size
       
   213         << ", TX: 0x"
       
   214         << hex << setw(4) << slave.tx_mailbox_offset << "/"
       
   215         << dec << slave.tx_mailbox_size << endl
       
   216         << "  Supported protocols: ";
       
   217 
       
   218         if (slave.mailbox_protocols & EC_MBOX_AOE) {
       
   219             protoList.push_back("AoE");
       
   220         }
       
   221         if (slave.mailbox_protocols & EC_MBOX_EOE) {
       
   222             protoList.push_back("EoE");
       
   223         }
       
   224         if (slave.mailbox_protocols & EC_MBOX_COE) {
       
   225             protoList.push_back("CoE");
       
   226         }
       
   227         if (slave.mailbox_protocols & EC_MBOX_FOE) {
       
   228             protoList.push_back("FoE");
       
   229         }
       
   230         if (slave.mailbox_protocols & EC_MBOX_SOE) {
       
   231             protoList.push_back("SoE");
       
   232         }
       
   233         if (slave.mailbox_protocols & EC_MBOX_VOE) {
       
   234             protoList.push_back("VoE");
       
   235         }
       
   236 
       
   237         for (protoIter = protoList.begin(); protoIter != protoList.end();
       
   238                 protoIter++) {
       
   239             if (protoIter != protoList.begin())
       
   240                 cout << ", ";
       
   241             cout << *protoIter;
       
   242         }
       
   243         cout << endl;
       
   244     }
       
   245 
       
   246     if (slave.has_general_category) {
       
   247         cout << "General:" << endl
       
   248             << "  Group: " << slave.group << endl
       
   249             << "  Image name: " << slave.image << endl
       
   250             << "  Order number: " << slave.order << endl
       
   251             << "  Device name: " << slave.name << endl;
       
   252 
       
   253         if (slave.mailbox_protocols & EC_MBOX_COE) {
       
   254             cout << "  CoE details:" << endl
       
   255                 << "    Enable Sdo: "
       
   256                 << (slave.coe_details.enable_sdo ? "yes" : "no") << endl
       
   257                 << "    Enable Sdo Info: "
       
   258                 << (slave.coe_details.enable_sdo_info ? "yes" : "no") << endl
       
   259                 << "    Enable Pdo Assign: "
       
   260                 << (slave.coe_details.enable_pdo_assign
       
   261                         ? "yes" : "no") << endl
       
   262                 << "    Enable Pdo Configuration: "
       
   263                 << (slave.coe_details.enable_pdo_configuration
       
   264                         ? "yes" : "no") << endl
       
   265                 << "    Enable Upload at startup: "
       
   266                 << (slave.coe_details.enable_upload_at_startup
       
   267                         ? "yes" : "no") << endl
       
   268                 << "    Enable Sdo complete access: "
       
   269                 << (slave.coe_details.enable_sdo_complete_access
       
   270                         ? "yes" : "no") << endl;
       
   271         }
       
   272 
       
   273         cout << "  Flags:" << endl
       
   274             << "    Enable SafeOp: "
       
   275             << (slave.general_flags.enable_safeop ? "yes" : "no") << endl
       
   276             << "    Enable notLRW: "
       
   277             << (slave.general_flags.enable_not_lrw ? "yes" : "no") << endl
       
   278             << "  Current consumption: "
       
   279             << dec << slave.current_on_ebus << " mA" << endl;
       
   280     }
       
   281 }
       
   282 
       
   283 /*****************************************************************************/