Search in sources :

Example 1 with AmsPacket

use of org.apache.plc4x.java.ads.api.generic.AmsPacket in project plc4x by apache.

the class Ads2PayloadProtocol method handADSDeleteDeviceNotificationCommand.

private AmsPacket handADSDeleteDeviceNotificationCommand(State stateId, ByteBuf commandBuffer, AmsHeader amsHeader) {
    AmsPacket amsPacket;
    if (stateId.isRequest()) {
        NotificationHandle notificationHandle = NotificationHandle.of(commandBuffer);
        amsPacket = AdsDeleteDeviceNotificationRequest.of(amsHeader, notificationHandle);
    } else {
        Result result = Result.of(commandBuffer);
        amsPacket = AdsDeleteDeviceNotificationResponse.of(amsHeader, result);
    }
    return amsPacket;
}
Also used : AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket)

Example 2 with AmsPacket

use of org.apache.plc4x.java.ads.api.generic.AmsPacket in project plc4x by apache.

the class Ads2PayloadProtocol method handleADSReadDeviceInfoCommand.

private AmsPacket handleADSReadDeviceInfoCommand(State stateId, ByteBuf commandBuffer, AmsHeader amsHeader) {
    AmsPacket amsPacket;
    if (stateId.isRequest()) {
        amsPacket = AdsReadDeviceInfoRequest.of(amsHeader);
    } else {
        Result result = Result.of(commandBuffer);
        MajorVersion majorVersion = MajorVersion.of(commandBuffer);
        MinorVersion minorVersion = MinorVersion.of(commandBuffer);
        Version version = Version.of(commandBuffer);
        Device device = Device.of(commandBuffer);
        amsPacket = AdsReadDeviceInfoResponse.of(amsHeader, result, majorVersion, minorVersion, version, device);
    }
    return amsPacket;
}
Also used : AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket)

Example 3 with AmsPacket

use of org.apache.plc4x.java.ads.api.generic.AmsPacket in project plc4x by apache.

the class Ads2PayloadProtocol method handleADSWriteCommand.

private AmsPacket handleADSWriteCommand(State stateId, ByteBuf commandBuffer, AmsHeader amsHeader) {
    AmsPacket amsPacket;
    if (stateId.isRequest()) {
        IndexGroup indexGroup = IndexGroup.of(commandBuffer);
        IndexOffset indexOffset = IndexOffset.of(commandBuffer);
        Length length = Length.of(commandBuffer);
        if (length.getAsLong() > Integer.MAX_VALUE) {
            throw new AdsProtocolOverflowException(Integer.class, length.getAsLong());
        }
        if (length.getAsLong() > ADS_WRITE_COMMAND_MAX_BYTES) {
            throw new AdsProtocolOverflowException("ADS_WRITE_COMMAND_MAX_BYTES", ADS_WRITE_COMMAND_MAX_BYTES, length.getAsLong());
        }
        byte[] dataToRead = new byte[(int) length.getAsLong()];
        commandBuffer.readBytes(dataToRead);
        Data data = Data.of(dataToRead);
        amsPacket = AdsWriteRequest.of(amsHeader, indexGroup, indexOffset, length, data);
    } else {
        Result result = Result.of(commandBuffer);
        amsPacket = AdsWriteResponse.of(amsHeader, result);
    }
    return amsPacket;
}
Also used : AdsProtocolOverflowException(org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket)

Example 4 with AmsPacket

use of org.apache.plc4x.java.ads.api.generic.AmsPacket in project plc4x by apache.

the class Plc4x2AdsProtocol method encodeWriteRequest.

private void encodeWriteRequest(PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws PlcException {
    InternalPlcWriteRequest writeRequest = (InternalPlcWriteRequest) msg.getRequest();
    if (writeRequest.getFields().size() != 1) {
        throw new PlcProtocolException("Only one item supported");
    }
    PlcField field = writeRequest.getFields().get(0);
    if (field instanceof SymbolicAdsField) {
        DirectAdsField mappedField = fieldMapping.get(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());
    Object[] plcValues;
    PlcValue plcValue = writeRequest.getPlcValues().get(0);
    if (plcValue instanceof PlcList) {
        plcValues = ((PlcList) plcValue).getList().toArray(new Object[0]);
    } else {
        plcValues = new Object[] { plcValue.getObject() };
    }
    byte[] bytes = encodeData(directAdsField.getAdsDataType(), plcValues);
    int bytesToBeWritten = bytes.length;
    int maxTheoreticalSize = directAdsField.getAdsDataType().getTargetByteSize() * directAdsField.getNumberOfElements();
    if (bytesToBeWritten > maxTheoreticalSize) {
        LOGGER.debug("Requested AdsDatatype {} is exceeded by number of bytes {}. Limit {}.", directAdsField.getAdsDataType(), bytesToBeWritten, maxTheoreticalSize);
        throw new PlcProtocolPayloadTooBigException("ADS", maxTheoreticalSize, bytesToBeWritten, plcValues);
    }
    Data data = Data.of(bytes);
    AmsPacket amsPacket = AdsWriteRequest.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, indexGroup, indexOffset, data);
    LOGGER.debug("encoded write 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) PlcField(org.apache.plc4x.java.api.model.PlcField) LittleEndianEncoder.encodeData(org.apache.plc4x.java.ads.protocol.util.LittleEndianEncoder.encodeData) Invoke(org.apache.plc4x.java.ads.api.generic.types.Invoke) PlcList(org.apache.plc4x.java.api.value.PlcList) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DirectAdsField(org.apache.plc4x.java.ads.model.DirectAdsField) PlcProtocolPayloadTooBigException(org.apache.plc4x.java.api.exceptions.PlcProtocolPayloadTooBigException) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket)

Example 5 with AmsPacket

use of org.apache.plc4x.java.ads.api.generic.AmsPacket in project plc4x by apache.

the class Plc4x2AdsProtocol method encodeProprietaryRequest.

private void encodeProprietaryRequest(PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws PlcProtocolException {
    PlcProprietaryRequest plcProprietaryRequest = (PlcProprietaryRequest) msg.getRequest();
    if (!(plcProprietaryRequest.getProprietaryRequest() instanceof AmsPacket)) {
        throw new PlcProtocolException("Unsupported proprietary type for this driver " + plcProprietaryRequest.getProprietaryRequest().getClass());
    }
    AmsPacket amsPacket = (AmsPacket) plcProprietaryRequest.getProprietaryRequest();
    LOGGER.debug("encoded proprietary request {}", amsPacket);
    out.add(amsPacket);
    requests.put(amsPacket.getAmsHeader().getInvokeId().getAsLong(), msg);
}
Also used : PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket)

Aggregations

AmsPacket (org.apache.plc4x.java.ads.api.generic.AmsPacket)16 AdsProtocolOverflowException (org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException)6 ByteBuf (io.netty.buffer.ByteBuf)3 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Invoke (org.apache.plc4x.java.ads.api.generic.types.Invoke)2 DirectAdsField (org.apache.plc4x.java.ads.model.DirectAdsField)2 SymbolicAdsField (org.apache.plc4x.java.ads.model.SymbolicAdsField)2 PlcField (org.apache.plc4x.java.api.model.PlcField)2 BigDecimal (java.math.BigDecimal)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AdsWriteRequest (org.apache.plc4x.java.ads.api.commands.AdsWriteRequest)1 AmsHeader (org.apache.plc4x.java.ads.api.generic.AmsHeader)1 AmsSerialAcknowledgeFrame (org.apache.plc4x.java.ads.api.serial.AmsSerialAcknowledgeFrame)1 AmsSerialFrame (org.apache.plc4x.java.ads.api.serial.AmsSerialFrame)1 AdsDataType (org.apache.plc4x.java.ads.model.AdsDataType)1