fp@1122: /***************************************************************************** fp@1122: * fp@1122: * $Id$ fp@1122: * fp@1122: ****************************************************************************/ fp@1122: fp@1122: #include fp@1122: #include fp@1122: #include fp@1122: using namespace std; fp@1122: fp@1122: #include "globals.h" fp@1126: #include "sii_crc.h" fp@1122: fp@1122: /*****************************************************************************/ fp@1122: fp@1122: const char *help_alias = fp@1122: "[OPTIONS] \n" fp@1122: "\n" fp@1122: "Write the secondary slave address (alias) for either\n" fp@1122: "one or for multiple slaves.\n" fp@1122: "\n" fp@1122: "Arguments:\n" fp@1137: " ALIAS must be an unsigned 16 bit number. Zero means no alias.\n" fp@1122: "\n" fp@1122: "Command-specific options:\n" fp@1137: " --slave -s Positive numerical ring position, or 'all' for\n" fp@1137: " all slaves (default). The --force option is\n" fp@1137: " required in this case.\n" fp@1137: " --force Acknowledge writing aliases of all slaves.\n" fp@1137: "\n" fp@1137: "Numerical values can be specified either with decimal (no prefix),\n" fp@1137: "octal (prefix '0') or hexadecimal (prefix '0x') base.\n"; fp@1122: fp@1122: /*****************************************************************************/ fp@1122: fp@1122: void writeSlaveAlias(uint16_t, uint16_t); fp@1122: fp@1122: /*****************************************************************************/ fp@1122: fp@1122: /** Writes the Secondary slave address (alias) to the slave's SII. fp@1122: */ fp@1122: void command_alias(void) fp@1122: { fp@1122: uint16_t alias; fp@1122: stringstream err, strAlias; fp@1122: int number; fp@1122: unsigned int numSlaves, i; fp@1122: fp@1122: if (commandArgs.size() != 1) { fp@1125: err << "'" << commandName << "' takes exactly one argument!"; fp@1122: throw InvalidUsageException(err); fp@1122: } fp@1122: fp@1122: strAlias << commandArgs[0]; fp@1122: strAlias fp@1122: >> resetiosflags(ios::basefield) // guess base from prefix fp@1122: >> number; fp@1122: if (strAlias.fail() || number < 0x0000 || number > 0xffff) { fp@1122: err << "Invalid alias '" << commandArgs[0] << "'!"; fp@1122: throw InvalidUsageException(err); fp@1122: } fp@1122: alias = number; fp@1122: fp@1122: if (slavePosition == -1) { fp@1122: if (!force) { fp@1122: err << "This will write the alias addresses of all slaves to " fp@1122: << alias << "! Please specify --force to proceed."; fp@1136: throw CommandException(err); fp@1122: } fp@1122: fp@1122: masterDev.open(MasterDevice::ReadWrite); fp@1122: numSlaves = masterDev.slaveCount(); fp@1122: fp@1122: for (i = 0; i < numSlaves; i++) { fp@1122: writeSlaveAlias(i, alias); fp@1122: } fp@1122: } else { fp@1122: masterDev.open(MasterDevice::ReadWrite); fp@1122: writeSlaveAlias(slavePosition, alias); fp@1122: } fp@1122: } fp@1122: fp@1122: /*****************************************************************************/ fp@1122: fp@1122: /** Writes the Secondary slave address (alias) to the slave's SII. fp@1122: */ fp@1122: void writeSlaveAlias( fp@1122: uint16_t slavePosition, fp@1122: uint16_t alias fp@1122: ) fp@1122: { fp@1122: ec_ioctl_slave_sii_t data; fp@1122: ec_ioctl_slave_t slave; fp@1122: stringstream err; fp@1122: uint8_t crc; fp@1122: fp@1122: masterDev.getSlave(&slave, slavePosition); fp@1122: fp@1122: if (slave.sii_nwords < 8) { fp@1122: err << "Current SII contents are too small to set an alias " fp@1122: << "(" << slave.sii_nwords << " words)!"; fp@1136: throw CommandException(err); fp@1122: } fp@1122: fp@1122: // read first 8 SII words fp@1122: data.slave_position = slavePosition; fp@1122: data.offset = 0; fp@1122: data.nwords = 8; fp@1122: data.words = new uint16_t[data.nwords]; fp@1122: fp@1122: try { fp@1122: masterDev.readSii(&data); fp@1122: } catch (MasterDeviceException &e) { fp@1122: delete [] data.words; fp@1122: err << "Failed to read SII: " << e.what(); fp@1136: throw CommandException(err); fp@1122: } fp@1122: fp@1122: // write new alias address in word 4 fp@1122: data.words[4] = cputole16(alias); fp@1122: fp@1122: // calculate checksum over words 0 to 6 fp@1122: crc = calcSiiCrc((const uint8_t *) data.words, 14); fp@1122: fp@1122: // write new checksum into first byte of word 7 fp@1122: *(uint8_t *) (data.words + 7) = crc; fp@1122: fp@1122: // write first 8 words with new alias and checksum fp@1122: try { fp@1122: masterDev.writeSii(&data); fp@1122: } catch (MasterDeviceException &e) { fp@1122: delete [] data.words; fp@1122: err << "Failed to read SII: " << e.what(); fp@1136: throw CommandException(err); fp@1122: } fp@1122: fp@1122: delete [] data.words; fp@1122: } fp@1122: fp@1122: /*****************************************************************************/