Search in sources :

Example 11 with PlcProtocolException

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

the class S7ProtocolLogic method decodeReadResponse.

private PlcResponse decodeReadResponse(S7Message responseMessage, PlcReadRequest plcReadRequest) throws PlcProtocolException {
    Map<String, ResponseItem<PlcValue>> values = new HashMap<>();
    short errorClass;
    short errorCode;
    if (responseMessage instanceof S7MessageResponseData) {
        S7MessageResponseData messageResponseData = (S7MessageResponseData) responseMessage;
        errorClass = messageResponseData.getErrorClass();
        errorCode = messageResponseData.getErrorCode();
    } 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 : plcReadRequest.getFieldNames()) {
                ResponseItem<PlcValue> result = new ResponseItem<>(PlcResponseCode.ACCESS_DENIED, new PlcNull());
                values.put(fieldName, result);
            }
            return new DefaultPlcReadResponse(plcReadRequest, 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 : plcReadRequest.getFieldNames()) {
                ResponseItem<PlcValue> result = new ResponseItem<>(PlcResponseCode.INTERNAL_ERROR, new PlcNull());
                values.put(fieldName, result);
            }
            return new DefaultPlcReadResponse(plcReadRequest, values);
        }
    }
    // In all other cases all went well.
    S7PayloadReadVarResponse payload = (S7PayloadReadVarResponse) responseMessage.getPayload();
    // items from the request as this information is not returned by the PLC.
    if (plcReadRequest.getNumberOfFields() != payload.getItems().size()) {
        throw new PlcProtocolException("The number of requested items doesn't match the number of returned items");
    }
    List<S7VarPayloadDataItem> payloadItems = payload.getItems();
    int index = 0;
    for (String fieldName : plcReadRequest.getFieldNames()) {
        S7Field field = (S7Field) plcReadRequest.getField(fieldName);
        S7VarPayloadDataItem payloadItem = payloadItems.get(index);
        PlcResponseCode responseCode = decodeResponseCode(payloadItem.getReturnCode());
        PlcValue plcValue = null;
        ByteBuf data = Unpooled.wrappedBuffer(payloadItem.getData());
        if (responseCode == PlcResponseCode.OK) {
            try {
                plcValue = parsePlcValue(field, data);
            } catch (Exception e) {
                throw new PlcProtocolException("Error decoding PlcValue", e);
            }
        }
        ResponseItem<PlcValue> result = new ResponseItem<>(responseCode, plcValue);
        values.put(fieldName, result);
        index++;
    }
    return new DefaultPlcReadResponse(plcReadRequest, values);
}
Also used : PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) ByteBuf(io.netty.buffer.ByteBuf) TimeoutException(java.util.concurrent.TimeoutException) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcNull(org.apache.plc4x.java.spi.values.PlcNull) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) S7Field(org.apache.plc4x.java.s7.readwrite.field.S7Field)

Example 12 with PlcProtocolException

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

the class S7ProtocolLogic method subscribe.

@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
    CompletableFuture<PlcSubscriptionResponse> future = new CompletableFuture<>();
    DefaultPlcSubscriptionRequest request = (DefaultPlcSubscriptionRequest) subscriptionRequest;
    List<S7ParameterUserDataItem> parameterItems = new ArrayList<>(request.getNumberOfFields());
    List<S7PayloadUserDataItem> payloadItems = new ArrayList<>(request.getNumberOfFields());
    for (String fieldName : request.getFieldNames()) {
        final DefaultPlcSubscriptionField sf = (DefaultPlcSubscriptionField) request.getField(fieldName);
        final S7SubscriptionField field = (S7SubscriptionField) sf.getPlcField();
        switch(field.getFieldType()) {
            case EVENT_SUBSCRIPTION:
                encodeEventSubscriptionRequest(request, parameterItems, payloadItems);
                break;
            case EVENT_UNSUBSCRIPTION:
                // encodeEventUnSubscriptionRequest(msg, out);
                break;
            case ALARM_ACK:
                // encodeAlarmAckRequest(msg, out);
                break;
            case ALARM_QUERY:
                // encodeAlarmQueryRequest(msg, out);
                break;
            case CYCLIC_SUBSCRIPTION:
                // encodeCycledSubscriptionRequest(msg, out);
                break;
            case CYCLIC_UNSUBSCRIPTION:
                // encodeCycledUnSubscriptionRequest(msg, out);
                break;
            default:
        }
    // final PlcValue plcValue = request.getPlcValue(fieldName);
    // parameterItems.add(new S7VarRequestParameterItemAddress(encodeS7Address(field)));
    // payloadItems.add(serializePlcValue(field, plcValue));
    }
    final int tpduId = tpduGenerator.getAndIncrement();
    // If we've reached the max value for a 16 bit transaction identifier, reset back to 1
    if (tpduGenerator.get() == 0xFFFF) {
        tpduGenerator.set(1);
    }
    TPKTPacket tpktPacket = new TPKTPacket(new COTPPacketData(null, new S7MessageUserData(tpduId, new S7ParameterUserData(parameterItems), new S7PayloadUserData(payloadItems, null)), true, (short) tpduId, null));
    // Start a new request-transaction (Is ended in the response-handler)
    RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    transaction.submit(() -> context.sendRequest(tpktPacket).onTimeout(new TransactionErrorCallback<>(future, transaction)).onError(new TransactionErrorCallback<>(future, transaction)).expectResponse(TPKTPacket.class, REQUEST_TIMEOUT).check(p -> p.getPayload() instanceof COTPPacketData).unwrap(p -> ((COTPPacketData) p.getPayload())).unwrap(COTPPacket::getPayload).check(p -> p.getTpduReference() == tpduId).handle(p -> {
        try {
            future.complete(decodeEventSubscriptionRequest(p, subscriptionRequest));
        } catch (PlcProtocolException e) {
            logger.warn("Error sending 'write' message: '{}'", e.getMessage(), e);
        }
        // Finish the request-transaction.
        transaction.endRequest();
    }));
    return future;
}
Also used : PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) DriverContext(org.apache.plc4x.java.spi.context.DriverContext) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) TimeoutException(java.util.concurrent.TimeoutException) ByteBuffer(java.nio.ByteBuffer) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) Unpooled(io.netty.buffer.Unpooled) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) Plc4xProtocolBase(org.apache.plc4x.java.spi.Plc4xProtocolBase) PlcUnsubscriptionRequest(org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) BlockingQueue(java.util.concurrent.BlockingQueue) S7PlcSubscriptionHandle(org.apache.plc4x.java.s7.readwrite.utils.S7PlcSubscriptionHandle) InvocationTargetException(java.lang.reflect.InvocationTargetException) org.apache.plc4x.java.spi.generation(org.apache.plc4x.java.spi.generation) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) org.apache.plc4x.java.s7.readwrite(org.apache.plc4x.java.s7.readwrite) PlcNull(org.apache.plc4x.java.spi.values.PlcNull) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.s7.readwrite.types(org.apache.plc4x.java.s7.readwrite.types) IntStream(java.util.stream.IntStream) S7StringField(org.apache.plc4x.java.s7.readwrite.field.S7StringField) java.util(java.util) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcUnsubscriptionResponse(org.apache.plc4x.java.api.messages.PlcUnsubscriptionResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcResponse(org.apache.plc4x.java.api.messages.PlcResponse) S7DriverContext(org.apache.plc4x.java.s7.readwrite.context.S7DriverContext) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ByteBuf(io.netty.buffer.ByteBuf) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) BiConsumer(java.util.function.BiConsumer) Logger(org.slf4j.Logger) DefaultPlcUnsubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcUnsubscriptionRequest) PlcField(org.apache.plc4x.java.api.model.PlcField) IEC61131ValueHandler(org.apache.plc4x.java.spi.values.IEC61131ValueHandler) Consumer(java.util.function.Consumer) DefaultPlcSubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) S7SubscriptionField(org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField) S7Field(org.apache.plc4x.java.s7.readwrite.field.S7Field) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) S7SubscriptionField(org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest)

Example 13 with PlcProtocolException

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

the class S7ProtocolLogic method decodeWriteResponse.

private PlcResponse decodeWriteResponse(S7Message responseMessage, PlcWriteRequest plcWriteRequest) throws PlcProtocolException {
    Map<String, PlcResponseCode> responses = new HashMap<>();
    short errorClass;
    short errorCode;
    if (responseMessage instanceof S7MessageResponseData) {
        S7MessageResponseData messageResponseData = (S7MessageResponseData) responseMessage;
        errorClass = messageResponseData.getErrorClass();
        errorCode = messageResponseData.getErrorCode();
    } 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 : plcWriteRequest.getFieldNames()) {
                responses.put(fieldName, PlcResponseCode.ACCESS_DENIED);
            }
            return new DefaultPlcWriteResponse(plcWriteRequest, responses);
        } 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 : plcWriteRequest.getFieldNames()) {
                responses.put(fieldName, PlcResponseCode.INTERNAL_ERROR);
            }
            return new DefaultPlcWriteResponse(plcWriteRequest, responses);
        }
    }
    // In all other cases all went well.
    S7PayloadWriteVarResponse payload = (S7PayloadWriteVarResponse) responseMessage.getPayload();
    // items from the request as this information is not returned by the PLC.
    if (plcWriteRequest.getNumberOfFields() != payload.getItems().size()) {
        throw new PlcProtocolException("The number of requested items doesn't match the number of returned items");
    }
    List<S7VarPayloadStatusItem> payloadItems = payload.getItems();
    int index = 0;
    for (String fieldName : plcWriteRequest.getFieldNames()) {
        S7VarPayloadStatusItem payloadItem = payloadItems.get(index);
        PlcResponseCode responseCode = decodeResponseCode(payloadItem.getReturnCode());
        responses.put(fieldName, responseCode);
        index++;
    }
    return new DefaultPlcWriteResponse(plcWriteRequest, responses);
}
Also used : PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)

Example 14 with PlcProtocolException

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

the class S7ProtocolLogic method write.

@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
    DefaultPlcWriteRequest request = (DefaultPlcWriteRequest) writeRequest;
    List<S7VarRequestParameterItem> parameterItems = new ArrayList<>(request.getNumberOfFields());
    List<S7VarPayloadDataItem> payloadItems = new ArrayList<>(request.getNumberOfFields());
    for (String fieldName : request.getFieldNames()) {
        final S7Field field = (S7Field) request.getField(fieldName);
        final PlcValue plcValue = request.getPlcValue(fieldName);
        parameterItems.add(new S7VarRequestParameterItemAddress(encodeS7Address(field)));
        payloadItems.add(serializePlcValue(field, plcValue));
    }
    final int tpduId = tpduGenerator.getAndIncrement();
    // If we've reached the max value for a 16 bit transaction identifier, reset back to 1
    if (tpduGenerator.get() == 0xFFFF) {
        tpduGenerator.set(1);
    }
    TPKTPacket tpktPacket = new TPKTPacket(new COTPPacketData(null, new S7MessageRequest(tpduId, new S7ParameterWriteVarRequest(parameterItems), new S7PayloadWriteVarRequest(payloadItems, null)), true, (short) tpduId, Integer.MAX_VALUE));
    // Start a new request-transaction (Is ended in the response-handler)
    RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    transaction.submit(() -> context.sendRequest(tpktPacket).onTimeout(new TransactionErrorCallback<>(future, transaction)).onError(new TransactionErrorCallback<>(future, transaction)).expectResponse(TPKTPacket.class, REQUEST_TIMEOUT).check(p -> p.getPayload() instanceof COTPPacketData).unwrap(p -> ((COTPPacketData) p.getPayload())).unwrap(COTPPacket::getPayload).check(p -> p.getTpduReference() == tpduId).handle(p -> {
        try {
            future.complete(((PlcWriteResponse) decodeWriteResponse(p, writeRequest)));
        } catch (PlcProtocolException e) {
            logger.warn("Error sending 'write' message: '{}'", e.getMessage(), e);
        }
        // Finish the request-transaction.
        transaction.endRequest();
    }));
    return future;
}
Also used : PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) DriverContext(org.apache.plc4x.java.spi.context.DriverContext) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) TimeoutException(java.util.concurrent.TimeoutException) ByteBuffer(java.nio.ByteBuffer) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) Unpooled(io.netty.buffer.Unpooled) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) Plc4xProtocolBase(org.apache.plc4x.java.spi.Plc4xProtocolBase) PlcUnsubscriptionRequest(org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) BlockingQueue(java.util.concurrent.BlockingQueue) S7PlcSubscriptionHandle(org.apache.plc4x.java.s7.readwrite.utils.S7PlcSubscriptionHandle) InvocationTargetException(java.lang.reflect.InvocationTargetException) org.apache.plc4x.java.spi.generation(org.apache.plc4x.java.spi.generation) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) org.apache.plc4x.java.s7.readwrite(org.apache.plc4x.java.s7.readwrite) PlcNull(org.apache.plc4x.java.spi.values.PlcNull) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.s7.readwrite.types(org.apache.plc4x.java.s7.readwrite.types) IntStream(java.util.stream.IntStream) S7StringField(org.apache.plc4x.java.s7.readwrite.field.S7StringField) java.util(java.util) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcUnsubscriptionResponse(org.apache.plc4x.java.api.messages.PlcUnsubscriptionResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcResponse(org.apache.plc4x.java.api.messages.PlcResponse) S7DriverContext(org.apache.plc4x.java.s7.readwrite.context.S7DriverContext) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ByteBuf(io.netty.buffer.ByteBuf) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) BiConsumer(java.util.function.BiConsumer) Logger(org.slf4j.Logger) DefaultPlcUnsubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcUnsubscriptionRequest) PlcField(org.apache.plc4x.java.api.model.PlcField) IEC61131ValueHandler(org.apache.plc4x.java.spi.values.IEC61131ValueHandler) Consumer(java.util.function.Consumer) DefaultPlcSubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) S7SubscriptionField(org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField) S7Field(org.apache.plc4x.java.s7.readwrite.field.S7Field) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) CompletableFuture(java.util.concurrent.CompletableFuture) S7Field(org.apache.plc4x.java.s7.readwrite.field.S7Field) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcValue(org.apache.plc4x.java.api.value.PlcValue)

Example 15 with PlcProtocolException

use of org.apache.plc4x.java.api.exceptions.PlcProtocolException 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)

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