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