fp@1142: /***************************************************************************** fp@1142: * fp@1363: * $Id$ fp@1363: * fp@1363: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1363: * fp@1363: * This file is part of the IgH EtherCAT Master. fp@1363: * fp@1363: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1363: * modify it under the terms of the GNU General Public License version 2, as fp@1363: * published by the Free Software Foundation. fp@1363: * fp@1363: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1363: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1363: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1363: * Public License for more details. fp@1363: * fp@1363: * You should have received a copy of the GNU General Public License along fp@1363: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1363: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1363: * fp@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1142: * fp@1142: ****************************************************************************/ fp@1142: fp@1142: #include fp@1142: #include fp@1142: using namespace std; fp@1142: fp@1142: #include "CommandMaster.h" fp@1826: #include "MasterDevice.h" fp@1142: fp@1415: #define MAX_TIME_STR_SIZE 50 fp@1415: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandMaster::CommandMaster(): fp@1143: Command("master", "Show master and Ethernet device information.") fp@1142: { fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: string CommandMaster::helpString() const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1804: str << getName() << " [OPTIONS]" << endl fp@1804: << endl fp@1804: << getBriefDescription() << endl fp@1804: << endl fp@1804: << "Command-specific options:" << endl fp@1826: << " --master -m Master indices. A comma-separated" << endl fp@1826: << " list with ranges is supported." << endl fp@1826: << " Example: 1,4,5,7-9. Default: - (all)." fp@1804: << endl << endl fp@1804: << numericInfo(); fp@1142: fp@1804: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1826: void CommandMaster::execute(const StringVector &args) fp@1142: { fp@1142: ec_ioctl_master_t data; fp@1142: stringstream err; fp@1851: unsigned int i, j; fp@1415: time_t epoch; fp@1415: char time_str[MAX_TIME_STR_SIZE + 1]; fp@1415: size_t time_str_size; fp@1142: fp@1373: if (args.size()) { fp@1373: err << "'" << getName() << "' takes no arguments!"; fp@1373: throwInvalidUsageException(err); fp@1373: } fp@1373: fp@1826: MasterIndexList::const_iterator mi; fp@1826: for (mi = getMasterIndices().begin(); fp@1826: mi != getMasterIndices().end(); mi++) { fp@1826: MasterDevice m(*mi); fp@1826: m.open(MasterDevice::Read); fp@1826: m.getMaster(&data); fp@1142: fp@1826: cout fp@1826: << "Master" << m.getIndex() << endl fp@1826: << " Phase: "; fp@1142: fp@1826: switch (data.phase) { fp@1826: case 0: cout << "Waiting for device..."; break; fp@1826: case 1: cout << "Idle"; break; fp@1826: case 2: cout << "Operation"; break; fp@1826: default: cout << "???"; fp@1826: } fp@1826: fp@1826: cout << endl fp@1826: << " Active: " << (data.active ? "yes" : "no") << endl fp@1826: << " Slaves: " << data.slave_count << endl fp@1826: << " Ethernet devices:" << endl; fp@1826: fp@1826: for (i = 0; i < 2; i++) { fp@1826: cout << " " << (i == 0 ? "Main" : "Backup") << ": "; fp@1826: if (data.devices[i].address[0] == 0x00 fp@1826: && data.devices[i].address[1] == 0x00 fp@1826: && data.devices[i].address[2] == 0x00 fp@1826: && data.devices[i].address[3] == 0x00 fp@1826: && data.devices[i].address[4] == 0x00 fp@1826: && data.devices[i].address[5] == 0x00) { fp@1826: cout << "None."; fp@1826: } else { fp@1858: unsigned int lost = fp@1858: data.devices[i].tx_count - data.devices[i].rx_count; fp@1858: if (lost == 1) { fp@1858: // allow one frame travelling fp@1858: lost = 0; fp@1858: } fp@1826: cout << hex << setfill('0') fp@1826: << setw(2) << (unsigned int) data.devices[i].address[0] fp@1826: << ":" fp@1826: << setw(2) << (unsigned int) data.devices[i].address[1] fp@1826: << ":" fp@1826: << setw(2) << (unsigned int) data.devices[i].address[2] fp@1826: << ":" fp@1826: << setw(2) << (unsigned int) data.devices[i].address[3] fp@1826: << ":" fp@1826: << setw(2) << (unsigned int) data.devices[i].address[4] fp@1826: << ":" fp@1826: << setw(2) << (unsigned int) data.devices[i].address[5] fp@1826: << " (" fp@1826: << (data.devices[i].attached ? "attached" : "waiting...") fp@1826: << ")" << endl << dec fp@1826: << " Link: " fp@1826: << (data.devices[i].link_state ? "UP" : "DOWN") << endl fp@1858: << " Tx frames: " fp@1858: << data.devices[i].tx_count << endl fp@1858: << " Rx frames: " fp@1858: << data.devices[i].rx_count << endl fp@1858: << " Lost frames: " << lost << endl fp@1858: << " Tx bytes: " fp@1858: << data.devices[i].tx_bytes << endl fp@1858: << " Tx errors: " fp@1858: << data.devices[i].tx_errors << endl fp@1857: << " Tx frame rate [1/s]: " fp@1853: << setfill(' ') << setprecision(0) << fixed; fp@1851: for (j = 0; j < EC_RATE_COUNT; j++) { fp@1857: cout << fp@1857: setw(5) << data.devices[i].tx_frame_rates[j] / 1000.0; fp@1851: if (j < EC_RATE_COUNT - 1) { fp@1851: cout << " "; fp@1851: } fp@1851: } fp@1851: cout << endl fp@1857: << " Tx rate [KByte/s]: " fp@1858: << setprecision(1) << fixed; fp@1858: for (j = 0; j < EC_RATE_COUNT; j++) { fp@1858: cout << setw(5) fp@1858: << data.devices[i].tx_byte_rates[j] / 1024000.0; fp@1857: if (j < EC_RATE_COUNT - 1) { fp@1857: cout << " "; fp@1857: } fp@1857: } fp@1857: cout << endl fp@1857: << " Loss rate [1/s]: " fp@1857: << setprecision(0) << fixed; fp@1857: for (j = 0; j < EC_RATE_COUNT; j++) { fp@1853: cout << setw(5) << data.devices[i].loss_rates[j] / 1000.0; fp@1851: if (j < EC_RATE_COUNT - 1) { fp@1851: cout << " "; fp@1851: } fp@1851: } fp@1851: cout << endl fp@1857: << " Frame loss [%]: " fp@1853: << setprecision(1) << fixed; fp@1851: for (j = 0; j < EC_RATE_COUNT; j++) { fp@1851: double perc = 0.0; fp@1857: if (data.devices[i].tx_frame_rates[j]) { fp@1851: perc = 100.0 * data.devices[i].loss_rates[j] / fp@1857: data.devices[i].tx_frame_rates[j]; fp@1851: } fp@1853: cout << setw(5) << perc; fp@1851: if (j < EC_RATE_COUNT - 1) { fp@1851: cout << " "; fp@1851: } fp@1851: } fp@1851: cout << setprecision(0) << endl; fp@1826: } fp@1826: cout << endl; fp@1826: } fp@1826: fp@1826: cout << " Distributed clocks:" << endl fp@1826: << " Reference clock: "; fp@1826: if (data.ref_clock != 0xffff) { fp@1826: cout << "Slave " << dec << data.ref_clock; fp@1826: } else { fp@1826: cout << "None"; fp@1826: } fp@1826: cout << endl fp@1826: << " Application time: " << data.app_time << endl fp@1826: << " "; fp@1826: fp@1826: epoch = data.app_time / 1000000000 + 946684800ULL; fp@1826: time_str_size = strftime(time_str, MAX_TIME_STR_SIZE, fp@1826: "%Y-%m-%d %H:%M:%S", gmtime(&epoch)); fp@1826: cout << string(time_str, time_str_size) << "." fp@1826: << setfill('0') << setw(9) << data.app_time % 1000000000 << endl; fp@1142: } fp@1142: } fp@1142: fp@1142: /*****************************************************************************/