001package jmri.jmrix.dcc4pc;
002
003import jmri.jmrix.AbstractMRReply;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/**
008 * Dcc4PcReply.java
009 *
010 * Carries the reply to a Dcc4PcMessage
011 *
012 * @author Kevin Dickerson Copyright (C) 2012
013 * @author Bob Jacobsen Copyright (C) 2001
014 * 
015 */
016public class Dcc4PcReply extends AbstractMRReply {
017
018    static public final int maxSize = 2048;
019
020    // create a new one
021    public Dcc4PcReply() {
022        super();
023    }
024
025    public Dcc4PcReply(byte[] packet) {
026        this();
027        int i = 0; // counter of byte in output message
028        int j = 0; // counter of byte in input packet
029        setBinary(true);
030        // add each byte of the input message
031        for (j = 0; j < packet.length; j++) {
032            this.setElement(i, packet[i]);
033            i++;
034        }
035    }
036
037    // no need to do anything
038    @Override
039    protected int skipPrefix(int index) {
040        return index;
041    }
042
043    /**
044     * Create a new Dcc4PcReply as a deep copy of an existing Dcc4PcReply
045     *
046     * @param m the Dcc4PcReply to copy
047     */
048    public Dcc4PcReply(Dcc4PcReply m) {
049        this();
050        if (m == null) {
051            log.error("copy ctor of null message");
052            return;
053        }
054        _nDataChars = m._nDataChars;
055        if (m.isUnsolicited()) {
056            super.setUnsolicited();
057        }
058        for (int i = 0; i < _nDataChars; i++) {
059            _dataChars[i] = m._dataChars[i];
060        }
061    }
062
063    public Dcc4PcReply(String replyString) {
064        super(replyString);
065    }
066
067    boolean error = false;
068
069    public void setError(boolean boo) {
070        error = boo;
071    }
072
073    /**
074     * Is this reply indicating that a general error has occurred?
075     * @return true if error, else false.
076     */
077    public boolean isError() {
078        return error;
079    }
080
081    public static final int FAILED = 0x02;
082    public static final int INCOMPLETE = 0x01;
083    public static final int SUCCESS = 0x00;
084
085    boolean stripRun = false;
086
087    // Check and strip 
088    public void strip() {
089        //we only want to run the strip once
090        if (stripRun) {
091            return;
092        }
093        char tmp[] = new char[_nDataChars];
094        int j = 0;
095
096        // Check framing characters
097        if ((_dataChars[0] == FAILED) || (_dataChars[0] == INCOMPLETE) || (_dataChars[0] == SUCCESS)) {
098            for (int i = 1; i < _nDataChars; i++) {
099                tmp[j++] = (char) _dataChars[i];
100            }
101
102            // Copy back to original Dcc4PcReply
103            for (int i = 0; i < j; i++) {
104                _dataChars[i] = tmp[i];
105            }
106            _nDataChars = j;
107            stripRun = true;
108            return;
109        }
110    }
111
112    /**
113     * Returns a hex string representation of this Dcc4PcReply.
114     * @return hex string format of Reply using 0x format.
115     */
116    public String toHexString() {
117
118        StringBuffer buf = new StringBuffer();
119        buf.append("0x" + Integer.toHexString(0xFF & _dataChars[0]));
120        for (int i = 1; i < _nDataChars; i++) {
121            buf.append(", 0x" + Integer.toHexString(0xFF & _dataChars[i]));
122        }
123        return buf.toString();
124    }
125
126    /**
127     * Returns the index of String s in the reply
128     */
129    @Override
130    public int match(String s) {
131        // find a specific string in the reply
132        String rep = new String(_dataChars, 0, _nDataChars);
133        return rep.indexOf(s);
134    }
135
136    public int[] getDataAsArray() {
137        return _dataChars.clone();
138    }
139
140    /** {@inheritDoc} */
141    @Override
142    public String toString() {
143        StringBuilder buf = new StringBuilder();
144        buf.append("0x");
145        buf.append(Integer.toHexString(0xFF & _dataChars[0]));
146        for (int i = 1; i < _nDataChars; i++) {
147            buf.append(".0x");
148            buf.append(Integer.toHexString(0xff & _dataChars[i]));
149            //buf.append(", 0x" + Integer.toHexString(0xFF & _dataChars[i]));
150        }
151        return buf.toString();
152    }
153
154    public byte[] getFormattedReply() {
155        int len = this.getNumDataElements();
156        int cr = 0;
157
158        byte msg[] = new byte[len + cr];
159
160        for (int i = 0; i < len; i++) {
161            msg[i] = (byte) (0xFF & this.getElement(i));
162        }
163        return msg;
164    }
165
166    @Override
167    public int maxSize() {
168        return maxSize;
169    }
170    
171    Dcc4PcMessage origMsg;
172    
173    public Dcc4PcMessage getOriginalRequest(){
174        return origMsg;
175    }
176    
177    protected void setOriginalRequest(Dcc4PcMessage msg){
178        origMsg = msg;
179    }
180    
181    
182    public int getBoard() { 
183        if(origMsg!=null){
184            return origMsg.getBoard();
185        }
186        return -1; 
187    }
188    
189    public int getMessageType(){
190        if(origMsg!=null){
191            return origMsg.getMessageType();
192        }
193        return -1;
194    }
195    private final static Logger log = LoggerFactory.getLogger(Dcc4PcReply.class);
196
197}