001package jmri.jmrix.loconet.alm;
002
003import jmri.jmrix.loconet.LnConstants;
004import jmri.jmrix.loconet.LocoNetMessage;
005
006/**
007 * ALM Message Helper.
008 *
009 * @author Bob Milhaupt    Copyright (C) 2022
010 */
011public class Alm {
012    private Alm () {
013        throw new IllegalStateException("Utility class"); // NOI18N
014    }
015
016    private static final LocoNetMessage almcapq = new LocoNetMessage(new int[] {
017        LnConstants.OPC_IMM_PACKET_2, 16, 1, 0,0,0,0,0,
018        0,0,0,0,0,0,0,0});
019
020    private static final int[] capqmask = {255, 255, 255, 255, 255, 255, 255, 255,
021    255, 255, 255,255, 255, 255, 0, 0};
022
023    private static final LocoNetMessage capCsRr = new LocoNetMessage(new int[] {
024        0xE6, 0x10, 0x01, 0x00, 0x40, 0x02, 0x03, 0x02,
025        0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
026    });
027
028    private static final int[] csCapabilitiesMasks = {
029        0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x7d, 0, 0x7f,
030        0x67, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x00 };
031
032    private static final LocoNetMessage drcDs74 = new LocoNetMessage(new int[] {
033        0xE6, 0x10, 0x02, 0x00, 0x10, 0x00, 0x00, 0x02,
034        0x08, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
035    });
036
037    private static final int[] ds74CapabilitiesMasks = {
038        0xff, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
039        0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
040
041    private static final LocoNetMessage drcDs78v = new LocoNetMessage(new int[] {
042        0xE6, 0x10, 0x02, 0x00, 0x20, 0x00, 0x00, 0x02,
043        0x08, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
044    });
045
046    private static final LocoNetMessage drcSe74 = new LocoNetMessage(new int[] {
047        0xE6, 0x10, 0x02, 0x00, 0x10, 0x00, 0x00, 0x02,
048        0x08, 0x46, 0x00, 0x46, 0x08, 0x7C, 0x01, 0x00
049    });
050
051    private static final LocoNetMessage drcPm74 = new LocoNetMessage(new int[] {
052        0xE6, 0x10, 0x02, 0x00, 0x01, 0x00, 0x00, 0x02,
053        0x04, 0x4A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
054    });
055
056    private static final LocoNetMessage almchga = new LocoNetMessage (new int[] {
057        0xEE, 0x10, 2, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
058    private static final int[] almchgam = {
059        255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0};
060
061    private static final LocoNetMessage almgetr = new LocoNetMessage (new int[] {
062        0xEE, 0x10, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
063    private static final int[] almgetrm = {
064        255, 255, 255, 255, 0, 0x7E, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0};
065
066    public static boolean isCsCapsRpt(LocoNetMessage l) {
067        return l.equals(capCsRr, csCapabilitiesMasks);
068    }
069    
070    public static boolean isDs74CapsRpt(LocoNetMessage l) {
071        return l.equals(drcDs74, ds74CapabilitiesMasks);
072    }
073
074    public static boolean isDs78vCapsRpt(LocoNetMessage l) {
075        return l.equals(drcDs78v, ds74CapabilitiesMasks);
076    }
077
078    public static boolean isSe74CapsRpt(LocoNetMessage l) {
079        return l.equals(drcSe74, ds74CapabilitiesMasks);
080    }
081
082    public static boolean isPm74CapsRpt(LocoNetMessage l) {
083        return l.equals(drcPm74, ds74CapabilitiesMasks);
084    }
085
086    public static boolean isDevBAW(LocoNetMessage l) {
087        return (getAlmMsgType(l) == AlmMsgTypes.ALM_BAW);
088    }
089
090    public static boolean isDs7xRQ(LocoNetMessage l) {
091        return getAlmMsgType(l) == AlmMsgTypes.ALM_RDQ;
092    }
093
094    static AlmMsgTypes getAlmMsgType(LocoNetMessage m) {
095        // Verify message basics
096        if (((m.getOpCode() != LnConstants.OPC_IMM_PACKET_2)
097                && (m.getOpCode() != LnConstants.OPC_ALM_READ))
098                || (m.getElement(1) != 0x10) || (m.getNumDataElements()!=16)) {
099            return AlmMsgTypes.NOT_ALM_MSG;
100        }
101
102        // check routes capabilities query
103        if (m.equals(almcapq)) {
104            return AlmMsgTypes.ALM_ROUTCAPQ;
105        }
106        if (m.equals(almcapq, capqmask)) {
107            return AlmMsgTypes.ALM_ROUTCAPREP;
108        }
109        if (m.equals(almchga, almchgam)) {
110            return AlmMsgTypes.ALM_BAW;
111        }
112        if (m.equals(almgetr, almgetrm)) {
113            return AlmMsgTypes.ALM_RDQ;
114        }
115        if (m.equals(capCsRr, csCapabilitiesMasks)) {
116            return AlmMsgTypes.ALM_ROUTECSCAPREP;
117        }
118        return AlmMsgTypes.NOT_ALM_MSG;
119    }
120//    private final static Logger log = LoggerFactory.getLogger(Alm.class);
121
122}