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