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    public SerialMessage(int l) {
018        super(l);
019        setResponseLength(0);  // only polls require a response
020        setBinary(true);
021        log.debug("secsi message generated");
022    }
023
024    /**
025     * This ctor interprets the String as the exact sequence to send,
026     * byte-for-byte.
027     * @param m message string.
028     * @param l response length.
029     */
030    public SerialMessage(String m, int l) {
031        super(m);
032        setResponseLength(l);
033        setBinary(true);
034    }
035
036    /**
037     * This ctor interprets the byte array as a sequence of characters to send.
038     *
039     * @param a Array of bytes to send
040     * @param l response length
041     */
042    public SerialMessage(byte[] a, int l) {
043        super(String.valueOf(a));
044        setResponseLength(l);
045        setBinary(true);
046    }
047
048    int responseLength = -1;  // -1 is an invalid value, indicating it hasn't been set
049
050    public void setResponseLength(int l) {
051        responseLength = l;
052    }
053
054    public int getResponseLength() {
055        return responseLength;
056    }
057
058    // static methods to recognize a message
059    public boolean isPoll() {
060        return getElement(1) == 48;
061    }
062
063    public boolean isXmt() {
064        return getElement(1) == 17;
065    }
066
067    public int getAddr() {
068        return getElement(0);
069    }
070
071    // static methods to return a formatted message
072    static public SerialMessage getPoll(int addr) {
073        // eventually this will have to include logic for reading
074        // various bytes on the card, but our supported
075        // cards don't require that yet
076        SerialMessage m = new SerialMessage(1);
077        m.setResponseLength(2);
078        m.setElement(0, addr);
079        m.setTimeout(SHORT_TIMEOUT);    // minumum reasonable timeout
080        log.debug("poll message generated");
081        return m;
082    }
083
084    private final static Logger log = LoggerFactory.getLogger(SerialMessage.class);
085
086}