Search in sources :

Example 1 with AdsProtocolOverflowException

use of org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException 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 2 with AdsProtocolOverflowException

use of org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException in project plc4x by apache.

the class Ads2PayloadProtocol method handleADSReadWriteCommand.

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

Example 3 with AdsProtocolOverflowException

use of org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException in project plc4x by apache.

the class Ads2PayloadProtocol method handleADSWriteControlCommand.

private AmsPacket handleADSWriteControlCommand(State stateId, ByteBuf commandBuffer, AmsHeader amsHeader) {
    AmsPacket amsPacket;
    if (stateId.isRequest()) {
        AdsState adsState = AdsState.of(commandBuffer);
        DeviceState deviceState = DeviceState.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_CONTROL_COMMAND_MAX_BYTES) {
            throw new AdsProtocolOverflowException("ADS_WRITE_CONTROL_COMMAND_MAX_BYTES", ADS_WRITE_CONTROL_COMMAND_MAX_BYTES, length.getAsLong());
        }
        byte[] dataToRead = new byte[(int) length.getAsLong()];
        commandBuffer.readBytes(dataToRead);
        Data data = Data.of(dataToRead);
        amsPacket = AdsWriteControlRequest.of(amsHeader, adsState, deviceState, length, data);
    } else {
        Result result = Result.of(commandBuffer);
        amsPacket = AdsWriteControlResponse.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 AdsProtocolOverflowException

use of org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException in project plc4x by apache.

the class Ads2PayloadProtocol method decode.

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out) {
    if (byteBuf == Unpooled.EMPTY_BUFFER) {
        // Cleanup...
        reset();
        return;
    }
    LOGGER.trace("(-->IN): {}, {}, {}", channelHandlerContext, byteBuf, out);
    AmsNetId targetAmsNetId = AmsNetId.of(byteBuf);
    AmsPort targetAmsPort = AmsPort.of(byteBuf);
    AmsNetId sourceAmsNetId = AmsNetId.of(byteBuf);
    AmsPort sourceAmsPort = AmsPort.of(byteBuf);
    Command commandId = Command.of(byteBuf);
    State stateId = State.of(byteBuf);
    DataLength dataLength = DataLength.of(byteBuf);
    AmsError errorCode = AmsError.of(byteBuf);
    Invoke invoke = Invoke.of(byteBuf);
    AmsPacket correlatedAmsPacket = requests.remove(invoke);
    if (correlatedAmsPacket != null) {
        LOGGER.debug("Correlated packet received {}", correlatedAmsPacket);
    }
    if (dataLength.getAsLong() > Integer.MAX_VALUE) {
        throw new AdsProtocolOverflowException(Integer.class, dataLength.getAsLong());
    }
    ByteBuf commandBuffer = byteBuf.readBytes((int) dataLength.getAsLong());
    try {
        AmsHeader amsHeader = AmsHeader.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, commandId, stateId, dataLength, errorCode, invoke);
        final AmsPacket amsPacket;
        switch(commandId) {
            case INVALID:
                amsPacket = handleInvalidCommand(commandBuffer, amsHeader);
                break;
            case ADS_READ_DEVICE_INFO:
                amsPacket = handleADSReadDeviceInfoCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_READ:
                amsPacket = handleADSReadCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_WRITE:
                amsPacket = handleADSWriteCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_READ_STATE:
                amsPacket = handleADSReadStateCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_WRITE_CONTROL:
                amsPacket = handleADSWriteControlCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_ADD_DEVICE_NOTIFICATION:
                amsPacket = handleADSAddDeviceNotificationCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_DELETE_DEVICE_NOTIFICATION:
                amsPacket = handADSDeleteDeviceNotificationCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_DEVICE_NOTIFICATION:
                amsPacket = handleADSDeviceNotificationCommand(stateId, commandBuffer, amsHeader);
                break;
            case ADS_READ_WRITE:
                amsPacket = handleADSReadWriteCommand(stateId, commandBuffer, amsHeader);
                break;
            case UNKNOWN:
            default:
                amsPacket = handleUnknownCommand(commandBuffer, amsHeader);
        }
        LOGGER.debug("Received amsPacket {}", amsPacket);
        out.add(amsPacket);
        if (commandBuffer.readableBytes() > 0) {
            throw new IllegalStateException("Unread bytes left: " + commandBuffer.readableBytes());
        }
    } finally {
        commandBuffer.release();
    }
}
Also used : AdsProtocolOverflowException(org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException) AmsHeader(org.apache.plc4x.java.ads.api.generic.AmsHeader) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with AdsProtocolOverflowException

use of org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException in project plc4x by apache.

the class Ads2PayloadProtocol method handleADSDeviceNotificationCommand.

private AmsPacket handleADSDeviceNotificationCommand(State stateId, ByteBuf commandBuffer, AmsHeader amsHeader) {
    AmsPacket amsPacket;
    if (stateId.isRequest()) {
        Length length = Length.of(commandBuffer);
        if (length.getAsLong() > Integer.MAX_VALUE) {
            throw new AdsProtocolOverflowException(Integer.class, length.getAsLong());
        }
        Stamps stamps = Stamps.of(commandBuffer);
        if (stamps.getAsLong() > Integer.MAX_VALUE) {
            throw new AdsProtocolOverflowException(Integer.class, stamps.getAsLong());
        }
        // Note: the length includes the 4 Bytes of stamps which we read already so we substract.
        ByteBuf adsDeviceNotificationBuffer = commandBuffer.readBytes((int) length.getAsLong() - Stamps.NUM_BYTES);
        try {
            List<AdsStampHeader> adsStampHeaders = new ArrayList<>((int) stamps.getAsLong());
            if (stamps.getAsLong() > MAX_NUM_STAMPS) {
                throw new AdsProtocolOverflowException("MAX_NUM_STAMPS", MAX_NUM_STAMPS, length.getAsLong());
            }
            for (int i = 1; i <= stamps.getAsLong(); i++) {
                AdsStampHeader adsStampHeader = handleStampHeader(adsDeviceNotificationBuffer);
                adsStampHeaders.add(adsStampHeader);
            }
            amsPacket = AdsDeviceNotificationRequest.of(amsHeader, length, stamps, adsStampHeaders);
        } finally {
            adsDeviceNotificationBuffer.release();
        }
    } else {
        amsPacket = UnknownCommand.of(amsHeader, commandBuffer);
    }
    return amsPacket;
}
Also used : AdsProtocolOverflowException(org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException) ArrayList(java.util.ArrayList) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

AmsPacket (org.apache.plc4x.java.ads.api.generic.AmsPacket)6 AdsProtocolOverflowException (org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException)6 ByteBuf (io.netty.buffer.ByteBuf)2 ArrayList (java.util.ArrayList)1 AmsHeader (org.apache.plc4x.java.ads.api.generic.AmsHeader)1