Rev 1559 | 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 | |||
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 { |
||
1560 | - | 31 | private static void configure(String portIdentifier, UniversalWriteParamSetRequestFrame frame, |
1532 | - | 32 | int parameterSetNumber) throws IOException { |
33 | MKCommPort 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); |
||
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 | |||
1560 | - | 55 | private static void writeConfiguration(String portIdentifier, String s_parameterSetNumber, |
1532 | - | 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); |
||
1560 | - | 61 | configure(portIdentifier, frame, parameterSetNumber); |
1532 | - | 62 | } |
63 | |||
1560 | - | 64 | private static ConfigSet readConfiguration(String portIdentifier, int parameterSetNumber) throws IOException { |
1559 | - | 65 | MKCommPort port = new RXTXSerialPort(); |
1560 | - | 66 | port.init(portIdentifier); |
1559 | - | 67 | FrameQueue q = new FrameQueue(port); |
68 | ConfigSet cs = null; |
||
69 | |||
70 | UniversalReadParamSetRequestFrame frame = new UniversalReadParamSetRequestFrame(); |
||
71 | frame.setConfigurationSetNumber(parameterSetNumber); |
||
72 | q.sendRequest(frame); |
||
73 | UniversalReadParamSetResponseFrame r = (UniversalReadParamSetResponseFrame) q.getResponseFor(frame, 3000); |
||
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 { |
1532 | - | 114 | int parameterSetNumber = Integer.parseInt(s_parameterSetNumber); |
1560 | - | 115 | ConfigSet cs = readConfiguration(portIdentifier, parameterSetNumber); |
1532 | - | 116 | if (cs != null) { |
117 | FileWriter fw = new FileWriter(fileName); |
||
118 | fw.write(cs.toXML()); |
||
119 | fw.close(); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | private static int substituteVariables(String s_value) { |
||
124 | int numberOfHighestVar = 255; |
||
125 | int numberOfLowestVar = numberOfHighestVar |
||
126 | - ConfigSet.NUMBER_OF_VARIABLES; |
||
127 | for (int i = 0; i < ConfigSet.NUMBER_OF_VARIABLES; i++) { |
||
128 | String varName = "var" + i; |
||
129 | if (varName.equals(s_value)) { |
||
130 | System.out.println("Substing: " + s_value + "-->" |
||
131 | + (numberOfLowestVar + i)); |
||
132 | return numberOfLowestVar + i; |
||
133 | } |
||
134 | } |
||
135 | return -1; |
||
136 | } |
||
137 | |||
138 | private static UniversalWriteParamSetRequestFrame parseXMLParameterSet( |
||
139 | InputStream input) throws IOException { |
||
140 | DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
||
141 | saxfac.setValidating(false); |
||
142 | try { |
||
143 | DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
||
144 | Document doc = bldr.parse(input); |
||
145 | |||
146 | XPath xpath = XPathFactory.newInstance().newXPath(); |
||
147 | |||
148 | String s_eepromVersion = xpath.evaluate( |
||
149 | "/parameterset/@eepromVersion", doc); |
||
150 | String ss_length = xpath.evaluate("/parameterset/@length", doc); |
||
151 | |||
152 | int s_length = 0; |
||
153 | try { |
||
154 | s_length = Integer.parseInt(ss_length); |
||
155 | } catch (NumberFormatException ex) { |
||
156 | // ignore. |
||
157 | } |
||
158 | |||
159 | int eepromVersion = -1; |
||
160 | |||
161 | try { |
||
162 | eepromVersion = Integer.parseInt(s_eepromVersion); |
||
163 | } catch (NumberFormatException ex) { |
||
164 | System.err |
||
165 | .println("The parameterset element must have an 'eepromVersion' attribute with a numerical value (eg.<parameterset eepromVersion='74'>)"); |
||
166 | System.exit(-1); |
||
167 | } |
||
168 | |||
169 | // if (s_length != null) |
||
170 | // try { |
||
171 | // length = Integer.parseInt(s_length); |
||
172 | // } catch (NumberFormatException ex) { |
||
173 | // System.err.println("The length attribute of the parameterset must have a numerical value"); |
||
174 | // System.exit(-1); |
||
175 | // } |
||
176 | |||
177 | NodeList parameterNodes = (NodeList) xpath.evaluate( |
||
178 | "/parameterset/parameter", doc, XPathConstants.NODESET); |
||
179 | int[] parameters = new int[parameterNodes.getLength()]; |
||
180 | |||
181 | if (s_length >=0 && s_length != parameterNodes.getLength()) { |
||
182 | System.err.println("The number of parameters ("+parameterNodes.getLength()+") does not match the number in the length attribute of the parameterset element ("+s_length+")."); |
||
183 | // System.exit(-1); |
||
184 | } |
||
185 | |||
186 | for (int i = 0; i < parameterNodes.getLength(); i++) { |
||
187 | Element e = (Element) parameterNodes.item(i); |
||
188 | int value = 0; |
||
189 | if (e.hasAttribute("value")) { |
||
190 | String s_value = e.getAttribute("value"); |
||
191 | value = substituteVariables(s_value); |
||
192 | if (value < 0) |
||
193 | try { |
||
194 | value = Integer.parseInt(s_value); |
||
195 | } catch (NumberFormatException ex) { |
||
196 | throw new NumberFormatException( |
||
197 | "Value is not a number and not a variable name."); |
||
198 | } |
||
199 | } else { |
||
200 | NodeList bitNodes = (NodeList) xpath.evaluate("bit", e, |
||
201 | XPathConstants.NODESET); |
||
202 | for (int j = 0; j < 8; j++) { |
||
203 | Element bitElement = (Element) bitNodes.item(j); |
||
204 | if (bitElement != null) { |
||
205 | String s_bitValue = bitElement |
||
206 | .getAttribute("value"); |
||
207 | if ("1".equals(s_bitValue)) |
||
208 | value |= (1 << j); |
||
209 | else if (!"0".equals(s_bitValue)) |
||
210 | throw new NumberFormatException( |
||
211 | "Bit value was not 0 or 1."); |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | parameters[i] = value; |
||
216 | } |
||
217 | input.close(); |
||
218 | return new UniversalWriteParamSetRequestFrame(eepromVersion, |
||
219 | parameters); |
||
220 | } catch (ParserConfigurationException ex) { |
||
221 | // Should never happen. |
||
222 | throw new RuntimeException(ex); |
||
223 | } catch (SAXException ex) { |
||
224 | System.err.println(ex); |
||
225 | System.err |
||
226 | .println("There is something screwed with your XML document. It is not well-formed and won't parse."); |
||
227 | throw new RuntimeException("Parse error."); |
||
228 | } catch (XPathExpressionException ex) { |
||
229 | // Should never happen. |
||
230 | throw new RuntimeException(ex); |
||
231 | } |
||
232 | } |
||
233 | |||
1560 | - | 234 | static void help() { |
235 | System.err |
||
236 | .println("Usage: UniversalConfigurator r [parameter set number to read from] [filename to write to]"); |
||
237 | System.err |
||
238 | .println("Usage: UniversalConfigurator w [parameter set number to write to] [filename to read from]"); |
||
239 | System.exit(-1); |
||
240 | } |
||
241 | |||
1532 | - | 242 | public static void main(String[] args) throws IOException { |
1560 | - | 243 | if (!"r".equals(args[0]) && !"w".equals(args[0])) help(); |
244 | if ("w".equals(args[0]) && (args.length!=3 && args.length!=4)) help(); |
||
245 | if ("r".equals(args[0]) && (args.length!=3 && args.length!=4)) help(); |
||
1532 | - | 246 | |
1560 | - | 247 | String portIdentifier = "COM10"; |
248 | |||
1532 | - | 249 | if ("r".equals(args[0])) { |
1560 | - | 250 | readConfiguration(portIdentifier, Integer.parseInt(args[1])); |
1532 | - | 251 | } else { |
1560 | - | 252 | writeConfiguration(portIdentifier, args[1], args[2]); |
1532 | - | 253 | } |
254 | System.exit(0); |
||
255 | } |
||
256 | } |