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 io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelOption;
20  import io.netty.channel.DefaultChannelConfig;
21  import io.netty.channel.MessageSizeEstimator;
22  import io.netty.channel.RecvByteBufAllocator;
23  
24  import java.util.Map;
25  
26  import static io.netty.channel.rxtx.RxtxChannelOption.*;
27  
28  /**
29   * Default configuration class for RXTX device connections.
30   */
31  final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements RxtxChannelConfig {
32  
33      private volatile int baudrate = 115200;
34      private volatile boolean dtr;
35      private volatile boolean rts;
36      private volatile Stopbits stopbits = Stopbits.STOPBITS_1;
37      private volatile Databits databits = Databits.DATABITS_8;
38      private volatile Paritybit paritybit = Paritybit.NONE;
39      private volatile int waitTime;
40      private volatile int readTimeout = 1000;
41  
42      DefaultRxtxChannelConfig(RxtxChannel channel) {
43          super(channel);
44      }
45  
46      @Override
47      public Map<ChannelOption<?>, Object> getOptions() {
48          return getOptions(super.getOptions(), BAUD_RATE, DTR, RTS, STOP_BITS, DATA_BITS, PARITY_BIT, WAIT_TIME);
49      }
50  
51      @SuppressWarnings("unchecked")
52      @Override
53      public <T> T getOption(ChannelOption<T> option) {
54          if (option == BAUD_RATE) {
55              return (T) Integer.valueOf(getBaudrate());
56          }
57          if (option == DTR) {
58              return (T) Boolean.valueOf(isDtr());
59          }
60          if (option == RTS) {
61              return (T) Boolean.valueOf(isRts());
62          }
63          if (option == STOP_BITS) {
64              return (T) getStopbits();
65          }
66          if (option == DATA_BITS) {
67              return (T) getDatabits();
68          }
69          if (option == PARITY_BIT) {
70              return (T) getParitybit();
71          }
72          if (option == WAIT_TIME) {
73              return (T) Integer.valueOf(getWaitTimeMillis());
74          }
75          if (option == READ_TIMEOUT) {
76              return (T) Integer.valueOf(getReadTimeout());
77          }
78          return super.getOption(option);
79      }
80  
81      @Override
82      public <T> boolean setOption(ChannelOption<T> option, T value) {
83          validate(option, value);
84  
85          if (option == BAUD_RATE) {
86              setBaudrate((Integer) value);
87          } else if (option == DTR) {
88              setDtr((Boolean) value);
89          } else if (option == RTS) {
90              setRts((Boolean) value);
91          } else if (option == STOP_BITS) {
92              setStopbits((Stopbits) value);
93          } else if (option == DATA_BITS) {
94              setDatabits((Databits) value);
95          } else if (option == PARITY_BIT) {
96              setParitybit((Paritybit) value);
97          } else if (option == WAIT_TIME) {
98              setWaitTimeMillis((Integer) value);
99          } else if (option == READ_TIMEOUT) {
100             setReadTimeout((Integer) value);
101         } else {
102             return super.setOption(option, value);
103         }
104         return true;
105     }
106 
107     @Override
108     public RxtxChannelConfig setBaudrate(final int baudrate) {
109         this.baudrate = baudrate;
110         return this;
111     }
112 
113     @Override
114     public RxtxChannelConfig setStopbits(final Stopbits stopbits) {
115         this.stopbits = stopbits;
116         return this;
117     }
118 
119     @Override
120     public RxtxChannelConfig setDatabits(final Databits databits) {
121         this.databits = databits;
122         return this;
123     }
124 
125     @Override
126     public RxtxChannelConfig setParitybit(final Paritybit paritybit) {
127         this.paritybit = paritybit;
128         return  this;
129     }
130 
131     @Override
132     public int getBaudrate() {
133         return baudrate;
134     }
135 
136     @Override
137     public Stopbits getStopbits() {
138         return stopbits;
139     }
140 
141     @Override
142     public Databits getDatabits() {
143         return databits;
144     }
145 
146     @Override
147     public Paritybit getParitybit() {
148         return paritybit;
149     }
150 
151     @Override
152     public boolean isDtr() {
153         return dtr;
154     }
155 
156     @Override
157     public RxtxChannelConfig setDtr(final boolean dtr) {
158         this.dtr = dtr;
159         return this;
160     }
161 
162     @Override
163     public boolean isRts() {
164         return rts;
165     }
166 
167     @Override
168     public RxtxChannelConfig setRts(final boolean rts) {
169         this.rts = rts;
170         return this;
171     }
172 
173     @Override
174     public int getWaitTimeMillis() {
175         return waitTime;
176     }
177 
178     @Override
179     public RxtxChannelConfig setWaitTimeMillis(final int waitTimeMillis) {
180         if (waitTimeMillis < 0) {
181             throw new IllegalArgumentException("Wait time must be >= 0");
182         }
183         waitTime = waitTimeMillis;
184         return this;
185     }
186 
187     @Override
188     public RxtxChannelConfig setReadTimeout(int readTimeout) {
189         if (readTimeout < 0) {
190             throw new IllegalArgumentException("readTime must be >= 0");
191         }
192         this.readTimeout = readTimeout;
193         return this;
194     }
195 
196     @Override
197     public int getReadTimeout() {
198         return readTimeout;
199     }
200 
201     @Override
202     public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
203         super.setConnectTimeoutMillis(connectTimeoutMillis);
204         return this;
205     }
206 
207     @Override
208     public RxtxChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
209         super.setMaxMessagesPerRead(maxMessagesPerRead);
210         return this;
211     }
212 
213     @Override
214     public RxtxChannelConfig setWriteSpinCount(int writeSpinCount) {
215         super.setWriteSpinCount(writeSpinCount);
216         return this;
217     }
218 
219     @Override
220     public RxtxChannelConfig setAllocator(ByteBufAllocator allocator) {
221         super.setAllocator(allocator);
222         return this;
223     }
224 
225     @Override
226     public RxtxChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
227         super.setRecvByteBufAllocator(allocator);
228         return this;
229     }
230 
231     @Override
232     public RxtxChannelConfig setAutoRead(boolean autoRead) {
233         super.setAutoRead(autoRead);
234         return this;
235     }
236 
237     @Override
238     public RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
239         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
240         return this;
241     }
242 
243     @Override
244     public RxtxChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
245         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
246         return this;
247     }
248 
249     @Override
250     public RxtxChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
251         super.setMessageSizeEstimator(estimator);
252         return this;
253     }
254 }