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