tool/CommandMaster.cpp
changeset 1826 ec6223c3b7ec
parent 1804 742607c464c4
child 1851 a56bd34e20a1
equal deleted inserted replaced
1825:65781b048a47 1826:ec6223c3b7ec
    30 #include <iostream>
    30 #include <iostream>
    31 #include <iomanip>
    31 #include <iomanip>
    32 using namespace std;
    32 using namespace std;
    33 
    33 
    34 #include "CommandMaster.h"
    34 #include "CommandMaster.h"
       
    35 #include "MasterDevice.h"
    35 
    36 
    36 #define MAX_TIME_STR_SIZE 50
    37 #define MAX_TIME_STR_SIZE 50
    37 
    38 
    38 /*****************************************************************************/
    39 /*****************************************************************************/
    39 
    40 
    51     str << getName() << " [OPTIONS]" << endl
    52     str << getName() << " [OPTIONS]" << endl
    52         << endl
    53         << endl
    53         << getBriefDescription() << endl
    54         << getBriefDescription() << endl
    54         << endl
    55         << endl
    55         << "Command-specific options:" << endl
    56         << "Command-specific options:" << endl
    56         << "  --master -m <index>  Index of the master to use. Default: 0."
    57         << "  --master -m <indices>  Master indices. A comma-separated" << endl
       
    58         << "                         list with ranges is supported." << endl
       
    59         << "                         Example: 1,4,5,7-9. Default: - (all)."
    57         << endl << endl
    60         << endl << endl
    58         << numericInfo();
    61         << numericInfo();
    59 
    62 
    60     return str.str();
    63     return str.str();
    61 }
    64 }
    62 
    65 
    63 /****************************************************************************/
    66 /****************************************************************************/
    64 
    67 
    65 void CommandMaster::execute(MasterDevice &m, const StringVector &args)
    68 void CommandMaster::execute(const StringVector &args)
    66 {
    69 {
    67     ec_ioctl_master_t data;
    70     ec_ioctl_master_t data;
    68     stringstream err;
    71     stringstream err;
    69     unsigned int i;
    72     unsigned int i;
    70     time_t epoch;
    73     time_t epoch;
    74     if (args.size()) {
    77     if (args.size()) {
    75         err << "'" << getName() << "' takes no arguments!";
    78         err << "'" << getName() << "' takes no arguments!";
    76         throwInvalidUsageException(err);
    79         throwInvalidUsageException(err);
    77     }
    80     }
    78 
    81 
    79     m.open(MasterDevice::Read);
    82     MasterIndexList::const_iterator mi;
    80     m.getMaster(&data);
    83     for (mi = getMasterIndices().begin();
       
    84             mi != getMasterIndices().end(); mi++) {
       
    85         MasterDevice m(*mi);
       
    86         m.open(MasterDevice::Read);
       
    87         m.getMaster(&data);
    81 
    88 
    82     cout
    89         cout
    83         << "Master" << m.getIndex() << endl
    90             << "Master" << m.getIndex() << endl
    84         << "  Phase: ";
    91             << "  Phase: ";
    85 
    92 
    86     switch (data.phase) {
    93         switch (data.phase) {
    87         case 0:  cout << "Waiting for device..."; break;
    94             case 0:  cout << "Waiting for device..."; break;
    88         case 1:  cout << "Idle"; break;
    95             case 1:  cout << "Idle"; break;
    89         case 2:  cout << "Operation"; break;
    96             case 2:  cout << "Operation"; break;
    90         default: cout << "???";
    97             default: cout << "???";
       
    98         }
       
    99 
       
   100         cout << endl
       
   101             << "  Active: " << (data.active ? "yes" : "no") << endl
       
   102             << "  Slaves: " << data.slave_count << endl
       
   103             << "  Ethernet devices:" << endl;
       
   104 
       
   105         for (i = 0; i < 2; i++) {
       
   106             cout << "    " << (i == 0 ? "Main" : "Backup") << ": ";
       
   107             if (data.devices[i].address[0] == 0x00
       
   108                     && data.devices[i].address[1] == 0x00
       
   109                     && data.devices[i].address[2] == 0x00
       
   110                     && data.devices[i].address[3] == 0x00
       
   111                     && data.devices[i].address[4] == 0x00
       
   112                     && data.devices[i].address[5] == 0x00) {
       
   113                 cout << "None.";
       
   114             } else {
       
   115                 cout << hex << setfill('0')
       
   116                     << setw(2) << (unsigned int) data.devices[i].address[0]
       
   117                     << ":"
       
   118                     << setw(2) << (unsigned int) data.devices[i].address[1]
       
   119                     << ":"
       
   120                     << setw(2) << (unsigned int) data.devices[i].address[2]
       
   121                     << ":"
       
   122                     << setw(2) << (unsigned int) data.devices[i].address[3]
       
   123                     << ":"
       
   124                     << setw(2) << (unsigned int) data.devices[i].address[4]
       
   125                     << ":"
       
   126                     << setw(2) << (unsigned int) data.devices[i].address[5]
       
   127                     << " ("
       
   128                     << (data.devices[i].attached ? "attached" : "waiting...")
       
   129                     << ")" << endl << dec
       
   130                     << "      Link: "
       
   131                     << (data.devices[i].link_state ? "UP" : "DOWN") << endl
       
   132                     << "      Tx count: " << data.devices[i].tx_count << endl
       
   133                     << "      Rx count: " << data.devices[i].rx_count;
       
   134             }
       
   135             cout << endl;
       
   136         }
       
   137 
       
   138         cout << "  Distributed clocks:" << endl
       
   139             << "    Reference clock: ";
       
   140         if (data.ref_clock != 0xffff) {
       
   141             cout << "Slave " << dec << data.ref_clock;
       
   142         } else {
       
   143             cout << "None";
       
   144         }
       
   145         cout << endl
       
   146             << "    Application time: " << data.app_time << endl
       
   147             << "                      ";
       
   148 
       
   149         epoch = data.app_time / 1000000000 + 946684800ULL;
       
   150         time_str_size = strftime(time_str, MAX_TIME_STR_SIZE,
       
   151                 "%Y-%m-%d %H:%M:%S", gmtime(&epoch));
       
   152         cout << string(time_str, time_str_size) << "."
       
   153             << setfill('0') << setw(9) << data.app_time % 1000000000 << endl;
    91     }
   154     }
    92 
       
    93     cout << endl
       
    94         << "  Active: " << (data.active ? "yes" : "no") << endl
       
    95         << "  Slaves: " << data.slave_count << endl
       
    96         << "  Ethernet devices:" << endl;
       
    97 
       
    98     for (i = 0; i < 2; i++) {
       
    99         cout << "    " << (i == 0 ? "Main" : "Backup") << ": ";
       
   100         if (data.devices[i].address[0] == 0x00
       
   101                 && data.devices[i].address[1] == 0x00
       
   102                 && data.devices[i].address[2] == 0x00
       
   103                 && data.devices[i].address[3] == 0x00
       
   104                 && data.devices[i].address[4] == 0x00
       
   105                 && data.devices[i].address[5] == 0x00) {
       
   106             cout << "None.";
       
   107         } else {
       
   108             cout << hex << setfill('0')
       
   109                 << setw(2) << (unsigned int) data.devices[i].address[0] << ":"
       
   110                 << setw(2) << (unsigned int) data.devices[i].address[1] << ":"
       
   111                 << setw(2) << (unsigned int) data.devices[i].address[2] << ":"
       
   112                 << setw(2) << (unsigned int) data.devices[i].address[3] << ":"
       
   113                 << setw(2) << (unsigned int) data.devices[i].address[4] << ":"
       
   114                 << setw(2) << (unsigned int) data.devices[i].address[5] << " ("
       
   115                 << (data.devices[i].attached ? "attached" : "waiting...")
       
   116                 << ")" << endl << dec
       
   117                 << "      Link: " << (data.devices[i].link_state ? "UP" : "DOWN") << endl
       
   118                 << "      Tx count: " << data.devices[i].tx_count << endl
       
   119                 << "      Rx count: " << data.devices[i].rx_count;
       
   120         }
       
   121         cout << endl;
       
   122     }
       
   123 
       
   124     cout << "  Distributed clocks:" << endl
       
   125         << "    Reference clock: ";
       
   126     if (data.ref_clock != 0xffff) {
       
   127         cout << "Slave " << dec << data.ref_clock;
       
   128     } else {
       
   129         cout << "None";
       
   130     }
       
   131     cout << endl
       
   132         << "    Application time: " << data.app_time << endl
       
   133         << "                      ";
       
   134 
       
   135     epoch = data.app_time / 1000000000 + 946684800ULL;
       
   136     time_str_size = strftime(time_str, MAX_TIME_STR_SIZE,
       
   137             "%Y-%m-%d %H:%M:%S", gmtime(&epoch));
       
   138     cout << string(time_str, time_str_size) << "."
       
   139         << setfill('0') << setw(9) << data.app_time % 1000000000 << endl;
       
   140 }
   155 }
   141 
   156 
   142 /*****************************************************************************/
   157 /*****************************************************************************/