001package jmri.jmrix.powerline.cm11;
002
003import jmri.jmrix.powerline.SerialReply;
004import jmri.jmrix.powerline.SerialTrafficController;
005import jmri.jmrix.powerline.X10Sequence;
006import jmri.util.StringUtil;
007
008/**
009 * Contains the data payload of a serial reply packet. Note that it's _only_ the
010 * payload.
011 *
012 * @author Bob Jacobsen Copyright (C) 2002, 2006, 2007, 2008 Converted to
013 * multiple connection
014 * @author kcameron Copyright (C) 2011
015 */
016public class SpecificReply extends SerialReply {
017
018    // create a new one
019    public SpecificReply(SerialTrafficController tc) {
020        super(tc);
021        setBinary(true);
022    }
023
024    public SpecificReply(String s, SerialTrafficController tc) {
025        super(tc, s);
026        setBinary(true);
027    }
028
029    public SpecificReply(SerialReply l, SerialTrafficController tc) {
030        super(tc, l);
031        setBinary(true);
032    }
033
034    @Override
035    public String toMonitorString() {
036        // check for valid length
037        StringBuilder sb = new StringBuilder();
038        if (getNumDataElements() == 1) {
039            int msg = getElement(0);
040            switch (msg & 0xFF) {
041                case Constants.POLL_REQ:
042                    sb.append("Data Available\n");
043                    break;
044                case Constants.TIME_REQ_CP11:
045                    sb.append("CP11 time request\n");
046                    break;
047                case Constants.TIME_REQ_CP10:
048                    sb.append("CP10 time request\n");
049                    break;
050                case Constants.FILTER_FAIL:
051                    sb.append("Input Filter Failed\n");
052                    break;
053                case Constants.READY_REQ:
054                    sb.append("Interface Ready\n");
055                    break;
056                default:
057                    sb.append("One byte, probably CRC\n");
058                    break;
059            }
060            return sb.toString();
061        } else if ((getNumDataElements() == 2) && ((getElement(1) & 0xFF) == Constants.READY_REQ)) {
062            sb.append("CRC 0x");
063            sb.append(jmri.util.StringUtil.twoHexFromInt(getElement(0)));
064            sb.append(" and Interface Ready\n");
065            return sb.toString();
066        } else if ((getElement(0) & 0xFF) == Constants.POLL_REQ) {
067            // must be received data
068            sb.append("Receive data, ");
069            sb.append(getElement(1) & 0xFF);
070            sb.append(" bytes; ");
071            int last = (getElement(1) & 0xFF) + 1;
072            int bits = (getElement(2) & 0xFF);
073            for (int i = 3; i <= last; i++) {
074                if (i != 3) {
075                    sb.append("; ");  // separate all but last command
076                }
077                if ((bits & 0x01) != 0) {
078                    sb.append(X10Sequence.formatCommandByte(getElement(i) & 0xFF));
079                } else {
080                    sb.append(X10Sequence.formatAddressByte(getElement(i) & 0xFF));
081                }
082                bits = bits >> 1;  // shift over before next byte
083            }
084            sb.append("\n");
085            return sb.toString();
086        } else if ((getElement(0) & 0xFF) == 0x5B) {
087            if (getNumDataElements() == 3) {
088                sb.append("EPROM Address Poll: ");
089                sb.append(StringUtil.twoHexFromInt(getElement(1) & 0xFF));
090                sb.append(":");
091                sb.append(StringUtil.twoHexFromInt(getElement(2) & 0xFF));
092                sb.append("\n");
093                return sb.toString();
094            } else {
095                sb.append("EPROM Address Poll, invalid length: ");
096                sb.append(getNumDataElements());
097                return sb.toString();
098            }
099        } else {
100            // don't know, just show
101            sb.append("Unknown reply of length ");
102            sb.append(getNumDataElements());
103            sb.append(" ");
104            sb.append(toString()).append("\n");
105            sb.append("\n");
106            return sb.toString();
107        }
108    }
109
110}
111
112