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);
}
}
}
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;
}
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()));
}
}
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);
}
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());
}
}
Aggregations