com.digitalpetri.enip.cip.services.LargeForwardOpenService Maven / Gradle / Ivy
package com.digitalpetri.enip.cip.services;
import com.digitalpetri.enip.cip.CipResponseException;
import com.digitalpetri.enip.cip.epath.EPath.PaddedEPath;
import com.digitalpetri.enip.cip.epath.LogicalSegment.ClassId;
import com.digitalpetri.enip.cip.epath.LogicalSegment.InstanceId;
import com.digitalpetri.enip.cip.structs.LargeForwardOpenRequest;
import com.digitalpetri.enip.cip.structs.LargeForwardOpenResponse;
import com.digitalpetri.enip.cip.structs.MessageRouterRequest;
import com.digitalpetri.enip.cip.structs.MessageRouterResponse;
import io.netty.buffer.ByteBuf;
import io.netty.util.ReferenceCountUtil;
public class LargeForwardOpenService implements CipService {
public static final int SERVICE_CODE = 0x5B;
private static final PaddedEPath CONNECTION_MANAGER_PATH = new PaddedEPath(
new ClassId(0x06),
new InstanceId(0x01)
);
private final LargeForwardOpenRequest request;
public LargeForwardOpenService(LargeForwardOpenRequest request) {
this.request = request;
}
@Override
public void encodeRequest(ByteBuf buffer) {
MessageRouterRequest mrr = new MessageRouterRequest(
SERVICE_CODE,
CONNECTION_MANAGER_PATH,
this::encode
);
MessageRouterRequest.encode(mrr, buffer);
}
@Override
public LargeForwardOpenResponse decodeResponse(ByteBuf buffer) throws CipResponseException, PartialResponseException {
MessageRouterResponse mResponse = MessageRouterResponse.decode(buffer);
int generalStatus = mResponse.getGeneralStatus();
if (generalStatus == 0x00) {
try {
return LargeForwardOpenResponse.decode(mResponse.getData());
} finally {
ReferenceCountUtil.release(mResponse.getData());
}
} else {
throw new CipResponseException(generalStatus, mResponse.getAdditionalStatus());
}
}
private void encode(ByteBuf buffer) {
LargeForwardOpenRequest.encode(request, buffer);
}
}