Search in sources :

Example 1 with ParseException

use of org.apache.plc4x.java.spi.generation.ParseException 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 ParseException

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

the class StaticHelper method parseS7String.

public static String parseS7String(ReadBuffer io, int stringLength, String encoding) {
    try {
        if ("UTF-8".equalsIgnoreCase(encoding)) {
            // This is the maximum number of bytes a string can be long.
            short maxLength = io.readUnsignedShort(8);
            // This is the total length of the string on the PLC (Not necessarily the number of characters read)
            short totalStringLength = io.readUnsignedShort(8);
            final byte[] byteArray = new byte[totalStringLength];
            for (int i = 0; (i < stringLength) && io.hasMore(8); i++) {
                final byte curByte = io.readByte();
                if (i < totalStringLength) {
                    byteArray[i] = curByte;
                } else {
                    // Gobble up the remaining data, which is not added to the string.
                    i++;
                    for (; (i < stringLength) && io.hasMore(8); i++) {
                        io.readByte();
                    }
                    break;
                }
            }
            return new String(byteArray, StandardCharsets.UTF_8);
        } else if ("UTF-16".equalsIgnoreCase(encoding)) {
            // This is the maximum number of bytes a string can be long.
            int maxLength = io.readUnsignedInt(16);
            // This is the total length of the string on the PLC (Not necessarily the number of characters read)
            int totalStringLength = io.readUnsignedInt(16);
            final byte[] byteArray = new byte[totalStringLength * 2];
            for (int i = 0; (i < stringLength) && io.hasMore(16); i++) {
                final short curShort = io.readShort(16);
                if (i < totalStringLength) {
                    byteArray[i * 2] = (byte) (curShort >>> 8);
                    byteArray[(i * 2) + 1] = (byte) (curShort & 0xFF);
                } else {
                    // Gobble up the remaining data, which is not added to the string.
                    i++;
                    for (; (i < stringLength) && io.hasMore(16); i++) {
                        io.readShort(16);
                    }
                    break;
                }
            }
            return new String(byteArray, StandardCharsets.UTF_16);
        } else {
            throw new PlcRuntimeException("Unsupported string encoding " + encoding);
        }
    } catch (ParseException e) {
        throw new PlcRuntimeException("Error parsing string", e);
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) ParseException(org.apache.plc4x.java.spi.generation.ParseException)

Example 3 with ParseException

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

the class StaticHelper method parseAmsString.

public static String parseAmsString(ReadBuffer readBuffer, int stringLength, String encoding) {
    try {
        if ("UTF-8".equalsIgnoreCase(encoding)) {
            List<Byte> bytes = new ArrayList<>();
            for (int i = 0; (i < stringLength) && readBuffer.hasMore(8); i++) {
                final byte curByte = readBuffer.readByte();
                if (curByte != 0) {
                    bytes.add(curByte);
                } else {
                    // Gobble up the remaining data, which is not added to the string.
                    i++;
                    for (; (i < stringLength) && readBuffer.hasMore(8); i++) {
                        readBuffer.readByte();
                    }
                    break;
                }
            }
            final byte[] byteArray = new byte[bytes.size()];
            for (int i = 0; i < bytes.size(); i++) {
                byteArray[i] = bytes.get(i);
            }
            return new String(byteArray, StandardCharsets.UTF_8);
        } else if ("UTF-16".equalsIgnoreCase(encoding)) {
            List<Byte> bytes = new ArrayList<>();
            for (int i = 0; (i < stringLength) && readBuffer.hasMore(16); i++) {
                final short curShort = readBuffer.readShort(16);
                if (curShort != 0) {
                    bytes.add((byte) (curShort >>> 8));
                    bytes.add((byte) (curShort & 0xFF));
                } else {
                    // Gobble up the remaining data, which is not added to the string.
                    i++;
                    for (; (i < stringLength) && readBuffer.hasMore(16); i++) {
                        readBuffer.readShort(16);
                    }
                    break;
                }
            }
            final byte[] byteArray = new byte[bytes.size()];
            for (int i = 0; i < bytes.size(); i++) {
                byteArray[i] = bytes.get(i);
            }
            return new String(byteArray, StandardCharsets.UTF_16);
        } else {
            throw new PlcRuntimeException("Unsupported string encoding " + encoding);
        }
    } catch (ParseException e) {
        throw new PlcRuntimeException("Error parsing string", e);
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(org.apache.plc4x.java.spi.generation.ParseException)

Example 4 with ParseException

use of org.apache.plc4x.java.spi.generation.ParseException 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)

Aggregations

PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 ParseException (org.apache.plc4x.java.spi.generation.ParseException)4 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)2 EipField (org.apache.plc4x.java.eip.readwrite.field.EipField)2 ReadBuffer (org.apache.plc4x.java.spi.generation.ReadBuffer)2 ReadBufferByteBased (org.apache.plc4x.java.spi.generation.ReadBufferByteBased)2 ByteBuf (io.netty.buffer.ByteBuf)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1