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