author | Florian Pose <fp@igh-essen.com> |
Thu, 09 Oct 2008 14:35:08 +0000 | |
changeset 1256 | 45fe0af4340b |
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 "CommandUpload.h" |
|
12 |
||
13 |
/*****************************************************************************/ |
|
14 |
||
15 |
CommandUpload::CommandUpload(): |
|
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("upload", "Read an Sdo entry from a slave.") |
1142 | 17 |
{ |
18 |
} |
|
19 |
||
20 |
/*****************************************************************************/ |
|
21 |
||
22 |
string CommandUpload::helpString() const |
|
23 |
{ |
|
24 |
stringstream str; |
|
25 |
||
26 |
str << getName() << " [OPTIONS] <INDEX> <SUBINDEX>" << endl |
|
27 |
<< endl |
|
28 |
<< getBriefDescription() << endl |
|
29 |
<< endl |
|
1157
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 |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
31 |
<< endl |
1142 | 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 |
|
41 |
<< "Arguments:" << endl |
|
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 |
<< endl |
|
47 |
<< "Command-specific options:" << endl |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
48 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
49 |
<< " --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
|
50 |
<< " the 'slaves' command." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1155
diff
changeset
|
51 |
<< " --type -t <type> Sdo entry data type (see above)." << endl |
1142 | 52 |
<< endl |
53 |
<< numericInfo(); |
|
54 |
||
55 |
return str.str(); |
|
56 |
} |
|
57 |
||
58 |
/****************************************************************************/ |
|
59 |
||
60 |
void CommandUpload::execute(MasterDevice &m, const StringVector &args) |
|
61 |
{ |
|
1151 | 62 |
SlaveList slaves; |
1142 | 63 |
stringstream err, strIndex, strSubIndex; |
64 |
int sval; |
|
65 |
ec_ioctl_slave_sdo_upload_t data; |
|
66 |
unsigned int uval; |
|
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; |
1142 | 68 |
|
69 |
if (args.size() != 2) { |
|
70 |
err << "'" << getName() << "' takes two arguments!"; |
|
71 |
throwInvalidUsageException(err); |
|
72 |
} |
|
73 |
||
74 |
strIndex << args[0]; |
|
75 |
strIndex |
|
76 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
77 |
>> data.sdo_index; |
|
78 |
if (strIndex.fail()) { |
|
79 |
err << "Invalid Sdo index '" << args[0] << "'!"; |
|
80 |
throwInvalidUsageException(err); |
|
81 |
} |
|
82 |
||
83 |
strSubIndex << args[1]; |
|
84 |
strSubIndex |
|
85 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
86 |
>> uval; |
|
87 |
if (strSubIndex.fail() || uval > 0xff) { |
|
88 |
err << "Invalid Sdo subindex '" << args[1] << "'!"; |
|
89 |
throwInvalidUsageException(err); |
|
90 |
} |
|
91 |
data.sdo_entry_subindex = uval; |
|
92 |
||
1151 | 93 |
m.open(MasterDevice::Read); |
94 |
slaves = selectedSlaves(m); |
|
95 |
if (slaves.size() != 1) { |
|
1155
bd4e5b544473
Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
96 |
throwSingleSlaveRequired(slaves.size()); |
1151 | 97 |
} |
98 |
data.slave_position = slaves.front().position; |
|
99 |
||
1166 | 100 |
if (!getDataType().empty()) { // data type specified |
101 |
if (!(dataType = findDataType(getDataType()))) { |
|
102 |
err << "Invalid data type '" << getDataType() << "'!"; |
|
1142 | 103 |
throwInvalidUsageException(err); |
104 |
} |
|
105 |
} else { // no data type specified: fetch from dictionary |
|
106 |
ec_ioctl_slave_sdo_entry_t entry; |
|
107 |
||
108 |
try { |
|
1151 | 109 |
m.getSdoEntry(&entry, data.slave_position, |
1142 | 110 |
data.sdo_index, data.sdo_entry_subindex); |
111 |
} catch (MasterDeviceException &e) { |
|
112 |
err << "Failed to determine Sdo entry data type. " |
|
113 |
<< "Please specify --type."; |
|
114 |
throwCommandException(err); |
|
115 |
} |
|
116 |
if (!(dataType = findDataType(entry.data_type))) { |
|
117 |
err << "Pdo entry has unknown data type 0x" |
|
118 |
<< hex << setfill('0') << setw(4) << entry.data_type << "!" |
|
119 |
<< " Please specify --type."; |
|
120 |
throwCommandException(err); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
if (dataType->byteSize) { |
|
125 |
data.target_size = dataType->byteSize; |
|
126 |
} else { |
|
127 |
data.target_size = DefaultBufferSize; |
|
128 |
} |
|
129 |
||
130 |
data.target = new uint8_t[data.target_size + 1]; |
|
131 |
||
132 |
try { |
|
133 |
m.sdoUpload(&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
|
134 |
} 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
|
135 |
delete [] data.target; |
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
|
136 |
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
|
137 |
<< 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
|
138 |
<< ": " << 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
|
139 |
throwCommandException(err); |
1142 | 140 |
} catch (MasterDeviceException &e) { |
141 |
delete [] data.target; |
|
142 |
throw e; |
|
143 |
} |
|
144 |
||
145 |
m.close(); |
|
146 |
||
147 |
if (dataType->byteSize && data.data_size != dataType->byteSize) { |
|
148 |
err << "Data type mismatch. Expected " << dataType->name |
|
149 |
<< " with " << dataType->byteSize << " byte, but got " |
|
150 |
<< data.data_size << " byte."; |
|
151 |
throwCommandException(err); |
|
152 |
} |
|
153 |
||
154 |
cout << setfill('0'); |
|
155 |
switch (dataType->coeCode) { |
|
156 |
case 0x0002: // int8 |
|
157 |
sval = *(int8_t *) data.target; |
|
158 |
cout << sval << " 0x" << hex << setw(2) << sval << endl; |
|
159 |
break; |
|
160 |
case 0x0003: // int16 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
161 |
sval = le16_to_cpup(data.target); |
1142 | 162 |
cout << sval << " 0x" << hex << setw(4) << sval << endl; |
163 |
break; |
|
164 |
case 0x0004: // int32 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
165 |
sval = le32_to_cpup(data.target); |
1142 | 166 |
cout << sval << " 0x" << hex << setw(8) << sval << endl; |
167 |
break; |
|
168 |
case 0x0005: // uint8 |
|
169 |
uval = (unsigned int) *(uint8_t *) data.target; |
|
170 |
cout << uval << " 0x" << hex << setw(2) << uval << endl; |
|
171 |
break; |
|
172 |
case 0x0006: // uint16 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
173 |
uval = le16_to_cpup(data.target); |
1142 | 174 |
cout << uval << " 0x" << hex << setw(4) << uval << endl; |
175 |
break; |
|
176 |
case 0x0007: // uint32 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1184
diff
changeset
|
177 |
uval = le32_to_cpup(data.target); |
1142 | 178 |
cout << uval << " 0x" << hex << setw(8) << uval << endl; |
179 |
break; |
|
180 |
case 0x0009: // string |
|
181 |
cout << string((const char *) data.target, data.data_size) |
|
182 |
<< endl; |
|
183 |
break; |
|
184 |
default: |
|
185 |
printRawData(data.target, data.data_size); // FIXME |
|
186 |
break; |
|
187 |
} |
|
188 |
||
189 |
delete [] data.target; |
|
190 |
} |
|
191 |
||
192 |
/****************************************************************************/ |
|
193 |
||
194 |
void CommandUpload::printRawData( |
|
195 |
const uint8_t *data, |
|
196 |
unsigned int size |
|
197 |
) |
|
198 |
{ |
|
199 |
cout << hex << setfill('0'); |
|
200 |
while (size--) { |
|
201 |
cout << "0x" << setw(2) << (unsigned int) *data++; |
|
202 |
if (size) |
|
203 |
cout << " "; |
|
204 |
} |
|
205 |
cout << endl; |
|
206 |
} |
|
207 |
||
208 |
/*****************************************************************************/ |