Sourced out data type handling into an own class.
authorFlorian Pose <fp@igh-essen.com>
Fri, 05 Mar 2010 13:39:57 +0100
changeset 1835 20748e9d2238
parent 1834 67fc217d7341
child 1836 52036efdf1e3
Sourced out data type handling into an own class.
TODO
tool/CommandDownload.cpp
tool/CommandReg.cpp
tool/CommandReg.h
tool/CommandRegRead.cpp
tool/CommandRegRead.h
tool/CommandRegWrite.cpp
tool/CommandRegWrite.h
tool/CommandSoeRead.cpp
tool/CommandSoeRead.h
tool/CommandUpload.cpp
tool/CommandUpload.h
tool/DataTypeHandler.cpp
tool/DataTypeHandler.h
tool/Makefile.am
tool/MasterDevice.cpp
tool/MasterDevice.h
tool/SdoCommand.cpp
tool/SdoCommand.h
--- a/TODO	Fri Mar 05 13:20:42 2010 +0100
+++ b/TODO	Fri Mar 05 13:39:57 2010 +0100
@@ -30,6 +30,8 @@
 * Implement indent in 'ethercat ma'
 * Add master index to log messages.
 * Implement 0xXXXX:YY format for specifying SDOs.
+* Lookup CoE codes for 64bit data types.
+* Move data type usage string into DataTypeHandler.
 
 Future issues:
 
--- a/tool/CommandDownload.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandDownload.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -86,7 +86,7 @@
 
 void CommandDownload::execute(const StringVector &args)
 {
-    stringstream strIndex, strSubIndex, strValue, err;
+    stringstream strIndex, strSubIndex, err;
     ec_ioctl_slave_sdo_download_t data;
     unsigned int number;
     const DataType *dataType = NULL;
@@ -160,80 +160,12 @@
 
     data.data = new uint8_t[data.data_size + 1];
 
-    strValue << args[2];
-    strValue >> resetiosflags(ios::basefield); // guess base from prefix
-    strValue.exceptions(ios::failbit);
-
     try {
-        switch (dataType->coeCode) {
-            case 0x0002: // int8
-                {
-                    int16_t val; // uint8_t is interpreted as char
-                    strValue >> val;
-                    if (val > 127 || val < -128)
-                        throw ios::failure("Value out of range");
-                    *data.data = val;
-                    break;
-                }
-            case 0x0003: // int16
-                {
-                    int16_t val;
-                    strValue >> val;
-                    *(int16_t *) data.data = cpu_to_le16(val);
-                    break;
-                }
-            case 0x0004: // int32
-                {
-                    int32_t val;
-                    strValue >> val;
-                    *(int32_t *) data.data = cpu_to_le32(val);
-                    break;
-                }
-            case 0x0005: // uint8
-                {
-                    uint16_t val; // uint8_t is interpreted as char
-                    strValue >> val;
-                    if (val > 0xff)
-                        throw ios::failure("Value out of range");
-                    *data.data = val;
-                    break;
-                }
-            case 0x0006: // uint16
-                {
-                    uint16_t val;
-                    strValue >> val;
-                    *(uint16_t *) data.data = cpu_to_le16(val);
-                    break;
-                }
-            case 0x0007: // uint32
-                {
-                    uint32_t val;
-                    strValue >> val;
-                    *(uint32_t *) data.data = cpu_to_le32(val);
-                    break;
-                }
-            case 0x0009: // string
-                if (strValue.str().size() >= data.data_size) {
-                    err << "String too large";
-                    throwCommandException(err);
-                }
-                data.data_size = strValue.str().size();
-                strValue >> (char *) data.data;
-                break;
-            case 0x000a: // octet_string
-                if (strValue.str().size() >= data.data_size) {
-                    err << "String too large";
-                    throwCommandException(err);
-                }
-                data.data_size = strValue.str().size();
-                strValue >> (char *) data.data;
-                break;
-
-            default:
-                delete [] data.data;
-                err << "Unknown data type 0x" << hex << dataType->coeCode;
-                throwCommandException(err);
-        }
+        data.data_size = interpretAsType(
+                dataType, args[2], data.data, data.data_size);
+    } catch (SizeException &e) {
+        delete [] data.data;
+        throwCommandException(e.what());
     } catch (ios::failure &e) {
         delete [] data.data;
         err << "Invalid value argument '" << args[2]
--- a/tool/CommandReg.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-/*****************************************************************************
- *
- *  $Id$
- *
- *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
- *
- *  This file is part of the IgH EtherCAT Master.
- *
- *  The IgH EtherCAT Master is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU General Public License version 2, as
- *  published by the Free Software Foundation.
- *
- *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
- *  Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with the IgH EtherCAT Master; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- *  ---
- *
- *  The license mentioned above concerns the source code only. Using the
- *  EtherCAT technology and brand is only permitted in compliance with the
- *  industrial property and similar rights of Beckhoff Automation GmbH.
- *
- ****************************************************************************/
-
-#include "CommandReg.h"
-
-/*****************************************************************************/
-
-CommandReg::CommandReg(const string &name, const string &briefDesc):
-    Command(name, briefDesc)
-{
-}
-
-/****************************************************************************/
-
-const CommandReg::DataType *CommandReg::findDataType(
-        const string &str
-        )
-{
-    const DataType *d;
-    
-    for (d = dataTypes; d->name; d++)
-        if (str == d->name)
-            return d;
-
-    return NULL;
-}
-
-/****************************************************************************/
-
-const CommandReg::DataType CommandReg::dataTypes[] = {
-    {"int8",         1},
-    {"int16",        2},
-    {"int32",        4},
-    {"int64",        8},
-    {"uint8",        1},
-    {"uint16",       2},
-    {"uint32",       4},
-    {"uint64",       8},
-    {"string",       0},
-    {"raw",          0},
-    {}
-};
-
-/*****************************************************************************/
--- a/tool/CommandReg.h	Fri Mar 05 13:20:42 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-/*****************************************************************************
- *
- *  $Id$
- *
- *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
- *
- *  This file is part of the IgH EtherCAT Master.
- *
- *  The IgH EtherCAT Master is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU General Public License version 2, as
- *  published by the Free Software Foundation.
- *
- *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
- *  Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with the IgH EtherCAT Master; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- *  ---
- *
- *  The license mentioned above concerns the source code only. Using the
- *  EtherCAT technology and brand is only permitted in compliance with the
- *  industrial property and similar rights of Beckhoff Automation GmbH.
- *
- ****************************************************************************/
-
-#ifndef __COMMANDREG_H__
-#define __COMMANDREG_H__
-
-#include "Command.h"
-
-/****************************************************************************/
-
-class CommandReg:
-    public Command
-{
-    public:
-        CommandReg(const string &, const string &);
-
-    protected:
-        struct DataType {
-            const char *name;
-            unsigned int byteSize;
-        };
-        static const DataType dataTypes[];
-        static const DataType *findDataType(const string &);
-};
-
-/****************************************************************************/
-
-#endif
--- a/tool/CommandRegRead.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandRegRead.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -37,7 +37,7 @@
 /*****************************************************************************/
 
 CommandRegRead::CommandRegRead():
-    CommandReg("reg_read", "Output a slave's register contents.")
+    Command("reg_read", "Output a slave's register contents.")
 {
 }
 
@@ -164,49 +164,11 @@
         throw e;
     }
 
-    cout << setfill('0');
-    if (!dataType || string(dataType->name) == "string") {
-        uint16_t i;
-        for (i = 0; i < data.length; i++) {
-            cout << data.data[i];
-        }
-        if (dataType)
-            cout << endl;
-    } else if (string(dataType->name) == "int8") {
-        int val = (int) *data.data;
-        cout << "0x" << hex << setw(2) << val << " " << dec << val << endl;
-    } else if (string(dataType->name) == "int16") {
-        int16_t val = le16_to_cpup(data.data);
-        cout << "0x" << hex << setw(4) << val << " " << dec << val << endl;
-    } else if (string(dataType->name) == "int32") {
-        int32_t val = le32_to_cpup(data.data);
-        cout << "0x" << hex << setw(8) << val << " " << dec << val << endl;
-    } else if (string(dataType->name) == "int64") {
-        int64_t val = le64_to_cpup(data.data);
-        cout << "0x" << hex << setw(16) << val << " " << dec << val << endl;
-    } else if (string(dataType->name) == "uint8") {
-        unsigned int val = (unsigned int) *data.data;
-        cout << "0x" << hex << setw(2) << val << " " << dec << val << endl;
-    } else if (string(dataType->name) == "uint16") {
-        uint16_t val = le16_to_cpup(data.data);
-        cout << "0x" << hex << setw(4) << val << " " << dec << val << endl;
-    } else if (string(dataType->name) == "uint32") {
-        uint32_t val = le32_to_cpup(data.data);
-        cout << "0x" << hex << setw(8) << val << " " << dec << val << endl;
-    } else if (string(dataType->name) == "uint64") {
-        uint64_t val = le64_to_cpup(data.data);
-        cout << "0x" << hex << setw(16) << val << " " << dec << val << endl;
-    } else { // raw
-        uint8_t *d = data.data;
-        unsigned int size = data.length;
-
-        cout << hex << setfill('0');
-        while (size--) {
-            cout << "0x" << setw(2) << (unsigned int) *d++;
-            if (size)
-                cout << " ";
-        }
-        cout << endl;
+    try {
+        outputData(cout, dataType, data.data, data.length);
+    } catch (SizeException &e) {
+        delete [] data.data;
+        throwCommandException(e.what());
     }
 
     delete [] data.data;
--- a/tool/CommandRegRead.h	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandRegRead.h	Fri Mar 05 13:39:57 2010 +0100
@@ -30,12 +30,14 @@
 #ifndef __COMMANDREGREAD_H__
 #define __COMMANDREGREAD_H__
 
-#include "CommandReg.h"
+#include "Command.h"
+#include "DataTypeHandler.h"
 
 /****************************************************************************/
 
 class CommandRegRead:
-    public CommandReg
+    public Command,
+    public DataTypeHandler
 {
     public:
         CommandRegRead();
--- a/tool/CommandRegWrite.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandRegWrite.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -39,7 +39,7 @@
 /*****************************************************************************/
 
 CommandRegWrite::CommandRegWrite():
-    CommandReg("reg_write", "Write data to a slave's registers.")
+    Command("reg_write", "Write data to a slave's registers.")
 {
 }
 
@@ -131,63 +131,18 @@
 
         if (dataType->byteSize) {
             data.length = dataType->byteSize;
-            data.data = new uint8_t[data.length];
-        }
-
-        strValue << args[1];
-        strValue >> resetiosflags(ios::basefield); // guess base from prefix
-        strValue.exceptions(ios::failbit);
+        } else {
+            data.length = 1024; // FIXME
+        }
+
+        data.data = new uint8_t[data.length];
 
         try {
-            if (string(dataType->name) == "int8") {
-                int16_t val; // uint8_t is interpreted as char
-                strValue >> val;
-                if (val > 127 || val < -128)
-                    throw ios::failure("Value out of range");
-                *data.data = (int8_t) val;
-            } else if (string(dataType->name) == "int16") {
-                int16_t val;
-                strValue >> val;
-                *(int16_t *) data.data = cpu_to_le16(val);
-            } else if (string(dataType->name) == "int32") {
-                int32_t val;
-                strValue >> val;
-                *(int32_t *) data.data = cpu_to_le32(val);
-            } else if (string(dataType->name) == "int64") {
-                int64_t val;
-                strValue >> val;
-                *(int64_t *) data.data = cpu_to_le64(val);
-            } else if (string(dataType->name) == "uint8") {
-                uint16_t val; // uint8_t is interpreted as char
-                strValue >> val;
-                if (val > 0xff)
-                    throw ios::failure("Value out of range");
-                *data.data = (uint8_t) val;
-            } else if (string(dataType->name) == "uint16") {
-                uint16_t val;
-                strValue >> val;
-                *(uint16_t *) data.data = cpu_to_le16(val);
-            } else if (string(dataType->name) == "uint32") {
-                uint32_t val;
-                strValue >> val;
-                *(uint32_t *) data.data = cpu_to_le32(val);
-            } else if (string(dataType->name) == "uint64") {
-                uint64_t val;
-                strValue >> val;
-                *(uint64_t *) data.data = cpu_to_le64(val);
-            } else if (string(dataType->name) == "string" ||
-                    string(dataType->name) == "octet_string") {
-                data.length = strValue.str().size();
-                if (!data.length) {
-                    err << "Zero-size string now allowed!";
-                    throwCommandException(err);
-                }
-                data.data = new uint8_t[data.length];
-                strValue >> (char *) data.data;
-            } else {
-                err << "Invalid data type " << dataType->name;
-                throwCommandException(err);
-            }
+            data.length = interpretAsType(
+                    dataType, args[1], data.data, data.length);
+        } catch (SizeException &e) {
+            delete [] data.data;
+            throwCommandException(e.what());
         } catch (ios::failure &e) {
             delete [] data.data;
             err << "Invalid value argument '" << args[1]
--- a/tool/CommandRegWrite.h	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandRegWrite.h	Fri Mar 05 13:39:57 2010 +0100
@@ -30,12 +30,14 @@
 #ifndef __COMMANDREGWRITE_H__
 #define __COMMANDREGWRITE_H__
 
-#include "CommandReg.h"
+#include "Command.h"
+#include "DataTypeHandler.h"
 
 /****************************************************************************/
 
 class CommandRegWrite:
-    public CommandReg
+    public Command,
+    public DataTypeHandler
 {
     public:
         CommandRegWrite();
--- a/tool/CommandSoeRead.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandSoeRead.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -73,6 +73,7 @@
 {
     SlaveList slaves;
     stringstream err, strIdn;
+    const DataType *dataType = NULL;
     ec_ioctl_slave_soe_t data;
 
     if (args.size() != 1) {
@@ -101,7 +102,21 @@
     }
     data.slave_position = slaves.front().position;
 
-	data.mem_size = 1024;
+    if (getDataType().empty()) {
+        dataType = findDataType("raw"); // FIXME
+    } else { // no data type specified
+        if (!(dataType = findDataType(getDataType()))) {
+            err << "Invalid data type '" << getDataType() << "'!";
+            throwInvalidUsageException(err);
+        }
+    }
+
+    if (dataType->byteSize) {
+        data.mem_size = dataType->byteSize;
+    } else {
+        data.mem_size = 1024;
+    }
+
     data.data = new uint8_t[data.mem_size + 1];
 
     try {
@@ -118,25 +133,14 @@
 
     m.close();
 
-	printRawData(data.data, data.data_size);
+    try {
+        outputData(cout, dataType, data.data, data.data_size);
+    } catch (SizeException &e) {
+        delete [] data.data;
+        throwCommandException(e.what());
+    }
 
     delete [] data.data;
 }
 
-/****************************************************************************/
-
-void CommandSoeRead::printRawData(
-        const uint8_t *data,
-        unsigned int size
-        )
-{
-    cout << hex << setfill('0');
-    while (size--) {
-        cout << "0x" << setw(2) << (unsigned int) *data++;
-        if (size)
-            cout << " ";
-    }
-    cout << endl;
-}
-
 /*****************************************************************************/
--- a/tool/CommandSoeRead.h	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandSoeRead.h	Fri Mar 05 13:39:57 2010 +0100
@@ -31,20 +31,19 @@
 #define __COMMANDSOEREAD_H__
 
 #include "Command.h"
+#include "DataTypeHandler.h"
 
 /****************************************************************************/
 
 class CommandSoeRead:
-    public Command
+    public Command,
+    public DataTypeHandler
 {
     public:
         CommandSoeRead();
 
         string helpString() const;
         void execute(const StringVector &);
-
-    protected:
-        static void printRawData(const uint8_t *, unsigned int);
 };
 
 /****************************************************************************/
--- a/tool/CommandUpload.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandUpload.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -173,87 +173,14 @@
 
     m.close();
 
-    if (dataType->byteSize && data.data_size != dataType->byteSize) {
-        err << "Data type mismatch. Expected " << dataType->name
-            << " with " << dataType->byteSize << " byte, but got "
-            << data.data_size << " byte.";
-        throwCommandException(err);
-    }
-
-    cout << setfill('0');
-    switch (dataType->coeCode) {
-        case 0x0002: // int8
-            {
-                int val = (int) *data.target;
-                cout << "0x" << hex << setw(2) << val
-                    << " " << dec << val << endl;
-            }
-            break;
-        case 0x0003: // int16
-            {
-                int16_t val = le16_to_cpup(data.target);
-                cout << "0x" << hex << setw(4) << val
-                    << " " << dec << val << endl;
-            }
-            break;
-        case 0x0004: // int32
-            {
-                int32_t val = le32_to_cpup(data.target);
-                cout << "0x" << hex << setw(8) << val
-                    << " " << dec << val << endl;
-            }
-            break;
-        case 0x0005: // uint8
-            {
-                unsigned int val = (unsigned int) *data.target;
-                cout << "0x" << hex << setw(2) << val
-                    << " " << dec << val << endl;
-            }
-            break;
-        case 0x0006: // uint16
-            {
-                uint16_t val = le16_to_cpup(data.target);
-                cout << "0x" << hex << setw(4) << val
-                    << " " << dec << val << endl;
-            }
-            break;
-        case 0x0007: // uint32
-            {
-                uint32_t val = le32_to_cpup(data.target);
-                cout << "0x" << hex << setw(8) << val
-                    << " " << dec << val << endl;
-            }
-            break;
-        case 0x0009: // string
-            cout << string((const char *) data.target, data.data_size)
-                << endl;
-            break;
-        case 0x000a: // octet_string
-            cout << string((const char *) data.target, data.data_size)
-                << endl;
-            break;
-        default:
-            printRawData(data.target, data.data_size); // FIXME
-            break;
+    try {
+        outputData(cout, dataType, data.target, data.data_size);
+    } catch (SizeException &e) {
+        delete [] data.target;
+        throwCommandException(e.what());
     }
 
     delete [] data.target;
 }
 
-/****************************************************************************/
-
-void CommandUpload::printRawData(
-        const uint8_t *data,
-        unsigned int size
-        )
-{
-    cout << hex << setfill('0');
-    while (size--) {
-        cout << "0x" << setw(2) << (unsigned int) *data++;
-        if (size)
-            cout << " ";
-    }
-    cout << endl;
-}
-
 /*****************************************************************************/
--- a/tool/CommandUpload.h	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/CommandUpload.h	Fri Mar 05 13:39:57 2010 +0100
@@ -45,8 +45,6 @@
 
     protected:
         enum {DefaultBufferSize = 64 * 1024};
-
-        static void printRawData(const uint8_t *, unsigned int);
 };
 
 /****************************************************************************/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/DataTypeHandler.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -0,0 +1,265 @@
+/*****************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License version 2, as
+ *  published by the Free Software Foundation.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+ *  Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  ---
+ *
+ *  The license mentioned above concerns the source code only. Using the
+ *  EtherCAT technology and brand is only permitted in compliance with the
+ *  industrial property and similar rights of Beckhoff Automation GmbH.
+ *
+ ****************************************************************************/
+
+#include <iomanip>
+#include <sstream>
+using namespace std;
+
+#include "DataTypeHandler.h"
+
+#include "ecrt.h"
+
+/*****************************************************************************/
+
+DataTypeHandler::DataTypeHandler()
+{
+}
+
+/****************************************************************************/
+
+const DataTypeHandler::DataType *DataTypeHandler::findDataType(
+        const string &str
+        )
+{
+    const DataType *d;
+    
+    for (d = dataTypes; d->name; d++)
+        if (str == d->name)
+            return d;
+
+    return NULL; // FIXME exception
+}
+
+/****************************************************************************/
+
+const DataTypeHandler::DataType *DataTypeHandler::findDataType(uint16_t code)
+{
+    const DataType *d;
+    
+    for (d = dataTypes; d->name; d++)
+        if (code == d->code)
+            return d;
+
+    return NULL;
+}
+
+/****************************************************************************/
+
+size_t DataTypeHandler::interpretAsType(
+        const DataType *type,
+        const string &source,
+        void *target,
+        size_t targetSize
+        )
+{ 
+    stringstream str;
+    size_t dataSize = type->byteSize;
+
+    str << source;
+    str >> resetiosflags(ios::basefield); // guess base from prefix
+    str.exceptions(ios::failbit);
+
+    switch (type->code) {
+        case 0x0002: // int8
+            {
+                int16_t val; // uint8_t is interpreted as char
+                str >> val;
+                if (val > 127 || val < -128)
+                    throw ios::failure("Value out of range");
+                *(uint8_t *) target = val;
+                break;
+            }
+        case 0x0003: // int16
+            {
+                int16_t val;
+                str >> val;
+                *(int16_t *) target = cpu_to_le16(val);
+                break;
+            }
+        case 0x0004: // int32
+            {
+                int32_t val;
+                str >> val;
+                *(int32_t *) target = cpu_to_le32(val);
+                break;
+            }
+        case 0x0005: // uint8
+            {
+                uint16_t val; // uint8_t is interpreted as char
+                str >> val;
+                if (val > 0xff)
+                    throw ios::failure("Value out of range");
+                *(uint8_t *) target = val;
+                break;
+            }
+        case 0x0006: // uint16
+            {
+                uint16_t val;
+                str >> val;
+                *(uint16_t *) target = cpu_to_le16(val);
+                break;
+            }
+        case 0x0007: // uint32
+            {
+                uint32_t val;
+                str >> val;
+                *(uint32_t *) target = cpu_to_le32(val);
+                break;
+            }
+        case 0x0009: // string
+        case 0x000a: // octet_string
+            dataSize = str.str().size();
+            if (dataSize >= targetSize) {
+                stringstream err;
+                err << "String too large";
+                throw SizeException(err.str());
+            }
+            str >> (char *) target;
+            break;
+
+        default:
+            {
+                stringstream err;
+                err << "Unknown data type 0x" << hex << type->code;
+                throw runtime_error(err.str());
+            }
+    }
+
+    return dataSize;
+}
+
+/****************************************************************************/
+
+void DataTypeHandler::outputData(
+        ostream &o,
+        const DataType *type,
+        void *data,
+        size_t dataSize
+        )
+{ 
+    if (type->byteSize && dataSize != type->byteSize) {
+        stringstream err;
+        err << "Data type mismatch. Expected " << type->name
+            << " with " << type->byteSize << " byte, but got "
+            << dataSize << " byte.";
+        throw SizeException(err.str());
+    }
+
+    o << setfill('0');
+
+    switch (type->code) {
+        case 0x0002: // int8
+            {
+                int val = (int) *(int8_t *) data;
+                o << "0x" << hex << setw(2) << val
+                    << " " << dec << val << endl;
+            }
+            break;
+        case 0x0003: // int16
+            {
+                int16_t val = le16_to_cpup(data);
+                o << "0x" << hex << setw(4) << val
+                    << " " << dec << val << endl;
+            }
+            break;
+        case 0x0004: // int32
+            {
+                int32_t val = le32_to_cpup(data);
+                o << "0x" << hex << setw(8) << val
+                    << " " << dec << val << endl;
+            }
+            break;
+        case 0x0005: // uint8
+            {
+                unsigned int val = (unsigned int) *(uint8_t *) data;
+                o << "0x" << hex << setw(2) << val
+                    << " " << dec << val << endl;
+            }
+            break;
+        case 0x0006: // uint16
+            {
+                uint16_t val = le16_to_cpup(data);
+                o << "0x" << hex << setw(4) << val
+                    << " " << dec << val << endl;
+            }
+            break;
+        case 0x0007: // uint32
+            {
+                uint32_t val = le32_to_cpup(data);
+                o << "0x" << hex << setw(8) << val
+                    << " " << dec << val << endl;
+            }
+            break;
+        case 0x0009: // string
+            o << string((const char *) data, dataSize) << endl;
+            break;
+        case 0x000a: // octet_string
+            o << string((const char *) data, dataSize) << endl;
+            break;
+        default:
+            printRawData(o, (const uint8_t *) data, dataSize); // FIXME
+            break;
+    }
+}
+
+/****************************************************************************/
+
+void DataTypeHandler::printRawData(
+        ostream &o,
+        const uint8_t *data,
+        size_t size
+        )
+{
+    o << hex << setfill('0');
+    while (size--) {
+        o << "0x" << setw(2) << (unsigned int) *data++;
+        if (size)
+            o << " ";
+    }
+    o << endl;
+}
+
+/****************************************************************************/
+
+const DataTypeHandler::DataType DataTypeHandler::dataTypes[] = {
+    {"int8",         0x0002, 1},
+    {"int16",        0x0003, 2},
+    {"int32",        0x0004, 4},
+    {"uint8",        0x0005, 1},
+    {"uint16",       0x0006, 2},
+    {"uint32",       0x0007, 4},
+    {"string",       0x0009, 0},
+    {"octet_string", 0x000a, 0},
+    {"raw",          0xffff, 0},
+    //{"int64",        8},
+    //{"uint64",       8},
+    {}
+};
+
+/*****************************************************************************/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/DataTypeHandler.h	Fri Mar 05 13:39:57 2010 +0100
@@ -0,0 +1,77 @@
+/*****************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License version 2, as
+ *  published by the Free Software Foundation.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+ *  Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  ---
+ *
+ *  The license mentioned above concerns the source code only. Using the
+ *  EtherCAT technology and brand is only permitted in compliance with the
+ *  industrial property and similar rights of Beckhoff Automation GmbH.
+ *
+ ****************************************************************************/
+
+#ifndef __DATATYPEHANDLER_H__
+#define __DATATYPEHANDLER_H__
+
+/****************************************************************************/
+
+#include <stdint.h>
+#include <string>
+#include <stdexcept>
+#include <ostream>
+
+/****************************************************************************/
+
+class DataTypeHandler
+{
+    public:
+        DataTypeHandler();
+
+    protected:
+        struct DataType {
+            const char *name;
+            uint16_t code;
+            size_t byteSize;
+        };
+
+        static const DataType *findDataType(const std::string &);
+        static const DataType *findDataType(uint16_t);
+        static size_t interpretAsType(const DataType *, const std::string &,
+                void *, size_t);
+
+        class SizeException:
+            public std::runtime_error
+        {
+            public:
+                SizeException(const std::string &msg):
+                    runtime_error(msg) {}
+        };
+
+        static void outputData(std::ostream &, const DataType *,
+                void *, size_t);
+        static void printRawData(ostream &, const uint8_t *, size_t);
+
+    private:
+        static const DataType dataTypes[];
+};
+
+/****************************************************************************/
+
+#endif
--- a/tool/Makefile.am	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/Makefile.am	Fri Mar 05 13:39:57 2010 +0100
@@ -49,7 +49,6 @@
 	CommandGraph.cpp \
 	CommandMaster.cpp \
 	CommandPdos.cpp \
-	CommandReg.cpp \
 	CommandRegRead.cpp \
 	CommandRegWrite.cpp \
 	CommandSdos.cpp \
@@ -61,6 +60,7 @@
 	CommandUpload.cpp \
 	CommandVersion.cpp \
 	CommandXml.cpp \
+	DataTypeHandler.cpp \
 	FoeCommand.cpp \
 	MasterDevice.cpp \
 	NumberListParser.cpp \
@@ -88,7 +88,6 @@
 	CommandGraph.h \
 	CommandMaster.h \
 	CommandPdos.h \
-	CommandReg.h \
 	CommandRegRead.h \
 	CommandRegWrite.h \
 	CommandSdos.h \
@@ -100,6 +99,7 @@
 	CommandUpload.h \
 	CommandVersion.h \
 	CommandXml.h \
+	DataTypeHandler.h \
 	FoeCommand.h \
 	MasterDevice.h \
 	NumberListParser.h \
@@ -118,6 +118,9 @@
 		hg id -i $(top_srcdir) 2>/dev/null || echo "unknown"; \
 	fi`
 
-ethercat_CXXFLAGS = -I$(top_srcdir)/master -Wall -DREV=$(REV)
+ethercat_CXXFLAGS = \
+	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/master \
+	-Wall -DREV=$(REV)
 
 #------------------------------------------------------------------------------
--- a/tool/MasterDevice.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/MasterDevice.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -534,10 +534,10 @@
         if (errno == EIO && data->error_code) {
             throw MasterDeviceSoeException(data->error_code);
         } else {
-			stringstream err;
-			err << "Failed to read IDN: " << strerror(errno);
-			throw MasterDeviceException(err);
-		}
+            stringstream err;
+            err << "Failed to read IDN: " << strerror(errno);
+            throw MasterDeviceException(err);
+        }
     }
 }
 
--- a/tool/MasterDevice.h	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/MasterDevice.h	Fri Mar 05 13:39:57 2010 +0100
@@ -34,8 +34,8 @@
 #include <sstream>
 using namespace std;
 
-#include "../include/ecrt.h"
-#include "../master/ioctl.h"
+#include "ecrt.h"
+#include "ioctl.h"
 
 /****************************************************************************/
 
--- a/tool/SdoCommand.cpp	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/SdoCommand.cpp	Fri Mar 05 13:39:57 2010 +0100
@@ -38,32 +38,6 @@
 
 /****************************************************************************/
 
-const SdoCommand::DataType *SdoCommand::findDataType(const string &str)
-{
-    const DataType *d;
-    
-    for (d = dataTypes; d->name; d++)
-        if (str == d->name)
-            return d;
-
-    return NULL;
-}
-
-/****************************************************************************/
-
-const SdoCommand::DataType *SdoCommand::findDataType(uint16_t code)
-{
-    const DataType *d;
-    
-    for (d = dataTypes; d->name; d++)
-        if (code == d->coeCode)
-            return d;
-
-    return NULL;
-}
-
-/****************************************************************************/
-
 const char *SdoCommand::abortText(uint32_t abortCode)
 {
     const AbortMessage *abortMsg;
@@ -77,21 +51,6 @@
     return "???";
 }
 
-/****************************************************************************/
-
-const SdoCommand::DataType SdoCommand::dataTypes[] = {
-    {"int8",         0x0002, 1},
-    {"int16",        0x0003, 2},
-    {"int32",        0x0004, 4},
-    {"uint8",        0x0005, 1},
-    {"uint16",       0x0006, 2},
-    {"uint32",       0x0007, 4},
-    {"string",       0x0009, 0},
-    {"octet_string", 0x000a, 0},
-    {"raw",          0xffff, 0},
-    {}
-};
-
 /*****************************************************************************/
 
 /** SDO abort messages.
--- a/tool/SdoCommand.h	Fri Mar 05 13:20:42 2010 +0100
+++ b/tool/SdoCommand.h	Fri Mar 05 13:39:57 2010 +0100
@@ -31,22 +31,17 @@
 #define __SDOCOMMAND_H__
 
 #include "Command.h"
+#include "DataTypeHandler.h"
 
 /****************************************************************************/
 
 class SdoCommand:
-    public Command
+    public Command,
+    public DataTypeHandler
 {
     public:
         SdoCommand(const string &, const string &);
 
-        struct DataType {
-            const char *name;
-            uint16_t coeCode;
-            unsigned int byteSize;
-        };
-        static const DataType *findDataType(const string &);
-        static const DataType *findDataType(uint16_t);
         static const char *abortText(uint32_t);
 
     private:
@@ -55,7 +50,6 @@
             const char *message;
         };
 
-        static const DataType dataTypes[];
         static const AbortMessage abortMessages[];
 };