Search in sources :

Example 6 with AmsPacket

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

the class Ads2PayloadProtocolTest method roundTrip.

@Test
public void roundTrip() throws Exception {
    ArrayList<Object> outbound = new ArrayList<>();
    SUT.encode(null, amsPacket, outbound);
    assertEquals(1, outbound.size());
    assertThat(outbound, hasSize(1));
    assertThat(outbound.get(0), instanceOf(ByteBuf.class));
    ByteBuf byteBuf = (ByteBuf) outbound.get(0);
    ArrayList<Object> inbound = new ArrayList<>();
    SUT.decode(null, byteBuf, inbound);
    assertEquals(1, inbound.size());
    assertThat(inbound, hasSize(1));
    assertThat(inbound.get(0), instanceOf(AmsPacket.class));
    AmsPacket inboundAmsPacket = (AmsPacket) inbound.get(0);
    assertThat("inbound divers from outbound", this.amsPacket, equalTo(inboundAmsPacket));
}
Also used : ArrayList(java.util.ArrayList) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 7 with AmsPacket

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

the class Plc4x2AdsProtocolTest method encode.

@Test
public void encode() throws Exception {
    assumeThat(payloadClazzName + " not yet implemented", notYetSupportedDataType, not(hasItem(payloadClazzName)));
    ArrayList<Object> out = new ArrayList<>();
    SUT.encode(null, plcRequestContainer, out);
    assertThat(out, hasSize(1));
    assertThat(out.get(0), instanceOf(AmsPacket.class));
    AmsPacket amsPacket = (AmsPacket) out.get(0);
    LOGGER.info("{}\nHexDump:\n{}", amsPacket, amsPacket.dump());
    if (amsPacket instanceof AdsWriteRequest) {
        AdsWriteRequest adsWriteRequest = (AdsWriteRequest) amsPacket;
        byte[] value = adsWriteRequest.getData().getBytes();
        if (payloadClazzName.equals(Boolean.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x1 }));
        } else if (payloadClazzName.equals(Byte.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x1 }));
        } else if (payloadClazzName.equals(Short.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x1, 0x0 }));
        } else if (payloadClazzName.equals(Integer.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x1, 0x0, 0x0, 0x0 }));
        } else if (payloadClazzName.equals(Long.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x1, 0x0, 0x0, 0x0 }));
        } else if (payloadClazzName.equals(BigInteger.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x1, 0x0, 0x0, 0x0 }));
        } else if (payloadClazzName.equals(Float.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x0, 0x0, (byte) 0x80, 0x3F }));
        } else if (payloadClazzName.equals(Double.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F }));
        } else if (payloadClazzName.equals(BigDecimal.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, (byte) 0xF0, 0x3F }));
        } else if (payloadClazzName.equals(String.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
        } else if (payloadClazzName.equals(LocalTime.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
        } else if (payloadClazzName.equals(LocalDate.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
        } else if (payloadClazzName.equals(LocalDateTime.class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
        } else if (payloadClazzName.equals(byte[].class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
        } else if (payloadClazzName.equals(Byte[].class.getSimpleName())) {
            assertThat(value, equalTo(new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00 }));
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) AdsWriteRequest(org.apache.plc4x.java.ads.api.commands.AdsWriteRequest) BigDecimal(java.math.BigDecimal) AtomicLong(java.util.concurrent.atomic.AtomicLong) AmsPacket(org.apache.plc4x.java.ads.api.generic.AmsPacket) Test(org.junit.Test)

Example 8 with AmsPacket

use of org.apache.plc4x.java.ads.api.generic.AmsPacket 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 9 with AmsPacket

use of org.apache.plc4x.java.ads.api.generic.AmsPacket 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 10 with AmsPacket

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

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