tool/CommandCrc.cpp
changeset 2670 09c55ee9a69d
parent 2669 878a657d920a
child 2672 b9cda70ec8b0
equal deleted inserted replaced
2669:878a657d920a 2670:09c55ee9a69d
    25  *
    25  *
    26  ****************************************************************************/
    26  ****************************************************************************/
    27 
    27 
    28 #include <iostream>
    28 #include <iostream>
    29 #include <iomanip>
    29 #include <iomanip>
       
    30 #include <algorithm>
    30 using namespace std;
    31 using namespace std;
    31 
    32 
    32 #include "CommandCrc.h"
    33 #include "CommandCrc.h"
    33 #include "MasterDevice.h"
    34 #include "MasterDevice.h"
    34 
    35 
    43 
    44 
    44 string CommandCrc::helpString(const string &binaryBaseName) const
    45 string CommandCrc::helpString(const string &binaryBaseName) const
    45 {
    46 {
    46     stringstream str;
    47     stringstream str;
    47 
    48 
    48     str << binaryBaseName << " " << getName()
    49     str
    49         << " [OPTIONS]" << endl
    50         << binaryBaseName << " " << getName() << endl
       
    51         << binaryBaseName << " " << getName() << " reset" << endl
    50         << endl
    52         << endl
    51         << getBriefDescription() << endl
    53         << getBriefDescription() << endl
    52         << endl
    54         << endl
    53         << "CRC - CRC Error Counter                0x300, 0x302, 0x304, 0x306"
    55         << "CRC - CRC Error Counter                0x300, 0x302, 0x304, 0x306"
    54         << endl
    56         << endl
    64 }
    66 }
    65 
    67 
    66 /****************************************************************************/
    68 /****************************************************************************/
    67 
    69 
    68 #define NUM_PORTS (4)
    70 #define NUM_PORTS (4)
       
    71 #define REG_SIZE  (20)
    69 
    72 
    70 void CommandCrc::execute(const StringVector &args)
    73 void CommandCrc::execute(const StringVector &args)
    71 {
    74 {
       
    75     bool reset = false;
       
    76 
       
    77     if (args.size() > 1) {
       
    78         stringstream err;
       
    79         err << "'" << getName() << "' takes either no or 'reset' argument!";
       
    80         throwInvalidUsageException(err);
       
    81     }
       
    82 
       
    83     if (args.size() == 1) {
       
    84         string arg = args[0];
       
    85         transform(arg.begin(), arg.end(),
       
    86                 arg.begin(), (int (*) (int)) std::tolower);
       
    87         if (arg != "reset") {
       
    88             stringstream err;
       
    89             err << "'" << getName() << "' takes either no or 'reset' argument!";
       
    90             throwInvalidUsageException(err);
       
    91         }
       
    92 
       
    93         reset = true;
       
    94     }
    72 
    95 
    73     MasterDevice m(getSingleMasterIndex());
    96     MasterDevice m(getSingleMasterIndex());
    74     m.open(MasterDevice::Read);
    97     m.open(reset ? MasterDevice::ReadWrite : MasterDevice::Read);
    75 
    98 
    76     ec_ioctl_master_t master;
    99     ec_ioctl_master_t master;
    77     m.getMaster(&master);
   100     m.getMaster(&master);
    78 
   101 
       
   102     uint8_t data[REG_SIZE];
       
   103     ec_ioctl_slave_reg_t io;
       
   104     io.emergency = 0;
       
   105     io.address = 0x0300;
       
   106     io.size = REG_SIZE;
       
   107     io.data = data;
       
   108 
       
   109     if (reset) {
       
   110         for (int j = 0; j < REG_SIZE; j++) {
       
   111             data[j] = 0x00;
       
   112         }
       
   113 
       
   114         for (unsigned int i = 0; i < master.slave_count; i++) {
       
   115 
       
   116             io.slave_position = i;
       
   117             try {
       
   118                 m.writeReg(&io);
       
   119             } catch (MasterDeviceException &e) {
       
   120                 throw e;
       
   121             }
       
   122         }
       
   123         return;
       
   124     }
    79 
   125 
    80     cout << "   |";
   126     cout << "   |";
    81     for (unsigned int port = 0; port < NUM_PORTS; port++) {
   127     for (unsigned int port = 0; port < NUM_PORTS; port++) {
    82         cout << "Port " << port << "         |";
   128         cout << "Port " << port << "         |";
    83     }
   129     }
    87     for (unsigned int port = 0; port < NUM_PORTS; port++) {
   133     for (unsigned int port = 0; port < NUM_PORTS; port++) {
    88         cout << "CRC PHY FWD LNK|";
   134         cout << "CRC PHY FWD LNK|";
    89     }
   135     }
    90     cout << endl;
   136     cout << endl;
    91 
   137 
    92     ec_ioctl_slave_reg_t io;
       
    93     io.emergency = 0;
       
    94     io.address = 0x0300;
       
    95     io.size = 20;
       
    96     io.data = new uint8_t[20];
       
    97 
       
    98     for (unsigned int i = 0; i < master.slave_count; i++) {
   138     for (unsigned int i = 0; i < master.slave_count; i++) {
    99 
   139 
   100         io.slave_position = i;
   140         io.slave_position = i;
   101         try {
   141         try {
   102             m.readReg(&io);
   142             m.readReg(&io);
   103         } catch (MasterDeviceException &e) {
   143         } catch (MasterDeviceException &e) {
   104             delete [] io.data;
       
   105             throw e;
   144             throw e;
   106         }
   145         }
   107 
   146 
   108         cout << setw(3) << i << "|";
   147         cout << setw(3) << i << "|";
   109         for (int port = 0; port < 4; port++) {
   148         for (int port = 0; port < 4; port++) {
   118         m.getSlave(&slave, i);
   157         m.getSlave(&slave, i);
   119         std::string slaveName(slave.name);
   158         std::string slaveName(slave.name);
   120         slaveName = slaveName.substr(0, 11);
   159         slaveName = slaveName.substr(0, 11);
   121         cout << slaveName << endl;
   160         cout << slaveName << endl;
   122     }
   161     }
   123 
       
   124     delete [] io.data;
       
   125 }
   162 }
   126 
   163 
   127 /*****************************************************************************/
   164 /*****************************************************************************/