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