001package jmri.jmrix.nce;
002
003import jmri.jmrix.ConnectionStatus;
004import jmri.util.swing.JmriJOptionPane;
005
006/* 
007 * Checks to see if AIU broadcasts are enabled and warns user to 
008 * disable AIU broadcast for proper operation.  NCE command station
009 * battery-backed memory location 0xDC15 contains the control for
010 * AIU broadcasts, 0 = disabled, 1 = enabled.
011 *  
012 * @author Daniel Boudreau (C) 2007
013 * @author Ken Cameron Copyright (C) 2023
014 */
015public class NceAIUChecker implements NceListener {
016 
017    private static final int REPLY_LEN = 1;  // number of bytes read
018    private boolean EXPECT_REPLY = false;   // flag 
019
020    private NceTrafficController tc = null;
021
022    public NceAIUChecker(NceTrafficController t) {
023        super();
024        this.tc = t;
025    }
026
027    public NceMessage nceAiuPoll() {
028
029        if (tc.getCommandOptions() <= NceTrafficController.OPTION_1999) {
030            return null;
031        }
032
033        // If USB, just return
034        if (tc.getUsbSystem() != NceTrafficController.USB_SYSTEM_NONE) {
035            return null;
036        }
037
038        // read one byte from NCE memory to determine if AIU broadcasts are enabled
039        byte[] bl = NceBinaryCommand.accMemoryRead1(tc.csm.getAiuFlagAddr());
040        NceMessage m = NceMessage.createBinaryMessage(tc, bl, REPLY_LEN);
041        EXPECT_REPLY = true;
042        return m;
043
044    }
045
046    @Override
047    public void message(NceMessage m) {
048        if (log.isDebugEnabled()) {
049            log.debug("unexpected message");
050        }
051    }
052
053    @Override
054    public void reply(NceReply r) {
055        if (!EXPECT_REPLY && log.isDebugEnabled()) {
056            log.debug("Unexpected reply in AIU broadcast checker");
057            return;
058        }
059        EXPECT_REPLY = false;
060        if (r.getNumDataElements() == REPLY_LEN) {
061
062            // if broadcasts are enabled, put up warning
063            byte AIUstatus = (byte) r.getElement(0);
064            if (AIUstatus > 1) {
065                log.warn("AIU check broadcast return value is out of range");
066            }
067            if (AIUstatus == 1) {
068                log.warn("AIU broadcasts are enabled");
069                ConnectionStatus.instance().setConnectionState(
070                        tc.getUserName(),
071                        tc.getPortName(),
072                        ConnectionStatus.CONNECTION_DOWN);
073                JmriJOptionPane.showMessageDialog(null,
074                        "JMRI has detected that AIU broadcasts are enabled. \n"
075                        + "You must disable AIU broadcasts for proper operation of this program. \n"
076                        + "For more information, see Setup Command Station in your NCE System Reference Manual.",
077                        "Warning", JmriJOptionPane.INFORMATION_MESSAGE);
078
079            }
080
081        } else {
082            log.warn("wrong number of read bytes for revision check");
083        }
084    }
085
086    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(NceAIUChecker.class);
087
088}
089