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