001package jmri.jmrix.sprog.update;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Class to hold SPROG type and firmware version.
008 *
009 * @author Andrew Crosland Copyright (C) 2012
010 */
011public class SprogVersion {
012
013    public SprogType sprogType = null;
014    public String sprogVersion = "";
015
016    /**
017     * Construct a new SPROG version object from a SPROG type
018     *
019     * @param t SprogType the type of SPROG
020     */
021    public SprogVersion(SprogType t) {
022        if (log.isDebugEnabled()) {
023            log.debug("SprogVersion(SprogType) ctor: {}", t.toString());
024        }
025        sprogType = t;
026        sprogVersion = "";
027    }
028
029    /**
030     * Construct a new SPROG version object from a SPROG type and version.
031     *
032     * @param t SprogType the type of SPROG
033     * @param s String version in "major.minor" format, e.g. "3.1"
034     */
035    public SprogVersion(SprogType t, String s) {
036        if (log.isDebugEnabled()) {
037            log.debug("SprogVersion(SprogType, String) ctor: {}v{}", t.toString(), s);
038        }
039        if (log.isDebugEnabled()) {
040            log.debug("sprogType: {}", t.sprogType);
041        }
042        sprogType = t;
043        sprogVersion = s;
044    }
045
046    /**
047     * Return major version number for a known SPROG.
048     *
049     * @return major version
050     */
051    public int getMajorVersion() {
052        int major = 0;
053        if (sprogType.isSprog()) {
054            try {
055                major = Integer.parseInt(sprogVersion.substring(sprogVersion.indexOf(".")
056                        - 1, sprogVersion.indexOf(".")));
057            } catch (NumberFormatException e) {
058                log.error("Cannot parse SPROG major version number");
059            }
060        }
061        return major;
062    }
063
064    /**
065     * Return minor version number for a known SPROG.
066     *
067     * @return minor version
068     */
069    public int getMinorVersion() {
070        int minor = 0;
071        if (sprogType.isSprog()) {
072            try {
073                minor = Integer.parseInt(sprogVersion.substring(sprogVersion.indexOf(".")
074                        + 1, sprogVersion.indexOf(".") + 2));
075            } catch (NumberFormatException e) {
076                log.error("Cannot parse SPROG minor version number");
077            }
078        }
079        return minor;
080    }
081
082    /**
083     * Check if the SPROG has various extra features that were not present in
084     * the original firmware. This means later SPROG II versions or any type
085     * equal or higher than SPROG 3.
086     *
087     * @return boolean if the current SPROG has extra features
088     */
089    public boolean hasExtraFeatures() {
090        int major = this.getMajorVersion();
091        int minor = this.getMinorVersion();
092        if (log.isDebugEnabled()) {
093            log.debug("Major: {} Minor: {}", major, minor);
094        }
095        if (this.sprogType.isSprogII() && (((major == 1) && (minor >= 6))
096                || ((major == 2) && (minor >= 1))
097                || (major >= 3))
098                || ((this.sprogType.sprogType >= SprogType.SPROGIIv3)
099                    && (this.sprogType.sprogType < SprogType.NANO))) {
100            if (log.isDebugEnabled()) {
101                log.debug("This version has extra features");
102            }
103            return true;
104        }
105        if (log.isDebugEnabled()) {
106            log.debug("This version does not have extra features");
107        }
108        return false;
109    }
110
111    /**
112     * Check if the SPROG has blueline decoder mode.
113     *
114     * @return true if the SPROG has blueline decoder support
115     */
116    public boolean hasBlueLine() {
117        return this.hasExtraFeatures();
118    }
119
120    /**
121     * Check if the SPROG has an adjustable current limit.
122     *
123     * @return true if the SPROG has adjustable current limit
124     */
125    public boolean hasCurrentLimit() {
126        return this.hasExtraFeatures();
127    }
128
129    /**
130     * Check if the SPROG has an interlock for the bootloader.
131     *
132     * @return true if the SPROG has firmware interlock
133     */
134    public boolean hasFirmwareLock() {
135        return (this.hasExtraFeatures() || (this.sprogType.sprogType <= SprogType.NANO));
136    }
137
138    public boolean hasZTCMode() {
139        return (this.sprogType.sprogType < SprogType.NANO);
140    }
141
142    /**
143     * Check if the SPROG supports use of CV hints when reading. This means
144     * SPROG IIv4 or SPROG 3v4 version 4.5 or higher
145     *
146     * @return boolean if the current SPROG supports CV hints when reading
147     */
148    public boolean supportsCVHints() {
149        int major = this.getMajorVersion();
150        int minor = this.getMinorVersion();
151        if (log.isDebugEnabled()) {
152            log.debug("Major: {} Minor: {}", major, minor);
153        }
154        if ((this.sprogType.sprogType == SprogType.SPROGIIv4) && (minor >= 5)
155                || (this.sprogType.sprogType == SprogType.SPROG3) && (major == 4) && (minor >= 5)) {
156            if (log.isDebugEnabled()) {
157                log.debug("This version supports CV hints");
158            }
159            return true;
160        }
161        if (log.isDebugEnabled()) {
162            log.debug("This version does not support CV hints");
163        }
164        return false;
165    }
166
167    /**
168     *
169     * @return String representation of SPROG version
170     */
171    @Override
172    public String toString() {
173        return this.toString(this);
174    }
175
176    /**
177     * Custom toString with Version Number.
178     * @param s sprog version.
179     * @return String representation of SPROG version
180     */
181    public String toString(SprogVersion s) {
182        if (log.isDebugEnabled()) {
183            log.debug("sprogType: {}", s.sprogType.sprogType);
184        }
185        return (s.sprogType.toString() +" v"+ sprogVersion);
186    }
187
188    private final static Logger log = LoggerFactory.getLogger(SprogVersion.class);
189
190}