001package jmri.jmrix.ieee802154.serialdriver;
002
003import java.util.Arrays;
004import jmri.jmrix.ieee802154.IEEE802154PortController;
005import jmri.jmrix.ieee802154.IEEE802154SystemConnectionMemo;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Provide access to IEEE802.15.4 devices via a serial com port.
011 * Derived from the Oaktree code.
012 *
013 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2008
014 * @author Ken Cameron, (C) 2009, sensors from poll replies Converted to
015 * multiple connection
016 * @author kcameron Copyright (C) 2011
017 * @author Paul Bender Copyright (C) 2013,2023
018 */
019public class SerialDriverAdapter extends IEEE802154PortController {
020
021    public SerialDriverAdapter() {
022        this(new SerialSystemConnectionMemo());
023    }
024
025    protected SerialDriverAdapter(IEEE802154SystemConnectionMemo connectionMemo) {
026        super(connectionMemo);
027        this.manufacturerName = jmri.jmrix.ieee802154.SerialConnectionTypeList.IEEE802154;
028    }
029
030    @Override
031    public String openPort(String portName, String appName) {
032        currentSerialPort = activatePort(portName,log);
033        // try to set it for serial
034        setSerialPort();
035
036        // report status?
037        reportPortStatus(log,portName);
038        opened = true;
039
040        return null; // normal operation
041    }
042
043    /**
044     * Can the port accept additional characters? Yes, always
045     * @return always true
046     */
047    public boolean okToSend() {
048        return true;
049    }
050
051    /**
052     * Set up all of the other objects to operate connected to this port.
053     */
054    @Override
055    public void configure() {
056        log.debug("configure() called.");
057        SerialTrafficController tc = new SerialTrafficController();
058
059        // connect to the traffic controller
060        this.getSystemConnectionMemo().setTrafficController(tc);
061        tc.setAdapterMemo(this.getSystemConnectionMemo());
062        this.getSystemConnectionMemo().configureManagers();
063        tc.connectPort(this);
064    }
065
066    @Override
067    public boolean status() {
068        return opened;
069    }
070
071    /**
072     * Local method to do specific port configuration.
073     */
074    protected void setSerialPort() {
075        // find the baud rate value, configure comm options
076        int baud = currentBaudNumber(mBaudRate);
077        setBaudRate(currentSerialPort,baud);
078
079        // find and configure flow control
080        configureLeads(currentSerialPort,true,true);
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public String[] validBaudRates() {
088        return Arrays.copyOf(validSpeeds, validSpeeds.length);
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public int[] validBaudNumbers() {
096        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
097    }
098
099    String[] stdOption1Values = new String[]{"CM11", "CP290", "Insteon 2412S"}; // NOI18N
100
101    public String[] validOption1() {
102        return Arrays.copyOf(stdOption1Values, stdOption1Values.length);
103    }
104
105    /**
106     * Get a String that says what Option 1 represents.
107     *
108     * @return fixed string 'Adapter'
109     */
110    public String option1Name() {
111        return "Adapter"; // NOI18N
112    }
113
114    private String[] validSpeeds = new String[]{Bundle.getMessage("BaudAutomatic")};
115    private int[] validSpeedValues = new int[]{9600};
116
117    @Override
118    public int defaultBaudIndex() {
119        return 0;
120    }
121
122    /**
123     * Get an array of valid values for "option 2"; used to display valid
124     * options. May not be null, but may have zero entries.
125     *
126     * @return empty string array
127     */
128    public String[] validOption2() {
129        return new String[]{""};
130    }
131
132    /**
133     * Get a String that says what Option 2 represents. May be an empty string,
134     * but will not be null.
135     *
136     * @return empty string
137     */
138    public String option2Name() {
139        return "";
140    }
141
142    private final static Logger log = LoggerFactory.getLogger(SerialDriverAdapter.class);
143
144}