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   * @deprecated this transport will be removed in the next major version.
53   */
54  @Deprecated
55  public interface RxtxChannelConfig extends ChannelConfig {
56      enum Stopbits {
57          /**
58           * 1 stop bit will be sent at the end of every character
59           */
60          STOPBITS_1(SerialPort.STOPBITS_1),
61          /**
62           * 2 stop bits will be sent at the end of every character
63           */
64          STOPBITS_2(SerialPort.STOPBITS_2),
65          /**
66           * 1.5 stop bits will be sent at the end of every character
67           */
68          STOPBITS_1_5(SerialPort.STOPBITS_1_5);
69  
70          private final int value;
71  
72          Stopbits(int value) {
73              this.value = value;
74          }
75  
76          public int value() {
77              return value;
78          }
79  
80          public static Stopbits valueOf(int value) {
81              for (Stopbits stopbit : Stopbits.values()) {
82                  if (stopbit.value == value) {
83                      return stopbit;
84                  }
85              }
86              throw new IllegalArgumentException("unknown " + Stopbits.class.getSimpleName() + " value: " + value);
87          }
88      }
89  
90      enum Databits {
91          /**
92           * 5 data bits will be used for each character (ie. Baudot code)
93           */
94          DATABITS_5(SerialPort.DATABITS_5),
95          /**
96           * 6 data bits will be used for each character
97           */
98          DATABITS_6(SerialPort.DATABITS_6),
99          /**
100          * 7 data bits will be used for each character (ie. ASCII)
101          */
102         DATABITS_7(SerialPort.DATABITS_7),
103         /**
104          * 8 data bits will be used for each character (ie. binary data)
105          */
106         DATABITS_8(SerialPort.DATABITS_8);
107 
108         private final int value;
109 
110         Databits(int value) {
111             this.value = value;
112         }
113 
114         public int value() {
115             return value;
116         }
117 
118         public static Databits valueOf(int value) {
119             for (Databits databit : Databits.values()) {
120                 if (databit.value == value) {
121                     return databit;
122                 }
123             }
124             throw new IllegalArgumentException("unknown " + Databits.class.getSimpleName() + " value: " + value);
125         }
126     }
127 
128     enum Paritybit {
129         /**
130          * No parity bit will be sent with each data character at all
131          */
132         NONE(SerialPort.PARITY_NONE),
133         /**
134          * An odd parity bit will be sent with each data character, ie. will be set
135          * to 1 if the data character contains an even number of bits set to 1.
136          */
137         ODD(SerialPort.PARITY_ODD),
138         /**
139          * An even parity bit will be sent with each data character, ie. will be set
140          * to 1 if the data character contains an odd number of bits set to 1.
141          */
142         EVEN(SerialPort.PARITY_EVEN),
143         /**
144          * A mark parity bit (ie. always 1) will be sent with each data character
145          */
146         MARK(SerialPort.PARITY_MARK),
147         /**
148          * A space parity bit (ie. always 0) will be sent with each data character
149          */
150         SPACE(SerialPort.PARITY_SPACE);
151 
152         private final int value;
153 
154         Paritybit(int value) {
155             this.value = value;
156         }
157 
158         public int value() {
159             return value;
160         }
161 
162         public static Paritybit valueOf(int value) {
163             for (Paritybit paritybit : Paritybit.values()) {
164                 if (paritybit.value == value) {
165                     return paritybit;
166                 }
167             }
168             throw new IllegalArgumentException("unknown " + Paritybit.class.getSimpleName() + " value: " + value);
169         }
170     }
171     /**
172      * Sets the baud rate (ie. bits per second) for communication with the serial device.
173      * The baud rate will include bits for framing (in the form of stop bits and parity),
174      * such that the effective data rate will be lower than this value.
175      *
176      * @param baudrate The baud rate (in bits per second)
177      */
178     RxtxChannelConfig setBaudrate(int baudrate);
179 
180     /**
181      * Sets the number of stop bits to include at the end of every character to aid the
182      * serial device in synchronising with the data.
183      *
184      * @param stopbits The number of stop bits to use
185      */
186     RxtxChannelConfig setStopbits(Stopbits stopbits);
187 
188     /**
189      * Sets the number of data bits to use to make up each character sent to the serial
190      * device.
191      *
192      * @param databits The number of data bits to use
193      */
194     RxtxChannelConfig setDatabits(Databits databits);
195 
196     /**
197      * Sets the type of parity bit to be used when communicating with the serial device.
198      *
199      * @param paritybit The type of parity bit to be used
200      */
201     RxtxChannelConfig setParitybit(Paritybit paritybit);
202 
203     /**
204      * @return The configured baud rate, defaulting to 115200 if unset
205      */
206     int getBaudrate();
207 
208     /**
209      * @return The configured stop bits, defaulting to {@link Stopbits#STOPBITS_1} if unset
210      */
211     Stopbits getStopbits();
212 
213     /**
214      * @return The configured data bits, defaulting to {@link Databits#DATABITS_8} if unset
215      */
216     Databits getDatabits();
217 
218     /**
219      * @return The configured parity bit, defaulting to {@link Paritybit#NONE} if unset
220      */
221     Paritybit getParitybit();
222 
223     /**
224      * @return true if the serial device should support the Data Terminal Ready signal
225      */
226     boolean isDtr();
227 
228     /**
229      * Sets whether the serial device supports the Data Terminal Ready signal, used for
230      * flow control
231      *
232      * @param dtr true if DTR is supported, false otherwise
233      */
234     RxtxChannelConfig setDtr(boolean dtr);
235 
236     /**
237      * @return true if the serial device should support the Ready to Send signal
238      */
239     boolean isRts();
240 
241     /**
242      * Sets whether the serial device supports the Request To Send signal, used for flow
243      * control
244      *
245      * @param rts true if RTS is supported, false otherwise
246      */
247     RxtxChannelConfig setRts(boolean rts);
248 
249     /**
250      * @return The number of milliseconds to wait between opening the serial port and
251      *     initialising.
252      */
253     int getWaitTimeMillis();
254 
255     /**
256      * Sets the time to wait after opening the serial port and before sending it any
257      * configuration information or data. A value of 0 indicates that no waiting should
258      * occur.
259      *
260      * @param waitTimeMillis The number of milliseconds to wait, defaulting to 0 (no
261      *     wait) if unset
262      * @throws IllegalArgumentException if the supplied value is < 0
263      */
264     RxtxChannelConfig setWaitTimeMillis(int waitTimeMillis);
265 
266     /**
267      * Sets the maximal time (in ms) to block while try to read from the serial port. Default is 1000ms
268      */
269     RxtxChannelConfig setReadTimeout(int readTimeout);
270 
271     /**
272      * Return the maximal time (in ms) to block and wait for something to be ready to read.
273      */
274     int getReadTimeout();
275 
276     @Override
277     RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
278 
279     @Override
280     RxtxChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead);
281 
282     @Override
283     RxtxChannelConfig setWriteSpinCount(int writeSpinCount);
284 
285     @Override
286     RxtxChannelConfig setAllocator(ByteBufAllocator allocator);
287 
288     @Override
289     RxtxChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator);
290 
291     @Override
292     RxtxChannelConfig setAutoRead(boolean autoRead);
293 
294     @Override
295     RxtxChannelConfig setAutoClose(boolean autoClose);
296 
297     @Override
298     RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);
299 
300     @Override
301     RxtxChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark);
302 
303     @Override
304     RxtxChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
305 }