|
1 /***************************************************************************** |
|
2 * |
|
3 * $Id$ |
|
4 * |
|
5 ****************************************************************************/ |
|
6 |
|
7 #include <iostream> |
|
8 #include <iomanip> |
|
9 using namespace std; |
|
10 |
|
11 #include "CommandFoeRead.h" |
|
12 |
|
13 /*****************************************************************************/ |
|
14 |
|
15 CommandFoeRead::CommandFoeRead(): |
|
16 FoeCommand("foe_read", "Read a file from a slave via FoE.") |
|
17 { |
|
18 } |
|
19 |
|
20 /*****************************************************************************/ |
|
21 |
|
22 string CommandFoeRead::helpString() const |
|
23 { |
|
24 stringstream str; |
|
25 |
|
26 str << getName() << " [OPTIONS] <SOURCEFILE>" << endl |
|
27 << endl |
|
28 << getBriefDescription() << endl |
|
29 << endl |
|
30 << "This command requires a single slave to be selected." << endl |
|
31 << endl |
|
32 << "Arguments:" << endl |
|
33 << " SOURCEFILE is the name of the source file on the slave." << endl |
|
34 << endl |
|
35 << "Command-specific options:" << endl |
|
36 << " --output-file -o <file> Local target filename. If" << endl |
|
37 << " '-' (default), data are" << endl |
|
38 << " printed to stdout." << endl |
|
39 << " --alias -a <alias> " << endl |
|
40 << " --position -p <pos> Slave selection. See the help" << endl |
|
41 << " of the 'slaves' command." << endl |
|
42 << endl |
|
43 << numericInfo(); |
|
44 |
|
45 return str.str(); |
|
46 } |
|
47 |
|
48 /****************************************************************************/ |
|
49 |
|
50 void CommandFoeRead::execute(MasterDevice &m, const StringVector &args) |
|
51 { |
|
52 SlaveList slaves; |
|
53 ec_ioctl_slave_t *slave; |
|
54 ec_ioctl_slave_foe_t data; |
|
55 unsigned int i; |
|
56 stringstream err; |
|
57 |
|
58 if (args.size() != 1) { |
|
59 err << "'" << getName() << "' takes exactly one argument!"; |
|
60 throwInvalidUsageException(err); |
|
61 } |
|
62 |
|
63 m.open(MasterDevice::Read); |
|
64 slaves = selectedSlaves(m); |
|
65 |
|
66 if (slaves.size() != 1) { |
|
67 throwSingleSlaveRequired(slaves.size()); |
|
68 } |
|
69 slave = &slaves.front(); |
|
70 data.slave_position = slave->position; |
|
71 |
|
72 /* FIXME: No good idea to have a fixed buffer size. |
|
73 * Read in chunks and fill a buffer instead. |
|
74 */ |
|
75 data.offset = 0; |
|
76 data.buffer_size = 0x8800; |
|
77 data.buffer = new uint8_t[data.buffer_size]; |
|
78 |
|
79 strncpy(data.file_name, args[0].c_str(), sizeof(data.file_name)); |
|
80 |
|
81 try { |
|
82 m.readFoe(&data); |
|
83 } catch (MasterDeviceException &e) { |
|
84 delete [] data.buffer; |
|
85 throw e; |
|
86 } |
|
87 |
|
88 // TODO --output-file |
|
89 for (i = 0; i < data.data_size; i++) { |
|
90 uint8_t *w = data.buffer + i; |
|
91 cout << *(uint8_t *) w ; |
|
92 } |
|
93 |
|
94 delete [] data.buffer; |
|
95 } |
|
96 |
|
97 /*****************************************************************************/ |