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