Add RxtxChannelConfig and tighten up visibility. Also support method chaining
This commit is contained in:
parent
c1e827fd72
commit
b20e597217
@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.channel.rxtx;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBufAllocator;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
|
import io.netty.channel.DefaultChannelConfig;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static io.netty.channel.rxtx.RxtxChannelOption.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default configuration class for RXTX device connections.
|
||||||
|
*/
|
||||||
|
final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements RxtxChannelConfig {
|
||||||
|
|
||||||
|
private volatile int baudrate = 115200;
|
||||||
|
private volatile boolean dtr;
|
||||||
|
private volatile boolean rts;
|
||||||
|
private volatile Stopbits stopbits = Stopbits.STOPBITS_1;
|
||||||
|
private volatile Databits databits = Databits.DATABITS_8;
|
||||||
|
private volatile Paritybit paritybit = Paritybit.NONE;
|
||||||
|
|
||||||
|
public DefaultRxtxChannelConfig(RxtxChannel channel) {
|
||||||
|
super(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<ChannelOption<?>, Object> getOptions() {
|
||||||
|
return getOptions(super.getOptions(), BAUD_RATE, DTR, RTS, STOP_BITS, DATA_BITS, PARITY_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public <T> T getOption(ChannelOption<T> option) {
|
||||||
|
if (option == BAUD_RATE) {
|
||||||
|
return (T) Integer.valueOf(getBaudrate());
|
||||||
|
}
|
||||||
|
if (option == DTR) {
|
||||||
|
return (T) Boolean.valueOf(isDtr());
|
||||||
|
}
|
||||||
|
if (option == RTS) {
|
||||||
|
return (T) Boolean.valueOf(isRts());
|
||||||
|
}
|
||||||
|
if (option == STOP_BITS) {
|
||||||
|
return (T) getStopbits();
|
||||||
|
}
|
||||||
|
if (option == DATA_BITS) {
|
||||||
|
return (T) getDatabits();
|
||||||
|
}
|
||||||
|
if (option == PARITY_BIT) {
|
||||||
|
return (T) getParitybit();
|
||||||
|
}
|
||||||
|
return super.getOption(option);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> boolean setOption(ChannelOption<T> option, T value) {
|
||||||
|
validate(option, value);
|
||||||
|
|
||||||
|
if (option == BAUD_RATE) {
|
||||||
|
setBaudrate((Integer) value);
|
||||||
|
} else if (option == DTR) {
|
||||||
|
setDtr((Boolean) value);
|
||||||
|
} else if (option == RTS) {
|
||||||
|
setRts((Boolean) value);
|
||||||
|
} else if (option == STOP_BITS) {
|
||||||
|
setStopbits((Stopbits) value);
|
||||||
|
} else if (option == DATA_BITS) {
|
||||||
|
setDatabits((Databits) value);
|
||||||
|
} else if (option == PARITY_BIT) {
|
||||||
|
setParitybit((Paritybit) value);
|
||||||
|
} else {
|
||||||
|
return super.setOption(option, value);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setBaudrate(final int baudrate) {
|
||||||
|
this.baudrate = baudrate;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setStopbits(final Stopbits stopbits) {
|
||||||
|
this.stopbits = stopbits;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setDatabits(final Databits databits) {
|
||||||
|
this.databits = databits;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setParitybit(final Paritybit paritybit) {
|
||||||
|
this.paritybit = paritybit;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBaudrate() {
|
||||||
|
return baudrate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stopbits getStopbits() {
|
||||||
|
return stopbits;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Databits getDatabits() {
|
||||||
|
return databits;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Paritybit getParitybit() {
|
||||||
|
return paritybit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDtr() {
|
||||||
|
return dtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setDtr(final boolean dtr) {
|
||||||
|
this.dtr = dtr;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRts() {
|
||||||
|
return rts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setRts(final boolean rts) {
|
||||||
|
this.rts = rts;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
|
||||||
|
return (RxtxChannelConfig) super.setConnectTimeoutMillis(connectTimeoutMillis);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setWriteSpinCount(int writeSpinCount) {
|
||||||
|
return (RxtxChannelConfig) super.setWriteSpinCount(writeSpinCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setAllocator(ByteBufAllocator allocator) {
|
||||||
|
return (RxtxChannelConfig) super.setAllocator(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RxtxChannelConfig setAutoRead(boolean autoRead) {
|
||||||
|
return (RxtxChannelConfig) super.setAutoRead(autoRead);
|
||||||
|
}
|
||||||
|
}
|
@ -51,7 +51,7 @@ public class RxtxChannel extends AbstractOioByteChannel {
|
|||||||
public RxtxChannel() {
|
public RxtxChannel() {
|
||||||
super(null, null);
|
super(null, null);
|
||||||
|
|
||||||
config = new RxtxChannelConfig(this);
|
config = new DefaultRxtxChannelConfig(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -16,13 +16,8 @@
|
|||||||
package io.netty.channel.rxtx;
|
package io.netty.channel.rxtx;
|
||||||
|
|
||||||
import gnu.io.SerialPort;
|
import gnu.io.SerialPort;
|
||||||
import io.netty.channel.ChannelOption;
|
import io.netty.buffer.ByteBufAllocator;
|
||||||
import io.netty.channel.ChannelConfig;
|
import io.netty.channel.ChannelConfig;
|
||||||
import io.netty.channel.DefaultChannelConfig;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static io.netty.channel.rxtx.RxtxChannelOption.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A configuration class for RXTX device connections.
|
* A configuration class for RXTX device connections.
|
||||||
@ -30,7 +25,7 @@ import static io.netty.channel.rxtx.RxtxChannelOption.*;
|
|||||||
* <h3>Available options</h3>
|
* <h3>Available options</h3>
|
||||||
*
|
*
|
||||||
* In addition to the options provided by {@link ChannelConfig},
|
* In addition to the options provided by {@link ChannelConfig},
|
||||||
* {@link RxtxChannelConfig} allows the following options in the option map:
|
* {@link DefaultRxtxChannelConfig} allows the following options in the option map:
|
||||||
*
|
*
|
||||||
* <table border="1" cellspacing="0" cellpadding="6">
|
* <table border="1" cellspacing="0" cellpadding="6">
|
||||||
* <tr>
|
* <tr>
|
||||||
@ -50,9 +45,8 @@ import static io.netty.channel.rxtx.RxtxChannelOption.*;
|
|||||||
* </tr>
|
* </tr>
|
||||||
* </table>
|
* </table>
|
||||||
*/
|
*/
|
||||||
public class RxtxChannelConfig extends DefaultChannelConfig {
|
public interface RxtxChannelConfig extends ChannelConfig {
|
||||||
|
enum Stopbits {
|
||||||
public enum Stopbits {
|
|
||||||
/**
|
/**
|
||||||
* 1 stop bit will be sent at the end of every character
|
* 1 stop bit will be sent at the end of every character
|
||||||
*/
|
*/
|
||||||
@ -86,7 +80,7 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Databits {
|
enum Databits {
|
||||||
/**
|
/**
|
||||||
* 5 data bits will be used for each character (ie. Baudot code)
|
* 5 data bits will be used for each character (ie. Baudot code)
|
||||||
*/
|
*/
|
||||||
@ -124,7 +118,7 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Paritybit {
|
enum Paritybit {
|
||||||
/**
|
/**
|
||||||
* No parity bit will be sent with each data character at all
|
* No parity bit will be sent with each data character at all
|
||||||
*/
|
*/
|
||||||
@ -167,69 +161,6 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
throw new IllegalArgumentException("unknown " + Paritybit.class.getSimpleName() + " value: " + value);
|
throw new IllegalArgumentException("unknown " + Paritybit.class.getSimpleName() + " value: " + value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private volatile int baudrate = 115200;
|
|
||||||
private volatile boolean dtr;
|
|
||||||
private volatile boolean rts;
|
|
||||||
private volatile Stopbits stopbits = Stopbits.STOPBITS_1;
|
|
||||||
private volatile Databits databits = Databits.DATABITS_8;
|
|
||||||
private volatile Paritybit paritybit = Paritybit.NONE;
|
|
||||||
|
|
||||||
public RxtxChannelConfig(RxtxChannel channel) {
|
|
||||||
super(channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<ChannelOption<?>, Object> getOptions() {
|
|
||||||
return getOptions(super.getOptions(), BAUD_RATE, DTR, RTS, STOP_BITS, DATA_BITS, PARITY_BIT);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public <T> T getOption(ChannelOption<T> option) {
|
|
||||||
if (option == BAUD_RATE) {
|
|
||||||
return (T) Integer.valueOf(getBaudrate());
|
|
||||||
}
|
|
||||||
if (option == DTR) {
|
|
||||||
return (T) Boolean.valueOf(isDtr());
|
|
||||||
}
|
|
||||||
if (option == RTS) {
|
|
||||||
return (T) Boolean.valueOf(isRts());
|
|
||||||
}
|
|
||||||
if (option == STOP_BITS) {
|
|
||||||
return (T) getStopbits();
|
|
||||||
}
|
|
||||||
if (option == DATA_BITS) {
|
|
||||||
return (T) getDatabits();
|
|
||||||
}
|
|
||||||
if (option == PARITY_BIT) {
|
|
||||||
return (T) getParitybit();
|
|
||||||
}
|
|
||||||
return super.getOption(option);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> boolean setOption(ChannelOption<T> option, T value) {
|
|
||||||
validate(option, value);
|
|
||||||
|
|
||||||
if (option == BAUD_RATE) {
|
|
||||||
setBaudrate((Integer) value);
|
|
||||||
} else if (option == DTR) {
|
|
||||||
setDtr((Boolean) value);
|
|
||||||
} else if (option == RTS) {
|
|
||||||
setRts((Boolean) value);
|
|
||||||
} else if (option == STOP_BITS) {
|
|
||||||
setStopbits((Stopbits) value);
|
|
||||||
} else if (option == DATA_BITS) {
|
|
||||||
setDatabits((Databits) value);
|
|
||||||
} else if (option == PARITY_BIT) {
|
|
||||||
setParitybit((Paritybit) value);
|
|
||||||
} else {
|
|
||||||
return super.setOption(option, value);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the baud rate (ie. bits per second) for communication with the serial device.
|
* Sets the baud rate (ie. bits per second) for communication with the serial device.
|
||||||
* The baud rate will include bits for framing (in the form of stop bits and parity),
|
* The baud rate will include bits for framing (in the form of stop bits and parity),
|
||||||
@ -237,9 +168,7 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
*
|
*
|
||||||
* @param baudrate The baud rate (in bits per second)
|
* @param baudrate The baud rate (in bits per second)
|
||||||
*/
|
*/
|
||||||
public void setBaudrate(final int baudrate) {
|
RxtxChannelConfig setBaudrate(int baudrate);
|
||||||
this.baudrate = baudrate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the number of stop bits to include at the end of every character to aid the
|
* Sets the number of stop bits to include at the end of every character to aid the
|
||||||
@ -247,9 +176,7 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
*
|
*
|
||||||
* @param stopbits The number of stop bits to use
|
* @param stopbits The number of stop bits to use
|
||||||
*/
|
*/
|
||||||
public void setStopbits(final Stopbits stopbits) {
|
RxtxChannelConfig setStopbits(Stopbits stopbits);
|
||||||
this.stopbits = stopbits;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the number of data bits to use to make up each character sent to the serial
|
* Sets the number of data bits to use to make up each character sent to the serial
|
||||||
@ -257,53 +184,39 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
*
|
*
|
||||||
* @param databits The number of data bits to use
|
* @param databits The number of data bits to use
|
||||||
*/
|
*/
|
||||||
public void setDatabits(final Databits databits) {
|
RxtxChannelConfig setDatabits(Databits databits);
|
||||||
this.databits = databits;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the type of parity bit to be used when communicating with the serial device.
|
* Sets the type of parity bit to be used when communicating with the serial device.
|
||||||
*
|
*
|
||||||
* @param paritybit The type of parity bit to be used
|
* @param paritybit The type of parity bit to be used
|
||||||
*/
|
*/
|
||||||
public void setParitybit(final Paritybit paritybit) {
|
RxtxChannelConfig setParitybit(Paritybit paritybit);
|
||||||
this.paritybit = paritybit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The configured baud rate, defaulting to 115200 if unset
|
* @return The configured baud rate, defaulting to 115200 if unset
|
||||||
*/
|
*/
|
||||||
public int getBaudrate() {
|
int getBaudrate();
|
||||||
return baudrate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The configured stop bits, defaulting to {@link Stopbits#STOPBITS_1} if unset
|
* @return The configured stop bits, defaulting to {@link Stopbits#STOPBITS_1} if unset
|
||||||
*/
|
*/
|
||||||
public Stopbits getStopbits() {
|
Stopbits getStopbits();
|
||||||
return stopbits;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The configured data bits, defaulting to {@link Databits#DATABITS_8} if unset
|
* @return The configured data bits, defaulting to {@link Databits#DATABITS_8} if unset
|
||||||
*/
|
*/
|
||||||
public Databits getDatabits() {
|
Databits getDatabits();
|
||||||
return databits;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The configured parity bit, defaulting to {@link Paritybit#NONE} if unset
|
* @return The configured parity bit, defaulting to {@link Paritybit#NONE} if unset
|
||||||
*/
|
*/
|
||||||
public Paritybit getParitybit() {
|
Paritybit getParitybit();
|
||||||
return paritybit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if the serial device should support the Data Terminal Ready signal
|
* @return true if the serial device should support the Data Terminal Ready signal
|
||||||
*/
|
*/
|
||||||
public boolean isDtr() {
|
boolean isDtr();
|
||||||
return dtr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets whether the serial device supports the Data Terminal Ready signal, used for
|
* Sets whether the serial device supports the Data Terminal Ready signal, used for
|
||||||
@ -311,16 +224,12 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
*
|
*
|
||||||
* @param dtr true if DTR is supported, false otherwise
|
* @param dtr true if DTR is supported, false otherwise
|
||||||
*/
|
*/
|
||||||
public void setDtr(final boolean dtr) {
|
RxtxChannelConfig setDtr(boolean dtr);
|
||||||
this.dtr = dtr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if the serial device should support the Ready to Send signal
|
* @return true if the serial device should support the Ready to Send signal
|
||||||
*/
|
*/
|
||||||
public boolean isRts() {
|
boolean isRts();
|
||||||
return rts;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets whether the serial device supports the Request To Send signal, used for flow
|
* Sets whether the serial device supports the Request To Send signal, used for flow
|
||||||
@ -328,7 +237,17 @@ public class RxtxChannelConfig extends DefaultChannelConfig {
|
|||||||
*
|
*
|
||||||
* @param rts true if RTS is supported, false otherwise
|
* @param rts true if RTS is supported, false otherwise
|
||||||
*/
|
*/
|
||||||
public void setRts(final boolean rts) {
|
RxtxChannelConfig setRts(boolean rts);
|
||||||
this.rts = rts;
|
|
||||||
}
|
@Override
|
||||||
|
RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
RxtxChannelConfig setWriteSpinCount(int writeSpinCount);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
RxtxChannelConfig setAllocator(ByteBufAllocator allocator);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
RxtxChannelConfig setAutoRead(boolean autoRead);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user