tool/CommandConfig.cpp
changeset 1826 ec6223c3b7ec
parent 1804 742607c464c4
child 1827 489be2180f71
equal deleted inserted replaced
1825:65781b048a47 1826:ec6223c3b7ec
    32 #include <iomanip>
    32 #include <iomanip>
    33 #include <sstream>
    33 #include <sstream>
    34 using namespace std;
    34 using namespace std;
    35 
    35 
    36 #include "CommandConfig.h"
    36 #include "CommandConfig.h"
       
    37 #include "MasterDevice.h"
    37 
    38 
    38 /*****************************************************************************/
    39 /*****************************************************************************/
    39 
    40 
    40 CommandConfig::CommandConfig():
    41 CommandConfig::CommandConfig():
    41     Command("config", "Show slave configurations.")
    42     Command("config", "Show slave configurations.")
    99 
   100 
   100 /*****************************************************************************/
   101 /*****************************************************************************/
   101 
   102 
   102 /** Lists the bus configuration.
   103 /** Lists the bus configuration.
   103  */
   104  */
   104 void CommandConfig::execute(MasterDevice &m, const StringVector &args)
   105 void CommandConfig::execute(const StringVector &args)
   105 {
   106 {
   106     ConfigList configs;
   107     ConfigList configs;
       
   108     bool doIndent;
   107 
   109 
   108     if (args.size()) {
   110     if (args.size()) {
   109         stringstream err;
   111         stringstream err;
   110         err << "'" << getName() << "' takes no arguments!";
   112         err << "'" << getName() << "' takes no arguments!";
   111         throwInvalidUsageException(err);
   113         throwInvalidUsageException(err);
   112     }
   114     }
   113 
   115 
   114     m.open(MasterDevice::Read);
   116     doIndent = getMasterIndices().size() > 1;
   115     configs = selectedConfigs(m);
   117     MasterIndexList::const_iterator mi;
   116 
   118     for (mi = getMasterIndices().begin();
   117     if (getVerbosity() == Verbose) {
   119             mi != getMasterIndices().end(); mi++) {
   118         showDetailedConfigs(m, configs);
   120         MasterDevice m(*mi);
   119     } else {
   121         m.open(MasterDevice::Read);
   120         listConfigs(m, configs);
   122         configs = selectedConfigs(m);
       
   123 
       
   124         if (doIndent) {
       
   125             cout << "Master" << dec << *mi << endl;
       
   126         }
       
   127 
       
   128         if (getVerbosity() == Verbose) {
       
   129             showDetailedConfigs(m, configs, doIndent);
       
   130         } else {
       
   131             listConfigs(m, configs, doIndent);
       
   132         }
   121     }
   133     }
   122 }
   134 }
   123 
   135 
   124 /*****************************************************************************/
   136 /*****************************************************************************/
   125 
   137 
   126 /** Lists the complete bus configuration.
   138 /** Lists the complete bus configuration.
   127  */
   139  */
   128 void CommandConfig::showDetailedConfigs(
   140 void CommandConfig::showDetailedConfigs(
   129         MasterDevice &m,
   141         MasterDevice &m,
   130         const ConfigList &configList
   142         const ConfigList &configList,
       
   143         bool doIndent
   131         )
   144         )
   132 {
   145 {
   133     ConfigList::const_iterator configIter;
   146     ConfigList::const_iterator configIter;
   134     unsigned int i, j, k, l;
   147     unsigned int i, j, k, l;
   135     ec_ioctl_slave_t slave;
   148     ec_ioctl_slave_t slave;
   136     ec_ioctl_config_pdo_t pdo;
   149     ec_ioctl_config_pdo_t pdo;
   137     ec_ioctl_config_pdo_entry_t entry;
   150     ec_ioctl_config_pdo_entry_t entry;
   138     ec_ioctl_config_sdo_t sdo;
   151     ec_ioctl_config_sdo_t sdo;
       
   152     string indent(doIndent ? "  " : "");
   139 
   153 
   140     for (configIter = configList.begin();
   154     for (configIter = configList.begin();
   141             configIter != configList.end();
   155             configIter != configList.end();
   142             configIter++) {
   156             configIter++) {
   143 
   157 
   144         cout << "Alias: "
   158         cout << indent
   145             << dec << configIter->alias << endl
   159             << "Alias: "
   146             << "Position: " << configIter->position << endl
   160             << dec << configIter->alias << endl << indent
       
   161             << "Position: " << configIter->position << endl << indent
   147             << "Vendor Id: 0x"
   162             << "Vendor Id: 0x"
   148             << hex << setfill('0')
   163             << hex << setfill('0')
   149             << setw(8) << configIter->vendor_id << endl
   164             << setw(8) << configIter->vendor_id << endl << indent
   150             << "Product code: 0x"
   165             << "Product code: 0x"
   151             << setw(8) << configIter->product_code << endl
   166             << setw(8) << configIter->product_code << endl << indent
   152             << "Attached slave: ";
   167             << "Attached slave: ";
   153         
   168         
   154         if (configIter->slave_position != -1) {
   169         if (configIter->slave_position != -1) {
   155             m.getSlave(&slave, configIter->slave_position);
   170             m.getSlave(&slave, configIter->slave_position);
   156             cout << dec << configIter->slave_position
   171             cout << dec << configIter->slave_position
   157                 << " (" << alStateString(slave.al_state) << ")" << endl;
   172                 << " (" << alStateString(slave.al_state) << ")" << endl;
   158         } else {
   173         } else {
   159             cout << "none" << endl;
   174             cout << "none" << endl;
   160         }
   175         }
   161 
   176 
   162         cout << "Watchdog divider: ";
   177         cout << indent << "Watchdog divider: ";
   163         if (configIter->watchdog_divider) {
   178         if (configIter->watchdog_divider) {
   164             cout << dec << configIter->watchdog_divider;
   179             cout << dec << configIter->watchdog_divider;
   165         } else {
   180         } else {
   166             cout << "(Default)";
   181             cout << "(Default)";
   167         }
   182         }
   168         cout << endl
   183         cout << endl << indent
   169             << "Watchdog intervals: ";
   184             << "Watchdog intervals: ";
   170         if (configIter->watchdog_intervals) {
   185         if (configIter->watchdog_intervals) {
   171             cout << dec << configIter->watchdog_intervals;
   186             cout << dec << configIter->watchdog_intervals;
   172         } else {
   187         } else {
   173             cout << "(Default)";
   188             cout << "(Default)";
   174         }
   189         }
   175         cout << endl;
   190         cout << endl;
   176 
   191 
   177         for (j = 0; j < EC_MAX_SYNC_MANAGERS; j++) {
   192         for (j = 0; j < EC_MAX_SYNC_MANAGERS; j++) {
   178             if (configIter->syncs[j].pdo_count) {
   193             if (configIter->syncs[j].pdo_count) {
   179                 cout << "SM" << dec << j << ", Dir: "
   194                 cout << indent << "SM" << dec << j << ", Dir: "
   180                     << (configIter->syncs[j].dir == EC_DIR_INPUT
   195                     << (configIter->syncs[j].dir == EC_DIR_INPUT
   181                             ? "Input" : "Output") << ", Watchdog: ";
   196                             ? "Input" : "Output") << ", Watchdog: ";
   182                 switch (configIter->syncs[j].watchdog_mode) {
   197                 switch (configIter->syncs[j].watchdog_mode) {
   183                     case EC_WD_DEFAULT: cout << "Default"; break;
   198                     case EC_WD_DEFAULT: cout << "Default"; break;
   184                     case EC_WD_ENABLE: cout << "Enable"; break;
   199                     case EC_WD_ENABLE: cout << "Enable"; break;
   188                 cout << endl;
   203                 cout << endl;
   189 
   204 
   190                 for (k = 0; k < configIter->syncs[j].pdo_count; k++) {
   205                 for (k = 0; k < configIter->syncs[j].pdo_count; k++) {
   191                     m.getConfigPdo(&pdo, configIter->config_index, j, k);
   206                     m.getConfigPdo(&pdo, configIter->config_index, j, k);
   192 
   207 
   193                     cout << "  PDO 0x" << hex << setfill('0')
   208                     cout << indent << "  PDO 0x" << hex << setfill('0')
   194                         << setw(4) << pdo.index << endl;
   209                         << setw(4) << pdo.index << endl;
   195 
   210 
   196                     for (l = 0; l < pdo.entry_count; l++) {
   211                     for (l = 0; l < pdo.entry_count; l++) {
   197                         m.getConfigPdoEntry(&entry,
   212                         m.getConfigPdoEntry(&entry,
   198                                 configIter->config_index, j, k, l);
   213                                 configIter->config_index, j, k, l);
   199 
   214 
   200                         cout << "    PDO entry 0x" << hex << setfill('0')
   215                         cout << indent << "    PDO entry 0x"
       
   216                             << hex << setfill('0')
   201                             << setw(4) << entry.index << ":"
   217                             << setw(4) << entry.index << ":"
   202                             << setw(2) << (unsigned int) entry.subindex
   218                             << setw(2) << (unsigned int) entry.subindex
   203                             << ", " << dec << setfill(' ')
   219                             << ", " << dec << setfill(' ')
   204                             << setw(2) << (unsigned int) entry.bit_length
   220                             << setw(2) << (unsigned int) entry.bit_length
   205                             << " bit" << endl;
   221                             << " bit" << endl;
   206                     }
   222                     }
   207                 }
   223                 }
   208             }
   224             }
   209         }
   225         }
   210 
   226 
   211         cout << "SDO configuration:" << endl;
   227         cout << indent << "SDO configuration:" << endl;
   212         if (configIter->sdo_count) {
   228         if (configIter->sdo_count) {
   213             for (j = 0; j < configIter->sdo_count; j++) {
   229             for (j = 0; j < configIter->sdo_count; j++) {
   214                 m.getConfigSdo(&sdo, configIter->config_index, j);
   230                 m.getConfigSdo(&sdo, configIter->config_index, j);
   215 
   231 
   216                 cout << "  0x"
   232                 cout << indent << "  0x"
   217                     << hex << setfill('0')
   233                     << hex << setfill('0')
   218                     << setw(4) << sdo.index << ":"
   234                     << setw(4) << sdo.index << ":"
   219                     << setw(2) << (unsigned int) sdo.subindex
   235                     << setw(2) << (unsigned int) sdo.subindex
   220                     << ", " << dec << sdo.size << " byte" << endl;
   236                     << ", " << dec << sdo.size << " byte" << endl;
   221 
   237 
   222                 cout << "    " << hex;
   238                 cout << indent << "    " << hex;
   223                 for (i = 0; i < min((uint32_t) sdo.size,
   239                 for (i = 0; i < min((uint32_t) sdo.size,
   224                             (uint32_t) EC_MAX_SDO_DATA_SIZE); i++) {
   240                             (uint32_t) EC_MAX_SDO_DATA_SIZE); i++) {
   225                     cout << setw(2) << (unsigned int) sdo.data[i];
   241                     cout << setw(2) << (unsigned int) sdo.data[i];
   226                     if ((i + 1) % 16 == 0 && i < sdo.size - 1) {
   242                     if ((i + 1) % 16 == 0 && i < sdo.size - 1) {
   227                         cout << endl << "    ";
   243                         cout << endl << indent << "    ";
   228                     } else {
   244                     } else {
   229                         cout << " ";
   245                         cout << " ";
   230                     }
   246                     }
   231                 }
   247                 }
   232 
   248 
   233                 cout << endl;
   249                 cout << endl;
   234                 if (sdo.size > EC_MAX_SDO_DATA_SIZE) {
   250                 if (sdo.size > EC_MAX_SDO_DATA_SIZE) {
   235                     cout << "    ..." << endl;
   251                     cout << indent << "    ..." << endl;
   236                 }
   252                 }
   237             }
   253             }
   238         } else {
   254         } else {
   239             cout << "  None." << endl;
   255             cout << indent << "  None." << endl;
   240         }
   256         }
   241 
   257 
   242         if (configIter->dc_assign_activate) {
   258         if (configIter->dc_assign_activate) {
   243             int i;
   259             int i;
   244 
   260 
   245             cout << "DC configuration:" << endl
   261             cout << indent << "DC configuration:" << endl
   246                 << "  AssignActivate: 0x" << hex << setfill('0')
   262                 << indent << "  AssignActivate: 0x" << hex << setfill('0')
   247                 << setw(4) << configIter->dc_assign_activate << endl;
   263                 << setw(4) << configIter->dc_assign_activate << endl;
   248 
   264 
   249             cout << "         Cycle [ns]   Shift [ns]" << endl;
   265             cout << indent << "         Cycle [ns]   Shift [ns]" << endl;
   250             for (i = 0; i < EC_SYNC_SIGNAL_COUNT; i++) {
   266             for (i = 0; i < EC_SYNC_SIGNAL_COUNT; i++) {
   251                 cout << "  SYNC" << dec << i << "  "
   267                 cout << indent << "  SYNC" << dec << i << "  "
   252                     << setfill(' ') << right
   268                     << setfill(' ') << right
   253                     << setw(11) << configIter->dc_sync[i].cycle_time
   269                     << setw(11) << configIter->dc_sync[i].cycle_time
   254                     << "  "
   270                     << "  "
   255                     << setw(11) << configIter->dc_sync[i].shift_time
   271                     << setw(11) << configIter->dc_sync[i].shift_time
   256                     << endl;
   272                     << endl;
   264 
   280 
   265 /** Lists the bus configuration.
   281 /** Lists the bus configuration.
   266  */
   282  */
   267 void CommandConfig::listConfigs(
   283 void CommandConfig::listConfigs(
   268         MasterDevice &m,
   284         MasterDevice &m,
   269         const ConfigList &configList
   285         const ConfigList &configList,
       
   286         bool doIndent
   270         )
   287         )
   271 {
   288 {
   272     ConfigList::const_iterator configIter;
   289     ConfigList::const_iterator configIter;
   273     stringstream str;
   290     stringstream str;
   274     Info info;
   291     Info info;
   276     InfoList list;
   293     InfoList list;
   277     InfoList::const_iterator iter;
   294     InfoList::const_iterator iter;
   278     unsigned int maxAliasWidth = 0, maxPosWidth = 0,
   295     unsigned int maxAliasWidth = 0, maxPosWidth = 0,
   279                  maxSlavePosWidth = 0, maxStateWidth = 0;
   296                  maxSlavePosWidth = 0, maxStateWidth = 0;
   280     ec_ioctl_slave_t slave;
   297     ec_ioctl_slave_t slave;
       
   298     string indent(doIndent ? "  " : "");
   281 
   299 
   282     for (configIter = configList.begin();
   300     for (configIter = configList.begin();
   283             configIter != configList.end();
   301             configIter != configList.end();
   284             configIter++) {
   302             configIter++) {
   285 
   303 
   335         if (info.state.length() > maxStateWidth)
   353         if (info.state.length() > maxStateWidth)
   336             maxStateWidth = info.state.length();
   354             maxStateWidth = info.state.length();
   337     }
   355     }
   338 
   356 
   339     for (iter = list.begin(); iter != list.end(); iter++) {
   357     for (iter = list.begin(); iter != list.end(); iter++) {
   340         cout << setfill(' ') << right
   358         cout << indent << setfill(' ') << right
   341             << setw(maxAliasWidth) << iter->alias
   359             << setw(maxAliasWidth) << iter->alias
   342             << ":" << left
   360             << ":" << left
   343             << setw(maxPosWidth) << iter->pos
   361             << setw(maxPosWidth) << iter->pos
   344             << "  "
   362             << "  "
   345             << iter->ident
   363             << iter->ident