View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.rxtx;
17  
18  import gnu.io.SerialPort;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.channel.ChannelConfig;
21  import io.netty.channel.MessageSizeEstimator;
22  import io.netty.channel.RecvByteBufAllocator;
23  
24  /**
25   * A configuration class for RXTX device connections.
26   *
27   * <h3>Available options</h3>
28   *
29   * In addition to the options provided by {@link ChannelConfig},
30   * {@link DefaultRxtxChannelConfig} allows the following options in the option map:
31   *
32   * <table border="1" cellspacing="0" cellpadding="6">
33   * <tr>
34   * <th>Name</th><th>Associated setter method</th>
35   * </tr><tr>
36   * <td>{@link RxtxChannelOption#BAUD_RATE}</td><td>{@link #setBaudrate(int)}</td>
37   * </tr><tr>
38   * <td>{@link RxtxChannelOption#DTR}</td><td>{@link #setDtr(boolean)}</td>
39   * </tr><tr>
40   * <td>{@link RxtxChannelOption#RTS}</td><td>{@link #setRts(boolean)}</td>
41   * </tr><tr>
42   * <td>{@link RxtxChannelOption#STOP_BITS}</td><td>{@link #setStopbits(Stopbits)}</td>
43   * </tr><tr>
44   * <td>{@link RxtxChannelOption#DATA_BITS}</td><td>{@link #setDatabits(Databits)}</td>
45   * </tr><tr>
46   * <td>{@link RxtxChannelOption#PARITY_BIT}</td><td>{@link #setParitybit(Paritybit)}</td>
47   * </tr><tr>
48   * <td>{@link RxtxChannelOption#WAIT_TIME}</td><td>{@link #setWaitTimeMillis(int)}</td>
49   * </tr>
50   * </table>
51   */
52  public interface RxtxChannelConfig extends ChannelConfig {
53      enum Stopbits {
54          /**
55           * 1 stop bit will be sent at the end of every character
56           */
57          STOPBITS_1(SerialPort.STOPBITS_1),
58          /**
59           * 2 stop bits will be sent at the end of every character
60           */
61          STOPBITS_2(SerialPort.STOPBITS_2),
62          /**
63           * 1.5 stop bits will be sent at the end of every character
64           */
65          STOPBITS_1_5(SerialPort.STOPBITS_1_5);
66  
67          private final int value;
68  
69          Stopbits(int value) {
70              this.value = value;
71          }
72  
73          public int value() {
74              return value;
75          }
76  
77          public static Stopbits valueOf(int value) {
78              for (Stopbits stopbit : Stopbits.values()) {
79                  if (stopbit.value == value) {
80                      return stopbit;
81                  }
82              }
83              throw new IllegalArgumentException("unknown " + Stopbits.class.getSimpleName() + " value: " + value);
84          }
85      }
86  
87      enum Databits {
88          /**
89           * 5 data bits will be used for each character (ie. Baudot code)
90           */
91          DATABITS_5(SerialPort.DATABITS_5),
92          /**
93           * 6 data bits will be used for each character
94           */
95          DATABITS_6(SerialPort.DATABITS_6),
96          /**
97           * 7 data bits will be used for each character (ie. ASCII)
98           */
99          DATABITS_7(SerialPort.DATABITS_7),
100         /**
101          * 8 data bits will be used for each character (ie. binary data)
102          */
103         DATABITS_8(SerialPort.DATABITS_8);
104 
105         private final int value;
106 
107         Databits(int value) {
108             this.value = value;
109         }
110 
111         public int value() {
112             return value;
113         }
114 
115         public static Databits valueOf(int value) {
116             for (Databits databit : Databits.values()) {
117                 if (databit.value == value) {
118                     return databit;
119                 }
120             }
121             throw new IllegalArgumentException("unknown " + Databits.class.getSimpleName() + " value: " + value);
122         }
123     }
124 
125     enum Paritybit {
126         /**
127          * No parity bit will be sent with each data character at all
128          */
129         NONE(SerialPort.PARITY_NONE),
130         /**
131          * An odd parity bit will be sent with each data character, ie. will be set
132          * to 1 if the data character contains an even number of bits set to 1.
133          */
134         ODD(SerialPort.PARITY_ODD),
135         /**
136          * An even parity bit will be sent with each data character, ie. will be set
137          * to 1 if the data character contains an odd number of bits set to 1.
138          */
139         EVEN(SerialPort.PARITY_EVEN),
140         /**
141          * A mark parity bit (ie. always 1) will be sent with each data character
142          */
143         MARK(SerialPort.PARITY_MARK),
144         /**
145          * A space parity bit (ie. always 0) will be sent with each data character
146          */
147         SPACE(SerialPort.PARITY_SPACE);
148 
149         private final int value;
150 
151         Paritybit(int value) {
152             this.value = value;
153         }
154 
155         public int value() {
156             return value;
157         }
158 
159         public static Paritybit valueOf(int value) {
160             for (Paritybit paritybit : Paritybit.values()) {
161                 if (paritybit.value == value) {
162                     return paritybit;
163                 }
164             }
165             throw new IllegalArgumentException("unknown " + Paritybit.class.getSimpleName() + " value: " + value);
166         }
167     }
168     /**
169      * Sets the baud rate (ie. bits per second) for communication with the serial device.
170      * The baud rate will include bits for framing (in the form of stop bits and parity),
171      * such that the effective data rate will be lower than this value.
172      *
173      * @param baudrate The baud rate (in bits per second)
174      */
175     RxtxChannelConfig setBaudrate(int baudrate);
176 
177     /**
178      * Sets the number of stop bits to include at the end of every character to aid the
179      * serial device in synchronising with the data.
180      *
181      * @param stopbits The number of stop bits to use
182      */
183     RxtxChannelConfig setStopbits(Stopbits stopbits);
184 
185     /**
186      * Sets the number of data bits to use to make up each character sent to the serial
187      * device.
188      *
189      * @param databits The number of data bits to use
190      */
191     RxtxChannelConfig setDatabits(Databits databits);
192 
193     /**
194      * Sets the type of parity bit to be used when communicating with the serial device.
195      *
196      * @param paritybit The type of parity bit to be used
197      */
198     RxtxChannelConfig setParitybit(Paritybit paritybit);
199 
200     /**
201      * @return The configured baud rate, defaulting to 115200 if unset
202      */
203     int getBaudrate();
204 
205     /**
206      * @return The configured stop bits, defaulting to {@link Stopbits#STOPBITS_1} if unset
207      */
208     Stopbits getStopbits();
209 
210     /**
211      * @return The configured data bits, defaulting to {@link Databits#DATABITS_8} if unset
212      */
213     Databits getDatabits();
214 
215     /**
216      * @return The configured parity bit, defaulting to {@link Paritybit#NONE} if unset
217      */
218     Paritybit getParitybit();
219 
220     /**
221      * @return true if the serial device should support the Data Terminal Ready signal
222      */
223     boolean isDtr();
224 
225     /**
226      * Sets whether the serial device supports the Data Terminal Ready signal, used for
227      * flow control
228      *
229      * @param dtr true if DTR is supported, false otherwise
230      */
231     RxtxChannelConfig setDtr(boolean dtr);
232 
233     /**
234      * @return true if the serial device should support the Ready to Send signal
235      */
236     boolean isRts();
237 
238     /**
239      * Sets whether the serial device supports the Request To Send signal, used for flow
240      * control
241      *
242      * @param rts true if RTS is supported, false otherwise
243      */
244     RxtxChannelConfig setRts(boolean rts);
245 
246     /**
247      * @return The number of milliseconds to wait between opening the serial port and
248      *     initialising.
249      */
250     int getWaitTimeMillis();
251 
252     /**
253      * Sets the time to wait after opening the serial port and before sending it any
254      * configuration information or data. A value of 0 indicates that no waiting should
255      * occur.
256      *
257      * @param waitTimeMillis The number of milliseconds to wait, defaulting to 0 (no
258      *     wait) if unset
259      * @throws IllegalArgumentException if the supplied value is < 0
260      */
261     RxtxChannelConfig setWaitTimeMillis(int waitTimeMillis);
262 
263     /**
264      * Sets the maximal time (in ms) to block while try to read from the serial port. Default is 1000ms
265      */
266     RxtxChannelConfig setReadTimeout(int readTimout);
267 
268     /**
269      * Return the maximal time (in ms) to block and wait for something to be ready to read.
270      */
271     int getReadTimeout();
272 
273     @Override
274     RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
275 
276     @Override
277     RxtxChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead);
278 
279     @Override
280     RxtxChannelConfig setWriteSpinCount(int writeSpinCount);
281 
282     @Override
283     RxtxChannelConfig setAllocator(ByteBufAllocator allocator);
284 
285     @Override
286     RxtxChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator);
287 
288     @Override
289     RxtxChannelConfig setAutoRead(boolean autoRead);
290 
291     @Override
292     RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);
293 
294     @Override
295     RxtxChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark);
296 
297     @Override
298     RxtxChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
299 }