001package jmri.jmrix.loconet.ms100;
002
003import java.util.Vector;
004
005import jmri.jmrix.loconet.LnPacketizer;
006import jmri.jmrix.loconet.LnPortController;
007import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
008
009/**
010 * Provide access to LocoNet via a MS100 attached to a serial com port.
011 * Normally controlled by the jmri.jmrix.loconet.ms100.ConnectionConfig class.
012 * <p>
013 * By default, this attempts to use 16600 baud. If that fails, it falls back to
014 * 16457 baud. Neither the baud rate configuration nor the "option 1" option are
015 * used.
016 *
017 * @author Bob Jacobsen Copyright (C) 2001
018 */
019public class MS100Adapter extends LnPortController {
020
021    public MS100Adapter() {
022        super(new LocoNetSystemConnectionMemo());
023        option2Name = "CommandStation"; // NOI18N
024        option3Name = "TurnoutHandle"; // NOI18N
025        options.put(option2Name, new Option(Bundle.getMessage("CommandStationTypeLabel"), commandStationNames, false));
026        options.put(option3Name, new Option(Bundle.getMessage("TurnoutHandling"),
027                new String[]{Bundle.getMessage("HandleNormal"), Bundle.getMessage("HandleSpread"), Bundle.getMessage("HandleOneOnly"), Bundle.getMessage("HandleBoth")})); // I18N
028
029    }
030
031    Vector<String> portNameVector = null;
032
033    @Override
034    public String openPort(String portName, String appName) {
035
036        // get and open the primary port
037        currentSerialPort = activatePort(portName, log);
038        if (currentSerialPort == null) {
039            log.error("failed to connect MS100 to {}", portName);
040            return Bundle.getMessage("SerialPortNotFound", portName);
041        }
042        log.info("Connecting MS100 via {} {}", portName, currentSerialPort);
043        
044        // try to set it for communication via SerialDriver
045        // fixed baud rate
046        setBaudRate(currentSerialPort, 16600);
047        configureLeads(currentSerialPort, true, false);  // for MS100 power
048        setFlowControl(currentSerialPort, FlowControl.NONE);
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 MS100 connected to this
060     * port
061     */
062    @Override
063    public void configure() {
064
065        setCommandStationType(getOptionState(option2Name));
066        setTurnoutHandling(getOptionState(option3Name));
067        // connect to a packetizing traffic controller
068        LnPacketizer packets = new LnPacketizer(this.getSystemConnectionMemo());
069        packets.connectPort(this);
070
071        // create memo
072        this.getSystemConnectionMemo().setLnTrafficController(packets);
073        // do the common manager config
074        this.getSystemConnectionMemo().configureCommandStation(commandStationType,
075                mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect);
076        this.getSystemConnectionMemo().configureManagers();
077
078        // start operation
079        packets.startThreads();
080    }
081
082    @Override
083    public boolean status() {
084        return opened;
085    }
086
087    /**
088     * {@inheritDoc}
089     *
090     * Just a message saying it's fixed
091     */
092    @Override
093    public String[] validBaudRates() {
094        return new String[]{"fixed at 16,600 baud"};
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public int[] validBaudNumbers() {
102        return new int[]{16600};
103    }
104
105    @Override
106    public int defaultBaudIndex() {
107        return 0;
108    }
109
110    /**
111     * Set the second port option. Only to be used after construction, but
112     * before the openPort call
113     */
114    @Override
115    public void configureOption2(String value) {
116        super.configureOption2(value);
117        log.debug("configureOption2: {}", value);
118        setCommandStationType(value);
119    }
120
121    // private control members
122    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MS100Adapter.class);
123
124}