001package jmri.jmrix.lenz.li101;
002
003import java.util.Arrays;
004import jmri.jmrix.lenz.LenzCommandStation;
005import jmri.jmrix.lenz.XNetInitializationManager;
006import jmri.jmrix.lenz.XNetPacketizer;
007import jmri.jmrix.lenz.XNetSerialPortController;
008import jmri.jmrix.lenz.XNetTrafficController;
009
010/**
011 * Provide access to XpressNet via a LI101 on an attached serial com port.
012 * Normally controlled by the lenz.li101.LI101Frame class.
013 *
014 * @author Bob Jacobsen Copyright (C) 2002
015 * @author Paul Bender, Copyright (C) 2003-2010
016 */
017public class LI101Adapter extends XNetSerialPortController {
018
019    public LI101Adapter() {
020        super();
021        option1Name = "FlowControl"; // NOI18N
022        options.put(option1Name, new Option(Bundle.getMessage("XconnectionUsesLabel", Bundle.getMessage("IFTypeLI101")), validOption1));
023        this.manufacturerName = jmri.jmrix.lenz.LenzConnectionTypeList.LENZ;
024    }
025
026    @Override
027    public String openPort(String portName, String appName) {
028        // get and open the primary port
029        currentSerialPort = activatePort(portName, log);
030        if (currentSerialPort == null) {
031            log.error("failed to connect LI101 to {}", portName);
032            return Bundle.getMessage("SerialPortNotFound", portName);
033        }
034        log.info("Connecting LI101 to {} {}", portName, currentSerialPort);
035        
036        // try to set it for communication via SerialDriver
037        // find the baud rate value, configure comm options
038        int baud = currentBaudNumber(mBaudRate);
039        setBaudRate(currentSerialPort, baud);
040        configureLeads(currentSerialPort, true, true);
041        FlowControl flow = FlowControl.RTSCTS; // default, but also default for getOptionState(option1Name)
042        if (!getOptionState(option1Name).equals(validOption1[0])) {
043            flow = FlowControl.NONE;
044        }
045        setFlowControl(currentSerialPort, flow);
046
047        // report status
048        reportPortStatus(log, portName);
049
050        opened = true;
051
052        return null; // indicates OK return
053    }
054
055    /**
056     * Set up all of the other objects to operate with a LI101 connected to this
057     * port.
058     */
059    @Override
060    public void configure() {
061        // connect to a packetizing traffic controller
062        XNetTrafficController packets = new XNetPacketizer(new LenzCommandStation());
063        packets.connectPort(this);
064
065        // start operation
066        // packets.startThreads();
067        this.getSystemConnectionMemo().setXNetTrafficController(packets);
068
069        new XNetInitializationManager()
070                .memo(this.getSystemConnectionMemo())
071                .setDefaults()
072                .versionCheck()
073                .setTimeout(30000)
074                .init();
075    }
076
077    // Base class methods for the XNetSerialPortController interface
078
079    @Override
080    public boolean status() {
081        return opened;
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public String[] validBaudRates() {
089        return Arrays.copyOf(validSpeeds, validSpeeds.length);
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public int[] validBaudNumbers() {
097        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
098    }
099
100    protected final String[] validSpeeds = new String[]{Bundle.getMessage("LIBaud19200"), Bundle.getMessage("Baud38400"),
101            Bundle.getMessage("Baud57600"), Bundle.getMessage("Baud115200")};
102    protected final int[] validSpeedValues = new int[]{19200, 38400, 57600, 115200};
103
104    @Override
105    public int defaultBaudIndex() {
106        return 0;
107    }
108
109    // meanings are assigned to these above, so make sure the order is consistent
110    protected final String[] validOption1 = new String[]{Bundle.getMessage("FlowOptionHwRecomm"), Bundle.getMessage("FlowOptionNo")};
111
112    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LI101Adapter.class);
113
114}