author | Florian Pose <fp@igh-essen.com> |
Fri, 25 Jul 2008 14:10:04 +0000 | |
changeset 1149 | f1fce286d53b |
parent 1144 | 7dbfdd61812c |
child 1151 | 1fc1535dec29 |
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; |
|
60 |
unsigned int numSlaves, i; |
|
61 |
||
62 |
if (args.size() != 1) { |
|
63 |
err << "'" << getName() << "' takes exactly one argument!"; |
|
64 |
throwInvalidUsageException(err); |
|
65 |
} |
|
66 |
||
67 |
strAlias << args[0]; |
|
68 |
strAlias |
|
69 |
>> resetiosflags(ios::basefield) // guess base from prefix |
|
70 |
>> number; |
|
71 |
if (strAlias.fail() || number < 0x0000 || number > 0xffff) { |
|
72 |
err << "Invalid alias '" << args[0] << "'!"; |
|
73 |
throwInvalidUsageException(err); |
|
74 |
} |
|
75 |
alias = number; |
|
76 |
||
77 |
if (slavePosition == -1) { |
|
78 |
if (!force) { |
|
79 |
err << "This will write the alias addresses of all slaves to " |
|
80 |
<< alias << "! Please specify --force to proceed."; |
|
81 |
throwCommandException(err); |
|
82 |
} |
|
83 |
||
84 |
m.open(MasterDevice::ReadWrite); |
|
85 |
numSlaves = m.slaveCount(); |
|
86 |
||
87 |
for (i = 0; i < numSlaves; i++) { |
|
88 |
writeSlaveAlias(m, i, alias); |
|
89 |
} |
|
90 |
} else { |
|
91 |
m.open(MasterDevice::ReadWrite); |
|
92 |
writeSlaveAlias(m, slavePosition, alias); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
/*****************************************************************************/ |
|
97 |
||
98 |
/** Writes the Secondary slave address (alias) to the slave's SII. |
|
99 |
*/ |
|
100 |
void CommandAlias::writeSlaveAlias( |
|
101 |
MasterDevice &m, |
|
102 |
uint16_t slavePosition, |
|
103 |
uint16_t alias |
|
104 |
) |
|
105 |
{ |
|
106 |
ec_ioctl_slave_sii_t data; |
|
107 |
ec_ioctl_slave_t slave; |
|
108 |
stringstream err; |
|
109 |
uint8_t crc; |
|
110 |
||
111 |
m.getSlave(&slave, slavePosition); |
|
112 |
||
113 |
if (slave.sii_nwords < 8) { |
|
114 |
err << "Current SII contents are too small to set an alias " |
|
115 |
<< "(" << slave.sii_nwords << " words)!"; |
|
116 |
throwCommandException(err); |
|
117 |
} |
|
118 |
||
119 |
// read first 8 SII words |
|
120 |
data.slave_position = slavePosition; |
|
121 |
data.offset = 0; |
|
122 |
data.nwords = 8; |
|
123 |
data.words = new uint16_t[data.nwords]; |
|
124 |
||
125 |
try { |
|
126 |
m.readSii(&data); |
|
127 |
} catch (MasterDeviceException &e) { |
|
128 |
delete [] data.words; |
|
129 |
err << "Failed to read SII: " << e.what(); |
|
130 |
throwCommandException(err); |
|
131 |
} |
|
132 |
||
133 |
// write new alias address in word 4 |
|
134 |
data.words[4] = cputole16(alias); |
|
135 |
||
136 |
// calculate checksum over words 0 to 6 |
|
137 |
crc = calcSiiCrc((const uint8_t *) data.words, 14); |
|
138 |
||
139 |
// write new checksum into first byte of word 7 |
|
140 |
*(uint8_t *) (data.words + 7) = crc; |
|
141 |
||
142 |
// write first 8 words with new alias and checksum |
|
143 |
try { |
|
144 |
m.writeSii(&data); |
|
145 |
} catch (MasterDeviceException &e) { |
|
146 |
delete [] data.words; |
|
147 |
err << "Failed to read SII: " << e.what(); |
|
148 |
throwCommandException(err); |
|
149 |
} |
|
150 |
||
151 |
delete [] data.words; |
|
152 |
} |
|
153 |
||
154 |
/*****************************************************************************/ |