public CommandStationPomMessage prepareTerminatingDataPacket(DecoderInformation.DecoderType decoderType) { LOGGER.info("Prepare terminating data packet for decoder type: {}", decoderType); // prepare the decoder type AddressTypeEnum addressType = AddressTypeEnum.LOCOMOTIVE_FORWARD; if (decoderType == DecoderType.accessoryDecoder) { addressType = AddressTypeEnum.ACCESSORY; } AddressData decoderAddress = new AddressData(ADDR_TERM_PACKET, addressType); CommandStationPom opCode = CommandStationPom.WR_BYTE; int cvNumber = 166; byte[] data = { 0 }; CommandStationPomMessage message = new CommandStationPomMessage(decoderAddress, opCode, cvNumber, data); return message; }
private static byte[] prepareData(AddressData decoderAddress, byte acknowledge) { ByteArrayOutputStream out = new ByteArrayOutputStream(); // write decoder address out.write(ByteUtils.getLowByte(decoderAddress.getAddress())); out.write(ByteUtils.getHighByte(decoderAddress.getAddress())); // data out.write(acknowledge); return out.toByteArray(); }
public AddressData getAddress() { byte[] data = getData(); AddressData addressData = null; switch (getOpCode()) { case RC_BIND_ACCEPTED_LOCO: case RC_BIND_ACCEPTED_ACCESSORY: addressData = AddressData.fromByteArray(data, 2 + 5); break; default: LOGGER.warn("The current opCode is not RC_BIND_ACCEPTED!"); break; } return addressData; }
@Override public boolean equals(Object obj) { if (obj instanceof AddressData) { AddressData other = (AddressData) obj; if (address == other.getAddress() && type.equals(other.getType())) { return true; } } return false; }
public FeedbackSpeedResponse(byte[] addr, int num, int decoderAddress, int speed) throws ProtocolException { this(addr, num, TYPE, ByteUtils .concat(AddressData.toByteArray(new AddressData(decoderAddress, AddressTypeEnum.LOCOMOTIVE_FORWARD)), ByteUtils.toWORD(speed))); }
private static byte[] prepareData(AddressData decoderAddress, byte acknowledge) { ByteArrayOutputStream out = new ByteArrayOutputStream(); // write decoder address decoderAddress.writeToStream(out); // data out.write(acknowledge); return out.toByteArray(); }
public void writeToStream(ByteArrayOutputStream out) { if (address > 0) { // write address out.write(ByteUtils.getLowByte(address)); byte highAddr = ByteUtils.getHighByte(address); switch (getType()) { case LOCOMOTIVE_BACKWARD: highAddr = (byte) (highAddr & 0x3F); highAddr = (byte) (highAddr | (1 << 7)); break; case ACCESSORY: highAddr = (byte) (highAddr & 0x3F); highAddr = (byte) (highAddr | (1 << 6)); break; case EXTENDED_ACCESSORY: highAddr = (byte) (highAddr | (1 << 6)); highAddr = (byte) (highAddr | (1 << 7)); break; default: highAddr = (byte) (highAddr & 0x3F); break; } out.write(highAddr); } else { out.write((byte) 0); out.write((byte) 0); } }
/** * Convert the address data into a byte array. * * @param addressData * the address */ public static byte[] toByteArray(AddressData addressData) { byte[] data = new byte[2]; byte lowByte = ByteUtils.getLowByte(addressData.getAddress()); data[0] = lowByte; byte highByte = ByteUtils .getLowByte((ByteUtils.getHighByte(addressData.getAddress()) | (addressData.getType().getType() << 6))); data[1] = highByte; return data; }
private static byte[] prepareData(AddressData decoderAddress, byte acknowledge) { ByteArrayOutputStream out = new ByteArrayOutputStream(); // write decoder address decoderAddress.writeToStream(out); // no ADDR_XL and ADDR_XH out.write((byte) 0); out.write((byte) 0); // no MID out.write((byte) 0); // data out.write(acknowledge); return out.toByteArray(); }
public AddressData getDecoderAddress() { int index = 0; byte lowByte = getData()[index++]; byte highByte = getData()[index++]; int address = ByteUtils.getWord(lowByte, highByte); AddressTypeEnum addressTypeEnum = AddressTypeEnum.LOCOMOTIVE_BACKWARD; AddressData addressData = new AddressData(address, addressTypeEnum); LOGGER.debug("Prepared address data: {}", addressData); return addressData; }
@Test public void prepareDecoderUpdatePomMessagesTest() throws URISyntaxException { DecoderPomUpdate decoderPomUpdate = new DecoderPomUpdate(); File firmwareFile = new File(DecoderPomUpdate.class.getResource("/firmware/LD-G-31-Plus_V2_2.hex").toURI().getPath()); List<String> firmwareContent = decoderPomUpdate.loadFirmwareFile(firmwareFile); Assert.assertNotNull(firmwareContent); DecoderInformation decoderInformation = new DecoderInformation("3E000031020200000305201500000000"); List<CommandStationPomMessage> messages = decoderPomUpdate.prepareDecoderUpdatePomMessages(firmwareContent, decoderInformation); Assert.assertNotNull(messages); CommandStationPomMessage message = messages.get(0); Assert.assertEquals(message.getDecoderAddress().getAddress(), 8705); Assert.assertEquals(message.getCvNumber(), 1); Assert.assertEquals(message.getCvValue(), 0x31); } }
/** * Add the address data to the provided byte array. * * @param addressData * the address * @param data * the byte array * @param offset * the offset to insert the address data */ public static void toByteArray(AddressData addressData, byte[] data, int offset) { byte lowByte = ByteUtils.getLowByte(addressData.getAddress()); data[offset] = lowByte; byte highByte = ByteUtils .getLowByte((ByteUtils.getHighByte(addressData.getAddress()) | (addressData.getType().getType() << 6))); data[offset + 1] = highByte; }
private static byte[] prepareDynamicStateData( int detectorNumber, AddressData addressData, byte dynNumber, byte dynValue) { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(ByteUtils.getLowByte(detectorNumber)); addressData.writeToStream(out); out.write(dynNumber); out.write(dynValue); return out.toByteArray(); }
/** * Create a RcPlusBindData instance from the provided byte array. * * @param data * the byte array * @param offset * the offset to start * @return the RcPlusBindData instance */ public static RcPlusBindData fromByteArray(byte[] data, int offset) { if (data.length < (offset + 7)) { LOGGER.warn("The size of the provided data does not meet the expected length."); throw new IllegalArgumentException( "The size of the provided data does not meet the expected length (=7). Provided length: " + data.length); } DecoderUniqueIdData uniqueIdData = DecoderUniqueIdData.fromByteArray(data, offset); AddressData address = AddressData.fromByteArray(data, offset + 5); RcPlusBindData answerData = new RcPlusBindData(uniqueIdData, address); LOGGER.trace("Return RcPlusBindData: {}", answerData); return answerData; }
public List<CommandStationPomMessage> prepareDecoderInfoPomMessages( DecoderInformation decoderInfo, int decoderAddress) { // prepare the decoder type AddressTypeEnum addressType = AddressTypeEnum.LOCOMOTIVE_FORWARD; if (decoderInfo.getDecoderType() == DecoderType.accessoryDecoder) { addressType = AddressTypeEnum.ACCESSORY; } AddressData address = new AddressData(decoderAddress, addressType); CommandStationPom opCode = CommandStationPom.WR_BYTE; List<CommandStationPomMessage> messages = new ArrayList<CommandStationPomMessage>(); for (int cvNumber = 800; cvNumber < 812; cvNumber++) { byte[] data = { decoderInfo.getCvValue(cvNumber) }; CommandStationPomMessage message = new CommandStationPomMessage(address, opCode, cvNumber, data); messages.add(message); } return messages; }
@Test public void prepareLine2Test() { String line2 = ":1000000080090020ED000000C1000000C1000000D8"; DecoderPomUpdate decoderPomUpdate = new DecoderPomUpdate(); FirmwarePacket packet = decoderPomUpdate.parseLine(line2); Assert.assertNotNull(packet); LOGGER.info("Current packet: {}", packet); int packetLen = ByteUtils.getInt(packet.getLen()); // 08 F2D0 00 CommandStationPomMessage message = decoderPomUpdate.prepareNextRowPacket(packet, DecoderType.locoDecoder); Assert.assertNotNull(message); LOGGER.info("Row packet, message: {}, formatted data: {}", message, ByteUtils.bytesToHex(message.getData())); Assert.assertEquals(message.getDecoderAddress().getAddress(), 8448); Assert.assertEquals(message.getCvNumber(), 0x300 + 1); Assert.assertEquals(message.getCvValue(), 0); for (int index = 0; index < (packetLen / 3 + 1); index++) { message = decoderPomUpdate.prepareNextDataPacket(packet, DecoderType.locoDecoder); Assert.assertNotNull(message); LOGGER.info("Data packet, message: {}, formatted data: {}", message, ByteUtils.bytesToHex(message.getData())); } // verify the last message Assert.assertNotNull(message); LOGGER.info("Data packet, message: {}, formatted data: {}", message, ByteUtils.bytesToHex(message.getData())); Assert.assertEquals(message.getDecoderAddress().getAddress(), 8192 + 0); Assert.assertEquals(message.getCvNumber(), 0x00 + 1 + 0x0100); Assert.assertEquals(message.getCvValue(), 0x00); }
private static byte[] prepareAddressData(int detectorNumber, List<AddressData> addresses) { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(ByteUtils.getLowByte(detectorNumber)); if (addresses.size() > 0) { for (AddressData addressData : addresses) { addressData.writeToStream(out); } } else { out.write((byte) 0); out.write((byte) 0); } return out.toByteArray(); }
/** * Create a RcPlusDecoderAnswerData instance from the provided byte array. * * @param data * the byte array * @param offset * the offset to start * @return the RcPlusDecoderAnswerData instance */ public static RcPlusFeedbackBindData fromByteArray(byte[] data, int offset) { // TODO change this to be 7 after the GBM returns the address if (data.length < (offset + 5)) { LOGGER.warn("The size of the provided data does not meet the expected length."); throw new IllegalArgumentException( "The size of the provided data does not meet the expected length (=5). Provided length: " + data.length + ", offset: " + offset); } DecoderUniqueIdData uniqueIdData = DecoderUniqueIdData.fromByteArray(data, offset); AddressData address = null; if (data.length > (offset + 5)) { address = AddressData.fromByteArray(data, offset + 5); } RcPlusFeedbackBindData answerData = new RcPlusFeedbackBindData(uniqueIdData, address); LOGGER.info("Return RcPlusBindAcceptedData: {}", answerData); return answerData; }
public AddressData getAddress() { byte[] data = getData(); int index = 1; byte lowByte = data[index++]; byte highByte = data[index]; int address = ByteUtils.getWord(lowByte, (byte) (highByte & 0x3F)); LOGGER.debug("Return the address of MSG_BM_DYN_STATE: {}", address); return new AddressData(address, AddressTypeEnum.valueOf((byte) ((highByte & 0xC0) >> 6))); }
@Test public void prepareSmallerLine2Test() { String line2 = ":08F2D000047E00003281000001"; DecoderPomUpdate decoderPomUpdate = new DecoderPomUpdate(); FirmwarePacket packet = decoderPomUpdate.parseLine(line2); Assert.assertNotNull(packet); LOGGER.info("Current packet: {}", packet); int packetLen = ByteUtils.getInt(packet.getLen()); // 08 F2D0 00 CommandStationPomMessage message = decoderPomUpdate.prepareNextRowPacket(packet, DecoderType.locoDecoder); Assert.assertNotNull(message); LOGGER.info("Row packet, message: {}, formatted data: {}", message, ByteUtils.bytesToHex(message.getData())); Assert.assertEquals(message.getDecoderAddress().getAddress(), 8448); Assert.assertEquals(message.getCvNumber(), 0xF2 + 1); Assert.assertEquals(message.getCvValue(), 0xD0); for (int index = 0; index < (packetLen / 3 + 1); index++) { message = decoderPomUpdate.prepareNextDataPacket(packet, DecoderType.locoDecoder); Assert.assertNotNull(message); LOGGER.info("Data packet, message: {}, formatted data: {}", message, ByteUtils.bytesToHex(message.getData())); } // verify the last message Assert.assertNotNull(message); LOGGER.info("Data packet, message: {}, formatted data: {}", message, ByteUtils.bytesToHex(message.getData())); Assert.assertEquals(message.getDecoderAddress().getAddress(), 8192 + 0 /* 0x00 : 8192 */); Assert.assertEquals(message.getCvNumber(), 0x00 + 1 + 0x0200); Assert.assertEquals(message.getCvValue(), 0x00); }