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@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@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@1835: case 0x0009: // string fp@1835: case 0x000a: // octet_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@1835: 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@1835: case 0x0002: // int8 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@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@1835: case 0x0009: // string fp@1835: o << string((const char *) data, dataSize) << endl; fp@1835: break; fp@1835: case 0x000a: // octet_string fp@1835: o << string((const char *) data, dataSize) << endl; fp@1835: break; 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@1835: {"int8", 0x0002, 1}, fp@1835: {"int16", 0x0003, 2}, fp@1835: {"int32", 0x0004, 4}, fp@1835: {"uint8", 0x0005, 1}, fp@1835: {"uint16", 0x0006, 2}, fp@1835: {"uint32", 0x0007, 4}, fp@1835: {"string", 0x0009, 0}, fp@1835: {"octet_string", 0x000a, 0}, fp@1835: {"raw", 0xffff, 0}, fp@1835: //{"int64", 8}, fp@1835: //{"uint64", 8}, fp@1835: {} fp@1835: }; fp@1835: fp@1835: /*****************************************************************************/