001package jmri.jmrix.secsi;
002
003import jmri.util.StringUtil;
004
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Contains the data payload of a serial packet.
010 * <p>
011 * Note that <i>only</i> the payload, not the header or trailer, nor the padding
012 * DLE characters are included. These are added during transmission.
013 *
014 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2006, 2007, 2008
015 */
016public class SerialMessage extends jmri.jmrix.AbstractMRMessage {
017    // is this logically an abstract class?
018
019    public SerialMessage(int l) {
020        super(l);
021        setResponseLength(0);  // only polls require a response
022        setBinary(true);
023        log.debug("secsi message generated");
024    }
025
026    /**
027     * This ctor interprets the String as the exact sequence to send,
028     * byte-for-byte.
029     * @param m message string.
030     * @param l response length.
031     */
032    public SerialMessage(String m, int l) {
033        super(m);
034        setResponseLength(l);
035        setBinary(true);
036    }
037
038    /**
039     * This ctor interprets the byte array as a sequence of characters to send.
040     * @deprecated 5.13.5, unused, requires further development.
041     * @param a Array of bytes to send
042     * @param l response length
043     */
044    @Deprecated( since="5.13.5", forRemoval=true)
045    public SerialMessage(byte[] a, int l) {
046        // super(String.valueOf(a)); // Spotbug toString on array
047        // requires further development to produce correct values for hardware type.
048        super(StringUtil.hexStringFromBytes(a).replaceAll("\\s", ""));
049        setResponseLength(l);
050        setBinary(true);
051    }
052
053    int responseLength = -1;  // -1 is an invalid value, indicating it hasn't been set
054
055    public void setResponseLength(int l) {
056        responseLength = l;
057    }
058
059    public int getResponseLength() {
060        return responseLength;
061    }
062
063    // static methods to recognize a message
064    public boolean isPoll() {
065        return getElement(1) == 48;
066    }
067
068    public boolean isXmt() {
069        return getElement(1) == 17;
070    }
071
072    public int getAddr() {
073        return getElement(0);
074    }
075
076    // static methods to return a formatted message
077    static public SerialMessage getPoll(int addr) {
078        // eventually this will have to include logic for reading
079        // various bytes on the card, but our supported
080        // cards don't require that yet
081        SerialMessage m = new SerialMessage(1);
082        m.setResponseLength(2);
083        m.setElement(0, addr);
084        m.setTimeout(SHORT_TIMEOUT);    // minumum reasonable timeout
085        log.debug("poll message generated");
086        return m;
087    }
088
089    private final static Logger log = LoggerFactory.getLogger(SerialMessage.class);
090
091}