package dongfang.mkt.main;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import dongfang.mkt.comm.FrameQueue;
import dongfang.mkt.comm.MKConnection;
import dongfang.mkt.comm.serial.RXTXSerialPort;
import dongfang.mkt.configuration.ParameterSet;
import dongfang.mkt.frames.ReadParamSetRequestFrame;
import dongfang.mkt.frames.ReadParamSetResponseFrame;
import dongfang.mkt.frames.WriteParamSetRequestFrame;
import dongfang.mkt.frames.WriteParamSetResponseFrame;
public class ParameterSetConfigurator
{
private static void configure
(String portIdentifier, WriteParamSetRequestFrame frame,
int parameterSetNumber
) throws IOException {
MKConnection port =
new RXTXSerialPort
();
port.
init(portIdentifier
);
FrameQueue q =
new FrameQueue
(port
);
frame.
setConfigurationSetNumber(parameterSetNumber
);
q.
sendRequest(frame
);
WriteParamSetResponseFrame r =
(WriteParamSetResponseFrame
) q.
getResponseFor(frame,
5000);
if (r ==
null) {
System.
err.
println("ERROR. Timeout waiting for response.");
} else if (!r.
wasAccepted()) {
System.
err
.
println("ERROR. Parameter set not accepted. Check version against MK firmware EEPROM configuration version and checkparameter set number");
} else {
System.
out.
println("Saved parameter set #"
+ r.
getParameterSetNumber() +
".");
}
q.
kill();
}
private static void writeConfiguration
(String portIdentifier,
String s_parameterSetNumber,
String fileName
) throws IOException {
System.
out.
println("Writing parameter set #" + s_parameterSetNumber +
" from file: " + fileName
);
int parameterSetNumber =
Integer.
parseInt(s_parameterSetNumber
);
InputStream inputStream =
new FileInputStream(fileName
);
WriteParamSetRequestFrame frame = parseXMLParameterSet
(inputStream
);
configure
(portIdentifier, frame, parameterSetNumber
);
}
private static ParameterSet readConfiguration
(String portIdentifier,
String s_parameterSetNumber
) throws IOException {
MKConnection port =
new RXTXSerialPort
();
port.
init(portIdentifier
);
FrameQueue q =
new FrameQueue
(port
);
ParameterSet cs =
null;
int parameterSetNumber =
Integer.
parseInt(s_parameterSetNumber
);
ReadParamSetRequestFrame frame =
new ReadParamSetRequestFrame
();
frame.
setConfigurationSetNumber(parameterSetNumber
);
q.
sendRequest(frame
);
ReadParamSetResponseFrame r =
(ReadParamSetResponseFrame
) q.
getResponseFor(frame,
5000);
if (r ==
null) {
System.
err.
println("ERROR. Timeout waiting for response.");
} else {
int paramSetVersion = r.
getConfigurationVersion();
int[] data = r.
getData();
cs = ParameterSet.
parseXMLParameterSet(paramSetVersion
);
cs.
setData(data
);
System.
out.
println(cs.
toXML());
}
q.
kill();
return cs
;
}
/*
private static ConfigSet simpleReadConfiguration(int parameterSetNumber)
throws IOException {
MKCommPort port = new RXTXSerialPort();
port.init(null);
FrameQueue q = new FrameQueue(port);
ConfigSet cs = null;
UniversalReadParamSetRequestFrame frame = new UniversalReadParamSetRequestFrame();
frame.setConfigurationSetNumber(parameterSetNumber);
q.sendRequest(frame);
UniversalReadParamSetResponseFrame r = (UniversalReadParamSetResponseFrame) q
.getResponseFor(frame, 3000);
if (r == null) {
System.err.println("ERROR. Timeout waiting for response.");
} else {
int paramSetVersion = r.getConfigurationVersion();
int[] data = r.getData();
for (int i=0; i<data.length; i++)
System.out.println(data[i]);
}
q.kill();
return cs;
}
*/
private static void readConfiguration
(String portIdentifier,
String s_parameterSetNumber,
String fileName
) throws IOException {
ParameterSet cs = readConfiguration
(portIdentifier, s_parameterSetNumber
);
if (cs
!=
null) {
FileWriter fw =
new FileWriter(fileName
);
fw.
write(cs.
toXML());
fw.
close();
}
}
private static int substituteVariables
(String s_value
) {
int numberOfHighestVar =
255;
int numberOfLowestVar = numberOfHighestVar - ParameterSet.
NUMBER_OF_VARIABLES +
1;
for (int i =
0; i
< ParameterSet.
NUMBER_OF_VARIABLES; i++
) {
String varName =
"var" + i
;
if (varName.
equals(s_value
)) {
System.
out.
println("Substing: " + s_value +
"-->" +
(numberOfLowestVar + i
));
return numberOfLowestVar + i
;
}
}
return -
1;
}
private static WriteParamSetRequestFrame parseXMLParameterSet
(
InputStream input
) throws IOException {
DocumentBuilderFactory saxfac =
DocumentBuilderFactory.
newInstance();
saxfac.
setValidating(false);
try {
DocumentBuilder bldr = saxfac.
newDocumentBuilder();
Document doc = bldr.
parse(input
);
XPath xpath =
XPathFactory.
newInstance().
newXPath();
String s_eepromVersion = xpath.
evaluate(
"/parameterset/@eepromVersion", doc
);
String ss_length = xpath.
evaluate("/parameterset/@length", doc
);
int s_length =
0;
try {
s_length =
Integer.
parseInt(ss_length
);
} catch (NumberFormatException ex
) {
// ignore.
}
int eepromVersion = -
1;
try {
eepromVersion =
Integer.
parseInt(s_eepromVersion
);
} catch (NumberFormatException ex
) {
System.
err
.
println("The parameterset element must have an 'eepromVersion' attribute with a numerical value (eg.<parameterset eepromVersion='74'>)");
System.
exit(-
1);
}
// if (s_length != null)
// try {
// length = Integer.parseInt(s_length);
// } catch (NumberFormatException ex) {
// System.err.println("The length attribute of the parameterset must have a numerical value");
// System.exit(-1);
// }
NodeList parameterNodes =
(NodeList) xpath.
evaluate(
"/parameterset/parameter", doc,
XPathConstants.
NODESET);
int[] parameters =
new int[parameterNodes.
getLength()];
NodeList allChildNodes =
(NodeList) xpath.
evaluate(
"/parameterset/*", doc,
XPathConstants.
NODESET);
if (parameterNodes.
getLength() != allChildNodes.
getLength()) {
System.
err.
println("There seems to be a child element of parameterset whose name is not \"parameter\".");
// System.exit(-1);
}
if (s_length
>=
0 && s_length
!= parameterNodes.
getLength()) {
System.
err.
println("The number of parameters ("+parameterNodes.
getLength()+
") does not match the number in the length attribute of the parameterset element ("+s_length+
").");
// System.exit(-1);
}
for (int i =
0; i
< parameterNodes.
getLength(); i++
) {
Element e =
(Element) parameterNodes.
item(i
);
int value =
0;
if (e.
hasAttribute("value")) {
String s_value = e.
getAttribute("value");
value = substituteVariables
(s_value
);
if (value
< 0)
try {
value =
Integer.
parseInt(s_value
);
} catch (NumberFormatException ex
) {
throw new NumberFormatException(
"Value is not a number and not a variable name.");
}
} else {
NodeList bitNodes =
(NodeList) xpath.
evaluate("bit", e,
XPathConstants.
NODESET);
for (int j =
0; j
< 8; j++
) {
Element bitElement =
(Element) bitNodes.
item(j
);
if (bitElement
!=
null) {
String s_bitValue = bitElement
.
getAttribute("value");
if ("1".
equals(s_bitValue
))
value |=
(1 << j
);
else if (!"0".
equals(s_bitValue
))
throw new NumberFormatException(
"Bit value was not 0 or 1.");
}
}
}
parameters
[i
] = value
;
}
input.
close();
return new WriteParamSetRequestFrame
(eepromVersion,
"01234567890ab".
toCharArray(), parameters
);
} catch (ParserConfigurationException ex
) {
// Should never happen.
throw new RuntimeException(ex
);
} catch (SAXException ex
) {
System.
err.
println(ex
);
System.
err
.
println("There is something screwed with your XML document. It is not well-formed and won't parse.");
throw new RuntimeException("Parse error.");
} catch (XPathExpressionException ex
) {
// Should never happen.
throw new RuntimeException(ex
);
}
}
static void help
() {
System.
err
.
println("Usage: ParameterSetConfigurator r [parameter set number to read from] [filename to write to]");
System.
err
.
println("Usage: ParameterSetConfigurator w [parameter set number to write to] [filename to read from]");
System.
exit(-
1);
}
public static void main
(String[] args
) throws IOException {
if (!"r".
equals(args
[0]) && !"w".
equals(args
[0])) help
();
if ("w".
equals(args
[0]) && (args.
length!=
3 && args.
length!=
4)) help
();
if ("r".
equals(args
[0]) && (args.
length!=
3 && args.
length!=
4)) help
();
String portIdentifier =
"COM10";
if ("r".
equals(args
[0])) {
readConfiguration
(portIdentifier, args
[1]);
} else {
writeConfiguration
(portIdentifier, args
[1], args
[2]);
}
System.
exit(0);
}
}