fp@1142: /***************************************************************************** fp@1142: * fp@1142: * $Id$ 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@1142: #include "byteorder.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@1142: string CommandSiiRead::helpString() const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1142: str << getName() << " [OPTIONS]" << endl fp@1142: << endl fp@1142: << getBriefDescription() << endl fp@1142: << endl fp@1157: << "This command requires a single slave to be selected." << endl fp@1157: << endl fp@1142: << "Without the --verbose option, binary SII contents are" << endl fp@1142: << "output." << endl fp@1142: << endl fp@1142: << "With the --verbose option given, a textual representation" << endl fp@1142: << "of the data is output, that is separated by SII category" << endl fp@1142: << "names." << endl fp@1142: << endl fp@1142: << "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@1157: << " --verbose -v Output textual data with" << endl fp@1157: << " category names." << endl fp@1142: << endl fp@1142: << numericInfo(); fp@1142: fp@1142: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: void CommandSiiRead::execute(MasterDevice &m, 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@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@1142: try { fp@1142: m.readSii(&data); fp@1142: } catch (MasterDeviceException &e) { fp@1142: delete [] data.words; fp@1142: throw e; fp@1142: } 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@1142: categoryType = le16tocpu(*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@1142: categorySize = le16tocpu(*(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@1142: err << "SII data seem to be corrupted!"; fp@1142: throwCommandException(err); fp@1142: } fp@1142: categoryHeader += 2 + categorySize; fp@1142: categoryType = le16tocpu(*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: /*****************************************************************************/