Search in sources :

Example 1 with AmsHeader

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

the class Plc4x2AdsProtocolTest method syncInvoiceId.

private void syncInvoiceId() throws Exception {
    Field correlationBuilderField = SUT.getClass().getDeclaredField("correlationBuilder");
    correlationBuilderField.setAccessible(true);
    AtomicLong correlationBuilder = (AtomicLong) correlationBuilderField.get(SUT);
    AmsHeader amsHeader = amsPacket.getAmsHeader();
    Field invokeIdField = amsHeader.getClass().getDeclaredField("invokeId");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(invokeIdField, invokeIdField.getModifiers() & ~Modifier.FINAL);
    invokeIdField.setAccessible(true);
    invokeIdField.set(amsHeader, Invoke.of(correlationBuilder.get()));
}
Also used : Field(java.lang.reflect.Field) AtomicLong(java.util.concurrent.atomic.AtomicLong) AmsHeader(org.apache.plc4x.java.ads.api.generic.AmsHeader)

Example 2 with AmsHeader

use of org.apache.plc4x.java.ads.api.generic.AmsHeader 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)

Aggregations

AmsHeader (org.apache.plc4x.java.ads.api.generic.AmsHeader)2 ByteBuf (io.netty.buffer.ByteBuf)1 Field (java.lang.reflect.Field)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AmsPacket (org.apache.plc4x.java.ads.api.generic.AmsPacket)1 AdsProtocolOverflowException (org.apache.plc4x.java.ads.protocol.exception.AdsProtocolOverflowException)1