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