Search in sources :

Example 6 with PlcProtocolException

use of org.apache.plc4x.java.api.exceptions.PlcProtocolException in project plc4x by apache.

the class Plc4xAbEthProtocol method decode.

@Override
protected void decode(ChannelHandlerContext ctx, CIPEncapsulationPacket packet, List<Object> out) throws Exception {
    logger.trace("Received {}, decoding...", packet);
    if (packet instanceof CIPEncapsulationConnectionResponse) {
        CIPEncapsulationConnectionResponse connectionResponse = (CIPEncapsulationConnectionResponse) packet;
        // Save the session handle
        sessionHandle = connectionResponse.getSessionHandle();
        // Tell Netty we're finished connecting
        ctx.channel().pipeline().fireUserEventTriggered(new ConnectedEvent());
    } else {
        // We're currently just expecting responses.
        if (!(packet instanceof CIPEncapsulationReadResponse)) {
            return;
        }
        CIPEncapsulationReadResponse cipResponse = (CIPEncapsulationReadResponse) packet;
        int transactionCounter = cipResponse.getResponse().getTransactionCounter();
        if (!requests.containsKey(transactionCounter)) {
            ctx.fireExceptionCaught(new PlcProtocolException("Couldn't find request for response with transaction counter " + transactionCounter));
            return;
        }
        PlcRequestContainer requestContainer = requests.remove(transactionCounter);
        PlcRequest request = requestContainer.getRequest();
        PlcResponse response = null;
        if (request instanceof PlcReadRequest) {
            response = decodeReadResponse(cipResponse, requestContainer);
        } else {
            ctx.fireExceptionCaught(new PlcProtocolException("Unsupported request type " + request.getClass().getName()));
        }
        // Confirm the response being handled.
        if (response != null) {
            requestContainer.getResponseFuture().complete(response);
        }
    }
}
Also used : PlcRequestContainer(org.apache.plc4x.java.spi.messages.PlcRequestContainer) PlcResponse(org.apache.plc4x.java.api.messages.PlcResponse) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) ConnectedEvent(org.apache.plc4x.java.spi.events.ConnectedEvent) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest)

Example 7 with PlcProtocolException

use of org.apache.plc4x.java.api.exceptions.PlcProtocolException in project plc4x by apache.

the class S7ProtocolLogic method decodeEventSubscriptionRequest.

private PlcSubscriptionResponse decodeEventSubscriptionRequest(S7Message responseMessage, PlcSubscriptionRequest plcSubscriptionRequest) throws PlcProtocolException {
    Map<String, ResponseItem<PlcSubscriptionHandle>> values = new HashMap<>();
    short errorClass = 0;
    short errorCode = 0;
    if (responseMessage instanceof S7MessageUserData) {
        S7MessageUserData messageUserData = (S7MessageUserData) responseMessage;
        S7PayloadUserData payload = (S7PayloadUserData) messageUserData.getPayload();
    // errorClass = payload.getItems()[0].
    // errorCode = messageUserData.getParameter().
    } else if (responseMessage instanceof S7MessageResponse) {
        S7MessageResponse messageResponse = (S7MessageResponse) responseMessage;
        errorClass = messageResponse.getErrorClass();
        errorCode = messageResponse.getErrorCode();
    } else {
        throw new PlcProtocolException("Unsupported message type " + responseMessage.getClass().getName());
    }
    // If the result contains any form of non-null error code, handle this instead.
    if ((errorClass != 0) || (errorCode != 0)) {
        // This is usually the case if PUT/GET wasn't enabled on the PLC
        if ((errorClass == 129) && (errorCode == 4)) {
            logger.warn("Got an error response from the PLC. This particular response code usually indicates " + "that PUT/GET is not enabled on the PLC.");
            for (String fieldName : plcSubscriptionRequest.getFieldNames()) {
                values.put(fieldName, null);
            }
            return new DefaultPlcSubscriptionResponse(plcSubscriptionRequest, values);
        } else {
            logger.warn("Got an unknown error response from the PLC. Error Class: {}, Error Code {}. " + "We probably need to implement explicit handling for this, so please file a bug-report " + "on https://issues.apache.org/jira/projects/PLC4X and ideally attach a WireShark dump " + "containing a capture of the communication.", errorClass, errorCode);
            for (String fieldName : plcSubscriptionRequest.getFieldNames()) {
                values.put(fieldName, null);
            }
            return new DefaultPlcSubscriptionResponse(plcSubscriptionRequest, values);
        }
    }
    // In all other cases all went well.
    S7PayloadUserData payload = (S7PayloadUserData) responseMessage.getPayload();
    List<S7PayloadUserDataItem> payloadItems = payload.getItems();
    // Only one item for any number of subscription (4)
    if (payloadItems.size() == 0) {
        throw new PlcProtocolException("The number of requested items doesn't match the number of returned items");
    }
    boolean responseOk = false;
    if (payloadItems.get(0) instanceof S7PayloadUserDataItemCpuFunctionMsgSubscriptionResponse) {
        S7PayloadUserDataItemCpuFunctionMsgSubscriptionResponse item = (S7PayloadUserDataItemCpuFunctionMsgSubscriptionResponse) payloadItems.get(0);
        if ((item.getReturnCode() == DataTransportErrorCode.OK) && (item.getTransportSize() == DataTransportSize.OCTET_STRING)) {
            responseOk = true;
        }
    } else if (payloadItems.get(0) instanceof S7PayloadUserDataItemCpuFunctionMsgSubscriptionSysResponse) {
        S7PayloadUserDataItemCpuFunctionMsgSubscriptionSysResponse item = (S7PayloadUserDataItemCpuFunctionMsgSubscriptionSysResponse) payloadItems.get(0);
        if ((item.getReturnCode() == DataTransportErrorCode.OK) && (item.getTransportSize() == DataTransportSize.OCTET_STRING)) {
            responseOk = true;
        }
    } else if (payloadItems.get(0) instanceof S7PayloadUserDataItemCpuFunctionMsgSubscriptionAlarmResponse) {
        S7PayloadUserDataItemCpuFunctionMsgSubscriptionAlarmResponse item = (S7PayloadUserDataItemCpuFunctionMsgSubscriptionAlarmResponse) payloadItems.get(0);
        if ((item.getReturnCode() == DataTransportErrorCode.OK) && (item.getTransportSize() == DataTransportSize.OCTET_STRING)) {
            responseOk = true;
        }
    }
    if (responseOk) {
        for (String fieldName : plcSubscriptionRequest.getFieldNames()) {
            DefaultPlcSubscriptionField dfield = (DefaultPlcSubscriptionField) plcSubscriptionRequest.getField(fieldName);
            S7SubscriptionField field = (S7SubscriptionField) dfield.getPlcField();
            switch(field.getEventType()) {
                case MODE:
                    values.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, modeHandle));
                    break;
                case SYS:
                    values.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, sysHandle));
                    break;
                case USR:
                    values.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, usrHandle));
                    break;
                case ALM:
                    values.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, almHandle));
                    break;
            }
        }
        return new DefaultPlcSubscriptionResponse(plcSubscriptionRequest, values);
    }
    return null;
}
Also used : S7SubscriptionField(org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 8 with PlcProtocolException

use of org.apache.plc4x.java.api.exceptions.PlcProtocolException in project plc4x by apache.

the class Plc4xAbEthProtocol method encode.

@Override
protected void encode(ChannelHandlerContext ctx, PlcRequestContainer msg, List<Object> out) throws Exception {
    logger.trace("Encoding {}", msg);
    PlcRequest request = msg.getRequest();
    // reset counter since two byte values are possible in DF1
    if (transactionCounterGenerator.get() > 65000) {
        transactionCounterGenerator.set(10);
    }
    if (request instanceof PlcReadRequest) {
        PlcReadRequest readRequest = (PlcReadRequest) msg.getRequest();
        for (String fieldName : readRequest.getFieldNames()) {
            PlcField field = readRequest.getField(fieldName);
            if (!(field instanceof AbEthField)) {
                throw new PlcProtocolException("The field should have been of type AbEthField");
            }
            AbEthField abEthField = (AbEthField) field;
            DF1RequestProtectedTypedLogicalRead logicalRead = new DF1RequestProtectedTypedLogicalRead(abEthField.getByteSize(), abEthField.getFileNumber(), abEthField.getFileType().getTypeCode(), abEthField.getElementNumber(), // Subelementnumber default to zero
            (short) 0);
            // origin/sender: constant = 5
            DF1RequestMessage requestMessage = new DF1CommandRequestMessage((short) station, (short) 5, (short) 0, transactionCounterGenerator.incrementAndGet(), logicalRead);
            CIPEncapsulationReadRequest read = new CIPEncapsulationReadRequest(sessionHandle, 0, emptySenderContext, 0, requestMessage);
            requests.put(requestMessage.getTransactionCounter(), msg);
            out.add(read);
        }
    } else {
        ctx.fireExceptionCaught(new PlcProtocolException("Unsupported request type " + request.getClass().getName()));
    }
}
Also used : PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) PlcField(org.apache.plc4x.java.api.model.PlcField) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) AbEthField(org.apache.plc4x.java.abeth.field.AbEthField)

Example 9 with PlcProtocolException

use of org.apache.plc4x.java.api.exceptions.PlcProtocolException in project plc4x by apache.

the class Plc4x2AdsProtocol method encodeReadRequest.

private void encodeReadRequest(PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws PlcException {
    PlcReadRequest readRequest = (PlcReadRequest) msg.getRequest();
    if (readRequest.getFields().size() != 1) {
        throw new PlcProtocolException("Only one item supported");
    }
    PlcField field = readRequest.getFields().get(0);
    if (field instanceof SymbolicAdsField) {
        DirectAdsField mappedField = fieldMapping.get(field);
        if (mappedField == null) {
            throw new PlcProtocolException("No field mapping for " + field);
        }
        LOGGER.debug("Replacing {} with {}", field, mappedField);
        field = mappedField;
    }
    if (!(field instanceof DirectAdsField)) {
        throw new PlcProtocolException("PlcField not of type DirectAdsField: " + field.getClass());
    }
    DirectAdsField directAdsField = (DirectAdsField) field;
    Invoke invokeId = Invoke.of(correlationBuilder.incrementAndGet());
    IndexGroup indexGroup = IndexGroup.of(directAdsField.getIndexGroup());
    IndexOffset indexOffset = IndexOffset.of(directAdsField.getIndexOffset());
    AdsDataType adsDataType = directAdsField.getAdsDataType();
    int numberOfElements = directAdsField.getNumberOfElements();
    int readLength = adsDataType.getTargetByteSize() * numberOfElements;
    Length length = Length.of(readLength);
    AmsPacket amsPacket = AdsReadRequest.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, indexGroup, indexOffset, length);
    LOGGER.debug("encoded read request {}", amsPacket);
    out.add(amsPacket);
    requests.put(invokeId.getAsLong(), msg);
}
Also used : PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) SymbolicAdsField(org.apache.plc4x.java.ads.model.SymbolicAdsField) AdsDataType(org.apache.plc4x.java.ads.model.AdsDataType) PlcField(org.apache.plc4x.java.api.model.PlcField) DirectAdsField(org.apache.plc4x.java.ads.model.DirectAdsField) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) Invoke(org.apache.plc4x.java.ads.api.generic.types.Invoke)

Example 10 with PlcProtocolException

use of org.apache.plc4x.java.api.exceptions.PlcProtocolException in project plc4x by apache.

the class Plc4x2AdsProtocol method encode.

@Override
protected void encode(ChannelHandlerContext ctx, PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws Exception {
    LOGGER.trace("(<--OUT): {}, {}, {}", ctx, msg, out);
    PlcRequest request = msg.getRequest();
    if (request instanceof PlcReadRequest) {
        encodeReadRequest(msg, out);
    } else if (request instanceof PlcWriteRequest) {
        encodeWriteRequest(msg, out);
    } else if (request instanceof PlcProprietaryRequest) {
        encodeProprietaryRequest(msg, out);
    } else {
        throw new PlcProtocolException("Unknown type " + request.getClass());
    }
}
Also used : PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest)

Aggregations

PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)16 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)7 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)5 PlcField (org.apache.plc4x.java.api.model.PlcField)5 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)5 PlcRequest (org.apache.plc4x.java.api.messages.PlcRequest)4 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)4 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)4 PlcValue (org.apache.plc4x.java.api.value.PlcValue)4 S7SubscriptionField (org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField)4 DefaultPlcSubscriptionResponse (org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse)4 DefaultPlcSubscriptionField (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField)4 ByteBuf (io.netty.buffer.ByteBuf)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 TimeoutException (java.util.concurrent.TimeoutException)3 PlcResponse (org.apache.plc4x.java.api.messages.PlcResponse)3 S7Field (org.apache.plc4x.java.s7.readwrite.field.S7Field)3 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)3 DefaultPlcWriteResponse (org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)3 PlcNull (org.apache.plc4x.java.spi.values.PlcNull)3