001package jmri.jmrix.powerline.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.powerline.SerialPortController;
005import jmri.jmrix.powerline.SerialSystemConnectionMemo;
006import jmri.jmrix.powerline.SerialTrafficController;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Provide access to Powerline devices via a serial com port.
012 * Derived from the Oaktree code.
013 *
014 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2008
015 * @author Ken Cameron, (C) 2009, sensors from poll replies Converted to multiple connection
016 * @author kcameron Copyright (C) 2011
017 */
018public class SerialDriverAdapter extends SerialPortController {
019
020    public SerialDriverAdapter() {
021        super(new SerialSystemConnectionMemo());
022        option1Name = "Adapter"; // NOI18N
023        options.put(option1Name, new Option("Adapter", stdOption1Values));
024        this.manufacturerName = jmri.jmrix.powerline.SerialConnectionTypeList.POWERLINE;
025    }
026
027    @Override
028    public String openPort(String portName, String appName) {
029
030        // get and open the primary port
031        currentSerialPort = activatePort(portName, log);
032        if (currentSerialPort == null) {
033            log.error("failed to connect Powerline to {}", portName);
034            return Bundle.getMessage("SerialPortNotFound", portName);
035        }
036        log.info("Connecting Powerline to {} {}", portName, currentSerialPort);
037        
038        // try to set it for communication via SerialDriver
039        // find the baud rate value, configure comm options
040        // find the baud rate value, configure comm options
041        int baud = currentBaudNumber(mBaudRate);
042
043        // check for specific port type
044        String opt1 = getOptionState(option1Name);
045        if (opt1.equals("CM11")) {
046            // leave as 4800 baud
047        } else if (opt1.equals("CP290")) {
048            // set to 600 baud
049            baud = 600;
050        } else if (opt1.equals("Insteon 2412S")) {
051            // set to 19200 baud
052            baud = 19200;
053        }
054        setBaudRate(currentSerialPort, baud);
055        configureLeads(currentSerialPort, true, true);
056        setFlowControl(currentSerialPort, FlowControl.NONE);
057
058        // report status
059        reportPortStatus(log, portName);
060
061        opened = true;
062
063        return null; // indicates OK return
064    }
065
066    /**
067     * Can the port accept additional characters? Yes, always
068     * @return boolean of xmit port status
069     */
070    public boolean okToSend() {
071        return true;
072    }
073
074    /**
075     * Set up all of the other objects to operate connected to this port.
076     */
077    @Override
078    public void configure() {
079        SerialTrafficController tc = null;
080        // set up the system connection first
081        String opt1 = getOptionState(option1Name);
082        if (opt1.equals("CM11")) {
083            // create a CM11 port controller
084            this.setSystemConnectionMemo(new jmri.jmrix.powerline.cm11.SpecificSystemConnectionMemo());
085            tc = new jmri.jmrix.powerline.cm11.SpecificTrafficController(this.getSystemConnectionMemo());
086        } else if (opt1.equals("CP290")) {
087            // create a CP290 port controller
088            this.setSystemConnectionMemo(new jmri.jmrix.powerline.cp290.SpecificSystemConnectionMemo());
089            tc = new jmri.jmrix.powerline.cp290.SpecificTrafficController(this.getSystemConnectionMemo());
090        } else if (opt1.equals("Insteon 2412S")) {
091            // create an Insteon 2412s port controller
092            this.setSystemConnectionMemo(new jmri.jmrix.powerline.insteon2412s.SpecificSystemConnectionMemo());
093            tc = new jmri.jmrix.powerline.insteon2412s.SpecificTrafficController(this.getSystemConnectionMemo());
094        } else {
095            // no connection at all - warn
096            log.warn("protocol option {} defaults to CM11", opt1);
097            // create a CM11 port controller
098            this.setSystemConnectionMemo(new jmri.jmrix.powerline.cm11.SpecificSystemConnectionMemo());
099            tc = new jmri.jmrix.powerline.cm11.SpecificTrafficController(this.getSystemConnectionMemo());
100        }
101
102        // connect to the traffic controller
103        this.getSystemConnectionMemo().setTrafficController(tc);
104        tc.setAdapterMemo(this.getSystemConnectionMemo());
105        this.getSystemConnectionMemo().configureManagers();
106        tc.connectPort(this);
107        // Configure the form of serial address validation for this connection
108        this.getSystemConnectionMemo().setSerialAddress(new jmri.jmrix.powerline.SerialAddress(this.getSystemConnectionMemo()));
109    }
110
111    @Override
112    public boolean status() {
113        return opened;
114    }
115
116    String[] stdOption1Values = new String[]{"CM11", "CP290", "Insteon 2412S"}; // NOI18N
117
118    /**
119     * {@inheritDoc}
120     */
121    @Override
122    public String[] validBaudRates() {
123        return Arrays.copyOf(validSpeeds, validSpeeds.length);
124    }
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public int[] validBaudNumbers() {
131        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
132    }
133
134    protected String[] validSpeeds = new String[]{Bundle.getMessage("BaudAutomatic")};
135    protected int[] validSpeedValues = new int[]{4800};
136
137    @Override
138    public int defaultBaudIndex() {
139        return 0;
140    }
141
142    // private control members
143
144    private final static Logger log = LoggerFactory.getLogger(SerialDriverAdapter.class);
145
146}