fp@1134: /***************************************************************************** fp@1134: * fp@1134: * $Id$ fp@1134: * fp@1134: ****************************************************************************/ fp@1134: fp@1134: #include fp@1134: #include fp@1134: using namespace std; fp@1134: fp@1134: #include "globals.h" fp@1134: #include "coe_datatypes.h" fp@1134: fp@1134: /****************************************************************************/ fp@1134: fp@1134: const char *help_upload = fp@1137: "[OPTIONS] \n" fp@1134: "\n" fp@1137: "Upload an Sdo entry from a slave.\n" fp@1134: "\n" fp@1137: "The data type of the Sdo entry is taken from the Sdo dictionary by\n" fp@1137: "default. It can be overridden with the --type option. If the slave\n" fp@1137: "does not support the Sdo information service or the Sdo is not in the\n" fp@1137: "dictionary, the --type option is mandatory.\n" fp@1137: "\n" fp@1137: "These are the valid Sdo entry data types:\n" fp@1137: " int8, int16, int32, uint8, uint16, uint32, string.\n" fp@1137: "\n" fp@1137: "Arguments:\n" fp@1137: " INDEX is the Sdo index and must be an unsigned 16 bit number.\n" fp@1137: " SUBINDEX is the Sdo entry subindex and must be an unsigned 8 bit\n" fp@1137: " number.\n" fp@1137: "\n" fp@1137: "Command-specific options:\n" fp@1137: " --slave -s Positive numerical ring position (mandatory).\n" fp@1137: " --type -t Forced Sdo entry data type (see above).\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@1134: fp@1134: /****************************************************************************/ fp@1134: fp@1134: void command_upload(void) fp@1134: { fp@1136: stringstream err, strIndex, strSubIndex; fp@1134: int sval; fp@1134: ec_ioctl_slave_sdo_upload_t data; fp@1134: unsigned int uval; fp@1134: const CoEDataType *dataType = NULL; fp@1134: fp@1134: if (slavePosition < 0) { fp@1136: err << "'" << commandName << "' requires a slave! " fp@1136: << "Please specify --slave."; fp@1136: throw InvalidUsageException(err); fp@1134: } fp@1134: data.slave_position = slavePosition; fp@1134: fp@1134: if (commandArgs.size() != 2) { fp@1136: err << "'" << commandName << "' takes two arguments!"; fp@1136: throw InvalidUsageException(err); fp@1134: } fp@1134: fp@1134: strIndex << commandArgs[0]; fp@1134: strIndex fp@1134: >> resetiosflags(ios::basefield) // guess base from prefix fp@1134: >> data.sdo_index; fp@1134: if (strIndex.fail()) { fp@1134: err << "Invalid Sdo index '" << commandArgs[0] << "'!"; fp@1136: throw InvalidUsageException(err); fp@1134: } fp@1134: fp@1134: strSubIndex << commandArgs[1]; fp@1134: strSubIndex fp@1134: >> resetiosflags(ios::basefield) // guess base from prefix fp@1134: >> uval; fp@1134: if (strSubIndex.fail() || uval > 0xff) { fp@1134: err << "Invalid Sdo subindex '" << commandArgs[1] << "'!"; fp@1136: throw InvalidUsageException(err); fp@1134: } fp@1134: data.sdo_entry_subindex = uval; fp@1134: fp@1134: if (dataTypeStr != "") { // data type specified fp@1134: if (!(dataType = findDataType(dataTypeStr))) { fp@1134: err << "Invalid data type '" << dataTypeStr << "'!"; fp@1136: throw InvalidUsageException(err); fp@1134: } fp@1134: } else { // no data type specified: fetch from dictionary fp@1134: ec_ioctl_slave_sdo_entry_t entry; fp@1134: fp@1134: masterDev.open(MasterDevice::Read); fp@1134: fp@1134: try { fp@1134: masterDev.getSdoEntry(&entry, slavePosition, fp@1134: data.sdo_index, data.sdo_entry_subindex); fp@1134: } catch (MasterDeviceException &e) { fp@1134: err << "Failed to determine Sdo entry data type. " fp@1134: << "Please specify --type."; fp@1136: throw CommandException(err); fp@1134: } fp@1134: if (!(dataType = findDataType(entry.data_type))) { fp@1134: err << "Pdo entry has unknown data type 0x" fp@1134: << hex << setfill('0') << setw(4) << entry.data_type << "!" fp@1134: << " Please specify --type."; fp@1136: throw CommandException(err); fp@1134: } fp@1134: } fp@1134: fp@1134: if (dataType->byteSize) { fp@1134: data.target_size = dataType->byteSize; fp@1134: } else { fp@1134: data.target_size = DefaultBufferSize; fp@1134: } fp@1134: fp@1134: data.target = new uint8_t[data.target_size + 1]; fp@1134: fp@1134: masterDev.open(MasterDevice::Read); fp@1134: fp@1134: try { fp@1134: masterDev.sdoUpload(&data); fp@1134: } catch (MasterDeviceException &e) { fp@1134: delete [] data.target; fp@1134: throw e; fp@1134: } fp@1134: fp@1134: masterDev.close(); fp@1134: fp@1134: if (dataType->byteSize && data.data_size != dataType->byteSize) { fp@1134: err << "Data type mismatch. Expected " << dataType->name fp@1134: << " with " << dataType->byteSize << " byte, but got " fp@1134: << data.data_size << " byte."; fp@1136: throw CommandException(err); fp@1134: } fp@1134: fp@1134: cout << setfill('0'); fp@1134: switch (dataType->coeCode) { fp@1134: case 0x0002: // int8 fp@1134: sval = *(int8_t *) data.target; fp@1134: cout << sval << " 0x" << hex << setw(2) << sval << endl; fp@1134: break; fp@1134: case 0x0003: // int16 fp@1134: sval = le16tocpu(*(int16_t *) data.target); fp@1134: cout << sval << " 0x" << hex << setw(4) << sval << endl; fp@1134: break; fp@1134: case 0x0004: // int32 fp@1134: sval = le32tocpu(*(int32_t *) data.target); fp@1134: cout << sval << " 0x" << hex << setw(8) << sval << endl; fp@1134: break; fp@1134: case 0x0005: // uint8 fp@1134: uval = (unsigned int) *(uint8_t *) data.target; fp@1134: cout << uval << " 0x" << hex << setw(2) << uval << endl; fp@1134: break; fp@1134: case 0x0006: // uint16 fp@1134: uval = le16tocpu(*(uint16_t *) data.target); fp@1134: cout << uval << " 0x" << hex << setw(4) << uval << endl; fp@1134: break; fp@1134: case 0x0007: // uint32 fp@1134: uval = le32tocpu(*(uint32_t *) data.target); fp@1134: cout << uval << " 0x" << hex << setw(8) << uval << endl; fp@1134: break; fp@1134: case 0x0009: // string fp@1134: cout << string((const char *) data.target, data.data_size) fp@1134: << endl; fp@1134: break; fp@1134: default: fp@1134: printRawData(data.target, data.data_size); fp@1134: break; fp@1134: } fp@1134: fp@1134: delete [] data.target; fp@1134: } fp@1134: fp@1134: /*****************************************************************************/