author | Florian Pose <fp@igh-essen.com> |
Mon, 26 Jan 2009 16:35:50 +0000 | |
changeset 1344 | e5756d4ed12c |
parent 1254 | c19d273a9e76 |
child 1363 | 11c0b2caa253 |
permissions | -rw-r--r-- |
1142 | 1 |
/***************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
5 |
****************************************************************************/ |
|
6 |
||
7 |
#include <iostream> |
|
8 |
#include <iomanip> |
|
9 |
#include <sstream> |
|
10 |
using namespace std; |
|
11 |
||
12 |
#include "CommandAlias.h" |
|
13 |
#include "sii_crc.h" |
|
14 |
||
15 |
/*****************************************************************************/ |
|
16 |
||
17 |
CommandAlias::CommandAlias(): |
|
18 |
Command("alias", "Write alias addresses.") |
|
19 |
{ |
|
20 |
} |
|
21 |
||
22 |
/*****************************************************************************/ |
|
23 |
||
24 |
string CommandAlias::helpString() const |
|
25 |
{ |
|
26 |
stringstream str; |
|
27 |
||
28 |
str << getName() << " [OPTIONS] <ALIAS>" << endl |
|
29 |
<< endl |
|
30 |
<< getBriefDescription() << endl |
|
31 |
<< endl |
|
32 |
<< "Arguments:" << endl |
|
1144
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
33 |
<< " ALIAS must be an unsigned 16 bit number. Zero means" << endl |
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
34 |
<< " removing an alias address." << endl |
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
35 |
<< endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
36 |
<< "If multiple slaves are selected, the --force option" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
37 |
<< "is required." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
38 |
<< endl |
1142 | 39 |
<< "Command-specific options:" << endl |
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
40 |
<< " --alias -a <alias>" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
41 |
<< " --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:
1151
diff
changeset
|
42 |
<< " the 'slaves' command." << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
43 |
<< " --force -f Acknowledge writing aliases of" << endl |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
44 |
<< " multiple slaves." << endl |
1142 | 45 |
<< endl |
46 |
<< numericInfo(); |
|
47 |
||
48 |
return str.str(); |
|
49 |
} |
|
50 |
||
51 |
/*****************************************************************************/ |
|
52 |
||
53 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
54 |
*/ |
|
55 |
void CommandAlias::execute(MasterDevice &m, const StringVector &args) |
|
56 |
{ |
|
57 |
uint16_t alias; |
|
58 |
stringstream err, strAlias; |
|
59 |
int number; |
|
1151 | 60 |
SlaveList slaves; |
61 |
SlaveList::const_iterator si; |
|
1142 | 62 |
|
63 |
if (args.size() != 1) { |
|
64 |
err << "'" << getName() << "' takes exactly one argument!"; |
|
65 |
throwInvalidUsageException(err); |
|
66 |
} |
|
67 |
||
68 |
strAlias << args[0]; |
|
69 |
strAlias |
|
70 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
71 |
>> number; |
|
72 |
if (strAlias.fail() || number < 0x0000 || number > 0xffff) { |
|
73 |
err << "Invalid alias '" << args[0] << "'!"; |
|
74 |
throwInvalidUsageException(err); |
|
75 |
} |
|
76 |
alias = number; |
|
77 |
||
1151 | 78 |
m.open(MasterDevice::ReadWrite); |
79 |
slaves = selectedSlaves(m); |
|
80 |
||
1166 | 81 |
if (slaves.size() > 1 && !getForce()) { |
1151 | 82 |
err << "This will write the alias addresses of " |
83 |
<< slaves.size() << " slaves to " << alias |
|
84 |
<< "! Please specify --force to proceed."; |
|
85 |
throwCommandException(err); |
|
86 |
} |
|
1142 | 87 |
|
1157
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
88 |
if (!slaves.size() && getVerbosity() != Quiet) { |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
89 |
cerr << "Warning: Selection matches no slaves!" << endl; |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
90 |
} |
04d1c950cf9d
Added --help for alias and position parameters.
Florian Pose <fp@igh-essen.com>
parents:
1151
diff
changeset
|
91 |
|
1151 | 92 |
for (si = slaves.begin(); si != slaves.end(); si++) { |
93 |
writeSlaveAlias(m, *si, alias); |
|
1142 | 94 |
} |
95 |
} |
|
96 |
||
97 |
/*****************************************************************************/ |
|
98 |
||
99 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
100 |
*/ |
|
101 |
void CommandAlias::writeSlaveAlias( |
|
102 |
MasterDevice &m, |
|
1151 | 103 |
const ec_ioctl_slave_t &slave, |
1142 | 104 |
uint16_t alias |
105 |
) |
|
106 |
{ |
|
107 |
ec_ioctl_slave_sii_t data; |
|
108 |
stringstream err; |
|
109 |
uint8_t crc; |
|
110 |
||
111 |
if (slave.sii_nwords < 8) { |
|
112 |
err << "Current SII contents are too small to set an alias " |
|
113 |
<< "(" << slave.sii_nwords << " words)!"; |
|
114 |
throwCommandException(err); |
|
115 |
} |
|
116 |
||
117 |
// read first 8 SII words |
|
1151 | 118 |
data.slave_position = slave.position; |
1142 | 119 |
data.offset = 0; |
120 |
data.nwords = 8; |
|
121 |
data.words = new uint16_t[data.nwords]; |
|
122 |
||
123 |
try { |
|
124 |
m.readSii(&data); |
|
125 |
} catch (MasterDeviceException &e) { |
|
126 |
delete [] data.words; |
|
127 |
err << "Failed to read SII: " << e.what(); |
|
128 |
throwCommandException(err); |
|
129 |
} |
|
130 |
||
131 |
// write new alias address in word 4 |
|
1254
c19d273a9e76
Moved byteorder macros to ecrt.h.
Florian Pose <fp@igh-essen.com>
parents:
1166
diff
changeset
|
132 |
data.words[4] = cpu_to_le16(alias); |
1142 | 133 |
|
134 |
// calculate checksum over words 0 to 6 |
|
135 |
crc = calcSiiCrc((const uint8_t *) data.words, 14); |
|
136 |
||
137 |
// write new checksum into first byte of word 7 |
|
138 |
*(uint8_t *) (data.words + 7) = crc; |
|
139 |
||
140 |
// write first 8 words with new alias and checksum |
|
141 |
try { |
|
142 |
m.writeSii(&data); |
|
143 |
} catch (MasterDeviceException &e) { |
|
144 |
delete [] data.words; |
|
145 |
err << "Failed to read SII: " << e.what(); |
|
146 |
throwCommandException(err); |
|
147 |
} |
|
148 |
||
149 |
delete [] data.words; |
|
150 |
} |
|
151 |
||
152 |
/*****************************************************************************/ |