use of org.apache.plc4x.java.ads.api.commands.AdsWriteRequest in project plc4x by apache.
the class Plc4x2AdsProtocolTest method encode.
@Test
public void encode() throws Exception {
assumeThat(payloadClazzName + " not yet implemented", notYetSupportedDataType, not(hasItem(payloadClazzName)));
ArrayList<Object> out = new ArrayList<>();
SUT.encode(null, plcRequestContainer, out);
assertThat(out, hasSize(1));
assertThat(out.get(0), instanceOf(AmsPacket.class));
AmsPacket amsPacket = (AmsPacket) out.get(0);
LOGGER.info("{}\nHexDump:\n{}", amsPacket, amsPacket.dump());
if (amsPacket instanceof AdsWriteRequest) {
AdsWriteRequest adsWriteRequest = (AdsWriteRequest) amsPacket;
byte[] value = adsWriteRequest.getData().getBytes();
if (payloadClazzName.equals(Boolean.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x1 }));
} else if (payloadClazzName.equals(Byte.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x1 }));
} else if (payloadClazzName.equals(Short.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x1, 0x0 }));
} else if (payloadClazzName.equals(Integer.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x1, 0x0, 0x0, 0x0 }));
} else if (payloadClazzName.equals(Long.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x1, 0x0, 0x0, 0x0 }));
} else if (payloadClazzName.equals(BigInteger.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x1, 0x0, 0x0, 0x0 }));
} else if (payloadClazzName.equals(Float.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x0, 0x0, (byte) 0x80, 0x3F }));
} else if (payloadClazzName.equals(Double.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F }));
} else if (payloadClazzName.equals(BigDecimal.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F }));
} else if (payloadClazzName.equals(String.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
} else if (payloadClazzName.equals(LocalTime.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
} else if (payloadClazzName.equals(LocalDate.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
} else if (payloadClazzName.equals(LocalDateTime.class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
} else if (payloadClazzName.equals(byte[].class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
} else if (payloadClazzName.equals(Byte[].class.getSimpleName())) {
assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
}
}
}
use of org.apache.plc4x.java.ads.api.commands.AdsWriteRequest in project plc4x by apache.
the class AdsDumper method main.
public static void main(String... args) throws Exception {
Path dumpFile = Files.createTempFile("pcapdump", ".pcap");
try (PcapHandle handle = Pcaps.openDead(DataLinkType.EN10MB, 65536);
PcapDumper dumper = handle.dumpOpen(dumpFile.toAbsolutePath().toString())) {
String randomString = RandomStringUtils.randomAscii(1024);
AdsWriteRequest adsWriteRequest = AdsWriteRequest.of(AmsNetId.of("192.168.99.101.1.1"), AmsPort.of(851), AmsNetId.of("192.168.99.1.1.1"), AmsPort.of(14), Invoke.of(0), IndexGroup.of(1), IndexOffset.of(3), Data.of(randomString.getBytes()));
AmsTCPPacket amsTCPPacket = AmsTCPPacket.of(UserData.of(adsWriteRequest.getBytes()));
// try (TcpHexDumper tcpHexDumper = TcpHexDumper.runOn(55862); Socket localhost = new Socket("localhost", tcpHexDumper.getPort())) {
try (TcpHexDumper tcpHexDumper = TcpHexDumper.runOn(55862);
Socket localhost = new Socket("192.168.99.100", 48898)) {
localhost.getOutputStream().write(amsTCPPacket.getBytes());
}
UnknownPacket.Builder amsPacket = new UnknownPacket.Builder();
amsPacket.rawData(amsTCPPacket.getBytes());
TcpPacket.Builder tcpPacketBuilder = new TcpPacket.Builder();
tcpPacketBuilder.srcAddr(InetAddress.getLocalHost()).srcPort(TcpPort.getInstance((short) 13)).dstAddr(InetAddress.getLocalHost()).dstPort(TcpPort.getInstance((short) 48898)).payloadBuilder(amsPacket).correctChecksumAtBuild(true).correctLengthAtBuild(true);
IpV4Packet.Builder ipv4PacketBuilder = new IpV4Packet.Builder();
ipv4PacketBuilder.version(IpVersion.IPV4).tos(IpV4Rfc1349Tos.newInstance((byte) 0x75)).protocol(IpNumber.TCP).srcAddr((Inet4Address) InetAddress.getLocalHost()).dstAddr((Inet4Address) InetAddress.getLocalHost()).payloadBuilder(tcpPacketBuilder).correctChecksumAtBuild(true).correctLengthAtBuild(true);
EthernetPacket.Builder etherPacketBuilder = new EthernetPacket.Builder();
etherPacketBuilder.srcAddr(MacAddress.getByName("fe:00:00:00:00:01")).dstAddr(MacAddress.getByName("fe:00:00:00:00:02")).type(EtherType.IPV4).payloadBuilder(ipv4PacketBuilder).paddingAtBuild(true);
dumper.dump(etherPacketBuilder.build());
dumper.flush();
logger.info("Wrote {}", dumpFile);
}
}
Aggregations