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