Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1532 | - | 1 | package dongfang.mkt.main; |
2 | |||
3 | import java.io.FileInputStream; |
||
4 | import java.io.FileWriter; |
||
5 | import java.io.IOException; |
||
6 | import java.io.InputStream; |
||
7 | |||
8 | import javax.xml.parsers.DocumentBuilder; |
||
9 | import javax.xml.parsers.DocumentBuilderFactory; |
||
10 | import javax.xml.parsers.ParserConfigurationException; |
||
11 | import javax.xml.xpath.XPath; |
||
12 | import javax.xml.xpath.XPathConstants; |
||
13 | import javax.xml.xpath.XPathExpressionException; |
||
14 | import javax.xml.xpath.XPathFactory; |
||
15 | |||
16 | import org.w3c.dom.Document; |
||
17 | import org.w3c.dom.Element; |
||
18 | import org.w3c.dom.NodeList; |
||
19 | import org.xml.sax.SAXException; |
||
20 | |||
21 | import dongfang.mkt.configuration.ConfigSet; |
||
22 | import dongfang.mkt.frames.UniversalReadParamSetRequestFrame; |
||
23 | import dongfang.mkt.frames.UniversalReadParamSetResponseFrame; |
||
24 | import dongfang.mkt.frames.UniversalWriteParamSetRequestFrame; |
||
25 | import dongfang.mkt.frames.UniversalWriteParamSetResponseFrame; |
||
26 | import dongfang.mkt.io.MKCommPort; |
||
27 | import dongfang.mkt.io.RXTXSerialPort; |
||
28 | import dongfang.mkt.serial.FrameQueue; |
||
29 | |||
30 | public class UniversalConfigurator { |
||
31 | private static void configure(UniversalWriteParamSetRequestFrame frame, |
||
32 | int parameterSetNumber) throws IOException { |
||
33 | MKCommPort port = new RXTXSerialPort(); |
||
34 | port.init("COM10"); |
||
35 | FrameQueue q = new FrameQueue(port); |
||
36 | |||
37 | frame.setConfigurationSetNumber(parameterSetNumber); |
||
38 | |||
39 | q.sendRequest(frame); |
||
40 | UniversalWriteParamSetResponseFrame r = (UniversalWriteParamSetResponseFrame) q |
||
41 | .getResponseFor(frame, 5000); |
||
42 | if (r == null) { |
||
43 | System.err.println("ERROR. Timeout waiting for response."); |
||
44 | } else if (!r.wasAccepted()) { |
||
45 | System.err |
||
46 | .println("ERROR. Parameter set not accepted. Check version against MK firmware EEPROM configuration version and checkparameter set number"); |
||
47 | } else { |
||
48 | System.out.println("Saved parameter set #" |
||
49 | + r.getParameterSetNumber() + "."); |
||
50 | } |
||
51 | |||
52 | q.kill(); |
||
53 | } |
||
54 | |||
55 | private static void writeConfiguration(String s_parameterSetNumber, |
||
56 | String fileName) throws IOException { |
||
57 | System.out.println("Writing parameter set #" + s_parameterSetNumber + " from file: " + fileName); |
||
58 | int parameterSetNumber = Integer.parseInt(s_parameterSetNumber); |
||
59 | InputStream inputStream = new FileInputStream(fileName); |
||
60 | UniversalWriteParamSetRequestFrame frame = parseXMLParameterSet(inputStream); |
||
61 | configure(frame, parameterSetNumber); |
||
62 | } |
||
63 | |||
64 | private static ConfigSet readConfiguration(int parameterSetNumber) |
||
65 | throws IOException { |
||
66 | MKCommPort port = new RXTXSerialPort(); |
||
67 | port.init(null); |
||
68 | FrameQueue q = new FrameQueue(port); |
||
69 | ConfigSet cs = null; |
||
70 | |||
71 | UniversalReadParamSetRequestFrame frame = new UniversalReadParamSetRequestFrame(); |
||
72 | frame.setConfigurationSetNumber(parameterSetNumber); |
||
73 | q.sendRequest(frame); |
||
74 | UniversalReadParamSetResponseFrame r = (UniversalReadParamSetResponseFrame) q |
||
75 | .getResponseFor(frame, 3000); |
||
76 | if (r == null) { |
||
77 | System.err.println("ERROR. Timeout waiting for response."); |
||
78 | } else { |
||
79 | int paramSetVersion = r.getConfigurationVersion(); |
||
80 | int[] data = r.getData(); |
||
81 | |||
82 | cs = ConfigSet.parseXMLConfigSet(paramSetVersion); |
||
83 | cs.setData(data); |
||
84 | // System.out.println(cs.toXML()); |
||
85 | } |
||
86 | q.kill(); |
||
87 | return cs; |
||
88 | } |
||
89 | |||
90 | private static void readConfiguration(String s_parameterSetNumber, |
||
91 | String fileName) throws IOException { |
||
92 | int parameterSetNumber = Integer.parseInt(s_parameterSetNumber); |
||
93 | ConfigSet cs = readConfiguration(parameterSetNumber); |
||
94 | if (cs != null) { |
||
95 | FileWriter fw = new FileWriter(fileName); |
||
96 | fw.write(cs.toXML()); |
||
97 | fw.close(); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | private static int substituteVariables(String s_value) { |
||
102 | int numberOfHighestVar = 255; |
||
103 | int numberOfLowestVar = numberOfHighestVar |
||
104 | - ConfigSet.NUMBER_OF_VARIABLES; |
||
105 | for (int i = 0; i < ConfigSet.NUMBER_OF_VARIABLES; i++) { |
||
106 | String varName = "var" + i; |
||
107 | if (varName.equals(s_value)) { |
||
108 | System.out.println("Substing: " + s_value + "-->" |
||
109 | + (numberOfLowestVar + i)); |
||
110 | return numberOfLowestVar + i; |
||
111 | } |
||
112 | } |
||
113 | return -1; |
||
114 | } |
||
115 | |||
116 | private static UniversalWriteParamSetRequestFrame parseXMLParameterSet( |
||
117 | InputStream input) throws IOException { |
||
118 | DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
||
119 | saxfac.setValidating(false); |
||
120 | try { |
||
121 | DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
||
122 | Document doc = bldr.parse(input); |
||
123 | |||
124 | XPath xpath = XPathFactory.newInstance().newXPath(); |
||
125 | |||
126 | String s_eepromVersion = xpath.evaluate( |
||
127 | "/parameterset/@eepromVersion", doc); |
||
128 | String ss_length = xpath.evaluate("/parameterset/@length", doc); |
||
129 | |||
130 | int s_length = 0; |
||
131 | try { |
||
132 | s_length = Integer.parseInt(ss_length); |
||
133 | } catch (NumberFormatException ex) { |
||
134 | // ignore. |
||
135 | } |
||
136 | |||
137 | int eepromVersion = -1; |
||
138 | |||
139 | try { |
||
140 | eepromVersion = Integer.parseInt(s_eepromVersion); |
||
141 | } catch (NumberFormatException ex) { |
||
142 | System.err |
||
143 | .println("The parameterset element must have an 'eepromVersion' attribute with a numerical value (eg.<parameterset eepromVersion='74'>)"); |
||
144 | System.exit(-1); |
||
145 | } |
||
146 | |||
147 | // if (s_length != null) |
||
148 | // try { |
||
149 | // length = Integer.parseInt(s_length); |
||
150 | // } catch (NumberFormatException ex) { |
||
151 | // System.err.println("The length attribute of the parameterset must have a numerical value"); |
||
152 | // System.exit(-1); |
||
153 | // } |
||
154 | |||
155 | NodeList parameterNodes = (NodeList) xpath.evaluate( |
||
156 | "/parameterset/parameter", doc, XPathConstants.NODESET); |
||
157 | int[] parameters = new int[parameterNodes.getLength()]; |
||
158 | |||
159 | if (s_length >=0 && s_length != parameterNodes.getLength()) { |
||
160 | System.err.println("The number of parameters ("+parameterNodes.getLength()+") does not match the number in the length attribute of the parameterset element ("+s_length+")."); |
||
161 | // System.exit(-1); |
||
162 | } |
||
163 | |||
164 | for (int i = 0; i < parameterNodes.getLength(); i++) { |
||
165 | Element e = (Element) parameterNodes.item(i); |
||
166 | int value = 0; |
||
167 | if (e.hasAttribute("value")) { |
||
168 | String s_value = e.getAttribute("value"); |
||
169 | value = substituteVariables(s_value); |
||
170 | if (value < 0) |
||
171 | try { |
||
172 | value = Integer.parseInt(s_value); |
||
173 | } catch (NumberFormatException ex) { |
||
174 | throw new NumberFormatException( |
||
175 | "Value is not a number and not a variable name."); |
||
176 | } |
||
177 | } else { |
||
178 | NodeList bitNodes = (NodeList) xpath.evaluate("bit", e, |
||
179 | XPathConstants.NODESET); |
||
180 | for (int j = 0; j < 8; j++) { |
||
181 | Element bitElement = (Element) bitNodes.item(j); |
||
182 | if (bitElement != null) { |
||
183 | String s_bitValue = bitElement |
||
184 | .getAttribute("value"); |
||
185 | if ("1".equals(s_bitValue)) |
||
186 | value |= (1 << j); |
||
187 | else if (!"0".equals(s_bitValue)) |
||
188 | throw new NumberFormatException( |
||
189 | "Bit value was not 0 or 1."); |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | parameters[i] = value; |
||
194 | } |
||
195 | input.close(); |
||
196 | return new UniversalWriteParamSetRequestFrame(eepromVersion, |
||
197 | parameters); |
||
198 | } catch (ParserConfigurationException ex) { |
||
199 | // Should never happen. |
||
200 | throw new RuntimeException(ex); |
||
201 | } catch (SAXException ex) { |
||
202 | System.err.println(ex); |
||
203 | System.err |
||
204 | .println("There is something screwed with your XML document. It is not well-formed and won't parse."); |
||
205 | throw new RuntimeException("Parse error."); |
||
206 | } catch (XPathExpressionException ex) { |
||
207 | // Should never happen. |
||
208 | throw new RuntimeException(ex); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | public static void main(String[] args) throws IOException { |
||
213 | if (args.length != 3 || (!"r".equals(args[0]) && !"w".equals(args[0]))) { |
||
214 | System.err |
||
215 | .println("Usage: UniversalConfigurator r [parameter set number to read from] [filename to write to]"); |
||
216 | System.err |
||
217 | .println("Usage: UniversalConfigurator w [parameter set number to write to] [filename to read from]"); |
||
218 | System.exit(-1); |
||
219 | } |
||
220 | |||
221 | if ("r".equals(args[0])) { |
||
222 | readConfiguration(args[1], args[2]); |
||
223 | } else { |
||
224 | writeConfiguration(args[1], args[2]); |
||
225 | } |
||
226 | System.exit(0); |
||
227 | } |
||
228 | } |