fp@1126: /***************************************************************************** fp@1126: * fp@1126: * $Id$ fp@1126: * fp@1126: ****************************************************************************/ fp@1126: fp@1126: #include fp@1126: #include fp@1126: using namespace std; fp@1126: fp@1126: #include "globals.h" fp@1126: #include "coe_datatypes.h" fp@1126: fp@1126: /****************************************************************************/ fp@1126: fp@1130: // FIXME fp@1126: const char *help_sdo_download = fp@1126: "[OPTIONS]\n" fp@1126: "\n" fp@1126: "\n" fp@1126: "Command-specific options:\n"; fp@1126: fp@1126: /****************************************************************************/ fp@1126: fp@1126: void command_sdo_download(void) fp@1126: { fp@1126: stringstream strIndex, strSubIndex, strValue, err; fp@1126: ec_ioctl_slave_sdo_download_t data; fp@1126: unsigned int number; fp@1126: const CoEDataType *dataType = NULL; fp@1126: fp@1126: if (slavePosition < 0) { fp@1126: err << "'sdo_download' requires a slave! Please specify --slave."; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: data.slave_position = slavePosition; fp@1126: fp@1126: if (commandArgs.size() != 3) { fp@1126: err << "'sdo_download' takes 3 arguments!"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: strIndex << commandArgs[0]; fp@1126: strIndex fp@1126: >> resetiosflags(ios::basefield) // guess base from prefix fp@1126: >> data.sdo_index; fp@1126: if (strIndex.fail()) { fp@1126: err << "Invalid Sdo index '" << commandArgs[0] << "'!"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: strSubIndex << commandArgs[1]; fp@1126: strSubIndex fp@1126: >> resetiosflags(ios::basefield) // guess base from prefix fp@1126: >> number; fp@1126: if (strSubIndex.fail() || number > 0xff) { fp@1126: err << "Invalid Sdo subindex '" << commandArgs[1] << "'!"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: data.sdo_entry_subindex = number; fp@1126: fp@1126: if (dataTypeStr != "") { // data type specified fp@1126: if (!(dataType = findDataType(dataTypeStr))) { fp@1126: err << "Invalid data type '" << dataTypeStr << "'!"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: } else { // no data type specified: fetch from dictionary fp@1126: ec_ioctl_slave_sdo_entry_t entry; fp@1126: fp@1126: masterDev.open(MasterDevice::ReadWrite); fp@1126: fp@1126: try { fp@1126: masterDev.getSdoEntry(&entry, slavePosition, fp@1126: data.sdo_index, data.sdo_entry_subindex); fp@1126: } catch (MasterDeviceException &e) { fp@1126: err << "Failed to determine Sdo entry data type. " fp@1126: << "Please specify --type."; fp@1126: throw ExecutionFailureException(err); fp@1126: } fp@1126: if (!(dataType = findDataType(entry.data_type))) { fp@1126: err << "Pdo entry has unknown data type 0x" fp@1126: << hex << setfill('0') << setw(4) << entry.data_type << "!" fp@1126: << " Please specify --type."; fp@1126: throw ExecutionFailureException(err); fp@1126: } fp@1126: } fp@1126: fp@1126: if (dataType->byteSize) { fp@1126: data.data_size = dataType->byteSize; fp@1126: } else { fp@1126: data.data_size = DefaultBufferSize; fp@1126: } fp@1126: fp@1126: data.data = new uint8_t[data.data_size + 1]; fp@1126: fp@1126: strValue << commandArgs[2]; fp@1126: strValue >> resetiosflags(ios::basefield); // guess base from prefix fp@1126: strValue.exceptions(ios::failbit); fp@1126: fp@1126: try { fp@1126: switch (dataType->coeCode) { fp@1126: case 0x0002: // int8 fp@1126: { fp@1126: int16_t val; // uint8_t is interpreted as char fp@1126: strValue >> val; fp@1126: if (val > 127 || val < -128) fp@1126: throw ios::failure("Value out of range"); fp@1126: *data.data = val; fp@1126: break; fp@1126: } fp@1126: case 0x0003: // int16 fp@1126: { fp@1126: int16_t val; fp@1126: strValue >> val; fp@1126: *(int16_t *) data.data = cputole16(val); fp@1126: break; fp@1126: } fp@1126: case 0x0004: // int32 fp@1126: { fp@1126: int32_t val; fp@1126: strValue >> val; fp@1126: *(int32_t *) data.data = cputole32(val); fp@1126: break; fp@1126: } fp@1126: case 0x0005: // uint8 fp@1126: { fp@1126: uint16_t val; // uint8_t is interpreted as char fp@1126: strValue >> val; fp@1126: if (val > 0xff) fp@1126: throw ios::failure("Value out of range"); fp@1126: *data.data = val; fp@1126: break; fp@1126: } fp@1126: case 0x0006: // uint16 fp@1126: { fp@1126: uint16_t val; fp@1126: strValue >> val; fp@1126: *(uint16_t *) data.data = cputole16(val); fp@1126: break; fp@1126: } fp@1126: case 0x0007: // uint32 fp@1126: { fp@1126: uint32_t val; fp@1126: strValue >> val; fp@1126: *(uint32_t *) data.data = cputole32(val); fp@1126: break; fp@1126: } fp@1126: case 0x0009: // string fp@1126: if (strValue.str().size() >= data.data_size) { fp@1126: err << "String too large"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: data.data_size = strValue.str().size(); fp@1126: strValue >> (char *) data.data; fp@1126: break; fp@1126: fp@1126: default: fp@1126: delete [] data.data; fp@1126: err << "Unknown data type 0x" << hex << dataType->coeCode; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: } catch (ios::failure &e) { fp@1126: delete [] data.data; fp@1126: err << "Invalid value argument '" << commandArgs[2] fp@1126: << "' for type '" << dataType->name << "'!"; fp@1126: throw MasterDeviceException(err.str()); fp@1126: } fp@1126: fp@1126: masterDev.open(MasterDevice::ReadWrite); fp@1126: fp@1126: try { fp@1126: masterDev.sdoDownload(&data); fp@1126: } catch(MasterDeviceException &e) { fp@1126: delete [] data.data; fp@1126: throw e; fp@1126: } fp@1126: fp@1126: delete [] data.data; fp@1126: } fp@1126: fp@1126: /*****************************************************************************/