fp@1835: /***************************************************************************** fp@1835: * fp@1835: * $Id$ fp@1835: * fp@1835: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1835: * fp@1835: * This file is part of the IgH EtherCAT Master. fp@1835: * fp@1835: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1835: * modify it under the terms of the GNU General Public License version 2, as fp@1835: * published by the Free Software Foundation. fp@1835: * fp@1835: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1835: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1835: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1835: * Public License for more details. fp@1835: * fp@1835: * You should have received a copy of the GNU General Public License along fp@1835: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1835: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1835: * fp@1835: * --- fp@1835: * fp@1835: * The license mentioned above concerns the source code only. Using the fp@1835: * EtherCAT technology and brand is only permitted in compliance with the fp@1835: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1835: * fp@1835: ****************************************************************************/ fp@1835: fp@1837: #define DEBUG 0 fp@1837: fp@1837: #if DEBUG fp@1837: #include fp@1837: #endif fp@1837: fp@1835: #include fp@1835: #include fp@1835: using namespace std; fp@1835: fp@1835: #include "DataTypeHandler.h" fp@1835: fp@1835: #include "ecrt.h" fp@1835: fp@1835: /*****************************************************************************/ fp@1835: fp@1835: DataTypeHandler::DataTypeHandler() fp@1835: { fp@1835: } fp@1835: fp@1835: /****************************************************************************/ fp@1835: fp@1835: const DataTypeHandler::DataType *DataTypeHandler::findDataType( fp@1835: const string &str fp@1835: ) fp@1835: { fp@1835: const DataType *d; fp@1835: fp@1835: for (d = dataTypes; d->name; d++) fp@1835: if (str == d->name) fp@1835: return d; fp@1835: fp@1835: return NULL; // FIXME exception fp@1835: } fp@1835: fp@1835: /****************************************************************************/ fp@1835: fp@1868: string DataTypeHandler::typeInfo() fp@1868: { fp@1868: stringstream s; fp@1868: fp@1868: s fp@1868: << "These are valid data types to use with" << endl fp@1868: << "the --type option:" << endl fp@1871: << " bool," << endl fp@1871: << " int8, int16, int32, int64," << endl fp@1871: << " uint8, uint16, uint32, uint64," << endl fp@1871: << " float, double" << endl fp@1888: << " string, octet_string, unicode_string." << endl fp@1888: << "For sign-and-magnitude coding, use the following types:" << endl fp@1888: << " sm8, sm16, sm32, sm64" << endl; fp@1868: return s.str(); fp@1868: } fp@1868: fp@1868: /****************************************************************************/ fp@1868: fp@1835: const DataTypeHandler::DataType *DataTypeHandler::findDataType(uint16_t code) fp@1835: { fp@1835: const DataType *d; fp@1835: fp@1835: for (d = dataTypes; d->name; d++) fp@1835: if (code == d->code) fp@1835: return d; fp@1835: fp@1835: return NULL; fp@1835: } fp@1835: fp@1835: /****************************************************************************/ fp@1835: fp@1835: size_t DataTypeHandler::interpretAsType( fp@1835: const DataType *type, fp@1835: const string &source, fp@1835: void *target, fp@1835: size_t targetSize fp@1835: ) fp@1835: { fp@1835: stringstream str; fp@1835: size_t dataSize = type->byteSize; fp@1835: fp@1837: #if DEBUG fp@1837: cerr << __func__ << "(targetSize=" << targetSize << ")" << endl; fp@1837: #endif fp@1837: fp@1835: str << source; fp@1835: str >> resetiosflags(ios::basefield); // guess base from prefix fp@1835: str.exceptions(ios::failbit); fp@1835: fp@1837: #if DEBUG fp@1837: cerr << "code=" << type->code << endl; fp@1837: #endif fp@1837: fp@1835: switch (type->code) { fp@1871: case 0x0001: // bool fp@1871: { fp@1871: int16_t val; // uint8_t is interpreted as char fp@1871: str >> val; fp@1871: if (val > 1 || val < 0) fp@1871: throw ios::failure("Value out of range"); fp@1871: *(uint8_t *) target = val; fp@1871: break; fp@1871: } fp@1835: case 0x0002: // int8 fp@1835: { fp@1835: int16_t val; // uint8_t is interpreted as char fp@1835: str >> val; fp@1835: if (val > 127 || val < -128) fp@1835: throw ios::failure("Value out of range"); fp@1835: *(uint8_t *) target = val; fp@1835: break; fp@1835: } fp@1835: case 0x0003: // int16 fp@1835: { fp@1835: int16_t val; fp@1835: str >> val; fp@1835: *(int16_t *) target = cpu_to_le16(val); fp@1835: break; fp@1835: } fp@1835: case 0x0004: // int32 fp@1835: { fp@1835: int32_t val; fp@1835: str >> val; fp@1835: *(int32_t *) target = cpu_to_le32(val); fp@1835: break; fp@1835: } fp@1835: case 0x0005: // uint8 fp@1835: { fp@1835: uint16_t val; // uint8_t is interpreted as char fp@1835: str >> val; fp@1835: if (val > 0xff) fp@1835: throw ios::failure("Value out of range"); fp@1835: *(uint8_t *) target = val; fp@1835: break; fp@1835: } fp@1835: case 0x0006: // uint16 fp@1835: { fp@1835: uint16_t val; fp@1835: str >> val; fp@1835: *(uint16_t *) target = cpu_to_le16(val); fp@1835: break; fp@1835: } fp@1835: case 0x0007: // uint32 fp@1835: { fp@1835: uint32_t val; fp@1835: str >> val; fp@1835: *(uint32_t *) target = cpu_to_le32(val); fp@1835: break; fp@1835: } fp@1871: case 0x0008: // float fp@1871: { fp@1871: float val; fp@1871: str >> val; fp@1871: *(uint32_t *) target = fp@1871: cpu_to_le32(*(uint32_t *) (void *) &val); fp@1871: break; fp@1871: } fp@1835: case 0x0009: // string fp@1835: case 0x000a: // octet_string fp@1871: case 0x000b: // unicode_string fp@1835: dataSize = str.str().size(); fp@1835: if (dataSize >= targetSize) { fp@1835: stringstream err; fp@1835: err << "String too large"; fp@1835: throw SizeException(err.str()); fp@1835: } fp@1835: str >> (char *) target; fp@1835: break; fp@1871: case 0x0011: // double fp@1871: { fp@1871: double val; fp@1871: str >> val; fp@1871: *(uint64_t *) target = fp@1871: cpu_to_le64(*(uint64_t *) (void *) &val); fp@1871: break; fp@1871: } fp@1871: break; fp@1871: case 0x0015: // int64 fp@1871: { fp@1871: int64_t val; fp@1871: str >> val; fp@1871: *(int64_t *) target = cpu_to_le64(val); fp@1871: break; fp@1871: } fp@1871: break; fp@1871: case 0x001b: // uint64 fp@1871: { fp@1871: uint64_t val; fp@1871: str >> val; fp@1871: *(uint64_t *) target = cpu_to_le64(val); fp@1871: break; fp@1871: } fp@1871: break; fp@1871: fp@1871: case 0x0010: // int24 fp@1871: case 0x0012: // int40 fp@1871: case 0x0013: // int48 fp@1871: case 0x0014: // int56 fp@1871: case 0x0016: // uint24 fp@1871: case 0x0018: // uint40 fp@1871: case 0x0019: // uint48 fp@1871: case 0x001a: // uint56 fp@1871: { fp@1871: stringstream err; fp@1871: err << "Non-native integer type " << type->name fp@1871: << " is not yet implemented."; fp@1871: throw runtime_error(err.str()); fp@1871: } fp@1835: fp@1888: case 0xfffb: // sm8 fp@1888: case 0xfffc: // sm16 fp@1888: case 0xfffd: // sm32 fp@1888: case 0xfffe: // sm64 fp@1888: { fp@1888: stringstream err; fp@1888: err << "Sign-and-magitude types not yet" fp@1888: " implemented for input direction."; fp@1888: throw runtime_error(err.str()); fp@1888: } fp@1888: fp@1835: default: fp@1835: { fp@1835: stringstream err; fp@1835: err << "Unknown data type 0x" << hex << type->code; fp@1835: throw runtime_error(err.str()); fp@1835: } fp@1835: } fp@1835: fp@1837: #if DEBUG fp@1837: printRawData(cerr, (const uint8_t *) target, dataSize); fp@1837: #endif fp@1837: fp@1835: return dataSize; fp@1835: } fp@1835: fp@1835: /****************************************************************************/ fp@1835: fp@1835: void DataTypeHandler::outputData( fp@1835: ostream &o, fp@1835: const DataType *type, fp@1835: void *data, fp@1835: size_t dataSize fp@1835: ) fp@1835: { fp@1835: if (type->byteSize && dataSize != type->byteSize) { fp@1835: stringstream err; fp@1835: err << "Data type mismatch. Expected " << type->name fp@1835: << " with " << type->byteSize << " byte, but got " fp@1835: << dataSize << " byte."; fp@1835: throw SizeException(err.str()); fp@1835: } fp@1835: fp@1835: o << setfill('0'); fp@1835: fp@1835: switch (type->code) { fp@1871: case 0x0001: // bool fp@1835: { fp@1835: int val = (int) *(int8_t *) data; fp@1835: o << "0x" << hex << setw(2) << val fp@1835: << " " << dec << val << endl; fp@1835: } fp@1835: break; fp@1871: case 0x0002: // int8 fp@1871: { fp@1871: int val = (int) *(int8_t *) data; fp@1871: o << "0x" << hex << setw(2) << val fp@1871: << " " << dec << val << endl; fp@1871: } fp@1871: break; fp@1835: case 0x0003: // int16 fp@1835: { fp@1835: int16_t val = le16_to_cpup(data); fp@1835: o << "0x" << hex << setw(4) << val fp@1835: << " " << dec << val << endl; fp@1835: } fp@1835: break; fp@1835: case 0x0004: // int32 fp@1835: { fp@1835: int32_t val = le32_to_cpup(data); fp@1835: o << "0x" << hex << setw(8) << val fp@1835: << " " << dec << val << endl; fp@1835: } fp@1835: break; fp@1835: case 0x0005: // uint8 fp@1835: { fp@1835: unsigned int val = (unsigned int) *(uint8_t *) data; fp@1835: o << "0x" << hex << setw(2) << val fp@1835: << " " << dec << val << endl; fp@1835: } fp@1835: break; fp@1835: case 0x0006: // uint16 fp@1835: { fp@1835: uint16_t val = le16_to_cpup(data); fp@1835: o << "0x" << hex << setw(4) << val fp@1835: << " " << dec << val << endl; fp@1835: } fp@1835: break; fp@1835: case 0x0007: // uint32 fp@1835: { fp@1835: uint32_t val = le32_to_cpup(data); fp@1835: o << "0x" << hex << setw(8) << val fp@1835: << " " << dec << val << endl; fp@1835: } fp@1835: break; fp@1871: case 0x0008: // float fp@1871: { fp@1871: uint32_t val = le32_to_cpup(data); fp@1871: float fval = *(float *) (void *) &val; fp@1871: o << fval << endl; fp@1871: } fp@1871: break; fp@1835: case 0x0009: // string fp@1835: o << string((const char *) data, dataSize) << endl; fp@1835: break; fp@1835: case 0x000a: // octet_string fp@1875: o << string((const char *) data, dataSize) << flush; fp@1835: break; fp@1871: case 0x000b: // unicode_string fp@1871: // FIXME encoding fp@1871: o << string((const char *) data, dataSize) << endl; fp@1871: break; fp@1871: case 0x0011: // double fp@1871: { fp@1871: uint64_t val = le64_to_cpup(data); fp@1871: double fval = *(double *) (void *) &val; fp@1871: o << fval << endl; fp@1871: } fp@1871: break; fp@1871: case 0x0015: // int64 fp@1871: { fp@1871: int64_t val = le64_to_cpup(data); fp@1871: o << "0x" << hex << setw(16) << val fp@1871: << " " << dec << val << endl; fp@1871: } fp@1871: break; fp@1871: case 0x001b: // uint64 fp@1871: { fp@1871: uint64_t val = le64_to_cpup(data); fp@1871: o << "0x" << hex << setw(16) << val fp@1871: << " " << dec << val << endl; fp@1871: } fp@1871: break; fp@1888: case 0xfffb: // sm8 fp@1888: { fp@1888: int8_t val = *(uint8_t *) data; fp@1888: int8_t smval = val < 0 ? (val & 0x7f) * -1 : val; fp@1888: fp@1888: o << "0x" << hex << setw(2) << (int) val fp@1888: << " " << dec << (int) smval << endl; fp@1888: } fp@1888: break; fp@1888: case 0xfffc: // sm16 fp@1888: { fp@1888: int16_t val = le16_to_cpup(data); fp@1888: int16_t smval = val < 0 ? (val & 0x7fff) * -1 : val; fp@1888: fp@1888: o << "0x" << hex << setw(4) << val fp@1888: << " " << dec << smval << endl; fp@1888: } fp@1888: break; fp@1888: case 0xfffd: // sm32 fp@1888: { fp@1888: int32_t val = le32_to_cpup(data); fp@1888: int32_t smval = val < 0 ? (val & 0x7fffffffUL) * -1 : val; fp@1888: fp@1888: o << "0x" << hex << setw(8) << val fp@1888: << " " << dec << smval << endl; fp@1888: } fp@1888: break; fp@1888: case 0xfffe: // sm64 fp@1888: { fp@1888: int64_t val = le64_to_cpup(data); fp@1888: int64_t smval = fp@1888: val < 0 ? (val & 0x7fffffffffffffffULL) * -1 : val; fp@1888: fp@1888: o << "0x" << hex << setw(16) << val fp@1888: << " " << dec << smval << endl; fp@1888: } fp@1888: break; fp@1871: fp@1835: default: fp@1835: printRawData(o, (const uint8_t *) data, dataSize); // FIXME fp@1835: break; fp@1835: } fp@1835: } fp@1835: fp@1835: /****************************************************************************/ fp@1835: fp@1835: void DataTypeHandler::printRawData( fp@1835: ostream &o, fp@1835: const uint8_t *data, fp@1835: size_t size fp@1835: ) fp@1835: { fp@1835: o << hex << setfill('0'); fp@1835: while (size--) { fp@1835: o << "0x" << setw(2) << (unsigned int) *data++; fp@1835: if (size) fp@1835: o << " "; fp@1835: } fp@1835: o << endl; fp@1835: } fp@1835: fp@1835: /****************************************************************************/ fp@1835: fp@1835: const DataTypeHandler::DataType DataTypeHandler::dataTypes[] = { fp@1871: {"bool", 0x0001, 1}, fp@1871: {"int8", 0x0002, 1}, fp@1871: {"int16", 0x0003, 2}, fp@1871: {"int32", 0x0004, 4}, fp@1871: {"uint8", 0x0005, 1}, fp@1871: {"uint16", 0x0006, 2}, fp@1871: {"uint32", 0x0007, 4}, fp@1871: {"float", 0x0008, 4}, fp@1871: {"string", 0x0009, 0}, // a. k. a. visible_string fp@1871: {"octet_string", 0x000a, 0}, fp@1871: {"unicode_string", 0x000b, 0}, fp@1871: // ... not implemented yet fp@1871: {"int24", 0x0010, 3}, fp@1871: {"double", 0x0011, 8}, fp@1871: {"int40", 0x0012, 5}, fp@1871: {"int48", 0x0013, 6}, fp@1871: {"int56", 0x0014, 7}, fp@1871: {"int64", 0x0015, 8}, fp@1871: {"uint24", 0x0016, 3}, fp@1871: // reserved 0x0017 fp@1871: {"uint40", 0x0018, 5}, fp@1871: {"uint48", 0x0019, 6}, fp@1871: {"uint56", 0x001a, 7}, fp@1871: {"uint64", 0x001b, 8}, fp@1871: // reserved 0x001c-0x001f fp@1888: {"sm8", 0xfffb, 1}, // sign-and-magnitude coding fp@1888: {"sm16", 0xfffc, 2}, // sign-and-magnitude coding fp@1888: {"sm32", 0xfffd, 4}, // sign-and-magnitude coding fp@1888: {"sm64", 0xfffe, 8}, // sign-and-magnitude coding fp@1871: {"raw", 0xffff, 0}, fp@1835: {} fp@1835: }; fp@1835: fp@1835: /*****************************************************************************/