/dongfang_FC_rewrite_tool/src/dongfang/mkt/configuration/ConfigSet.java |
---|
0,0 → 1,363 |
package dongfang.mkt.configuration; |
import java.io.File; |
import java.io.IOException; |
import java.util.ArrayList; |
import java.util.HashMap; |
import java.util.List; |
import java.util.Map; |
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; |
public class ConfigSet { |
public static final int NUMBER_OF_VARIABLES = 8; |
String name; |
int eepromVersion; |
List<Section> declaredSections = new ArrayList<Section>(); |
List<ConfigEntry> entries = new ArrayList<ConfigEntry>(); |
public static class Section { |
String name, title; |
List<ConfigEntry> entries = new ArrayList<ConfigEntry>(); |
public Section(String name, String title) { |
this.name = name; |
this.title = title; |
} |
public void addConfigEntry(ConfigEntry e) { |
this.entries.add(e); |
} |
public List<ConfigEntry> getEntries() { |
return entries; |
} |
public String getName() { return name; } |
public String getTitle() { return title; } |
} |
public static abstract class ConfigEntry { |
String name; |
ConfigEntry(String name) { |
this.name = name; |
} |
abstract int getByteCount(); |
abstract String toStringWithValues(); |
abstract void toXML(StringBuilder result); |
abstract int setValue(int[] data, int offset); |
public String getName() { |
return name; |
} |
public String getSection() { |
return name; |
} |
} |
public static class ByteEntry extends ConfigEntry { |
ByteEntry(String name, boolean isDynamic) { |
super(name); |
this.isDynamic = isDynamic; |
} |
boolean isDynamic; |
int value; |
int getByteCount() { |
return 1; |
} |
int getValue() { |
return value; |
} |
int setValue(int[] data, int offset) { |
this.value = data[offset]; |
return getByteCount(); |
} |
String toStringWithValues() { |
return name + ":\t" + value; |
} |
void toXML(StringBuilder result) { |
String s_value; |
int numberOfHighestVariable = 255; |
int numberOfLowestVariable = numberOfHighestVariable |
- NUMBER_OF_VARIABLES; |
if (isDynamic && value >= numberOfLowestVariable) { |
s_value = "var" + (value - numberOfLowestVariable); |
} else |
s_value = Integer.toString(value); |
result.append(" <parameter name=\"" + name + "\" value=\"" |
+ s_value + "\"/>\n"); |
} |
} |
public static class BitSetEntry extends ConfigEntry { |
BitSetEntry(String name, String[] bitNames) { |
super(name); |
this.bitNames = bitNames; |
} |
int value; |
String[] bitNames; |
int getByteCount() { |
return 1; |
} |
int getValue() { |
return value; |
} |
int setValue(int[] data, int offset) { |
value = data[offset]; |
return getByteCount(); |
} |
String toStringWithValues() { |
StringBuilder result = new StringBuilder(name + "\t["); |
for (int i = 0; i < Math.min(bitNames.length, 8); i++) { |
if (i != 0) |
result.append(", "); |
result.append(bitNames[i] + ":" |
+ ((value & (1 << i)) != 0 ? "1" : "0")); |
} |
return result.toString(); |
} |
void toXML(StringBuilder result) { |
result.append(" <parameter name=\"" + name + "\">\n"); |
for (int i = 0; i < Math.min(bitNames.length, 8); i++) { |
result.append(" <bit name=\"" + bitNames[i] + "\" value=\"" |
+ ((value & (1 << i)) != 0 ? "1" : "0") + "\"/>\n"); |
} |
result.append(" </parameter>\n"); |
} |
} |
public static class ArrayEntry extends ConfigEntry { |
int length; |
boolean isDynamic; |
int[] values; |
ArrayEntry(String name, boolean isDynamic, int length) { |
super(name); |
this.isDynamic = isDynamic; |
this.length = length; |
} |
int[] getValue() { |
return values; |
} |
int setValue(int[] data, int offset) { |
values = new int[length]; |
System.arraycopy(data, offset, values, 0, length); |
return length; |
} |
int getByteCount() { |
return length; |
} |
String toStringWithValues() { |
StringBuilder result = new StringBuilder(name + ":\t{"); |
for (int i = 0; i < length; i++) { |
if (i != 0) |
result.append(","); |
result.append(values[i]); |
} |
result.append("}"); |
return result.toString(); |
} |
void toXML(StringBuilder result) { |
result.append(" <list name=\"" + name + "\">\n"); |
for (int i = 0; i < length; i++) { |
result.append(" <entry value=\"" + values[i] + "\"/>\n"); |
} |
result.append(" </list>\n"); |
} |
} |
public List<ConfigEntry> getEntries() { |
return entries; |
} |
public List<Section> getDeclaredSections() { |
return declaredSections; |
} |
public void setData(int[] data) { |
int offset = 0; |
for (ConfigEntry entry : entries) { |
offset += entry.setValue(data, offset); |
} |
StringBuilder sbname = new StringBuilder(); |
for (int i = 0; i < 12; i++) { |
if (data[offset] == 0) |
break; |
sbname.append((char) data[offset++]); |
} |
name = sbname.toString(); |
} |
public int getByteCount() { |
int result = 0; |
for (ConfigEntry e : entries) { |
result += e.getByteCount(); |
} |
return result; |
} |
public String toStringWithValues() { |
StringBuilder result = new StringBuilder(); |
result.append(name + "\n"); |
for (ConfigEntry entry : entries) { |
result.append(entry.toStringWithValues() + "\n"); |
} |
return result.toString(); |
} |
public String toXML() { |
StringBuilder result = new StringBuilder(); |
result.append("<parameterset eepromVersion=\"" + eepromVersion |
+ "\" length=\"" + getByteCount() + "\">\n"); |
for (int i = 0; i < entries.size(); i++) { |
ConfigEntry entry = entries.get(i); |
entry.toXML(result); |
} |
result.append("</parameterset>\n"); |
return result.toString(); |
} |
public ConfigSet(int eepromVersion) { |
super(); |
this.eepromVersion = eepromVersion; |
} |
// This parses only for the purpose of naming and typing parameter sets! |
// It does not parse the section and default information, which could be |
// useful for GUIs etc. |
public static ConfigSet parseXMLConfigSet(int version) throws IOException { |
String fileName = "v" + version + ".xml"; |
File f = new File(fileName); |
DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
saxfac.setValidating(false); |
try { |
DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
Document doc = bldr.parse(f); |
XPath xpath = XPathFactory.newInstance().newXPath(); |
String s_eepromVersion = xpath.evaluate( |
"/parameterconfig/@eepromVersion", doc); |
int eepromVersion = Integer.parseInt(s_eepromVersion); |
if (eepromVersion != version) { |
throw new IOException( |
"Version mismatch between file name (" |
+ fileName |
+ ") and the version in the parameterconfig/@eepromVersion attribute(" |
+ s_eepromVersion + ")"); |
} |
ConfigSet result = new ConfigSet(eepromVersion); |
NodeList sectionNodes = (NodeList) xpath.evaluate( |
"/section", doc, XPathConstants.NODESET); |
for (int i = 0; i < sectionNodes.getLength(); i++) { |
Element e = (Element) sectionNodes.item(i); |
Section section = new Section(e.getAttribute("name"), e.getAttribute("title")); |
result.declaredSections.add(section); |
} |
NodeList parameterNodes = (NodeList) xpath.evaluate( |
"/parameterconfig/parameter", doc, XPathConstants.NODESET); |
Section rest = new Section("others", "Entries without a declared section"); |
for (int i = 0; i < parameterNodes.getLength(); i++) { |
Element e = (Element) parameterNodes.item(i); |
Map<String, Section> sectionMap = new HashMap<String, Section>(); |
ConfigEntry entry; |
if (!e.hasAttribute("name")) { |
throw new IOException("A parameter element (the " + i |
+ "th) had no name attribute!"); |
} |
String s_name = e.getAttribute("name"); |
String s_section = e.getAttribute("section"); |
String s_type; |
if (e.hasAttribute("type")) { |
s_type = e.getAttribute("type"); |
} else { |
s_type = "static"; |
} |
if ("static".equals(s_type)) { |
entry = new ConfigSet.ByteEntry(s_name, false); |
} else if ("dynamic".equals(s_type)) { |
entry = new ConfigSet.ByteEntry(s_name, true); |
} else if ("bitset".equals(s_type)) { |
NodeList bitNodes = (NodeList) xpath.evaluate("bit", e, |
XPathConstants.NODESET); |
String[] bitNames = new String[8]; |
for (int j = 0; j < 8; j++) { |
Element bitNode = (Element) bitNodes.item(j); |
if (bitNode != null) { |
bitNames[j] = bitNode.getAttribute("name"); |
} else { |
bitNames[j] = "Unused"; |
} |
} |
entry = new ConfigSet.BitSetEntry(s_name, |
bitNames); |
} else { |
throw new IOException("Unknown parameter type: " + s_type); |
} |
result.entries.add(entry); |
Section section = sectionMap.get(s_section); |
if (section==null) section = rest; |
section.addConfigEntry(entry); |
} |
if (!rest.getEntries().isEmpty()) { |
result.declaredSections.add(rest); |
} |
result.name = "" + version; |
return result; |
} catch (IOException ex) { |
throw ex; |
} catch (XPathExpressionException ex) { |
throw new IOException(ex); |
} catch (SAXException ex) { |
throw new IOException(ex); |
} catch (ParserConfigurationException ex) { |
throw new IOException(ex); |
} |
} |
} |
/dongfang_FC_rewrite_tool/src/dongfang/mkt/io/MKCommPort.java |
---|
0,0 → 1,11 |
package dongfang.mkt.io; |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.OutputStream; |
public interface MKCommPort { |
void init(String id) throws IOException; |
InputStream getInputStream() throws IOException ; |
OutputStream getOutputStream() throws IOException ; |
} |
/dongfang_FC_rewrite_tool/src/dongfang/mkt/io/RXTXSerialPort.java |
---|
0,0 → 1,62 |
package dongfang.mkt.io; |
import gnu.io.CommPort; |
import gnu.io.CommPortIdentifier; |
import gnu.io.NoSuchPortException; |
import gnu.io.SerialPort; |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.OutputStream; |
public class RXTXSerialPort implements MKCommPort { |
private SerialPort serialPort; |
public void init(String id) throws IOException { |
// CommPortIdentifier.getPortIdentifier("COM1"); |
// Just take the 1st available port. |
CommPortIdentifier portIdentifier = null; |
try { |
if (id == null) { |
portIdentifier = (CommPortIdentifier) CommPortIdentifier |
.getPortIdentifiers().nextElement(); |
if (portIdentifier == null) { |
throw new IOException( |
"No serial port found (in search for any serial port)."); |
} |
} else |
portIdentifier = CommPortIdentifier.getPortIdentifier(id); |
} catch (NoSuchPortException ex) { |
throw new IOException(ex.getMessage()); |
} |
System.out.println("Using " + portIdentifier.getName()); |
if (portIdentifier.isCurrentlyOwned()) { |
System.out.println("Error: Port is currently in use"); |
return; |
} else { |
try { |
CommPort commPort = portIdentifier.open(this.getClass() |
.getName(), 2001); |
if (commPort instanceof SerialPort) { |
serialPort = (SerialPort) commPort; |
serialPort.setSerialPortParams(57600, |
SerialPort.DATABITS_8, SerialPort.STOPBITS_1, |
SerialPort.PARITY_NONE); |
} |
} catch (Exception ex) { |
throw new IOException(ex.getMessage()); |
} |
} |
} |
public InputStream getInputStream() throws IOException { |
return serialPort.getInputStream(); |
} |
public OutputStream getOutputStream() throws IOException { |
return serialPort.getOutputStream(); |
} |
} |
/dongfang_FC_rewrite_tool/src/dongfang/mkt/main/MKDebugLogger.java |
---|
0,0 → 1,212 |
package dongfang.mkt.main; |
import java.io.BufferedReader; |
import java.io.FileOutputStream; |
import java.io.IOException; |
import java.io.InputStreamReader; |
import java.io.OutputStream; |
import java.io.OutputStreamWriter; |
import java.io.PrintStream; |
import java.io.Writer; |
import java.util.ArrayList; |
import java.util.Date; |
import java.util.LinkedList; |
import java.util.List; |
import dongfang.mkt.frames.AnalogDebugLabelRequestFrame; |
import dongfang.mkt.frames.AnalogDebugLabelResponseFrame; |
import dongfang.mkt.frames.DebugRequestFrame; |
import dongfang.mkt.frames.DebugResponseFrame; |
import dongfang.mkt.frames.Frame; |
import dongfang.mkt.frames.FrameFactory; |
import dongfang.mkt.io.MKCommPort; |
import dongfang.mkt.io.RXTXSerialPort; |
import dongfang.mkt.serial.FrameQueue; |
import dongfang.mkt.version.MKVersion; |
public class MKDebugLogger { |
private static final PrintStream STDERR = System.out; |
private static final FrameFactory ff = MKVersion.getFrameFactory(null); |
interface DebugLogger { |
void setLabel(int index, String label); |
void start(String timestamp) throws IOException; |
void log(DebugResponseFrame f, long timestamp) throws IOException; |
void end() throws IOException; |
} |
static class ConsoleDebugLogger implements DebugLogger { |
String[] labels = new String[32]; |
public void setLabel(int channel, String label) { |
labels[channel] = label; |
} |
public void start(String logId) {} |
public void log(DebugResponseFrame f, long timestamp) { |
for (int i = 0; i < f.getDigital().length; i++) { |
System.out.println("Digital " + i + ":\t" + f.getDigital()[i]); |
} |
for (int i = 0; i < f.getAnalog().length; i++) { |
String label = labels[i] == null ? ("Analog " + i) : labels[i]; |
System.out.println(label + ":\t" + f.getAnalog()[i]); |
} |
} |
public void end() {} |
} |
static class XMLDebugLogger implements DebugLogger { |
String[] labels = new String[32]; |
Writer w; |
String encoding; |
XMLDebugLogger(OutputStream out, String encoding) throws IOException { |
w = new OutputStreamWriter(out, encoding); |
this.encoding = encoding; |
} |
public void setLabel(int channel, String label) { |
labels[channel] = label.trim(); |
} |
public void start(String logId) throws IOException { |
w.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); |
w.write("<mk-debugdata time=\"" + logId + "\">\n"); |
} |
public void log(DebugResponseFrame f, long timestamp) |
throws IOException { |
w.write(" <dataset timestamp=\"" + timestamp + "\">\n"); |
for (int i = 0; i < 32; i++) { |
w.write(" <data offset=\"" + i + "\" label=\"" + labels[i] |
+ "\" value=\"" + f.getAnalog()[i] + "\"/>\n"); |
} |
w.write(" </dataset>\n"); |
} |
public void end() throws IOException { |
w.write("</mk-debugdata>\n"); |
w.flush(); |
w.close(); |
} |
} |
static class CompositeDebugLogger implements DebugLogger { |
List<DebugLogger> loggers = new ArrayList<DebugLogger>(2); |
public void setLabel(int index, String label) { |
for (DebugLogger l : loggers) |
l.setLabel(index, label); |
} |
public void log(DebugResponseFrame f, long timestamp) throws IOException { |
for (DebugLogger l : loggers) |
l.log(f, timestamp); |
} |
public void start(String logId) throws IOException { |
for (DebugLogger l : loggers) |
l.start(logId); |
} |
public void end() throws IOException { |
for (DebugLogger l : loggers) |
l.end(); |
} |
void add(DebugLogger l) { |
loggers.add(l); |
} |
} |
private static DebugLogger createLogger(OutputStream out, String encoding) |
throws IOException { |
DebugLogger consoleLogger = new ConsoleDebugLogger(); |
XMLDebugLogger xmlLogger = new XMLDebugLogger(out, encoding); |
CompositeDebugLogger logger = new CompositeDebugLogger(); |
logger.add(consoleLogger); |
logger.add(xmlLogger); |
return logger; |
} |
private void prepareLoggers(final FrameQueue q, final DebugLogger logger) |
throws IOException { |
new Thread() { |
public void run() { |
} |
}.start(); |
} |
private void debug(final FrameQueue q, final DebugLogger logger, final int interval) throws IOException { |
LinkedList<Integer> missing = new LinkedList<Integer>(); |
for (int i = 0; i < 32; i++) { |
missing.add(i); |
} |
int tries = 0; |
while (!missing.isEmpty() && tries < 100) { |
int i = missing.get(0); |
tries++; |
AnalogDebugLabelRequestFrame f2 = ff |
.createAnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i); |
try { |
q.sendRequest(f2); |
AnalogDebugLabelResponseFrame rf = (AnalogDebugLabelResponseFrame) q.getResponseFor(f2, 1000); |
if (rf != null) { |
logger.setLabel(i, rf.getLabelAsString()); |
} else { |
String unknown = "analog " + i; |
logger.setLabel(i, unknown); |
missing.add(i); |
} |
} catch (IOException ex) { |
} |
} |
DebugRequestFrame f = ff.createDebugRequestFrame(Frame.FC_ADDRESS); |
f.setAutoSendInterval(interval); |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
q.sendRequest(f); |
Long firsttimestamp = null; |
while (!in.ready()) |
try { |
// q.output(f); |
DebugResponseFrame rf = (DebugResponseFrame) q.getResponseFor(f, 1000); |
// System.out.println(rf); |
long timestamp = System.currentTimeMillis(); |
if (firsttimestamp == null) { |
firsttimestamp = timestamp; |
timestamp = 0; |
} else { |
timestamp -= firsttimestamp; |
} |
logger.log(rf, timestamp); |
} catch (IOException ex) { |
STDERR.println(ex); |
} |
} |
public static void main(String[] args) throws IOException { |
MKDebugLogger test = new MKDebugLogger(); |
MKCommPort port = new RXTXSerialPort(); |
port.init(null); |
FrameQueue q = new FrameQueue(port); |
String encoding = "iso-8859-1"; |
OutputStream fout = new FileOutputStream("debug.xml"); |
DebugLogger logger = createLogger(fout, encoding); |
logger.start(new Date().toGMTString()); |
test.debug(q, logger, 10); |
logger.end(); |
fout.close(); |
} |
} |
/dongfang_FC_rewrite_tool/src/dongfang/mkt/main/UniversalConfigurator.java |
---|
0,0 → 1,228 |
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.configuration.ConfigSet; |
import dongfang.mkt.frames.UniversalReadParamSetRequestFrame; |
import dongfang.mkt.frames.UniversalReadParamSetResponseFrame; |
import dongfang.mkt.frames.UniversalWriteParamSetRequestFrame; |
import dongfang.mkt.frames.UniversalWriteParamSetResponseFrame; |
import dongfang.mkt.io.MKCommPort; |
import dongfang.mkt.io.RXTXSerialPort; |
import dongfang.mkt.serial.FrameQueue; |
public class UniversalConfigurator { |
private static void configure(UniversalWriteParamSetRequestFrame frame, |
int parameterSetNumber) throws IOException { |
MKCommPort port = new RXTXSerialPort(); |
port.init("COM10"); |
FrameQueue q = new FrameQueue(port); |
frame.setConfigurationSetNumber(parameterSetNumber); |
q.sendRequest(frame); |
UniversalWriteParamSetResponseFrame r = (UniversalWriteParamSetResponseFrame) 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 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); |
UniversalWriteParamSetRequestFrame frame = parseXMLParameterSet(inputStream); |
configure(frame, parameterSetNumber); |
} |
private static ConfigSet readConfiguration(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(); |
cs = ConfigSet.parseXMLConfigSet(paramSetVersion); |
cs.setData(data); |
// System.out.println(cs.toXML()); |
} |
q.kill(); |
return cs; |
} |
private static void readConfiguration(String s_parameterSetNumber, |
String fileName) throws IOException { |
int parameterSetNumber = Integer.parseInt(s_parameterSetNumber); |
ConfigSet cs = readConfiguration(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 |
- ConfigSet.NUMBER_OF_VARIABLES; |
for (int i = 0; i < ConfigSet.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 UniversalWriteParamSetRequestFrame 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()]; |
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 UniversalWriteParamSetRequestFrame(eepromVersion, |
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); |
} |
} |
public static void main(String[] args) throws IOException { |
if (args.length != 3 || (!"r".equals(args[0]) && !"w".equals(args[0]))) { |
System.err |
.println("Usage: UniversalConfigurator r [parameter set number to read from] [filename to write to]"); |
System.err |
.println("Usage: UniversalConfigurator w [parameter set number to write to] [filename to read from]"); |
System.exit(-1); |
} |
if ("r".equals(args[0])) { |
readConfiguration(args[1], args[2]); |
} else { |
writeConfiguration(args[1], args[2]); |
} |
System.exit(0); |
} |
} |