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