Search in sources :

Example 6 with ReadBuffer

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

the class EipProtocolLogic method decodeReadResponse.

private PlcResponse decodeReadResponse(CipService p, PlcReadRequest readRequest) {
    Map<String, ResponseItem<PlcValue>> values = new HashMap<>();
    // only 1 field
    if (p instanceof CipReadResponse) {
        CipReadResponse resp = (CipReadResponse) p;
        String fieldName = readRequest.getFieldNames().iterator().next();
        EipField field = (EipField) readRequest.getField(fieldName);
        PlcResponseCode code = decodeResponseCode(resp.getStatus());
        PlcValue plcValue = null;
        CIPDataTypeCode type = resp.getDataType();
        ByteBuf data = Unpooled.wrappedBuffer(resp.getData());
        if (code == PlcResponseCode.OK) {
            plcValue = parsePlcValue(field, data, type);
        }
        ResponseItem<PlcValue> result = new ResponseItem<>(code, plcValue);
        values.put(fieldName, result);
    } else // Multiple response
    if (p instanceof MultipleServiceResponse) {
        MultipleServiceResponse responses = (MultipleServiceResponse) p;
        int nb = responses.getServiceNb();
        List<CipService> arr = new ArrayList<>(nb);
        ReadBufferByteBased read = new ReadBufferByteBased(responses.getServicesData(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
        int total = (int) read.getTotalBytes();
        for (int i = 0; i < nb; i++) {
            int length = 0;
            // Substract first offset as we only have the service in the buffer (not servicesNb and offsets)
            int offset = responses.getOffsets().get(i) - responses.getOffsets().get(0);
            if (i == nb - 1) {
                // Get the rest if last
                length = total - offset;
            } else {
                // Calculate length with offsets (substracting first offset)
                length = responses.getOffsets().get(i + 1) - offset - responses.getOffsets().get(0);
            }
            ReadBuffer serviceBuf = new ReadBufferByteBased(read.getBytes(offset, 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, responses.getOffsets(), arr, -1);
        Iterator<String> it = readRequest.getFieldNames().iterator();
        for (int i = 0; i < nb && it.hasNext(); i++) {
            String fieldName = it.next();
            EipField field = (EipField) readRequest.getField(fieldName);
            PlcValue plcValue = null;
            if (services.getServices().get(i) instanceof CipReadResponse) {
                CipReadResponse readResponse = (CipReadResponse) services.getServices().get(i);
                PlcResponseCode code;
                if (readResponse.getStatus() == 0) {
                    code = PlcResponseCode.OK;
                } else {
                    code = PlcResponseCode.INTERNAL_ERROR;
                }
                CIPDataTypeCode type = readResponse.getDataType();
                ByteBuf data = Unpooled.wrappedBuffer(readResponse.getData());
                if (code == PlcResponseCode.OK) {
                    plcValue = parsePlcValue(field, data, type);
                }
                ResponseItem<PlcValue> result = new ResponseItem<>(code, plcValue);
                values.put(fieldName, result);
            }
        }
    }
    return new DefaultPlcReadResponse(readRequest, values);
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) EipField(org.apache.plc4x.java.eip.readwrite.field.EipField) ByteBuf(io.netty.buffer.ByteBuf) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) ParseException(org.apache.plc4x.java.spi.generation.ParseException)

Example 7 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 8 with ReadBuffer

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

the class ManualBacNetDecoder method main.

public static void main(String[] args) throws Exception {
    final byte[] bytes = Hex.decodeHex("810a002b01040205790109011c020000142c000002f93a06b24e09552e44434a00002f096f2e8204002f4f");
    ReadBuffer readBuffer = new ReadBufferByteBased(bytes);
    final BVLC packet = BVLC.staticParse(readBuffer);
    System.out.println(packet);
}
Also used : ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) BVLC(org.apache.plc4x.java.bacnetip.readwrite.BVLC) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased)

Aggregations

ReadBuffer (org.apache.plc4x.java.spi.generation.ReadBuffer)8 ReadBufferByteBased (org.apache.plc4x.java.spi.generation.ReadBufferByteBased)7 ParseException (org.apache.plc4x.java.spi.generation.ParseException)3 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 ByteBuf (io.netty.buffer.ByteBuf)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 FieldUtils (org.apache.commons.lang3.reflect.FieldUtils)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AmsTCPPacket (org.apache.plc4x.java.ads.readwrite.AmsTCPPacket)1 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)1 org.apache.plc4x.java.bacnetip.readwrite (org.apache.plc4x.java.bacnetip.readwrite)1 BVLC (org.apache.plc4x.java.bacnetip.readwrite.BVLC)1