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 "CommandSiiRead.h" fp@1826: #include "MasterDevice.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandSiiRead::CommandSiiRead(): fp@1142: Command("sii_read", "Output a slave's SII contents.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1968: string CommandSiiRead::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@1157: << "This command requires a single slave to be selected." << endl fp@1804: << endl fp@1804: << "Without the --verbose option, binary SII contents are" << endl fp@1804: << "output." << endl fp@1804: << endl fp@1804: << "With the --verbose option given, a textual representation" << endl fp@1804: << "of the data is output, that is separated by SII category" << endl fp@1804: << "names." << endl fp@1804: << endl fp@1804: << "Command-specific options:" << endl fp@1157: << " --alias -a " << endl fp@1157: << " --position -p Slave selection. See the help of" << endl fp@1157: << " the 'slaves' command." << endl fp@1804: << " --verbose -v Output textual data with" << endl fp@1804: << " category names." << endl fp@1804: << endl fp@1804: << numericInfo(); fp@1804: fp@1804: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1826: void CommandSiiRead::execute(const StringVector &args) fp@1142: { fp@1151: SlaveList slaves; fp@1151: ec_ioctl_slave_t *slave; fp@1142: ec_ioctl_slave_sii_t data; fp@1142: unsigned int i; fp@1142: const uint16_t *categoryHeader; fp@1142: uint16_t categoryType, categorySize; fp@1142: stringstream err; fp@1142: fp@1373: if (args.size()) { fp@1373: err << "'" << getName() << "' takes no arguments!"; fp@1373: throwInvalidUsageException(err); fp@1373: } fp@1373: fp@1870: MasterDevice m(getSingleMasterIndex()); fp@1151: m.open(MasterDevice::Read); fp@1151: slaves = selectedSlaves(m); fp@1151: fp@1151: if (slaves.size() != 1) { fp@1155: throwSingleSlaveRequired(slaves.size()); fp@1142: } fp@1151: slave = &slaves.front(); fp@1151: data.slave_position = slave->position; fp@1142: fp@1151: if (!slave->sii_nwords) fp@1142: return; fp@1142: fp@1142: data.offset = 0; fp@1151: data.nwords = slave->sii_nwords; fp@1142: data.words = new uint16_t[data.nwords]; fp@1142: fp@1804: try { fp@1804: m.readSii(&data); fp@1804: } catch (MasterDeviceException &e) { fp@1142: delete [] data.words; fp@1804: throw e; fp@1804: } fp@1142: fp@1142: if (getVerbosity() == Verbose) { fp@1142: cout << "SII Area:" << hex << setfill('0'); fp@1142: for (i = 0; i < min(data.nwords, 0x0040U) * 2; i++) { fp@1142: if (i % BreakAfterBytes) { fp@1142: cout << " "; fp@1142: } else { fp@1142: cout << endl << " "; fp@1142: } fp@1142: cout << setw(2) << (unsigned int) *((uint8_t *) data.words + i); fp@1142: } fp@1142: cout << endl; fp@1142: fp@1142: if (data.nwords > 0x0040U) { fp@1142: // cycle through categories fp@1142: categoryHeader = data.words + 0x0040U; fp@1254: categoryType = le16_to_cpup(categoryHeader); fp@1142: while (categoryType != 0xffff) { fp@1142: cout << "SII Category 0x" << hex fp@1142: << setw(4) << categoryType fp@1142: << " (" << getCategoryName(categoryType) << ")" << flush; fp@1142: fp@1142: if (categoryHeader + 1 > data.words + data.nwords) { fp@1142: err << "SII data seem to be corrupted!"; fp@1142: throwCommandException(err); fp@1142: } fp@1254: categorySize = le16_to_cpup(categoryHeader + 1); fp@1142: cout << ", " << dec << categorySize << " words" << flush; fp@1142: fp@1142: if (categoryHeader + 2 + categorySize fp@1142: > data.words + data.nwords) { fp@1142: err << "SII data seem to be corrupted!"; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: cout << hex; fp@1142: for (i = 0; i < categorySize * 2U; i++) { fp@1142: if (i % BreakAfterBytes) { fp@1142: cout << " "; fp@1142: } else { fp@1142: cout << endl << " "; fp@1142: } fp@1142: cout << setw(2) << (unsigned int) fp@1142: *((uint8_t *) (categoryHeader + 2) + i); fp@1142: } fp@1142: cout << endl; fp@1142: fp@1142: if (categoryHeader + 2 + categorySize + 1 fp@1142: > data.words + data.nwords) { fp@2421: err << "SII data seem to be corrupted!"; fp@1142: throwCommandException(err); fp@1142: } fp@1142: categoryHeader += 2 + categorySize; fp@1254: categoryType = le16_to_cpup(categoryHeader); fp@1142: } fp@1142: } fp@1142: } else { fp@1142: for (i = 0; i < data.nwords; i++) { fp@1142: uint16_t *w = data.words + i; fp@1142: cout << *(uint8_t *) w << *((uint8_t *) w + 1); fp@1142: } fp@1142: } fp@1142: fp@1142: delete [] data.words; fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: const CommandSiiRead::CategoryName CommandSiiRead::categoryNames[] = { fp@1142: {0x000a, "STRINGS"}, fp@1142: {0x0014, "DataTypes"}, fp@1142: {0x001e, "General"}, fp@1142: {0x0028, "FMMU"}, fp@1142: {0x0029, "SyncM"}, fp@1142: {0x0032, "TXPDO"}, fp@1142: {0x0033, "RXPDO"}, fp@1142: {0x003c, "DC"}, fp@1142: {} fp@1142: }; fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: const char *CommandSiiRead::getCategoryName(uint16_t type) fp@1142: { fp@1142: const CategoryName *cn = categoryNames; fp@1142: fp@1142: while (cn->type) { fp@1142: if (cn->type == type) { fp@1142: return cn->name; fp@1142: } fp@1142: cn++; fp@1142: } fp@1142: fp@1142: return "unknown"; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/