use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class EipProtocolLogic method decodeReadResponse.
private PlcResponse decodeReadResponse(CipService p, PlcReadRequest readRequest) {
Map<String, ResponseItem<PlcValue>> values = new HashMap<>();
// only 1 field
if (p instanceof CipReadResponse) {
CipReadResponse resp = (CipReadResponse) p;
String fieldName = readRequest.getFieldNames().iterator().next();
EipField field = (EipField) readRequest.getField(fieldName);
PlcResponseCode code = decodeResponseCode(resp.getStatus());
PlcValue plcValue = null;
CIPDataTypeCode type = resp.getDataType();
ByteBuf data = Unpooled.wrappedBuffer(resp.getData());
if (code == PlcResponseCode.OK) {
plcValue = parsePlcValue(field, data, type);
}
ResponseItem<PlcValue> result = new ResponseItem<>(code, plcValue);
values.put(fieldName, result);
} else // Multiple response
if (p instanceof MultipleServiceResponse) {
MultipleServiceResponse responses = (MultipleServiceResponse) p;
int nb = responses.getServiceNb();
List<CipService> arr = new ArrayList<>(nb);
ReadBufferByteBased read = new ReadBufferByteBased(responses.getServicesData(), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
int total = (int) read.getTotalBytes();
for (int i = 0; i < nb; i++) {
int length = 0;
// Substract first offset as we only have the service in the buffer (not servicesNb and offsets)
int offset = responses.getOffsets().get(i) - responses.getOffsets().get(0);
if (i == nb - 1) {
// Get the rest if last
length = total - offset;
} else {
// Calculate length with offsets (substracting first offset)
length = responses.getOffsets().get(i + 1) - offset - responses.getOffsets().get(0);
}
ReadBuffer serviceBuf = new ReadBufferByteBased(read.getBytes(offset, offset + length), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
CipService service = null;
try {
service = CipService.staticParse(read, length);
arr.add(service);
} catch (ParseException e) {
throw new PlcRuntimeException(e);
}
}
Services services = new Services(nb, responses.getOffsets(), arr, -1);
Iterator<String> it = readRequest.getFieldNames().iterator();
for (int i = 0; i < nb && it.hasNext(); i++) {
String fieldName = it.next();
EipField field = (EipField) readRequest.getField(fieldName);
PlcValue plcValue = null;
if (services.getServices().get(i) instanceof CipReadResponse) {
CipReadResponse readResponse = (CipReadResponse) services.getServices().get(i);
PlcResponseCode code;
if (readResponse.getStatus() == 0) {
code = PlcResponseCode.OK;
} else {
code = PlcResponseCode.INTERNAL_ERROR;
}
CIPDataTypeCode type = readResponse.getDataType();
ByteBuf data = Unpooled.wrappedBuffer(readResponse.getData());
if (code == PlcResponseCode.OK) {
plcValue = parsePlcValue(field, data, type);
}
ResponseItem<PlcValue> result = new ResponseItem<>(code, plcValue);
values.put(fieldName, result);
}
}
}
return new DefaultPlcReadResponse(readRequest, values);
}
use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class Df1Protocol method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
DF1Symbol resp;
// do {
in.markReaderIndex();
short size = 0x00;
// Yes, it's a little complicated, but we need to find out if we've got enough data.
if (in.readableBytes() > 1) {
if (in.getUnsignedByte(0) != (short) 0x10) {
logger.warn("Expecting DF1 magic number: {}", 0x10);
if (logger.isDebugEnabled()) {
logger.debug("Got Data: {}", ByteBufUtil.hexDump(in));
}
exceptionCaught(ctx, new PlcProtocolException(String.format("Expecting DF1 magic number: %02X", 0x10)));
return;
}
short symbolType = in.getUnsignedByte(1);
switch(symbolType) {
case (short) 0x02:
{
if (in.readableBytes() < 5) {
return;
}
short commandType = in.getUnsignedByte(4);
switch(commandType) {
case (short) 0x01:
{
if (in.readableBytes() < 11) {
return;
}
break;
}
case (short) 0x41:
{
// TODO: Let's just assume all is good for now ...
break;
}
}
break;
}
case (short) 0x03:
{
if (in.readableBytes() < 4) {
return;
}
break;
}
}
}
// Parse the message received from the DF1 device
byte[] data = new byte[in.readableBytes()];
in.readBytes(data);
ReadBuffer readBuffer = new ReadBufferByteBased(data);
resp = DF1Symbol.staticParse(readBuffer);
// } while (readWasSucessfull);
// // TODO if unableto read
// in.resetReaderIndex();
// Add the received message to the output
out.add(resp);
}
use of org.apache.plc4x.java.spi.generation.ReadBuffer in project plc4x by apache.
the class ManualBacNetDecoder method main.
public static void main(String[] args) throws Exception {
final byte[] bytes = Hex.decodeHex("810a002b01040205790109011c020000142c000002f93a06b24e09552e44434a00002f096f2e8204002f4f");
ReadBuffer readBuffer = new ReadBufferByteBased(bytes);
final BVLC packet = BVLC.staticParse(readBuffer);
System.out.println(packet);
}
Aggregations