author | Florian Pose <fp@igh-essen.com> |
Fri, 01 Aug 2008 09:32:35 +0000 | |
changeset 1183 | d77f634ab0b5 |
parent 1166 | 006244d53f68 |
child 1184 | 75cd6681eb08 |
permissions | -rw-r--r-- |
1142 | 1 |
/***************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
5 |
****************************************************************************/ |
|
6 |
||
7 |
#include <iostream> |
|
8 |
#include <iomanip> |
|
9 |
using namespace std; |
|
10 |
||
11 |
#include "CommandDownload.h" |
|
12 |
#include "coe_datatypes.h" |
|
13 |
#include "byteorder.h" |
|
14 |
||
15 |
/*****************************************************************************/ |
|
16 |
||
17 |
CommandDownload::CommandDownload(): |
|
18 |
Command("download", "Write an Sdo entry to a slave.") |
|
19 |
{ |
|
20 |
} |
|
21 |
||
22 |
/*****************************************************************************/ |
|
23 |
||
24 |
string CommandDownload::helpString() const |
|
25 |
{ |
|
26 |
stringstream str; |
|
27 |
||
28 |
str << getName() << " [OPTIONS] <INDEX> <SUBINDEX> <VALUE>" << endl |
|
29 |
<< endl |
|
30 |
<< getBriefDescription() << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
31 |
<< endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
32 |
<< "This command requires a single slave to be selected." << endl |
1142 | 33 |
<< endl |
34 |
<< "The data type of the Sdo entry is taken from the Sdo" << endl |
|
35 |
<< "dictionary by default. It can be overridden with the" << endl |
|
36 |
<< "--type option. If the slave does not support the Sdo" << endl |
|
37 |
<< "information service or the Sdo is not in the dictionary," << endl |
|
38 |
<< "the --type option is mandatory." << endl |
|
39 |
<< endl |
|
40 |
<< "These are the valid Sdo entry data types:" << endl |
|
41 |
<< " int8, int16, int32, uint8, uint16, uint32, string." << endl |
|
42 |
<< endl |
|
1144
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
43 |
<< "Arguments:" << endl |
1142 | 44 |
<< " INDEX is the Sdo index and must be an unsigned" << endl |
45 |
<< " 16 bit number." << endl |
|
46 |
<< " SUBINDEX is the Sdo entry subindex and must be an" << endl |
|
47 |
<< " unsigned 8 bit number." << endl |
|
48 |
<< " VALUE is the value to download and must correspond" << endl |
|
49 |
<< " to the Sdo entry datatype (see above)." << endl |
|
50 |
<< endl |
|
51 |
<< "Command-specific options:" << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
52 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
53 |
<< " --position -p <pos> Slave selection. See the help of" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
54 |
<< " the 'slaves' command." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
55 |
<< " --type -t <type> Sdo entry data type (see above)." << endl |
1142 | 56 |
<< endl |
57 |
<< numericInfo(); |
|
58 |
||
59 |
return str.str(); |
|
60 |
} |
|
61 |
||
62 |
/****************************************************************************/ |
|
63 |
||
64 |
void CommandDownload::execute(MasterDevice &m, const StringVector &args) |
|
65 |
{ |
|
66 |
stringstream strIndex, strSubIndex, strValue, err; |
|
67 |
ec_ioctl_slave_sdo_download_t data; |
|
68 |
unsigned int number; |
|
69 |
const CoEDataType *dataType = NULL; |
|
1151 | 70 |
SlaveList slaves; |
1142 | 71 |
|
72 |
if (args.size() != 3) { |
|
73 |
err << "'" << getName() << "' takes 3 arguments!"; |
|
74 |
throwInvalidUsageException(err); |
|
75 |
} |
|
76 |
||
77 |
strIndex << args[0]; |
|
78 |
strIndex |
|
79 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
80 |
>> data.sdo_index; |
|
81 |
if (strIndex.fail()) { |
|
82 |
err << "Invalid Sdo index '" << args[0] << "'!"; |
|
83 |
throwInvalidUsageException(err); |
|
84 |
} |
|
85 |
||
86 |
strSubIndex << args[1]; |
|
87 |
strSubIndex |
|
88 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
89 |
>> number; |
|
90 |
if (strSubIndex.fail() || number > 0xff) { |
|
91 |
err << "Invalid Sdo subindex '" << args[1] << "'!"; |
|
92 |
throwInvalidUsageException(err); |
|
93 |
} |
|
94 |
data.sdo_entry_subindex = number; |
|
95 |
||
1151 | 96 |
m.open(MasterDevice::ReadWrite); |
97 |
slaves = selectedSlaves(m); |
|
98 |
if (slaves.size() != 1) { |
|
1155
bd4e5b544473
Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
99 |
throwSingleSlaveRequired(slaves.size()); |
1151 | 100 |
} |
101 |
data.slave_position = slaves.front().position; |
|
102 |
||
1166 | 103 |
if (!getDataType().empty()) { // data type specified |
104 |
if (!(dataType = findDataType(getDataType()))) { |
|
105 |
err << "Invalid data type '" << getDataType() << "'!"; |
|
1142 | 106 |
throwInvalidUsageException(err); |
107 |
} |
|
108 |
} else { // no data type specified: fetch from dictionary |
|
109 |
ec_ioctl_slave_sdo_entry_t entry; |
|
110 |
||
111 |
try { |
|
1151 | 112 |
m.getSdoEntry(&entry, data.slave_position, |
1142 | 113 |
data.sdo_index, data.sdo_entry_subindex); |
114 |
} catch (MasterDeviceException &e) { |
|
115 |
err << "Failed to determine Sdo entry data type. " |
|
116 |
<< "Please specify --type."; |
|
117 |
throwCommandException(err); |
|
118 |
} |
|
119 |
if (!(dataType = findDataType(entry.data_type))) { |
|
120 |
err << "Pdo entry has unknown data type 0x" |
|
121 |
<< hex << setfill('0') << setw(4) << entry.data_type << "!" |
|
122 |
<< " Please specify --type."; |
|
123 |
throwCommandException(err); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
if (dataType->byteSize) { |
|
128 |
data.data_size = dataType->byteSize; |
|
129 |
} else { |
|
130 |
data.data_size = DefaultBufferSize; |
|
131 |
} |
|
132 |
||
133 |
data.data = new uint8_t[data.data_size + 1]; |
|
134 |
||
135 |
strValue << args[2]; |
|
136 |
strValue >> resetiosflags(ios::basefield); // guess base from prefix |
|
137 |
strValue.exceptions(ios::failbit); |
|
138 |
||
139 |
try { |
|
140 |
switch (dataType->coeCode) { |
|
141 |
case 0x0002: // int8 |
|
142 |
{ |
|
143 |
int16_t val; // uint8_t is interpreted as char |
|
144 |
strValue >> val; |
|
145 |
if (val > 127 || val < -128) |
|
146 |
throw ios::failure("Value out of range"); |
|
147 |
*data.data = val; |
|
148 |
break; |
|
149 |
} |
|
150 |
case 0x0003: // int16 |
|
151 |
{ |
|
152 |
int16_t val; |
|
153 |
strValue >> val; |
|
154 |
*(int16_t *) data.data = cputole16(val); |
|
155 |
break; |
|
156 |
} |
|
157 |
case 0x0004: // int32 |
|
158 |
{ |
|
159 |
int32_t val; |
|
160 |
strValue >> val; |
|
161 |
*(int32_t *) data.data = cputole32(val); |
|
162 |
break; |
|
163 |
} |
|
164 |
case 0x0005: // uint8 |
|
165 |
{ |
|
166 |
uint16_t val; // uint8_t is interpreted as char |
|
167 |
strValue >> val; |
|
168 |
if (val > 0xff) |
|
169 |
throw ios::failure("Value out of range"); |
|
170 |
*data.data = val; |
|
171 |
break; |
|
172 |
} |
|
173 |
case 0x0006: // uint16 |
|
174 |
{ |
|
175 |
uint16_t val; |
|
176 |
strValue >> val; |
|
177 |
*(uint16_t *) data.data = cputole16(val); |
|
178 |
break; |
|
179 |
} |
|
180 |
case 0x0007: // uint32 |
|
181 |
{ |
|
182 |
uint32_t val; |
|
183 |
strValue >> val; |
|
184 |
*(uint32_t *) data.data = cputole32(val); |
|
185 |
break; |
|
186 |
} |
|
187 |
case 0x0009: // string |
|
188 |
if (strValue.str().size() >= data.data_size) { |
|
189 |
err << "String too large"; |
|
190 |
throwCommandException(err); |
|
191 |
} |
|
192 |
data.data_size = strValue.str().size(); |
|
193 |
strValue >> (char *) data.data; |
|
194 |
break; |
|
195 |
||
196 |
default: |
|
197 |
delete [] data.data; |
|
198 |
err << "Unknown data type 0x" << hex << dataType->coeCode; |
|
199 |
throwCommandException(err); |
|
200 |
} |
|
201 |
} catch (ios::failure &e) { |
|
202 |
delete [] data.data; |
|
203 |
err << "Invalid value argument '" << args[2] |
|
204 |
<< "' for type '" << dataType->name << "'!"; |
|
205 |
throwInvalidUsageException(err); |
|
206 |
} |
|
207 |
||
208 |
try { |
|
1151 | 209 |
m.sdoDownload(&data); |
1142 | 210 |
} catch(MasterDeviceException &e) { |
211 |
delete [] data.data; |
|
212 |
throw e; |
|
213 |
} |
|
214 |
||
215 |
delete [] data.data; |
|
216 |
} |
|
217 |
||
218 |
/*****************************************************************************/ |