Rev 1623 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1611 | - | 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.comm.FrameQueue; |
||
22 | import dongfang.mkt.comm.MKConnection; |
||
23 | import dongfang.mkt.comm.serial.RXTXSerialPort; |
||
24 | import dongfang.mkt.configuration.ParameterSet; |
||
25 | import dongfang.mkt.frames.ReadParamSetRequestFrame; |
||
26 | import dongfang.mkt.frames.ReadParamSetResponseFrame; |
||
27 | import dongfang.mkt.frames.WriteParamSetRequestFrame; |
||
28 | import dongfang.mkt.frames.WriteParamSetResponseFrame; |
||
29 | |||
30 | public class ParameterSetConfigurator { |
||
31 | private static void configure(String portIdentifier, WriteParamSetRequestFrame frame, |
||
32 | int parameterSetNumber) throws IOException { |
||
33 | MKConnection port = new RXTXSerialPort(); |
||
34 | port.init(portIdentifier); |
||
35 | FrameQueue q = new FrameQueue(port); |
||
36 | |||
37 | frame.setConfigurationSetNumber(parameterSetNumber); |
||
38 | |||
39 | q.sendRequest(frame); |
||
40 | WriteParamSetResponseFrame r = (WriteParamSetResponseFrame) q.getResponseFor(frame, 5000); |
||
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 | |||
54 | private static void writeConfiguration(String portIdentifier, String s_parameterSetNumber, |
||
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 | WriteParamSetRequestFrame frame = parseXMLParameterSet(inputStream); |
||
60 | configure(portIdentifier, frame, parameterSetNumber); |
||
61 | } |
||
62 | |||
63 | private static ParameterSet readConfiguration(String portIdentifier, String s_parameterSetNumber) throws IOException { |
||
64 | MKConnection port = new RXTXSerialPort(); |
||
65 | port.init(portIdentifier); |
||
66 | FrameQueue q = new FrameQueue(port); |
||
67 | ParameterSet cs = null; |
||
68 | int parameterSetNumber = Integer.parseInt(s_parameterSetNumber); |
||
69 | |||
70 | ReadParamSetRequestFrame frame = new ReadParamSetRequestFrame(); |
||
71 | frame.setConfigurationSetNumber(parameterSetNumber); |
||
72 | q.sendRequest(frame); |
||
73 | ReadParamSetResponseFrame r = (ReadParamSetResponseFrame) q.getResponseFor(frame, 5000); |
||
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 = ParameterSet.parseXMLParameterSet(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) |
||
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(); |
||
105 | for (int i=0; i<data.length; i++) |
||
106 | System.out.println(data[i]); |
||
107 | } |
||
108 | q.kill(); |
||
109 | return cs; |
||
110 | } |
||
111 | */ |
||
112 | |||
113 | private static void readConfiguration(String portIdentifier, String s_parameterSetNumber, String fileName) throws IOException { |
||
114 | ParameterSet cs = readConfiguration(portIdentifier, s_parameterSetNumber); |
||
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; |
||
124 | int numberOfLowestVar = numberOfHighestVar - ParameterSet.NUMBER_OF_VARIABLES + 1; |
||
125 | for (int i = 0; i < ParameterSet.NUMBER_OF_VARIABLES; i++) { |
||
126 | String varName = "var" + i; |
||
127 | if (varName.equals(s_value)) { |
||
128 | System.out.println("Substing: " + s_value + "-->" + (numberOfLowestVar + i)); |
||
129 | return numberOfLowestVar + i; |
||
130 | } |
||
131 | } |
||
132 | return -1; |
||
133 | } |
||
134 | |||
135 | private static WriteParamSetRequestFrame 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 | |||
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 | |||
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(); |
||
223 | return new WriteParamSetRequestFrame(eepromVersion, "01234567890ab".toCharArray(), parameters); |
||
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 | |||
238 | static void help() { |
||
239 | System.err |
||
1623 | - | 240 | .println("Usage: ParameterSetConfigurator r [parameter set number to read from] [filename to write to]"); |
1611 | - | 241 | System.err |
1623 | - | 242 | .println("Usage: ParameterSetConfigurator w [parameter set number to write to] [filename to read from]"); |
1611 | - | 243 | System.exit(-1); |
244 | } |
||
245 | |||
246 | public static void main(String[] args) throws IOException { |
||
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(); |
||
250 | |||
1806 | - | 251 | String portIdentifier = "COM10"; |
1611 | - | 252 | |
253 | if ("r".equals(args[0])) { |
||
254 | readConfiguration(portIdentifier, args[1]); |
||
255 | } else { |
||
256 | writeConfiguration(portIdentifier, args[1], args[2]); |
||
257 | } |
||
258 | System.exit(0); |
||
259 | } |
||
260 | } |