tool/CommandRegWrite.cpp
author Florian Pose <fp@igh-essen.com>
Mon, 04 May 2009 12:10:11 +0000
changeset 1435 f1df39224f49
parent 1431 70e891465da1
child 1804 742607c464c4
permissions -rw-r--r--
Take ref clock address from dc_ref_clock pointer.
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  published by the Free Software Foundation.
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  Public License for more details.
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  ---
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  The license mentioned above concerns the source code only. Using the
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 ****************************************************************************/
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#include <iostream>
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#include <iomanip>
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include <fstream>
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
using namespace std;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include "CommandRegWrite.h"
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include "sii_crc.h"
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
/*****************************************************************************/
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
CommandRegWrite::CommandRegWrite():
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    41
    CommandReg("reg_write", "Write data to a slave's registers.")
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
{
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
}
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
/*****************************************************************************/
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
string CommandRegWrite::helpString() const
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
{
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
    stringstream str;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    51
    str << getName() << " [OPTIONS] <OFFSET> <DATA>" << endl
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
        << endl 
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
        << getBriefDescription() << endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        << endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
        << "This command requires a single slave to be selected." << endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    	<< endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
        << "Arguments:" << endl
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    58
        << "  OFFSET  is the register address to write to." << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    59
        << "  DATA    depends on whether a datatype was specified" << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    60
		<< "          with the --type option: If not, DATA must be" << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    61
		<< "          either a path to a file with data to write," << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    62
		<< "          or '-', which means, that data are read from" << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    63
		<< "          stdin. If a datatype was specified, VALUE is" << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    64
		<< "          interpreted respective to the given type." << endl
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
        << endl
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    66
        << "These are the valid data types:" << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    67
        << "  int8, int16, int32, int64, uint8, uint16, uint32," << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    68
        << "  uint64, string." << endl
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    69
		<< endl
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
        << "Command-specific options:" << endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
        << "  --alias    -a <alias>" << endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
        << "  --position -p <pos>    Slave selection. See the help of" << endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
        << "                         the 'slaves' command." << endl
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
    74
        << "  --type     -t <type>   Data type (see above)." << endl
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
        << endl
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
        << numericInfo();
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
    return str.str();
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
}
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
/****************************************************************************/
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
void CommandRegWrite::execute(MasterDevice &m, const StringVector &args)
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
{
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
    stringstream strOffset, err;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
    ec_ioctl_slave_reg_t data;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
    ifstream file;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
    SlaveList slaves;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
    if (args.size() != 2) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
        err << "'" << getName() << "' takes exactly two arguments!";
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
        throwInvalidUsageException(err);
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
    
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    strOffset << args[0];
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    strOffset
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
        >> resetiosflags(ios::basefield) // guess base from prefix
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
        >> data.offset;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    if (strOffset.fail()) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
        err << "Invalid offset '" << args[0] << "'!";
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
        throwInvalidUsageException(err);
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   104
	if (getDataType().empty()) {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   105
		if (args[1] == "-") {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   106
			loadRegData(&data, cin);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   107
		} else {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   108
			file.open(args[1].c_str(), ifstream::in | ifstream::binary);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   109
			if (file.fail()) {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   110
				err << "Failed to open '" << args[1] << "'!";
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   111
				throwCommandException(err);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   112
			}
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   113
			loadRegData(&data, file);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   114
			file.close();
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   115
		}
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   116
	} else {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   117
		stringstream strValue;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   118
		const DataType *dataType = findDataType(getDataType());
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   119
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   120
        if (!dataType) {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   121
            err << "Invalid data type '" << getDataType() << "'!";
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   122
            throwInvalidUsageException(err);
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
        }
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   124
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   125
		if (dataType->byteSize) {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   126
			data.length = dataType->byteSize;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   127
			data.data = new uint8_t[data.length];
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   128
		}
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   129
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   130
		strValue << args[1];
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   131
		strValue >> resetiosflags(ios::basefield); // guess base from prefix
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   132
		strValue.exceptions(ios::failbit);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   133
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   134
		try {
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   135
			if (string(dataType->name) == "int8") {
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   136
				int16_t val; // uint8_t is interpreted as char
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   137
				strValue >> val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   138
				if (val > 127 || val < -128)
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   139
					throw ios::failure("Value out of range");
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   140
				*data.data = (int8_t) val;
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   141
			} else if (string(dataType->name) == "int16") {
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   142
				int16_t val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   143
				strValue >> val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   144
				*(int16_t *) data.data = cpu_to_le16(val);
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   145
			} else if (string(dataType->name) == "int32") {
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   146
				int32_t val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   147
				strValue >> val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   148
				*(int32_t *) data.data = cpu_to_le32(val);
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   149
			} else if (string(dataType->name) == "int64") {
1395
e9fe40c458cc Added 64-bit types to reg_write.
Florian Pose <fp@igh-essen.com>
parents: 1389
diff changeset
   150
				int64_t val;
e9fe40c458cc Added 64-bit types to reg_write.
Florian Pose <fp@igh-essen.com>
parents: 1389
diff changeset
   151
				strValue >> val;
e9fe40c458cc Added 64-bit types to reg_write.
Florian Pose <fp@igh-essen.com>
parents: 1389
diff changeset
   152
				*(int64_t *) data.data = cpu_to_le64(val);
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   153
			} else if (string(dataType->name) == "uint8") {
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   154
				uint16_t val; // uint8_t is interpreted as char
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   155
				strValue >> val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   156
				if (val > 0xff)
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   157
					throw ios::failure("Value out of range");
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   158
				*data.data = (uint8_t) val;
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   159
			} else if (string(dataType->name) == "uint16") {
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   160
				uint16_t val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   161
				strValue >> val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   162
				*(uint16_t *) data.data = cpu_to_le16(val);
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   163
			} else if (string(dataType->name) == "uint32") {
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   164
				uint32_t val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   165
				strValue >> val;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   166
				*(uint32_t *) data.data = cpu_to_le32(val);
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   167
			} else if (string(dataType->name) == "uint64") {
1395
e9fe40c458cc Added 64-bit types to reg_write.
Florian Pose <fp@igh-essen.com>
parents: 1389
diff changeset
   168
				uint64_t val;
e9fe40c458cc Added 64-bit types to reg_write.
Florian Pose <fp@igh-essen.com>
parents: 1389
diff changeset
   169
				strValue >> val;
e9fe40c458cc Added 64-bit types to reg_write.
Florian Pose <fp@igh-essen.com>
parents: 1389
diff changeset
   170
				*(uint64_t *) data.data = cpu_to_le64(val);
1431
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   171
			} else if (string(dataType->name) == "string" ||
70e891465da1 Fixed const char * comparison.
Florian Pose <fp@igh-essen.com>
parents: 1395
diff changeset
   172
					string(dataType->name) == "octet_string") {
1389
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   173
				data.length = strValue.str().size();
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   174
				if (!data.length) {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   175
					err << "Zero-size string now allowed!";
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   176
					throwCommandException(err);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   177
				}
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   178
				data.data = new uint8_t[data.length];
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   179
				strValue >> (char *) data.data;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   180
			} else {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   181
				err << "Invalid data type " << dataType->name;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   182
				throwCommandException(err);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   183
			}
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   184
		} catch (ios::failure &e) {
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   185
			delete [] data.data;
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   186
			err << "Invalid value argument '" << args[1]
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   187
				<< "' for type '" << dataType->name << "'!";
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   188
			throwInvalidUsageException(err);
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   189
		}
5b392b22a2da reg_write with data types.
Florian Pose <fp@igh-essen.com>
parents: 1388
diff changeset
   190
	}
1388
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
    if ((uint32_t) data.offset + data.length > 0xffff) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
        err << "Offset and length exceeding 64k!";
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
        delete [] data.data;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
        throwInvalidUsageException(err);
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
    try {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
        m.open(MasterDevice::ReadWrite);
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
    } catch (MasterDeviceException &e) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
        delete [] data.data;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
        throw e;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
    slaves = selectedSlaves(m);
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
    if (slaves.size() != 1) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
        delete [] data.data;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
        throwSingleSlaveRequired(slaves.size());
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    data.slave_position = slaves.front().position;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
    // send data to master
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
    try {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
        m.writeReg(&data);
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
    } catch (MasterDeviceException &e) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
        delete [] data.data;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
        throw e;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
    if (getVerbosity() == Verbose) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
        cerr << "Register writing finished." << endl;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
    delete [] data.data;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
}
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
/*****************************************************************************/
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
void CommandRegWrite::loadRegData(
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
        ec_ioctl_slave_reg_t *data,
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
        const istream &in
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
        )
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
{
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
    stringstream err;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
    ostringstream tmp;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
    tmp << in.rdbuf();
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
    string const &contents = tmp.str();
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
    if (getVerbosity() == Verbose) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
        cerr << "Read " << contents.size() << " bytes of data." << endl;
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
    if (contents.size() > 0xffff) {
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
        err << "Invalid data size " << contents.size() << "!";
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
        throwInvalidUsageException(err);
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
    }
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
    data->length = contents.size();
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
    // allocate buffer and read file into buffer
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
    data->data = new uint8_t[data->length];
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
    contents.copy((char *) data->data, contents.size());
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
}
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
3c886ec376f5 Renamed phy_ commands to reg_.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
/*****************************************************************************/