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 |
#include <fstream> |
|
10 |
using namespace std; |
|
11 |
||
12 |
#include "CommandSiiWrite.h" |
|
13 |
#include "sii_crc.h" |
|
14 |
#include "byteorder.h" |
|
15 |
||
16 |
/*****************************************************************************/ |
|
17 |
||
18 |
CommandSiiWrite::CommandSiiWrite(): |
|
19 |
Command("sii_write", "Write SII contents to a slave.") |
|
20 |
{ |
|
21 |
} |
|
22 |
||
23 |
/*****************************************************************************/ |
|
24 |
||
25 |
string CommandSiiWrite::helpString() const |
|
26 |
{ |
|
27 |
stringstream str; |
|
28 |
||
29 |
str << getName() << " [OPTIONS] <FILENAME>" << endl |
|
30 |
<< endl |
|
31 |
<< getBriefDescription() << endl |
|
32 |
<< endl |
|
33 |
<< "The file contents are checked for validity and integrity." << endl |
|
34 |
<< "These checks can be overridden with the --force option." << endl |
|
35 |
<< endl |
|
36 |
<< "Arguments:" << endl |
|
37 |
<< " FILENAME must be a path to a file that contains a" << endl |
|
38 |
<< " positive number of words." << endl |
|
39 |
<< endl |
|
40 |
<< "Command-specific options:" << endl |
|
41 |
<< " --slave -s <index> Positive numerical ring position" << endl |
|
42 |
<< " (mandatory)." << endl |
|
43 |
<< " --force -f Override validity checks." << endl |
|
44 |
<< endl |
|
45 |
<< numericInfo(); |
|
46 |
||
47 |
return str.str(); |
|
48 |
} |
|
49 |
||
50 |
/****************************************************************************/ |
|
51 |
||
52 |
void CommandSiiWrite::execute(MasterDevice &m, const StringVector &args) |
|
53 |
{ |
|
1151 | 54 |
SlaveList slaves; |
1142 | 55 |
stringstream err; |
56 |
ec_ioctl_slave_sii_t data; |
|
57 |
ifstream file; |
|
58 |
unsigned int byte_size; |
|
59 |
const uint16_t *categoryHeader; |
|
60 |
uint16_t categoryType, categorySize; |
|
61 |
uint8_t crc; |
|
62 |
||
1151 | 63 |
slaves = selectedSlaves(m); |
64 |
||
65 |
if (slaves.size() != 1) { |
|
1155
bd4e5b544473
Common message for single slave selections.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
66 |
throwSingleSlaveRequired(slaves.size()); |
1142 | 67 |
} |
1151 | 68 |
data.slave_position = slaves.front().position; |
1142 | 69 |
|
70 |
if (args.size() != 1) { |
|
71 |
err << "'" << getName() << "' takes exactly one argument!"; |
|
72 |
throwInvalidUsageException(err); |
|
73 |
} |
|
74 |
||
75 |
file.open(args[0].c_str(), ifstream::in | ifstream::binary); |
|
76 |
if (file.fail()) { |
|
77 |
err << "Failed to open '" << args[0] << "'!"; |
|
78 |
throwCommandException(err); |
|
79 |
} |
|
80 |
||
81 |
// get length of file |
|
82 |
file.seekg(0, ios::end); |
|
83 |
byte_size = file.tellg(); |
|
84 |
file.seekg(0, ios::beg); |
|
85 |
||
86 |
if (!byte_size || byte_size % 2) { |
|
87 |
err << "Invalid file size! Must be non-zero and even."; |
|
88 |
throwCommandException(err); |
|
89 |
} |
|
90 |
||
91 |
data.nwords = byte_size / 2; |
|
92 |
if (data.nwords < 0x0041 && !force) { |
|
93 |
err << "SII data too short (" << data.nwords << " words)! Mimimum is" |
|
94 |
" 40 fixed words + 1 delimiter. Use --force to write anyway."; |
|
95 |
throwCommandException(err); |
|
96 |
} |
|
97 |
||
98 |
// allocate buffer and read file into buffer |
|
99 |
data.words = new uint16_t[data.nwords]; |
|
100 |
file.read((char *) data.words, byte_size); |
|
101 |
file.close(); |
|
102 |
||
103 |
if (!force) { |
|
104 |
// calculate checksum over words 0 to 6 |
|
105 |
crc = calcSiiCrc((const uint8_t *) data.words, 14); |
|
106 |
if (crc != ((const uint8_t *) data.words)[14]) { |
|
107 |
err << "CRC incorrect. Must be 0x" |
|
108 |
<< hex << setfill('0') << setw(2) << (unsigned int) crc |
|
109 |
<< ". Use --force to write anyway."; |
|
110 |
throwCommandException(err); |
|
111 |
} |
|
112 |
||
113 |
// cycle through categories to detect corruption |
|
114 |
categoryHeader = data.words + 0x0040U; |
|
115 |
categoryType = le16tocpu(*categoryHeader); |
|
116 |
while (categoryType != 0xffff) { |
|
117 |
if (categoryHeader + 1 > data.words + data.nwords) { |
|
118 |
err << "SII data seem to be corrupted! " |
|
119 |
<< "Use --force to write anyway."; |
|
120 |
throwCommandException(err); |
|
121 |
} |
|
122 |
categorySize = le16tocpu(*(categoryHeader + 1)); |
|
123 |
if (categoryHeader + 2 + categorySize + 1 |
|
124 |
> data.words + data.nwords) { |
|
125 |
err << "SII data seem to be corrupted! " |
|
126 |
"Use --force to write anyway."; |
|
127 |
throwCommandException(err); |
|
128 |
} |
|
129 |
categoryHeader += 2 + categorySize; |
|
130 |
categoryType = le16tocpu(*categoryHeader); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
// send data to master |
|
135 |
m.open(MasterDevice::ReadWrite); |
|
136 |
data.offset = 0; |
|
137 |
m.writeSii(&data); |
|
138 |
} |
|
139 |
||
140 |
/*****************************************************************************/ |