tool/DataTypeHandler.cpp
author Florian Pose <fp@igh-essen.com>
Fri, 19 Mar 2010 11:07:49 +0100
changeset 1876 0ebb802ac08d
parent 1875 01d31e749ee2
child 1888 02aedba35b94
permissions -rw-r--r--
TODO.
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  published by the Free Software Foundation.
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  Public License for more details.
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  ---
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  The license mentioned above concerns the source code only. Using the
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 ****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
1837
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
    30
#define DEBUG 0
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
    31
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
    32
#if DEBUG
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
    33
#include <iostream>
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
    34
#endif
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
    35
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <iomanip>
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <sstream>
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
using namespace std;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
#include "DataTypeHandler.h"
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include "ecrt.h"
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
/*****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
DataTypeHandler::DataTypeHandler()
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
{
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
}
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
/****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
const DataTypeHandler::DataType *DataTypeHandler::findDataType(
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
        const string &str
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        )
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
{
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    const DataType *d;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
    
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    for (d = dataTypes; d->name; d++)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
        if (str == d->name)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
            return d;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
    return NULL; // FIXME exception
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
}
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
/****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
1868
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    67
string DataTypeHandler::typeInfo()
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    68
{
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    69
	stringstream s;
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    70
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    71
	s
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    72
		<< "These are valid data types to use with" << endl
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    73
		<< "the --type option:" << endl
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
    74
		<< "  bool," << endl
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
    75
		<< "  int8, int16, int32, int64," << endl
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
    76
		<< "  uint8, uint16, uint32, uint64," << endl
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
    77
		<< "  float, double" << endl
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
    78
		<< "  string, octet_string, unicode_string." << endl;
1868
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    79
	return s.str();
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    80
}
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    81
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    82
/****************************************************************************/
489ea0becd74 Implemented SoE IDN parser for strings like 'P-0-0150'; moved data type
Florian Pose <fp@igh-essen.com>
parents: 1837
diff changeset
    83
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
const DataTypeHandler::DataType *DataTypeHandler::findDataType(uint16_t code)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
{
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
    const DataType *d;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
    
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
    for (d = dataTypes; d->name; d++)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
        if (code == d->code)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
            return d;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
    return NULL;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
}
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
/****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
size_t DataTypeHandler::interpretAsType(
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
        const DataType *type,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
        const string &source,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
        void *target,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
        size_t targetSize
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
        )
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
{ 
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
    stringstream str;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
    size_t dataSize = type->byteSize;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
1837
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   107
#if DEBUG
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   108
	cerr << __func__ << "(targetSize=" << targetSize << ")" << endl;
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   109
#endif
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   110
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    str << source;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    str >> resetiosflags(ios::basefield); // guess base from prefix
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    str.exceptions(ios::failbit);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
1837
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   115
#if DEBUG
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   116
	cerr << "code=" << type->code << endl;
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   117
#endif
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   118
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
    switch (type->code) {
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   120
        case 0x0001: // bool
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   121
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   122
                int16_t val; // uint8_t is interpreted as char
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   123
                str >> val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   124
                if (val > 1 || val < 0)
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   125
                    throw ios::failure("Value out of range");
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   126
                *(uint8_t *) target = val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   127
                break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   128
            }
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
        case 0x0002: // int8
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
                int16_t val; // uint8_t is interpreted as char
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
                str >> val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
                if (val > 127 || val < -128)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
                    throw ios::failure("Value out of range");
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
                *(uint8_t *) target = val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
                break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
        case 0x0003: // int16
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
                int16_t val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
                str >> val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
                *(int16_t *) target = cpu_to_le16(val);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
                break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
        case 0x0004: // int32
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
                int32_t val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
                str >> val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
                *(int32_t *) target = cpu_to_le32(val);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
                break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
        case 0x0005: // uint8
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
                uint16_t val; // uint8_t is interpreted as char
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
                str >> val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
                if (val > 0xff)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
                    throw ios::failure("Value out of range");
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
                *(uint8_t *) target = val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
                break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
        case 0x0006: // uint16
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
                uint16_t val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
                str >> val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
                *(uint16_t *) target = cpu_to_le16(val);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
                break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
        case 0x0007: // uint32
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
                uint32_t val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
                str >> val;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
                *(uint32_t *) target = cpu_to_le32(val);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
                break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
            }
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   175
        case 0x0008: // float
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   176
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   177
                float val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   178
                str >> val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   179
                *(uint32_t *) target =
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   180
					cpu_to_le32(*(uint32_t *) (void *) &val);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   181
                break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   182
            }
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
        case 0x0009: // string
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
        case 0x000a: // octet_string
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   185
        case 0x000b: // unicode_string
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
            dataSize = str.str().size();
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
            if (dataSize >= targetSize) {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
                stringstream err;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
                err << "String too large";
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
                throw SizeException(err.str());
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
            str >> (char *) target;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
            break;
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   194
        case 0x0011: // double
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   195
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   196
                double val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   197
                str >> val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   198
                *(uint64_t *) target =
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   199
					cpu_to_le64(*(uint64_t *) (void *) &val);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   200
                break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   201
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   202
            break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   203
        case 0x0015: // int64
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   204
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   205
                int64_t val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   206
                str >> val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   207
                *(int64_t *) target = cpu_to_le64(val);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   208
                break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   209
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   210
            break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   211
        case 0x001b: // uint64
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   212
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   213
                uint64_t val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   214
                str >> val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   215
                *(uint64_t *) target = cpu_to_le64(val);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   216
                break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   217
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   218
            break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   219
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   220
        case 0x0010: // int24
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   221
        case 0x0012: // int40
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   222
        case 0x0013: // int48
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   223
        case 0x0014: // int56
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   224
        case 0x0016: // uint24
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   225
        case 0x0018: // uint40
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   226
        case 0x0019: // uint48
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   227
        case 0x001a: // uint56
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   228
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   229
                stringstream err;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   230
                err << "Non-native integer type " << type->name
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   231
					<< " is not yet implemented.";
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   232
                throw runtime_error(err.str());
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   233
            }
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
        default:
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
                stringstream err;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
                err << "Unknown data type 0x" << hex << type->code;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
                throw runtime_error(err.str());
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
    }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
1837
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   243
#if DEBUG
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   244
	printRawData(cerr, (const uint8_t *) target, dataSize);
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   245
#endif
32136215c1fa Implemented SoE write state machine and soe_write command.
Florian Pose <fp@igh-essen.com>
parents: 1835
diff changeset
   246
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
    return dataSize;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
}
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
/****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
void DataTypeHandler::outputData(
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
        ostream &o,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
        const DataType *type,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
        void *data,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
        size_t dataSize
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
        )
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
{ 
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
    if (type->byteSize && dataSize != type->byteSize) {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
        stringstream err;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
        err << "Data type mismatch. Expected " << type->name
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
            << " with " << type->byteSize << " byte, but got "
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
            << dataSize << " byte.";
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
        throw SizeException(err.str());
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
    o << setfill('0');
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
    switch (type->code) {
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   270
        case 0x0001: // bool
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
                int val = (int) *(int8_t *) data;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
                o << "0x" << hex << setw(2) << val
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
                    << " " << dec << val << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
            break;
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   277
        case 0x0002: // int8
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   278
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   279
                int val = (int) *(int8_t *) data;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   280
                o << "0x" << hex << setw(2) << val
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   281
                    << " " << dec << val << endl;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   282
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   283
            break;
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
        case 0x0003: // int16
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
                int16_t val = le16_to_cpup(data);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
                o << "0x" << hex << setw(4) << val
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
                    << " " << dec << val << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
            break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
        case 0x0004: // int32
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
                int32_t val = le32_to_cpup(data);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
                o << "0x" << hex << setw(8) << val
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
                    << " " << dec << val << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
            break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
        case 0x0005: // uint8
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
                unsigned int val = (unsigned int) *(uint8_t *) data;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
                o << "0x" << hex << setw(2) << val
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
                    << " " << dec << val << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
            break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
        case 0x0006: // uint16
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
                uint16_t val = le16_to_cpup(data);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
                o << "0x" << hex << setw(4) << val
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
                    << " " << dec << val << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
            break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
        case 0x0007: // uint32
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   313
            {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   314
                uint32_t val = le32_to_cpup(data);
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   315
                o << "0x" << hex << setw(8) << val
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   316
                    << " " << dec << val << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   317
            }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   318
            break;
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   319
        case 0x0008: // float
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   320
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   321
                uint32_t val = le32_to_cpup(data);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   322
				float fval = *(float *) (void *) &val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   323
                o << fval << endl;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   324
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   325
            break;
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   326
        case 0x0009: // string
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   327
            o << string((const char *) data, dataSize) << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   328
            break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   329
        case 0x000a: // octet_string
1875
01d31e749ee2 Omit newline on octet_string output.
Florian Pose <fp@igh-essen.com>
parents: 1871
diff changeset
   330
            o << string((const char *) data, dataSize) << flush;
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   331
            break;
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   332
        case 0x000b: // unicode_string
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   333
			// FIXME encoding
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   334
            o << string((const char *) data, dataSize) << endl;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   335
            break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   336
        case 0x0011: // double
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   337
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   338
                uint64_t val = le64_to_cpup(data);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   339
				double fval = *(double *) (void *) &val;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   340
                o << fval << endl;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   341
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   342
            break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   343
        case 0x0015: // int64
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   344
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   345
                int64_t val = le64_to_cpup(data);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   346
                o << "0x" << hex << setw(16) << val
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   347
                    << " " << dec << val << endl;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   348
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   349
            break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   350
        case 0x001b: // uint64
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   351
            {
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   352
                uint64_t val = le64_to_cpup(data);
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   353
                o << "0x" << hex << setw(16) << val
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   354
                    << " " << dec << val << endl;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   355
            }
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   356
            break;
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   357
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   358
        default:
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   359
            printRawData(o, (const uint8_t *) data, dataSize); // FIXME
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   360
            break;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   361
    }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   362
}
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   363
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   364
/****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   365
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   366
void DataTypeHandler::printRawData(
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   367
        ostream &o,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   368
        const uint8_t *data,
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   369
        size_t size
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   370
        )
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   371
{
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   372
    o << hex << setfill('0');
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   373
    while (size--) {
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   374
        o << "0x" << setw(2) << (unsigned int) *data++;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   375
        if (size)
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   376
            o << " ";
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   377
    }
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   378
    o << endl;
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   379
}
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   380
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   381
/****************************************************************************/
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   382
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   383
const DataTypeHandler::DataType DataTypeHandler::dataTypes[] = {
1871
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   384
    {"bool",           0x0001, 1},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   385
    {"int8",           0x0002, 1},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   386
    {"int16",          0x0003, 2},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   387
    {"int32",          0x0004, 4},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   388
    {"uint8",          0x0005, 1},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   389
    {"uint16",         0x0006, 2},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   390
    {"uint32",         0x0007, 4},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   391
    {"float",          0x0008, 4},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   392
    {"string",         0x0009, 0}, // a. k. a. visible_string
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   393
    {"octet_string",   0x000a, 0},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   394
    {"unicode_string", 0x000b, 0},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   395
	// ... not implemented yet
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   396
    {"int24",          0x0010, 3},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   397
    {"double",         0x0011, 8},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   398
    {"int40",          0x0012, 5},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   399
    {"int48",          0x0013, 6},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   400
    {"int56",          0x0014, 7},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   401
    {"int64",          0x0015, 8},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   402
    {"uint24",         0x0016, 3},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   403
	// reserved        0x0017
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   404
    {"uint40",         0x0018, 5},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   405
    {"uint48",         0x0019, 6},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   406
    {"uint56",         0x001a, 7},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   407
    {"uint64",         0x001b, 8},
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   408
	// reserved        0x001c-0x001f
04ea48971eed Implemented a bunch of CANopen data types for the command-line tool.
Florian Pose <fp@igh-essen.com>
parents: 1868
diff changeset
   409
    {"raw",            0xffff, 0},
1835
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   410
    {}
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   411
};
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   412
20748e9d2238 Sourced out data type handling into an own class.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   413
/*****************************************************************************/