46 string CommandRegRead::helpString(const string &binaryBaseName) const |
46 string CommandRegRead::helpString(const string &binaryBaseName) const |
47 { |
47 { |
48 stringstream str; |
48 stringstream str; |
49 |
49 |
50 str << binaryBaseName << " " << getName() |
50 str << binaryBaseName << " " << getName() |
51 << " [OPTIONS] <OFFSET> [LENGTH]" << endl |
51 << " [OPTIONS] <ADDRESS> [SIZE]" << endl |
52 << endl |
52 << endl |
53 << getBriefDescription() << endl |
53 << getBriefDescription() << endl |
54 << endl |
54 << endl |
55 << "This command requires a single slave to be selected." << endl |
55 << "This command requires a single slave to be selected." << endl |
56 << endl |
56 << endl |
57 << "Arguments:" << endl |
57 << "Arguments:" << endl |
58 << " OFFSET is the register address. Must" << endl |
58 << " ADDRESS is the register address. Must" << endl |
59 << " be an unsigned 16 bit number." << endl |
59 << " be an unsigned 16 bit number." << endl |
60 << " LENGTH is the number of bytes to read and must also be" << endl |
60 << " SIZE is the number of bytes to read and must also be" << endl |
61 << " an unsigned 16 bit number. OFFSET plus LENGTH" << endl |
61 << " an unsigned 16 bit number. ADDRESS plus SIZE" << endl |
62 << " may not exceed 64k. The length is ignored (and" << endl |
62 << " may not exceed 64k. The size is ignored (and" << endl |
63 << " can be omitted), if a selected data type" << endl |
63 << " can be omitted), if a selected data type" << endl |
64 << " implies a length." << endl |
64 << " implies a size." << endl |
65 << endl |
65 << endl |
66 << typeInfo() |
66 << typeInfo() |
67 << endl |
67 << endl |
68 << "Command-specific options:" << endl |
68 << "Command-specific options:" << endl |
69 << " --alias -a <alias>" << endl |
69 << " --alias -a <alias>" << endl |
91 } |
91 } |
92 |
92 |
93 strOffset << args[0]; |
93 strOffset << args[0]; |
94 strOffset |
94 strOffset |
95 >> resetiosflags(ios::basefield) // guess base from prefix |
95 >> resetiosflags(ios::basefield) // guess base from prefix |
96 >> data.offset; |
96 >> io.address; |
97 if (strOffset.fail()) { |
97 if (strOffset.fail()) { |
98 err << "Invalid offset '" << args[0] << "'!"; |
98 err << "Invalid address '" << args[0] << "'!"; |
99 throwInvalidUsageException(err); |
99 throwInvalidUsageException(err); |
100 } |
100 } |
101 |
101 |
102 if (args.size() > 1) { |
102 if (args.size() > 1) { |
103 stringstream strLength; |
103 stringstream strLength; |
104 strLength << args[1]; |
104 strLength << args[1]; |
105 strLength |
105 strLength |
106 >> resetiosflags(ios::basefield) // guess base from prefix |
106 >> resetiosflags(ios::basefield) // guess base from prefix |
107 >> data.length; |
107 >> io.size; |
108 if (strLength.fail()) { |
108 if (strLength.fail()) { |
109 err << "Invalid length '" << args[1] << "'!"; |
109 err << "Invalid size '" << args[1] << "'!"; |
110 throwInvalidUsageException(err); |
110 throwInvalidUsageException(err); |
111 } |
111 } |
112 |
112 |
113 if (!data.length) { |
113 if (!io.size) { |
114 err << "Length may not be zero!"; |
114 err << "Length may not be zero!"; |
115 throwInvalidUsageException(err); |
115 throwInvalidUsageException(err); |
116 } |
116 } |
117 } else { // no length argument given |
117 } else { // no size argument given |
118 data.length = 0; |
118 io.size = 0; |
119 } |
119 } |
120 |
120 |
121 if (!getDataType().empty()) { |
121 if (!getDataType().empty()) { |
122 if (!(dataType = findDataType(getDataType()))) { |
122 if (!(dataType = findDataType(getDataType()))) { |
123 err << "Invalid data type '" << getDataType() << "'!"; |
123 err << "Invalid data type '" << getDataType() << "'!"; |
124 throwInvalidUsageException(err); |
124 throwInvalidUsageException(err); |
125 } |
125 } |
126 |
126 |
127 if (dataType->byteSize) { |
127 if (dataType->byteSize) { |
128 // override length argument |
128 // override size argument |
129 data.length = dataType->byteSize; |
129 io.size = dataType->byteSize; |
130 } |
130 } |
131 } |
131 } |
132 |
132 |
133 if (!data.length) { |
133 if (!io.size) { |
134 err << "The length argument is mandatory, if no datatype is " << endl |
134 err << "The size argument is mandatory, if no datatype is " << endl |
135 << "specified, or the datatype does not imply a length!"; |
135 << "specified, or the datatype does not imply a size!"; |
136 throwInvalidUsageException(err); |
136 throwInvalidUsageException(err); |
137 } |
137 } |
138 |
138 |
139 if ((uint32_t) data.offset + data.length > 0xffff) { |
139 if ((uint32_t) io.address + io.size > 0xffff) { |
140 err << "Offset and length exceeding 64k!"; |
140 err << "Address and size exceeding 64k!"; |
141 throwInvalidUsageException(err); |
141 throwInvalidUsageException(err); |
142 } |
142 } |
143 |
143 |
144 MasterDevice m(getSingleMasterIndex()); |
144 MasterDevice m(getSingleMasterIndex()); |
145 m.open(MasterDevice::Read); |
145 m.open(MasterDevice::Read); |
146 slaves = selectedSlaves(m); |
146 slaves = selectedSlaves(m); |
147 |
147 |
148 if (slaves.size() != 1) { |
148 if (slaves.size() != 1) { |
149 throwSingleSlaveRequired(slaves.size()); |
149 throwSingleSlaveRequired(slaves.size()); |
150 } |
150 } |
151 data.slave_position = slaves.front().position; |
151 io.slave_position = slaves.front().position; |
152 |
152 |
153 data.data = new uint8_t[data.length]; |
153 io.data = new uint8_t[io.size]; |
154 |
154 |
155 try { |
155 try { |
156 m.readReg(&data); |
156 m.readReg(&io); |
157 } catch (MasterDeviceException &e) { |
157 } catch (MasterDeviceException &e) { |
158 delete [] data.data; |
158 delete [] io.data; |
159 throw e; |
159 throw e; |
160 } |
160 } |
161 |
161 |
162 try { |
162 try { |
163 outputData(cout, dataType, data.data, data.length); |
163 outputData(cout, dataType, io.data, io.size); |
164 } catch (SizeException &e) { |
164 } catch (SizeException &e) { |
165 delete [] data.data; |
165 delete [] io.data; |
166 throwCommandException(e.what()); |
166 throwCommandException(e.what()); |
167 } |
167 } |
168 |
168 |
169 delete [] data.data; |
169 delete [] io.data; |
170 } |
170 } |
171 |
171 |
172 /*****************************************************************************/ |
172 /*****************************************************************************/ |