Search in sources :

Example 1 with ReadBuffer

use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.

the class EipProtocolLogic method decodeWriteResponse.

private PlcResponse decodeWriteResponse(CipService p, PlcWriteRequest writeRequest) {
    Map<String, PlcResponseCode> responses = new HashMap<>();
    if (p instanceof CipWriteResponse) {
        CipWriteResponse resp = (CipWriteResponse) p;
        String fieldName = writeRequest.getFieldNames().iterator().next();
        EipField field = (EipField) writeRequest.getField(fieldName);
        responses.put(fieldName, decodeResponseCode(resp.getStatus()));
        return new DefaultPlcWriteResponse(writeRequest, responses);
    } else if (p instanceof MultipleServiceResponse) {
        MultipleServiceResponse resp = (MultipleServiceResponse) p;
        int nb = resp.getServiceNb();
        List<CipService> arr = new ArrayList<>(nb);
        ReadBufferByteBased read = new ReadBufferByteBased(resp.getServicesData());
        int total = (int) read.getTotalBytes();
        for (int i = 0; i < nb; i++) {
            int length = 0;
            int offset = resp.getOffsets().get(i);
            if (offset == nb - 1) {
                // Get the rest if last
                length = total - offset;
            } else {
                // Calculate length with offsets
                length = resp.getOffsets().get(i + 1) - offset;
            }
            ReadBuffer serviceBuf = new ReadBufferByteBased(read.getBytes(offset, length), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
            CipService service = null;
            try {
                service = CipService.staticParse(read, length);
                arr.add(service);
            } catch (ParseException e) {
                throw new PlcRuntimeException(e);
            }
        }
        Services services = new Services(nb, resp.getOffsets(), arr, -1);
        Iterator<String> it = writeRequest.getFieldNames().iterator();
        for (int i = 0; i < nb && it.hasNext(); i++) {
            String fieldName = it.next();
            EipField field = (EipField) writeRequest.getField(fieldName);
            PlcValue plcValue = null;
            if (services.getServices().get(i) instanceof CipWriteResponse) {
                CipWriteResponse writeResponse = (CipWriteResponse) services.getServices().get(i);
                PlcResponseCode code = decodeResponseCode(writeResponse.getStatus());
                responses.put(fieldName, code);
            }
        }
        return new DefaultPlcWriteResponse(writeRequest, responses);
    }
    return null;
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) EipField(org.apache.plc4x.java.eip.readwrite.field.EipField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased) ParseException(org.apache.plc4x.java.spi.generation.ParseException)

Example 2 with ReadBuffer

use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.

the class ManualParserTest method readBufferTest.

@Test
public void readBufferTest() throws Exception {
    ReadBuffer buffer = new ReadBufferByteBased(new byte[] { (byte) 0xA1, 0x05, 0x00, 0x00 }, ByteOrder.LITTLE_ENDIAN);
    int value = buffer.readInt(32);
    assertEquals(value, 0x5A1);
}
Also used : ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased) Test(org.junit.jupiter.api.Test)

Example 3 with ReadBuffer

use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.

the class BenchmarkGeneratedS7 method main.

public static void main(String[] args) throws Exception {
    byte[] rData = Hex.decodeHex("0300001611e00000000f00c2020100c1020311c0010a");
    long start = System.currentTimeMillis();
    int numRunsParse = 2000000;
    // Benchmark the parsing code
    TPKTPacket packet = null;
    for (int i = 0; i < numRunsParse; i++) {
        ReadBuffer rBuf = new ReadBufferByteBased(rData);
        packet = TPKTPacket.staticParse(rBuf);
    }
    long endParsing = System.currentTimeMillis();
    System.out.println("Parsed " + numRunsParse + " packets in " + (endParsing - start) + "ms");
    System.out.println("That's " + ((float) (endParsing - start) / numRunsParse) + "ms per packet");
    // Benchmark the serializing code
    int numRunsSerialize = 2000000;
    byte[] oData = null;
    for (int i = 0; i < numRunsSerialize; i++) {
        WriteBufferByteBased wBuf = new WriteBufferByteBased(packet.getLengthInBytes());
        packet.serialize(wBuf);
        oData = wBuf.getData();
    }
    long endSerializing = System.currentTimeMillis();
    System.out.println("Serialized " + numRunsSerialize + " packets in " + (endSerializing - endParsing) + "ms");
    System.out.println("That's " + ((float) (endSerializing - endParsing) / numRunsSerialize) + "ms per packet");
    if (!Arrays.equals(rData, oData)) {
        for (int i = 0; i < rData.length; i++) {
            if (rData[i] != oData[i]) {
                System.out.println("Difference in byte " + i);
            }
        }
        System.out.println("Not equals");
    } else {
        System.out.println("Bytes equal");
    }
}
Also used : ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) TPKTPacket(org.apache.plc4x.java.s7.readwrite.TPKTPacket) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased) WriteBufferByteBased(org.apache.plc4x.java.spi.generation.WriteBufferByteBased)

Example 4 with ReadBuffer

use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.

the class Df1Protocol method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    DF1Symbol resp;
    // do {
    in.markReaderIndex();
    short size = 0x00;
    // Yes, it's a little complicated, but we need to find out if we've got enough data.
    if (in.readableBytes() > 1) {
        if (in.getUnsignedByte(0) != (short) 0x10) {
            logger.warn("Expecting DF1 magic number: {}", 0x10);
            if (logger.isDebugEnabled()) {
                logger.debug("Got Data: {}", ByteBufUtil.hexDump(in));
            }
            exceptionCaught(ctx, new PlcProtocolException(String.format("Expecting DF1 magic number: %02X", 0x10)));
            return;
        }
        short symbolType = in.getUnsignedByte(1);
        switch(symbolType) {
            case (short) 0x02:
                {
                    if (in.readableBytes() < 5) {
                        return;
                    }
                    short commandType = in.getUnsignedByte(4);
                    switch(commandType) {
                        case (short) 0x01:
                            {
                                if (in.readableBytes() < 11) {
                                    return;
                                }
                                break;
                            }
                        case (short) 0x41:
                            {
                                // TODO: Let's just assume all is good for now ...
                                break;
                            }
                    }
                    break;
                }
            case (short) 0x03:
                {
                    if (in.readableBytes() < 4) {
                        return;
                    }
                    break;
                }
        }
    }
    // Parse the message received from the DF1 device
    byte[] data = new byte[in.readableBytes()];
    in.readBytes(data);
    ReadBuffer readBuffer = new ReadBufferByteBased(data);
    resp = DF1Symbol.staticParse(readBuffer);
    // } while (readWasSucessfull);
    // // TODO if unableto read
    // in.resetReaderIndex();
    // Add the received message to the output
    out.add(resp);
}
Also used : ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased) DF1Symbol(org.apache.plc4x.java.df1.readwrite.DF1Symbol)

Example 5 with ReadBuffer

use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.

the class ManualParserSerializerTest method main.

public static void main(String[] args) throws Exception {
    byte[] data = Hex.decodeHex("0000f1000000c0a817c80101feffc0a817140101530309000500d1000000000000002f00000000000000c90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a00a07d0f7e1c8e31489f8cf1006b3604b888defc88f64eb6fbb217d11fc5cf5f148b0abf05407c941a93b8263301db0f4940d66875727a0000000000d2040000ab6459032bbf7e03b888defcb8a5b249044de82ee03a2ab8a577006f006c0066000000488bf0488bfa33c9");
    // byte[] data = Hex.decodeHex("0000f1000000c0a817c80101feffc0a817140101530309000500d1000000000000002f00000000000000c90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a00a07d0f7e1c8e31489f8cf1006b3604b888defc");
    ReadBuffer readBuffer = new ReadBufferByteBased(data, ByteOrder.LITTLE_ENDIAN);
    final AmsTCPPacket amsTCPPacket = AmsTCPPacket.staticParse(readBuffer);
    System.out.println(amsTCPPacket);
}
Also used : ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) AmsTCPPacket(org.apache.plc4x.java.ads.readwrite.AmsTCPPacket) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased)

Aggregations

ReadBuffer (org.apache.plc4x.java.spi.generation.ReadBuffer)7 ReadBufferByteBased (org.apache.plc4x.java.spi.generation.ReadBufferByteBased)7 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)2 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)2 EipField (org.apache.plc4x.java.eip.readwrite.field.EipField)2 ParseException (org.apache.plc4x.java.spi.generation.ParseException)2 ByteBuf (io.netty.buffer.ByteBuf)1 AmsTCPPacket (org.apache.plc4x.java.ads.readwrite.AmsTCPPacket)1 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)1 BVLC (org.apache.plc4x.java.bacnetip.readwrite.BVLC)1 DF1Symbol (org.apache.plc4x.java.df1.readwrite.DF1Symbol)1 TPKTPacket (org.apache.plc4x.java.s7.readwrite.TPKTPacket)1 WriteBufferByteBased (org.apache.plc4x.java.spi.generation.WriteBufferByteBased)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 Test (org.junit.jupiter.api.Test)1