|
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 |
|
50 << " --type -t <type> Forced Sdo entry data type (see" << endl |
|
51 << " above)." << endl |
|
52 << endl |
|
53 << numericInfo(); |
|
54 |
|
55 return str.str(); |
|
56 } |
|
57 |
|
58 /****************************************************************************/ |
|
59 |
|
60 void CommandUpload::execute(MasterDevice &m, const StringVector &args) |
|
61 { |
|
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 (slavePosition < 0) { |
|
69 err << "'" << getName() << "' requires a slave! " |
|
70 << "Please specify --slave."; |
|
71 throwInvalidUsageException(err); |
|
72 } |
|
73 data.slave_position = slavePosition; |
|
74 |
|
75 if (args.size() != 2) { |
|
76 err << "'" << getName() << "' takes two arguments!"; |
|
77 throwInvalidUsageException(err); |
|
78 } |
|
79 |
|
80 strIndex << args[0]; |
|
81 strIndex |
|
82 >> resetiosflags(ios::basefield) // guess base from prefix |
|
83 >> data.sdo_index; |
|
84 if (strIndex.fail()) { |
|
85 err << "Invalid Sdo index '" << args[0] << "'!"; |
|
86 throwInvalidUsageException(err); |
|
87 } |
|
88 |
|
89 strSubIndex << args[1]; |
|
90 strSubIndex |
|
91 >> resetiosflags(ios::basefield) // guess base from prefix |
|
92 >> uval; |
|
93 if (strSubIndex.fail() || uval > 0xff) { |
|
94 err << "Invalid Sdo subindex '" << args[1] << "'!"; |
|
95 throwInvalidUsageException(err); |
|
96 } |
|
97 data.sdo_entry_subindex = uval; |
|
98 |
|
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 m.open(MasterDevice::Read); |
|
108 |
|
109 try { |
|
110 m.getSdoEntry(&entry, slavePosition, |
|
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.target_size = dataType->byteSize; |
|
127 } else { |
|
128 data.target_size = DefaultBufferSize; |
|
129 } |
|
130 |
|
131 data.target = new uint8_t[data.target_size + 1]; |
|
132 |
|
133 m.open(MasterDevice::Read); |
|
134 |
|
135 try { |
|
136 m.sdoUpload(&data); |
|
137 } catch (MasterDeviceException &e) { |
|
138 delete [] data.target; |
|
139 throw e; |
|
140 } |
|
141 |
|
142 m.close(); |
|
143 |
|
144 if (dataType->byteSize && data.data_size != dataType->byteSize) { |
|
145 err << "Data type mismatch. Expected " << dataType->name |
|
146 << " with " << dataType->byteSize << " byte, but got " |
|
147 << data.data_size << " byte."; |
|
148 throwCommandException(err); |
|
149 } |
|
150 |
|
151 cout << setfill('0'); |
|
152 switch (dataType->coeCode) { |
|
153 case 0x0002: // int8 |
|
154 sval = *(int8_t *) data.target; |
|
155 cout << sval << " 0x" << hex << setw(2) << sval << endl; |
|
156 break; |
|
157 case 0x0003: // int16 |
|
158 sval = le16tocpu(*(int16_t *) data.target); |
|
159 cout << sval << " 0x" << hex << setw(4) << sval << endl; |
|
160 break; |
|
161 case 0x0004: // int32 |
|
162 sval = le32tocpu(*(int32_t *) data.target); |
|
163 cout << sval << " 0x" << hex << setw(8) << sval << endl; |
|
164 break; |
|
165 case 0x0005: // uint8 |
|
166 uval = (unsigned int) *(uint8_t *) data.target; |
|
167 cout << uval << " 0x" << hex << setw(2) << uval << endl; |
|
168 break; |
|
169 case 0x0006: // uint16 |
|
170 uval = le16tocpu(*(uint16_t *) data.target); |
|
171 cout << uval << " 0x" << hex << setw(4) << uval << endl; |
|
172 break; |
|
173 case 0x0007: // uint32 |
|
174 uval = le32tocpu(*(uint32_t *) data.target); |
|
175 cout << uval << " 0x" << hex << setw(8) << uval << endl; |
|
176 break; |
|
177 case 0x0009: // string |
|
178 cout << string((const char *) data.target, data.data_size) |
|
179 << endl; |
|
180 break; |
|
181 default: |
|
182 printRawData(data.target, data.data_size); // FIXME |
|
183 break; |
|
184 } |
|
185 |
|
186 delete [] data.target; |
|
187 } |
|
188 |
|
189 /****************************************************************************/ |
|
190 |
|
191 void CommandUpload::printRawData( |
|
192 const uint8_t *data, |
|
193 unsigned int size |
|
194 ) |
|
195 { |
|
196 cout << hex << setfill('0'); |
|
197 while (size--) { |
|
198 cout << "0x" << setw(2) << (unsigned int) *data++; |
|
199 if (size) |
|
200 cout << " "; |
|
201 } |
|
202 cout << endl; |
|
203 } |
|
204 |
|
205 /*****************************************************************************/ |