1200
|
1 |
/*****************************************************************************
|
|
2 |
*
|
|
3 |
* $Id$
|
|
4 |
*
|
|
5 |
****************************************************************************/
|
|
6 |
|
|
7 |
#include <iostream>
|
|
8 |
#include <iomanip>
|
|
9 |
#include <fstream>
|
|
10 |
using namespace std;
|
|
11 |
|
|
12 |
#include "CommandPhyWrite.h"
|
|
13 |
#include "sii_crc.h"
|
|
14 |
|
|
15 |
/*****************************************************************************/
|
|
16 |
|
|
17 |
CommandPhyWrite::CommandPhyWrite():
|
|
18 |
Command("phy_write", "Write data to a slave's physical memory.")
|
|
19 |
{
|
|
20 |
}
|
|
21 |
|
|
22 |
/*****************************************************************************/
|
|
23 |
|
|
24 |
string CommandPhyWrite::helpString() const
|
|
25 |
{
|
|
26 |
stringstream str;
|
|
27 |
|
|
28 |
str << getName() << " [OPTIONS] <OFFSET> <FILENAME>" << endl
|
|
29 |
<< endl
|
|
30 |
<< getBriefDescription() << endl
|
|
31 |
<< endl
|
|
32 |
<< "This command requires a single slave to be selected." << endl
|
|
33 |
<< endl
|
|
34 |
<< "Arguments:" << endl
|
|
35 |
<< " OFFSET must be the physical memory offset to start." << endl
|
|
36 |
<< " FILENAME must be a path to a file with data to write." << endl
|
|
37 |
<< " If it is '-', data are read from stdin." << endl
|
|
38 |
<< endl
|
|
39 |
<< "Command-specific options:" << endl
|
|
40 |
<< " --alias -a <alias>" << endl
|
|
41 |
<< " --position -p <pos> Slave selection. See the help of" << endl
|
|
42 |
<< " the 'slaves' command." << endl
|
|
43 |
<< endl
|
|
44 |
<< numericInfo();
|
|
45 |
|
|
46 |
return str.str();
|
|
47 |
}
|
|
48 |
|
|
49 |
/****************************************************************************/
|
|
50 |
|
|
51 |
void CommandPhyWrite::execute(MasterDevice &m, const StringVector &args)
|
|
52 |
{
|
|
53 |
stringstream strOffset, err;
|
|
54 |
ec_ioctl_slave_phy_t data;
|
|
55 |
ifstream file;
|
|
56 |
SlaveList slaves;
|
|
57 |
|
|
58 |
if (args.size() != 2) {
|
|
59 |
err << "'" << getName() << "' takes exactly one argument!";
|
|
60 |
throwInvalidUsageException(err);
|
|
61 |
}
|
|
62 |
|
|
63 |
strOffset << args[0];
|
|
64 |
strOffset
|
|
65 |
>> resetiosflags(ios::basefield) // guess base from prefix
|
|
66 |
>> data.offset;
|
|
67 |
if (strOffset.fail()) {
|
|
68 |
err << "Invalid offset '" << args[0] << "'!";
|
|
69 |
throwInvalidUsageException(err);
|
|
70 |
}
|
|
71 |
|
|
72 |
if (args[1] == "-") {
|
|
73 |
loadPhyData(&data, cin);
|
|
74 |
} else {
|
|
75 |
file.open(args[1].c_str(), ifstream::in | ifstream::binary);
|
|
76 |
if (file.fail()) {
|
|
77 |
err << "Failed to open '" << args[0] << "'!";
|
|
78 |
throwCommandException(err);
|
|
79 |
}
|
|
80 |
loadPhyData(&data, file);
|
|
81 |
file.close();
|
|
82 |
}
|
|
83 |
|
|
84 |
if ((uint32_t) data.offset + data.length > 0xffff) {
|
|
85 |
err << "Offset and length exceeding 64k!";
|
|
86 |
delete [] data.data;
|
|
87 |
throwInvalidUsageException(err);
|
|
88 |
}
|
|
89 |
|
|
90 |
try {
|
|
91 |
m.open(MasterDevice::ReadWrite);
|
|
92 |
} catch (MasterDeviceException &e) {
|
|
93 |
delete [] data.data;
|
|
94 |
throw e;
|
|
95 |
}
|
|
96 |
|
|
97 |
slaves = selectedSlaves(m);
|
|
98 |
if (slaves.size() != 1) {
|
|
99 |
delete [] data.data;
|
|
100 |
throwSingleSlaveRequired(slaves.size());
|
|
101 |
}
|
|
102 |
data.slave_position = slaves.front().position;
|
|
103 |
|
|
104 |
// send data to master
|
|
105 |
try {
|
|
106 |
m.writePhy(&data);
|
|
107 |
} catch (MasterDeviceException &e) {
|
|
108 |
delete [] data.data;
|
|
109 |
throw e;
|
|
110 |
}
|
|
111 |
|
|
112 |
if (getVerbosity() == Verbose) {
|
|
113 |
cerr << "Physical memory writing finished." << endl;
|
|
114 |
}
|
|
115 |
|
|
116 |
delete [] data.data;
|
|
117 |
}
|
|
118 |
|
|
119 |
/*****************************************************************************/
|
|
120 |
|
|
121 |
void CommandPhyWrite::loadPhyData(
|
|
122 |
ec_ioctl_slave_phy_t *data,
|
|
123 |
const istream &in
|
|
124 |
)
|
|
125 |
{
|
|
126 |
stringstream err;
|
|
127 |
ostringstream tmp;
|
|
128 |
|
|
129 |
tmp << in.rdbuf();
|
|
130 |
string const &contents = tmp.str();
|
|
131 |
|
|
132 |
if (getVerbosity() == Verbose) {
|
|
133 |
cerr << "Read " << contents.size() << " bytes of data." << endl;
|
|
134 |
}
|
|
135 |
|
|
136 |
if (contents.size() > 0xffff) {
|
|
137 |
err << "Invalid data size " << contents.size() << "!";
|
|
138 |
throwInvalidUsageException(err);
|
|
139 |
}
|
|
140 |
data->length = contents.size();
|
|
141 |
|
|
142 |
// allocate buffer and read file into buffer
|
|
143 |
data->data = new uint8_t[data->length];
|
|
144 |
contents.copy((char *) data->data, contents.size());
|
|
145 |
}
|
|
146 |
|
|
147 |
/*****************************************************************************/
|