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