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