001package jmri.jmrix.lenz.ztc640;
002
003import jmri.jmrix.lenz.XNetPacketizer;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/**
008 * This is an extention of the XNetPacketizer to handle the device specific
009 * requirements of the ZTC640.
010 * <p>
011 * In particular, ZTC640XNetPacketizer adds functions to add and remove the 0xFF
012 * bytes that appear prior to some messages.
013 *
014 * @author Paul Bender Copyright (C) 2006
015 *
016 */
017public class ZTC640XNetPacketizer extends XNetPacketizer {
018
019    public ZTC640XNetPacketizer(jmri.jmrix.lenz.LenzCommandStation pCommandStation) {
020        super(pCommandStation);
021        log.debug("Loading ZTC640 Extention to XNetPacketizer");
022    }
023
024    /**
025     * Get characters from the input source, and file a message.
026     * <p>
027     * Returns only when the message is complete.
028     * <p>
029     * Only used in the Receive thread.
030     *
031     * @param msg     message to fill
032     * @param istream character source.
033     * @throws java.io.IOException when presented by the input source.
034     */
035    @Override
036    protected void loadChars(jmri.jmrix.AbstractMRReply msg, java.io.DataInputStream istream) throws java.io.IOException {
037        int i;
038        log.debug("loading characters from port");
039        for (i = 0; i < msg.maxSize(); i++) {
040            byte char1 = readByteProtected(istream);
041            // This is a test for the ZTC640 device
042            while ((i == 0) && ((char1 & 0xF0) == 0xF0)) {
043                if ((char1 & 0xFF) != 0xF0 && (char1 & 0xFF) != 0xF2) {
044                    if(log.isDebugEnabled()) {
045                        log.debug("Filtering 0x{} from stream", Integer.toHexString(char1 & 0xFF));
046                    }
047                    //  toss this byte and read the next one
048                    char1 = readByteProtected(istream);
049                }
050            }
051            msg.setElement(i, char1 & 0xFF);
052            if (endOfMessage(msg)) {
053                break;
054            }
055        }
056        log.debug("Accepted Message: {}",msg);
057    }
058
059    private static final Logger log = LoggerFactory.getLogger(ZTC640XNetPacketizer.class);
060}
061
062