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.testdevice;
26  
27  import java.io.FileInputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  import org.apache.commons.cli.CommandLine;
32  import org.apache.commons.cli.CommandLineParser;
33  import org.apache.commons.cli.HelpFormatter;
34  import org.apache.commons.cli.Option;
35  import org.apache.commons.cli.OptionGroup;
36  import org.apache.commons.cli.Options;
37  import org.apache.commons.cli.ParseException;
38  import org.apache.commons.cli.PosixParser;
39  
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  import gnu.io.NoSuchPortException;
44  import gnu.io.PortInUseException;
45  import gnu.io.UnsupportedCommOperationException;
46  
47  /**
48   * DOCUMENT ME!
49   *
50   * @author aploese
51   */
52  public class Main {
53      private static Logger log = LoggerFactory.getLogger(Main.class);
54      private final KW2Dummy kwDummy;
55  
56      /**
57       * Creates a new Main object.
58       */
59      public Main() {
60          super();
61          log = LoggerFactory.getLogger(Main.class);
62          kwDummy = new KW2Dummy();
63      }
64  
65      /**
66       * DOCUMENT ME!
67       *
68       * @throws InterruptedException DOCUMENT ME!
69       */
70      public void close() throws InterruptedException {
71          kwDummy.close();
72      }
73  
74      /**
75       * DOCUMENT ME!
76       *
77       * @param args the command line arguments
78       *
79       * @throws Exception DOCUMENT ME!
80       */
81      public static void main(String[] args) throws Exception {
82          Options options = new Options();
83  
84          Option helpOpt = new Option("h", "help", false, "print this help message");
85          options.addOption(helpOpt);
86  
87          Option portOpt = new Option("p", "port", true, "serial port to use");
88          portOpt.setArgName("port");
89          portOpt.setType(String.class);
90          portOpt.setRequired(true);
91          options.addOption(portOpt);
92  
93          OptionGroup memMapOptionGroup = new OptionGroup();
94          memMapOptionGroup.setRequired(true);
95  
96          Option fileMemMapOpt = new Option("f", "file", true, "read from File");
97          fileMemMapOpt.setArgName("mem map file name");
98          portOpt.setType(String.class);
99          memMapOptionGroup.addOption(fileMemMapOpt);
100 
101         options.addOptionGroup(memMapOptionGroup);
102 
103         CommandLineParser parser = new PosixParser();
104         CommandLine cmd = null;
105 
106         try {
107             cmd = parser.parse(options, args);
108         } catch (ParseException ex) {
109             printHelp(options);
110 
111             return;
112         }
113 
114         if (cmd.hasOption('h')) {
115             printHelp(options);
116 
117             return;
118         }
119 
120         InputStream is = null;
121 
122         if (cmd.hasOption('f')) {
123             is = new FileInputStream(cmd.getOptionValue('f'));
124         }
125 
126         run(cmd.getOptionValue('p'), is);
127     }
128 
129     /**
130      * DOCUMENT ME!
131      *
132      * @param port DOCUMENT ME!
133      * @param is DOCUMENT ME!
134      *
135      * @throws Exception DOCUMENT ME!
136      */
137     public static void run(String port, InputStream is)
138                     throws Exception {
139         final Main device = new Main();
140 
141         try {
142             device.readFromStream(is);
143             device.openPort(port);
144             System.out.print("Press any key to quit!");
145             System.in.read();
146         } finally {
147             device.close();
148         }
149 
150         log.info("Ende");
151     }
152 
153     private void openPort(String serialPortName) throws IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
154         kwDummy.openPort(serialPortName);
155     }
156 
157     private static void printHelp(Options opts) {
158         HelpFormatter formatter = new HelpFormatter();
159         formatter.setWidth(300);
160         formatter.printHelp("openv4j-memory-image", opts);
161     }
162 
163     private void readFromStream(InputStream is) throws IOException {
164         kwDummy.readFromStream(is);
165     }
166 }