001package jmri.jmrix.rfid.protocol.parallax;
002
003import jmri.jmrix.AbstractMRReply;
004import jmri.jmrix.rfid.RfidProtocol;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Common routines to extract the Tag information and validate checksum for
010 * implementations that use the Parallax protocol.
011 * <hr>
012 * This file is part of JMRI.
013 * <p>
014 * JMRI is free software; you can redistribute it and/or modify it under the
015 * terms of version 2 of the GNU General Public License as published by the Free
016 * Software Foundation. See the "COPYING" file for a copy of this license.
017 * <p>
018 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
020 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
021 *
022 * @author Matthew Harris Copyright (C) 2014
023 * @since 3.9.2
024 */
025public class ParallaxRfidProtocol extends RfidProtocol {
026
027    public static final int SPECIFICMAXSIZE = 12;
028
029    public static final int getMaxSize() {
030        return SPECIFICMAXSIZE;
031    }
032
033    @Override
034    public String initString() {
035        // None required for Parallax
036        return "";
037    }
038
039    @Override
040    public String getTag(AbstractMRReply msg) {
041        StringBuilder sb = new StringBuilder(10);
042
043        for (int i = 1; i < 11; i++) {
044            sb.append((char) msg.getElement(i));
045        }
046
047        return sb.toString();
048    }
049
050    @Override
051    public String getCheckSum(AbstractMRReply msg) {
052        return "";
053    }
054
055    @Override
056    public boolean isValid(AbstractMRReply msg) {
057        return msg.getElement(0) == 0x0A
058                && msg.getElement(SPECIFICMAXSIZE - 1) == 0x0D;
059    }
060
061    @Override
062    public boolean endOfMessage(AbstractMRReply msg) {
063        if (msg.getNumDataElements() == SPECIFICMAXSIZE) {
064            if ((msg.getElement(0)) == 0x0A
065                    && (msg.getElement(SPECIFICMAXSIZE - 1)) == 0x0D) {
066                return true;
067            }
068            if (log.isDebugEnabled()) {
069                log.debug("Not a correctly formed message");
070            }
071            return true;
072        }
073        return false;
074    }
075
076    @Override
077    public String toMonitorString(AbstractMRReply msg) {
078        // check for valid message
079        if (isValid(msg)) {
080            StringBuilder sb = new StringBuilder();
081            sb.append("Reply from Parallax reader.");
082            sb.append(" Tag read ");
083            sb.append(getTag(msg));
084            return sb.toString();
085        } else {
086            return super.toMonitorString(msg);
087        }
088    }
089
090    private static final Logger log = LoggerFactory.getLogger(ParallaxRfidProtocol.class);
091
092}