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