tool/CommandSlaves.cpp
changeset 1826 ec6223c3b7ec
parent 1463 37361de7b34e
child 1827 489be2180f71
equal deleted inserted replaced
1825:65781b048a47 1826:ec6223c3b7ec
    32 #include <list>
    32 #include <list>
    33 #include <string.h>
    33 #include <string.h>
    34 using namespace std;
    34 using namespace std;
    35 
    35 
    36 #include "CommandSlaves.h"
    36 #include "CommandSlaves.h"
       
    37 #include "MasterDevice.h"
    37 
    38 
    38 /*****************************************************************************/
    39 /*****************************************************************************/
    39 
    40 
    40 CommandSlaves::CommandSlaves():
    41 CommandSlaves::CommandSlaves():
    41     Command("slaves", "Display slaves on the bus.")
    42     Command("slaves", "Display slaves on the bus.")
   103     return str.str();
   104     return str.str();
   104 }
   105 }
   105 
   106 
   106 /****************************************************************************/
   107 /****************************************************************************/
   107 
   108 
   108 void CommandSlaves::execute(MasterDevice &m, const StringVector &args)
   109 void CommandSlaves::execute(const StringVector &args)
   109 {
   110 {
   110     SlaveList slaves;
   111     SlaveList slaves;
       
   112     bool doIndent;
   111     
   113     
   112     if (args.size()) {
   114     if (args.size()) {
   113         stringstream err;
   115         stringstream err;
   114         err << "'" << getName() << "' takes no arguments!";
   116         err << "'" << getName() << "' takes no arguments!";
   115         throwInvalidUsageException(err);
   117         throwInvalidUsageException(err);
   116     }
   118     }
   117 
   119 
   118     m.open(MasterDevice::Read);
   120     doIndent = getMasterIndices().size() > 1;
   119     slaves = selectedSlaves(m);
   121     MasterIndexList::const_iterator mi;
   120 
   122     for (mi = getMasterIndices().begin();
   121     if (getVerbosity() == Verbose) {
   123             mi != getMasterIndices().end(); mi++) {
   122         showSlaves(m, slaves);
   124         MasterDevice m(*mi);
   123     } else {
   125         m.open(MasterDevice::Read);
   124         listSlaves(m, slaves);
   126         slaves = selectedSlaves(m);
       
   127 
       
   128         if (doIndent) {
       
   129             cout << "Master" << dec << *mi << endl;
       
   130         }
       
   131 
       
   132         if (getVerbosity() == Verbose) {
       
   133             showSlaves(m, slaves);
       
   134         } else {
       
   135             listSlaves(m, slaves, doIndent);
       
   136         }
   125     }
   137     }
   126 }
   138 }
   127 
   139 
   128 /****************************************************************************/
   140 /****************************************************************************/
   129 
   141 
   130 void CommandSlaves::listSlaves(
   142 void CommandSlaves::listSlaves(
   131         MasterDevice &m,
   143         MasterDevice &m,
   132         const SlaveList &slaves
   144         const SlaveList &slaves,
       
   145         bool doIndent
   133         )
   146         )
   134 {
   147 {
   135     ec_ioctl_master_t master;
   148     ec_ioctl_master_t master;
   136     unsigned int i;
   149     unsigned int i;
   137     ec_ioctl_slave_t slave;
   150     ec_ioctl_slave_t slave;
   141     InfoList infoList;
   154     InfoList infoList;
   142     InfoList::const_iterator iter;
   155     InfoList::const_iterator iter;
   143     stringstream str;
   156     stringstream str;
   144     unsigned int maxPosWidth = 0, maxAliasWidth = 0,
   157     unsigned int maxPosWidth = 0, maxAliasWidth = 0,
   145                  maxRelPosWidth = 0, maxStateWidth = 0;
   158                  maxRelPosWidth = 0, maxStateWidth = 0;
       
   159     string indent(doIndent ? "  " : "");
   146     
   160     
   147     m.getMaster(&master);
   161     m.getMaster(&master);
   148 
   162 
   149     lastAlias = 0;
   163     lastAlias = 0;
   150     aliasIndex = 0;
   164     aliasIndex = 0;
   198 
   212 
   199         aliasIndex++;
   213         aliasIndex++;
   200     }
   214     }
   201 
   215 
   202     for (iter = infoList.begin(); iter != infoList.end(); iter++) {
   216     for (iter = infoList.begin(); iter != infoList.end(); iter++) {
   203         cout << setfill(' ') << right
   217         cout << indent << setfill(' ') << right
   204             << setw(maxPosWidth) << iter->pos << "  "
   218             << setw(maxPosWidth) << iter->pos << "  "
   205             << setw(maxAliasWidth) << iter->alias
   219             << setw(maxAliasWidth) << iter->alias
   206             << ":" << left
   220             << ":" << left
   207             << setw(maxRelPosWidth) << iter->relPos << "  "
   221             << setw(maxRelPosWidth) << iter->relPos << "  "
   208             << setw(maxStateWidth) << iter->state << "  "
   222             << setw(maxStateWidth) << iter->state << "  "
   220 {
   234 {
   221     SlaveList::const_iterator si;
   235     SlaveList::const_iterator si;
   222     int i;
   236     int i;
   223 
   237 
   224     for (si = slaves.begin(); si != slaves.end(); si++) {
   238     for (si = slaves.begin(); si != slaves.end(); si++) {
   225         cout << "=== Slave " << dec << si->position << " ===" << endl;
   239         cout << "=== Master " << dec << m.getIndex()
       
   240             << ", Slave " << dec << si->position << " ===" << endl;
   226 
   241 
   227         if (si->alias)
   242         if (si->alias)
   228             cout << "Alias: " << si->alias << endl;
   243             cout << "Alias: " << si->alias << endl;
   229 
   244 
   230         cout
   245         cout