001package jmri.jmrix.marklin.cdb.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.marklin.cdb.CdBPortController;
005import jmri.jmrix.marklin.cdb.CdBSystemConnectionMemo;
006import jmri.jmrix.marklin.MarklinTrafficController;
007
008/**
009 * Implements SerialPortAdapter for the Marklin CDB system.
010 * <p>
011 * This connects a CC-Schnitte command station via a serial usb port.
012 * <p>
013 * Based on work by Bob Jacobsen
014 *
015 * @author Ralf Lang Copyright (C) 2022
016 */
017public class SerialDriverAdapter extends CdBPortController {
018
019    public SerialDriverAdapter() {
020        super(new CdBSystemConnectionMemo());
021        setManufacturer(jmri.jmrix.marklin.cdb.CdBConnectionTypeList.CDB);
022    }
023
024    @Override
025    public String openPort(String portName, String appName) {
026
027        // get and open the primary port
028        currentSerialPort = activatePort(portName, log);
029        if (currentSerialPort == null) {
030            log.error("failed to connect Marklin CDB to {}", portName);
031            return Bundle.getMessage("SerialPortNotFound", portName);
032        }
033        log.info("Connecting Marklin CDB to {} {}", portName, currentSerialPort);
034        
035        // try to set it for communication via SerialDriver
036        // find the baud rate value, configure comm options
037        int baud = currentBaudNumber(mBaudRate);
038        setBaudRate(currentSerialPort, baud);
039        configureLeads(currentSerialPort, true, true);
040        setFlowControl(currentSerialPort, FlowControl.NONE);
041
042        // report status
043        reportPortStatus(log, portName);
044
045        opened = true;
046
047        return null; // indicates OK return
048    }
049
050    /**
051     * set up all of the other objects to operate with an NCE command station
052     * connected to this port
053     */
054    @Override
055    public void configure() {
056        MarklinTrafficController tc = new MarklinTrafficController();
057        this.getSystemConnectionMemo().setMarklinTrafficController(tc);
058        tc.setAdapterMemo(this.getSystemConnectionMemo());
059
060        tc.connectPort(this);
061
062        this.getSystemConnectionMemo().configureManagers();
063    }
064
065    // base class methods for the MarklinPortController interface
066
067    @Override
068    public boolean status() {
069        return opened;
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public String[] validBaudRates() {
077        return Arrays.copyOf(validSpeeds, validSpeeds.length);
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public int[] validBaudNumbers() {
085        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
086    }
087
088    private final String[] validSpeeds = new String[]{Bundle.getMessage("Baud500000")};
089    private final int[] validSpeedValues = new int[]{500000};
090
091    @Override
092    public int defaultBaudIndex() {
093        return 0;
094    }
095
096    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialDriverAdapter.class);
097
098}