1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sf.openv4j.memoryimage;
26
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.OutputStreamWriter;
36
37 import java.util.Date;
38
39 import org.apache.commons.cli.CommandLine;
40 import org.apache.commons.cli.CommandLineParser;
41 import org.apache.commons.cli.HelpFormatter;
42 import org.apache.commons.cli.Option;
43 import org.apache.commons.cli.OptionGroup;
44 import org.apache.commons.cli.Options;
45 import org.apache.commons.cli.ParseException;
46 import org.apache.commons.cli.PosixParser;
47
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import gnu.io.NoSuchPortException;
52 import gnu.io.PortInUseException;
53 import gnu.io.SerialPort;
54 import gnu.io.UnsupportedCommOperationException;
55
56 import net.sf.openv4j.DataPoint;
57 import net.sf.openv4j.protocolhandlers.DataContainer;
58 import net.sf.openv4j.protocolhandlers.ProtocolHandler;
59 import net.sf.openv4j.protocolhandlers.SegmentedDataContainer;
60
61
62
63
64
65
66 public class Main {
67 private static Logger log = LoggerFactory.getLogger(Main.class);
68
69
70
71
72 public Main() {
73 super();
74 }
75
76
77
78
79
80
81
82
83
84 public static void main(String[] args) throws FileNotFoundException, IOException {
85 Options options = new Options();
86 Option opt;
87 OptionGroup optg;
88
89 opt = new Option("h", "help", false, "print this help message");
90 options.addOption(opt);
91
92 optg = new OptionGroup();
93 optg.setRequired(true);
94
95 opt = new Option("p", "port", true, "serial port to use");
96 opt.setArgName("port");
97 opt.setType(String.class);
98 optg.addOption(opt);
99
100 opt = new Option("r", "reevaluate-file", true, "reevaluate memory image file");
101 opt.setArgName("reevaluateImage");
102 opt.setType(String.class);
103 optg.addOption(opt);
104
105 options.addOptionGroup(optg);
106
107 opt = new Option("s", "segment-size", true, "segment-size default " + SegmentedDataContainer.DEFAULT_SEGMENT_SIZE);
108 opt.setArgName("segmentSize");
109 opt.setType(Integer.class);
110 options.addOption(opt);
111
112 optg = new OptionGroup();
113 optg.setRequired(true);
114
115 opt = new Option("a", "all-known-blocks", false, "fetch all known 16 Byte blocks wich are different from 0xFF");
116 opt.setArgName("allBlocks");
117 optg.addOption(opt);
118
119 opt = new Option("d", "all-data-points", false, "fetch all known datapoints");
120 opt.setArgName("allDataPoints");
121 optg.addOption(opt);
122
123 opt = new Option("f", "full-scan", false, "fetch all 0x0000 to 0xffff");
124 opt.setArgName("complete memory map");
125 optg.addOption(opt);
126
127 options.addOptionGroup(optg);
128 opt = null;
129 optg = null;
130
131 CommandLineParser parser = new PosixParser();
132 CommandLine cmd = null;
133
134 try {
135 cmd = parser.parse(options, args);
136 } catch (ParseException ex) {
137 printHelp(options);
138
139 return;
140 }
141
142 if (cmd.hasOption("help")) {
143 printHelp(options);
144
145 return;
146 }
147
148 DataContainer container;
149
150 if (cmd.hasOption("reevaluate-file")) {
151 container = new SegmentedDataContainer(Integer.parseInt(cmd.getOptionValue("segment-size", String.valueOf(SegmentedDataContainer.DEFAULT_SEGMENT_SIZE))));
152 extractMemoryImage(new FileInputStream(cmd.getOptionValue("reevaluate-file")), container);
153 } else {
154 container = new SegmentedDataContainer(Integer.parseInt(cmd.getOptionValue("segment-size", String.valueOf(SegmentedDataContainer.DEFAULT_SEGMENT_SIZE))));
155
156 if (cmd.hasOption("all-known-blocks")) {
157 for (int address : DataPoint.BLOCKS_16) {
158 container.addToDataContainer(address, 16);
159 }
160 } else if (cmd.hasOption("all-data-points")) {
161 for (DataPoint dp : DataPoint.values()) {
162 container.addToDataContainer(dp);
163 }
164 } else if (cmd.hasOption("full-scan")) {
165 container.addToDataContainer(0x0000, 0x010000);
166 }
167 }
168
169 Date startTime = new Date();
170 String outName;
171
172 if (cmd.hasOption("reevaluate-file")) {
173 outName = cmd.getOptionValue("reevaluate-file");
174 } else {
175 run(cmd.getOptionValue("port"), container);
176 outName = String.format("mem-image_%1$tY%1$tm%1$td_%1$tH%1$tM%1$tS.txt", startTime);
177 }
178
179 System.out.println("Data Points:");
180
181 StringBuilder sb = new StringBuilder();
182 DataPoint.printAll(sb, container);
183 System.out.append(sb.toString());
184 sb = null;
185
186 FileOutputStream fos = new FileOutputStream(outName);
187 sb = new StringBuilder();
188 sb.append("Memory Image: {\n");
189 sb.append(container.toString());
190 sb.append("\n}");
191 sb.append("\nProperties By Address: {\n");
192 DataPoint.printAddresses(sb, container);
193 sb.append("}");
194 sb.append("\nProperties By Group: {\n");
195 DataPoint.printAll(sb, container);
196 sb.append("}");
197
198 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
199 bw.write(sb.toString());
200 bw.flush();
201 bw.close();
202 fos.flush();
203 fos.close();
204 }
205
206
207
208
209
210
211
212 public static void run(String port, final DataContainer dc) {
213 SerialPort masterPort = null;
214 Main expl = new Main();
215 ProtocolHandler protocolHandler = new ProtocolHandler();
216
217 try {
218 masterPort = ProtocolHandler.openPort(port);
219 protocolHandler.setStreams(masterPort.getInputStream(), masterPort.getOutputStream());
220 } catch (NoSuchPortException ex) {
221 log.error(ex.getMessage(), ex);
222 } catch (PortInUseException ex) {
223 log.error(ex.getMessage(), ex);
224 } catch (UnsupportedCommOperationException ex) {
225 log.error(ex.getMessage(), ex);
226 } catch (IOException ex) {
227 log.error(ex.getMessage(), ex);
228 }
229
230 try {
231 protocolHandler.setReadRequest(dc);
232
233 synchronized (dc) {
234 dc.wait(dc.getDataBlockCount() * 60000);
235 }
236 } catch (Exception ex) {
237 System.err.print("Error sleep " + ex);
238 }
239
240 try {
241 System.out.println("CLOSE");
242 protocolHandler.close();
243 } catch (InterruptedException ex) {
244 log.error(ex.getMessage(), ex);
245 }
246
247 if (masterPort != null) {
248 masterPort.close();
249 }
250 }
251
252 private static void extractMemoryImage(InputStream is, DataContainer container)
253 throws IOException {
254 boolean isMemoryImage = false;
255 BufferedReader br = new BufferedReader(new InputStreamReader(is));
256 String line;
257
258 while ((line = br.readLine()) != null) {
259 if (!isMemoryImage) {
260 if ("Memory Image: {".equals(line)) {
261 isMemoryImage = true;
262 }
263 } else {
264 if ("}".equals(line)) {
265 return;
266 } else {
267 container.addMemoryImageLine(line);
268 }
269 }
270 }
271 }
272
273 private static void printHelp(Options opts) {
274 HelpFormatter formatter = new HelpFormatter();
275 formatter.setWidth(300);
276 formatter.printHelp("openv4j-memory-image", opts);
277 }
278 }