View Javadoc
1   /*
2    * Copyright 2009, openv4j.sf.net, and individual contributors as indicated
3    * by the @authors tag. See the copyright.txt in the distribution for a
4    * full listing of individual contributors.
5    *
6    * This is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU General Public License as
8    * published by the Free Software Foundation; either version 3 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   *
21   * $Id: $
22   *
23   * @author arnep
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   * DOCUMENT ME!
63   *
64   * @author aploese
65   */
66  public class Main {
67      private static Logger log = LoggerFactory.getLogger(Main.class);
68  
69      /**
70       * Creates a new Main object.
71       */
72      public Main() {
73          super();
74      }
75  
76      /**
77       * DOCUMENT ME!
78       *
79       * @param args the command line arguments
80       *
81       * @throws FileNotFoundException DOCUMENT ME!
82       * @throws IOException DOCUMENT ME!
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      * DOCUMENT ME!
208      *
209      * @param port DOCUMENT ME!
210      * @param dc DOCUMENT ME!
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 }