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@1968: string CommandMaster::helpString(const string &binaryBaseName) const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1968: str << binaryBaseName << " " << 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@1869: MasterIndexList masterIndices; fp@1142: ec_ioctl_master_t data; fp@1142: stringstream err; fp@2453: unsigned int dev_idx, j; fp@1415: time_t epoch; fp@1415: char time_str[MAX_TIME_STR_SIZE + 1]; fp@1415: size_t time_str_size; fp@2421: fp@1373: if (args.size()) { fp@1373: err << "'" << getName() << "' takes no arguments!"; fp@1373: throwInvalidUsageException(err); fp@1373: } fp@1373: fp@1869: masterIndices = getMasterIndices(); fp@1826: MasterIndexList::const_iterator mi; fp@1869: for (mi = masterIndices.begin(); fp@1869: mi != masterIndices.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@2156: case 0: cout << "Waiting for device(s)..."; 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@2453: for (dev_idx = EC_DEVICE_MAIN; dev_idx < data.num_devices; fp@2453: dev_idx++) { fp@2453: cout << " " << (dev_idx == EC_DEVICE_MAIN ? "Main" : "Backup") fp@2453: << ": "; fp@2453: cout << hex << setfill('0') fp@2453: << setw(2) << (unsigned int) data.devices[dev_idx].address[0] fp@2453: << ":" fp@2453: << setw(2) << (unsigned int) data.devices[dev_idx].address[1] fp@2453: << ":" fp@2453: << setw(2) << (unsigned int) data.devices[dev_idx].address[2] fp@2453: << ":" fp@2453: << setw(2) << (unsigned int) data.devices[dev_idx].address[3] fp@2453: << ":" fp@2453: << setw(2) << (unsigned int) data.devices[dev_idx].address[4] fp@2453: << ":" fp@2453: << setw(2) << (unsigned int) data.devices[dev_idx].address[5] fp@2453: << " (" fp@2453: << (data.devices[dev_idx].attached ? fp@2453: "attached" : "waiting...") fp@2453: << ")" << endl << dec fp@2453: << " Link: " fp@2453: << (data.devices[dev_idx].link_state ? "UP" : "DOWN") << endl fp@2453: << " Tx frames: " fp@2453: << data.devices[dev_idx].tx_count << endl fp@2453: << " Tx bytes: " fp@2453: << data.devices[dev_idx].tx_bytes << endl fp@2453: << " Rx frames: " fp@2453: << data.devices[dev_idx].rx_count << endl fp@2453: << " Rx bytes: " fp@2453: << data.devices[dev_idx].rx_bytes << endl fp@2453: << " Tx errors: " fp@2453: << data.devices[dev_idx].tx_errors << endl fp@2453: << " Tx frame rate [1/s]: " fp@2453: << setfill(' ') << setprecision(0) << fixed; fp@2453: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2453: cout << setw(ColWidth) fp@2453: << data.devices[dev_idx].tx_frame_rates[j] / 1000.0; fp@2453: if (j < EC_RATE_COUNT - 1) { fp@2453: cout << " "; fp@2453: } fp@2453: } fp@2453: cout << endl fp@2453: << " Tx rate [KByte/s]: " fp@2453: << setprecision(1) << fixed; fp@2453: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2453: cout << setw(ColWidth) fp@2453: << data.devices[dev_idx].tx_byte_rates[j] / 1024.0; fp@2453: if (j < EC_RATE_COUNT - 1) { fp@2453: cout << " "; fp@2453: } fp@2453: } fp@2453: cout << endl fp@2453: << " Rx frame rate [1/s]: " fp@2453: << setfill(' ') << setprecision(0) << fixed; fp@2453: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2453: cout << setw(ColWidth) fp@2453: << data.devices[dev_idx].rx_frame_rates[j] / 1000.0; fp@2453: if (j < EC_RATE_COUNT - 1) { fp@2453: cout << " "; fp@2453: } fp@2453: } fp@2453: cout << endl fp@2453: << " Rx rate [KByte/s]: " fp@2453: << setprecision(1) << fixed; fp@2453: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2453: cout << setw(ColWidth) fp@2453: << data.devices[dev_idx].rx_byte_rates[j] / 1024.0; fp@2453: if (j < EC_RATE_COUNT - 1) { fp@2453: cout << " "; fp@2453: } fp@2453: } fp@2453: cout << setprecision(0) << endl; fp@2158: } fp@2158: unsigned int lost = data.tx_count - data.rx_count; fp@2158: if (lost == 1) { fp@2158: // allow one frame travelling fp@2158: lost = 0; fp@2158: } fp@2158: cout << " Common:" << endl fp@2158: << " Tx frames: " fp@2158: << data.tx_count << endl fp@2158: << " Tx bytes: " fp@2158: << data.tx_bytes << endl fp@2158: << " Rx frames: " fp@2158: << data.rx_count << endl fp@2158: << " Rx bytes: " fp@2158: << data.rx_bytes << endl fp@2158: << " Lost frames: " << lost << endl fp@2158: << " Tx frame rate [1/s]: " fp@2158: << setfill(' ') << setprecision(0) << fixed; fp@2158: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2158: cout << setw(ColWidth) fp@2158: << data.tx_frame_rates[j] / 1000.0; fp@2158: if (j < EC_RATE_COUNT - 1) { fp@2158: cout << " "; fp@2158: } fp@2158: } fp@2158: cout << endl fp@2158: << " Tx rate [KByte/s]: " fp@2158: << setprecision(1) << fixed; fp@2158: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2158: cout << setw(ColWidth) fp@2158: << data.tx_byte_rates[j] / 1024.0; fp@2158: if (j < EC_RATE_COUNT - 1) { fp@2158: cout << " "; fp@2158: } fp@2158: } fp@2158: cout << endl fp@2158: << " Rx frame rate [1/s]: " fp@2158: << setfill(' ') << setprecision(0) << fixed; fp@2158: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2158: cout << setw(ColWidth) fp@2158: << data.rx_frame_rates[j] / 1000.0; fp@2158: if (j < EC_RATE_COUNT - 1) { fp@2158: cout << " "; fp@2158: } fp@2158: } fp@2158: cout << endl fp@2158: << " Rx rate [KByte/s]: " fp@2158: << setprecision(1) << fixed; fp@2158: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2158: cout << setw(ColWidth) fp@2158: << data.rx_byte_rates[j] / 1024.0; fp@2158: if (j < EC_RATE_COUNT - 1) { fp@2158: cout << " "; fp@2158: } fp@2158: } fp@2158: cout << endl fp@2158: << " Loss rate [1/s]: " fp@2158: << setprecision(0) << fixed; fp@2158: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2158: cout << setw(ColWidth) fp@2158: << data.loss_rates[j] / 1000.0; fp@2158: if (j < EC_RATE_COUNT - 1) { fp@2158: cout << " "; fp@2158: } fp@2158: } fp@2158: cout << endl fp@2158: << " Frame loss [%]: " fp@2158: << setprecision(1) << fixed; fp@2158: for (j = 0; j < EC_RATE_COUNT; j++) { fp@2158: double perc = 0.0; fp@2158: if (data.tx_frame_rates[j]) { fp@2158: perc = 100.0 * data.loss_rates[j] / data.tx_frame_rates[j]; fp@2158: } fp@2158: cout << setw(ColWidth) << perc; fp@2158: if (j < EC_RATE_COUNT - 1) { fp@2158: cout << " "; fp@2158: } fp@2158: } fp@2158: cout << setprecision(0) << endl; 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: /*****************************************************************************/