author | Florian Pose <fp@igh-essen.com> |
Mon, 28 Jul 2008 09:12:21 +0000 | |
changeset 1156 | ecaf2a896ea3 |
parent 1151 | 1fc1535dec29 |
child 1157 | 04d1c950cf9d |
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 |
1142 | 36 |
<< endl << endl |
37 |
<< "Command-specific options:" << endl |
|
38 |
<< " --slave -s <index> Positive numerical ring position, or 'all'" |
|
39 |
<< endl |
|
40 |
<< " for all slaves (default). The --force" |
|
41 |
<< endl |
|
42 |
<< " option is required in this case." << endl |
|
43 |
<< " --force -f Acknowledge writing aliases of all" << endl |
|
44 |
<< " slaves." << endl |
|
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 |
||
81 |
if (slaves.size() > 1 && !force) { |
|
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 |
|
1151 | 88 |
for (si = slaves.begin(); si != slaves.end(); si++) { |
89 |
writeSlaveAlias(m, *si, alias); |
|
1142 | 90 |
} |
91 |
} |
|
92 |
||
93 |
/*****************************************************************************/ |
|
94 |
||
95 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
96 |
*/ |
|
97 |
void CommandAlias::writeSlaveAlias( |
|
98 |
MasterDevice &m, |
|
1151 | 99 |
const ec_ioctl_slave_t &slave, |
1142 | 100 |
uint16_t alias |
101 |
) |
|
102 |
{ |
|
103 |
ec_ioctl_slave_sii_t data; |
|
104 |
stringstream err; |
|
105 |
uint8_t crc; |
|
106 |
||
107 |
if (slave.sii_nwords < 8) { |
|
108 |
err << "Current SII contents are too small to set an alias " |
|
109 |
<< "(" << slave.sii_nwords << " words)!"; |
|
110 |
throwCommandException(err); |
|
111 |
} |
|
112 |
||
113 |
// read first 8 SII words |
|
1151 | 114 |
data.slave_position = slave.position; |
1142 | 115 |
data.offset = 0; |
116 |
data.nwords = 8; |
|
117 |
data.words = new uint16_t[data.nwords]; |
|
118 |
||
119 |
try { |
|
120 |
m.readSii(&data); |
|
121 |
} catch (MasterDeviceException &e) { |
|
122 |
delete [] data.words; |
|
123 |
err << "Failed to read SII: " << e.what(); |
|
124 |
throwCommandException(err); |
|
125 |
} |
|
126 |
||
127 |
// write new alias address in word 4 |
|
128 |
data.words[4] = cputole16(alias); |
|
129 |
||
130 |
// calculate checksum over words 0 to 6 |
|
131 |
crc = calcSiiCrc((const uint8_t *) data.words, 14); |
|
132 |
||
133 |
// write new checksum into first byte of word 7 |
|
134 |
*(uint8_t *) (data.words + 7) = crc; |
|
135 |
||
136 |
// write first 8 words with new alias and checksum |
|
137 |
try { |
|
138 |
m.writeSii(&data); |
|
139 |
} catch (MasterDeviceException &e) { |
|
140 |
delete [] data.words; |
|
141 |
err << "Failed to read SII: " << e.what(); |
|
142 |
throwCommandException(err); |
|
143 |
} |
|
144 |
||
145 |
delete [] data.words; |
|
146 |
} |
|
147 |
||
148 |
/*****************************************************************************/ |