001package jmri.jmrix.powerline;
002
003import jmri.util.StringUtil;
004
005/**
006 * Contains the data payload of a serial packet.
007 * <p>
008 * The transmission protocol can come in one of several forms:
009 * <ul>
010 * <li>If the interlocked parameter is false (default), the packet is just sent.
011 * If the response length is not zero, a reply of that length is expected.
012 * <li>If the interlocked parameter is true, the transmission will require a CRC
013 * interlock, which will be automatically added. (Design note: this is done to
014 * make sure that the messages remain atomic)
015 * </ul>
016 *
017 * @author Bob Jacobsen Copyright (C) 2001,2003, 2006, 2007, 2008 Converted to
018 * multiple connection
019 * @author kcameron Copyright (C) 2011
020 */
021abstract public class SerialMessage extends jmri.jmrix.AbstractMRMessage {
022    // is this logically an abstract class?
023
024    /**
025     * Suppress the default ctor, as the length must always be specified
026     */
027    protected SerialMessage() {
028    }
029
030    public SerialMessage(int l) {
031        super(l);
032        setResponseLength(0);  // only polls require a response
033        setBinary(true);
034        setTimeout(5000);
035    }
036
037    /**
038     * This ctor interprets the String as the exact sequence to send,
039     * byte-for-byte.
040     * @param m sequence to send
041     * @param l expected reply length
042     *
043     */
044    public SerialMessage(String m, int l) {
045        super(m);
046        setResponseLength(l);
047        setBinary(true);
048        setTimeout(5000);
049    }
050
051    boolean interlocked = false;
052
053    public void setInterlocked(boolean v) {
054        interlocked = v;
055    }
056
057    public boolean getInterlocked() {
058        return interlocked;
059    }
060
061    /**
062     * This ctor interprets the byte array as a sequence of characters to send.
063     * @deprecated 5.13.5, unused, requires further development.
064     * @param a Array of bytes to send
065     * @param l expected reply length
066     */
067    @Deprecated( since="5.13.5", forRemoval=true)
068    public SerialMessage(byte[] a, int l) {
069        // super(String.valueOf(a)); // Spotbug toString on array
070        // requires further development to produce correct values for hardware type.
071        super(StringUtil.hexStringFromBytes(a).replaceAll("\\s", ""));
072        setResponseLength(l);
073        setBinary(true);
074        setTimeout(5000);
075    }
076
077    int responseLength = -1;  // -1 is an invalid value, indicating it hasn't been set
078
079    public void setResponseLength(int l) {
080        responseLength = l;
081    }
082
083    public int getResponseLength() {
084        return responseLength;
085    }
086
087    // static methods to recognize a message
088    public boolean isPoll() {
089        return getElement(1) == 48;
090    }
091
092    public boolean isXmt() {
093        return getElement(1) == 17;
094    }
095
096    public int getAddr() {
097        return getElement(0);
098    }
099
100    // static methods to return a formatted message
101    static public SerialMessage getPoll(int addr) {
102        // Powerline implementation does not currently poll
103        return null;
104    }
105}
106
107