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 "CommandUpload.h" fp@1142: #include "byteorder.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandUpload::CommandUpload(): fp@1686: SdoCommand("upload", "Read an SDO entry from a slave.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: string CommandUpload::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@1686: << "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@1686: << "--type option. If the slave does not support the SDO" << endl fp@1686: << "information service or the SDO is not in the dictionary," << endl fp@1142: << "the --type option is mandatory." << endl fp@1142: << endl fp@1686: << "These are the valid SDO entry data types:" << endl fp@1710: << " int8, int16, int32, uint8, uint16, uint32, string," << endl fp@1710: << " octet_string." << endl fp@1142: << endl fp@1142: << "Arguments:" << endl fp@1686: << " INDEX is the SDO index and must be an unsigned" << endl fp@1142: << " 16 bit number." << endl fp@1686: << " SUBINDEX is the SDO entry subindex and must be an" << endl fp@1142: << " unsigned 8 bit number." << 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@1686: << " --type -t SDO entry data type (see 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 CommandUpload::execute(MasterDevice &m, const StringVector &args) fp@1142: { fp@1151: SlaveList slaves; fp@1142: stringstream err, strIndex, strSubIndex; fp@1142: int sval; fp@1142: ec_ioctl_slave_sdo_upload_t data; fp@1142: unsigned int uval; fp@1184: const DataType *dataType = NULL; fp@1142: fp@1142: if (args.size() != 2) { fp@1142: err << "'" << getName() << "' takes two 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@1686: 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: >> uval; fp@1142: if (strSubIndex.fail() || uval > 0xff) { fp@1686: err << "Invalid SDO subindex '" << args[1] << "'!"; fp@1142: throwInvalidUsageException(err); fp@1142: } fp@1142: data.sdo_entry_subindex = uval; fp@1142: fp@1151: m.open(MasterDevice::Read); fp@1151: slaves = selectedSlaves(m); fp@1151: if (slaves.size() != 1) { fp@1155: throwSingleSlaveRequired(slaves.size()); fp@1151: } fp@1151: data.slave_position = slaves.front().position; fp@1151: fp@1166: if (!getDataType().empty()) { // data type specified fp@1166: if (!(dataType = findDataType(getDataType()))) { fp@1166: err << "Invalid data type '" << getDataType() << "'!"; 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: try { fp@1151: m.getSdoEntry(&entry, data.slave_position, fp@1142: data.sdo_index, data.sdo_entry_subindex); fp@1142: } catch (MasterDeviceException &e) { fp@1686: 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@1686: 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.target_size = dataType->byteSize; fp@1142: } else { fp@1142: data.target_size = DefaultBufferSize; fp@1142: } fp@1142: fp@1142: data.target = new uint8_t[data.target_size + 1]; fp@1142: fp@1142: try { fp@1142: m.sdoUpload(&data); fp@1184: } catch (MasterDeviceSdoAbortException &e) { fp@1184: delete [] data.target; fp@1686: err << "SDO transfer aborted with code 0x" fp@1184: << setfill('0') << hex << setw(8) << e.abortCode fp@1184: << ": " << abortText(e.abortCode); fp@1184: throwCommandException(err); fp@1142: } catch (MasterDeviceException &e) { fp@1142: delete [] data.target; fp@1142: throw e; fp@1142: } fp@1142: fp@1142: m.close(); fp@1142: fp@1142: if (dataType->byteSize && data.data_size != dataType->byteSize) { fp@1142: err << "Data type mismatch. Expected " << dataType->name fp@1142: << " with " << dataType->byteSize << " byte, but got " fp@1142: << data.data_size << " byte."; fp@1142: throwCommandException(err); fp@1142: } fp@1142: fp@1142: cout << setfill('0'); fp@1142: switch (dataType->coeCode) { fp@1142: case 0x0002: // int8 fp@1142: sval = *(int8_t *) data.target; fp@1142: cout << sval << " 0x" << hex << setw(2) << sval << endl; fp@1142: break; fp@1142: case 0x0003: // int16 fp@1142: sval = le16tocpu(*(int16_t *) data.target); fp@1142: cout << sval << " 0x" << hex << setw(4) << sval << endl; fp@1142: break; fp@1142: case 0x0004: // int32 fp@1142: sval = le32tocpu(*(int32_t *) data.target); fp@1142: cout << sval << " 0x" << hex << setw(8) << sval << endl; fp@1142: break; fp@1142: case 0x0005: // uint8 fp@1142: uval = (unsigned int) *(uint8_t *) data.target; fp@1142: cout << uval << " 0x" << hex << setw(2) << uval << endl; fp@1142: break; fp@1142: case 0x0006: // uint16 fp@1142: uval = le16tocpu(*(uint16_t *) data.target); fp@1142: cout << uval << " 0x" << hex << setw(4) << uval << endl; fp@1142: break; fp@1142: case 0x0007: // uint32 fp@1142: uval = le32tocpu(*(uint32_t *) data.target); fp@1142: cout << uval << " 0x" << hex << setw(8) << uval << endl; fp@1142: break; fp@1142: case 0x0009: // string fp@1142: cout << string((const char *) data.target, data.data_size) fp@1142: << endl; fp@1142: break; fp@1710: case 0x000a: // octet_string fp@1710: cout << string((const char *) data.target, data.data_size) fp@1710: << endl; fp@1710: break; fp@1142: default: fp@1142: printRawData(data.target, data.data_size); // FIXME fp@1142: break; fp@1142: } fp@1142: fp@1142: delete [] data.target; fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: void CommandUpload::printRawData( fp@1142: const uint8_t *data, fp@1142: unsigned int size fp@1142: ) fp@1142: { fp@1142: cout << hex << setfill('0'); fp@1142: while (size--) { fp@1142: cout << "0x" << setw(2) << (unsigned int) *data++; fp@1142: if (size) fp@1142: cout << " "; fp@1142: } fp@1142: cout << endl; fp@1142: } fp@1142: fp@1142: /*****************************************************************************/