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 "CommandDownload.h" fp@1142: #include "coe_datatypes.h" fp@1142: #include "byteorder.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandDownload::CommandDownload(): fp@1142: Command("download", "Write an Sdo entry to a slave.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: string CommandDownload::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@1142: << "The data type of the Sdo entry is taken from the Sdo" << endl fp@1142: << "dictionary by default. It can be overridden with the" << endl fp@1142: << "--type option. If the slave does not support the Sdo" << endl fp@1142: << "information service or the Sdo is not in the dictionary," << endl fp@1142: << "the --type option is mandatory." << endl fp@1142: << endl fp@1142: << "These are the valid Sdo entry data types:" << endl fp@1142: << " int8, int16, int32, uint8, uint16, uint32, string." << endl fp@1142: << endl fp@1142: << "Arguments:" fp@1142: << " INDEX is the Sdo index and must be an unsigned" << endl fp@1142: << " 16 bit number." << endl fp@1142: << " SUBINDEX is the Sdo entry subindex and must be an" << endl fp@1142: << " unsigned 8 bit number." << endl fp@1142: << " VALUE is the value to download and must correspond" << endl fp@1142: << " to the Sdo entry datatype (see above)." << endl fp@1142: << endl fp@1142: << "Command-specific options:" << endl fp@1142: << " --slave -s Positive numerical ring position" << endl fp@1142: << " (mandatory)." << endl fp@1142: << " --type -t Forced Sdo entry data type (see" << endl fp@1142: << " above)." << 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 CommandDownload::execute(MasterDevice &m, const StringVector &args) fp@1142: { fp@1142: stringstream strIndex, strSubIndex, strValue, err; fp@1142: ec_ioctl_slave_sdo_download_t data; fp@1142: unsigned int number; fp@1142: const CoEDataType *dataType = NULL; fp@1142: fp@1142: if (slavePosition < 0) { fp@1142: err << "'" << getName() << "' requires a slave! " fp@1142: << "Please specify --slave."; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: data.slave_position = slavePosition; fp@1142: fp@1142: if (args.size() != 3) { fp@1142: err << "'" << getName() << "' takes 3 arguments!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: fp@1142: strIndex << args[0]; fp@1142: strIndex fp@1142: >> resetiosflags(ios::basefield) // guess base from prefix fp@1142: >> data.sdo_index; fp@1142: if (strIndex.fail()) { fp@1142: err << "Invalid Sdo index '" << args[0] << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: fp@1142: strSubIndex << args[1]; fp@1142: strSubIndex fp@1142: >> resetiosflags(ios::basefield) // guess base from prefix fp@1142: >> number; fp@1142: if (strSubIndex.fail() || number > 0xff) { fp@1142: err << "Invalid Sdo subindex '" << args[1] << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: data.sdo_entry_subindex = number; fp@1142: fp@1142: if (dataTypeStr != "") { // data type specified fp@1142: if (!(dataType = findDataType(dataTypeStr))) { fp@1142: err << "Invalid data type '" << dataTypeStr << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: } else { // no data type specified: fetch from dictionary fp@1142: ec_ioctl_slave_sdo_entry_t entry; fp@1142: fp@1142: m.open(MasterDevice::ReadWrite); fp@1142: fp@1142: try { fp@1142: m.getSdoEntry(&entry, slavePosition, fp@1142: data.sdo_index, data.sdo_entry_subindex); fp@1142: } catch (MasterDeviceException &e) { fp@1142: err << "Failed to determine Sdo entry data type. " fp@1142: << "Please specify --type."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: if (!(dataType = findDataType(entry.data_type))) { fp@1142: err << "Pdo entry has unknown data type 0x" fp@1142: << hex << setfill('0') << setw(4) << entry.data_type << "!" fp@1142: << " Please specify --type."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: } fp@1142: fp@1142: if (dataType->byteSize) { fp@1142: data.data_size = dataType->byteSize; fp@1142: } else { fp@1142: data.data_size = DefaultBufferSize; fp@1142: } fp@1142: fp@1142: data.data = new uint8_t[data.data_size + 1]; fp@1142: fp@1142: strValue << args[2]; fp@1142: strValue >> resetiosflags(ios::basefield); // guess base from prefix fp@1142: strValue.exceptions(ios::failbit); fp@1142: fp@1142: try { fp@1142: switch (dataType->coeCode) { fp@1142: case 0x0002: // int8 fp@1142: { fp@1142: int16_t val; // uint8_t is interpreted as char fp@1142: strValue >> val; fp@1142: if (val > 127 || val < -128) fp@1142: throw ios::failure("Value out of range"); fp@1142: *data.data = val; fp@1142: break; fp@1142: } fp@1142: case 0x0003: // int16 fp@1142: { fp@1142: int16_t val; fp@1142: strValue >> val; fp@1142: *(int16_t *) data.data = cputole16(val); fp@1142: break; fp@1142: } fp@1142: case 0x0004: // int32 fp@1142: { fp@1142: int32_t val; fp@1142: strValue >> val; fp@1142: *(int32_t *) data.data = cputole32(val); fp@1142: break; fp@1142: } fp@1142: case 0x0005: // uint8 fp@1142: { fp@1142: uint16_t val; // uint8_t is interpreted as char fp@1142: strValue >> val; fp@1142: if (val > 0xff) fp@1142: throw ios::failure("Value out of range"); fp@1142: *data.data = val; fp@1142: break; fp@1142: } fp@1142: case 0x0006: // uint16 fp@1142: { fp@1142: uint16_t val; fp@1142: strValue >> val; fp@1142: *(uint16_t *) data.data = cputole16(val); fp@1142: break; fp@1142: } fp@1142: case 0x0007: // uint32 fp@1142: { fp@1142: uint32_t val; fp@1142: strValue >> val; fp@1142: *(uint32_t *) data.data = cputole32(val); fp@1142: break; fp@1142: } fp@1142: case 0x0009: // string fp@1142: if (strValue.str().size() >= data.data_size) { fp@1142: err << "String too large"; fp@1142: throwCommandException(err); fp@1142: } fp@1142: data.data_size = strValue.str().size(); fp@1142: strValue >> (char *) data.data; fp@1142: break; fp@1142: fp@1142: default: fp@1142: delete [] data.data; fp@1142: err << "Unknown data type 0x" << hex << dataType->coeCode; fp@1142: throwCommandException(err); fp@1142: } fp@1142: } catch (ios::failure &e) { fp@1142: delete [] data.data; fp@1142: err << "Invalid value argument '" << args[2] fp@1142: << "' for type '" << dataType->name << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: fp@1142: m.open(MasterDevice::ReadWrite); fp@1142: fp@1142: try { fp@1142: m.sdoDownload(&data); fp@1142: } catch(MasterDeviceException &e) { fp@1142: delete [] data.data; fp@1142: throw e; fp@1142: } fp@1142: fp@1142: delete [] data.data; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/