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        setComPortTimeouts(currentSerialPort, Blocking.READ_SEMI_BLOCKING, 100);
051
052        // report status
053        reportPortStatus(log, portName);
054
055        opened = true;
056
057        return null; // indicates OK return
058    }
059
060    /**
061     * set up all of the other objects to operate with a MS100 connected to this
062     * port
063     */
064    @Override
065    public void configure() {
066
067        setCommandStationType(getOptionState(option2Name));
068        setTurnoutHandling(getOptionState(option3Name));
069        // connect to a packetizing traffic controller
070        LnPacketizer packets = new LnPacketizer(this.getSystemConnectionMemo());
071        packets.connectPort(this);
072
073        // create memo
074        this.getSystemConnectionMemo().setLnTrafficController(packets);
075        // do the common manager config
076        this.getSystemConnectionMemo().configureCommandStation(commandStationType,
077                mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect);
078        this.getSystemConnectionMemo().configureManagers();
079
080        // start operation
081        packets.startThreads();
082    }
083
084    @Override
085    public boolean status() {
086        return opened;
087    }
088
089    /**
090     * {@inheritDoc}
091     *
092     * Just a message saying it's fixed
093     */
094    @Override
095    public String[] validBaudRates() {
096        return new String[]{"fixed at 16,600 baud"};
097    }
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public int[] validBaudNumbers() {
104        return new int[]{16600};
105    }
106
107    @Override
108    public int defaultBaudIndex() {
109        return 0;
110    }
111
112    /**
113     * Set the second port option. Only to be used after construction, but
114     * before the openPort call
115     */
116    @Override
117    public void configureOption2(String value) {
118        super.configureOption2(value);
119        log.debug("configureOption2: {}", value);
120        setCommandStationType(value);
121    }
122
123    // private control members
124    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MS100Adapter.class);
125
126}