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